Passed
Push — master ( 0baeb0...d4017c )
by Tony
19:18 queued 08:55
created

includes/discovery/vmware-vminfo.inc.php (1 issue)

1
<?php
2
3
// FIXME should do the deletion etc in a common file perhaps? like for the sensors
4
/*
5
 * Try to discover any Virtual Machines.
6
 */
7
8
if (($device['os'] == 'vmware') || ($device['os'] == 'linux')) {
9
    /*
10
     * Variable to hold the discovered Virtual Machines.
11
     */
12
13
    $vmw_vmlist = array();
14
15
    /*
16
     * CONSOLE: Start the VMware discovery process.
17
     */
18
19
    /*
20
     * Fetch information about Virtual Machines.
21
     */
22
23
    $oids = snmpwalk_cache_multi_oid($device, 'vmwVmTable', [], '+VMWARE-ROOT-MIB:VMWARE-VMINFO-MIB', 'vmware');
24
25
    foreach ($oids as $index => $entry) {
26
        $vmwVmDisplayName = $entry['vmwVmDisplayName'];
27
        $vmwVmGuestOS     = $entry['vmwVmGuestOS'];
28
        $vmwVmMemSize     = $entry['vmwVmMemSize'];
29
        $vmwVmState       = $entry['vmwVmState'];
30
        $vmwVmCpus        = $entry['vmwVmCpus'];
31
32
        /*
33
         * VMware does not return an INTEGER but a STRING of the vmwVmMemSize. This bug
34
         * might be resolved by VMware in the future making this code obsolete.
35
         */
36
        if (preg_match('/^([0-9]+) .*$/', $vmwVmMemSize, $matches)) {
37
            $vmwVmMemSize = $matches[1];
38
        }
39
40
        /*
41
         * Check whether the Virtual Machine is already known for this host.
42
         */
43
        if (dbFetchCell("SELECT COUNT(id) FROM `vminfo` WHERE `device_id` = ? AND `vmwVmVMID` = ? AND vm_type='vmware'", array($device['device_id'], $index)) == 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing dbFetchCell('SELECT COUN...['device_id'], $index)) of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
44
            $vmid = dbInsert(array('device_id' => $device['device_id'], 'vm_type' => 'vmware', 'vmwVmVMID' => $index, 'vmwVmDisplayName' => mres($vmwVmDisplayName), 'vmwVmGuestOS' => mres($vmwVmGuestOS), 'vmwVmMemSize' => mres($vmwVmMemSize), 'vmwVmCpus' => mres($vmwVmCpus), 'vmwVmState' => mres($vmwVmState)), 'vminfo');
45
            log_event(mres($vmwVmDisplayName) . " ($vmwVmMemSize GB / $vmwVmCpus vCPU) Discovered", $device, 'system', 3, $vmid);
46
            echo '+';
47
            // FIXME eventlog
48
        } else {
49
            echo '.';
50
        }
51
52
        /*
53
         * Save the discovered Virtual Machine.
54
         */
55
56
        $vmw_vmlist[] = $index;
57
    }
58
59
    /*
60
     * Get a list of all the known Virtual Machines for this host.
61
     */
62
63
    $sql = "SELECT id, vmwVmVMID, vmwVmDisplayName FROM vminfo WHERE device_id = '".$device['device_id']."' AND vm_type='vmware'";
64
65
    foreach (dbFetchRows($sql) as $db_vm) {
66
        /*
67
         * Delete the Virtual Machines that are removed from the host.
68
         */
69
70
        if (!in_array($db_vm['vmwVmVMID'], $vmw_vmlist)) {
71
            dbDelete('vminfo', '`id` = ?', array($db_vm['id']));
72
            log_event(mres($db_vm['vmwVmDisplayName']) . ' Removed', $device, 'system', 4, $db_vm['vmwVmVMID']);
73
            echo '-';
74
            // FIXME eventlog
75
        }
76
    }
77
78
    /*
79
     * Finished discovering VMware information.
80
     */
81
82
    echo "\n";
83
}//end if
84