mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Remove legacy code and fix missing device graphs (#11950)
* removing $graphs global * remove unused things * fix some additional graphs * Fix graphs persisting too soon * correct name for poller module performance graph * only one type of graph is used here
This commit is contained in:
@@ -725,58 +725,12 @@ function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0)
|
||||
}//end snmpwalk_cache_threepart_oid()
|
||||
|
||||
|
||||
function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0)
|
||||
{
|
||||
$cmd = gen_snmpwalk_cmd($device, $oid, '-OQUs', $mib);
|
||||
$data = trim(external_exec($cmd));
|
||||
|
||||
foreach (explode("\n", $data) as $entry) {
|
||||
$entry = str_replace($oid.'.', '', $entry);
|
||||
[$slotport, $value] = explode('=', $entry, 2);
|
||||
$slotport = trim($slotport);
|
||||
$value = trim($value);
|
||||
if ($array[$slotport]['ifIndex']) {
|
||||
$ifIndex = $array[$slotport]['ifIndex'];
|
||||
$array[$ifIndex][$oid] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}//end snmp_cache_slotport_oid()
|
||||
|
||||
|
||||
function snmp_cache_oid($oid, $device, $array, $mib = 0)
|
||||
{
|
||||
$array = snmpwalk_cache_oid($device, $oid, $array, $mib);
|
||||
return $array;
|
||||
}//end snmp_cache_oid()
|
||||
|
||||
|
||||
function snmp_cache_port_oids($oids, $port, $device, $array, $mib = 0)
|
||||
{
|
||||
$string = '';
|
||||
foreach ($oids as $oid) {
|
||||
$string .= " $oid.$port";
|
||||
}
|
||||
|
||||
$cmd = gen_snmpget_cmd($device, $string, '-Ovq', $mib);
|
||||
$data = trim(external_exec($cmd));
|
||||
|
||||
$x = 0;
|
||||
$values = explode("\n", $data);
|
||||
// echo("Caching: ifIndex $port\n");
|
||||
foreach ($oids as $oid) {
|
||||
if (!strstr($values[$x], 'at this OID')) {
|
||||
$array[$port][$oid] = $values[$x];
|
||||
}
|
||||
|
||||
$x++;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}//end snmp_cache_port_oids()
|
||||
|
||||
|
||||
/**
|
||||
* generate snmp auth arguments
|
||||
* @param array $device
|
||||
@@ -815,226 +769,6 @@ function snmp_gen_auth(&$device, $cmd = [], $strIndexing = null)
|
||||
return $cmd;
|
||||
}//end snmp_gen_auth()
|
||||
|
||||
|
||||
/*
|
||||
* Translate the given MIB into a PHP array. Each keyword becomes an array index.
|
||||
*
|
||||
* Example:
|
||||
* snmptranslate -Td -On -M mibs -m RUCKUS-ZD-SYSTEM-MIB RUCKUS-ZD-SYSTEM-MIB::ruckusZDSystemStatsNumSta
|
||||
* .1.3.6.1.4.1.25053.1.2.1.1.1.15.30
|
||||
* ruckusZDSystemStatsAllNumSta OBJECT-TYPE
|
||||
* -- FROM RUCKUS-ZD-SYSTEM-MIB
|
||||
* SYNTAX Unsigned32
|
||||
* MAX-ACCESS read-only
|
||||
* STATUS current
|
||||
* DESCRIPTION "Number of All client devices"
|
||||
* ::= { iso(1) org(3) dod(6) internet(1) private(4) enterprises(1) ruckusRootMIB(25053) ruckusObjects(1) ruckusZD(2) ruckusZDSystemModule(1) ruckusZDSystemMIB(1) ruckusZDSystemObjects(1)
|
||||
* ruckusZDSystemStats(15) 30 }
|
||||
*/
|
||||
function snmp_mib_parse($oid, $mib, $module, $mibdir = null, $device = array())
|
||||
{
|
||||
$fulloid = explode('.', $oid);
|
||||
$lastpart = end($fulloid);
|
||||
|
||||
$cmd = 'snmptranslate -Td -On';
|
||||
$cmd .= ' -M ' . mibdir($mibdir, $device);
|
||||
$cmd .= ' -m '.$module.' '.$module.'::';
|
||||
$cmd .= $lastpart;
|
||||
|
||||
$result = array();
|
||||
$lines = preg_split('/\n+/', trim(shell_exec($cmd)));
|
||||
foreach ($lines as $l) {
|
||||
$f = preg_split('/\s+/', trim($l));
|
||||
// first line is all numeric
|
||||
if (preg_match('/^[\d.]+$/', $f[0])) {
|
||||
$result['oid'] = $f[0];
|
||||
continue;
|
||||
}
|
||||
|
||||
// then the name of the object type
|
||||
if ($f[1] && $f[1] == 'OBJECT-TYPE') {
|
||||
$result['object_type'] = $f[0];
|
||||
continue;
|
||||
}
|
||||
|
||||
// then the other data elements
|
||||
if ($f[0] == '--' && $f[1] == 'FROM') {
|
||||
$result['module'] = $f[2];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($f[0] == 'MAX-ACCESS') {
|
||||
$result['max_access'] = $f[1];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($f[0] == 'STATUS') {
|
||||
$result[strtolower($f[0])] = $f[1];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($f[0] == 'SYNTAX') {
|
||||
$result[strtolower($f[0])] = $f[1];
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($f[0] == 'DESCRIPTION') {
|
||||
$desc = explode('"', $l);
|
||||
if ($desc[1]) {
|
||||
$str = preg_replace('/^[\s.]*/', '', $desc[1]);
|
||||
$str = preg_replace('/[\s.]*$/', '', $str);
|
||||
$result[strtolower($f[0])] = $str;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}//end foreach
|
||||
|
||||
// The main mib entry doesn't have any useful data in it - only return items that have the syntax specified.
|
||||
if (isset($result['syntax']) && isset($result['object_type'])) {
|
||||
$result['mib'] = $mib;
|
||||
return $result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} // snmp_mib_parse
|
||||
|
||||
|
||||
/*
|
||||
* Walks through the given MIB module, looking for the given MIB.
|
||||
* NOTE: different from snmp walk - this doesn't touch the device.
|
||||
* NOTE: There's probably a better way to do this with snmptranslate.
|
||||
*
|
||||
* Example:
|
||||
* snmptranslate -Ts -M mibs -m RUCKUS-ZD-SYSTEM-MIB | grep ruckusZDSystemStats
|
||||
* .iso.org.dod.internet.private.enterprises.ruckusRootMIB.ruckusObjects.ruckusZD.ruckusZDSystemModule.ruckusZDSystemMIB.ruckusZDSystemObjects.ruckusZDSystemStats
|
||||
* .iso.org.dod.internet.private.enterprises.ruckusRootMIB.ruckusObjects.ruckusZD.ruckusZDSystemModule.ruckusZDSystemMIB.ruckusZDSystemObjects.ruckusZDSystemStats.ruckusZDSystemStatsNumAP
|
||||
* .iso.org.dod.internet.private.enterprises.ruckusRootMIB.ruckusObjects.ruckusZD.ruckusZDSystemModule.ruckusZDSystemMIB.ruckusZDSystemObjects.ruckusZDSystemStats.ruckusZDSystemStatsNumSta
|
||||
* ...
|
||||
*/
|
||||
|
||||
|
||||
function snmp_mib_walk($mib, $module, $mibdir = null, $device = array())
|
||||
{
|
||||
$cmd = 'snmptranslate -Ts';
|
||||
$cmd .= ' -M ' . mibdir($mibdir, $device);
|
||||
$cmd .= ' -m '.$module;
|
||||
$result = array();
|
||||
$data = preg_split('/\n+/', shell_exec($cmd));
|
||||
foreach ($data as $oid) {
|
||||
// only include oids which are part of this mib
|
||||
if (strstr($oid, $mib)) {
|
||||
$obj = snmp_mib_parse($oid, $mib, $module, $mibdir, $device);
|
||||
if ($obj) {
|
||||
$result[] = $obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
} // snmp_mib_walk
|
||||
|
||||
|
||||
function quote_column($a)
|
||||
{
|
||||
return '`'.$a.'`';
|
||||
} // quote_column
|
||||
|
||||
|
||||
function join_array($a, $b)
|
||||
{
|
||||
return quote_column($a).'='.$b;
|
||||
} // join_array
|
||||
|
||||
|
||||
/*
|
||||
* Update the given table in the database with the given row & column data.
|
||||
* @param tablename The table to update
|
||||
* @param columns An array of column names
|
||||
* @param numkeys The number of columns which are in the primary key of the table; these primary keys must be first in the list of columns
|
||||
* @param rows Row data to insert, an array of arrays with column names as the second-level keys
|
||||
*/
|
||||
function update_db_table($tablename, $columns, $numkeys, $rows)
|
||||
{
|
||||
dbBeginTransaction();
|
||||
foreach ($rows as $nothing => $obj) {
|
||||
// create a parameter list based on the columns
|
||||
$params = array();
|
||||
foreach ($columns as $column) {
|
||||
$params[] = $obj[$column];
|
||||
}
|
||||
$column_placeholders = array_fill(0, count($columns), '?');
|
||||
|
||||
// build the "ON DUPLICATE KEY" part
|
||||
$non_key_columns = array_slice($columns, $numkeys);
|
||||
$non_key_placeholders = array_slice($column_placeholders, $numkeys);
|
||||
$update_definitions = array_map("join_array", $non_key_columns, $non_key_placeholders);
|
||||
$non_key_params = array_slice($params, $numkeys);
|
||||
|
||||
$sql = 'INSERT INTO `' . $tablename . '` (' .
|
||||
implode(',', array_map("quote_column", $columns)) .
|
||||
') VALUES (' . implode(',', $column_placeholders) .
|
||||
') ON DUPLICATE KEY UPDATE ' . implode(',', $update_definitions);
|
||||
$result = dbQuery($sql, array_merge($params, $non_key_params));
|
||||
d_echo("Result: $result\n");
|
||||
}
|
||||
dbCommitTransaction();
|
||||
} // update_db_table
|
||||
|
||||
/*
|
||||
* Load the given MIB into the database.
|
||||
* @return count of objects loaded
|
||||
*/
|
||||
function snmp_mib_load($mib, $module, $included_by, $mibdir = null, $device = array())
|
||||
{
|
||||
$mibs = array();
|
||||
foreach (snmp_mib_walk($mib, $module, $mibdir, $device) as $obj) {
|
||||
$mibs[$obj['object_type']] = $obj;
|
||||
$mibs[$obj['object_type']]['included_by'] = $included_by;
|
||||
}
|
||||
d_echo($mibs);
|
||||
// NOTE: `last_modified` omitted due to being automatically maintained by MySQL
|
||||
$columns = array('module', 'mib', 'object_type', 'oid', 'syntax', 'description', 'max_access', 'status', 'included_by');
|
||||
update_db_table('mibdefs', $columns, 3, $mibs);
|
||||
return count($mibs);
|
||||
} // snmp_mib_load
|
||||
|
||||
|
||||
/*
|
||||
* Turn the given oid (name or numeric value) into a MODULE::mib name.
|
||||
* @return an array consisting of the module and mib names, or null if no matching MIB is found.
|
||||
* Example:
|
||||
* snmptranslate -m all -M mibs .1.3.6.1.4.1.8072.3.2.10 2>/dev/null
|
||||
* NET-SNMP-TC::linux
|
||||
*/
|
||||
function snmp_mib_translate($oid, $module, $mibdir = null, $device = array())
|
||||
{
|
||||
if ($module !== 'all') {
|
||||
$oid = "$module::$oid";
|
||||
}
|
||||
|
||||
// load all the MIBs looking for our object (-IR)
|
||||
$cmd = [Config::get('snmptranslate', 'snmptranslate'), '-M', mibdir($mibdir, $device), '-IR', '-m', $module, $oid];
|
||||
// ignore invalid MIBs
|
||||
$lines = preg_split('/\n+/', external_exec($cmd));
|
||||
if (empty($lines)) {
|
||||
d_echo("No results from snmptranslate\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
$matches = array();
|
||||
if (!preg_match('/(.*)::(.*)/', $lines[0], $matches)) {
|
||||
d_echo("This doesn't look like a MIB: $lines[0]\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
d_echo("SNMP translated: $module::$oid -> $matches[1]::$matches[2]\n");
|
||||
return array(
|
||||
$matches[1],
|
||||
$matches[2],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* SNMP translate between numeric and textual oids
|
||||
*
|
||||
@@ -1068,257 +802,6 @@ function snmp_translate($oid, $mib = 'ALL', $mibdir = null, $options = null, $de
|
||||
return trim(external_exec($cmd));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* check if the type of the oid is a numeric type, and if so,
|
||||
* return the correct RrdDefinition
|
||||
*
|
||||
* @param string $oid
|
||||
* @param array $mibdef
|
||||
* @return RrdDefinition|false
|
||||
*/
|
||||
function oid_rrd_def($oid, $mibdef)
|
||||
{
|
||||
if (!isset($mibdef[$oid])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($mibdef[$oid]['syntax']) {
|
||||
case 'OCTET':
|
||||
case 'IpAddress':
|
||||
return false;
|
||||
|
||||
case 'TimeTicks':
|
||||
// FIXME
|
||||
return false;
|
||||
|
||||
case 'INTEGER':
|
||||
case 'Integer32':
|
||||
return RrdDefinition::make()->addDataset('mibval', 'GAUGE');
|
||||
|
||||
case 'Counter32':
|
||||
case 'Counter64':
|
||||
return RrdDefinition::make()->addDataset('mibval', 'COUNTER', 0);
|
||||
|
||||
case 'Gauge32':
|
||||
case 'Unsigned32':
|
||||
return RrdDefinition::make()->addDataset('mibval', 'GAUGE', 0);
|
||||
}
|
||||
|
||||
return false;
|
||||
} // oid_rrd_type
|
||||
|
||||
|
||||
/*
|
||||
* Construct a graph names for use in the database.
|
||||
* Tag each as in use on this device in &$graphs.
|
||||
* Update the database with graph definitions as needed.
|
||||
* We don't include the index in the graph name - that is handled at display time.
|
||||
*/
|
||||
function tag_graphs($mibname, $oids, $mibdef, &$graphs)
|
||||
{
|
||||
foreach ($oids as $index => $array) {
|
||||
foreach ($array as $oid => $val) {
|
||||
$graphname = $mibname.'-'.$mibdef[$oid]['shortname'];
|
||||
$graphs[$graphname] = true;
|
||||
}
|
||||
}
|
||||
} // tag_graphs
|
||||
|
||||
|
||||
/*
|
||||
* Ensure a graph_type definition exists in the database for the entities in this MIB
|
||||
*/
|
||||
function update_mib_graph_types($mibname, $oids, $mibdef, $graphs)
|
||||
{
|
||||
$seengraphs = array();
|
||||
|
||||
// Get the list of graphs currently in the database
|
||||
// FIXME: there's probably a more efficient way to do this
|
||||
foreach (dbFetch('SELECT DISTINCT `graph_subtype` FROM `graph_types` WHERE `graph_subtype` LIKE ?', array("$mibname-%")) as $graph) {
|
||||
$seengraphs[$graph['graph_subtype']] = true;
|
||||
}
|
||||
|
||||
foreach ($oids as $index => $array) {
|
||||
$i = 1;
|
||||
foreach ($array as $oid => $val) {
|
||||
$graphname = "$mibname-".$mibdef[$oid]['shortname'];
|
||||
|
||||
// add the graph if it's not in the database already
|
||||
if ($graphs[$graphname] && !$seengraphs[$graphname]) {
|
||||
// construct a graph definition based on the MIB definition
|
||||
$graphdef = array();
|
||||
$graphdef['graph_type'] = 'device';
|
||||
$graphdef['graph_subtype'] = $graphname;
|
||||
$graphdef['graph_section'] = 'mib';
|
||||
$graphdef['graph_descr'] = $mibdef[$oid]['description'];
|
||||
$graphdef['graph_order'] = $i++;
|
||||
// TODO: add colours, unit_text, and ds
|
||||
// add graph to the database
|
||||
dbInsert($graphdef, 'graph_types');
|
||||
}
|
||||
}
|
||||
}
|
||||
} // update_mib_graph_types
|
||||
|
||||
|
||||
/*
|
||||
* Save all of the measurable oids for the device in their own RRDs.
|
||||
* Save the current value of all the oids in the database.
|
||||
*/
|
||||
function save_mibs($device, $mibname, $oids, $mibdef, &$graphs)
|
||||
{
|
||||
$usedoids = array();
|
||||
$deviceoids = array();
|
||||
foreach ($oids as $index => $array) {
|
||||
foreach ($array as $obj => $val) {
|
||||
// build up the device_oid row for saving into the database
|
||||
$numvalue = is_numeric($val) ? $val + 0 : 0;
|
||||
$deviceoids[] = array(
|
||||
'device_id' => $device['device_id'],
|
||||
'oid' => $mibdef[$obj]['oid'].".".$index,
|
||||
'module' => $mibdef[$obj]['module'],
|
||||
'mib' => $mibdef[$obj]['mib'],
|
||||
'object_type' => $obj,
|
||||
'value' => $val,
|
||||
'numvalue' => $numvalue,
|
||||
);
|
||||
|
||||
$rrd_def = oid_rrd_def($obj, $mibdef);
|
||||
if ($rrd_def === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$usedoids[$index][$obj] = $val;
|
||||
|
||||
$tags = array(
|
||||
'rrd_def' => $rrd_def,
|
||||
'rrd_name' => array($mibname, $mibdef[$obj]['shortname'], $index),
|
||||
'rrd_oldname' => array($mibname, $mibdef[$obj]['object_type'], $index),
|
||||
'index' => $index,
|
||||
'oid' => $mibdef[$obj]['oid'],
|
||||
'module' => $mibdef[$obj]['module'],
|
||||
'mib' => $mibdef[$obj]['mib'],
|
||||
'object_type' => $obj,
|
||||
);
|
||||
data_update($device, 'mibval', $tags, $val);
|
||||
}
|
||||
}
|
||||
|
||||
tag_graphs($mibname, $usedoids, $mibdef, $graphs);
|
||||
update_mib_graph_types($mibname, $usedoids, $mibdef, $graphs);
|
||||
|
||||
// update database
|
||||
$columns = array('device_id', 'oid', 'module', 'mib', 'object_type', 'value', 'numvalue');
|
||||
update_db_table('device_oids', $columns, 2, $deviceoids);
|
||||
} // save_mibs
|
||||
|
||||
|
||||
/*
|
||||
* @return an array of MIB objects matching $module, $name, keyed by object_type
|
||||
*/
|
||||
function load_mibdefs($module, $name)
|
||||
{
|
||||
$params = array($module, $name);
|
||||
$result = array();
|
||||
$object_types = array();
|
||||
foreach (dbFetchRows("SELECT * FROM `mibdefs` WHERE `module` = ? AND `mib` = ?", $params) as $row) {
|
||||
$mib = $row['object_type'];
|
||||
$object_types[] = $mib;
|
||||
$result[$mib] = $row;
|
||||
}
|
||||
|
||||
// add shortname to each element
|
||||
$prefix = longest_matching_prefix($name, $object_types);
|
||||
foreach ($result as $mib => $m) {
|
||||
if (strlen($prefix) > 2) {
|
||||
$result[$mib]['shortname'] = preg_replace("/^$prefix/", '', $m['object_type'], 1);
|
||||
} else {
|
||||
$result[$mib]['shortname'] = $m['object_type'];
|
||||
}
|
||||
}
|
||||
|
||||
d_echo($result);
|
||||
return $result;
|
||||
} // load_mibdefs
|
||||
|
||||
/*
|
||||
* @return an array of MIB names and modules for $device from the database
|
||||
*/
|
||||
function load_device_mibs($device)
|
||||
{
|
||||
$params = array($device['device_id']);
|
||||
$result = array();
|
||||
foreach (dbFetchRows("SELECT `mib`, `module` FROM device_mibs WHERE device_id = ?", $params) as $row) {
|
||||
$result[$row['mib']] = $row['module'];
|
||||
}
|
||||
return $result;
|
||||
} // load_device_mibs
|
||||
|
||||
|
||||
/*
|
||||
* Run MIB-based polling for $device. Update $graphs with the results.
|
||||
*/
|
||||
function poll_mibs($device, &$graphs)
|
||||
{
|
||||
if (!is_mib_poller_enabled($device)) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo 'MIB: polling ';
|
||||
d_echo("\n");
|
||||
|
||||
foreach (load_device_mibs($device) as $name => $module) {
|
||||
echo "$name ";
|
||||
d_echo("\n");
|
||||
$oids = snmpwalk_cache_oid($device, $name, array(), $module, null, "-OQUsb");
|
||||
d_echo($oids);
|
||||
save_mibs($device, $name, $oids, load_mibdefs($module, $name), $graphs);
|
||||
}
|
||||
echo "\n";
|
||||
} // poll_mibs
|
||||
|
||||
/*
|
||||
* Take a list of MIB name => module pairs.
|
||||
* Validate MIBs and store the device->mib mapping in the database.
|
||||
* See includes/discovery/os/ruckuswireless.inc.php for an example of usage.
|
||||
*/
|
||||
function register_mibs($device, $mibs, $included_by)
|
||||
{
|
||||
if (!is_mib_poller_enabled($device)) {
|
||||
return;
|
||||
}
|
||||
|
||||
d_echo("MIB: registering\n");
|
||||
|
||||
foreach ($mibs as $name => $module) {
|
||||
$translated = snmp_mib_translate($name, $module, null, $device);
|
||||
if ($translated) {
|
||||
$mod = $translated[0];
|
||||
$nam = $translated[1];
|
||||
d_echo(" $mod::$nam\n");
|
||||
if (snmp_mib_load($nam, $mod, $included_by, null, $device) > 0) {
|
||||
// NOTE: `last_modified` omitted due to being automatically maintained by MySQL
|
||||
$columns = array('device_id', 'module', 'mib', 'included_by');
|
||||
$rows = array();
|
||||
$rows[] = array(
|
||||
'device_id' => $device['device_id'],
|
||||
'module' => $mod,
|
||||
'mib' => $nam,
|
||||
'included_by' => $included_by,
|
||||
);
|
||||
update_db_table('device_mibs', $columns, 3, $rows);
|
||||
} else {
|
||||
d_echo("MIB: Could not load definition for $mod::$nam\n");
|
||||
}
|
||||
} else {
|
||||
d_echo("MIB: Could not find $module::$name\n");
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n";
|
||||
} // register_mibs
|
||||
|
||||
/**
|
||||
* SNMPWalk_array_num - performs a numeric SNMPWalk and returns an array containing $count indexes
|
||||
* One Index:
|
||||
|
Reference in New Issue
Block a user