[pve-devel] r5095 - in qemu-server/pve2: . PVE/API2

svn-commits at proxmox.com svn-commits at proxmox.com
Tue Sep 7 12:11:20 CEST 2010


Author: dietmar
Date: 2010-09-07 10:11:20 +0000 (Tue, 07 Sep 2010)
New Revision: 5095

Added:
   qemu-server/pve2/nqm
Modified:
   qemu-server/pve2/ChangeLog
   qemu-server/pve2/PVE/API2/QemuServer.pm
   qemu-server/pve2/QemuServer.pm
Log:
	* nqm: temporary file to test new API - will replace 'qm' later.

	* QemuServer.pm (vmstatus): moved from PVE::Qemu.pm



Modified: qemu-server/pve2/ChangeLog
===================================================================
--- qemu-server/pve2/ChangeLog	2010-09-07 08:33:21 UTC (rev 5094)
+++ qemu-server/pve2/ChangeLog	2010-09-07 10:11:20 UTC (rev 5095)
@@ -1,3 +1,9 @@
+2010-09-07  Proxmox Support Team  <support at proxmox.com>
+
+	* nqm: temporary file to test new API - will replace 'qm' later.
+
+	* QemuServer.pm (vmstatus): moved from PVE::Qemu.pm
+
 2010-08-26  Proxmox Support Team  <support at proxmox.com>
 
 	* PVE/*: created directory hierachy for library compoments

Modified: qemu-server/pve2/PVE/API2/QemuServer.pm
===================================================================
--- qemu-server/pve2/PVE/API2/QemuServer.pm	2010-09-07 08:33:21 UTC (rev 5094)
+++ qemu-server/pve2/PVE/API2/QemuServer.pm	2010-09-07 10:11:20 UTC (rev 5095)
@@ -4,15 +4,16 @@
 use warnings;
 
 use PVE::RESTHandler;
+use PVE::QemuServer;
 
 # API URLs
 #
-# GET    /vms/
-# GET    /vms/index  status of all VMs (cluster wide)
+## GET    /vms/
+## GET    /vms/index  status of all VMs (cluster wide)
 # GET    /vms/qm  status of all Qemu VMs (cluster wide)
 # GET    /vms/qm/{node}/  status of all Qemu VMs (per node)
 # POST   /vms/qm/{node}/  create VM
-# GET    /vms/qm/{node}/{vmid}  status of all Qemu VMs (per node)
+# GET    /vms/qm/{node}/{vmid}  get VM config
 # PUT    /vms/qm/{node}/{vmid}  set VM config/status
 # DELETE /vms/qm/{node}/{vmid}  destroy VM
 
@@ -22,7 +23,7 @@
     name => 'index', 
     path => '', 
     method => 'GET',
-    description => "Virtual machine index.",
+    description => "Virtual machine index (cluster wide).",
     parameters => {
     	additionalProperties => 0,
 	properties => {},
@@ -33,13 +34,44 @@
 	    type => "object",
 	    properties => {},
 	},
-	links => [ { rel => 'child', href => "{id}" } ],
+	links => [ { rel => 'child', href => "{vmid}" } ],
     },
     code => sub {
 	my ($param) = @_;
 
-	return [{ id => 100 }];
+	# fixme: not implemented
+	die "not implemented";
 
+	return [{ vmid => 100 }];
+
     }});
 
+__PACKAGE__->register_method ({
+    name => 'vmlist', 
+    path => '{node}', 
+    method => 'GET',
+    description => "Virtual machine index.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => { type => 'string', format => 'pve-node' },
+	},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => {},
+	},
+	links => [ { rel => 'child', href => "{vmid}" } ],
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $vmstatus =  PVE::QemuServer::vmstatus();
+
+	return PVE::RESTHandler::hash_to_array($vmstatus, 'vmid');
+
+    }});
+
 1;

Modified: qemu-server/pve2/QemuServer.pm
===================================================================
--- qemu-server/pve2/QemuServer.pm	2010-09-07 08:33:21 UTC (rev 5094)
+++ qemu-server/pve2/QemuServer.pm	2010-09-07 10:11:20 UTC (rev 5095)
@@ -18,9 +18,13 @@
 use Fcntl;
 use Sys::Syslog;
 use PVE::Storage;
-use PVE::Tools qw(run_command lock_file);
+use PVE::Tools qw(run_command lock_file file_read_firstline);
 use PVE::INotify qw(read_file);
+use PVE::ProcFSTools;
+use Time::HiRes qw (gettimeofday);
 
+my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK);
+
 # Note about locking: we use flock on the config file protect 
 # against concurent actions.
 # Adiitionaly, we have a 'lock' setting in the config file. This
@@ -1167,6 +1171,107 @@
     return $vzlist;
 }
 
+
+my $last_proc_pid_stat;
+
+sub vmstatus {
+    my $res = {};
+
+    my $list =  vzlist();
+    my ($uptime) = PVE::ProcFSTools::read_proc_uptime();
+
+
+    foreach my $veid (keys %$list) {
+	my $fn = config_file ($veid);
+	my $conf = read_file ($fn);
+
+	my $d = {};
+	$d->{type} = 'qemu';
+	$d->{nproc} = 0;
+	$d->{pid} = $list->{$veid}->{pid};
+	# fixme: better status?
+	$d->{status} = $list->{$veid}->{pid} ? 'running' : 'stopped';
+	$d->{ip} = '-';
+	$d->{disk} = 0;
+	$d->{cpus} = ($conf->{sockets} || 1) * ($conf->{cores} || 1);
+	$d->{name} = $conf->{name} || "VM$veid";
+
+	$d->{maxmem} = $conf->{memory} ? $conf->{memory} : 0; 
+
+	$d->{maxdisk} = $conf->{disksize} ? int($conf->{disksize}*1024) : 0;
+
+	$d->{uptime} = 0;
+	$d->{pctcpu} = 0;
+	$d->{relcpu} = 0;
+	$d->{mem} = 0;
+
+	$res->{$veid} = $d;
+
+    }
+
+    my $cpuinfo = PVE::ProcFSTools::get_cpu_info();
+    my $cpucount = $cpuinfo->{cpus} || 1;
+    my $ctime = gettimeofday;
+
+    foreach my $veid (keys %$list) {
+
+	my $d = $res->{$veid};
+	my $pid = $d->{pid};
+	next if !$pid;
+
+	my $statstr = file_read_firstline("/proc/$pid/stat");
+	next if !$statstr;
+
+	my ($utime, $stime, $vsize, $rss, $starttime);
+	if ($statstr =~ m/^$pid \(.*\) \S (-?\d+) -?\d+ -?\d+ -?\d+ -?\d+ \d+ \d+ \d+ \d+ \d+ (\d+) (\d+) (-?\d+) (-?\d+) -?\d+ -?\d+ -?\d+ 0 (\d+) (\d+) (-?\d+) \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ -?\d+ -?\d+ \d+ \d+ \d+/) {
+	    ($utime, $stime, $vsize, $rss, $starttime) = ($2, $3, $7, $8 * 4096, $6);
+	} else {
+	    next;
+	}
+ 
+	my $used = $utime + $stime;
+
+	my $vcpus = $d->{cpus} > $cpucount ? $cpucount : $d->{cpus};
+
+	$d->{uptime} = int ($uptime - ($starttime/100));
+
+	if ($vsize) {
+	    $d->{mem} = int (($rss/$vsize)*$d->{maxmem});
+	}
+
+	my $old = $last_proc_pid_stat->{$pid};
+	if (!$old) {
+	    $last_proc_pid_stat->{$pid} = { 
+		time => $ctime, 
+		used => $used,
+		pctcpu => 0,
+		relcpu => 0,
+	    };
+	    next;
+	}
+
+	my $dtime = ($ctime -  $old->{time}) * $cpucount * $clock_ticks;
+
+	if ($dtime > 1000) {
+	    my $dutime = $used -  $old->{used};
+
+	    $d->{pctcpu} = int((($dutime)*100)/$dtime);
+	    $d->{relcpu} = int (($d->{pctcpu} * $cpucount) / $vcpus);
+	    $last_proc_pid_stat->{$pid} = {
+		time => $ctime, 
+		used => $used,
+		pctcpu => $d->{pctcpu},
+		relcpu => $d->{relcpu},
+	    };
+	} else {
+	    $d->{pctcpu} = $old->{pctcpu};
+	    $d->{relcpu} = $old->{relcpu};
+	}
+    }
+
+    return $res;
+}
+
 sub config_to_command {
     my ($self, $vmid, $conf, $defaults, $migrate_uri) = @_;
 
@@ -2029,17 +2134,6 @@
 
 # pci helpers
 
-sub file_read_firstline {
-    my ($filename) = @_;
-
-    my $fh = IO::File->new ($filename, "r");
-    return undef if !$fh;
-    my $res = <$fh>;
-    chomp $res;
-    $fh->close;
-    return $res;
-}
-
 sub file_write {
     my ($filename, $buf) = @_;
 

Added: qemu-server/pve2/nqm
===================================================================
--- qemu-server/pve2/nqm	                        (rev 0)
+++ qemu-server/pve2/nqm	2010-09-07 10:11:20 UTC (rev 5095)
@@ -0,0 +1,54 @@
+#!/usr/bin/perl -w
+
+use strict;
+use Getopt::Long;
+use Fcntl ':flock';
+use File::Path;
+
+use PVE::INotify qw(read_file);
+use PVE::RPCEnvironment;
+use PVE::QemuServer;
+use PVE::API2::QemuServer;
+
+use PVE::CLIHandler;
+
+use base qw(PVE::CLIHandler);
+
+$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+
+die "please run as root\n" if $> != 0;
+
+PVE::INotify::inotify_init();
+
+my $rpcenv = PVE::RPCEnvironment->init('cli');
+
+$rpcenv->set_language($ENV{LANG});
+
+$rpcenv->set_user('root'); 
+
+my $hostname = read_file('hostname');
+
+my $cmddef = {
+    list => [ "PVE::API2::QemuServer", 'vmlist', [],
+	     { node => $hostname }, sub {
+		 my $vmlist = shift;
+
+		 exit 0 if (!scalar(@$vmlist));
+
+		 printf "%10s %-20s %-10s %-10s %12s %-10s\n", 
+		 qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID);
+
+		 foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) {
+		     printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, 
+		     $rec->{status}, $rec->{maxmem}, $rec->{maxdisk}||0, $rec->{pid}||0;
+		 }
+
+		 
+	      } ],
+};
+
+my $cmd = shift;
+
+PVE::CLIHandler::handle_cmd($cmddef, "qm", $cmd, \@ARGV);
+
+exit 0;


Property changes on: qemu-server/pve2/nqm
___________________________________________________________________
Added: svn:executable
   + *




More information about the pve-devel mailing list