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 <pr_id>`, i.e `./scripts/github-apply 5926`
This commit is contained in:
Tony Murray
2018-07-12 15:30:39 -05:00
committed by Neil Lathwood
parent 33887417ee
commit 846cf11331
12 changed files with 129 additions and 66 deletions

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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) {

View File

@@ -111,12 +111,15 @@ if (!$where) {
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 "-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);

View File

@@ -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 /
```

View File

@@ -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

View File

@@ -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()

View File

@@ -1,5 +1,6 @@
<?php
use LibreNMS\Config;
use LibreNMS\Device\YamlDiscovery;
use LibreNMS\OS;
@@ -58,6 +59,10 @@ $run_sensors = array(
'eer',
'waterflow',
);
// filter submodules
$run_sensors = array_intersect($run_sensors, Config::get('discovery_submodules.sensors', $run_sensors));
sensors($run_sensors, $device, $valid, $pre_cache);
unset(
$pre_cache,

View File

@@ -79,6 +79,47 @@ function only_alphanumeric($string)
return preg_replace('/[^a-zA-Z0-9]/', '', $string);
}
/**
* Parse cli discovery or poller modules and set config for this run
*
* @param string $type discovery or poller
* @param array $options get_opts array (only m key is checked)
* @return bool
*/
function parse_modules($type, $options)
{
$override = false;
if ($options['m']) {
Config::set("{$type}_modules", []);
foreach (explode(',', $options['m']) as $module) {
// parse submodules (only supported by some modules)
if (str_contains($module, '/')) {
list($module, $submodule) = explode('/', $module, 2);
$existing_submodules = Config::get("{$type}_submodules.$module", []);
$existing_submodules[] = $submodule;
Config::set("{$type}_submodules.$module", $existing_submodules);
}
$dir = $type == 'poller' ? 'polling' : $type;
if (is_file("includes/$dir/$module.inc.php")) {
Config::set("{$type}_modules.$module", 1);
$override = true;
}
}
// display selected modules
$modules = array_map(function ($module) use ($type) {
$submodules = Config::get("{$type}_submodules.$module");
return $module . ($submodules ? '(' . implode(',', $submodules) . ')' : '');
}, array_keys(Config::get("{$type}_modules", [])));
d_echo("Override $type modules: " . implode(', ', $modules) . PHP_EOL);
}
return $override;
}
function logfile($string)
{
global $config;

View File

@@ -219,11 +219,11 @@ function record_sensor_data($device, $all_sensors)
}
/**
* @param $device
* @param $options
* @param array $device The device to poll
* @param bool $force_module Ignore device module overrides
* @return bool
*/
function poll_device($device, $options)
function poll_device($device, $force_module = false)
{
global $config, $device;
@@ -276,20 +276,9 @@ function poll_device($device, $options)
$graphs = array();
$oldgraphs = array();
$force_module = false;
if ($device['snmp_disable']) {
$config['poller_modules'] = array();
} else {
if ($options['m']) {
$config['poller_modules'] = array();
foreach (explode(',', $options['m']) as $module) {
if (is_file('includes/polling/' . $module . '.inc.php')) {
$config['poller_modules'][$module] = 1;
$force_module = true;
}
}
}
// we always want the core module to be included, prepend it
$config['poller_modules'] = array('core' => true) + $config['poller_modules'];
}

View File

@@ -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);

View File

@@ -11,6 +11,8 @@
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
use LibreNMS\Config;
$init_modules = array('polling', 'alerts');
require __DIR__ . '/includes/init.php';
@@ -69,7 +71,7 @@ if (!$where) {
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 "-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++;
}