librenms/includes/common.php

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

905 lines
26 KiB
PHP
Raw Normal View History

<?php
2015-06-14 08:00:21 +00:00
/*
* LibreNMS - Common Functions
*
* Original Observium version by: Adam Armstrong, Tom Laermans
* Copyright (c) 2009-2012 Adam Armstrong.
*
* Additions for LibreNMS by: Neil Lathwood, Paul Gear, Tim DuFrane
* Copyright (c) 2014-2015 Neil Lathwood <http://www.lathwood.co.uk>
* Copyright (c) 2014-2015 Gear Consulting Pty Ltd <http://libertysys.com.au/>
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
use LibreNMS\Config;
use LibreNMS\Enum\Severity;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\Debug;
use LibreNMS\Util\IP;
use LibreNMS\Util\Laravel;
use Symfony\Component\Process\Process;
/**
* Execute and snmp command, filter debug output unless -v is specified
*
2021-09-08 21:35:56 +00:00
* @param array $command
* @return null|string
*/
function external_exec($command)
{
$device = DeviceCache::getPrimary();
$proc = new Process($command);
$proc->setTimeout(Config::get('snmp.exec_timeout', 1200));
if (Debug::isEnabled() && ! Debug::isVerbose()) {
$patterns = [
'/-c\' \'[\S]+\'/',
'/-u\' \'[\S]+\'/',
'/-U\' \'[\S]+\'/',
'/-A\' \'[\S]+\'/',
'/-X\' \'[\S]+\'/',
'/-P\' \'[\S]+\'/',
'/-H\' \'[\S]+\'/',
'/-y\' \'[\S]+\'/',
'/(udp|udp6|tcp|tcp6):([^:]+):([\d]+)/',
];
$replacements = [
'-c\' \'COMMUNITY\'',
'-u\' \'USER\'',
2019-01-29 13:40:15 +00:00
'-U\' \'USER\'',
'-A\' \'PASSWORD\'',
'-X\' \'PASSWORD\'',
'-P\' \'PASSWORD\'',
'-H\' \'HOSTNAME\'',
'-y\' \'KG_KEY\'',
'\1:HOSTNAME:\3',
];
$debug_command = preg_replace($patterns, $replacements, $proc->getCommandLine());
c_echo('SNMP[%c' . $debug_command . "%n]\n");
} elseif (Debug::isVerbose()) {
c_echo('SNMP[%c' . $proc->getCommandLine() . "%n]\n");
}
$proc->run();
$output = $proc->getOutput();
if ($proc->getExitCode()) {
if (Str::startsWith($proc->getErrorOutput(), 'Invalid authentication protocol specified')) {
\App\Models\Eventlog::log('Unsupported SNMP authentication algorithm - ' . $proc->getExitCode(), optional($device)->device_id, 'poller', Severity::Error);
} elseif (Str::startsWith($proc->getErrorOutput(), 'Invalid privacy protocol specified')) {
\App\Models\Eventlog::log('Unsupported SNMP privacy algorithm - ' . $proc->getExitCode(), optional($device)->device_id, 'poller', Severity::Error);
}
d_echo('Exitcode: ' . $proc->getExitCode());
d_echo($proc->getErrorOutput());
}
if (Debug::isEnabled() && ! Debug::isVerbose()) {
$ip_regex = '/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/';
$debug_output = preg_replace($ip_regex, '*', $output);
d_echo($debug_output . PHP_EOL);
} elseif (Debug::isVerbose()) {
d_echo($output . PHP_EOL);
}
d_echo($proc->getErrorOutput());
return $output;
}
function shorthost($hostname, $len = 12)
{
2014-03-28 17:05:39 +00:00
// IP addresses should not be shortened
if (filter_var($hostname, FILTER_VALIDATE_IP)) {
return $hostname;
}
$len = Config::get('shorthost_target_length', $len);
2015-07-13 18:10:26 +00:00
$parts = explode('.', $hostname);
$shorthost = $parts[0];
$i = 1;
while ($i < count($parts) && strlen($shorthost . '.' . $parts[$i]) < $len) {
$shorthost = $shorthost . '.' . $parts[$i];
$i++;
}
2020-09-21 13:40:17 +00:00
return $shorthost;
}
2015-07-13 18:10:26 +00:00
function print_error($text)
{
if (Laravel::isCli()) {
c_echo('%r' . $text . "%n\n");
} else {
2016-09-21 19:00:17 +00:00
echo '<div class="alert alert-danger"><i class="fa fa-fw fa-exclamation-circle" aria-hidden="true"></i> ' . $text . '</div>';
}
}
function print_message($text)
{
if (Laravel::isCli()) {
c_echo('%g' . $text . "%n\n");
} else {
2016-09-22 12:09:31 +00:00
echo '<div class="alert alert-success"><i class="fa fa-fw fa-check-circle" aria-hidden="true"></i> ' . $text . '</div>';
}
}
function get_sensor_rrd($device, $sensor)
{
return Rrd::name($device['hostname'], get_sensor_rrd_name($device, $sensor));
}
function get_sensor_rrd_name($device, $sensor)
{
// For IPMI, sensors tend to change order, and there is no index, so we prefer to use the description as key here.
if (Config::getOsSetting($device['os'], 'sensor_descr') || $sensor['poller_type'] == 'ipmi') {
return ['sensor', $sensor['sensor_class'], $sensor['sensor_type'], $sensor['sensor_descr']];
} else {
return ['sensor', $sensor['sensor_class'], $sensor['sensor_type'], $sensor['sensor_index']];
}
}
function get_port_rrdfile_path($hostname, $port_id, $suffix = '')
{
return Rrd::name($hostname, Rrd::portName($port_id, $suffix));
}
function get_port_by_index_cache($device_id, $ifIndex)
{
global $port_index_cache;
if (isset($port_index_cache[$device_id][$ifIndex]) && is_array($port_index_cache[$device_id][$ifIndex])) {
$port = $port_index_cache[$device_id][$ifIndex];
} else {
$port = get_port_by_ifIndex($device_id, $ifIndex);
$port_index_cache[$device_id][$ifIndex] = $port;
}
return $port;
}
function get_port_by_ifIndex($device_id, $ifIndex)
{
return dbFetchRow('SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', [$device_id, $ifIndex]);
}
function table_from_entity_type($type)
{
// Fuck you, english pluralisation.
if ($type == 'storage') {
return $type;
} else {
return $type . 's';
2015-07-13 18:10:26 +00:00
}
}
function get_entity_by_id_cache($type, $id)
{
global $entity_cache;
$table = table_from_entity_type($type);
if (is_array($entity_cache[$type][$id])) {
$entity = $entity_cache[$type][$id];
} else {
$entity = dbFetchRow('SELECT * FROM `' . $table . '` WHERE `' . $type . '_id` = ?', [$id]);
$entity_cache[$type][$id] = $entity;
2015-07-13 18:10:26 +00:00
}
2020-09-21 13:40:17 +00:00
return $entity;
2015-07-13 18:10:26 +00:00
}
function get_port_by_id($port_id)
{
if (is_numeric($port_id)) {
$port = dbFetchRow('SELECT * FROM `ports` WHERE `port_id` = ?', [$port_id]);
2015-02-22 19:28:09 +00:00
if (is_array($port)) {
return $port;
} else {
return false;
2015-07-13 18:10:26 +00:00
}
}
}
function get_sensor_by_id($sensor_id)
{
if (is_numeric($sensor_id)) {
$sensor = dbFetchRow('SELECT * FROM `sensors` WHERE `sensor_id` = ?', [$sensor_id]);
2015-02-22 19:28:09 +00:00
if (is_array($sensor)) {
return $sensor;
} else {
return false;
2015-07-13 18:10:26 +00:00
}
2015-02-22 19:28:09 +00:00
}
}
function get_device_id_by_port_id($port_id)
{
if (is_numeric($port_id)) {
$device_id = dbFetchCell('SELECT `device_id` FROM `ports` WHERE `port_id` = ?', [$port_id]);
2015-02-22 19:28:09 +00:00
if (is_numeric($device_id)) {
return $device_id;
} else {
return false;
2015-07-13 18:10:26 +00:00
}
2015-02-22 19:28:09 +00:00
}
}
function ifclass($ifOperStatus, $ifAdminStatus)
{
// fake a port model
return \LibreNMS\Util\Url::portLinkDisplayClass((object) ['ifOperStatus' => $ifOperStatus, 'ifAdminStatus' => $ifAdminStatus]);
}
2015-07-13 18:10:26 +00:00
function device_by_name($name)
{
return device_by_id_cache(getidbyname($name));
}
function accesspoint_by_id($ap_id, $refresh = '0')
{
$ap = dbFetchRow('SELECT * FROM `access_points` WHERE `accesspoint_id` = ?', [$ap_id]);
return $ap;
}
function device_by_id_cache($device_id, $refresh = false)
{
$model = $refresh ? DeviceCache::refresh((int) $device_id) : DeviceCache::get((int) $device_id);
$device = $model->toArray();
$device['location'] = $model->location->location ?? null;
$device['lat'] = $model->location->lat ?? null;
$device['lng'] = $model->location->lng ?? null;
$device['attribs'] = $model->getAttribs();
return $device;
}
function truncate($substring, $max = 50, $rep = '...')
{
if (strlen($substring) < 1) {
$string = $rep;
} else {
$string = $substring;
}
$leave = $max - strlen($rep);
if (strlen($string) > $max) {
return substr_replace($string, $rep, $leave);
} else {
return $string;
}
}
function gethostbyid($device_id)
{
return DeviceCache::get((int) $device_id)->hostname;
}
function strgen($length = 16)
{
$entropy = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e',
'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n',
'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w',
'W', 'x', 'X', 'y', 'Y', 'z', 'Z', ];
$string = '';
for ($i = 0; $i < $length; $i++) {
$key = mt_rand(0, 61);
$string .= $entropy[$key];
}
return $string;
}
function getpeerhost($id)
{
return dbFetchCell('SELECT `device_id` from `bgpPeers` WHERE `bgpPeer_id` = ?', [$id]);
}
function getifindexbyid($id)
{
return dbFetchCell('SELECT `ifIndex` FROM `ports` WHERE `port_id` = ?', [$id]);
}
function getifbyid($id)
{
return dbFetchRow('SELECT * FROM `ports` WHERE `port_id` = ?', [$id]);
}
function getifdescrbyid($id)
{
return dbFetchCell('SELECT `ifDescr` FROM `ports` WHERE `port_id` = ?', [$id]);
}
function getidbyname($hostname)
{
return DeviceCache::getByHostname($hostname)->device_id;
}
function zeropad($num, $length = 2)
{
return str_pad($num, $length, '0', STR_PAD_LEFT);
}
function set_dev_attrib($device, $attrib_type, $attrib_value)
{
return DeviceCache::get((int) $device['device_id'])->setAttrib($attrib_type, $attrib_value);
}
function get_dev_attribs($device_id)
{
return DeviceCache::get((int) $device_id)->getAttribs();
}
function get_dev_entity_state($device)
{
$state = [];
foreach (dbFetchRows('SELECT * FROM entPhysical_state WHERE `device_id` = ?', [$device]) as $entity) {
$state['group'][$entity['group']][$entity['entPhysicalIndex']][$entity['subindex']][$entity['key']] = $entity['value'];
$state['index'][$entity['entPhysicalIndex']][$entity['subindex']][$entity['group']][$entity['key']] = $entity['value'];
}
2020-09-21 13:40:17 +00:00
return $state;
}
function get_dev_attrib($device, $attrib_type)
{
return DeviceCache::get((int) $device['device_id'])->getAttrib($attrib_type);
}
function del_dev_attrib($device, $attrib_type)
{
return DeviceCache::get((int) $device['device_id'])->forgetAttrib($attrib_type);
}
/**
* Output using console color if possible
* https://github.com/pear/Console_Color2/blob/master/examples/documentation
*
2021-09-08 21:35:56 +00:00
* @param string $string the string to print with console color
* @param bool $enabled if set to false, this function does nothing
*/
function c_echo($string, $enabled = true)
{
if (! $enabled) {
return;
}
if (Laravel::isCli()) {
global $console_color;
if ($console_color) {
echo $console_color->convert($string);
} else {
// limited functionality for validate.php
$search = [
'/%n/',
'/%g/',
'/%R/',
'/%Y/',
'/%B/',
'/%((%)|.)/', // anything left over replace with empty string
];
$replace = [
"\e[0m",
"\e[32m",
"\e[1;31m",
"\e[1;33m",
"\e[1;34m",
'',
];
echo preg_replace($search, $replace, $string);
}
} else {
echo preg_replace('/%((%)|.)/', '', $string);
}
}
/*
* @return true if client IP address is authorized to access graphs
*/
function is_client_authorized($clientip)
{
if (Config::get('allow_unauth_graphs', false)) {
d_echo("Unauthorized graphs allowed\n");
2020-09-21 13:40:17 +00:00
return true;
}
foreach (Config::get('allow_unauth_graphs_cidr', []) as $range) {
try {
if (IP::parse($clientip)->inNetwork($range)) {
d_echo("Unauthorized graphs allowed from $range\n");
2020-09-21 13:40:17 +00:00
return true;
}
} catch (InvalidIpException $e) {
d_echo("Client IP ($clientip) is invalid.\n");
}
}
return false;
} // is_client_authorized
/*
* @return an array of all graph subtypes for the given type
*/
function get_graph_subtypes($type, $device = null)
{
$type = basename($type);
$types = [];
// find the subtypes defined in files
if ($handle = opendir(Config::get('install_dir') . "/includes/html/graphs/$type/")) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && $file != 'auth.inc.php' && strstr($file, '.inc.php')) {
$types[] = str_replace('.inc.php', '', $file);
}
}
closedir($handle);
}
sort($types);
2020-09-21 13:40:17 +00:00
return $types;
} // get_graph_subtypes
function get_smokeping_files($device)
{
$smokeping = new \LibreNMS\Util\Smokeping(DeviceCache::get((int) $device['device_id']));
2020-09-21 13:40:17 +00:00
return $smokeping->findFiles();
}
2015-07-10 15:10:35 +00:00
function generate_smokeping_file($device, $file = '')
{
$smokeping = new \LibreNMS\Util\Smokeping(DeviceCache::get((int) $device['device_id']));
2020-09-21 13:40:17 +00:00
return $smokeping->generateFileName($file);
}
/*
* @return rounded value to 10th/100th/1000th depending on input (valid: 10, 100, 1000)
*/
function round_Nth($val, $round_to)
{
if (($round_to == '10') || ($round_to == '100') || ($round_to == '1000')) {
$diff = $val % $round_to;
if ($diff >= ($round_to / 2)) {
$ret = $val + ($round_to - $diff);
} else {
$ret = $val - $diff;
}
2020-09-21 13:40:17 +00:00
return $ret;
}
} // end round_Nth
function is_customoid_graph($type, $subtype)
{
if (! empty($subtype) && $type == 'customoid') {
return true;
}
2020-09-21 13:40:17 +00:00
return false;
} // is_customoid_graph
//
// maintain a simple cache of objects
//
function object_add_cache($section, $obj)
{
global $object_cache;
$object_cache[$section][$obj] = true;
} // object_add_cache
function object_is_cached($section, $obj)
{
global $object_cache;
if (is_array($object_cache) && array_key_exists($obj, $object_cache)) {
2015-12-06 08:07:51 +00:00
return $object_cache[$section][$obj];
} else {
return false;
}
} // object_is_cached
2015-12-06 08:07:51 +00:00
function search_phrase_column($c)
{
global $searchPhrase;
2020-09-21 13:40:17 +00:00
2015-12-06 08:07:51 +00:00
return "$c LIKE '%$searchPhrase%'";
} // search_phrase_column
/**
* Parse location field for coordinates
2021-09-10 18:09:53 +00:00
*
* @param string location The location field to look for coords in.
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
* @return array|bool Containing the lat and lng coords
**/
function parse_location($location)
{
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
preg_match('/\[(-?[0-9. ]+), *(-?[0-9. ]+)\]/', $location, $tmp_loc);
if (is_numeric($tmp_loc[1]) && is_numeric($tmp_loc[2])) {
return ['lat' => $tmp_loc[1], 'lng' => $tmp_loc[2]];
}
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
return false;
}//end parse_location()
/**
* Convert a MySQL binary v4 (4-byte) or v6 (16-byte) IP address to a printable string.
2021-09-10 18:09:53 +00:00
*
2021-09-08 21:35:56 +00:00
* @param string $ip A binary string containing an IP address, as returned from MySQL's INET6_ATON function
* @return string Empty if not valid.
*/
// Fuction is from https://php.net/manual/en/function.inet-ntop.php
function inet6_ntop($ip)
{
$l = strlen($ip);
if ($l == 4 or $l == 16) {
return inet_ntop(pack('A' . $l, $ip));
}
2020-09-21 13:40:17 +00:00
return '';
}
2016-01-19 09:00:18 +00:00
/**
* If hostname is an ip, use return sysName
2021-09-10 18:09:53 +00:00
*
2021-09-08 21:35:56 +00:00
* @param array $device (uses hostname and sysName fields)
* @return string
*/
function format_hostname($device): string
{
$hostname = $device['hostname'] ?? 'invalid hostname';
$hostname_is_ip = IP::isValid($hostname);
$sysName = empty($device['sysName']) ? $hostname : $device['sysName'];
return \App\View\SimpleTemplate::parse(empty($device['display']) ? Config::get('device_display_default', '{{ $hostname }}') : $device['display'], [
'hostname' => $hostname,
'sysName' => $sysName,
'sysName_fallback' => $hostname_is_ip ? $sysName : $hostname,
'ip' => empty($device['overwrite_ip']) ? ($hostname_is_ip ? $device['hostname'] : $device['ip'] ?? '') : $device['overwrite_ip'],
]);
}
/**
* Query all ports of the given device (by ID) and build port array and
* port association maps for ifIndex, ifName, ifDescr. Query port stats
* if told to do so, too.
2021-09-10 18:09:53 +00:00
*
2021-09-08 21:35:56 +00:00
* @param int $device_id ID of device to query ports for
* @param bool $with_statistics Query port statistics, too. (optional, default false)
* @return array
*/
function get_ports_mapped($device_id, $with_statistics = false)
{
2016-01-30 10:05:46 +00:00
$ports = [];
2016-01-30 10:10:16 +00:00
$maps = [
2016-01-30 10:05:46 +00:00
'ifIndex' => [],
'ifName' => [],
'ifDescr' => [],
];
if ($with_statistics) {
/* ... including any related ports_statistics if requested */
$query = 'SELECT *, `ports_statistics`.`port_id` AS `ports_statistics_port_id`, `ports`.`port_id` AS `port_id` FROM `ports` LEFT OUTER JOIN `ports_statistics` ON `ports`.`port_id` = `ports_statistics`.`port_id` WHERE `ports`.`device_id` = ? ORDER BY ports.port_id';
} else {
/* Query all information available for ports for this device ... */
$query = 'SELECT * FROM `ports` WHERE `device_id` = ? ORDER BY port_id';
}
// Query known ports in order of discovery to make sure the latest
// discoverd/polled port is in the mapping tables.
foreach (dbFetchRows($query, [$device_id]) as $port) {
// Store port information by ports port_id from DB
$ports[$port['port_id']] = $port;
// Build maps from ifIndex, ifName, ifDescr to port_id
$maps['ifIndex'][$port['ifIndex']] = $port['port_id'];
$maps['ifName'][$port['ifName']] = $port['port_id'];
$maps['ifDescr'][$port['ifDescr']] = $port['port_id'];
}
2016-01-30 10:05:46 +00:00
return [
'ports' => $ports,
'maps' => $maps,
2016-01-30 10:05:46 +00:00
];
}
/**
* Calculate port_id of given port using given devices port information and port association mode
2021-09-10 18:09:53 +00:00
*
2021-09-08 21:35:56 +00:00
* @param array $ports_mapped Port information of device queried by get_ports_mapped()
* @param array $port Port information as fetched from DB
* @param string $port_association_mode Port association mode to use for mapping
* @return int port_id (or Null)
*/
function get_port_id($ports_mapped, $port, $port_association_mode)
{
// Get port_id according to port_association_mode used for this device
$port_id = null;
/*
* Information an all ports is available through $ports_mapped['ports']
* This might come in handy sometime in the future to add you nifty new
* port mapping schema:
*
* $ports = $ports_mapped['ports'];
*/
$maps = $ports_mapped['maps'];
if (in_array($port_association_mode, ['ifIndex', 'ifName', 'ifDescr', 'ifAlias'])) {
$port_id = $maps[$port_association_mode][$port[$port_association_mode]] ?? null;
}
return $port_id;
}
/**
* Create a glue-chain
2021-09-10 18:09:53 +00:00
*
2021-09-08 21:35:56 +00:00
* @param array $tables Initial Tables to construct glue-chain
* @param string $target Glue to find (usual device_id)
* @param int $x Recursion Anchor
* @param array $hist History of processed tables
* @param array $last Glues on the fringe
* @return array|false
*/
function ResolveGlues($tables, $target, $x = 0, $hist = [], $last = [])
{
if (sizeof($tables) == 1 && $x != 0) {
if (dbFetchCell('SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_NAME = ? && COLUMN_NAME = ?', [$tables[0], $target]) == 1) {
return array_merge($last, [$tables[0] . '.' . $target]);
} else {
return false;
}
} else {
$x++;
if ($x > 30) {
//Too much recursion. Abort.
return false;
}
foreach ($tables as $table) {
if ($table == 'state_translations' && ($target == 'device_id' || $target == 'sensor_id')) {
// workaround for state_translations
return array_merge($last, [
'state_translations.state_index_id',
'sensors_to_state_indexes.sensor_id',
"sensors.$target",
]);
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
} elseif ($table == 'application_metrics' && $target == 'device_id') {
return array_merge($last, [
'application_metrics.app_id',
"applications.$target",
]);
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
} elseif ($table == 'locations' && $target == 'device_id') {
return array_merge($last, [
'locations.id',
'devices.device_id.location_id',
]);
}
$glues = dbFetchRows('SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = ? && COLUMN_NAME LIKE "%\_id"', [$table]);
if (sizeof($glues) == 1 && $glues[0]['COLUMN_NAME'] != $target) {
//Search for new candidates to expand
$ntables = [];
[$tmp] = explode('_', $glues[0]['COLUMN_NAME'], 2);
$ntables[] = $tmp;
$ntables[] = $tmp . 's';
$tmp = dbFetchRows('SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE "' . substr($table, 0, -1) . '_%" && TABLE_NAME != "' . $table . '"');
foreach ($tmp as $expand) {
$ntables[] = $expand['TABLE_NAME'];
}
$tmp = ResolveGlues($ntables, $target, $x++, array_merge($tables, $ntables), array_merge($last, [$table . '.' . $glues[0]['COLUMN_NAME']]));
if (is_array($tmp)) {
return $tmp;
}
} else {
foreach ($glues as $glue) {
if ($glue['COLUMN_NAME'] == $target) {
return array_merge($last, [$table . '.' . $target]);
} else {
[$tmp] = explode('_', $glue['COLUMN_NAME']);
$tmp .= 's';
if (! in_array($tmp, $tables) && ! in_array($tmp, $hist)) {
//Expand table
$tmp = ResolveGlues([$tmp], $target, $x++, array_merge($tables, [$tmp]), array_merge($last, [$table . '.' . $glue['COLUMN_NAME']]));
if (is_array($tmp)) {
return $tmp;
}
}
}
}
}
}
}
//You should never get here.
return false;
}
/**
* Determine if a given string contains a given substring.
*
2021-09-08 21:35:56 +00:00
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function str_i_contains($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) !== false) {
return true;
}
}
2020-09-21 13:40:17 +00:00
return false;
}
/**
* Get alert_rules sql filter by minimal severity
*
2021-09-08 21:35:56 +00:00
* @param string|int $min_severity
* @param string $alert_rules_name
* @return string
*/
function get_sql_filter_min_severity($min_severity, $alert_rules_name)
{
$alert_severities = [
// alert_rules.status is enum('ok','warning','critical')
'ok' => 1,
'warning' => 2,
'critical' => 3,
'ok only' => 4,
'warning only' => 5,
'critical only' => 6,
];
if (is_numeric($min_severity)) {
$min_severity_id = $min_severity;
} elseif (! empty($min_severity)) {
$min_severity_id = $alert_severities[$min_severity];
}
if (isset($min_severity_id)) {
return " AND `$alert_rules_name`.`severity` " . ($min_severity_id > 3 ? '' : '>') . '= ' . ($min_severity_id > 3 ? $min_severity_id - 3 : $min_severity_id);
}
2020-09-21 13:40:17 +00:00
return '';
}
/**
* Converts fahrenheit to celsius (with 2 decimal places)
* if $scale is not fahrenheit, it assumes celsius and returns the value
*
2021-09-08 21:35:56 +00:00
* @param float $value
* @param string $scale fahrenheit or celsius
* @return string (containing a float)
*/
function fahrenheit_to_celsius($value, $scale = 'fahrenheit')
{
if ($scale === 'fahrenheit') {
$value = ($value - 32) / 1.8;
}
2020-09-21 13:40:17 +00:00
return sprintf('%.02f', $value);
}
/**
* Converts celsius to fahrenheit (with 2 decimal places)
* if $scale is not celsius, it assumes celsius and returns the value
*
2021-09-08 21:35:56 +00:00
* @param float $value
* @param string $scale fahrenheit or celsius
* @return string (containing a float)
*/
function celsius_to_fahrenheit($value, $scale = 'celsius')
{
if ($scale === 'celsius') {
$value = ($value * 1.8) + 32;
}
2020-09-21 13:40:17 +00:00
return sprintf('%.02f', $value);
}
/**
* Converts string to float
*/
function string_to_float($value)
{
return sprintf('%.02f', $value);
}
/**
* Converts uW to dBm
* $value must be positive
*/
function uw_to_dbm($value)
{
return $value == 0 ? -60 : 10 * log10($value / 1000);
}
/**
* Converts mW to dBm
* $value must be positive
*/
function mw_to_dbm($value)
{
return $value == 0 ? -60 : 10 * log10($value);
}
/**
* @param $value
2021-09-08 21:35:56 +00:00
* @param null $default
* @param int $min
* @return null
*/
function set_null($value, $default = null, $min = null)
{
if (! is_numeric($value)) {
return $default;
} elseif (is_nan($value)) {
return $default;
} elseif (is_infinite($value)) {
return $default;
} elseif (isset($min) && $value < $min) {
return $default;
}
2020-09-21 13:40:17 +00:00
return $value;
}
/*
* @param $value
* @param int $default
* @return int
*/
function set_numeric($value, $default = 0)
{
if (! is_numeric($value) ||
is_nan($value) ||
is_infinite($value)
) {
$value = $default;
}
2020-09-21 13:40:17 +00:00
return $value;
}
function get_vm_parent_id($device)
{
if (empty($device['hostname'])) {
return false;
}
return dbFetchCell('SELECT `device_id` FROM `vminfo` WHERE `vmwVmDisplayName` = ? OR `vmwVmDisplayName` = ?', [$device['hostname'], $device['hostname'] . '.' . Config::get('mydomain')]);
}
feature: Wireless Sensors Overhaul (#6471) * feature: Wireless Sensors Includes client counts for ios and unifi Graphing could use some improvement. Alerting and threshold ui not implemented WIP: starting OO based wireless sensors. Class based functionality working remove old functional files add schema file discovery needs to be enabled, not polling fix up schema fix Unifi discovery not returning an array Add some debug when discovering a sensor. Fix style. Add missing semicolin Add a null object (Generic) for OS. Fill out some phpdocs Re-organized code Each sensor type now has it's own discovery and polling interface Custom polling tested with Unifi CCQ Left to do: Implement UI (Graphs and Custom thresholds) Alerting Testing Fix event message text Remove runDiscovery and runPolling from OS, they are unused and don't belong there. Cleanups/docs Missed this file. Remove the requirement to fetch the current value to check validity. Do that automatically if current is not specified A few cleanups here and there First pass at graphing. device_ and wireless_ graphs added. Add RouterOS support Singleton OS instance isn't required right now. Remove that to allow some memory to be freed. Add wireless to the device list metrics. Make all metrics clickable Tweak graphs a bit Implement limit configuration page. Use sensors page as common code instead of duplicating. Clean up some javascript interactions: Allow enter on values to save. Cancel if update is not needed. Enable the clear custom button after setting a custom value. Add some wireless alert rules to the library. Add documentation. Add unifi client counts by ssid in addition to radio. Optimize Sensor polling a bit. Add HP MSM clients support (for full controller) Fix function accessibility Formalize the discovery and poller interfaces. Add Xirrus clients and noise floor move module interfaces to a more appropriate place. push caching code up to os, unsure about this do to the limitations No point in selectively enabling wireless discovery. We only discover if the device supports something. Add RSSI, Power, and Rate. Add these sensors for Ubnt Airos. Clean up some copyrights. Reduce the amount of files need to add new types. Leave graph files for consistency and to allow customization. Remove the old wifi clients graph completely. ciscowlc should have improved counts (total and per-ssid) Schema didn't get added. Impelement the rest of the AirOS sensors Reformat and re-organize the Airos.php class. Add several UBNT AirFiber sensors A few fixes add links to the section headers Add HP MSM mibs. * Schema file got dropped in rebase. * Add wireless menu to view sensors across all devices. Icons in the menu need help :/ * Add HeliOS, Mimosa, and Siklu support Sensors added SNR + Noise * Add power and utilization to Unifi * Update polling to prefetch all sensor data in a few snmp requests as possible * Add Extendair: tx+rx power, aggregate rate, frequency * Add a check for duplicate sensors in discovery. Just print an error for now. * Add Bit Error Ratio (named error-ratio to allow for bit error rate to be added if needed) Fix an incorrect link in the wireless sensors table * Add error rate and change all bps and Hz to use si units * Fixes to limits and frequency display * Fix overview graph frequency display A few decimal place tweaks * Don't allow switching sensor and wireless-sensor graphs, it doesn't work. Change individual distance graphs to use si units * Go through the OS and make sure I got all the sensors I can (probably missed some still) Because pollWirelessChannelAsFrequency() is generic and a little complex, so pull it up to OS. Message to help developers adding supports that don't return an array from discover functions. * Fix some issues * Remove noise and signal for now at least A couple more fixes Add a notification * Oopsie * Bonus AirFiber sensors
2017-05-02 04:49:11 +00:00
/**
* Generate a class name from a lowercase string containing - or _
* Remove - and _ and camel case words
*
2021-09-08 21:35:56 +00:00
* @param string $name The string to convert to a class name
* @param string $namespace namespace to prepend to the name for example: LibreNMS\
* @return string Class name
feature: Wireless Sensors Overhaul (#6471) * feature: Wireless Sensors Includes client counts for ios and unifi Graphing could use some improvement. Alerting and threshold ui not implemented WIP: starting OO based wireless sensors. Class based functionality working remove old functional files add schema file discovery needs to be enabled, not polling fix up schema fix Unifi discovery not returning an array Add some debug when discovering a sensor. Fix style. Add missing semicolin Add a null object (Generic) for OS. Fill out some phpdocs Re-organized code Each sensor type now has it's own discovery and polling interface Custom polling tested with Unifi CCQ Left to do: Implement UI (Graphs and Custom thresholds) Alerting Testing Fix event message text Remove runDiscovery and runPolling from OS, they are unused and don't belong there. Cleanups/docs Missed this file. Remove the requirement to fetch the current value to check validity. Do that automatically if current is not specified A few cleanups here and there First pass at graphing. device_ and wireless_ graphs added. Add RouterOS support Singleton OS instance isn't required right now. Remove that to allow some memory to be freed. Add wireless to the device list metrics. Make all metrics clickable Tweak graphs a bit Implement limit configuration page. Use sensors page as common code instead of duplicating. Clean up some javascript interactions: Allow enter on values to save. Cancel if update is not needed. Enable the clear custom button after setting a custom value. Add some wireless alert rules to the library. Add documentation. Add unifi client counts by ssid in addition to radio. Optimize Sensor polling a bit. Add HP MSM clients support (for full controller) Fix function accessibility Formalize the discovery and poller interfaces. Add Xirrus clients and noise floor move module interfaces to a more appropriate place. push caching code up to os, unsure about this do to the limitations No point in selectively enabling wireless discovery. We only discover if the device supports something. Add RSSI, Power, and Rate. Add these sensors for Ubnt Airos. Clean up some copyrights. Reduce the amount of files need to add new types. Leave graph files for consistency and to allow customization. Remove the old wifi clients graph completely. ciscowlc should have improved counts (total and per-ssid) Schema didn't get added. Impelement the rest of the AirOS sensors Reformat and re-organize the Airos.php class. Add several UBNT AirFiber sensors A few fixes add links to the section headers Add HP MSM mibs. * Schema file got dropped in rebase. * Add wireless menu to view sensors across all devices. Icons in the menu need help :/ * Add HeliOS, Mimosa, and Siklu support Sensors added SNR + Noise * Add power and utilization to Unifi * Update polling to prefetch all sensor data in a few snmp requests as possible * Add Extendair: tx+rx power, aggregate rate, frequency * Add a check for duplicate sensors in discovery. Just print an error for now. * Add Bit Error Ratio (named error-ratio to allow for bit error rate to be added if needed) Fix an incorrect link in the wireless sensors table * Add error rate and change all bps and Hz to use si units * Fixes to limits and frequency display * Fix overview graph frequency display A few decimal place tweaks * Don't allow switching sensor and wireless-sensor graphs, it doesn't work. Change individual distance graphs to use si units * Go through the OS and make sure I got all the sensors I can (probably missed some still) Because pollWirelessChannelAsFrequency() is generic and a little complex, so pull it up to OS. Message to help developers adding supports that don't return an array from discover functions. * Fix some issues * Remove noise and signal for now at least A couple more fixes Add a notification * Oopsie * Bonus AirFiber sensors
2017-05-02 04:49:11 +00:00
*/
function str_to_class($name, $namespace = null)
{
return \LibreNMS\Util\StringHelpers::toClass($name, $namespace);
feature: Wireless Sensors Overhaul (#6471) * feature: Wireless Sensors Includes client counts for ios and unifi Graphing could use some improvement. Alerting and threshold ui not implemented WIP: starting OO based wireless sensors. Class based functionality working remove old functional files add schema file discovery needs to be enabled, not polling fix up schema fix Unifi discovery not returning an array Add some debug when discovering a sensor. Fix style. Add missing semicolin Add a null object (Generic) for OS. Fill out some phpdocs Re-organized code Each sensor type now has it's own discovery and polling interface Custom polling tested with Unifi CCQ Left to do: Implement UI (Graphs and Custom thresholds) Alerting Testing Fix event message text Remove runDiscovery and runPolling from OS, they are unused and don't belong there. Cleanups/docs Missed this file. Remove the requirement to fetch the current value to check validity. Do that automatically if current is not specified A few cleanups here and there First pass at graphing. device_ and wireless_ graphs added. Add RouterOS support Singleton OS instance isn't required right now. Remove that to allow some memory to be freed. Add wireless to the device list metrics. Make all metrics clickable Tweak graphs a bit Implement limit configuration page. Use sensors page as common code instead of duplicating. Clean up some javascript interactions: Allow enter on values to save. Cancel if update is not needed. Enable the clear custom button after setting a custom value. Add some wireless alert rules to the library. Add documentation. Add unifi client counts by ssid in addition to radio. Optimize Sensor polling a bit. Add HP MSM clients support (for full controller) Fix function accessibility Formalize the discovery and poller interfaces. Add Xirrus clients and noise floor move module interfaces to a more appropriate place. push caching code up to os, unsure about this do to the limitations No point in selectively enabling wireless discovery. We only discover if the device supports something. Add RSSI, Power, and Rate. Add these sensors for Ubnt Airos. Clean up some copyrights. Reduce the amount of files need to add new types. Leave graph files for consistency and to allow customization. Remove the old wifi clients graph completely. ciscowlc should have improved counts (total and per-ssid) Schema didn't get added. Impelement the rest of the AirOS sensors Reformat and re-organize the Airos.php class. Add several UBNT AirFiber sensors A few fixes add links to the section headers Add HP MSM mibs. * Schema file got dropped in rebase. * Add wireless menu to view sensors across all devices. Icons in the menu need help :/ * Add HeliOS, Mimosa, and Siklu support Sensors added SNR + Noise * Add power and utilization to Unifi * Update polling to prefetch all sensor data in a few snmp requests as possible * Add Extendair: tx+rx power, aggregate rate, frequency * Add a check for duplicate sensors in discovery. Just print an error for now. * Add Bit Error Ratio (named error-ratio to allow for bit error rate to be added if needed) Fix an incorrect link in the wireless sensors table * Add error rate and change all bps and Hz to use si units * Fixes to limits and frequency display * Fix overview graph frequency display A few decimal place tweaks * Don't allow switching sensor and wireless-sensor graphs, it doesn't work. Change individual distance graphs to use si units * Go through the OS and make sure I got all the sensors I can (probably missed some still) Because pollWirelessChannelAsFrequency() is generic and a little complex, so pull it up to OS. Message to help developers adding supports that don't return an array from discover functions. * Fix some issues * Remove noise and signal for now at least A couple more fixes Add a notification * Oopsie * Bonus AirFiber sensors
2017-05-02 04:49:11 +00:00
}
/**
* Index an array by a column
*
2021-09-08 21:35:56 +00:00
* @param array $array
* @param string|int $column
* @return array
*/
function array_by_column($array, $column)
{
return array_combine(array_column($array, $column), $array);
}