2010-09-03 18:26:59 +00:00
|
|
|
#!/usr/bin/env php
|
2008-03-22 23:09:35 +00:00
|
|
|
<?php
|
|
|
|
|
2012-05-09 10:01:42 +00:00
|
|
|
/**
|
2016-09-08 14:12:23 +01:00
|
|
|
* LibreNMS
|
2011-03-15 15:12:16 +00:00
|
|
|
*
|
2016-09-08 14:12:23 +01:00
|
|
|
* This file is part of LibreNMS.
|
2012-05-09 10:01:42 +00:00
|
|
|
*
|
2013-10-28 12:01:36 -07:00
|
|
|
* @copyright (C) 2006 - 2012 Adam Armstrong
|
2011-03-15 15:12:16 +00:00
|
|
|
*/
|
2021-04-29 22:42:18 -05:00
|
|
|
|
|
|
|
use LibreNMS\Util\Debug;
|
|
|
|
|
2016-11-21 14:12:59 -06:00
|
|
|
$init_modules = ['discovery'];
|
|
|
|
require __DIR__ . '/includes/init.php';
|
2008-03-22 23:09:35 +00:00
|
|
|
|
2016-01-08 13:33:32 +01: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'])) {
|
2019-10-16 21:22:05 +00:00
|
|
|
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 = '';
|
2011-03-16 14:21:33 +00:00
|
|
|
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') {
|
2020-10-07 07:36:35 -05:00
|
|
|
$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
|
|
|
|
|
2015-10-09 12:36:41 +02:00
|
|
|
if (isset($options['os'])) {
|
2015-10-09 19:41:19 +02:00
|
|
|
$where .= ' AND os = ?';
|
|
|
|
$sqlparams[] = $options['os'];
|
2015-10-09 12:36:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 12:57:43 +02:00
|
|
|
if (isset($options['type'])) {
|
2015-10-09 19:41:19 +02:00
|
|
|
$where .= ' AND type = ?';
|
|
|
|
$sqlparams[] = $options['type'];
|
2015-10-09 12:57:43 +02:00
|
|
|
}
|
|
|
|
|
2011-03-09 12:11:14 +00:00
|
|
|
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'];
|
2010-07-19 18:21:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 22:42:18 -05:00
|
|
|
if (Debug::set(isset($options['d']), false) || isset($options['v'])) {
|
2022-10-02 00:41:56 -05:00
|
|
|
echo \LibreNMS\Util\Version::get()->header();
|
2017-04-22 14:10:15 +01:00
|
|
|
|
2011-03-23 09:54:56 +00:00
|
|
|
echo "DEBUG!\n";
|
2021-04-29 22:42:18 -05:00
|
|
|
Debug::setVerbose(isset($options['v']));
|
2020-05-19 14:35:32 -05:00
|
|
|
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
|
2010-08-21 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
2011-03-15 15:12:16 +00:00
|
|
|
if (! $where) {
|
2011-03-23 09:54:56 +00:00
|
|
|
echo "-h <device id> | <device hostname wildcard> Poll single device\n";
|
2018-07-12 15:30:39 -05:00
|
|
|
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";
|
2011-03-27 10:21:19 +00:00
|
|
|
echo "-i <instances> -n <number> Poll as instance <number> of <instances>\n";
|
2018-07-12 15:30:39 -05:00
|
|
|
echo " Instances start at 0. 0-3 for -n 4\n";
|
2011-04-04 09:52:09 +00:00
|
|
|
echo "\n";
|
|
|
|
echo "Debugging and testing options:\n";
|
2018-07-12 15:30:39 -05:00
|
|
|
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";
|
2011-04-04 09:52:09 +00:00
|
|
|
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
|
|
|
|
2018-07-12 15:30:39 -05:00
|
|
|
// If we've specified modules with -m, use them
|
|
|
|
$module_override = parse_modules('discovery', $options);
|
|
|
|
|
2010-09-26 22:10:04 +00:00
|
|
|
$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-03-19 22:14:08 +00:00
|
|
|
}
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2017-04-28 16:50:00 -05:00
|
|
|
global $device;
|
2023-10-12 07:15:03 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-08 13:33:32 +01:00
|
|
|
$end = microtime(true);
|
2008-03-22 23:09:35 +00:00
|
|
|
$run = ($end - $start);
|
|
|
|
$proctime = substr($run, 0, 5);
|
|
|
|
|
2020-10-07 07:36:35 -05:00
|
|
|
if (isset($new_discovery_lock)) {
|
2017-07-03 14:14:36 +09:30
|
|
|
$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";
|
2015-08-20 16:06:44 +02:00
|
|
|
d_echo("$string\n");
|
2008-03-22 23:09:35 +00:00
|
|
|
|
2012-04-29 20:04:35 +00:00
|
|
|
if (! isset($options['q'])) {
|
2021-10-06 17:09:54 -05:00
|
|
|
echo PHP_EOL;
|
|
|
|
app(\App\Polling\Measure\MeasurementManager::class)->printStats();
|
2011-05-05 11:27:22 +00:00
|
|
|
}
|
|
|
|
|
2010-11-24 11:32:53 +00:00
|
|
|
logfile($string);
|
2018-06-30 06:19:49 -05:00
|
|
|
|
2018-07-10 22:41:24 +02:00
|
|
|
if ($doing !== 'new' && $discovered_devices == 0) {
|
2018-06-30 06:19:49 -05:00
|
|
|
// No discoverable devices, either down or disabled
|
|
|
|
exit(5);
|
|
|
|
}
|