From 846cf11331bec6315fc5cb2df9d6a5f6fbe013aa Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Thu, 12 Jul 2018 15:30:39 -0500 Subject: [PATCH] Support specifying submodules in poller and discovery for debug (#8896) Mostly useful for debugging DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply `, i.e `./scripts/github-apply 5926` --- LibreNMS/Device/Sensor.php | 12 +++++++- LibreNMS/Device/WirelessSensor.php | 7 ++++- LibreNMS/Util/ModuleTestHelper.php | 14 +++++----- discovery.php | 25 +++++++++-------- doc/Support/Discovery Support.md | 2 +- doc/Support/Poller Support.md | 2 +- includes/discovery/functions.inc.php | 25 ++++++----------- includes/discovery/sensors.inc.php | 5 ++++ includes/functions.php | 41 ++++++++++++++++++++++++++++ includes/polling/functions.inc.php | 17 ++---------- includes/polling/sensors.inc.php | 18 ++++++++++-- poller.php | 27 ++++++++++-------- 12 files changed, 129 insertions(+), 66 deletions(-) diff --git a/LibreNMS/Device/Sensor.php b/LibreNMS/Device/Sensor.php index 046bc65808..7b12e3e252 100644 --- a/LibreNMS/Device/Sensor.php +++ b/LibreNMS/Device/Sensor.php @@ -25,6 +25,7 @@ namespace LibreNMS\Device; +use LibreNMS\Config; use LibreNMS\Interfaces\Discovery\DiscoveryModule; use LibreNMS\Interfaces\Polling\PollerModule; use LibreNMS\OS; @@ -268,9 +269,18 @@ class Sensor implements DiscoveryModule, PollerModule { $table = static::$table; + $query = "SELECT * FROM `$table` WHERE `device_id` = ?"; + $params = [$os->getDeviceId()]; + + $submodules = Config::get('poller_submodules.wireless', []); + if (!empty($submodules)) { + $query .= " AND `sensor_class` IN " . dbGenPlaceholders(count($submodules)); + $params = array_merge($params, $submodules); + } + // fetch and group sensors, decode oids $sensors = array_reduce( - dbFetchRows("SELECT * FROM `$table` WHERE `device_id` = ?", array($os->getDeviceId())), + dbFetchRows($query, $params), function ($carry, $sensor) { $sensor['sensor_oids'] = json_decode($sensor['sensor_oids']); $carry[$sensor['sensor_class']][] = $sensor; diff --git a/LibreNMS/Device/WirelessSensor.php b/LibreNMS/Device/WirelessSensor.php index e32886ddd3..d6e666079b 100644 --- a/LibreNMS/Device/WirelessSensor.php +++ b/LibreNMS/Device/WirelessSensor.php @@ -25,6 +25,7 @@ namespace LibreNMS\Device; +use LibreNMS\Config; use LibreNMS\OS; class WirelessSensor extends Sensor @@ -105,7 +106,11 @@ class WirelessSensor extends Sensor public static function runDiscovery(OS $os) { - foreach (self::getTypes() as $type => $descr) { + $types = array_keys(self::getTypes()); + $submodules = Config::get('discovery_submodules.wireless', $types); + $types = array_intersect($types, $submodules); + + foreach ($types as $type) { static::discoverType($os, $type); } } diff --git a/LibreNMS/Util/ModuleTestHelper.php b/LibreNMS/Util/ModuleTestHelper.php index 0be795fed8..bf461c97cf 100644 --- a/LibreNMS/Util/ModuleTestHelper.php +++ b/LibreNMS/Util/ModuleTestHelper.php @@ -175,8 +175,8 @@ class ModuleTestHelper $save_vedbug = $vdebug; $debug = true; $vdebug = false; - discover_device($device, $this->getArgs()); - poll_device($device, $this->getArgs()); + discover_device($device, $this->parseArgs('discovery')); + poll_device($device, $this->parseArgs('poller')); $debug = $save_debug; $vdebug = $save_vedbug; $collection_output = ob_get_contents(); @@ -306,13 +306,13 @@ class ModuleTestHelper return array_unique($full_list); } - private function getArgs() + private function parseArgs($type) { if (empty($this->modules)) { - return []; + return false; } - return ['m' => implode(',', $this->modules)]; + return parse_modules($type, ['m' => implode(',', $this->modules)]); } private function qPrint($var) @@ -519,7 +519,7 @@ class ModuleTestHelper } ob_start(); - discover_device($device, $this->getArgs()); + discover_device($device, $this->parseArgs('discovery')); $this->discovery_output = ob_get_contents(); if ($this->quiet) { @@ -547,7 +547,7 @@ class ModuleTestHelper } ob_start(); - poll_device($device, $this->getArgs()); + poll_device($device, $this->parseArgs('poller')); $this->poller_output = ob_get_contents(); if ($this->quiet) { diff --git a/discovery.php b/discovery.php index 6fb9f15703..c418df14d9 100755 --- a/discovery.php +++ b/discovery.php @@ -99,24 +99,27 @@ EOH; if (!$where) { echo "-h | Poll single device\n"; - echo "-h odd Poll odd numbered devices (same as -i 2 -n 0)\n"; - echo "-h even Poll even numbered devices (same as -i 2 -n 1)\n"; - echo "-h all Poll all devices\n"; - echo "-h new Poll all devices that have not had a discovery run before\n"; - echo "--os Poll devices only with specified operating system\n"; - echo "--type Poll devices only with specified type\n"; + echo "-h odd Poll odd numbered devices (same as -i 2 -n 0)\n"; + echo "-h even Poll even numbered devices (same as -i 2 -n 1)\n"; + echo "-h all Poll all devices\n"; + echo "-h new Poll all devices that have not had a discovery run before\n"; + echo "--os Poll devices only with specified operating system\n"; + echo "--type Poll devices only with specified type\n"; echo "-i -n Poll as instance of \n"; - echo " Instances start at 0. 0-3 for -n 4\n"; + echo " Instances start at 0. 0-3 for -n 4\n"; echo "\n"; echo "Debugging and testing options:\n"; - echo "-d Enable debugging output\n"; - echo "-v Enable verbose debugging output\n"; - echo "-m Specify single module to be run\n"; + echo "-d Enable debugging output\n"; + echo "-v Enable verbose debugging output\n"; + echo "-m Specify single module to be run. Comma separate modules, submodules may be added with /\n"; echo "\n"; echo "Invalid arguments!\n"; exit; } +// If we've specified modules with -m, use them +$module_override = parse_modules('discovery', $options); + $discovered_devices = 0; if (!empty($config['distributed_poller_group'])) { @@ -125,7 +128,7 @@ if (!empty($config['distributed_poller_group'])) { global $device; foreach (dbFetch("SELECT * FROM `devices` WHERE disabled = 0 AND snmp_disable = 0 $where ORDER BY device_id DESC", $sqlparams) as $device) { - $discovered_devices += discover_device($device, $options); + $discovered_devices += (int)discover_device($device, $module_override); } $end = microtime(true); diff --git a/doc/Support/Discovery Support.md b/doc/Support/Discovery Support.md index b663c17865..ad937f02e8 100644 --- a/doc/Support/Discovery Support.md +++ b/doc/Support/Discovery Support.md @@ -18,7 +18,7 @@ This document will explain how to use discovery.php to debug issues or manually Debugging and testing options: -d Enable debugging output -v Enable verbose debugging output --m Specify single module to be run +-m Specify module(s) to be run. Comma separate modules, submodules may be added with / ``` diff --git a/doc/Support/Poller Support.md b/doc/Support/Poller Support.md index 0b1052f0d4..0103a8776e 100644 --- a/doc/Support/Poller Support.md +++ b/doc/Support/Poller Support.md @@ -20,7 +20,7 @@ Debugging and testing options: -f Do not insert data into InfluxDB -d Enable debugging output -v Enable verbose debugging output --m Specify module(s) to be run +-m Specify module(s) to be run. Comma separate modules, submodules may be added with / ``` `-h` Use this to specify a device via either id or hostname (including wildcard using *). You can also specify odd and diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 2f4869568e..5cde7559d6 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -110,10 +110,15 @@ function load_discovery(&$device) } } -function discover_device(&$device, $options = null) +/** + * @param array $device The device to poll + * @param bool $force_module Ignore device module overrides + * @return bool if the device was discovered or skipped + */ +function discover_device(&$device, $force_module = false) { if ($device['snmp_disable'] == '1') { - return 0; + return false; } global $valid; @@ -131,7 +136,7 @@ function discover_device(&$device, $options = null) $response = device_is_up($device, true); if ($response['status'] !== '1') { - return 0; + return false; } if ($device['os'] == 'generic') { @@ -152,18 +157,6 @@ function discover_device(&$device, $options = null) echo "\n"; - $force_module = false; - // If we've specified modules, use them, else walk the modules array - if ($options['m']) { - Config::set('discovery_modules', array()); - foreach (explode(',', $options['m']) as $module) { - if (is_file("includes/discovery/$module.inc.php")) { - Config::set("discovery_modules.$module", 1); - $force_module = true; - } - } - } - $discovery_devices = Config::get('discovery_modules', array()); $discovery_devices = array('core' => true) + $discovery_devices; @@ -208,7 +201,7 @@ function discover_device(&$device, $options = null) echo "Discovered in $device_time seconds\n"; echo PHP_EOL; - return 1; + return true; } //end discover_device() diff --git a/includes/discovery/sensors.inc.php b/includes/discovery/sensors.inc.php index 82d64151c6..e6d0948990 100644 --- a/includes/discovery/sensors.inc.php +++ b/includes/discovery/sensors.inc.php @@ -1,5 +1,6 @@ true) + $config['poller_modules']; } diff --git a/includes/polling/sensors.inc.php b/includes/polling/sensors.inc.php index 9a29c2265e..e07468a9c1 100644 --- a/includes/polling/sensors.inc.php +++ b/includes/polling/sensors.inc.php @@ -11,9 +11,21 @@ * See COPYING for more details. */ -$sensors = dbFetchRows("SELECT `sensor_class` FROM `sensors` WHERE `device_id` = ? GROUP BY `sensor_class`", array($device['device_id'])); -foreach ($sensors as $sensor_type) { +use LibreNMS\Config; + +$query = "SELECT `sensor_class` FROM `sensors` WHERE `device_id` = ?"; +$params = [$device['device_id']]; + +$submodules = Config::get('poller_submodules.sensors', []); +if (!empty($submodules)) { + $query .= " AND `sensor_class` IN " . dbGenPlaceholders(count($submodules)); + $params = array_merge($params, $submodules); +} + +$query .= " GROUP BY `sensor_class`"; + +foreach (dbFetchRows($query, $params) as $sensor_type) { poll_sensor($device, $sensor_type['sensor_class']); } -unset($sensors, $sensor_type); +unset($submodules, $sensor_type, $query, $params); diff --git a/poller.php b/poller.php index bd492da06a..87ef0aaf84 100755 --- a/poller.php +++ b/poller.php @@ -11,6 +11,8 @@ * @copyright (C) 2006 - 2012 Adam Armstrong */ +use LibreNMS\Config; + $init_modules = array('polling', 'alerts'); require __DIR__ . '/includes/init.php'; @@ -58,18 +60,18 @@ if (isset($options['i']) && $options['i'] && isset($options['n'])) { if (!$where) { echo "-h | Poll single device\n"; - echo "-h odd Poll odd numbered devices (same as -i 2 -n 0)\n"; - echo "-h even Poll even numbered devices (same as -i 2 -n 1)\n"; - echo "-h all Poll all devices\n\n"; + echo "-h odd Poll odd numbered devices (same as -i 2 -n 0)\n"; + echo "-h even Poll even numbered devices (same as -i 2 -n 1)\n"; + echo "-h all Poll all devices\n\n"; echo "-i -n Poll as instance of \n"; - echo " Instances start at 0. 0-3 for -n 4\n\n"; + echo " Instances start at 0. 0-3 for -n 4\n\n"; echo "Debugging and testing options:\n"; - echo "-r Do not create or update RRDs\n"; - echo "-f Do not insert data into InfluxDB\n"; - echo "-p Do not insert data into Prometheus\n"; - echo "-d Enable debugging output\n"; - echo "-v Enable verbose debugging output\n"; - echo "-m Specify module(s) to be run\n"; + echo "-r Do not create or update RRDs\n"; + echo "-f Do not insert data into InfluxDB\n"; + echo "-p Do not insert data into Prometheus\n"; + echo "-d Enable debugging output\n"; + echo "-v Enable verbose debugging output\n"; + echo "-m Specify module(s) to be run. Comma separate modules, submodules may be added with /\n"; echo "\n"; echo "No polling type specified!\n"; exit; @@ -142,6 +144,9 @@ if ($config['nographite'] !== true && $config['graphite']['enable'] === true) { $graphite = false; } +// If we've specified modules with -m, use them +$module_override = parse_modules('poller', $options); + rrdtool_initialize(); echo "Starting polling run:\n\n"; @@ -158,7 +163,7 @@ foreach (dbFetch($query) as $device) { $device['vrf_lite_cisco'] = ''; } - if (!poll_device($device, $options)) { + if (!poll_device($device, $module_override)) { $unreachable_devices++; }