Save MIB graphs in database, tag devices for graphing

This commit is contained in:
Paul Gear
2015-06-16 08:27:56 +10:00
parent e77d4d3063
commit 984ec0d060
3 changed files with 95 additions and 43 deletions
-1
View File
@@ -358,7 +358,6 @@ function rrd_create_update($device, $name, $def, $val, $step = 300)
{ {
global $config; global $config;
$rrd = implode("/", array($config['rrd_dir'], $device['hostname'], safename(implode("-", $name)).".rrd")); $rrd = implode("/", array($config['rrd_dir'], $device['hostname'], safename(implode("-", $name)).".rrd"));
d_echo("RRD file: $rrd");
if (!is_file($rrd)) { if (!is_file($rrd)) {
// add the --step and the rra definitions to the array // add the --step and the rra definitions to the array
+1 -1
View File
@@ -43,6 +43,6 @@ if (isset($ruckuscountry) && $ruckuscountry != "") {
$ruckus_mibs = array( $ruckus_mibs = array(
"ruckusZDSystemStats" => "RUCKUS-ZD-SYSTEM-MIB", "ruckusZDSystemStats" => "RUCKUS-ZD-SYSTEM-MIB",
); );
poll_mibs($ruckus_mibs, $device); poll_mibs($ruckus_mibs, $device, $graphs);
?> ?>
+79 -26
View File
@@ -805,6 +805,23 @@ function snmp_gen_auth (&$device)
return $cmd; return $cmd;
} }
/*
* Shorten the name to so it works as an RRD data source.
* Substitute for $subst if necessary.
* @return the shortened name
*/
function name_shorten($name, $common, $subst = "mibval", $len = 19)
{
if (strlen($name) > $len && strpos($name, $common) >= 0) {
$newname = str_replace($common, '', $name);
$name = $newname;
}
if (strlen($name) > $len) {
$name = $subst;
}
return $name;
}
/* /*
* Translate the given MIB into a vaguely useful PHP array. Each keyword becomes an array index. * Translate the given MIB into a vaguely useful PHP array. Each keyword becomes an array index.
* *
@@ -843,6 +860,8 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null)
// then the name of the object type // then the name of the object type
if ($f[1] && $f[1] == "OBJECT-TYPE") { if ($f[1] && $f[1] == "OBJECT-TYPE") {
$result['object_type'] = $f[0]; $result['object_type'] = $f[0];
$result['shortname'] = str_replace($mib, '', $f[0]);
$result['dsname'] = name_shorten($f[0], $mib);
continue; continue;
} }
// then the other data elements // then the other data elements
@@ -872,8 +891,8 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null)
continue; continue;
} }
} }
// This gets rid of the main mib entry that doesn't have any useful data in it // The main mib entry doesn't have any useful data in it - only return items that have the syntax specified.
if (isset($result['syntax'])) { if (isset($result['syntax']) && isset($result['object_type'])) {
$result['mib'] = $mib; $result['mib'] = $mib;
return $result; return $result;
} }
@@ -960,11 +979,11 @@ function snmp_translate($oid, $module, $mibdir = null)
* check if the type of the oid is a numeric type, and if so, * check if the type of the oid is a numeric type, and if so,
* @return the name of RRD type that is best suited to saving it * @return the name of RRD type that is best suited to saving it
*/ */
function oid_rrd_type($oid, $mibdefs) { function oid_rrd_type($oid, $mibdef) {
if (!isset($mibdefs[$oid])) { if (!isset($mibdef[$oid])) {
return false; return false;
} }
switch ($mibdefs[$oid]['syntax']) { switch ($mibdef[$oid]['syntax']) {
case 'OCTET': case 'OCTET':
case 'IpAddress': case 'IpAddress':
@@ -986,51 +1005,87 @@ function oid_rrd_type($oid, $mibdefs) {
} }
/* /*
* Shorten the RRD variable name to less than 19 characters * Construct a graph names for use in the database.
* Substitute for "mibval" if necessary. * 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 rrd_shorten($name, $prefix) function tag_graphs($mibname, $oids, $mibdef, &$graphs)
{ {
if (strlen($name) > 19 && strpos($name, $prefix) == 0) { foreach ($oids as $index => $array) {
$newname = str_replace($prefix, '', $name); foreach ($array as $oid => $val) {
d_echo("Shortened $name to $newname"); $graphname = $mibname."-".$mibdef[$oid]['shortname'];
$name = $newname; $graphs[$graphname] = true;
}
}
}
/*
* 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');
}
} }
if (strlen($name) > 19) {
d_echo("Shortened $name to mibval");
$name = "mibval";
} }
return $name;
} }
/* /*
* Save all of the measurable oids for the device in their own RRDs. * Save all of the measurable oids for the device in their own RRDs.
* FIXME: Do we have to save the shortened name in order to get it back for graphing?
*/ */
function save_mibs($device, $mibname, $oids, $mibdefs) function save_mibs($device, $mibname, $oids, $mibdef, &$graphs)
{ {
$usedoids = array();
foreach ($oids as $index => $array) { foreach ($oids as $index => $array) {
foreach ($array as $oid => $val) { foreach ($array as $oid => $val) {
$type = oid_rrd_type($oid, $mibdefs); $type = oid_rrd_type($oid, $mibdef);
if (!$type) { if (!$type) {
continue; continue;
} }
$usedoids[$index][$oid] = $val;
rrd_create_update( rrd_create_update(
$device, $device,
array($mibname, str_replace($mibname, '', $oid), $index), array($mibname, $mibdef[$oid]['shortname'], $index),
array("DS:".rrd_shorten($oid, $mibname).":$type"), array("DS:".$mibdef[$oid]['dsname'].":$type"),
"N:$val" "N:$val"
); );
} }
} }
tag_graphs($mibname, $usedoids, $mibdef, $graphs);
update_mib_graph_types($mibname, $usedoids, $mibdef, $graphs);
} }
/* /*
* Take a list of MIB name => module pairs. * Take a list of MIB name => module pairs.
* Validate MIBs and poll based on the results. * Validate MIBs and poll based on the results.
* Can be slow due to use of snmptranslate.
*/ */
function poll_mibs($list, $device) function poll_mibs($list, $device, &$graphs)
{ {
if (!is_dev_attrib_enabled($device, "poll_mib")) { if (!is_dev_attrib_enabled($device, "poll_mib")) {
d_echo("MIB module disabled for ".$device['hostname']); d_echo("MIB module disabled for ".$device['hostname']);
@@ -1038,15 +1093,13 @@ function poll_mibs($list, $device)
} }
$mibdefs = array(); $mibdefs = array();
foreach ($list as $name => $module) { foreach ($list as $name => $module) {
d_echo("MIB searching: $module::$name");
$translated = snmp_translate($name, $module); $translated = snmp_translate($name, $module);
if ($translated) { if ($translated) {
$mod = $translated[0]; $mod = $translated[0];
$nam = $translated[1]; $nam = $translated[1];
d_echo("MIB found: $mod::$nam");
$mibdefs[$nam] = snmp_mib_load($nam, $mod); $mibdefs[$nam] = snmp_mib_load($nam, $mod);
$oids = snmpwalk_cache_oid($device, "$mod::$nam", array(), $mod); $oids = snmpwalk_cache_oid($device, "$mod::$nam", array(), $mod);
save_mibs($device, $nam, $oids, $mibdefs[$nam]); save_mibs($device, $nam, $oids, $mibdefs[$nam], $graphs);
} }
else { else {
d_echo("MIB: no match for $module::$name"); d_echo("MIB: no match for $module::$name");