Skip to content

Commit

Permalink
Vmware vminfo modernize (#15008)
Browse files Browse the repository at this point in the history
* Vmware vminfo
Remove legacy file and migrate to OS discovery

* tighter

* ios_stp-vlans working correctly now

* Make vmwVmGuestOS nullable

* Discover os info too

* VM Info module

* Apply fixes from StyleCI

* Fix log severity

* Fix log severity (more)

* VM Info module

* Poll with ESXi too because it is lightweight
add test data

* poller data now too

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
murrant and StyleCIBot committed Oct 6, 2023
1 parent bec7a9f commit 087d588
Show file tree
Hide file tree
Showing 38 changed files with 990 additions and 446 deletions.
39 changes: 39 additions & 0 deletions LibreNMS/Interfaces/Discovery/VminfoDiscovery.php
@@ -0,0 +1,39 @@
<?php
/*
* VminfoDiscovery.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 <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2023 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace LibreNMS\Interfaces\Discovery;

use App\Models\Vminfo;
use Illuminate\Support\Collection;

interface VminfoDiscovery
{
/**
* Discover all the VMs and return a collection of Vminfo models
*
* @return Collection<Vminfo>
*/
public function discoverVminfo(): Collection;
}
40 changes: 40 additions & 0 deletions LibreNMS/Interfaces/Polling/VminfoPolling.php
@@ -0,0 +1,40 @@
<?php
/*
* VminfoPolling.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 <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2023 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace LibreNMS\Interfaces\Polling;

use App\Models\Vminfo;
use Illuminate\Support\Collection;

interface VminfoPolling
{
/**
* Poll the given VMs
*
* @param Collection<Vminfo> $vms
* @return Collection<Vminfo>
*/
public function pollVminfo(Collection $vms): Collection;
}
14 changes: 5 additions & 9 deletions LibreNMS/Modules/Os.php
Expand Up @@ -26,7 +26,9 @@
namespace LibreNMS\Modules;

use App\Models\Device;
use App\Models\Eventlog;
use App\Models\Location;
use LibreNMS\Enum\Severity;
use LibreNMS\Interfaces\Data\DataStorageInterface;
use LibreNMS\Interfaces\Module;
use LibreNMS\Interfaces\Polling\OSPolling;
Expand Down Expand Up @@ -77,23 +79,17 @@ public function poll(\LibreNMS\OS $os, DataStorageInterface $datastore): void
if ($os instanceof OSPolling) {
$os->pollOS($datastore);
} 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', Severity::Error);
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', Severity::Error);
include base_path('/includes/polling/os/' . $device['os_group'] . '.inc.php');
} else {
echo "Generic :(\n";
}

// handle legacy variables, sometimes they are false
Expand Down
119 changes: 119 additions & 0 deletions LibreNMS/Modules/Vminfo.php
@@ -0,0 +1,119 @@
<?php
/*
* Vminfo.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 <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2023 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace LibreNMS\Modules;

use App\Models\Device;
use App\Observers\ModuleModelObserver;
use LibreNMS\Config;
use LibreNMS\DB\SyncsModels;
use LibreNMS\Interfaces\Data\DataStorageInterface;
use LibreNMS\Interfaces\Discovery\VminfoDiscovery;
use LibreNMS\Interfaces\Polling\VminfoPolling;
use LibreNMS\OS;
use LibreNMS\Polling\ModuleStatus;

class Vminfo implements \LibreNMS\Interfaces\Module
{
use SyncsModels;

/**
* @inheritDoc
*/
public function dependencies(): array
{
return [];
}

public function shouldDiscover(OS $os, ModuleStatus $status): bool
{
// libvirt does not use snmp, only ssh tunnels
if (! Config::get('enable_libvirt') && $os->getDevice()->snmp_disable) {
return false;
}

return $status->isEnabled() && $os->getDevice()->status && $os instanceof VminfoDiscovery;
}

/**
* @inheritDoc
*/
public function discover(OS $os): void
{
if ($os instanceof VminfoDiscovery) {
$vms = $os->discoverVminfo();

ModuleModelObserver::observe(\App\Models\Vminfo::class);
$this->syncModels($os->getDevice(), 'vminfo', $vms);
}
echo PHP_EOL;
}

public function shouldPoll(OS $os, ModuleStatus $status): bool
{
return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status && $os instanceof VminfoPolling;
}

/**
* @inheritDoc
*/
public function poll(OS $os, DataStorageInterface $datastore): void
{
if ($os->getDevice()->vminfo->isEmpty()) {
return;
}

if ($os instanceof VminfoPolling) {
$vms = $os->pollVminfo($os->getDevice()->vminfo);

ModuleModelObserver::observe(\App\Models\Vminfo::class);
$this->syncModels($os->getDevice(), 'vminfo', $vms);

return;
}

// just run discovery again
$this->discover($os);
}

/**
* @inheritDoc
*/
public function cleanup(Device $device): void
{
$device->vminfo()->delete();
}

/**
* @inheritDoc
*/
public function dump(Device $device)
{
return [
'vminfo' => $device->vminfo()->orderBy('vmwVmVMID')
->get()->map->makeHidden(['id', 'device_id']),
];
}
}
9 changes: 2 additions & 7 deletions LibreNMS/OS.php
Expand Up @@ -94,10 +94,8 @@ class OS implements

/**
* OS constructor. Not allowed to be created directly. Use OS::make()
*
* @param array $device
*/
protected function __construct(&$device)
protected function __construct(array &$device)
{
$this->device = &$device;
$this->graphs = [];
Expand Down Expand Up @@ -231,11 +229,8 @@ public function isCached($oid)
* OS Factory, returns an instance of the OS for this device
* If no specific OS is found, Try the OS group.
* Otherwise, returns Generic
*
* @param array $device device array, must have os set
* @return OS
*/
public static function make(&$device)
public static function make(array &$device): OS
{
if (isset($device['os'])) {
// load os definition and populate os_group
Expand Down
3 changes: 1 addition & 2 deletions LibreNMS/OS/Beagleboard.php
Expand Up @@ -25,9 +25,8 @@

use App\Models\Device;
use LibreNMS\Interfaces\Discovery\OSDiscovery;
use LibreNMS\OS;

class Beagleboard extends OS implements OSDiscovery
class Beagleboard extends Linux implements OSDiscovery
{
/**
* Retrieve basic information about the OS / device
Expand Down
54 changes: 54 additions & 0 deletions LibreNMS/OS/Linux.php
@@ -0,0 +1,54 @@
<?php
/*
* Linux.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 <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2023 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace LibreNMS\OS;

use Illuminate\Support\Collection;
use LibreNMS\Interfaces\Discovery\VminfoDiscovery;
use LibreNMS\OS\Traits\VminfoLibvirt;
use LibreNMS\OS\Traits\VminfoVmware;

class Linux extends Shared\Unix implements VminfoDiscovery
{
// NOTE: Only Linux specific stuff should go here, most things should be in Unix

use VminfoLibvirt, VminfoVmware {
VminfoLibvirt::discoverVminfo as discoverLibvirtVminfo;
VminfoVmware::discoverVmInfo as discoverVmwareVminfo;
}

public function discoverVmInfo(): Collection
{
$vms = $this->discoverLibvirtVminfo();

if ($vms->isNotEmpty()) {
return $vms;
}

echo PHP_EOL;

return $this->discoverVmwareVminfo();
}
}

1 comment on commit 087d588

@librenms-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on LibreNMS Community. There might be relevant details there:

https://community.librenms.org/t/librenms-trying-to-ssh-into-a-machine-every-six-hours/22512/8

Please sign in to comment.