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