Skip to content

Commit

Permalink
Vmware vminfo
Browse files Browse the repository at this point in the history
Remove legacy file and migrate to OS discovery
  • Loading branch information
murrant committed Apr 26, 2023
1 parent 121c8ff commit 1f82607
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 174 deletions.
2 changes: 1 addition & 1 deletion LibreNMS/Data/Source/SnmpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function getRawWithoutBadLines(): string
{
return (string) preg_replace([
'/^.*No Such Instance currently exists.*$/m',
'/\n[^\r\n]+No more variables left[^\r\n]+$/s',
'/\n[^\r\n]+No more variables left[^\r\n]+$/ms',
], '', $this->raw);
}

Expand Down
13 changes: 4 additions & 9 deletions LibreNMS/Modules/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace LibreNMS\Modules;

use App\Models\Device;
use App\Models\Eventlog;
use App\Models\Location;
use LibreNMS\Interfaces\Module;
use LibreNMS\Interfaces\Polling\OSPolling;
Expand Down Expand Up @@ -65,23 +66,17 @@ public function poll(\LibreNMS\OS $os): void
if ($os instanceof OSPolling) {
$os->pollOS();
} else {
// legacy poller files
global $graphs, $device;

if (empty($device)) {
$device = $os->getDeviceArray();
}

$device = $os->getDeviceArray();
$location = null;

if (is_file(base_path('/includes/polling/os/' . $device['os'] . '.inc.php'))) {
// OS Specific
Eventlog::log("Warning: OS {$device['os']} using deprecated polling method", $deviceModel, 'poller', 5);
include base_path('/includes/polling/os/' . $device['os'] . '.inc.php');
} elseif (! empty($device['os_group']) && is_file(base_path('/includes/polling/os/' . $device['os_group'] . '.inc.php'))) {
// OS Group Specific
Eventlog::log("Warning: OS {$device['os']} using deprecated polling method", $deviceModel, 'poller', 5);
include base_path('/includes/polling/os/' . $device['os_group'] . '.inc.php');
} else {
echo "Generic :(\n";
}

// handle legacy variables, sometimes they are false
Expand Down
82 changes: 82 additions & 0 deletions LibreNMS/OS/VmwareEsxi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* VmwareEsxi.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2023 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace LibreNMS\OS;

use App\Models\Device;
use App\Models\Vminfo;
use App\Observers\ModuleModelObserver;
use LibreNMS\DB\SyncsModels;
use LibreNMS\Enum\PowerState;

class VmwareEsxi extends \LibreNMS\OS
{
use SyncsModels;

public function discoverOS(Device $device): void
{
echo 'VMware VM: ';

/*
* Fetch the Virtual Machine information.
*
* VMWARE-VMINFO-MIB::vmwVmDisplayName.224 = STRING: My First VM
* VMWARE-VMINFO-MIB::vmwVmGuestOS.224 = STRING: windows7Server64Guest
* VMWARE-VMINFO-MIB::vmwVmMemSize.224 = INTEGER: 8192 megabytes
* VMWARE-VMINFO-MIB::vmwVmState.224 = STRING: poweredOn
* VMWARE-VMINFO-MIB::vmwVmVMID.224 = INTEGER: 224
* VMWARE-VMINFO-MIB::vmwVmCpus.224 = INTEGER: 2
*/

$vm_info = \SnmpQuery::hideMib()->walk('VMWARE-VMINFO-MIB::vmwVmTable');

$vms = $vm_info->mapTable(function ($data, $vmwVmVMID) {
$vm_data = [
'vm_type' => 'vmware',
'vmwVmVMID' => $vmwVmVMID,
'vmwVmDisplayName' => $data['vmwVmDisplayName'],
'vmwVmGuestOS' => $data['vmwVmGuestOS'],
'vmwVmMemSize' => $data['vmwVmMemSize'],
'vmwVmCpus' => $data['vmwVmCpus'],
'vmwVmState' => PowerState::STATES[$data['vmwVmState']] ?? PowerState::UNKNOWN,
];

/*
* If VMware Tools is not running then don't overwrite the GuestOS with the error
* message, but just leave it as it currently is.
*/
if (str_contains($vm_data['vmwVmGuestOS'], 'tools not ')) {
unset($vm_data['vmwVmGuestOS']);
}

return new Vminfo($vm_data);
});

ModuleModelObserver::observe(Vminfo::class);
$this->syncModels($device, 'vminfo', $vms);

echo "\n";
}
}
17 changes: 16 additions & 1 deletion app/Models/Vminfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;
use LibreNMS\Interfaces\Models\Keyable;
use LibreNMS\Util\Html;
use LibreNMS\Util\Number;
use LibreNMS\Util\Rewrite;

class Vminfo extends DeviceRelatedModel
class Vminfo extends DeviceRelatedModel implements Keyable
{
use HasFactory;

protected $table = 'vminfo';
public $timestamps = false;
protected $fillable = [
'vm_type',
'vmwVmVMID',
'vmwVmDisplayName',
'vmwVmGuestOS',
'vmwVmMemSize',
'vmwVmCpus',
'vmwVmState',
];

public function getStateLabelAttribute(): array
{
Expand Down Expand Up @@ -56,4 +66,9 @@ public function parentDevice(): HasOne
{
return $this->hasOne(\App\Models\Device::class, 'hostname', 'vmwVmDisplayName');
}

public function getCompositeKey()
{
return $this->vm_type . $this->vmwVmVMID;
}
}
66 changes: 66 additions & 0 deletions app/Observers/VminfoObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Observers;

use App\Models\Eventlog;
use App\Models\Vminfo;

class VminfoObserver
{
/**
* Handle the Vminfo "created" event.
*
* @param \App\Models\Vminfo $vminfo
* @return void
*/
public function created(Vminfo $vminfo)
{
Eventlog::log($vminfo->vmwVmDisplayName . " ($vminfo->vmwVmMemSize GB / $vminfo->vmwVmCpus vCPU) Discovered", $vminfo->device_id, 'system', 3, $vminfo->vmwVmVMID);
}

/**
* Handle the Vminfo "updated" event.
*
* @param \App\Models\Vminfo $vminfo
* @return void
*/
public function updating(Vminfo $vminfo)
{
foreach ($vminfo->getDirty() as $field => $value) {
Eventlog::log($vminfo->vmwVmDisplayName . ' (' . preg_replace('/^vmwVm/', '', $field) . ') -> ' . $value, $vminfo->device_id);
}
}

/**
* Handle the Vminfo "deleted" event.
*
* @param \App\Models\Vminfo $vminfo
* @return void
*/
public function deleted(Vminfo $vminfo)
{
Eventlog::log($vminfo->vmwVmDisplayName . ' Removed', $vminfo->device_id, 'system', 4, $vminfo->vmwVmVMID);
}

/**
* Handle the Vminfo "restored" event.
*
* @param \App\Models\Vminfo $vminfo
* @return void
*/
public function restored(Vminfo $vminfo)
{
//
}

/**
* Handle the Vminfo "force deleted" event.
*
* @param \App\Models\Vminfo $vminfo
* @return void
*/
public function forceDeleted(Vminfo $vminfo)
{
//
}
}
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private function bootObservers()
\App\Models\Service::observe(\App\Observers\ServiceObserver::class);
\App\Models\Stp::observe(\App\Observers\StpObserver::class);
\App\Models\User::observe(\App\Observers\UserObserver::class);
\App\Models\Vminfo::observe(\App\Observers\VminfoObserver::class);
}

private function bootCustomValidators()
Expand Down
85 changes: 0 additions & 85 deletions includes/discovery/vmware-vminfo.inc.php

This file was deleted.

Loading

0 comments on commit 1f82607

Please sign in to comment.