Commit 463cd35a authored by Daniel P. Berrange's avatar Daniel P. Berrange

Expanded examples to demo more capabilities

parent caaaa583
...@@ -4,14 +4,23 @@ use Sys::Virt; ...@@ -4,14 +4,23 @@ use Sys::Virt;
my $con = Sys::Virt->new(address => "", readonly => 1); my $con = Sys::Virt->new(address => "", readonly => 1);
print "VMM yype: ", $con->get_type(), "\n"; print "VMM type: ", $con->get_type(), "\n";
print "Node: {\n";
my $ninfo = $con->get_node_info;
foreach (sort { $a cmp $b } keys %{$ninfo}) {
print " ", $_, ": ", $ninfo->{$_}, "\n";
}
print "}\n";
print "libvirt Version: ", $con->get_major_version(), ".", $con->get_minor_version(), ".", $con->get_micro_version(), "\n"; print "libvirt Version: ", $con->get_major_version(), ".", $con->get_minor_version(), ".", $con->get_micro_version(), "\n";
foreach my $dom (sort { $a->get_id <=> $b->get_id } $con->list_domains) { foreach my $dom (sort { $a->get_id <=> $b->get_id } $con->list_domains) {
print "Domain: ", $dom->get_id(), " " , $dom->get_name(), "\n"; print "Domain: {\n";
print " UUID: ", $dom->get_uuid(), "\n"; print " ID: ", $dom->get_id(), " '" , $dom->get_name(), "'\n";
print " UUID: ", $dom->get_uuid_string(), "\n";
my $info = $dom->get_info; my $info = $dom->get_info;
foreach (sort { $a cmp $b } keys %{$info}) { foreach (sort { $a cmp $b } keys %{$info}) {
print " ", $_, ": ", $info->{$_}, "\n"; print " ", $_, ": ", $info->{$_}, "\n";
} }
print "}\n";
} }
#!/usr/bin/perl
use strict;
use warnings;
use Sys::Virt;
use Sys::Virt::Domain;
use Time::HiRes qw(time);
my $hv = Sys::Virt->new(readonly => 1);
my $interval = @ARGV ? shift @ARGV : 1;
my $iterations = @ARGV ? shift @ARGV : 1;
my %states = (
&Sys::Virt::Domain::STATE_NOSTATE => "nostate",
&Sys::Virt::Domain::STATE_RUNNING => "running",
&Sys::Virt::Domain::STATE_BLOCKED => "blocked",
&Sys::Virt::Domain::STATE_PAUSED => "paused",
&Sys::Virt::Domain::STATE_SHUTDOWN => "shutdown",
&Sys::Virt::Domain::STATE_SHUTOFF => "shutoff",
&Sys::Virt::Domain::STATE_CRASHED => "crashed",
&Sys::Virt::Domain::STATE_RUNNING => "running",
);
my %cpuTime;
my $sample;
for (my $i = 0 ; $i < $iterations ; $i++) {
sleep $interval if $i;
my $now = time;
my @domains = $hv->list_domains;
my @stats;
if (!($i % 10)) {
printf " %-4s %-15s %-8s %-6s %-4s\n", "ID", "Name", "State", "CPU", "Memory";
}
foreach my $domain (sort { $a->get_id <=> $b->get_id } @domains) {
my $uuid = $domain->get_uuid_string;
my $info = $domain->get_info;
my $cpudelta = exists $cpuTime{$uuid} ? $info->{cpuTime} - $cpuTime{$uuid} : 0;
my $timedelta = defined $sample ? ($now - $sample)*1000*1000*1000 :0;
$cpuTime{$uuid} = $info->{cpuTime};
my $util = $timedelta > 0 ? $cpudelta * 100 / $timedelta : 0;
printf " %-4d %-15s %-8s %-6s %-4dMB \n", $domain->get_id, $domain->get_name, $states{$info->{state}}, (sprintf "%d%%",$util), ($info->{memory}/1024);
}
$sample = $now;
print "\n";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment