line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Inventory::Virtualization::Lxc; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Authors: Egor Shornikov , Egor Morozov |
4
|
|
|
|
|
|
|
# License: GPLv2+ |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
46863279
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
68
|
|
7
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
84
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
768
|
use FusionInventory::Agent::Tools; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1174
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub isEnabled { |
12
|
0
|
|
|
0
|
0
|
0
|
return canRun('lxc-ls'); |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub doInventory { |
16
|
0
|
|
|
0
|
0
|
0
|
my (%params) = @_; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
0
|
my $inventory = $params{inventory}; |
19
|
0
|
|
|
|
|
0
|
my $logger = $params{logger}; |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
0
|
my @machines = _getVirtualMachines( |
22
|
|
|
|
|
|
|
command => '/usr/bin/lxc-ls -1', |
23
|
|
|
|
|
|
|
logger => $logger |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
0
|
foreach my $machine (@machines) { |
27
|
0
|
|
|
|
|
0
|
$inventory->addEntry( |
28
|
|
|
|
|
|
|
section => 'VIRTUALMACHINES', entry => $machine |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub _getVirtualMachineState { |
34
|
2
|
|
|
2
|
|
1004
|
my (%params) = @_; |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
8
|
my $handle = getFileHandle(%params); |
37
|
2
|
50
|
|
|
|
5
|
return unless $handle; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
1
|
my %info; |
40
|
2
|
|
|
|
|
16
|
while (my $line = <$handle>){ |
41
|
11
|
|
|
|
|
10
|
chomp $line; |
42
|
11
|
100
|
|
|
|
34
|
next unless $line =~ m/^(\S+):\s*(\S+)$/; |
43
|
7
|
|
|
|
|
26
|
$info{lc($1)} = $2; |
44
|
|
|
|
|
|
|
} |
45
|
2
|
|
|
|
|
11
|
close $handle; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
return |
48
|
|
|
|
|
|
|
$info{state} eq 'RUNNING' ? 'running' : |
49
|
|
|
|
|
|
|
$info{state} eq 'FROZEN' ? 'paused' : |
50
|
|
|
|
|
|
|
$info{state} eq 'STOPPED' ? 'off' : |
51
|
2
|
0
|
|
|
|
12
|
$info{state}; |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _getVirtualMachineConfig { |
55
|
1
|
|
|
1
|
|
388
|
my (%params) = @_; |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
6
|
my $handle = getFileHandle(%params); |
58
|
1
|
50
|
|
|
|
4
|
return unless $handle; |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
3
|
my $config = { |
61
|
|
|
|
|
|
|
VCPU => 0 |
62
|
|
|
|
|
|
|
}; |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
|
|
11
|
while (my $line = <$handle>) { |
65
|
34
|
|
|
|
|
19
|
chomp $line; |
66
|
34
|
100
|
|
|
|
55
|
next if $line =~ /^#.*/; |
67
|
27
|
100
|
|
|
|
92
|
next unless $line =~ m/^\s*(\S+)\s*=\s*(\S+)\s*$/; |
68
|
|
|
|
|
|
|
|
69
|
13
|
|
|
|
|
17
|
my $key = $1; |
70
|
13
|
|
|
|
|
11
|
my $val = $2; |
71
|
13
|
100
|
|
|
|
19
|
if ($key eq 'lxc.network.hwaddr') { |
72
|
1
|
|
|
|
|
2
|
$config->{MAC} = $val; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
13
|
100
|
|
|
|
17
|
if ($key eq 'lxc.cgroup.memory.limit_in_bytes') { |
76
|
1
|
|
|
|
|
3
|
$config->{MEMORY} = $val; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
13
|
100
|
|
|
|
30
|
if ($key eq 'lxc.cgroup.cpuset.cpus') { |
80
|
|
|
|
|
|
|
###eg: lxc.cgroup.cpuset.cpus = 0,3-5,7,2,1 |
81
|
1
|
|
|
|
|
4
|
foreach my $cpu ( split( /,/, $val ) ){ |
82
|
2
|
100
|
|
|
|
6
|
if ( $cpu =~ /(\d+)-(\d+)/ ){ |
83
|
1
|
|
|
|
|
6
|
my @tmp = ($1..$2); |
84
|
1
|
|
|
|
|
6
|
$config->{VCPU} += $#tmp + 1; |
85
|
|
|
|
|
|
|
} else { |
86
|
1
|
|
|
|
|
2
|
$config->{VCPU} += 1; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
1
|
|
|
|
|
7
|
close $handle; |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
5
|
return $config; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _getVirtualMachines { |
97
|
0
|
|
|
0
|
|
|
my (%params) = @_; |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my $handle = getFileHandle(%params); |
100
|
0
|
0
|
|
|
|
|
return unless $handle; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my @machines; |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
while(my $line = <$handle>) { |
105
|
0
|
|
|
|
|
|
chomp $line; |
106
|
0
|
0
|
|
|
|
|
next unless $line =~ m/^(\S+)$/; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
my $name = $1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $status = _getVirtualMachineState( |
111
|
|
|
|
|
|
|
command => "/usr/bin/lxc-info -n $name", |
112
|
|
|
|
|
|
|
logger => $params{logger} |
113
|
0
|
|
|
|
|
|
); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
my $config = _getVirtualMachineConfig( |
116
|
|
|
|
|
|
|
file => "/var/lib/lxc/$name/config", |
117
|
|
|
|
|
|
|
logger => $params{logger} |
118
|
0
|
|
|
|
|
|
); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
push @machines, { |
121
|
|
|
|
|
|
|
NAME => $name, |
122
|
|
|
|
|
|
|
VMTYPE => 'LXC', |
123
|
|
|
|
|
|
|
STATUS => $status, |
124
|
|
|
|
|
|
|
VCPU => $config->{VCPU}, |
125
|
|
|
|
|
|
|
MEMORY => $config->{MEMORY}, |
126
|
0
|
|
|
|
|
|
}; |
127
|
|
|
|
|
|
|
} |
128
|
0
|
|
|
|
|
|
close $handle; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
return @machines; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |