librenms/includes/polling/core.inc.php
Tony Murray 3e35ee0e7d 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

104 lines
4.0 KiB
PHP

<?php
/*
* LibreNMS Network Management and Monitoring System
* Copyright (C) 2006-2011, Observium Developers - http://www.observium.org
*
* 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.
*
* See COPYING for more details.
*/
use App\Models\Location;
use LibreNMS\Config;
use LibreNMS\RRD\RrdDefinition;
$snmpdata = snmp_get_multi_oid($device, 'sysUpTime.0 sysLocation.0 sysContact.0 sysName.0 sysObjectID.0 sysDescr.0', '-OQnUt', 'SNMPv2-MIB');
$poll_device['sysUptime'] = $snmpdata['.1.3.6.1.2.1.1.3.0'];
$poll_device['sysLocation'] = $snmpdata['.1.3.6.1.2.1.1.6.0'];
$poll_device['sysContact'] = $snmpdata['.1.3.6.1.2.1.1.4.0'];
$poll_device['sysName'] = strtolower($snmpdata['.1.3.6.1.2.1.1.5.0']);
$poll_device['sysObjectID'] = $snmpdata['.1.3.6.1.2.1.1.2.0'];
$poll_device['sysDescr'] = $snmpdata['.1.3.6.1.2.1.1.1.0'];
if (!empty($agent_data['uptime'])) {
list($uptime) = explode(' ', $agent_data['uptime']);
$uptime = round($uptime);
echo "Using UNIX Agent Uptime ($uptime)\n";
} else {
$uptime_data = snmp_get_multi($device, 'snmpEngineTime.0 hrSystemUptime.0', '-OQnUst', 'HOST-RESOURCES-MIB:SNMP-FRAMEWORK-MIB');
$uptime = max(
round($poll_device['sysUptime'] / 100),
$config['os'][$device['os']]['bad_snmpEngineTime'] ? 0 : $uptime_data[0]['snmpEngineTime'],
$config['os'][$device['os']]['bad_hrSystemUptime'] ? 0 : round($uptime_data[0]['hrSystemUptime'] / 100)
);
d_echo("Uptime seconds: $uptime\n");
}
if ($uptime != 0 && $config['os'][$device['os']]['bad_uptime'] !== true) {
if ($uptime < $device['uptime']) {
log_event('Device rebooted after ' . formatUptime($device['uptime']) . " -> {$uptime}s", $device, 'reboot', 4, $device['uptime']);
}
$tags = array(
'rrd_def' => RrdDefinition::make()->addDataset('uptime', 'GAUGE', 0),
);
data_update($device, 'uptime', $tags, $uptime);
$graphs['uptime'] = true;
echo 'Uptime: ' . formatUptime($uptime) . PHP_EOL;
$update_array['uptime'] = $uptime;
$device['uptime'] = $uptime;
}//end if
$poll_device['sysLocation'] = str_replace('"', '', $poll_device['sysLocation']);
// Rewrite sysLocation if there is a mapping array (database too?)
if (!empty($poll_device['sysLocation']) && (is_array($config['location_map']) || is_array($config['location_map_regex']) || is_array($config['location_map_regex_sub']))) {
$poll_device['sysLocation'] = rewrite_location($poll_device['sysLocation']);
}
$poll_device['sysContact'] = str_replace('"', '', $poll_device['sysContact']);
foreach (array('sysLocation', 'sysContact') as $elem) {
if ($poll_device[$elem] == 'not set') {
$poll_device[$elem] = '';
}
}
// Save results of various polled values to the database
foreach (array('sysContact', 'sysObjectID', 'sysName', 'sysDescr') as $elem) {
if ($poll_device[$elem] != $device[$elem]) {
$update_array[$elem] = $poll_device[$elem];
$device[$elem] = $poll_device[$elem];
log_event("$elem -> " . $poll_device[$elem], $device, 'system', 3);
}
}
if ($device['override_sysLocation'] == 0 && $poll_device['sysLocation']) {
/** @var Location $location */
$location = Location::firstOrCreate(['location' => $poll_device['sysLocation']]);
if ($device['location_id'] != $location->id) {
$device['location_id'] = $location->id;
$update_array['location_id'] = $location->id;
log_event('Location -> ' . $location->location, $device, 'system', 3);
}
}
// make sure the location has coordinates
if (Config::get('geoloc.latlng', true) && ($location || $location = Location::find($device['location_id']))) {
if (!$location->hasCoordinates()) {
$location->lookupCoordinates();
$location->save();
}
}
unset($snmpdata, $uptime_data, $uptime, $tags, $poll_device);