1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
package Ocsinventory::Agent::Backend::Virtualization::Libvirt;
use strict;
use XML::Simple;
sub check {
my $params = shift;
my $common = $params->{common};
$common->can_run('virsh')
}
sub run {
my $params = shift;
my $common = $params->{common};
foreach (`virsh --readonly list --all`) {
if (/^\s*(\d+|\s+\-)\s+(\S+)\s+(\S.+)/){
my $memory;
my $vcpu;
my $name = $2;
my $status = $3;
$status =~ s/^shut off/off/;
my $xml = `virsh --readonly dumpxml $name`;
my $data = XMLin($xml);
my $vcpu = $data->{vcpu};
my $uuid = $data->{uuid};
my $vmtype = $data->{type};
if ($data->{currentMemory}->{unit}) {
$memory = $1 if $data->{currentMemory}->{content} =~ /(\d+)\d{3}$/;
$vcpu = $data->{vcpu}->{content};
} else {
$memory = $1 if $data->{currentMemory} =~ /(\d+)\d{3}$/;
$vcpu = $data->{vcpu};
}
my %machine = (
MEMORY => $memory,
NAME => $name,
UUID => $uuid,
STATUS => $status,
SUBSYSTEM => "Libvirt",
VMTYPE => $vmtype,
VCPU => $vcpu,
);
$common->addVirtualMachine(\%machine);
}
}
}
1;
|