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
|
2021-02-08 23:29:04 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2012-05-09 10:01:42 +00:00
|
|
|
*
|
2013-10-28 19:01:36 +00:00
|
|
|
* @copyright (C) 2006 - 2012 Adam Armstrong
|
2020-09-21 12:54:51 +00:00
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
2021-11-18 01:23:55 +00:00
|
|
|
use App\Action;
|
|
|
|
use App\Actions\Device\UpdateDeviceGroupsAction;
|
2019-07-18 13:36:02 +00:00
|
|
|
use LibreNMS\Alert\AlertRules;
|
2020-09-21 12:54:51 +00:00
|
|
|
use LibreNMS\Config;
|
2020-03-16 14:17:58 +00:00
|
|
|
use LibreNMS\Data\Store\Datastore;
|
2021-04-30 03:42:18 +00:00
|
|
|
use LibreNMS\Util\Debug;
|
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);
|
2021-11-18 01:23:55 +00:00
|
|
|
Log::setDefaultDriver('console');
|
2020-09-21 12:54:51 +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'])) {
|
2020-09-21 13:59:34 +00:00
|
|
|
$where = 'AND `device_id` = ' . $options['h'];
|
2019-01-08 03:26:57 +00:00
|
|
|
$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'])) {
|
2021-03-28 22:25:30 +00:00
|
|
|
$where = "AND `hostname` LIKE '" . str_replace('*', '%', $options['h']) . "'";
|
2019-01-08 03:26:57 +00:00
|
|
|
} else {
|
2021-03-28 22:25:30 +00:00
|
|
|
$where = "AND `hostname` = '" . $options['h'] . "'";
|
2019-01-08 03:26:57 +00:00
|
|
|
}
|
|
|
|
$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
|
2021-03-28 22:25:30 +00:00
|
|
|
WHERE MOD(temp.rownum, ' . $options['i'] . ') = ' . $options['n'] . ';';
|
2020-09-21 12:54:51 +00:00
|
|
|
$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
|
|
|
|
2021-04-30 03:42:18 +00:00
|
|
|
if (Debug::set(isset($options['d']), false) || 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'])) {
|
2021-04-30 03:42:18 +00:00
|
|
|
Debug::setVerbose();
|
2016-01-17 14:39:17 +00:00
|
|
|
}
|
2020-05-19 19:35:32 +00:00
|
|
|
\LibreNMS\Util\OS::updateCache(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
|
|
|
$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;
|
2020-09-21 12:54:51 +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']);
|
2018-06-30 11:19:49 +00:00
|
|
|
|
2020-09-21 12:54:51 +00:00
|
|
|
if (! poll_device($device, $module_override)) {
|
2018-06-30 11:19:49 +00:00
|
|
|
$unreachable_devices++;
|
|
|
|
}
|
|
|
|
|
2020-05-30 23:23:31 +00:00
|
|
|
// Update device_groups
|
|
|
|
echo "### Start Device Groups ###\n";
|
|
|
|
$dg_start = microtime(true);
|
2021-11-18 01:23:55 +00:00
|
|
|
$group_changes = Action::execute(UpdateDeviceGroupsAction::class);
|
2020-09-21 13:59:34 +00:00
|
|
|
d_echo('Groups Added: ' . implode(',', $group_changes['attached']) . PHP_EOL);
|
|
|
|
d_echo('Groups Removed: ' . implode(',', $group_changes['detached']) . PHP_EOL);
|
|
|
|
echo '### End Device Groups, runtime: ' . round(microtime(true) - $dg_start, 4) . "s ### \n\n";
|
2020-05-30 23:23:31 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-21 12:54:51 +00:00
|
|
|
$poller_end = microtime(true);
|
|
|
|
$poller_run = ($poller_end - $poller_start);
|
2015-07-13 18:10:26 +00:00
|
|
|
$poller_time = substr($poller_run, 0, 5);
|
2011-05-03 21:17:04 +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
|
|
|
|
2020-09-21 12:54:51 +00:00
|
|
|
if (! isset($options['q'])) {
|
2021-10-06 22:09:54 +00:00
|
|
|
echo PHP_EOL;
|
|
|
|
app(\App\Polling\Measure\MeasurementManager::class)->printStats();
|
2016-11-23 06:57:19 +00:00
|
|
|
}
|
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);
|