2010-09-03 18:26:59 +00:00
|
|
|
#!/usr/bin/env php
|
2009-10-28 13:49:37 +00:00
|
|
|
<?php
|
2012-05-09 10:01:42 +00:00
|
|
|
/**
|
2019-07-18 13:36:02 +00:00
|
|
|
* poller.php
|
|
|
|
*
|
|
|
|
* -Description-
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2011-03-15 15:12:16 +00:00
|
|
|
*
|
2019-07-18 13:36:02 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-05-09 10:01:42 +00:00
|
|
|
*
|
2016-09-08 13:12:23 +00:00
|
|
|
* @package LibreNMS
|
2013-10-28 19:01:36 +00:00
|
|
|
* @subpackage poller
|
|
|
|
* @copyright (C) 2006 - 2012 Adam Armstrong
|
2019-07-18 13:36:02 +00:00
|
|
|
|
|
|
|
* Modified 4/17/19
|
|
|
|
* @author Heath Barnhart <hbarnhart@kanren.net>
|
2011-03-15 15:12:16 +00:00
|
|
|
*/
|
|
|
|
|
2018-07-12 20:30:39 +00:00
|
|
|
use LibreNMS\Config;
|
2019-07-18 13:36:02 +00:00
|
|
|
use LibreNMS\Alert\AlertRules;
|
2020-03-16 14:17:58 +00:00
|
|
|
use LibreNMS\Data\Store\Datastore;
|
2018-07-12 20:30:39 +00:00
|
|
|
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
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`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 22:49:18 +00:00
|
|
|
$init_modules = ['polling', 'alerts', 'laravel'];
|
2016-11-21 20:12:59 +00:00
|
|
|
require __DIR__ . '/includes/init.php';
|
2009-10-28 13:49:37 +00:00
|
|
|
|
2016-01-08 12:33:32 +00:00
|
|
|
$poller_start = microtime(true);
|
2019-10-16 21:22:05 +00:00
|
|
|
echo Config::get('project_name')." Poller\n";
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2016-11-23 06:57:19 +00:00
|
|
|
$options = getopt('h:m:i:n:r::d::v::a::f::q');
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2019-01-08 03:26:57 +00:00
|
|
|
if (isset($options['h'])) {
|
|
|
|
if ($options['h'] == 'odd') {
|
|
|
|
$options['n'] = '1';
|
|
|
|
$options['i'] = '2';
|
|
|
|
} elseif ($options['h'] == 'even') {
|
|
|
|
$options['n'] = '0';
|
|
|
|
$options['i'] = '2';
|
|
|
|
} elseif ($options['h'] == 'all') {
|
|
|
|
$where = ' ';
|
|
|
|
$doing = 'all';
|
|
|
|
} elseif ($options['h']) {
|
|
|
|
if (is_numeric($options['h'])) {
|
|
|
|
$where = "AND `device_id` = " . $options['h'];
|
|
|
|
$doing = $options['h'];
|
2016-09-21 17:56:42 +00:00
|
|
|
} else {
|
2019-01-08 03:26:57 +00:00
|
|
|
if (preg_match('/\*/', $options['h'])) {
|
|
|
|
$where = "AND `hostname` LIKE '" . str_replace('*', '%', mres($options['h'])) . "'";
|
|
|
|
} else {
|
|
|
|
$where = "AND `hostname` = '" . mres($options['h']) . "'";
|
|
|
|
}
|
|
|
|
$doing = $options['h'];
|
2016-09-21 17:56:42 +00:00
|
|
|
}
|
2015-07-13 18:10:26 +00:00
|
|
|
}
|
2010-07-19 18:21:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
if (isset($options['i']) && $options['i'] && isset($options['n'])) {
|
|
|
|
$where = true;
|
|
|
|
// FIXME
|
2016-12-16 18:15:13 +00:00
|
|
|
$query = 'SELECT * FROM (SELECT @rownum :=0) r,
|
2015-07-13 18:10:26 +00:00
|
|
|
(
|
2016-12-22 21:33:51 +00:00
|
|
|
SELECT @rownum := @rownum +1 AS rownum, `devices`.*
|
2015-07-13 18:10:26 +00:00
|
|
|
FROM `devices`
|
|
|
|
WHERE `disabled` = 0
|
|
|
|
ORDER BY `device_id` ASC
|
|
|
|
) temp
|
|
|
|
WHERE MOD(temp.rownum, '.mres($options['i']).') = '.mres($options['n']).';';
|
|
|
|
$doing = $options['n'].'/'.$options['i'];
|
2009-10-28 13:49:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 03:26:57 +00:00
|
|
|
if (empty($where)) {
|
2015-07-13 18:10:26 +00:00
|
|
|
echo "-h <device id> | <device hostname wildcard> Poll single device\n";
|
2018-07-12 20:30:39 +00: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\n";
|
2015-07-13 18:10:26 +00:00
|
|
|
echo "-i <instances> -n <number> Poll as instance <number> of <instances>\n";
|
2018-07-12 20:30:39 +00:00
|
|
|
echo " Instances start at 0. 0-3 for -n 4\n\n";
|
2015-07-13 18:10:26 +00:00
|
|
|
echo "Debugging and testing options:\n";
|
2018-07-12 20:30:39 +00:00
|
|
|
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";
|
2015-07-13 18:10:26 +00:00
|
|
|
echo "\n";
|
|
|
|
echo "No polling type specified!\n";
|
|
|
|
exit;
|
2011-03-27 10:21:19 +00:00
|
|
|
}
|
2009-10-28 13:49:37 +00:00
|
|
|
|
2018-07-13 22:08:00 +00:00
|
|
|
if (set_debug(isset($options['d'])) || isset($options['v'])) {
|
2017-10-26 06:56:09 +00:00
|
|
|
$versions = version_info();
|
2017-04-22 13:10:15 +00: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 18:10:26 +00: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
|
|
|
}
|
2010-06-23 19:17:06 +00:00
|
|
|
|
2018-07-12 20:30:39 +00:00
|
|
|
// If we've specified modules with -m, use them
|
|
|
|
$module_override = parse_modules('poller', $options);
|
2020-03-16 14:17:58 +00:00
|
|
|
set_debug($debug);
|
2018-07-12 20:30:39 +00:00
|
|
|
|
2020-03-16 14:17:58 +00:00
|
|
|
$datastore = Datastore::init($options);
|
2011-10-01 14:54:06 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
echo "Starting polling run:\n\n";
|
2010-06-23 19:17:06 +00:00
|
|
|
$polled_devices = 0;
|
2018-06-30 11:19:49 +00:00
|
|
|
$unreachable_devices = 0;
|
2015-07-13 18:10:26 +00:00
|
|
|
if (!isset($query)) {
|
2016-12-16 18:15:13 +00:00
|
|
|
$query = "SELECT * FROM `devices` WHERE `disabled` = 0 $where ORDER BY `device_id` ASC";
|
2011-05-03 12:22:04 +00:00
|
|
|
}
|
2011-03-08 17:14:32 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
foreach (dbFetch($query) as $device) {
|
2019-11-14 21:56:06 +00:00
|
|
|
DeviceCache::setPrimary($device['device_id']);
|
2016-10-15 01:03:26 +00:00
|
|
|
if ($device['os_group'] == 'cisco') {
|
|
|
|
$device['vrf_lite_cisco'] = dbFetchRows("SELECT * FROM `vrf_lite_cisco` WHERE `device_id` = " . $device['device_id']);
|
|
|
|
} else {
|
|
|
|
$device['vrf_lite_cisco'] = '';
|
|
|
|
}
|
2018-06-30 11:19:49 +00:00
|
|
|
|
2018-07-12 20:30:39 +00:00
|
|
|
if (!poll_device($device, $module_override)) {
|
2018-06-30 11:19:49 +00:00
|
|
|
$unreachable_devices++;
|
|
|
|
}
|
|
|
|
|
2016-09-25 15:01:58 +00:00
|
|
|
echo "#### Start Alerts ####\n";
|
2019-07-18 13:36:02 +00:00
|
|
|
$rules = new AlertRules();
|
|
|
|
$rules->runRules($device['device_id']);
|
2016-09-25 15:01:58 +00:00
|
|
|
echo "#### End Alerts ####\r\n";
|
2015-07-13 18:10:26 +00:00
|
|
|
$polled_devices++;
|
2011-05-03 21:17:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 12:33:32 +00:00
|
|
|
$poller_end = microtime(true);
|
2015-07-13 18:10:26 +00:00
|
|
|
$poller_run = ($poller_end - $poller_start);
|
|
|
|
$poller_time = substr($poller_run, 0, 5);
|
2011-05-03 21:17:04 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
if ($polled_devices) {
|
2018-06-30 11:19:49 +00:00
|
|
|
dbInsert(array(
|
|
|
|
'type' => 'poll',
|
|
|
|
'doing' => $doing,
|
|
|
|
'start' => $poller_start,
|
|
|
|
'duration' => $poller_time,
|
|
|
|
'devices' => $polled_devices,
|
2019-06-23 05:29:12 +00:00
|
|
|
'poller' => Config::get('base_url')
|
2018-06-30 11:19:49 +00:00
|
|
|
), 'perf_times');
|
2011-05-08 00:13:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 05:29:12 +00:00
|
|
|
$string = $argv[0] . " $doing " . date(Config::get('dateformat.compact')) . " - $polled_devices devices polled in $poller_time secs";
|
2015-08-20 14:06:44 +00:00
|
|
|
d_echo("$string\n");
|
2011-05-13 11:42:26 +00:00
|
|
|
|
2016-11-23 06:57:19 +00:00
|
|
|
if (!isset($options['q'])) {
|
|
|
|
printStats();
|
|
|
|
}
|
2010-11-24 11:32:53 +00:00
|
|
|
|
2011-09-20 14:29:04 +00:00
|
|
|
logfile($string);
|
2020-03-16 14:17:58 +00:00
|
|
|
Datastore::terminate();
|
2015-07-13 18:10:26 +00:00
|
|
|
// Remove this for testing
|
|
|
|
// print_r(get_defined_vars());
|
2018-06-30 11:19:49 +00:00
|
|
|
|
|
|
|
if ($polled_devices === $unreachable_devices) {
|
|
|
|
exit(6);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|