mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
feature: Added support Entity State polling (#7625)
* Entity state sensors?? * feature: Entity State polling Display entity state on the Inventory page. Allows for alerting based on states. * fix empty last changed, timezones, alarm parsing, and db updates * do not display unavailable alarms (80) add tooltip with state value * remove debug * Entity state sensors?? * feature: Entity State polling Display entity state on the Inventory page. Allows for alerting based on states. * fix empty last changed, timezones, alarm parsing, and db updates * do not display unavailable alarms (80) add tooltip with state value * remove debug * Use a discovery module and only fetch the rest of the data if entStateLastChanged is updated. * A little more efficient sql use in the case nothing has changed. * disabled by default, add to docs. * moved schema file to 220.sql
This commit is contained in:
committed by
Neil Lathwood
parent
4bb722a7ca
commit
a21a3fb2b6
105
includes/discovery/entity-state.inc.php
Normal file
105
includes/discovery/entity-state.inc.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* entity-state.inc.php
|
||||
*
|
||||
* ENTITY-STATE-MIB discovery module
|
||||
*
|
||||
* 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 2017 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
$entPhysical = dbFetchRows(
|
||||
'SELECT entPhysical_id, entPhysicalIndex FROM entPhysical WHERE device_id=?',
|
||||
array($device['device_id'])
|
||||
);
|
||||
|
||||
if (!empty($entPhysical)) {
|
||||
echo "\nEntity States: ";
|
||||
|
||||
$entPhysical = array_combine(
|
||||
array_column($entPhysical, 'entPhysicalIndex'),
|
||||
array_column($entPhysical, 'entPhysical_id')
|
||||
);
|
||||
$state_data = snmpwalk_group($device, 'entStateTable', 'ENTITY-STATE-MIB');
|
||||
$db_states = dbFetchRows('SELECT * FROM entityState WHERE device_id=?', array($device['device_id']));
|
||||
$db_states = array_combine(array_column($db_states, 'entPhysical_id'), $db_states);
|
||||
|
||||
foreach ($state_data as $index => $state) {
|
||||
if (isset($entPhysical[$index])) {
|
||||
$id = $entPhysical[$index];
|
||||
|
||||
// format datetime
|
||||
if (empty($state['entStateLastChanged'])) {
|
||||
$state['entStateLastChanged'] = null;
|
||||
} else {
|
||||
list($date, $time, $tz) = explode(',', $state['entStateLastChanged']);
|
||||
try {
|
||||
$lastChanged = new DateTime("$date $time", new DateTimeZone($tz));
|
||||
$state['entStateLastChanged'] = $lastChanged
|
||||
->setTimezone(new DateTimeZone(date_default_timezone_get()))
|
||||
->format('Y-m-d H:i:s');
|
||||
} catch (Exception $e) {
|
||||
// no update
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($db_states[$id])) { // update the db
|
||||
$db_state = $db_states[$id];
|
||||
$update = array_diff($state, $db_state);
|
||||
|
||||
if (!empty($update)) {
|
||||
if (array_key_exists('entStateLastChanged', $update) && is_null($update['entStateLastChanged'])) {
|
||||
$update['entStateLastChanged'] = array('NULL');
|
||||
}
|
||||
|
||||
dbUpdate($update, 'entityState', 'entity_state_id=?', array($db_state['entity_state_id']));
|
||||
d_echo("Updating entity state: ", 'U');
|
||||
d_echo($update);
|
||||
} else {
|
||||
echo '.';
|
||||
}
|
||||
|
||||
unset($state_data[$index]); // remove so we don't insert later
|
||||
unset($db_states[$id]); // remove so we don't delete later
|
||||
} else {
|
||||
// prep for insert later
|
||||
$state_data[$index]['device_id'] = $device['device_id'];
|
||||
$state_data[$index]['entPhysical_id'] = $id;
|
||||
$state_data[$index]['entStateLastChanged'] = $state['entStateLastChanged'];
|
||||
d_echo("Inserting entity state:: ", '+');
|
||||
d_echo($state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($state_data)) {
|
||||
dbBulkInsert($state_data, 'entityState');
|
||||
}
|
||||
|
||||
if (!empty($db_states)) {
|
||||
dbDelete(
|
||||
'entityState',
|
||||
'entity_state_id IN ' . dbGenPlaceholders(count($db_states)),
|
||||
array_column($db_states, 'entity_state_id')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
echo PHP_EOL;
|
||||
|
||||
unset($entPhysical, $state_data, $db_states, $update);
|
Reference in New Issue
Block a user