Files

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

167 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2010-09-03 18:26:59 +00:00
#!/usr/bin/env php
2009-10-28 13:49:37 +00:00
<?php
/**
* 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.
*
* 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-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @copyright (C) 2006 - 2012 Adam Armstrong
2020-09-21 14:54:51 +02:00
*
* Modified 4/17/19
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
2021-11-17 19:23:55 -06:00
use App\Action;
use App\Actions\Device\UpdateDeviceGroupsAction;
use LibreNMS\Alert\AlertRules;
use LibreNMS\Config;
use LibreNMS\Data\Store\Datastore;
2021-04-29 22:42:18 -05:00
use LibreNMS\Util\Debug;
$init_modules = ['polling', 'alerts', 'laravel'];
require __DIR__ . '/includes/init.php';
2009-10-28 13:49:37 +00:00
$poller_start = microtime(true);
2021-11-17 19:23:55 -06:00
Log::setDefaultDriver('console');
echo Config::get('project_name') . " Poller\n";
2015-07-13 20:10:26 +02:00
$options = getopt('h:m:i:n:r::d::v::a::f::q');
2015-07-13 20:10:26 +02:00
2019-01-07 21:26:57 -06: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'];
} else {
2019-01-07 21:26:57 -06:00
if (preg_match('/\*/', $options['h'])) {
2021-03-28 17:25:30 -05:00
$where = "AND `hostname` LIKE '" . str_replace('*', '%', $options['h']) . "'";
2019-01-07 21:26:57 -06:00
} else {
2021-03-28 17:25:30 -05:00
$where = "AND `hostname` = '" . $options['h'] . "'";
2019-01-07 21:26:57 -06:00
}
$doing = $options['h'];
}
}
}
if (isset($options['i']) && $options['i'] && isset($options['n'])) {
2011-05-03 11:24:50 +00:00
$where = true;
// FIXME
$query = 'SELECT * FROM (SELECT @rownum :=0) r,
2011-05-03 11:24:50 +00:00
(
SELECT @rownum := @rownum +1 AS rownum, `devices`.*
2011-05-03 11:24:50 +00:00
FROM `devices`
2011-09-20 09:55:11 +00:00
WHERE `disabled` = 0
2011-05-03 11:24:50 +00:00
ORDER BY `device_id` ASC
) temp
2021-03-28 17:25:30 -05:00
WHERE MOD(temp.rownum, ' . $options['i'] . ') = ' . $options['n'] . ';';
$doing = $options['n'] . '/' . $options['i'];
2009-10-28 13:49:37 +00:00
}
2019-01-07 21:26:57 -06:00
if (empty($where)) {
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\n";
echo "-i <instances> -n <number> Poll as instance <number> of <instances>\n";
echo " Instances start at 0. 0-3 for -n 4\n\n";
2011-04-13 09:29:44 +00:00
echo "Debugging and testing options:\n";
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";
2009-10-28 13:49:37 +00:00
echo "\n";
echo "No polling type specified!\n";
exit;
}
2009-10-28 13:49:37 +00:00
2021-04-29 22:42:18 -05:00
if (Debug::set(isset($options['d']), false) || isset($options['v'])) {
echo \LibreNMS\Util\Version::get()->header();
2010-08-21 12:54:42 +00:00
echo "DEBUG!\n";
if (isset($options['v'])) {
2021-04-29 22:42:18 -05:00
Debug::setVerbose();
}
\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache
2010-08-21 12:54:42 +00:00
}
// If we've specified modules with -m, use them
$module_override = parse_modules('poller', $options);
$datastore = Datastore::init($options);
2011-10-01 14:54:06 +00:00
echo "Starting polling run:\n\n";
$polled_devices = 0;
$unreachable_devices = 0;
if (! isset($query)) {
$query = "SELECT * FROM `devices` WHERE `disabled` = 0 $where ORDER BY `device_id` ASC";
}
foreach (dbFetch($query) as $device) {
2019-11-14 21:56:06 +00:00
DeviceCache::setPrimary($device['device_id']);
if (! poll_device($device, $module_override)) {
$unreachable_devices++;
}
// Update device_groups
echo "### Start Device Groups ###\n";
$dg_start = microtime(true);
2021-11-17 19:23:55 -06:00
$group_changes = Action::execute(UpdateDeviceGroupsAction::class);
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";
echo "#### Start Alerts ####\n";
$rules = new AlertRules();
$rules->runRules($device['device_id']);
echo "#### End Alerts ####\r\n";
2012-01-25 05:50:21 +00:00
$polled_devices++;
}
$poller_end = microtime(true);
$poller_run = ($poller_end - $poller_start);
$poller_time = substr($poller_run, 0, 5);
2019-06-23 00:29:12 -05:00
$string = $argv[0] . " $doing " . date(Config::get('dateformat.compact')) . " - $polled_devices devices polled in $poller_time secs";
d_echo("$string\n");
if (! isset($options['q'])) {
echo PHP_EOL;
app(\App\Polling\Measure\MeasurementManager::class)->printStats();
}
2010-11-24 11:32:53 +00:00
logfile($string);
Datastore::terminate();
2012-05-25 12:24:34 +00:00
// Remove this for testing
// print_r(get_defined_vars());
if ($polled_devices === $unreachable_devices) {
exit(6);
}
exit(0);