File: Lxc.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 (85 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download | duplicates (4)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package Ocsinventory::Agent::Backend::Virtualization::Lxc;

use strict;

sub check { 
    my $params = shift;
    my $common = $params->{common};
    $common->can_run('lxc-ls') && $common->can_run('lxc-info') 
}

my $memory;
my $status;
my $name;
my $vmtype;
my $vmid;
my $vcpu=0;
my $lstatus="";
my $cpu;
my @lxc_vm;

sub run {

    my $params = shift;
    my $common = $params->{common};

    # Retrieve name and state of the vm
    foreach (`lxc-ls -1`) {
        my $vm = $1 if (/^(\S+)$/);
        #push (@lxc_vm, $vm);
        foreach (`lxc-info -n $vm`){
            $name = $1 if (/^Name:\s*(\S+)$/);
            $vmid = $1 if (/^PID:\s*(\S+)$/); 
            $lstatus = $1 if (/^State:\s*(\S+)$/);

            if ($lstatus eq "RUNNING") {
                $status = "Running";
                $memory = $1 if (`lxc-cgroup -n $name memory.limit_in_bytes` =~ /(\S+)/);
                if (`lxc-cgroup -n $name cpuset.cpus` =~ /(\S+)/) {
                    $cpu = $1;
                    if ($cpu =~ /^(\d+)-(\d+)/){
                        my @tmp = ($1..$2);
                        $vcpu += $#tmp + 1;
                    } else {
                        $vcpu += 1;
                    }
                }
            } elsif ($lstatus eq "FROZEN") {
                $status = "Paused";
            } elsif ($lstatus eq "STOPPED") {
                $status = "Off";
                open LXC, "</var/lib/lxc/$name/config" or warn;
                foreach (<LXC>) {
                    next if (/^#.*/);
                    if (/^lxc.cgroup.memory.limit_in_bytes\s+=\s*(\S+)\s*$/){
                        $memory = $1;
                    }
                    if (/^lxc.cgroup.cpuset.cpus\s+=\s*(\S+)\s*$/){
                        foreach $cpu (split(/,/,$1)){
                            $cpu = $1;
                            if ($cpu =~ /(\d+)-(\d+)/){
                                my @tmp = ($1..$2);
                                $vcpu += $#tmp + 1;
                            } else {
                                $vcpu += 1;
                            }
                        }
                    } 
                }
            }
        }
        
        my $machine = {
            MEMORY => $memory,
            NAME => $name,
            STATUS => $status,
            SUBSYSTEM => "LXC Container",
            VCPU   => $vcpu,
            VMID => $vmid,
            VMTYPE => "LXC",
        };
        $common->addVirtualMachine($machine);    
    }
}

1;