Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Entity State polling #7625

Merged
merged 15 commits into from
Dec 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/Support/Discovery Support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions doc/Support/Poller Support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
89 changes: 56 additions & 33 deletions html/pages/device/entphysical.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,40 @@ 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') {
echo '<i class="fa fa-database fa-lg icon-theme" aria-hidden="true"></i> ';
}

if ($ent['entPhysicalClass'] == 'port') {
echo '<i class="fa fa-link fa-lg icon-theme" aria-hidden="true"></i> ';
}

if ($ent['entPhysicalClass'] == 'container') {
echo '<i class="fa fa-square fa-lg icon-theme" aria-hidden="true"></i> ';
}

if ($ent['entPhysicalClass'] == 'sensor') {
echo '<i class="fa fa-heartbeat fa-lg icon-theme" aria-hidden="true"></i> ';
echo '<i class="fa fa-server fa-lg icon-theme" aria-hidden="true"></i> ';
} elseif ($ent['entPhysicalClass'] == 'module') {
echo '<i class="fa fa-database fa-lg icon-theme" aria-hidden="true"></i> ';
} elseif ($ent['entPhysicalClass'] == 'port') {
echo '<i class="fa fa-link fa-lg icon-theme" aria-hidden="true"></i> ';
} elseif ($ent['entPhysicalClass'] == 'container') {
echo '<i class="fa fa-square fa-lg icon-theme" aria-hidden="true"></i> ';
} 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') {
echo '<i class="fa fa-bars fa-lg icon-theme" aria-hidden="true"></i> ';
} elseif ($ent['entPhysicalClass'] == 'backplane') {
echo '<i class="fa fa-bars fa-lg icon-theme" aria-hidden="true"></i> ';
}

if ($ent['entPhysicalParentRelPos'] > '-1') {
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 = cleanPort($interface);
$interface = get_port_by_ifIndex($device['device_id'], $ent['ifIndex']);
$interface = cleanPort($interface);
$ent['entPhysicalName'] = generate_port_link($interface);
}

Expand All @@ -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)) . ')';
}

echo "<br /><div class='interface-desc' style='margin-left: 20px;'>".$ent['entPhysicalDescr'];

if ($link) {
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 ($ent['entPhysicalSerialNum']) {
echo " <br /><span style='color: #000099;'>Serial No. ".$ent['entPhysicalSerialNum'].'</span> ';
}
Expand Down
6 changes: 5 additions & 1 deletion includes/dbFacile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.")";
}
Expand Down
2 changes: 2 additions & 0 deletions includes/defaults.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@
$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;
Expand All @@ -774,6 +775,7 @@
$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;
Expand Down
105 changes: 105 additions & 0 deletions includes/discovery/entity-state.inc.php
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions includes/discovery/sensors.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
// 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') {
Expand Down
2 changes: 1 addition & 1 deletion includes/discovery/sensors/entity-sensor.inc.php
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
99 changes: 99 additions & 0 deletions includes/polling/entity-state.inc.php
Original file line number Diff line number Diff line change
@@ -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);
Loading