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:
Tony Murray 2017-12-02 14:23:56 -06:00 committed by Neil Lathwood
parent 4bb722a7ca
commit a21a3fb2b6
12 changed files with 380 additions and 36 deletions

View File

@ -57,6 +57,7 @@ $config['discovery_modules']['os'] = 1;
$config['discovery_modules']['ports'] = 1;
$config['discovery_modules']['ports-stack'] = 1;
$config['discovery_modules']['entity-physical'] = 1;
$config['discovery_modules']['entity-state'] = 0;
$config['discovery_modules']['processors'] = 1;
$config['discovery_modules']['mempools'] = 1;
$config['discovery_modules']['cisco-vrf-lite'] = 1;

View File

@ -76,6 +76,7 @@ $config['poller_modules']['cisco-vpdn'] = 0;
$config['poller_modules']['netscaler-vsvr'] = 0;
$config['poller_modules']['aruba-controller'] = 0;
$config['poller_modules']['entity-physical'] = 1;
$config['poller_modules']['entity-state'] = 0;
$config['poller_modules']['applications'] = 1;
$config['poller_modules']['mib'] = 0;
$config['poller_modules']['stp'] = 1;

View File

@ -6,37 +6,26 @@ function printEntPhysical($ent, $level, $class)
global $device;
$ents = dbFetchRows('SELECT * FROM `entPhysical` WHERE device_id = ? AND entPhysicalContainedIn = ? ORDER BY entPhysicalContainedIn,entPhysicalIndex', array($device['device_id'], $ent));
foreach ($ents as $ent) {
echo "
<li class='$class'>";
if ($ent['entPhysicalClass'] == 'chassis') {
echo '<i class="fa fa-server fa-lg icon-theme" aria-hidden="true"></i> ';
}
if ($ent['entPhysicalClass'] == 'module') {
} elseif ($ent['entPhysicalClass'] == 'module') {
echo '<i class="fa fa-database fa-lg icon-theme" aria-hidden="true"></i> ';
}
if ($ent['entPhysicalClass'] == 'port') {
} elseif ($ent['entPhysicalClass'] == 'port') {
echo '<i class="fa fa-link fa-lg icon-theme" aria-hidden="true"></i> ';
}
if ($ent['entPhysicalClass'] == 'container') {
} elseif ($ent['entPhysicalClass'] == 'container') {
echo '<i class="fa fa-square fa-lg icon-theme" aria-hidden="true"></i> ';
}
if ($ent['entPhysicalClass'] == 'sensor') {
} elseif ($ent['entPhysicalClass'] == 'sensor') {
echo '<i class="fa fa-heartbeat fa-lg icon-theme" aria-hidden="true"></i> ';
$sensor = dbFetchRow('SELECT * FROM `sensors` WHERE `device_id` = ? AND (`entPhysicalIndex` = ? OR `sensor_index` = ?)', array($device['device_id'], $ent['entPhysicalIndex'], $ent['entPhysicalIndex']));
if (count($sensor)) {
$link = " href='device/device=".$device['device_id'].'/tab=health/metric='.$sensor['sensor_class']."/' onmouseover=\"return overlib('<img src=\'graph.php?id=".$sensor['sensor_id'].'&amp;type=sensor_'.$sensor['sensor_class'].'&amp;from=-2d&amp;to=now&amp;width=400&amp;height=150&amp;a='.$ent['entPhysical_id']."\'><img src=\'graph.php?id=".$sensor['sensor_id'].'&amp;type=sensor_'.$sensor['sensor_class'].'&amp;from=-2w&amp;to=now&amp;width=400&amp;height=150&amp;a='.$ent['entPhysical_id']."\'>', LEFT,FGCOLOR,'#e5e5e5', BGCOLOR, '#c0c0c0', BORDER, 5, CELLPAD, 4, CAPCOLOR, '#050505');\" onmouseout=\"return nd();\"";
$link = "<a href='device/device=" . $device['device_id'] . '/tab=health/metric=' . $sensor['sensor_class'] . "/' onmouseover=\"return overlib('<img src=\'graph.php?id=" . $sensor['sensor_id'] . '&amp;type=sensor_' . $sensor['sensor_class'] . '&amp;from=-2d&amp;to=now&amp;width=400&amp;height=150&amp;a=' . $ent['entPhysical_id'] . "\'><img src=\'graph.php?id=" . $sensor['sensor_id'] . '&amp;type=sensor_' . $sensor['sensor_class'] . '&amp;from=-2w&amp;to=now&amp;width=400&amp;height=150&amp;a=' . $ent['entPhysical_id'] . "\'>', LEFT,FGCOLOR,'#e5e5e5', BGCOLOR, '#c0c0c0', BORDER, 5, CELLPAD, 4, CAPCOLOR, '#050505');\" onmouseout=\"return nd();\">";
}
} else {
unset($link);
}
if ($ent['entPhysicalClass'] == 'backplane') {
} elseif ($ent['entPhysicalClass'] == 'backplane') {
echo '<i class="fa fa-bars fa-lg icon-theme" aria-hidden="true"></i> ';
}
@ -44,12 +33,12 @@ function printEntPhysical($ent, $level, $class)
echo '<strong>'.$ent['entPhysicalParentRelPos'].'.</strong> ';
}
if ($link) {
echo "<a $link>";
if (isset($link)) {
echo $link;
}
if ($ent['ifIndex']) {
$interface = dbFetchRow('SELECT * FROM `ports` WHERE ifIndex = ? AND device_id = ?', array($ent['ifIndex'], $device['device_id']));
$interface = get_port_by_ifIndex($device['device_id'], $ent['ifIndex']);
$interface = cleanPort($interface);
$ent['entPhysicalName'] = generate_port_link($interface);
}
@ -66,16 +55,50 @@ function printEntPhysical($ent, $level, $class)
echo '<strong>'.$ent['entPhysicalDescr'].'</strong>';
}
if ($ent['entPhysicalClass'] == 'sensor') {
echo ' ('.$ent['entSensorValue'].' '.$ent['entSensorType'].')';
if ($ent['entPhysicalClass'] == 'sensor' && isset($sensor)) {
echo ' ('.trim($sensor['sensor_current'] . ' ' . get_units_from_sensor($sensor)) . ')';
}
if (isset($link)) {
echo '</a>';
unset($link);
}
// display entity state
$entState = dbFetchRow(
'SELECT * FROM `entityState` WHERE `device_id`=? && `entPhysical_id`=?',
array($device['device_id'], $ent['entPhysical_id'])
);
if (!empty($entState)) {
$display_states = array(
// 'entStateAdmin',
'entStateOper',
'entStateUsage',
'entStateStandby'
);
foreach ($display_states as $state_name) {
$value = $entState[$state_name];
$display = parse_entity_state($state_name, $value);
echo " <span class='label label-{$display['color']}' data-toggle='tooltip' title='$state_name ($value)'>";
echo $display['text'];
echo "</span> ";
}
// ignore none and unavailable alarms
if ($entState['entStateAlarm'] != '00' && $entState['entStateAlarm'] != '80') {
$alarms = parse_entity_state_alarm($entState['entStateAlarm']);
echo '<br />';
echo "<span style='margin-left: 20px;'>Alarms: ";
foreach ($alarms as $alarm) {
echo " <span class='label label-{$alarm['color']}'>{$alarm['text']}</span>";
}
echo '</span>';
}
}
echo "<br /><div class='interface-desc' style='margin-left: 20px;'>".$ent['entPhysicalDescr'];
if ($link) {
echo '</a>';
}
if ($ent['entPhysicalSerialNum']) {
echo " <br /><span style='color: #000099;'>Serial No. ".$ent['entPhysicalSerialNum'].'</span> ';
}

View File

@ -210,7 +210,11 @@ function dbBulkInsert($data, $table)
if ($rowvalues != '') {
$rowvalues .= ',';
}
$rowvalues .= "'".mres($value)."'";
if (is_null($value)) {
$rowvalues .= 'NULL';
} else {
$rowvalues .= "'" . mres($value) . "'";
}
}
$values .= "(".$rowvalues.")";
}

