Issues (2963)

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

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