]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/API2Tools.pm
api2: network: improve code readability
[pve-manager.git] / PVE / API2Tools.pm
index 2956b5ad8ac7b0cc1c588d473441ae46e501bf60..a3d7ca84cd3f9d5eee6d4bdd3f6791fb457f005c 100644 (file)
@@ -8,15 +8,18 @@ use PVE::Exception qw(raise_param_exc);
 use PVE::Tools;
 use PVE::INotify;
 use PVE::Cluster;
+use PVE::DataCenterConfig;
+use PVE::RPCEnvironment;
 use Digest::MD5 qw(md5_hex);
 use URI;
 use URI::Escape;
 use PVE::SafeSyslog;
+use PVE::Storage::Plugin;
 
 my $hwaddress;
 
 sub get_hwaddress {
-    
+
     return $hwaddress if defined ($hwaddress);
 
     my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
@@ -27,7 +30,7 @@ sub get_hwaddress {
 }
 
 sub extract_node_stats {
-    my ($node, $members, $rrd) = @_;
+    my ($node, $members, $rrd, $exclude_stats) = @_;
 
     my $entry = {
        id => "node/$node",
@@ -37,19 +40,23 @@ sub extract_node_stats {
     };
 
     if (my $d = $rrd->{"pve2-node/$node"}) {
-                   
+
        if (!$members || # no cluster
            ($members->{$node} && $members->{$node}->{online})) {
-           $entry->{uptime} = ($d->[0] || 0) + 0;
-           $entry->{cpu} = ($d->[5] || 0) + 0;
-           $entry->{mem} = ($d->[8] || 0) + 0;
-           $entry->{disk} = ($d->[12] || 0) + 0;
+           if (!$exclude_stats) {
+               $entry->{uptime} = ($d->[0] || 0) + 0;
+               $entry->{cpu} = ($d->[5] || 0) + 0;
+               $entry->{mem} = ($d->[8] || 0) + 0;
+               $entry->{disk} = ($d->[12] || 0) + 0;
+           }
            $entry->{status} = 'online';
        }
        $entry->{level} = $d->[1];
-       $entry->{maxcpu} = ($d->[4] || 0) + 0;
-       $entry->{maxmem} = ($d->[7] || 0) + 0;
-       $entry->{maxdisk} = ($d->[11] || 0) + 0;
+       if (!$exclude_stats) {
+           $entry->{maxcpu} = ($d->[4] || 0) + 0;
+           $entry->{maxmem} = ($d->[7] || 0) + 0;
+           $entry->{maxdisk} = ($d->[11] || 0) + 0;
+       }
     }
 
     if ($members && $members->{$node} &&
@@ -65,9 +72,10 @@ sub extract_vm_stats {
 
     my $entry = {
        id => "$data->{type}/$vmid",
-       vmid => $vmid + 0, 
+       vmid => $vmid + 0,
        node => $data->{node},
        type => $data->{type},
+       status => 'unknown',
     };
 
     my $d;
@@ -87,7 +95,7 @@ sub extract_vm_stats {
        $entry->{netout} = ($d->[10] || 0) + 0;
        $entry->{diskread} = ($d->[11] || 0) + 0;
        $entry->{diskwrite} = ($d->[12] || 0) + 0;
-    
+
     } elsif ($d = $rrd->{"pve2.3-vm/$vmid"}) {
 
        $entry->{uptime} = ($d->[0] || 0) + 0;
@@ -108,25 +116,32 @@ sub extract_vm_stats {
     };
 
     return $entry;
-};
+}
 
 sub extract_storage_stats {
     my ($storeid, $scfg, $node, $rrd) = @_;
 
+    my $content = PVE::Storage::Plugin::content_hash_to_string($scfg->{content});
+
     my $entry = {
        id => "storage/$node/$storeid",
-       storage => $storeid, 
-       node => $node, 
-       type => 'storage', 
-    }; 
+       storage => $storeid,
+       node => $node,
+       type => 'storage',
+       plugintype => $scfg->{type},
+       status => 'unknown',
+       shared => $scfg->{shared} || 0,
+       content => $content,
+    };
 
     if (my $d = $rrd->{"pve2-storage/$node/$storeid"}) {
        $entry->{maxdisk} = ($d->[1] || 0) + 0;
        $entry->{disk} = ($d->[2] || 0) + 0;
+       $entry->{status} = 'available';
     }
 
     return $entry;
-};
+}
 
 sub parse_http_proxy {
     my ($proxyenv) = @_;
@@ -156,14 +171,14 @@ sub run_spiceterm {
     my $nodename = PVE::INotify::nodename();
     my $family = PVE::Tools::get_host_address_family($nodename);
     my $port = PVE::Tools::next_spice_port($family);
-    
-    my ($ticket, undef, $remote_viewer_config) = 
+
+    my ($ticket, undef, $remote_viewer_config) =
        PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port);
 
-    my $timeout = 40; 
+    my $timeout = 40;
 
-    my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', '127.0.0.1',
-              '--timeout', $timeout, '--authpath', $authpath, 
+    my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', 'localhost',
+              '--timeout', $timeout, '--authpath', $authpath,
               '--permissions', $permissions];
 
     my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
@@ -207,47 +222,6 @@ sub run_spiceterm {
     return $remote_viewer_config;
 }
 
-sub read_proxy_config {
-
-    my $conffile = "/etc/default/pveproxy";
-
-    # Note: evaluate with bash 
-    my $shcmd = ". $conffile;\n";
-    $shcmd .= 'echo \"ALLOW_FROM:\$ALLOW_FROM\";';
-    $shcmd .= 'echo \"DENY_FROM:\$DENY_FROM\";';
-    $shcmd .= 'echo \"POLICY:\$POLICY\";';
-    $shcmd .= 'echo \"CIPHERS:\$CIPHERS\";';
-    $shcmd .= 'echo \"DHPARAMS:\$DHPARAMS\";';
-
-    my $data = -f $conffile ? `bash -c "$shcmd"` : '';
-
-    my $res = {};
-
-    while ($data =~ s/^(.*)\n//) {
-       my ($key, $value) = split(/:/, $1, 2);
-       next if !$value;
-       if ($key eq 'ALLOW_FROM' || $key eq 'DENY_FROM') {
-           my $ips = [];
-           foreach my $ip (split(/,/, $value)) {
-               $ip = "0/0" if $ip eq 'all';
-               push @$ips, Net::IP->new($ip) || die Net::IP::Error() . "\n";
-           }
-           $res->{$key} = $ips;
-       } elsif ($key eq 'POLICY') {
-           die "unknown policy '$value'\n" if $value !~ m/^(allow|deny)$/;
-           $res->{$key} = $value;
-       } elsif ($key eq 'CIPHERS') {
-           $res->{$key} = $value;
-       } elsif ($key eq 'DHPARAMS') {
-           $res->{$key} = $value;
-       } else {
-           # silently skip everythin else?
-       }
-    }
-
-    return $res;
-}
-
 sub resolve_proxyto {
     my ($rpcenv, $proxyto_callback, $proxyto, $uri_param) = @_;
 
@@ -258,10 +232,27 @@ sub resolve_proxyto {
            if !$node;
     } else {
        $node = $uri_param->{$proxyto};
-       raise_param_exc({ $proxyto =>  "proxyto parameter does not exists"})
+       raise_param_exc({ $proxyto =>  "proxyto parameter does not exist"})
            if !$node;
     }
     return $node;
 }
 
+sub get_resource_pool_guest_members {
+    my ($pool) = @_;
+
+    my $usercfg = PVE::Cluster::cfs_read_file("user.cfg");
+
+    my $vmlist = PVE::Cluster::get_vmlist() || {};
+    my $idlist = $vmlist->{ids} || {};
+
+    my $data = $usercfg->{pools}->{$pool};
+
+    die "pool '$pool' does not exist\n" if !$data;
+
+    my $pool_members = [ grep { $idlist->{$_} } keys %{$data->{vms}} ];
+
+    return $pool_members;
+}
+
 1;