Files
librenms-librenms/discovery.php
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
4.4 KiB
PHP
Raw Normal View History

2010-09-03 18:26:59 +00:00
#!/usr/bin/env php
2008-03-22 23:09:35 +00:00
<?php
/**
* LibreNMS
*
* This file is part of LibreNMS.
*
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
2021-04-29 22:42:18 -05:00
use LibreNMS\Util\Debug;
$init_modules = ['discovery'];
require __DIR__ . '/includes/init.php';
2008-03-22 23:09:35 +00:00
$start = microtime(true);
2021-11-17 19:23:55 -06:00
Log::setDefaultDriver('console');
2015-10-09 19:41:19 +02:00
$sqlparams = [];
2016-08-28 17:32:55 -05:00
$options = getopt('h:m:i:n:d::v::a::q', ['os:', 'type:']);
2008-03-22 23:09:35 +00:00
2011-05-06 14:04:53 +00:00
if (! isset($options['q'])) {
echo \LibreNMS\Config::get('project_name') . " Discovery\n";
2011-05-06 14:04:53 +00:00
}
2009-11-07 02:30:38 +00:00
2024-07-22 21:21:57 -05:00
$where = '';
if (isset($options['h'])) {
2011-03-23 09:54:56 +00:00
if ($options['h'] == 'odd') {
$options['n'] = '1';
$options['i'] = '2';
2016-08-28 17:32:55 -05:00
} elseif ($options['h'] == 'even') {
2011-03-23 09:54:56 +00:00
$options['n'] = '0';
$options['i'] = '2';
2016-08-28 17:32:55 -05:00
} elseif ($options['h'] == 'all') {
2011-03-23 09:54:56 +00:00
$where = ' ';
$doing = 'all';
2016-08-28 17:32:55 -05:00
} elseif ($options['h'] == 'new') {
$new_discovery_lock = Cache::lock('new-discovery', 300);
2011-03-23 09:54:56 +00:00
$where = 'AND `last_discovered` IS NULL';
$doing = 'new';
2016-08-28 17:32:55 -05:00
} elseif ($options['h']) {
2011-03-23 09:54:56 +00:00
if (is_numeric($options['h'])) {
$where = "AND `device_id` = '" . $options['h'] . "'";
$doing = $options['h'];
2016-08-28 17:32:55 -05:00
} else {
2021-03-28 17:25:30 -05:00
$where = "AND `hostname` LIKE '" . str_replace('*', '%', $options['h']) . "'";
2011-03-23 09:54:56 +00:00
$doing = $options['h'];
2015-07-13 20:10:26 +02:00
}
}//end if
}//end if
if (isset($options['os'])) {
2015-10-09 19:41:19 +02:00
$where .= ' AND os = ?';
$sqlparams[] = $options['os'];
}
if (isset($options['type'])) {
2015-10-09 19:41:19 +02:00
$where .= ' AND type = ?';
$sqlparams[] = $options['type'];
}
if (isset($options['i']) && $options['i'] && isset($options['n'])) {
2015-10-09 19:41:19 +02:00
$where .= ' AND MOD(device_id,' . $options['i'] . ") = '" . $options['n'] . "'";
2011-03-23 09:54:56 +00:00
$doing = $options['n'] . '/' . $options['i'];
}
2021-04-29 22:42:18 -05:00
if (Debug::set(isset($options['d']), false) || isset($options['v'])) {
echo \LibreNMS\Util\Version::get()->header();
2011-03-23 09:54:56 +00:00
echo "DEBUG!\n";
2021-04-29 22:42:18 -05:00
Debug::setVerbose(isset($options['v']));
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
2010-08-21 12:54:42 +00:00
}
if (! $where) {
2011-03-23 09:54:56 +00:00
echo "-h <device id> | <device hostname wildcard> 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 <os_name> Poll devices only with specified operating system\n";
echo "--type <type> Poll devices only with specified type\n";
echo "-i <instances> -n <number> Poll as instance <number> of <instances>\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. Comma separate modules, submodules may be added with /\n";
2011-03-23 09:54:56 +00:00
echo "\n";
echo "Invalid arguments!\n";
2011-03-23 09:54:56 +00:00
exit;
2011-03-22 20:27:39 +00:00
}
2009-11-07 02:30:38 +00:00
// If we've specified modules with -m, use them
$module_override = parse_modules('discovery', $options);
$discovered_devices = 0;
2008-03-22 23:09:35 +00:00
2019-06-23 00:29:12 -05:00
if (! empty(\LibreNMS\Config::get('distributed_poller_group'))) {
$where .= ' AND poller_group IN(' . \LibreNMS\Config::get('distributed_poller_group') . ')';
}
2015-07-13 20:10:26 +02:00
2017-04-28 16:50:00 -05:00
global $device;
foreach (dbFetchRows("SELECT * FROM `devices` WHERE disabled = 0 $where ORDER BY device_id DESC", $sqlparams) as $device) {
2023-08-13 09:56:54 -05:00
$device_start = microtime(true);
if (discover_device($device, $module_override)) {
$discovered_devices++;
$device_time = round(microtime(true) - $device_start, 3);
DB::table('devices')->where('device_id', $device['device_id'])->update([
'last_discovered_timetaken' => $device_time,
'last_discovered' => DB::raw('NOW()'),
]);
echo "Discovered in $device_time seconds\n\n";
}
2008-03-22 23:09:35 +00:00
}
$end = microtime(true);
2008-03-22 23:09:35 +00:00
$run = ($end - $start);
$proctime = substr($run, 0, 5);
if (isset($new_discovery_lock)) {
$new_discovery_lock->release();
}
2019-06-23 00:29:12 -05:00
$string = $argv[0] . " $doing " . date(\LibreNMS\Config::get('dateformat.compact')) . " - $discovered_devices devices discovered in $proctime secs";
d_echo("$string\n");
2008-03-22 23:09:35 +00:00
2012-04-29 20:04:35 +00:00
if (! isset($options['q'])) {
echo PHP_EOL;
app(\App\Polling\Measure\MeasurementManager::class)->printStats();
}
2010-11-24 11:32:53 +00:00
logfile($string);
if ($doing !== 'new' && $discovered_devices == 0) {
// No discoverable devices, either down or disabled
exit(5);
}