VMWare Guest State Traps and UI (#11035)

* Initial push

* power trap tests

* vmware heartbeat trap tests

* heartbeat trap test

* created utilclass for common functions

* Changed util class name to better reflect its use

* make vm power state changes to db

* Traps will modify DB

* Removed unecesarry relationship

* Added vminfo modelfactory, tests, and fixed format

* Added suspended state to ui
This commit is contained in:
Heath Barnhart
2020-01-17 11:16:14 -06:00
committed by Jellyfrog
parent f25e3efa53
commit a342a45b74
13 changed files with 566 additions and 11 deletions

View File

@@ -0,0 +1,69 @@
<?php
/**
* VmwTrapUtil.php
*
* -Description-
*
* Common utility class for handling VmWare ESXi traps.
*
* Assuming VMWare Tools is installed the VMHost will receive a periodic
* heartbeat from a VMGuest. This trap is sent once a heartbeat is
* received after not receiving heartbeats for a configured period
* of time.
*
* 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 2019 KanREN, Inc.
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
class VmwTrapUtil
{
/**
* Get the VMGuest hostname
*
* @param Trap $trap
* @return string
*/
public static function getGuestName($trap)
{
return $trap->getOidData($trap->findOid('VMWARE-VMINFO-MIB::vmwVmDisplayName'));
}
/**
* Get the VMGuest ID number
*
* @param Trap $trap
* @return string
*/
public static function getGuestId($trap)
{
return $trap->getOidData($trap->findOid('VMWARE-VMINFO-MIB::vmwVmID'));
}
/**
* Get the VMGuest configuration path
*
* @param Trap $trap
* @return string
*/
public static function getGuestConfigPath($trap)
{
return $trap->getOidData($trap->findOid('VMWARE-VMINFO-MIB::vmwVmConfigFilePath'));
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* VmwVmHBDetected.php
*
* -Description-
*
* Assuming VMWare Tools is installed the VMHost will receive a periodic
* heartbeat from a VMGuest. This trap is sent once a heartbeat is
* received after not receiving heartbeats for a configured period
* of time.
*
* 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 2019 KanREN, Inc.
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Handlers\VmwTrapUtil;
use LibreNMS\Snmptrap\Trap;
use Log;
class VmwVmHBDetected implements SnmptrapHandler
{
/**
* Handle snmptrap.
* Data is pre-parsed and delivered as a Trap.
*
* @param Device $device
* @param Trap $trap
* @return void
*/
public function handle(Device $device, Trap $trap)
{
$vmGuestName = VmwTrapUtil::getGuestName($trap);
Log::event("Heartbeat from guest $vmGuestName detected", $device->device_id, 'trap', 1);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* VmwVmHBLost.php
*
* -Description-
*
* If VMWaretools is install the VMGuest will periodically send a
* heartbeat to the VMHost. If the host does not receive a heartbeat
* within a configured time period this trap is sent.
*
* 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 2019 KanREN, Inc.
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Handlers\VmwTrapUtil;
use LibreNMS\Snmptrap\Trap;
use Log;
class VmwVmHBLost implements SnmptrapHandler
{
/**
* Handle snmptrap.
* Data is pre-parsed and delivered as a Trap.
*
* @param Device $device
* @param Trap $trap
* @return void
*/
public function handle(Device $device, Trap $trap)
{
$vmGuestName = VmwTrapUtil::getGuestName($trap);
Log::event("Heartbeat from guest $vmGuestName lost", $device->device_id, 'trap', 4);
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* VmwVmPoweredOff.php
*
* -Description-
*
* VMWare guest was powered off.
*
* 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 2019 KanREN, Inc.
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Handlers\VmwTrapUtil;
use LibreNMS\Snmptrap\Trap;
use Log;
class VmwVmPoweredOff implements SnmptrapHandler
{
/**
* Handle snmptrap.
* Data is pre-parsed and delivered as a Trap.
*
* @param Device $device
* @param Trap $trap
* @return void
*/
public function handle(Device $device, Trap $trap)
{
$vmGuestName = VmwTrapUtil::getGuestName($trap);
$vminfo = $device->vminfo()->where('vmwVmDisplayName', $vmGuestName)->first();
$vminfo->vmwVmState = "powered off";
Log::event("Guest $vmGuestName was powered off", $device->device_id, 'trap', 2);
$vminfo->save();
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* VmwVmPoweredOn.php
*
* -Description-
*
* VMWare guest was powered on.
*
* 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 2019 KanREN, Inc.
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Handlers\VmwTrapUtil;
use LibreNMS\Snmptrap\Trap;
use Log;
class VmwVmPoweredOn implements SnmptrapHandler
{
/**
* Handle snmptrap.
* Data is pre-parsed and delivered as a Trap.
*
* @param Device $device
* @param Trap $trap
* @return void
*/
public function handle(Device $device, Trap $trap)
{
$vmGuestName = VmwTrapUtil::getGuestName($trap);
$vminfo = $device->vminfo()->where('vmwVmDisplayName', $vmGuestName)->first();
$vminfo->vmwVmState = "powered on";
Log::event("Guest $vmGuestName was powered on", $device->device_id, 'trap', 2);
$vminfo->save();
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* VmwVmSuspended.php
*
* -Description-
*
* VMWare guest was suspended.
*
* 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 2019 KanREN, Inc.
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Handlers\VmwTrapUtil;
use LibreNMS\Snmptrap\Trap;
use Log;
class VmwVmSuspended implements SnmptrapHandler
{
/**
* Handle snmptrap.
* Data is pre-parsed and delivered as a Trap.
*
* @param Device $device
* @param Trap $trap
* @return void
*/
public function handle(Device $device, Trap $trap)
{
$vmGuestName = VmwTrapUtil::getGuestName($trap);
$vminfo = $device->vminfo()->where('vmwVmDisplayName', $vmGuestName)->first();
$vminfo->vmwVmState = "suspended";
Log::event("Guest $vmGuestName has been suspended", $device->device_id, 'trap', 2);
$vminfo->save();
}
}