File Coverage

blib/lib/FusionInventory/Agent/SOAP/VMware/Host.pm
Criterion Covered Total %
statement 121 130 93.0
branch 34 54 62.9
condition 12 23 52.1
subroutine 15 16 93.7
pod 11 11 100.0
total 193 234 82.4


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::SOAP::VMware::Host;
2              
3 5     5   27432753 use strict;
  5         18  
  5         173  
4 5     5   34 use warnings;
  5         11  
  5         177  
5              
6 5     5   744 use FusionInventory::Agent::Tools;
  5         12  
  5         12516  
7              
8             sub new {
9 1     1 1 5 my ($class, %params) = @_;
10              
11             my $self = {
12             hash => $params{hash},
13             vms => $params{vms}
14 1         4 };
15              
16 1         4 bless $self, $class;
17              
18 1         3 return $self;
19             }
20              
21             sub _asArray {
22 17     17   29 my $h = shift;
23              
24             return
25 17 100       116 ref $h eq 'ARRAY' ? @$h :
    100          
26             $h ? ($h) :
27             () ;
28             }
29              
30             sub getBootTime {
31 0     0 1 0 my ($self) = @_;
32              
33 0         0 return $self->{hash}[0]{summary}{runtime}{bootTime};
34             }
35              
36             sub getHostname {
37 1     1 1 690 my ($self) = @_;
38              
39             return $self->{hash}[0]{name}
40              
41 1         20 }
42              
43             sub getBiosInfo {
44 1     1 1 1443 my ($self) = @_;
45              
46 1         3 my $hardware = $self->{hash}[0]{hardware};
47 1         3 my $biosInfo = $hardware->{biosInfo};
48 1         3 my $systemInfo = $hardware->{systemInfo};
49 1         2 my $ssn;
50              
51 1 50       7 return unless ref($biosInfo) eq 'HASH';
52              
53 1         3 my $identifierValue;
54 1 50       8 if (ref($systemInfo->{otherIdentifyingInfo}) eq 'HASH') {
    0          
55 1         3 $identifierValue = $systemInfo->{otherIdentifyingInfo}->{identifierValue};
56             }
57             elsif (ref($systemInfo->{otherIdentifyingInfo}) eq 'ARRAY') {
58 0         0 foreach (@{$systemInfo->{otherIdentifyingInfo}}) {
  0         0  
59 0 0       0 if ($_->{identifierType}->{key} eq 'ServiceTag') {
60 0         0 $ssn = $_->{identifierValue};
61 0         0 last;
62             }
63             }
64             }
65              
66             return {
67             BDATE => $biosInfo->{releaseDate},
68             BVERSION => $biosInfo->{biosVersion},
69             SMODEL => $systemInfo->{model},
70             SMANUFACTURER => $systemInfo->{vendor},
71 1         14 ASSETTAG => $identifierValue,
72             SSN => $ssn
73             };
74             }
75              
76             sub getHardwareInfo {
77 1     1 1 2396 my ($self) = @_;
78              
79 1         10 my $dnsConfig = $self->{hash}[0]{config}{network}{dnsConfig};
80 1         3 my $hardware = $self->{hash}[0]{hardware};
81 1         3 my $summary = $self->{hash}[0]{summary};
82 1         3 my $product = $summary->{config}->{product};
83 1         3 my $systemInfo = $hardware->{systemInfo};
84              
85             return {
86             NAME => $dnsConfig->{hostName},
87             DNS => join('/', _asArray($dnsConfig->{address})),
88             WORKGROUP => $dnsConfig->{domainName},
89             MEMORY => int($hardware->{memorySize} / (1024 * 1024)),
90             UUID => $summary->{hardware}->{uuid} || $systemInfo->{uuid},
91             OSVERSION => $product->{version},
92             OSNAME => $product->{name},
93             OSCOMMENTS => $product->{fullName}
94 1   33     5 };
95             }
96              
97             sub getCPUs {
98 1     1 1 2246 my ($self) = @_;
99              
100 1         5 my %cpuManufacturor = (
101             amd => 'AMD',
102             intel => 'Intel',
103             );
104              
105 1         3 my $hardware = $self->{hash}[0]{hardware};
106 1         4 my $totalCore = $hardware->{cpuInfo}{numCpuCores};
107 1         2 my $totalThread = $hardware->{cpuInfo}{numCpuThreads};
108 1         3 my $cpuEntries = $hardware->{cpuPkg};
109              
110 1         2 my @cpus;
111 1         3 foreach (_asArray($cpuEntries)) {
112             push @cpus,
113             {
114             CORE => $totalCore / _asArray($cpuEntries),
115             MANUFACTURER => $cpuManufacturor{ $_->{vendor} } || $_->{vendor},
116             NAME => $_->{description},
117             SPEED => int( $_->{hz} / ( 1000 * 1000 ) ),
118 2   33     6 THREAD => eval { $totalThread / $totalCore }
  2         12  
119             };
120             }
121              
122 1         9 return @cpus;
123             }
124              
125             sub getControllers {
126 1     1 1 3175 my ($self) = @_;
127              
128 1         2 my @controllers;
129              
130 1         3 foreach ( @{ $self->{hash}[0]{hardware}{pciDevice} } ) {
  1         6  
131              
132 27         116 my $pciid = sprintf( "%x:%x", $_->{vendorId}, $_->{deviceId} );
133             my $pcisubsystemid =
134 27         86 sprintf( "%x:%x", $_->{subVendorId}, $_->{subDeviceId} );
135 27         64 my $pciclass = sprintf( "%x", $_->{classId} );
136              
137 27 100       98 $pcisubsystemid = '' if $pcisubsystemid =~ /^[0:]+$/;
138              
139             # Workaround: sometime the pciid are odd negative number.
140             # e.g: 111d:ffff8018, ffff8086:244e, ffff8086:ffffa02c
141 27         48 foreach ( $pciid, $pcisubsystemid, $pciclass ) {
142 81         247 s/(\w+:)/000$1:/;
143 81         203 s/:(\w+)/:000$1/;
144 81         295 s/.*(\w{4}:).*(\w{4}).*/$1$2/g;
145             }
146             push @controllers,
147             {
148             NAME => $_->{deviceName},
149             MANUFACTURER => $_->{vendorName},
150             PCICLASS => $pciclass,
151             PCIID => $pciid,
152             PCISUBSYSTEMID => $pcisubsystemid,
153             PCISLOT => $_->{id},
154 27         188 };
155              
156             }
157              
158 1         13 return @controllers;
159             }
160              
161             sub _getNic {
162 6     6   10 my ($ref, $isVirtual) = @_;
163              
164             return {
165             DESCRIPTION => $ref->{device},
166             DRIVER => $ref->{driver},
167             IPADDRESS => $ref->{spec}{ip}{ipAddress},
168             IPMASK => $ref->{spec}{ip}{subnetMask},
169             MACADDR => $ref->{mac} || $ref->{spec}{mac},
170             MTU => $ref->{spec}{mtu},
171             PCISLOT => $ref->{pci},
172             STATUS => $ref->{spec}{ip}{ipAddress} ? 'Up' : 'Down',
173             VIRTUALDEV => $isVirtual,
174             SPEED => $ref->{spec}{linkSpeed}{speedMb},
175             }
176 6 100 66     93 }
177              
178             sub getNetworks {
179 1     1 1 26396 my ($self) = @_;
180              
181 1         3 my @networks;
182              
183 1         2 my $seen = {};
184              
185 1         3 foreach my $nicType (qw/vnic pnic consoleVnic/) {
186 3         16 foreach (_asArray($self->{hash}[0]{config}{network}{$nicType}))
187             {
188              
189 6 50       31 next if $seen->{$_->{device}}++;
190 6 100       14 my $isVirtual = $nicType eq 'vnic'?1:0;
191 6         13 push @networks, _getNic($_, $isVirtual);
192             }
193             }
194              
195 1         2 my @vnic;
196             push @vnic, $self->{hash}[0]{config}{network}{consoleVnic}
197 1 50       12 if $self->{hash}[0]{config}{network}{consoleVnic};
198             push @vnic, $self->{hash}[0]{config}{vmotion}{netConfig}{candidateVnic}
199 1 50       7 if $self->{hash}[0]{config}{vmotion}{netConfig}{candidateVnic};
200 1         3 foreach my $entry (@vnic) {
201 2         6 foreach (_asArray($entry)) {
202 2 50       9 next if $seen->{$_->{device}}++;
203              
204 0         0 push @networks, _getNic($_, 1);
205             }
206             }
207              
208 1         11 return @networks;
209             }
210              
211             sub getStorages {
212 1     1 1 7199 my ($self) = @_;
213              
214 1         2 my @storages;
215 1         7 foreach my $entry (
216             _asArray($self->{hash}[0]{config}{storageDevice}{scsiLun}))
217             {
218 2         4 my $serialnumber;
219             my $size;
220              
221             # TODO
222             #$volumnMapping{$entry->{canonicalName}} = $entry->{deviceName};
223              
224 2         9 foreach my $altName (_asArray($entry->{alternateName})) {
225 4 50       13 next unless ref($altName) eq 'HASH';
226 4 50       13 next unless $altName->{namespace};
227 4 50       10 next unless $altName->{data};
228 4 100       12 if ( $altName->{namespace} eq 'SERIALNUM' ) {
229 1         2 $serialnumber .= $_ foreach ( @{ $altName->{data} } );
  1         13  
230             }
231             }
232 2 50 66     14 if ( $entry->{capacity}{blockSize} && $entry->{capacity}{block} ) {
233 1         7 $size = int(($entry->{capacity}{blockSize} *$entry->{capacity}{block})/1024/1024);
234             }
235 2         3 my $manufacturer;
236 2 100 66     19 if ( $entry->{vendor} && ( $entry->{vendor} !~ /^\s*ATA\s*$/ ) ) {
237 1         3 $manufacturer = $entry->{vendor};
238             } else {
239 1         8 $manufacturer = getCanonicalManufacturer( $entry->{model} );
240             }
241              
242 2         11 $manufacturer =~ s/\s*(\S.*\S)\s*/$1/;
243              
244 2         5 my $model = $entry->{model};
245 2         9 $model =~ s/\s*(\S.*\S)\s*/$1/;
246              
247             push @storages, {
248             DESCRIPTION => $entry->{displayName},
249             DISKSIZE => $size,
250              
251             # INTERFACE
252             MANUFACTURER => $manufacturer,
253             MODEL => $model,
254             NAME => $entry->{deviceName},
255             TYPE => $entry->{deviceType},
256             SERIAL => $serialnumber,
257             FIRMWARE => $entry->{revision},
258              
259             # SCSI_COID
260             # SCSI_CHID
261             # SCSI_UNID
262             # SCSI_LUN
263 2         24 };
264              
265             }
266              
267 1         8 return @storages;
268              
269             }
270              
271             sub getDrives {
272 1     1 1 3212 my ($self) = @_;
273              
274 1         2 my @drives;
275              
276 1         7 foreach (
277             _asArray($self->{hash}[0]{config}{fileSystemVolume}{mountInfo}))
278             {
279 2         4 my $volumn;
280 2 100 66     18 if ( $_->{volume}{type} && ( $_->{volume}{type} =~ /NFS/i ) ) {
281 1         6 $volumn = $_->{volume}{remoteHost} . ':' . $_->{volume}{remotePath};
282              
283             # TODO
284             # } else {
285             # $volumn = $volumnMapping{$_->{volume}{extent}{diskName}}." ".$_->{volume}{extent}{partition};
286             }
287             push @drives,
288             {
289             SERIAL => $_->{volume}{uuid},
290             TOTAL => int( ( $_->{volume}{capacity} || 0 ) / ( 1000 * 1000 ) ),
291             TYPE => $_->{mountInfo}{path},
292             VOLUMN => $volumn,
293             NAME => $_->{volume}{name},
294             FILESYSTEM => lc( $_->{volume}{type} )
295 2   50     26 };
296             }
297              
298 1         8 return @drives;
299             }
300              
301             sub getVirtualMachines {
302 1     1 1 3229 my ($self) = @_;
303              
304 1         3 my @virtualMachines;
305              
306 1         3 foreach my $vm (@{$self->{vms}}) {
  1         5  
307 4         8 my $machine = $vm->[0];
308             my $status =
309             $machine->{summary}{runtime}{powerState} eq 'poweredOn' ? 'running' :
310             $machine->{summary}{runtime}{powerState} eq 'poweredOff' ? 'off' :
311 4 0       20 $machine->{summary}{runtime}{powerState} eq 'suspended' ? 'pause' :
    50          
    100          
312             undef ;
313 4 50       13 print "Unknown status (".$machine->{summary}{runtime}{powerState}.")\n" if !$status;
314              
315 4         4 my @mac;
316 4         15 foreach my $device (_asArray($machine->{config}{hardware}{device})) {
317 55 100       128 push @mac, $device->{macAddress} if $device->{macAddress};
318             }
319              
320 4         12 my $comment = $machine->{config}{annotation};
321              
322             # hack to preserve annotation / comment formating
323 4 50       11 $comment =~ s/\n/ /gm if $comment;
324              
325 4 50 33     16 if (
326             defined($_->[0]{summary}{config}{template})
327             &&
328             $_->[0]{summary}{config}{template} eq 'true'
329             ) {
330 0         0 next;
331             }
332              
333             push @virtualMachines,
334             {
335             NAME => $machine->{name},
336             STATUS => $status,
337             UUID => $machine->{summary}{config}{uuid},
338             MEMORY => $machine->{summary}{config}{memorySizeMB},
339             VMTYPE => 'VMware',
340             VCPU => $machine->{summary}{config}{numCpu},
341 4         45 MAC => join( '/', @mac ),
342             COMMENT => $comment
343             };
344             }
345              
346 1         9 return @virtualMachines;
347             }
348              
349             1;
350              
351             __END__