From 874dc5ede554b0fe6e972812bbef815038c00227 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Tue, 2 Jun 2015 23:56:09 +1000 Subject: [PATCH 01/42] Start at generic MIB parsing --- includes/snmp.inc.php | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index c06d71da7..6c8b64a1a 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -818,4 +818,107 @@ function snmp_gen_auth (&$device) return $cmd; } +/* + * 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) +{ + global $debug; + + $lastpart = end(explode(".", $oid)); + + $cmd = "snmptranslate -Td -On"; + $cmd .= mibdir($mibdir); + $cmd .= " -m ".$module." ".$module."::"; + $cmd .= $lastpart; + + $result = array(); + $lines = preg_split('/\n+/', trim(external_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[strtolower($f[1])] = $f[0]; + continue; + } + // then the other data elements + if ($f[0] == "--" && $f[1] == "FROM") { + $result[strtolower($f[1])] = $f[2]; + continue; + } + if ($f[0] == "MAX-ACCESS") { + $result[strtolower($f[0])] = $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; + } + } + // This gets rid of the main mib entry that doesn't have any useful data in it + if (isset($result['syntax'])) { + $result['mib'] = $mib; + return $result; + } + else { + return null; + } +} + + +/* + * 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) +{ + $cmd = "snmptranslate -Ts"; + $cmd .= mibdir($mibdir); + $cmd .= " -m ".$module; + $result = array(); + $data = preg_split('/\n+/', external_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); + if ($obj) { + $result[] = $obj; + } + } + } + return $result; +} + ?> From d969ef679b8cbe76d15f1c1e223919c9c593c3aa Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 6 Jun 2015 21:17:24 +1000 Subject: [PATCH 02/42] First cut at MIB-based discovery; not efficient --- includes/defaults.inc.php | 1 + includes/discovery/mib.inc.php | 53 +++++++++++ includes/discovery/os/ruckuswireless.inc.php | 8 ++ includes/snmp.inc.php | 95 +++++++++++++++++--- 4 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 includes/discovery/mib.inc.php diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php index 739cc1127..fa8dda436 100644 --- a/includes/defaults.inc.php +++ b/includes/defaults.inc.php @@ -565,6 +565,7 @@ $config['discovery_modules']['toner'] = 1; $config['discovery_modules']['ucd-diskio'] = 1; $config['discovery_modules']['services'] = 1; $config['discovery_modules']['charge'] = 1; +$config['discovery_modules']['mib'] = 0; $config['modules_compat']['rfc1628']['liebert'] = 1; $config['modules_compat']['rfc1628']['netmanplus'] = 1; diff --git a/includes/discovery/mib.inc.php b/includes/discovery/mib.inc.php new file mode 100644 index 000000000..5d1d608e0 --- /dev/null +++ b/includes/discovery/mib.inc.php @@ -0,0 +1,53 @@ + + * + * 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. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +/* + * The overall plan for MIB-based support is: + * + * Discovery: + * 1. SNMP_get sysObjectID; look for a MIB matching this object (set_os_mib). + * 2. Add any extra MIBs that should be there for a given device + * (includes/discovery/os/*.inc.php). + * 3. Walk these MIBs to see if they exist in the device (this file). + * Save the ones that do in the database table device_oids. + * + * Polling: + * 5. For each MIB in the device_oids table, walk the device for that MIB. + * 6. Save each MIB value in its own RRD file. (At present there is no + * deletion of values that disappear.) + * + * Graphing: + * 7. For each MIB in the device_oids table, create a graph from the RRD + * file. All graphs go into the MIB section at present. + */ + +set_os_mib($device); +$mibs = array(); + +// remove any existing device_oids for this device +dbDelete('device_oids', 'device_id = ?', array($device['device_id'])); + +// parse MIBs and check for them on the device +foreach ($device['mibs'] as $name => $module) { + d_echo("MIB discovery: $name, $module"); + $mibs[$name] = snmp_mib_load($name, $module); + $oids = snmpwalk_cache_oid($device, "$module::$name", array(), $module); + + // add the oids for this device + foreach ($oids[0] as $key => $val) { + $data = $mibs[$name][$key]; + $data['device_id'] = $device['device_id']; + $result = dbInsert($data, 'device_oids'); + d_echo("dbInsert for $name $key returned $result"); + } +} diff --git a/includes/discovery/os/ruckuswireless.inc.php b/includes/discovery/os/ruckuswireless.inc.php index edc1c90f9..3e7325eb0 100644 --- a/includes/discovery/os/ruckuswireless.inc.php +++ b/includes/discovery/os/ruckuswireless.inc.php @@ -3,6 +3,7 @@ * LibreNMS Ruckus Wireless OS information module * * Copyright (c) 2015 Søren Friis Rosiak +* Copyright (c) 2015 Gear Consulting Pty Ltd * 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 @@ -14,4 +15,11 @@ if (!$os) { $os = "ruckuswireless"; } } + +if ($os == "ruckuswireless") { + $ruckus_mibs = array( + "ruckusZDSystemStats" => "RUCKUS-ZD-SYSTEM-MIB", + ); + set_mibs($ruckus_mibs, $device); +} ?> diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 6c8b64a1a..68d119aeb 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -819,15 +819,17 @@ function snmp_gen_auth (&$device) } /* + * Translate the given MIB into a vaguely useful 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" + * -- 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 } */ @@ -843,7 +845,7 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null) $cmd .= $lastpart; $result = array(); - $lines = preg_split('/\n+/', trim(external_exec($cmd))); + $lines = preg_split('/\n+/', trim(shell_exec($cmd))); foreach ($lines as $l) { $f = preg_split('/\s+/', trim($l)); // first line is all numeric @@ -853,16 +855,16 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null) } // then the name of the object type if ($f[1] && $f[1] == "OBJECT-TYPE") { - $result[strtolower($f[1])] = $f[0]; + $result['object_type'] = $f[0]; continue; } // then the other data elements if ($f[0] == "--" && $f[1] == "FROM") { - $result[strtolower($f[1])] = $f[2]; + $result['module'] = $f[2]; continue; } if ($f[0] == "MAX-ACCESS") { - $result[strtolower($f[0])] = $f[1]; + $result['max_access'] = $f[1]; continue; } if ($f[0] == "STATUS") { @@ -895,6 +897,10 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null) /* + * 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 @@ -908,7 +914,7 @@ function snmp_mib_walk($mib, $module, $mibdir = null) $cmd .= mibdir($mibdir); $cmd .= " -m ".$module; $result = array(); - $data = preg_split('/\n+/', external_exec($cmd)); + $data = preg_split('/\n+/', shell_exec($cmd)); foreach ($data as $oid) { // only include oids which are part of this mib if (strstr($oid, $mib)) { @@ -921,4 +927,73 @@ function snmp_mib_walk($mib, $module, $mibdir = null) return $result; } +/* + * @return an array containing all of the mib objects, keyed by object-type; + * returns an empty array if something goes wrong. + */ +function snmp_mib_load($mib, $module, $mibdir = null) +{ + $mibs = array(); + foreach (snmp_mib_walk($mib, $module, $mibdir) as $obj) { + $mibs[$obj['object_type']] = $obj; + } + return $mibs; +} + +/* + * 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. + */ +function snmp_translate($oid, $module, $mibdir = null) +{ + if ($module !== "all") { + $oid = "$module::$oid"; + } + $cmd = "snmptranslate" . mibdir($mibdir); + $cmd .= " -m $module $oid"; // load all the MIBs looking for our object + $cmd .= " 2>/dev/null"; // ignore invalid MIBs + + $lines = preg_split('/\n+/', shell_exec($cmd)); + if (!$lines) { + d_echo("No results from snmptranslate"); + return null; + } + + $matches = array(); + if (!preg_match('/(.*)::(.*)/', $lines[0], $matches)) { + d_echo("This doesn't look like a MIB: ".$lines[0]); + return null; + } + + d_echo("SNMP translated: $module::$oid -> $matches[1]::$matches[2]"); + return $matches; +} + +/* + * Validate MIBs and set $device['mibs'][$name] = $module based on the results. + * Can be slow due to use of snmptranslate - call only during discovery. + */ +function set_mibs($list, &$device) +{ + foreach ($list as $name => $module) { + $matches = snmp_translate($name, $module); + if ($matches) { + $device['mibs'][$matches[2]] = $matches[1]; + } + } +} + +/* + * Validate the MIB given in sysObjectId against our MIB collection. If none is found, do nothing. + * If one is found, call set_mibs() for the given MIB name & module. + */ +function set_os_mib(&$device) +{ + $sysObjectId = trim(snmp_get($device, "SNMPv2-MIB::sysObjectID.0", "-Ovqn")); + if ($sysObjectId === false || $sysObjectID === "") { + return; + } + set_mibs(array($sysObjectId => "all"), $device); +} + ?> From ddb66841082b24efd09b64337861759fef126a56 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 6 Jun 2015 22:43:30 +1000 Subject: [PATCH 03/42] Move MIB-based polling out of discovery altogether --- includes/discovery/os/ruckuswireless.inc.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/includes/discovery/os/ruckuswireless.inc.php b/includes/discovery/os/ruckuswireless.inc.php index 3e7325eb0..edc1c90f9 100644 --- a/includes/discovery/os/ruckuswireless.inc.php +++ b/includes/discovery/os/ruckuswireless.inc.php @@ -3,7 +3,6 @@ * LibreNMS Ruckus Wireless OS information module * * Copyright (c) 2015 Søren Friis Rosiak -* Copyright (c) 2015 Gear Consulting Pty Ltd * 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 @@ -15,11 +14,4 @@ if (!$os) { $os = "ruckuswireless"; } } - -if ($os == "ruckuswireless") { - $ruckus_mibs = array( - "ruckusZDSystemStats" => "RUCKUS-ZD-SYSTEM-MIB", - ); - set_mibs($ruckus_mibs, $device); -} ?> From 2a18afe3afcefc683dd2c8f6d104973e8908ea52 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 6 Jun 2015 22:44:32 +1000 Subject: [PATCH 04/42] Don't search junk --- contrib/findit | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/findit b/contrib/findit index 6c89281ca..14151b2f9 100755 --- a/contrib/findit +++ b/contrib/findit @@ -5,6 +5,7 @@ find . \ -path ./html/includes/geshi -prune -o \ -path ./html/includes/jpgraph -prune -o \ -path ./html/js/jqplot -prune -o \ + -path ./junk -prune -o \ -path ./logs -prune -o \ -path ./mibs -prune -o \ -path ./rrd -prune -o \ From 5bdf67ba6e81772318023d14928dcd1fc5351152 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 6 Jun 2015 22:46:01 +1000 Subject: [PATCH 05/42] Move MIB-based polling out of discovery altogether --- doc/General/MIB-based-polling.md | 27 ++++++++++++++++ includes/defaults.inc.php | 2 +- includes/discovery/mib.inc.php | 53 -------------------------------- 3 files changed, 28 insertions(+), 54 deletions(-) create mode 100644 doc/General/MIB-based-polling.md delete mode 100644 includes/discovery/mib.inc.php diff --git a/doc/General/MIB-based-polling.md b/doc/General/MIB-based-polling.md new file mode 100644 index 000000000..cb5ccb012 --- /dev/null +++ b/doc/General/MIB-based-polling.md @@ -0,0 +1,27 @@ +The overall design of MIB-based support is: + +Discovery: + - MIBs are not involved; any work done here would have to be + duplicated by the poller and thus would only increase load. + +Polling: + - Look for a MIB matching sysObjectID in the MIB directory; if one + is found: + - parse it + - walk that MIB on the device + - store any numeric results in individual RRD files + - update/add graph definitions in the database + - Individual OSes can add extra MIBs that should be there for a given + OS (includes/polling/os/*.inc.php). The MIB poller will poll, + store, and update graph definitions for them after attempting the + sysObjectID-based MIB poll. + - Devices may be excluded from MIB polling by adding poll_mib = 0 to + devices_attribs (see /device/device=ID/tab=edit/section=modules/) + +Graphing: + - For each file in the device directory, create a graph using the + definition in the database. Future enhancements: + - Allow graphs to go in different sections + - Allow graphs to be combined automatically or on a user-defined + basis. + diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php index fa8dda436..8c02f3779 100644 --- a/includes/defaults.inc.php +++ b/includes/defaults.inc.php @@ -533,6 +533,7 @@ $config['poller_modules']['aruba-controller'] = 1; $config['poller_modules']['entity-physical'] = 1; $config['poller_modules']['applications'] = 1; $config['poller_modules']['cisco-asa-firewall'] = 1; +$config['poller_modules']['mib'] = 0; // List of discovery modules. Need to be in this array to be // considered for execution. @@ -565,7 +566,6 @@ $config['discovery_modules']['toner'] = 1; $config['discovery_modules']['ucd-diskio'] = 1; $config['discovery_modules']['services'] = 1; $config['discovery_modules']['charge'] = 1; -$config['discovery_modules']['mib'] = 0; $config['modules_compat']['rfc1628']['liebert'] = 1; $config['modules_compat']['rfc1628']['netmanplus'] = 1; diff --git a/includes/discovery/mib.inc.php b/includes/discovery/mib.inc.php deleted file mode 100644 index 5d1d608e0..000000000 --- a/includes/discovery/mib.inc.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * 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. Please see LICENSE.txt at the top level of - * the source code distribution for details. - */ - -/* - * The overall plan for MIB-based support is: - * - * Discovery: - * 1. SNMP_get sysObjectID; look for a MIB matching this object (set_os_mib). - * 2. Add any extra MIBs that should be there for a given device - * (includes/discovery/os/*.inc.php). - * 3. Walk these MIBs to see if they exist in the device (this file). - * Save the ones that do in the database table device_oids. - * - * Polling: - * 5. For each MIB in the device_oids table, walk the device for that MIB. - * 6. Save each MIB value in its own RRD file. (At present there is no - * deletion of values that disappear.) - * - * Graphing: - * 7. For each MIB in the device_oids table, create a graph from the RRD - * file. All graphs go into the MIB section at present. - */ - -set_os_mib($device); -$mibs = array(); - -// remove any existing device_oids for this device -dbDelete('device_oids', 'device_id = ?', array($device['device_id'])); - -// parse MIBs and check for them on the device -foreach ($device['mibs'] as $name => $module) { - d_echo("MIB discovery: $name, $module"); - $mibs[$name] = snmp_mib_load($name, $module); - $oids = snmpwalk_cache_oid($device, "$module::$name", array(), $module); - - // add the oids for this device - foreach ($oids[0] as $key => $val) { - $data = $mibs[$name][$key]; - $data['device_id'] = $device['device_id']; - $result = dbInsert($data, 'device_oids'); - d_echo("dbInsert for $name $key returned $result"); - } -} From 6874d329044b80f028b8dc2b0af067b98be649f4 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 6 Jun 2015 22:46:36 +1000 Subject: [PATCH 06/42] Save results of polling sysObjectID to the database --- includes/polling/system.inc.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/includes/polling/system.inc.php b/includes/polling/system.inc.php index 4cf0c7754..95c43aa22 100644 --- a/includes/polling/system.inc.php +++ b/includes/polling/system.inc.php @@ -113,6 +113,12 @@ log_event("Contact -> ".$poll_device['sysContact'], $device, 'system'); } + if ($poll_device['sysObjectID'] && $poll_device['sysObjectID'] != $device['sysObjectID']) + { + $update_array['sysObjectID'] = $poll_device['sysObjectID']; + log_event("ObjectID -> ".$poll_device['sysObjectID'], $device, 'system'); + } + if ($poll_device['sysName'] && $poll_device['sysName'] != $device['sysName']) { $update_array['sysName'] = $poll_device['sysName']; From f27de0b8ff1e8ba6119cba0495e7ceb43c838f43 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Mon, 8 Jun 2015 12:56:20 +1000 Subject: [PATCH 07/42] Remove duplicate code --- includes/snmp.inc.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 68d119aeb..c9ff65ca0 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -322,9 +322,9 @@ function snmp_cache_ifIndex($device) return $array; } -function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) +function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL, $snmpflags = "-OQUs") { - $data = snmp_walk($device, $oid, "-OQUs", $mib, $mibdir); + $data = snmp_walk($device, $oid, $snmpflags, $mib, $mibdir); foreach (explode("\n", $data) as $entry) { list($oid,$value) = explode("=", $entry, 2); @@ -345,20 +345,7 @@ function snmpwalk_cache_oid($device, $oid, $array, $mib = NULL, $mibdir = NULL) // to be the same. function snmpwalk_cache_oid_num($device, $oid, $array, $mib = NULL, $mibdir = NULL) { - $data = snmp_walk($device, $oid, "-OQUn", $mib, $mibdir); - - foreach (explode("\n", $data) as $entry) - { - list($oid,$value) = explode("=", $entry, 2); - $oid = trim($oid); $value = trim($value); - list($oid, $index) = explode(".", $oid, 2); - if (!strstr($value, "at this OID") && isset($oid) && isset($index)) - { - $array[$index][$oid] = $value; - } - } - - return $array; + return snmpwalk_cache_oid($device, $oid, $array, $mib, $mibdir, $snmpflags = "-OQUn"); } From 8f5bc4cdb6886dcb82b313df417b30e24703f09a Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Mon, 8 Jun 2015 14:56:12 +1000 Subject: [PATCH 08/42] Minor tidies --- doc/Support/Poller Support.md | 4 ++-- includes/polling/functions.inc.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/Support/Poller Support.md b/doc/Support/Poller Support.md index 4e06b54df..0a980e251 100644 --- a/doc/Support/Poller Support.md +++ b/doc/Support/Poller Support.md @@ -112,7 +112,7 @@ $config['poller_modules']['cisco-asa-firewall'] = 1; `ospf`: OSPF Support. -`cisco-ipsec-flow-monitor': IPSec statistics support. +`cisco-ipsec-flow-monitor`: IPSec statistics support. `cisco-remote-access-monitor`: Cisco remote access support. @@ -176,4 +176,4 @@ DB Updates RRD Updates -SNMP Response \ No newline at end of file +SNMP Response diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index a2ec23be9..9700edb2d 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -266,7 +266,7 @@ function poll_mib_def($device, $mib_name_table, $mib_subdir, $mib_oids, $mib_gra global $config; - echo("This is mag_poll_mib_def Processing\n"); + echo("This is poll_mib_def Processing\n"); $mib = NULL; if (stristr($mib_name_table, "UBNT")) { From 4af7dd8225b94bc7432bbf4211f5d6be4ebdf9a4 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Mon, 8 Jun 2015 14:58:22 +1000 Subject: [PATCH 09/42] First cut at working poller for MIBs --- doc/General/MIB-based-polling.md | 6 +- doc/Support/Poller Support.md | 3 + includes/common.php | 10 +++ includes/polling/functions.inc.php | 24 ++++++ includes/polling/os/ruckuswireless.inc.php | 6 ++ includes/snmp.inc.php | 98 ++++++++++++++++++---- 6 files changed, 129 insertions(+), 18 deletions(-) diff --git a/doc/General/MIB-based-polling.md b/doc/General/MIB-based-polling.md index cb5ccb012..f21bc4816 100644 --- a/doc/General/MIB-based-polling.md +++ b/doc/General/MIB-based-polling.md @@ -11,10 +11,8 @@ Polling: - walk that MIB on the device - store any numeric results in individual RRD files - update/add graph definitions in the database - - Individual OSes can add extra MIBs that should be there for a given - OS (includes/polling/os/*.inc.php). The MIB poller will poll, - store, and update graph definitions for them after attempting the - sysObjectID-based MIB poll. + - Individual OSes (includes/polling/os/*.inc.php) can poll extra MIBs + that should be there for a given OS by calling poll_mib(). - Devices may be excluded from MIB polling by adding poll_mib = 0 to devices_attribs (see /device/device=ID/tab=edit/section=modules/) diff --git a/doc/Support/Poller Support.md b/doc/Support/Poller Support.md index 0a980e251..616698630 100644 --- a/doc/Support/Poller Support.md +++ b/doc/Support/Poller Support.md @@ -70,6 +70,7 @@ $config['poller_modules']['aruba-controller'] = 1; $config['poller_modules']['entity-physical'] = 1; $config['poller_modules']['applications'] = 1; $config['poller_modules']['cisco-asa-firewall'] = 1; +$config['poller_modules']['mib'] = 0; ``` #### Poller modules @@ -138,6 +139,8 @@ $config['poller_modules']['cisco-asa-firewall'] = 1; `cisco-asa-firewall`: Cisco ASA firewall support. +`mib`: Support for generic MIB parsing. + #### Running Here are some examples of running poller from within your install directory. diff --git a/includes/common.php b/includes/common.php index d14395e7f..9f94a2b65 100644 --- a/includes/common.php +++ b/includes/common.php @@ -453,6 +453,16 @@ function get_dev_attribs($device) return $attribs; } +function is_dev_attrib_enabled($device, $attrib) +{ + foreach (get_dev_attribs($device) as $name => $val) { + if ($name == $attrib && $val == 0) { + return false; + } + } + return true; +} + function get_dev_entity_state($device) { $state = array(); diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 9700edb2d..5df7b181d 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -346,4 +346,28 @@ function poll_mib_def($device, $mib_name_table, $mib_subdir, $mib_oids, $mib_gra return TRUE; } +/* + * Please use this instead of creating & updating RRD files manually. + * @param device Device object - only 'hostname' is used at present + * @param name Array of name objects - filename will be constructed using this with a "-" delimiter + * @param def Array of data definitions + * @param val Array of value definitions + * + */ +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 + $newdef = "--step $step ".implode(' ', $def).$config['rrd_rra']; + d_echo("Creating RRD $rrd: $newdef"); + rrdtool_create($rrd, $newdef); + } + + rrdtool_update($rrd, $val); +} + ?> diff --git a/includes/polling/os/ruckuswireless.inc.php b/includes/polling/os/ruckuswireless.inc.php index 1c9b1b0ec..31776ae77 100644 --- a/includes/polling/os/ruckuswireless.inc.php +++ b/includes/polling/os/ruckuswireless.inc.php @@ -39,4 +39,10 @@ $ruckuscountry = first_oid_match($device, $ruckuscountries); if (isset($ruckuscountry) && $ruckuscountry != "") { $version .= " ($ruckuscountry)"; } + +$ruckus_mibs = array( + "ruckusZDSystemStats" => "RUCKUS-ZD-SYSTEM-MIB", +); +poll_mibs($ruckus_mibs, $device); + ?> diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index c9ff65ca0..b25e31382 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -953,34 +953,104 @@ function snmp_translate($oid, $module, $mibdir = null) } d_echo("SNMP translated: $module::$oid -> $matches[1]::$matches[2]"); - return $matches; + return array($matches[1], $matches[2]); } /* - * Validate MIBs and set $device['mibs'][$name] = $module based on the results. - * Can be slow due to use of snmptranslate - call only during discovery. + * 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 set_mibs($list, &$device) +function oid_rrd_type($oid, $mibdefs) { + if (!isset($mibdefs[$oid])) { + return false; + } + switch ($mibdefs[$oid]['syntax']) { + + case 'OCTET': + case 'IpAddress': + return false; + + case 'TimeTicks': + // Need to find a way to flag that this should be parsed + //return 'COUNTER'; + return false; + + case 'Counter64': + return 'COUNTER:600:0:U'; + + case 'Unsigned32': + return 'GAUGE:600:U:U'; + + } + return false; +} + +/* + * Shorten the RRD variable name to less than 19 characters + * Substitute for "mibval" if necessary. + */ +function rrd_shorten($name, $prefix) { - foreach ($list as $name => $module) { - $matches = snmp_translate($name, $module); - if ($matches) { - $device['mibs'][$matches[2]] = $matches[1]; + 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. + */ +function save_mibs($device, $mibname, $oids, $mibdefs) +{ + 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, $oid, $index), + array("DS:".rrd_shorten($oid, $mibname).":$type"), + "N:$val" + ); } } } /* - * Validate the MIB given in sysObjectId against our MIB collection. If none is found, do nothing. - * If one is found, call set_mibs() for the given MIB name & module. + * 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 set_os_mib(&$device) +function poll_mibs($list, $device) { - $sysObjectId = trim(snmp_get($device, "SNMPv2-MIB::sysObjectID.0", "-Ovqn")); - if ($sysObjectId === false || $sysObjectID === "") { + if (!is_dev_attrib_enabled($device, "poll_mib")) { + d_echo("MIB module disabled for ".$device['hostname']); return; } - set_mibs(array($sysObjectId => "all"), $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]); + } + else { + d_echo("MIB: no match for $module::$name"); + } + } } ?> From 1ddaa5c288518b5c37e388f56c39984ab7978250 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Mon, 8 Jun 2015 15:12:32 +1000 Subject: [PATCH 10/42] Minor fixup to MIB polling --- includes/snmp.inc.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index b25e31382..4552348b0 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -1005,6 +1005,7 @@ function rrd_shorten($name, $prefix) /* * 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) { @@ -1016,7 +1017,7 @@ function save_mibs($device, $mibname, $oids, $mibdefs) } rrd_create_update( $device, - array($mibname, $oid, $index), + array($mibname, str_replace($mibname, '', $oid), $index), array("DS:".rrd_shorten($oid, $mibname).":$type"), "N:$val" ); From e77d4d3063bf17cc3d25d405dbe130a89848ca48 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Mon, 8 Jun 2015 21:50:50 +1000 Subject: [PATCH 11/42] Reverse the logic to make it clearer what is happening --- includes/polling/functions.inc.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 5df7b181d..c13316114 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -201,11 +201,11 @@ function poll_device($device, $options) foreach (dbFetch("SELECT `graph` FROM `device_graphs` WHERE `device_id` = ?", array($device['device_id'])) as $graph) { - if (!isset($graphs[$graph["graph"]])) + if (isset($graphs[$graph["graph"]])) { - dbDelete('device_graphs', "`device_id` = ? AND `graph` = ?", array($device['device_id'], $graph["graph"])); - } else { $oldgraphs[$graph["graph"]] = TRUE; + } else { + dbDelete('device_graphs', "`device_id` = ? AND `graph` = ?", array($device['device_id'], $graph["graph"])); } } From 984ec0d06037d468581a8416d284ad43da66093d Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Tue, 9 Jun 2015 00:12:43 +1000 Subject: [PATCH 12/42] Save MIB graphs in database, tag devices for graphing --- includes/polling/functions.inc.php | 1 - includes/polling/os/ruckuswireless.inc.php | 2 +- includes/snmp.inc.php | 135 ++++++++++++++------- 3 files changed, 95 insertions(+), 43 deletions(-) diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index c13316114..b01d03e80 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -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 diff --git a/includes/polling/os/ruckuswireless.inc.php b/includes/polling/os/ruckuswireless.inc.php index 31776ae77..6e65a47b2 100644 --- a/includes/polling/os/ruckuswireless.inc.php +++ b/includes/polling/os/ruckuswireless.inc.php @@ -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); ?> diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 4552348b0..40bf0aa33 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -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"); From 38441130e88d26389263aa7300b8a2c1cdd10b96 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Wed, 10 Jun 2015 20:00:09 +1000 Subject: [PATCH 13/42] Minor clarification on OS support --- doc/Support/FAQ.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/Support/FAQ.md b/doc/Support/FAQ.md index ef6d6232e..4fb4d74cb 100644 --- a/doc/Support/FAQ.md +++ b/doc/Support/FAQ.md @@ -103,7 +103,8 @@ To go into a bit more detail, the following are usually needed: **includes/definitions.inc.php** Update this file to include the required definitions for the new OS. **includes/discovery/os/ciscowlc.inc.php** -This file just sets the $os variable, done by checking the sysDescr snmp value for a particular value that matches the OS you are adding. +This file just sets the $os variable, done by checking the SNMP tree for a particular value that matches the OS you are adding. Typically, this will come from the presence of specific values in +sysObjectID or sysDescr, or the existence of a particular enterprise tree. **includes/polling/os/ciscowlc.inc.php** This file will usually set the variables for $version and $hardware gained from an snmp lookup. **html/images/os/$os.png** From e02ed25deef035d104c5ac320df269a6d1ae87f5 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Wed, 10 Jun 2015 20:03:32 +1000 Subject: [PATCH 14/42] temporary testing graph --- includes/definitions.inc.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/includes/definitions.inc.php b/includes/definitions.inc.php index 090702c33..ec244cfcd 100644 --- a/includes/definitions.inc.php +++ b/includes/definitions.inc.php @@ -1176,8 +1176,8 @@ foreach ($config['os'] as $this_os => $blah) // Graph Types -$config['graph_sections'] = array('general', 'system', 'firewall', 'netstats', 'wireless', 'storage', 'vpdn', 'load balancer'); +// FIXME: Load graph_types from database here so that config overrides database // Device - Wireless - AirMAX $config['graph_types']['device']['ubnt_airmax_WlStatStaCount'] = array( @@ -1534,6 +1534,18 @@ $config['graph_types']['device']['siklu_rfinterfaceOtherOctets'] = array( ) ); +$config['graph_types']['device']['ruckusZD1000-SystemLicensedAPs'] = array( + 'section' => 'MIB', + 'order' => '1', + 'descr' => 'Licensed APs', + 'file' => 'ruckusZD1000-SystemLicensedAPs-0.rrd', + 'colours' => 'reds', + 'unit_text' => 'Number', + 'ds' => array( + 'SystemLicensedAPs' => array('label' => 'LicensedAPs', 'draw' => 'LINE', 'line' => TRUE) + ) +); + $config['graph_types']['device']['wifi_clients']['section'] = 'wireless'; $config['graph_types']['device']['wifi_clients']['order'] = '0'; $config['graph_types']['device']['wifi_clients']['descr'] = 'Wireless Clients'; From 78bf0b7a0a97bc52391c444e054b2695c8063809 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Wed, 10 Jun 2015 20:04:38 +1000 Subject: [PATCH 15/42] Remove inaccurate references to graph_sections --- html/pages/device/graphs.inc.php | 6 +++--- html/pages/device/munin.inc.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/html/pages/device/graphs.inc.php b/html/pages/device/graphs.inc.php index 3be4ad7e7..49b4d7691 100644 --- a/html/pages/device/graphs.inc.php +++ b/html/pages/device/graphs.inc.php @@ -1,6 +1,5 @@ 'device', @@ -20,14 +19,15 @@ $sep = ""; foreach (dbFetchRows("SELECT * FROM device_graphs WHERE device_id = ? ORDER BY graph", array($device['device_id'])) as $graph) { $section = $config['graph_types']['device'][$graph['graph']]['section']; - $graph_enable[$section][$graph['graph']] = $graph['graph']; + if ($section != "") { + $graph_enable[$section][$graph['graph']] = $graph['graph']; + } } // These are standard graphs we should have for all systems $graph_enable['poller']['poller_perf'] = 'device_poller_perf'; $graph_enable['poller']['ping_perf'] = 'device_ping_perf'; -#foreach ($config['graph_sections'] as $section) foreach ($graph_enable as $section => $nothing) { if (isset($graph_enable) && is_array($graph_enable[$section])) diff --git a/html/pages/device/munin.inc.php b/html/pages/device/munin.inc.php index a061530da..67fd22d6e 100644 --- a/html/pages/device/munin.inc.php +++ b/html/pages/device/munin.inc.php @@ -1,6 +1,5 @@ 'device', From 982faeb86921f53e2d227a2e23a6c51356b2b1be Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 09:26:26 +1000 Subject: [PATCH 16/42] Implement more efficient is_dev_attrib_enabled() --- includes/common.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/includes/common.php b/includes/common.php index 9f94a2b65..1d1896148 100644 --- a/includes/common.php +++ b/includes/common.php @@ -453,16 +453,6 @@ function get_dev_attribs($device) return $attribs; } -function is_dev_attrib_enabled($device, $attrib) -{ - foreach (get_dev_attribs($device) as $name => $val) { - if ($name == $attrib && $val == 0) { - return false; - } - } - return true; -} - function get_dev_entity_state($device) { $state = array(); @@ -486,6 +476,19 @@ function get_dev_attrib($device, $attrib_type) } } +function is_dev_attrib_enabled($device, $attrib, $default = true) +{ + $val = get_dev_attrib($device, $attrib); + if ($val != NULL) { + // attribute is set + return ($val != 0); + } + else { + // attribute not set + return $default; + } +} + function del_dev_attrib($device, $attrib_type) { return dbDelete('devices_attribs', "`device_id` = ? AND `attrib_type` = ?", array($device['device_id'], $attrib_type)); From db2f57f2bc5a7fe3d563da6b819bdade5137d4cf Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 14:55:40 +1000 Subject: [PATCH 17/42] Poll sysObjectID by default --- includes/polling/mib.inc.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 includes/polling/mib.inc.php diff --git a/includes/polling/mib.inc.php b/includes/polling/mib.inc.php new file mode 100644 index 000000000..8ce1ad67f --- /dev/null +++ b/includes/polling/mib.inc.php @@ -0,0 +1,20 @@ + + * + * 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. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +d_echo("MIB-based polling"); + +$devicemib = array($device['sysObjectID'] => "all"); +poll_mibs($devicemib, $device, $graphs); + +d_echo("Done MIB-based polling"); +?> From c5c0c1f0a03ee320f84231bf924ec2fd6a518a81 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 15:21:58 +1000 Subject: [PATCH 18/42] Move $sep to where it is actually used --- html/pages/device/graphs.inc.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/html/pages/device/graphs.inc.php b/html/pages/device/graphs.inc.php index 49b4d7691..a275e71e3 100644 --- a/html/pages/device/graphs.inc.php +++ b/html/pages/device/graphs.inc.php @@ -14,8 +14,6 @@ print_optionbar_start(); echo("Graphs » "); -$sep = ""; - foreach (dbFetchRows("SELECT * FROM device_graphs WHERE device_id = ? ORDER BY graph", array($device['device_id'])) as $graph) { $section = $config['graph_types']['device'][$graph['graph']]['section']; @@ -28,6 +26,7 @@ foreach (dbFetchRows("SELECT * FROM device_graphs WHERE device_id = ? ORDER BY g $graph_enable['poller']['poller_perf'] = 'device_poller_perf'; $graph_enable['poller']['ping_perf'] = 'device_ping_perf'; +$sep = ""; foreach ($graph_enable as $section => $nothing) { if (isset($graph_enable) && is_array($graph_enable[$section])) @@ -47,8 +46,8 @@ foreach ($graph_enable as $section => $nothing) $sep = " | "; } } - unset ($sep); + print_optionbar_end(); $graph_enable = $graph_enable[$vars['group']]; From 96bee673945e1244023ddbc7dd8a9725eab50f34 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 15:22:43 +1000 Subject: [PATCH 19/42] Do not attempt to create file if there are no DS definitions --- includes/polling/functions.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index b01d03e80..77e7d7825 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -359,7 +359,7 @@ function rrd_create_update($device, $name, $def, $val, $step = 300) global $config; $rrd = implode("/", array($config['rrd_dir'], $device['hostname'], safename(implode("-", $name)).".rrd")); - if (!is_file($rrd)) { + if (!is_file($rrd) && $def != null) { // add the --step and the rra definitions to the array $newdef = "--step $step ".implode(' ', $def).$config['rrd_rra']; d_echo("Creating RRD $rrd: $newdef"); From 0cc75d7d1de48f2bebee6bd6de6fd22128a0bb17 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 16:13:12 +1000 Subject: [PATCH 20/42] Separate convenience function for RRD filename generation; tidy formatting --- includes/functions.php | 18 ++++++++++++++++-- includes/polling/functions.inc.php | 4 ++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index bcde5c598..d3846c980 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1251,7 +1251,8 @@ function ip_exists($ip) { /* * convenience function - please use this instead of 'if ($debug) { echo ...; }' */ -function d_echo($text) { +function d_echo($text) +{ global $debug; if ($debug) { echo "$text\n"; @@ -1261,9 +1262,22 @@ function d_echo($text) { /* * convenience function - please use this instead of 'if ($debug) { print_r ...; }' */ -function d_print_r($var) { +function d_print_r($var) +{ global $debug; if ($debug) { print_r($var); } } + +/* + * @return the name of the rrd file for $host's $extra component + * @param host Host name + * @param extra Components of RRD filename - will be separated with "-" + */ +function rrdname($host, $extra) +{ + global $config; + return implode("/", array($config['rrd_dir'], $host, safename(implode("-", $extra)).".rrd")); +} + diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 77e7d7825..006dbaf7f 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -349,7 +349,7 @@ function poll_mib_def($device, $mib_name_table, $mib_subdir, $mib_oids, $mib_gra /* * Please use this instead of creating & updating RRD files manually. * @param device Device object - only 'hostname' is used at present - * @param name Array of name objects - filename will be constructed using this with a "-" delimiter + * @param name Array of rrdname components * @param def Array of data definitions * @param val Array of value definitions * @@ -357,7 +357,7 @@ function poll_mib_def($device, $mib_name_table, $mib_subdir, $mib_oids, $mib_gra function rrd_create_update($device, $name, $def, $val, $step = 300) { global $config; - $rrd = implode("/", array($config['rrd_dir'], $device['hostname'], safename(implode("-", $name)).".rrd")); + $rrd = rrd_name($device['hostname'], $name); if (!is_file($rrd) && $def != null) { // add the --step and the rra definitions to the array From 7ca0c547034fcf322f03d0a945ffbd0f25afd245 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 16:14:19 +1000 Subject: [PATCH 21/42] Add note about duplicate code --- html/includes/api_functions.inc.php | 1 + 1 file changed, 1 insertion(+) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index d4dce5e87..d2e9f3061 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -436,6 +436,7 @@ function get_graphs() { $router = $app->router()->getCurrentRoute()->getParams(); $hostname = $router['hostname']; + // FIXME: this has some overlap with html/pages/device/graphs.inc.php // use hostname as device_id if it's all digits $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); $graphs = array(); From 70c415124d9a29bb56c34d08fa6f1611517fb8c8 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 16:14:50 +1000 Subject: [PATCH 22/42] Remove graph_types note --- includes/definitions.inc.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/definitions.inc.php b/includes/definitions.inc.php index ec244cfcd..2604b6381 100644 --- a/includes/definitions.inc.php +++ b/includes/definitions.inc.php @@ -1177,8 +1177,6 @@ foreach ($config['os'] as $this_os => $blah) // Graph Types -// FIXME: Load graph_types from database here so that config overrides database - // Device - Wireless - AirMAX $config['graph_types']['device']['ubnt_airmax_WlStatStaCount'] = array( 'section' => 'wireless', From 942e5047e5bfe7f68dd59207320918431623e1fd Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 16:15:03 +1000 Subject: [PATCH 23/42] Graph sections must be lower case --- includes/definitions.inc.php | 2 +- includes/snmp.inc.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/definitions.inc.php b/includes/definitions.inc.php index 2604b6381..ccdac23a6 100644 --- a/includes/definitions.inc.php +++ b/includes/definitions.inc.php @@ -1533,7 +1533,7 @@ $config['graph_types']['device']['siklu_rfinterfaceOtherOctets'] = array( ); $config['graph_types']['device']['ruckusZD1000-SystemLicensedAPs'] = array( - 'section' => 'MIB', + 'section' => 'mib', 'order' => '1', 'descr' => 'Licensed APs', 'file' => 'ruckusZD1000-SystemLicensedAPs-0.rrd', diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 40bf0aa33..4be24da1f 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -1044,7 +1044,7 @@ function update_mib_graph_types($mibname, $oids, $mibdef, $graphs) $graphdef = array(); $graphdef['graph_type'] = "device"; $graphdef['graph_subtype'] = $graphname; - $graphdef['graph_section'] = "MIB"; + $graphdef['graph_section'] = "mib"; $graphdef['graph_descr'] = $mibdef[$oid]['description']; $graphdef['graph_order'] = $i++; // TODO: add colours, unit_text, and ds From 6038f11db9f149b37c1bfbab3e9c77055d46b72c Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 20:49:02 +1000 Subject: [PATCH 24/42] phpcs run --- html/includes/graphs/device/auth.inc.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/html/includes/graphs/device/auth.inc.php b/html/includes/graphs/device/auth.inc.php index 063baba3f..79027e7bf 100644 --- a/html/includes/graphs/device/auth.inc.php +++ b/html/includes/graphs/device/auth.inc.php @@ -2,9 +2,9 @@ if ($auth || device_permitted($device['device_id'])) { - $title = generate_device_link($device); - $graph_title = $device['hostname']; - $auth = TRUE; + $title = generate_device_link($device); + $graph_title = $device['hostname']; + $auth = true; } ?> From e884bfa8603e034cf89374cd8a63a6377483211a Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:41:21 +1000 Subject: [PATCH 25/42] Include to read graphs from the graph_types table --- includes/load_db_graph_types.inc.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 includes/load_db_graph_types.inc.php diff --git a/includes/load_db_graph_types.inc.php b/includes/load_db_graph_types.inc.php new file mode 100644 index 000000000..b32b4e038 --- /dev/null +++ b/includes/load_db_graph_types.inc.php @@ -0,0 +1,22 @@ + + * + * 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. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +// load graph types from the database +foreach (dbFetchRows('select * from graph_types') as $graph) { + // remove leading 'graph_' from column names + foreach ($graph as $k => $v) { + $key = strpos($k, 'graph_') == 0 ? str_replace('graph_', '', $k) : $k; + $g[$key] = $v; + } + $config['graph_types'][$g['type']][$g['subtype']] = $g; +} From c873cafbae6225baa6ff1a433002d4d550ca8e07 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:45:03 +1000 Subject: [PATCH 26/42] Move client authorization checks to a separate function --- html/includes/graphs/graph.inc.php | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/html/includes/graphs/graph.inc.php b/html/includes/graphs/graph.inc.php index 7e609b6ed..c0a6906e8 100644 --- a/html/includes/graphs/graph.inc.php +++ b/html/includes/graphs/graph.inc.php @@ -38,34 +38,11 @@ $graphfile = $config['temp_dir'] . "/" . strgen() . ".png"; $type = $graphtype['type']; $subtype = $graphtype['subtype']; -if (is_file($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php")) -{ +$auth = is_client_authorized($_SERVER['REMOTE_ADDR']); +include($config['install_dir'] . "/html/includes/graphs/$type/auth.inc.php"); - if (isset($config['allow_unauth_graphs']) && $config['allow_unauth_graphs']) - { - $auth = "1"; // hardcode auth for all with config function - } - - if (isset($config['allow_unauth_graphs_cidr']) && count($config['allow_unauth_graphs_cidr']) > 0) - { - foreach ($config['allow_unauth_graphs_cidr'] as $range) - { - if (Net_IPv4::ipInNetwork($_SERVER['REMOTE_ADDR'], $range)) - { - $auth = "1"; - if ($debug) { echo("matched $range"); } - break; - } - } - } - - include($config['install_dir'] . "/html/includes/graphs/$type/auth.inc.php"); - - if (isset($auth) && $auth) - { +if ($auth && is_file($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php")) { include($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php"); - } - } else { From a2baae3ccfdd3eb9fa08c7d3593087baad6fd5e2 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:50:37 +1000 Subject: [PATCH 27/42] Move convenience functions into common; d_echo no longer adds "\n" automatically --- includes/common.php | 90 ++++++++++++++++++++++++++++++++++++++++++ includes/functions.php | 34 ---------------- includes/snmp.inc.php | 17 -------- 3 files changed, 90 insertions(+), 51 deletions(-) diff --git a/includes/common.php b/includes/common.php index 1d1896148..43c24d97f 100644 --- a/includes/common.php +++ b/includes/common.php @@ -605,4 +605,94 @@ function edit_service($service, $descr, $service_ip, $service_param = "", $servi } +/* + * convenience function - please use this instead of 'if ($debug) { echo ...; }' + */ +function d_echo($text, $no_debug_text = null) +{ + global $debug; + if ($debug) { + echo "$text"; + } + elseif ($no_debug_text) { + echo "$no_debug_text"; + } +} + +/* + * convenience function - please use this instead of 'if ($debug) { print_r ...; }' + */ +function d_print_r($var, $no_debug_text = null) +{ + global $debug; + if ($debug) { + print_r($var); + } + elseif ($no_debug_text) { + echo "$no_debug_text"; + } +} + +/* + * Shorten the name so it works as an RRD data source name (limited to 19 chars by default). + * 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; +} + +/* + * @return the name of the rrd file for $host's $extra component + * @param host Host name + * @param extra Components of RRD filename - will be separated with "-" + */ +function rrd_name($host, $extra, $exten = ".rrd") +{ + global $config; + $filename = safename(is_array($extra) ? implode("-", $extra) : $extra); + return implode("/", array($config['rrd_dir'], $host, $filename.$exten)); +} + +/* + * @return true if the given graph type is a dynamic MIB graph + */ +function is_mib_graph($type, $subtype) +{ + global $config; + return $config['graph_types'][$type][$subtype]['section'] == 'mib'; +} + +/* + * @return true if client IP address is authorized to access graphs + */ +function is_client_authorized($clientip) +{ + + global $config; + if (isset($config['allow_unauth_graphs']) && $config['allow_unauth_graphs']) { + d_echo("Unauthorized graphs allowed\n"); + return true; + } + + if (isset($config['allow_unauth_graphs_cidr'])) { + foreach ($config['allow_unauth_graphs_cidr'] as $range) { + if (Net_IPv4::ipInNetwork($clientip, $range)) { + d_echo("Unauthorized graphs allowed from $range\n"); + return true; + } + } + } + + return false; +} + ?> diff --git a/includes/functions.php b/includes/functions.php index d3846c980..96d6a5227 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1247,37 +1247,3 @@ function ip_exists($ip) { } return true; } - -/* - * convenience function - please use this instead of 'if ($debug) { echo ...; }' - */ -function d_echo($text) -{ - global $debug; - if ($debug) { - echo "$text\n"; - } -} - -/* - * convenience function - please use this instead of 'if ($debug) { print_r ...; }' - */ -function d_print_r($var) -{ - global $debug; - if ($debug) { - print_r($var); - } -} - -/* - * @return the name of the rrd file for $host's $extra component - * @param host Host name - * @param extra Components of RRD filename - will be separated with "-" - */ -function rrdname($host, $extra) -{ - global $config; - return implode("/", array($config['rrd_dir'], $host, safename(implode("-", $extra)).".rrd")); -} - diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 4be24da1f..8db5abde3 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -805,23 +805,6 @@ 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. * From ed41fa9c886b05da731b4f27fd8f9be958929fa3 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:51:26 +1000 Subject: [PATCH 28/42] A couple of minor notes for later --- includes/polling/functions.inc.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 006dbaf7f..d71b39175 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -183,6 +183,7 @@ function poll_device($device, $options) { if ($attribs['poll_'.$module] || ( $module_status && !isset($attribs['poll_'.$module]))) { + // TODO per-module polling stats include('includes/polling/'.$module.'.inc.php'); } elseif (isset($attribs['poll_'.$module]) && $attribs['poll_'.$module] == "0") { echo("Module [ $module ] disabled on host.\n"); @@ -222,6 +223,7 @@ function poll_device($device, $options) $device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5); + // TODO: These should be easy converts to rrd_create_update() // Poller performance rrd $poller_rrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/poller-perf.rrd"; if (!is_file($poller_rrd)) From 0e17cd967f4bd8f3b74e8140be5ff990861bfe91 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:53:39 +1000 Subject: [PATCH 29/42] Remove manual graph_type definition; call db load instead --- includes/definitions.inc.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/includes/definitions.inc.php b/includes/definitions.inc.php index ccdac23a6..1fb938f36 100644 --- a/includes/definitions.inc.php +++ b/includes/definitions.inc.php @@ -1176,6 +1176,8 @@ foreach ($config['os'] as $this_os => $blah) // Graph Types +include_once($config['install_dir'] . "/includes/load_db_graph_types.inc.php"); + // Device - Wireless - AirMAX $config['graph_types']['device']['ubnt_airmax_WlStatStaCount'] = array( @@ -1532,18 +1534,6 @@ $config['graph_types']['device']['siklu_rfinterfaceOtherOctets'] = array( ) ); -$config['graph_types']['device']['ruckusZD1000-SystemLicensedAPs'] = array( - 'section' => 'mib', - 'order' => '1', - 'descr' => 'Licensed APs', - 'file' => 'ruckusZD1000-SystemLicensedAPs-0.rrd', - 'colours' => 'reds', - 'unit_text' => 'Number', - 'ds' => array( - 'SystemLicensedAPs' => array('label' => 'LicensedAPs', 'draw' => 'LINE', 'line' => TRUE) - ) -); - $config['graph_types']['device']['wifi_clients']['section'] = 'wireless'; $config['graph_types']['device']['wifi_clients']['order'] = '0'; $config['graph_types']['device']['wifi_clients']['descr'] = 'Wireless Clients'; From da346c3a3546322daa85fd81edeec5045de464ac Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:54:40 +1000 Subject: [PATCH 30/42] Tidy up debug vs. non-debug output --- includes/polling/functions.inc.php | 1 - includes/polling/mib.inc.php | 4 +++- includes/snmp.inc.php | 10 +++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index d71b39175..3067d1cb7 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -364,7 +364,6 @@ function rrd_create_update($device, $name, $def, $val, $step = 300) if (!is_file($rrd) && $def != null) { // add the --step and the rra definitions to the array $newdef = "--step $step ".implode(' ', $def).$config['rrd_rra']; - d_echo("Creating RRD $rrd: $newdef"); rrdtool_create($rrd, $newdef); } diff --git a/includes/polling/mib.inc.php b/includes/polling/mib.inc.php index 8ce1ad67f..8488b2064 100644 --- a/includes/polling/mib.inc.php +++ b/includes/polling/mib.inc.php @@ -11,10 +11,12 @@ * the source code distribution for details. */ -d_echo("MIB-based polling"); +echo("MIB-based polling"); +d_echo("\n", ": "); $devicemib = array($device['sysObjectID'] => "all"); poll_mibs($devicemib, $device, $graphs); d_echo("Done MIB-based polling"); +echo("\n"); ?> diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 8db5abde3..5713ccdf0 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -944,17 +944,17 @@ function snmp_translate($oid, $module, $mibdir = null) $lines = preg_split('/\n+/', shell_exec($cmd)); if (!$lines) { - d_echo("No results from snmptranslate"); + 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]); + d_echo("This doesn't look like a MIB: $lines[0]\n"); return null; } - d_echo("SNMP translated: $module::$oid -> $matches[1]::$matches[2]"); + d_echo("SNMP translated: $module::$oid -> $matches[1]::$matches[2]\n"); return array($matches[1], $matches[2]); } @@ -1071,7 +1071,7 @@ function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) function poll_mibs($list, $device, &$graphs) { if (!is_dev_attrib_enabled($device, "poll_mib")) { - d_echo("MIB module disabled for ".$device['hostname']); + d_echo("MIB module disabled for ".$device['hostname']."\n"); return; } $mibdefs = array(); @@ -1085,7 +1085,7 @@ function poll_mibs($list, $device, &$graphs) save_mibs($device, $nam, $oids, $mibdefs[$nam], $graphs); } else { - d_echo("MIB: no match for $module::$name"); + d_echo("MIB: no match for $module::$name\n"); } } } From 7dbb6706388cd956ba0c670b56b388f8c5b2f8c2 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:55:18 +1000 Subject: [PATCH 31/42] Factor out getting the list of subtypes; add MIB types which were loaded from database --- html/pages/graphs.inc.php | 23 ++++++----------------- includes/common.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/html/pages/graphs.inc.php b/html/pages/graphs.inc.php index 89c47aeec..8e438489d 100644 --- a/html/pages/graphs.inc.php +++ b/html/pages/graphs.inc.php @@ -45,20 +45,6 @@ if (!$auth) $title .= " :: ".ucfirst($subtype); } - # Load our list of available graphtypes for this object - // FIXME not all of these are going to be valid - if ($handle = opendir($config['install_dir'] . "/html/includes/graphs/".$type."/")) - { - while (false !== ($file = readdir($handle))) - { - if ($file != "." && $file != ".." && $file != "auth.inc.php" &&strstr($file, ".inc.php")) - { - $types[] = str_replace(".inc.php", "", $file); - } - } - closedir($handle); - } - $graph_array = $vars; $graph_array['height'] = "60"; $graph_array['width'] = $thumb_width; @@ -75,11 +61,14 @@ if (!$auth) onchange="window.open(this.options[this.selectedIndex].value,'_top')" > $type."_".$avail_type, 'page' => "graphs"))."'"); - if ($avail_type == $subtype) { echo(" selected"); } - echo(">".nicecase($avail_type).""); + if ($avail_type == $subtype) { + echo(" selected"); + } + $display_type = is_mib_graph($type, $avail_type) ? $avail_type : nicecase($avail_type); + echo(">$display_type"); } ?> diff --git a/includes/common.php b/includes/common.php index 43c24d97f..18abd43d1 100644 --- a/includes/common.php +++ b/includes/common.php @@ -695,4 +695,39 @@ function is_client_authorized($clientip) return false; } +/* + * @return an array of all graph subtypes for the given type + * FIXME not all of these are going to be valid + */ +function get_graph_subtypes($type) +{ + global $config; + + $types = array(); + + // find the subtypes defined in files + if ($handle = opendir($config['install_dir'] . "/html/includes/graphs/$type/")) { + while (false !== ($file = readdir($handle))) { + if ($file != "." && $file != ".." && $file != "auth.inc.php" && strstr($file, ".inc.php")) { + $types[] = str_replace(".inc.php", "", $file); + } + } + closedir($handle); + } + + // find the MIB subtypes + foreach ($config['graph_types'] as $type => $unused1) { + print_r($type); + foreach ($config['graph_types'][$type] as $subtype => $unused2) { + print_r($subtype); + if (is_mib_graph($type, $subtype)) { + $types[] = $subtype; + } + } + } + + sort($types); + return $types; +} + ?> From d617aaacd07486dd722c725e5aaf80c0f34b04aa Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:55:51 +1000 Subject: [PATCH 32/42] Add graph type for MIB-based polling --- html/includes/graphs/device/mib.inc.php | 36 +++++++++++++++++++++++++ html/includes/graphs/graph.inc.php | 6 +++-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 html/includes/graphs/device/mib.inc.php diff --git a/html/includes/graphs/device/mib.inc.php b/html/includes/graphs/device/mib.inc.php new file mode 100644 index 000000000..3767691b8 --- /dev/null +++ b/html/includes/graphs/device/mib.inc.php @@ -0,0 +1,36 @@ + + * + * 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. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +$rrd_list = array(); +$prefix = rrd_name($device['hostname'], array($subtype, ""), ""); +foreach (glob($prefix."*.rrd") as $filename) { + // find out what * expanded to + $globpart = str_replace($prefix, '', $filename); // take off the prefix + $instance = substr($globpart, 0, -4); // take off ".rrd" + + $ds = array(); + $mibvar = end(explode("-", $subtype)); + $ds['ds'] = name_shorten($mibvar); + $ds['descr'] = "$mibvar-$instance"; + $ds['filename'] = $filename; + $rrd_list[] = $ds; +} + +$colours = 'mixed'; +$scale_min = "0"; +$nototal = 0; +$simple_rrd = true; + +include("includes/graphs/generic_multi_line.inc.php"); + +?> diff --git a/html/includes/graphs/graph.inc.php b/html/includes/graphs/graph.inc.php index c0a6906e8..d0b001500 100644 --- a/html/includes/graphs/graph.inc.php +++ b/html/includes/graphs/graph.inc.php @@ -44,8 +44,10 @@ include($config['install_dir'] . "/html/includes/graphs/$type/auth.inc.php"); if ($auth && is_file($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php")) { include($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php"); } -else -{ +elseif ($auth && is_mib_graph($type, $subtype)) { + include($config['install_dir'] . "/html/includes/graphs/$type/mib.inc.php"); +} +else { graph_error("$type*$subtype ");//Graph Template Missing"); } From 0e218ef22c305d0d83c416918935b6a8861df012 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 14 Jun 2015 09:09:54 +1000 Subject: [PATCH 33/42] Move MIB poller debugging into SNMP functions --- includes/polling/mib.inc.php | 5 ----- includes/snmp.inc.php | 12 ++++++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/includes/polling/mib.inc.php b/includes/polling/mib.inc.php index 8488b2064..857733cef 100644 --- a/includes/polling/mib.inc.php +++ b/includes/polling/mib.inc.php @@ -11,12 +11,7 @@ * the source code distribution for details. */ -echo("MIB-based polling"); -d_echo("\n", ": "); - $devicemib = array($device['sysObjectID'] => "all"); poll_mibs($devicemib, $device, $graphs); -d_echo("Done MIB-based polling"); -echo("\n"); ?> diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 5713ccdf0..b1fda71f5 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -942,7 +942,7 @@ function snmp_translate($oid, $module, $mibdir = null) $cmd .= " -m $module $oid"; // load all the MIBs looking for our object $cmd .= " 2>/dev/null"; // ignore invalid MIBs - $lines = preg_split('/\n+/', shell_exec($cmd)); + $lines = preg_split('/\n+/', external_exec($cmd)); if (!$lines) { d_echo("No results from snmptranslate\n"); return null; @@ -1075,19 +1075,27 @@ function poll_mibs($list, $device, &$graphs) return; } $mibdefs = array(); + echo("MIB-based polling:"); + d_echo("\n"); + foreach ($list as $name => $module) { $translated = snmp_translate($name, $module); if ($translated) { + echo(" $module::$name"); + d_echo("\n"); $mod = $translated[0]; $nam = $translated[1]; $mibdefs[$nam] = snmp_mib_load($nam, $mod); - $oids = snmpwalk_cache_oid($device, "$mod::$nam", array(), $mod); + $oids = snmpwalk_cache_oid($device, $nam, array(), $mod, null, "-OQUsb"); + d_print_r($oids); save_mibs($device, $nam, $oids, $mibdefs[$nam], $graphs); } else { d_echo("MIB: no match for $module::$name\n"); } } + d_echo("Done MIB-based polling"); + echo("\n"); } ?> From ad550ca6da5f9b4c9d3540acd56a2e237e372613 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 14 Jun 2015 09:10:09 +1000 Subject: [PATCH 34/42] Add a couple more MIBs to Ruckus support --- includes/polling/os/ruckuswireless.inc.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/polling/os/ruckuswireless.inc.php b/includes/polling/os/ruckuswireless.inc.php index 6e65a47b2..8ed67d6c2 100644 --- a/includes/polling/os/ruckuswireless.inc.php +++ b/includes/polling/os/ruckuswireless.inc.php @@ -42,6 +42,8 @@ if (isset($ruckuscountry) && $ruckuscountry != "") { $ruckus_mibs = array( "ruckusZDSystemStats" => "RUCKUS-ZD-SYSTEM-MIB", + "ruckusZDWLANTable" => "RUCKUS-ZD-WLAN-MIB", + "ruckusZDWLANAPTable" => "RUCKUS-ZD-WLAN-MIB", ); poll_mibs($ruckus_mibs, $device, $graphs); From 82cb45315d93561733a55582b31146aa938be6d6 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 14 Jun 2015 17:31:56 +1000 Subject: [PATCH 35/42] Add documentation for initial release --- doc/General/MIB-based-polling.md | 110 +++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 20 deletions(-) diff --git a/doc/General/MIB-based-polling.md b/doc/General/MIB-based-polling.md index f21bc4816..e52675335 100644 --- a/doc/General/MIB-based-polling.md +++ b/doc/General/MIB-based-polling.md @@ -1,25 +1,95 @@ -The overall design of MIB-based support is: +## WARNING ## -Discovery: - - MIBs are not involved; any work done here would have to be +MIB-based polling is experimental. It might overload your LibreNMS server, +destroy your data, set your routers on fire, and kick your cat. It has been +tested against a very limited set of devices (namely Ruckus ZD1000 wireless +controllers, and Net-SNMP on Linux). It may fail badly on other hardware. + +The approach taken is fairly basic and I claim no special expertise in +understanding MIBs. Most of my understanding of SNMP comes from reading +net-snmp man pages, and reverse engineering the output of snmptranslate and +snmpwalk and trying to make devices work with LibreNMS. I may have made +false assumptions and probably use wrong terminology in many places. Feel +free to offer corrections/suggestions via pull requests or email. + +Paul Gear + +## Overview ## + +MIB-based polling is disabled by default; you must set + `$config['poller_modules']['mib'] = 1;` +in config.php to enable it. + +The components involved in of MIB-based support are: + +### Discovery ### + + - MIB-based detection is not involved; any work done here would have to be duplicated by the poller and thus would only increase load. -Polling: - - Look for a MIB matching sysObjectID in the MIB directory; if one - is found: - - parse it - - walk that MIB on the device - - store any numeric results in individual RRD files - - update/add graph definitions in the database - - Individual OSes (includes/polling/os/*.inc.php) can poll extra MIBs - that should be there for a given OS by calling poll_mib(). - - Devices may be excluded from MIB polling by adding poll_mib = 0 to - devices_attribs (see /device/device=ID/tab=edit/section=modules/) +### Polling ### -Graphing: - - For each file in the device directory, create a graph using the - definition in the database. Future enhancements: - - Allow graphs to go in different sections - - Allow graphs to be combined automatically or on a user-defined - basis. + - The file `includes/snmp.inc.php` now contains code which can parse MIBs + using `snmptranslate` and use the data returned to populate an array + which guides the poller in what to store. At the moment, only OIDs with + Unsigned32 and Counter64 data types are parsed. + - `includes/polling/mib.inc.php` looks for a MIB matching sysObjectID in + the MIB directory; if one is found, it: + - parses it + - walks that MIB on the device + - stores any numeric results in individual RRD files + - updates/adds graph definitions in the previously-unused graph_types + database table + - Individual OSes (`includes/polling/os/*.inc.php`) can poll extra MIBs + for a given OS by calling poll_mib(). At the moment, this actually + happens before the general MIB polling. + - Devices may be excluded from MIB polling by changing the setting in the + device edit screen (`/device/device=ID/tab=edit/section=modules/`) + +### Graphing ### + + - For each graph type device directory, a graph will appear in the Device + -> Graphs -> MIB section. + - At the moment, all units are placed in the same graph. This is probably + non-optimal for, e.g., wifi controllers with hundreds of APs attached. + +## Adding/testing other device types ## + +One of the goals of this work is to help take out the heavy lifting of +adding new device types. Even if you want fully customised graphs or +tables, you can use the automatic collection of MIBs to make it easy to +gather the data you want. + +### How to add a new device MIB ### + + 1. Ensure the manufacturer's MIB is present in the mibs directory. If you + plan to submit your work to LibreNMS, make sure you attribute the source + of the MIB, including the exact download URL. + 2. Check that `snmptranslate -Ts -M mibs -m MODULE | grep mibName` produces + a named list of OIDs. See the comments for `snmp_mib_walk()` in + `includes/snmp.inc.php` for an example. + 3. Check that `snmptranslate -Td -On -M mibs -m MODULE MODULE::mibName` + produces a parsed description of the OID values. An example can be + found in the comments for `snmp_mib_parse()` in `includes/snmp.inc.php`. + 4. Get the `sysObjectID` from a device, for example:``` +snmpget -v2c -c public -OUsb -m SNMPv2-MIB -M /opt/librenms/mibs -t 30 hostname sysObjectID.0 +``` + 5. Ensure `snmptranslate -m all -M /opt/librenms/mibs OID 2>/dev/null` + (where OID is the value returned for sysObjectID above) results in a + valid name for the MIB. See the comments for `snmp_translate()` in + `includes/snmp.inc.php` for an example. If this step fails, it means + there is something wrong with the MIB and net-snmp cannot parse it. + +## TODO ## + + - Save the most recent MIB data in the database (including string types + which cannot be graphed). Display it in the appropriate places. + - Parse and save integer and timetick data types. + - Filter MIBs/OIDs from being polled and/or saved. + - Move graphs from the MIB section to elsewhere. e.g. There is already + specific support for wireless APs - this should be utilised, but isn't + yet. + - Combine multiple MIB values into graphs automatically on a predefined or + user-defined basis. + - Include MIB types in stats submissions. From 55496bf943942367b142717ec2b4bb0d3ed42dd8 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 14 Jun 2015 17:32:21 +1000 Subject: [PATCH 36/42] Add example referred to in documentation --- includes/snmp.inc.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index b1fda71f5..a2526203f 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -932,6 +932,9 @@ function snmp_mib_load($mib, $module, $mibdir = null) /* * 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_translate($oid, $module, $mibdir = null) { From a02871263ce45fa8303bca75c53a6e535719e8bf Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 14 Jun 2015 18:00:21 +1000 Subject: [PATCH 37/42] Fix up some unclear/missing copyrights --- html/includes/front/boxes.inc.php | 1 + html/includes/front/top_device_bits.inc.php | 1 + html/includes/front/top_ports.inc.php | 1 + html/includes/graphs/device/mib.inc.php | 1 + includes/common.php | 18 ++++++++++++++++-- includes/load_db_graph_types.inc.php | 1 + includes/polling/mib.inc.php | 1 + includes/polling/os/ruckuswireless.inc.php | 4 ++++ 8 files changed, 26 insertions(+), 2 deletions(-) diff --git a/html/includes/front/boxes.inc.php b/html/includes/front/boxes.inc.php index 0d6f2a939..665c1d738 100644 --- a/html/includes/front/boxes.inc.php +++ b/html/includes/front/boxes.inc.php @@ -2,6 +2,7 @@ /* * LibreNMS front page graphs * + * Author: Paul Gear * Copyright (c) 2013 Gear Consulting Pty Ltd * * This program is free software: you can redistribute it and/or modify it diff --git a/html/includes/front/top_device_bits.inc.php b/html/includes/front/top_device_bits.inc.php index a44d3a1e8..4b48bfd4a 100644 --- a/html/includes/front/top_device_bits.inc.php +++ b/html/includes/front/top_device_bits.inc.php @@ -3,6 +3,7 @@ * LibreNMS front page top devices graph * - Find most utilised devices that have been polled in the last N minutes * + * Author: Paul Gear * Copyright (c) 2013 Gear Consulting Pty Ltd * * This program is free software: you can redistribute it and/or modify it diff --git a/html/includes/front/top_ports.inc.php b/html/includes/front/top_ports.inc.php index 9a09f3057..008525207 100644 --- a/html/includes/front/top_ports.inc.php +++ b/html/includes/front/top_ports.inc.php @@ -3,6 +3,7 @@ * LibreNMS front page top ports graph * - Find most utilised ports that have been polled in the last N minutes * + * Author: Paul Gear * Copyright (c) 2013 Gear Consulting Pty Ltd * * This program is free software: you can redistribute it and/or modify it diff --git a/html/includes/graphs/device/mib.inc.php b/html/includes/graphs/device/mib.inc.php index 3767691b8..49f2b9a3e 100644 --- a/html/includes/graphs/device/mib.inc.php +++ b/html/includes/graphs/device/mib.inc.php @@ -2,6 +2,7 @@ /* * LibreNMS MIB-based polling * + * Author: Paul Gear * Copyright (c) 2015 Gear Consulting Pty Ltd * * This program is free software: you can redistribute it and/or modify it diff --git a/includes/common.php b/includes/common.php index 18abd43d1..3fbdd53f5 100644 --- a/includes/common.php +++ b/includes/common.php @@ -1,6 +1,20 @@ + * Copyright (c) 2014-2015 Gear Consulting Pty Ltd + * + * 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. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ function format_number_short($number, $sf) { diff --git a/includes/load_db_graph_types.inc.php b/includes/load_db_graph_types.inc.php index b32b4e038..618a49b8c 100644 --- a/includes/load_db_graph_types.inc.php +++ b/includes/load_db_graph_types.inc.php @@ -2,6 +2,7 @@ /* * LibreNMS dynamic graph types * + * Author: Paul Gear * Copyright (c) 2015 Gear Consulting Pty Ltd * * This program is free software: you can redistribute it and/or modify it diff --git a/includes/polling/mib.inc.php b/includes/polling/mib.inc.php index 857733cef..16c1430b1 100644 --- a/includes/polling/mib.inc.php +++ b/includes/polling/mib.inc.php @@ -2,6 +2,7 @@ /* * LibreNMS MIB-based polling * + * Author: Paul Gear * Copyright (c) 2015 Gear Consulting Pty Ltd * * This program is free software: you can redistribute it and/or modify it diff --git a/includes/polling/os/ruckuswireless.inc.php b/includes/polling/os/ruckuswireless.inc.php index 8ed67d6c2..d864e0b7b 100644 --- a/includes/polling/os/ruckuswireless.inc.php +++ b/includes/polling/os/ruckuswireless.inc.php @@ -2,8 +2,12 @@ /* * LibreNMS Ruckus Wireless OS information module * +* Originally by: * Copyright (c) 2015 Søren Friis Rosiak +* +* Updates by Paul Gear: * Copyright (c) 2015 Gear Consulting Pty Ltd +* * 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 From 723530a2f63346da2ea4b6a60c36546f341b4215 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 14 Jun 2015 18:10:31 +1000 Subject: [PATCH 38/42] Fix incomplete doc --- doc/General/MIB-based-polling.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/General/MIB-based-polling.md b/doc/General/MIB-based-polling.md index e52675335..5951eb3d3 100644 --- a/doc/General/MIB-based-polling.md +++ b/doc/General/MIB-based-polling.md @@ -48,8 +48,10 @@ The components involved in of MIB-based support are: ### Graphing ### - - For each graph type device directory, a graph will appear in the Device - -> Graphs -> MIB section. + - For each graph type defined in the database, a graph will appear in: + Device -> Graphs -> MIB + - MIB graphs are generated generically by + `html/includes/graphs/device/mib.inc.php` - At the moment, all units are placed in the same graph. This is probably non-optimal for, e.g., wifi controllers with hundreds of APs attached. From 044fa48917a1ad69f2761aa3eb01981dbf1c039c Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 14 Jun 2015 18:15:00 +1000 Subject: [PATCH 39/42] More missing documentation --- doc/General/MIB-based-polling.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/General/MIB-based-polling.md b/doc/General/MIB-based-polling.md index 5951eb3d3..e66461601 100644 --- a/doc/General/MIB-based-polling.md +++ b/doc/General/MIB-based-polling.md @@ -81,6 +81,11 @@ snmpget -v2c -c public -OUsb -m SNMPv2-MIB -M /opt/librenms/mibs -t 30 hostname valid name for the MIB. See the comments for `snmp_translate()` in `includes/snmp.inc.php` for an example. If this step fails, it means there is something wrong with the MIB and net-snmp cannot parse it. + 6. Add any additional MIBs you wish to poll for specific device types to + `includes/polling/os/OSNAME.inc.php` by calling `poll_mibs()` with the + MIB module and name. See includes/polling/os/ruckuswireless.inc.php for + an example. + 7. That should be all you need to see MIB graphs! ## TODO ## From 95e3d7000d3694f28dfc674284f790d998dc264e Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Tue, 16 Jun 2015 08:15:16 +1000 Subject: [PATCH 40/42] Add missing SQL schema --- sql-schema/054.sql | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 sql-schema/054.sql diff --git a/sql-schema/054.sql b/sql-schema/054.sql new file mode 100644 index 000000000..5122975a7 --- /dev/null +++ b/sql-schema/054.sql @@ -0,0 +1,4 @@ +ALTER TABLE `graph_types` CHANGE `graph_subtype` `graph_subtype` varchar(64); +ALTER TABLE `device_graphs` CHANGE `graph` `graph` varchar(64); +ALTER TABLE `graph_types` CHANGE `graph_descr` `graph_descr` varchar(255); +ALTER TABLE `graph_types` ADD PRIMARY KEY (`graph_type`, `graph_subtype`, `graph_section`); From 0eb3e691672c46346b2141fe87bd6c1a6af47d48 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Tue, 16 Jun 2015 08:42:05 +1000 Subject: [PATCH 41/42] Fix Scrutinizer issues --- html/includes/graphs/device/mib.inc.php | 5 ++--- html/includes/graphs/graph.inc.php | 4 ++-- includes/common.php | 13 ++++++++----- includes/polling/mib.inc.php | 2 -- includes/snmp.inc.php | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/html/includes/graphs/device/mib.inc.php b/html/includes/graphs/device/mib.inc.php index 49f2b9a3e..7f76f3bbe 100644 --- a/html/includes/graphs/device/mib.inc.php +++ b/html/includes/graphs/device/mib.inc.php @@ -20,7 +20,8 @@ foreach (glob($prefix."*.rrd") as $filename) { $instance = substr($globpart, 0, -4); // take off ".rrd" $ds = array(); - $mibvar = end(explode("-", $subtype)); + $mibparts = explode("-", $subtype); + $mibvar = end($mibparts); $ds['ds'] = name_shorten($mibvar); $ds['descr'] = "$mibvar-$instance"; $ds['filename'] = $filename; @@ -33,5 +34,3 @@ $nototal = 0; $simple_rrd = true; include("includes/graphs/generic_multi_line.inc.php"); - -?> diff --git a/html/includes/graphs/graph.inc.php b/html/includes/graphs/graph.inc.php index d0b001500..5c6bfe1c1 100644 --- a/html/includes/graphs/graph.inc.php +++ b/html/includes/graphs/graph.inc.php @@ -41,10 +41,10 @@ $subtype = $graphtype['subtype']; $auth = is_client_authorized($_SERVER['REMOTE_ADDR']); include($config['install_dir'] . "/html/includes/graphs/$type/auth.inc.php"); -if ($auth && is_file($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php")) { +if ($auth === true && is_file($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php")) { include($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php"); } -elseif ($auth && is_mib_graph($type, $subtype)) { +elseif ($auth === true && is_mib_graph($type, $subtype)) { include($config['install_dir'] . "/html/includes/graphs/$type/mib.inc.php"); } else { diff --git a/includes/common.php b/includes/common.php index 3fbdd53f5..8e7499841 100644 --- a/includes/common.php +++ b/includes/common.php @@ -652,11 +652,14 @@ function d_print_r($var, $no_debug_text = null) * Substitute for $subst if necessary. * @return the shortened name */ -function name_shorten($name, $common, $subst = "mibval", $len = 19) +function name_shorten($name, $common = null, $subst = "mibval", $len = 19) { - if (strlen($name) > $len && strpos($name, $common) >= 0) { - $newname = str_replace($common, '', $name); - $name = $newname; + if ($common !== null) { + // remove common from the beginning of the string, if present + if (strlen($name) > $len && strpos($name, $common) >= 0) { + $newname = str_replace($common, '', $name); + $name = $newname; + } } if (strlen($name) > $len) { $name = $subst; @@ -690,8 +693,8 @@ function is_mib_graph($type, $subtype) */ function is_client_authorized($clientip) { - global $config; + if (isset($config['allow_unauth_graphs']) && $config['allow_unauth_graphs']) { d_echo("Unauthorized graphs allowed\n"); return true; diff --git a/includes/polling/mib.inc.php b/includes/polling/mib.inc.php index 16c1430b1..07d6c16fc 100644 --- a/includes/polling/mib.inc.php +++ b/includes/polling/mib.inc.php @@ -14,5 +14,3 @@ $devicemib = array($device['sysObjectID'] => "all"); poll_mibs($devicemib, $device, $graphs); - -?> diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index a2526203f..91574c8e2 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -946,7 +946,7 @@ function snmp_translate($oid, $module, $mibdir = null) $cmd .= " 2>/dev/null"; // ignore invalid MIBs $lines = preg_split('/\n+/', external_exec($cmd)); - if (!$lines) { + if (empty($lines)) { d_echo("No results from snmptranslate\n"); return null; } @@ -1051,7 +1051,7 @@ function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) foreach ($oids as $index => $array) { foreach ($array as $oid => $val) { $type = oid_rrd_type($oid, $mibdef); - if (!$type) { + if ($type === false) { continue; } $usedoids[$index][$oid] = $val; From 884b98e250b196e78922d5900e13a506024aea51 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Tue, 16 Jun 2015 09:01:13 +1000 Subject: [PATCH 42/42] Another scrutinizer fix --- includes/snmp.inc.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 91574c8e2..ca4047815 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -824,7 +824,8 @@ function snmp_mib_parse($oid, $mib, $module, $mibdir = null) { global $debug; - $lastpart = end(explode(".", $oid)); + $fulloid = explode(".", $oid); + $lastpart = end($fulloid); $cmd = "snmptranslate -Td -On"; $cmd .= mibdir($mibdir);