line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Inventory::Virtualization::Libvirt; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
51505477
|
use strict; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
59
|
|
4
|
2
|
|
|
2
|
|
14
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
78
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
8
|
use English qw(-no_match_vars); |
|
2
|
|
|
|
|
30
|
|
|
2
|
|
|
|
|
28
|
|
7
|
2
|
|
|
2
|
|
1478
|
use XML::TreePP; |
|
2
|
|
|
|
|
5064
|
|
|
2
|
|
|
|
|
19
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
425
|
use FusionInventory::Agent::Tools; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1237
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub isEnabled { |
12
|
0
|
|
|
0
|
0
|
0
|
return canRun('virsh'); |
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
|
foreach my $machine (_getMachines(logger => $logger)) { |
22
|
0
|
|
|
|
|
0
|
$inventory->addEntry( |
23
|
|
|
|
|
|
|
section => 'VIRTUALMACHINES', entry => $machine |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
0
|
foreach my $machine (_getMachines(logger => $logger, uri => 'lxc:///')) { |
28
|
0
|
|
|
|
|
0
|
$inventory->addEntry( |
29
|
|
|
|
|
|
|
section => 'VIRTUALMACHINES', entry => $machine |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _getMachines { |
36
|
0
|
|
|
0
|
|
0
|
my (%params) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
0
|
my $uri_param = $params{'uri'} ? "-c ".$params{'uri'} : ""; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my @machines = _parseList( |
42
|
|
|
|
|
|
|
command => "virsh $uri_param --readonly list --all", |
43
|
|
|
|
|
|
|
logger => $params{logger} |
44
|
0
|
|
|
|
|
0
|
); |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
foreach my $machine (@machines) { |
47
|
|
|
|
|
|
|
my %infos = _parseDumpxml( |
48
|
|
|
|
|
|
|
command => "virsh $uri_param --readonly dumpxml $machine->{NAME}", |
49
|
|
|
|
|
|
|
logger => $params{logger} |
50
|
0
|
|
|
|
|
0
|
); |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
0
|
$machine->{MEMORY} = $infos{memory}; |
53
|
0
|
|
|
|
|
0
|
$machine->{UUID} = $infos{uuid}; |
54
|
0
|
|
|
|
|
0
|
$machine->{SUBSYSTEM} = $infos{vmtype}; |
55
|
0
|
|
|
|
|
0
|
$machine->{VCPU} = $infos{vcpu}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
return @machines; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _parseList { |
62
|
2
|
|
|
2
|
|
300
|
my (%params) = @_; |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
8
|
my $handle = getFileHandle(%params); |
65
|
2
|
50
|
|
|
|
5
|
return unless $handle; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
2
|
my @machines; |
68
|
2
|
|
|
|
|
21
|
while (my $line = <$handle>) { |
69
|
16
|
100
|
|
|
|
28
|
next if $line =~ /^\s*Id/; |
70
|
14
|
100
|
|
|
|
20
|
next if $line =~ /^-{5}/; |
71
|
12
|
100
|
|
|
|
30
|
next unless $line =~ /^\s*(\d+|)(\-|)\s+(\S+)\s+(\S.+)/; |
72
|
|
|
|
|
|
|
|
73
|
11
|
|
|
|
|
14
|
my $name = $3; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# ignore Xen Dom0 |
76
|
11
|
50
|
|
|
|
14
|
next if $name eq 'Domain-0'; |
77
|
|
|
|
|
|
|
|
78
|
11
|
|
|
|
|
11
|
my $status = $4; |
79
|
11
|
|
|
|
|
10
|
$status =~ s/^shut off/off/; |
80
|
|
|
|
|
|
|
|
81
|
11
|
|
|
|
|
19
|
my $machine = { |
82
|
|
|
|
|
|
|
NAME => $name, |
83
|
|
|
|
|
|
|
STATUS => $status, |
84
|
|
|
|
|
|
|
VMTYPE => "libvirt", |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
11
|
|
|
|
|
26
|
push @machines, $machine; |
88
|
|
|
|
|
|
|
} |
89
|
2
|
|
|
|
|
10
|
close $handle; |
90
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
11
|
return @machines; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub _getKeyText { |
95
|
15
|
|
|
15
|
|
12
|
my ($key) = @_; |
96
|
|
|
|
|
|
|
|
97
|
15
|
100
|
|
|
|
20
|
if (ref($key) eq 'HASH') { |
98
|
4
|
|
|
|
|
5
|
return $key->{'#text'}; |
99
|
|
|
|
|
|
|
} else { |
100
|
11
|
|
|
|
|
12
|
return $key; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _parseDumpxml { |
105
|
5
|
|
|
5
|
|
3817
|
my (%params) = @_; |
106
|
|
|
|
|
|
|
|
107
|
5
|
|
|
|
|
17
|
my $xml = getAllLines(%params); |
108
|
5
|
50
|
|
|
|
9
|
if (!$xml) { |
109
|
0
|
|
|
|
|
0
|
$params{logger}->error("No virsh xmldump output"); |
110
|
0
|
|
|
|
|
0
|
return; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
5
|
|
|
|
|
4
|
my $data; |
114
|
5
|
|
|
|
|
5
|
eval { |
115
|
5
|
|
|
|
|
23
|
$data = XML::TreePP->new()->parse($xml); |
116
|
|
|
|
|
|
|
}; |
117
|
5
|
50
|
|
|
|
11336
|
if ($EVAL_ERROR) { |
118
|
0
|
|
|
|
|
0
|
$params{logger}->error("Failed to parse XML output"); |
119
|
0
|
|
|
|
|
0
|
return; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
5
|
|
|
|
|
9
|
my $vcpu = _getKeyText($data->{domain}{vcpu}); |
123
|
5
|
|
|
|
|
8
|
my $uuid = _getKeyText($data->{domain}{uuid}); |
124
|
5
|
|
|
|
|
5
|
my $vmtype = $data->{domain}{'-type'}; |
125
|
5
|
|
|
|
|
5
|
my $memory; |
126
|
5
|
|
|
|
|
6
|
my $currentMemory = _getKeyText($data->{domain}{currentMemory}); |
127
|
5
|
50
|
|
|
|
23
|
if ($currentMemory =~ /(\d+)\d{3}$/) { |
128
|
5
|
|
|
|
|
7
|
$memory = $1; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
return ( |
132
|
5
|
|
|
|
|
71
|
vcpu => $vcpu, |
133
|
|
|
|
|
|
|
uuid => $uuid, |
134
|
|
|
|
|
|
|
vmtype => $vmtype, |
135
|
|
|
|
|
|
|
memory => $memory, |
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |