Save MIB graphs in database, tag devices for graphing

This commit is contained in:
Paul Gear
2015-06-09 00:12:43 +10:00
parent e77d4d3063
commit 984ec0d060
3 changed files with 95 additions and 43 deletions

View File

@@ -358,7 +358,6 @@ function rrd_create_update($device, $name, $def, $val, $step = 300)
{
global $config;
$rrd = implode("/", array($config['rrd_dir'], $device['hostname'], safename(implode("-", $name)).".rrd"));
d_echo("RRD file: $rrd");
if (!is_file($rrd)) {
// add the --step and the rra definitions to the array

View File

@@ -43,6 +43,6 @@ if (isset($ruckuscountry) && $ruckuscountry != "") {
$ruckus_mibs = array(
"ruckusZDSystemStats" => "RUCKUS-ZD-SYSTEM-MIB",
);
poll_mibs($ruckus_mibs, $device);
poll_mibs($ruckus_mibs, $device, $graphs);
?>

View File

@@ -805,6 +805,23 @@ function snmp_gen_auth (&$device)
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.
*
@@ -843,6 +860,8 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null)
// then the name of the object type
if ($f[1] && $f[1] == "OBJECT-TYPE") {
$result['object_type'] = $f[0];
$result['shortname'] = str_replace($mib, '', $f[0]);
$result['dsname'] = name_shorten($f[0], $mib);
continue;
}
// then the other data elements
@@ -872,8 +891,8 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null)
continue;
}
}
// This gets rid of the main mib entry that doesn't have any useful data in it
if (isset($result['syntax'])) {
// 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;
}
@@ -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,
* @return the name of RRD type that is best suited to saving it
*/
function oid_rrd_type($oid, $mibdefs) {
if (!isset($mibdefs[$oid])) {
function oid_rrd_type($oid, $mibdef) {
if (!isset($mibdef[$oid])) {
return false;
}
switch ($mibdefs[$oid]['syntax']) {
switch ($mibdef[$oid]['syntax']) {
case 'OCTET':
case 'IpAddress':
@@ -986,51 +1005,87 @@ function oid_rrd_type($oid, $mibdefs) {
}
/*
* Shorten the RRD variable name to less than 19 characters
* Substitute for "mibval" if necessary.
* 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 rrd_shorten($name, $prefix)
{
if (strlen($name) > 19 && strpos($name, $prefix) == 0) {
$newname = str_replace($prefix, '', $name);
d_echo("Shortened $name to $newname");
$name = $newname;
}
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.
* 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 tag_graphs($mibname, $oids, $mibdef, &$graphs)
{
foreach ($oids as $index => $array) {
foreach ($array as $oid => $val) {
$type = oid_rrd_type($oid, $mibdefs);
if (!$type) {
continue;
}
rrd_create_update(
$device,
array($mibname, str_replace($mibname, '', $oid), $index),
array("DS:".rrd_shorten($oid, $mibname).":$type"),
"N:$val"
);
$graphname = $mibname."-".$mibdef[$oid]['shortname'];
$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');
}
}
}
}
/*
* Save all of the measurable oids for the device in their own RRDs.
*/
function save_mibs($device, $mibname, $oids, $mibdef, &$graphs)
{
$usedoids = array();
foreach ($oids as $index => $array) {
foreach ($array as $oid => $val) {
$type = oid_rrd_type($oid, $mibdef);
if (!$type) {
continue;
}
$usedoids[$index][$oid] = $val;
rrd_create_update(
$device,
array($mibname, $mibdef[$oid]['shortname'], $index),
array("DS:".$mibdef[$oid]['dsname'].":$type"),
"N:$val"
);
}
}
tag_graphs($mibname, $usedoids, $mibdef, $graphs);
update_mib_graph_types($mibname, $usedoids, $mibdef, $graphs);
}
/*
* Take a list of MIB name => module pairs.
* 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")) {
d_echo("MIB module disabled for ".$device['hostname']);
@@ -1038,15 +1093,13 @@ function poll_mibs($list, $device)
}
$mibdefs = array();
foreach ($list as $name => $module) {
d_echo("MIB searching: $module::$name");
$translated = snmp_translate($name, $module);
if ($translated) {
$mod = $translated[0];
$nam = $translated[1];
d_echo("MIB found: $mod::$nam");
$mibdefs[$nam] = snmp_mib_load($nam, $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 {
d_echo("MIB: no match for $module::$name");