File: Libvirt.pm

package info (click to toggle)
ocsinventory-agent 2%3A2.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,424 kB
  • sloc: perl: 26,492; xml: 773; objc: 528; sh: 386; ansic: 333; makefile: 12
file content (56 lines) | stat: -rw-r--r-- 1,412 bytes parent folder | download | duplicates (3)
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;