line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Inventory::Virtualization::Xen; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
46181572
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
57
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
69
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
367
|
use FusionInventory::Agent::Tools; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1013
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $runMeIfTheseChecksFailed = ["FusionInventory::Agent::Task::Inventory::Virtualization::Libvirt"]; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub isEnabled { |
11
|
0
|
|
0
|
0
|
0
|
0
|
return canRun('xm') || |
12
|
|
|
|
|
|
|
canRun('xl'); |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub canRunOK { |
16
|
0
|
|
|
0
|
0
|
0
|
my ($cmd) = @_; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
0
|
return !system("$cmd >/dev/null 2>&1"); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub doInventory { |
22
|
0
|
|
|
0
|
0
|
0
|
my (%params) = @_; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
0
|
my $inventory = $params{inventory}; |
25
|
0
|
|
|
|
|
0
|
my $logger = $params{logger}; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
0
|
my $isXM = canRunOK('xm list'); |
28
|
0
|
|
|
|
|
0
|
my $isXL = canRunOK('xl list'); |
29
|
|
|
|
|
|
|
|
30
|
0
|
0
|
|
|
|
0
|
my $toolstack = $isXM ? 'xm' : |
|
|
0
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$isXL ? 'xl' : undef; |
32
|
0
|
0
|
|
|
|
0
|
my $listParam = $isXM ? '-l' : |
|
|
0
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$isXL ? '-v' : undef; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
0
|
$logger->info("Xen $toolstack toolstack detected"); |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
my $command = "$toolstack list"; |
38
|
0
|
|
|
|
|
0
|
foreach my $machine (_getVirtualMachines(command => $command, logger => $logger)) { |
39
|
0
|
|
|
|
|
0
|
$machine->{SUBSYSTEM} = $toolstack; |
40
|
0
|
|
|
|
|
0
|
my $uuid = _getUUID( |
41
|
|
|
|
|
|
|
command => "$command $listParam $machine->{NAME}", |
42
|
|
|
|
|
|
|
logger => $logger |
43
|
|
|
|
|
|
|
); |
44
|
0
|
|
|
|
|
0
|
$machine->{UUID} = $uuid; |
45
|
0
|
|
|
|
|
0
|
$inventory->addEntry( |
46
|
|
|
|
|
|
|
section => 'VIRTUALMACHINES', entry => $machine |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
0
|
$logger->debug("$machine->{NAME}: [$uuid]"); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _getUUID { |
54
|
2
|
|
|
2
|
|
476
|
my (%params) = @_; |
55
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
12
|
return getFirstMatch( |
57
|
|
|
|
|
|
|
pattern => qr/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/xi, |
58
|
|
|
|
|
|
|
%params |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _getVirtualMachines { |
63
|
|
|
|
|
|
|
|
64
|
3
|
|
|
3
|
|
402
|
my $handle = getFileHandle(@_); |
65
|
|
|
|
|
|
|
|
66
|
3
|
50
|
|
|
|
6
|
return unless $handle; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# xm status |
69
|
3
|
|
|
|
|
9
|
my %status_list = ( |
70
|
|
|
|
|
|
|
'r' => 'running', |
71
|
|
|
|
|
|
|
'b' => 'blocked', |
72
|
|
|
|
|
|
|
'p' => 'paused', |
73
|
|
|
|
|
|
|
's' => 'shutdown', |
74
|
|
|
|
|
|
|
'c' => 'crashed', |
75
|
|
|
|
|
|
|
'd' => 'dying', |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# drop headers |
79
|
3
|
|
|
|
|
32
|
my $line = <$handle>; |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
4
|
my @machines; |
82
|
3
|
|
|
|
|
9
|
while (my $line = <$handle>) { |
83
|
59
|
|
|
|
|
38
|
chomp $line; |
84
|
59
|
|
|
|
|
33
|
my ($name, $vmid, $memory, $vcpu, $status); |
85
|
59
|
|
|
|
|
86
|
my @fields = split(' ', $line); |
86
|
59
|
100
|
|
|
|
55
|
if (@fields == 4) { |
87
|
40
|
|
|
|
|
34
|
($name, $memory, $vcpu) = @fields; |
88
|
40
|
|
|
|
|
23
|
$status = 'off'; |
89
|
|
|
|
|
|
|
} else { |
90
|
19
|
|
|
|
|
16
|
($name, $vmid, $memory, $vcpu, $status) = @fields; |
91
|
19
|
|
|
|
|
37
|
$status =~ s/-//g; |
92
|
19
|
100
|
|
|
|
22
|
$status = $status ? $status_list{$status} : 'off'; |
93
|
19
|
100
|
|
|
|
36
|
next if $vmid == 0; |
94
|
|
|
|
|
|
|
} |
95
|
56
|
50
|
|
|
|
68
|
next if $name eq 'Domain-0'; |
96
|
|
|
|
|
|
|
|
97
|
56
|
|
|
|
|
109
|
my $machine = { |
98
|
|
|
|
|
|
|
MEMORY => $memory, |
99
|
|
|
|
|
|
|
NAME => $name, |
100
|
|
|
|
|
|
|
STATUS => $status, |
101
|
|
|
|
|
|
|
SUBSYSTEM => 'xm', |
102
|
|
|
|
|
|
|
VMTYPE => 'xen', |
103
|
|
|
|
|
|
|
VCPU => $vcpu, |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
56
|
|
|
|
|
127
|
push @machines, $machine; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} |
109
|
3
|
|
|
|
|
22
|
close $handle; |
110
|
|
|
|
|
|
|
|
111
|
3
|
|
|
|
|
16
|
return @machines; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |