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
|
|
|
*
|
2016-09-08 14:12:23 +01:00
|
|
|
* @package LibreNMS
|
2013-10-28 12:01:36 -07:00
|
|
|
* @subpackage discovery
|
|
|
|
* @copyright (C) 2006 - 2012 Adam Armstrong
|
2011-03-15 15:12:16 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-24 03:37:52 -06:00
|
|
|
use LibreNMS\Util\FileLock;
|
|
|
|
|
2016-11-21 14:12:59 -06:00
|
|
|
$init_modules = array('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);
|
2015-10-09 19:41:19 +02:00
|
|
|
$sqlparams = array();
|
2016-08-28 17:32:55 -05:00
|
|
|
$options = getopt('h:m:i:n:d::v::a::q', array('os:','type:'));
|
2008-03-22 23:09:35 +00:00
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
if (!isset($options['q'])) {
|
2015-08-24 20:54:19 +00:00
|
|
|
echo $config['project_name_version']." Discovery\n";
|
2011-05-06 14:04:53 +00:00
|
|
|
}
|
2009-11-07 02:30:38 +00:00
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
if (isset($options['h'])) {
|
|
|
|
if ($options['h'] == 'odd') {
|
|
|
|
$options['n'] = '1';
|
|
|
|
$options['i'] = '2';
|
2016-08-28 17:32:55 -05:00
|
|
|
} elseif ($options['h'] == 'even') {
|
2015-07-13 20:10:26 +02:00
|
|
|
$options['n'] = '0';
|
|
|
|
$options['i'] = '2';
|
2016-08-28 17:32:55 -05:00
|
|
|
} elseif ($options['h'] == 'all') {
|
2015-07-13 20:10:26 +02:00
|
|
|
$where = ' ';
|
|
|
|
$doing = 'all';
|
2016-08-28 17:32:55 -05:00
|
|
|
} elseif ($options['h'] == 'new') {
|
2017-11-24 03:37:52 -06:00
|
|
|
$new_discovery_lock = FileLock::lockOrDie('new-discovery');
|
2015-07-13 20:10:26 +02:00
|
|
|
$where = 'AND `last_discovered` IS NULL';
|
|
|
|
$doing = 'new';
|
2016-08-28 17:32:55 -05:00
|
|
|
} elseif ($options['h']) {
|
2015-07-13 20:10:26 +02:00
|
|
|
if (is_numeric($options['h'])) {
|
|
|
|
$where = "AND `device_id` = '".$options['h']."'";
|
|
|
|
$doing = $options['h'];
|
2016-08-28 17:32:55 -05:00
|
|
|
} else {
|
2015-07-13 20:10:26 +02:00
|
|
|
$where = "AND `hostname` LIKE '".str_replace('*', '%', mres($options['h']))."'";
|
|
|
|
$doing = $options['h'];
|
|
|
|
}
|
|
|
|
}//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
|
|
|
}
|
|
|
|
|
2015-07-13 20:10:26 +02: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']."'";
|
2015-07-13 20:10:26 +02:00
|
|
|
$doing = $options['n'].'/'.$options['i'];
|
2010-07-19 18:21:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:08:00 -05:00
|
|
|
if (set_debug(isset($options['d'])) || isset($options['v'])) {
|
2017-10-26 01:56:09 -05:00
|
|
|
$versions = version_info();
|
2017-04-22 14:10:15 +01:00
|
|
|
echo <<<EOH
|
|
|
|
===================================
|
|
|
|
Version info:
|
|
|
|
Commit SHA: {$versions['local_sha']}
|
|
|
|
Commit Date: {$versions['local_date']}
|
|
|
|
DB Schema: {$versions['db_schema']}
|
|
|
|
PHP: {$versions['php_ver']}
|
|
|
|
MySQL: {$versions['mysql_ver']}
|
|
|
|
RRDTool: {$versions['rrdtool_ver']}
|
|
|
|
SNMP: {$versions['netsnmp_ver']}
|
|
|
|
==================================
|
|
|
|
EOH;
|
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
echo "DEBUG!\n";
|
2016-01-17 14:39:17 +00:00
|
|
|
if (isset($options['v'])) {
|
|
|
|
$vdebug = true;
|
|
|
|
}
|
2018-01-09 16:51:26 +00:00
|
|
|
update_os_cache(true); // Force update of OS Cache
|
2010-08-21 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
if (!$where) {
|
|
|
|
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";
|
2015-07-13 20:10:26 +02: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";
|
2015-07-13 20:10:26 +02: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";
|
2015-07-13 20:10:26 +02:00
|
|
|
echo "\n";
|
|
|
|
echo "Invalid arguments!\n";
|
|
|
|
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
|
|
|
|
2016-02-24 12:48:22 -08:00
|
|
|
if (!empty($config['distributed_poller_group'])) {
|
2015-07-13 20:10:26 +02:00
|
|
|
$where .= ' AND poller_group IN('.$config['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;
|
2017-11-23 02:31:15 -06:00
|
|
|
foreach (dbFetch("SELECT * FROM `devices` WHERE disabled = 0 AND snmp_disable = 0 $where ORDER BY device_id DESC", $sqlparams) as $device) {
|
2018-07-12 15:30:39 -05:00
|
|
|
$discovered_devices += (int)discover_device($device, $module_override);
|
2008-03-22 23:09:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 13:33:32 +01:00
|
|
|
$end = microtime(true);
|
2015-07-13 20:10:26 +02:00
|
|
|
$run = ($end - $start);
|
2008-03-22 23:09:35 +00:00
|
|
|
$proctime = substr($run, 0, 5);
|
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
if ($discovered_devices) {
|
2017-01-13 12:47:16 +00:00
|
|
|
dbInsert(array('type' => 'discover', 'doing' => $doing, 'start' => $start, 'duration' => $proctime, 'devices' => $discovered_devices, 'poller' => $config['distributed_poller_name']), 'perf_times');
|
2017-04-28 04:31:48 +01:00
|
|
|
if ($doing === 'new') {
|
|
|
|
// We have added a new device by this point so we might want to do some other work
|
|
|
|
oxidized_reload_nodes();
|
|
|
|
}
|
2010-07-19 18:21:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 14:14:36 +09:30
|
|
|
if ($doing === 'new') {
|
|
|
|
$new_discovery_lock->release();
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
$string = $argv[0]." $doing ".date($config['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
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
if (!isset($options['q'])) {
|
2016-11-23 00:57:19 -06:00
|
|
|
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);
|
|
|
|
}
|