line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Inventory::Virtualization::Libvirt; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
57809553
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
15
|
|
|
2
|
|
|
|
|
122
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
11
|
use English qw(-no_match_vars); |
|
2
|
|
|
|
|
68
|
|
|
2
|
|
|
|
|
31
|
|
7
|
2
|
|
|
2
|
|
2618
|
use XML::TreePP; |
|
2
|
|
|
|
|
9957
|
|
|
2
|
|
|
|
|
31
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
823
|
use FusionInventory::Agent::Tools; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1881
|
|
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
|
|
351
|
my (%params) = @_; |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
11
|
my $handle = getFileHandle(%params); |
65
|
2
|
50
|
|
|
|
7
|
return unless $handle; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
3
|
my @machines; |
68
|
2
|
|
|
|
|
33
|
while (my $line = <$handle>) { |
69
|
16
|
100
|
|
|
|
50
|
next if $line =~ /^\s*Id/; |
70
|
14
|
100
|
|
|
|
35
|
next if $line =~ /^-{5}/; |
71
|
12
|
100
|
|
|
|
54
|
next unless $line =~ /^\s*(\d+|)(\-|)\s+(\S+)\s+(\S.+)/; |
72
|
|
|
|
|
|
|
|
73
|
11
|
|
|
|
|
19
|
my $name = $3; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# ignore Xen Dom0 |
76
|
11
|
50
|
|
|
|
24
|
next if $name eq 'Domain-0'; |
77
|
|
|
|
|
|
|
|
78
|
11
|
|
|
|
|
18
|
my $status = $4; |
79
|
11
|
|
|
|
|
18
|
$status =~ s/^shut off/off/; |
80
|
|
|
|
|
|
|
|
81
|
11
|
|
|
|
|
34
|
my $machine = { |
82
|
|
|
|
|
|
|
NAME => $name, |
83
|
|
|
|
|
|
|
STATUS => $status, |
84
|
|
|
|
|
|
|
VMTYPE => "libvirt", |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
11
|
|
|
|
|
43
|
push @machines, $machine; |
88
|
|
|
|
|
|
|
} |
89
|
2
|
|
|
|
|
34
|
close $handle; |
90
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
15
|
return @machines; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub _getKeyText { |
95
|
15
|
|
|
15
|
|
22
|
my ($key) = @_; |
96
|
|
|
|
|
|
|
|
97
|
15
|
100
|
|
|
|
31
|
if (ref($key) eq 'HASH') { |
98
|
4
|
|
|
|
|
10
|
return $key->{'#text'}; |
99
|
|
|
|
|
|
|
} else { |
100
|
11
|
|
|
|
|
23
|
return $key; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _parseDumpxml { |
105
|
5
|
|
|
5
|
|
5670
|
my (%params) = @_; |
106
|
|
|
|
|
|
|
|
107
|
5
|
|
|
|
|
29
|
my $xml = getAllLines(%params); |
108
|
5
|
50
|
|
|
|
15
|
if (!$xml) { |
109
|
0
|
|
|
|
|
0
|
$params{logger}->error("No virsh xmldump output"); |
110
|
0
|
|
|
|
|
0
|
return; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
5
|
|
|
|
|
7
|
my $data; |
114
|
5
|
|
|
|
|
7
|
eval { |
115
|
5
|
|
|
|
|
35
|
$data = XML::TreePP->new()->parse($xml); |
116
|
|
|
|
|
|
|
}; |
117
|
5
|
50
|
|
|
|
20372
|
if ($EVAL_ERROR) { |
118
|
0
|
|
|
|
|
0
|
$params{logger}->error("Failed to parse XML output"); |
119
|
0
|
|
|
|
|
0
|
return; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
5
|
|
|
|
|
16
|
my $vcpu = _getKeyText($data->{domain}{vcpu}); |
123
|
5
|
|
|
|
|
12
|
my $uuid = _getKeyText($data->{domain}{uuid}); |
124
|
5
|
|
|
|
|
10
|
my $vmtype = $data->{domain}{'-type'}; |
125
|
5
|
|
|
|
|
7
|
my $memory; |
126
|
5
|
|
|
|
|
12
|
my $currentMemory = _getKeyText($data->{domain}{currentMemory}); |
127
|
5
|
50
|
|
|
|
31
|
if ($currentMemory =~ /(\d+)\d{3}$/) { |
128
|
5
|
|
|
|
|
20
|
$memory = $1; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
return ( |
132
|
5
|
|
|
|
|
120
|
vcpu => $vcpu, |
133
|
|
|
|
|
|
|
uuid => $uuid, |
134
|
|
|
|
|
|
|
vmtype => $vmtype, |
135
|
|
|
|
|
|
|
memory => $memory, |
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |