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