View File

@ -765,6 +765,7 @@ $config['poller_modules']['cisco-vpdn'] = 0;
$config['poller_modules']['netscaler-vsvr'] = 0;
$config['poller_modules']['aruba-controller'] = 0;
$config['poller_modules']['entity-physical'] = 1;
$config['poller_modules']['entity-state'] = 0;
$config['poller_modules']['applications'] = 1;
$config['poller_modules']['mib'] = 0;
$config['poller_modules']['stp'] = 1;
@ -779,6 +780,7 @@ $config['discovery_modules']['os'] = 1;
$config['discovery_modules']['ports'] = 1;
$config['discovery_modules']['ports-stack'] = 1;
$config['discovery_modules']['entity-physical'] = 1;
$config['discovery_modules']['entity-state'] = 0;
$config['discovery_modules']['processors'] = 1;
$config['discovery_modules']['mempools'] = 1;
$config['discovery_modules']['cisco-vrf-lite'] = 1;

View 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);

View File

@ -34,6 +34,7 @@ if (isset($device['dynamic_discovery']['modules']['sensors']) && $device['os'] !
// Run custom sensors
require 'includes/discovery/sensors/cisco-entity-sensor.inc.php';
require 'includes/discovery/sensors/entity-sensor.inc.php';
require 'includes/discovery/sensors/entity-state.inc.php';
require 'includes/discovery/sensors/ipmi.inc.php';
if ($device['os'] == 'netscaler') {

View File

@ -1,7 +1,7 @@
<?php
echo ' ENTITY-SENSOR: ';
echo 'Caching OIDs:';
if (!is_array($entity_array)) {
if (empty($entity_array)) {
$entity_array = array();
echo ' entPhysicalDescr';
$entity_array = snmpwalk_cache_multi_oid($device, 'entPhysicalDescr', $entity_array, 'CISCO-ENTITY-SENSOR-MIB');

View File

@ -0,0 +1,99 @@
<?php
/**
* entity-state.inc.php
*
* ENTITY-STATE-MIB polling 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>
*/
$entityStatesIndexes = dbFetchRows(
'SELECT S.entity_state_id, S.entStateLastChanged, P.entPhysicalIndex FROM entityState AS S ' .
'LEFT JOIN entPhysical AS P USING (entPhysical_id) WHERE S.device_id=?',
array($device['device_id'])
);
if (!empty($entityStatesIndexes)) {
echo "\nEntity States: ";
// index by entPhysicalIndex
$entityStatesIndexes = array_combine(array_column($entityStatesIndexes, 'entPhysicalIndex'), $entityStatesIndexes);
$entLC = snmpwalk_group($device, 'entStateLastChanged', 'ENTITY-STATE-MIB', 0);
foreach (current($entLC) as $index => $changed) {
if ($changed) { // skip empty entries
try {
list($date, $time, $tz) = explode(',', $changed);
$lastChanged = new DateTime("$date $time", new DateTimeZone($tz));
$dbLastChanged = new DateTime($entityStatesIndexes[$index]['entStateLastChanged']);
if ($lastChanged != $dbLastChanged) {
// data has changed, fetch it
$new_states = snmp_get_multi(
$device,
array(
"entStateAdmin.$index",
"entStateOper.$index",
"entStateUsage.$index",
"entStateAlarm.$index",
"entStateStandby.$index"
),
'-OQUse',
'ENTITY-STATE-MIB'
);
$new_states = $new_states[$index]; // just get values
// add entStateLastChanged and update
$new_states['entStateLastChanged'] = $lastChanged
->setTimezone(new DateTimeZone(date_default_timezone_get()))
->format('Y-m-d H:i:s');
// check if anything has changed
$update = array_diff(
$new_states,
dbFetchRow(
'SELECT * FROM entityState WHERE entity_state_id=?',
array($entityStatesIndexes[$index]['entity_state_id'])
)
);
if (!empty($update)) {
dbUpdate(
$update,
'entityState',
'entity_state_id=?',
array($entityStatesIndexes[$index]['entity_state_id'])
);
d_echo("Updating $index: ", 'U');
d_echo($new_states[$index]);
continue;
}
}
} catch (Exception $e) {
// no update
d_echo("Error: " . $e->getMessage() . PHP_EOL);
}
}
echo '.';
}
echo PHP_EOL;
}
unset($entityStatesIndexes, $entLC, $lastChanged, $dbLastChanged, $new_states, $update);

View File

@ -1432,3 +1432,95 @@ function return_number($value)
}
return $value;
}
function get_units_from_sensor($sensor)
{
switch ($sensor['sensor_class']) {
case 'airflow':
return 'cfm';
case 'current':
return 'A';
case 'dbm':
case 'signal':
return 'dBm';
case 'fanspeed':
return 'rpm';
case 'frequency':
return 'Hz';
case 'charge':
case 'humidity':
case 'load':
return '%';
case 'cooling':
case 'power':
return 'W';
case 'pressure':
return 'kPa';
case 'runtime':
return 'Min';
case 'snr':
return 'SNR';
case 'state':
return '#';
case 'temperature':
return 'C';
case 'voltage':
return 'V';
default:
return '';
}
}
function parse_entity_state($state, $value)
{
$data = array(
'entStateOper' => array(
1 => array('text' => 'unavailable', 'color' => 'default'),
2 => array('text' => 'disabled', 'color' => 'danger'),
3 => array('text' => 'enabled', 'color' => 'success'),
4 => array('text' => 'testing', 'color' => 'warning'),
),
'entStateUsage' => array(
1 => array('text' => 'unavailable', 'color' => 'default'),
2 => array('text' => 'idle', 'color' => 'info'),
3 => array('text' => 'active', 'color' => 'success'),
4 => array('text' => 'busy', 'color' => 'success'),
),
'entStateStandby' => array(
1 => array('text' => 'unavailable', 'color' => 'default'),
2 => array('text' => 'hotStandby', 'color' => 'info'),
3 => array('text' => 'coldStandby', 'color' => 'info'),
4 => array('text' => 'providingService', 'color' => 'success'),
),
'entStateAdmin' => array(
1 => array('text' => 'unknown', 'color' => 'default'),
2 => array('text' => 'locked', 'color' => 'info'),
3 => array('text' => 'shuttingDown', 'color' => 'warning'),
4 => array('text' => 'unlocked', 'color' => 'success'),
),
);
if (isset($data[$state][$value])) {
return $data[$state][$value];
}
return array('text'=>'na', 'color'=>'default');
}
function parse_entity_state_alarm($bits)
{
// not sure if this is correct
$data = array(
0 => array('text' => 'unavailable', 'color' => 'default'),
1 => array('text' => 'underRepair', 'color' => 'warning'),
2 => array('text' => 'critical', 'color' => 'danger'),
3 => array('text' => 'major', 'color' => 'danger'),
4 => array('text' => 'minor', 'color' => 'info'),
5 => array('text' => 'warning', 'color' => 'warning'),
6 => array('text' => 'indeterminate', 'color' => 'default'),
);
$alarms = str_split(base_convert($bits, 16, 2));
$active_alarms = array_filter($alarms);
return array_intersect_key($data, $active_alarms);
}

View File

@ -525,6 +525,20 @@ device_perf:
Indexes:
id: { Name: id, Columns: [id], Unique: false, Type: BTREE }
device_id: { Name: device_id, Columns: [device_id], Unique: false, Type: BTREE }
entityState:
Columns:
- { Field: entity_state_id, Type: int(11), 'Null': false, Extra: auto_increment }
- { Field: device_id, Type: int(11), 'Null': true, Extra: '' }
- { Field: entPhysical_id, Type: int(11), 'Null': true, Extra: '' }
- { Field: entStateLastChanged, Type: datetime, 'Null': true, Extra: '' }
- { Field: entStateAdmin, Type: int(11), 'Null': true, Extra: '' }
- { Field: entStateOper, Type: int(11), 'Null': true, Extra: '' }
- { Field: entStateUsage, Type: int(11), 'Null': true, Extra: '' }
- { Field: entStateAlarm, Type: text, 'Null': true, Extra: '' }
- { Field: entStateStandby, Type: int(11), 'Null': true, Extra: '' }
Indexes:
PRIMARY: { Name: PRIMARY, Columns: [entity_state_id], Unique: true, Type: BTREE }
entityState_device_id_index: { Name: entityState_device_id_index, Columns: [device_id], Unique: false, Type: BTREE }
entPhysical:
Columns:
- { Field: entPhysical_id, Type: int(11), 'Null': false, Extra: auto_increment }

2
sql-schema/220.sql Normal file
View File

@ -0,0 +1,2 @@
CREATE TABLE entityState (entity_state_id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, device_id INT(11), entPhysical_id INT(11), entStateLastChanged DATETIME, entStateAdmin INT(11), entStateOper INT(11), entStateUsage INT(11), entStateAlarm TEXT, entStateStandby INT(11));
CREATE INDEX entityState_device_id_index ON entityState (device_id);