diff --git a/LibreNMS/Device/Sensor.php b/LibreNMS/Device/Sensor.php index 1a7fc062be..acefc0e40a 100644 --- a/LibreNMS/Device/Sensor.php +++ b/LibreNMS/Device/Sensor.php @@ -30,6 +30,7 @@ use LibreNMS\Interfaces\Discovery\DiscoveryModule; use LibreNMS\Interfaces\Polling\PollerModule; use LibreNMS\OS; use LibreNMS\RRD\RrdDefinition; +use LibreNMS\Util\StringHelpers; class Sensor implements DiscoveryModule, PollerModule { @@ -510,22 +511,22 @@ class Sensor implements DiscoveryModule, PollerModule protected static function getDiscoveryInterface($type) { - return str_to_class($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\') . 'Discovery'; + return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\') . 'Discovery'; } protected static function getDiscoveryMethod($type) { - return 'discover' . str_to_class($type); + return 'discover' . StringHelpers::toClass($type, null); } protected static function getPollingInterface($type) { - return str_to_class($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\') . 'Polling'; + return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\') . 'Polling'; } protected static function getPollingMethod($type) { - return 'poll' . str_to_class($type); + return 'poll' . StringHelpers::toClass($type, null); } /** diff --git a/LibreNMS/Device/WirelessSensor.php b/LibreNMS/Device/WirelessSensor.php index 6471bc0c01..8a085507cc 100644 --- a/LibreNMS/Device/WirelessSensor.php +++ b/LibreNMS/Device/WirelessSensor.php @@ -27,6 +27,7 @@ namespace LibreNMS\Device; use LibreNMS\Config; use LibreNMS\OS; +use LibreNMS\Util\StringHelpers; class WirelessSensor extends Sensor { @@ -229,22 +230,22 @@ class WirelessSensor extends Sensor protected static function getDiscoveryInterface($type) { - return str_to_class($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\Wireless') . 'Discovery'; + return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\Wireless') . 'Discovery'; } protected static function getDiscoveryMethod($type) { - return 'discoverWireless' . str_to_class($type); + return 'discoverWireless' . StringHelpers::toClass($type, null); } protected static function getPollingInterface($type) { - return str_to_class($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\Wireless') . 'Polling'; + return StringHelpers::toClass($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\Wireless') . 'Polling'; } protected static function getPollingMethod($type) { - return 'pollWireless' . str_to_class($type); + return 'pollWireless' . StringHelpers::toClass($type, null); } /** diff --git a/LibreNMS/IRCBot.php b/LibreNMS/IRCBot.php index 555299a83a..890ea5ebbe 100644 --- a/LibreNMS/IRCBot.php +++ b/LibreNMS/IRCBot.php @@ -27,6 +27,7 @@ use App\Models\Service; use App\Models\User; use LibreNMS\DB\Eloquent; use LibreNMS\Enum\AlertState; +use LibreNMS\Util\Mail; use LibreNMS\Util\Number; use LibreNMS\Util\Time; use LibreNMS\Util\Version; @@ -706,7 +707,7 @@ class IRCBot $this->log("Auth for '" . $params[0] . "', ID: '" . $user->user_id . "', Token: '" . $token . "', Mail: '" . $user->email . "'"); } - if (send_mail($user->email, 'LibreNMS IRC-Bot Authtoken', "Your Authtoken for the IRC-Bot:\r\n\r\n" . $token . "\r\n\r\n") === true) { + if (Mail::send($user->email, 'LibreNMS IRC-Bot Authtoken', "Your Authtoken for the IRC-Bot:\r\n\r\n" . $token . "\r\n\r\n", false) === true) { return $this->respond('Token sent!'); } else { return $this->respond('Sorry, seems like mail doesnt like us.'); diff --git a/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php b/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php index 9be1e50686..93e7ffe9d6 100644 --- a/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php +++ b/LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php @@ -29,6 +29,7 @@ use App\Models\Device; use LibreNMS\Enum\Severity; use LibreNMS\Interfaces\SnmptrapHandler; use LibreNMS\Snmptrap\Trap; +use LibreNMS\Util\AutonomousSystem; use Log; class BgpBackwardTransition implements SnmptrapHandler @@ -57,7 +58,7 @@ class BgpBackwardTransition implements SnmptrapHandler $bgpPeer->bgpPeerState = $trap->getOidData($state_oid); if ($bgpPeer->isDirty('bgpPeerState')) { - $trap->log('SNMP Trap: BGP Down ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, severity: Severity::Error, type: 'bgpPeer', + $trap->log('SNMP Trap: BGP Down ' . $bgpPeer->bgpPeerIdentifier . ' ' . AutonomousSystem::get($bgpPeer->bgpPeerRemoteAs)->name() . ' is now ' . $bgpPeer->bgpPeerState, severity: Severity::Error, type: 'bgpPeer', reference: $bgpPeerIp); } diff --git a/LibreNMS/Snmptrap/Handlers/BgpEstablished.php b/LibreNMS/Snmptrap/Handlers/BgpEstablished.php index 10343b3b7c..8fed24d757 100644 --- a/LibreNMS/Snmptrap/Handlers/BgpEstablished.php +++ b/LibreNMS/Snmptrap/Handlers/BgpEstablished.php @@ -29,6 +29,7 @@ use App\Models\Device; use LibreNMS\Enum\Severity; use LibreNMS\Interfaces\SnmptrapHandler; use LibreNMS\Snmptrap\Trap; +use LibreNMS\Util\AutonomousSystem; use Log; class BgpEstablished implements SnmptrapHandler @@ -57,7 +58,7 @@ class BgpEstablished implements SnmptrapHandler $bgpPeer->bgpPeerState = $trap->getOidData($state_oid); if ($bgpPeer->isDirty('bgpPeerState')) { - $trap->log('SNMP Trap: BGP Up ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, Severity::Ok, 'bgpPeer', $bgpPeerIp); + $trap->log('SNMP Trap: BGP Up ' . $bgpPeer->bgpPeerIdentifier . ' ' . AutonomousSystem::get($bgpPeer->bgpPeerRemoteAs)->name() . ' is now ' . $bgpPeer->bgpPeerState, Severity::Ok, 'bgpPeer', $bgpPeerIp); } $bgpPeer->save(); diff --git a/LibreNMS/Validations/Mail.php b/LibreNMS/Validations/Mail.php index 3c68ad482c..81eac15e4a 100644 --- a/LibreNMS/Validations/Mail.php +++ b/LibreNMS/Validations/Mail.php @@ -71,7 +71,7 @@ class Mail extends BaseValidation }//end if if ($run_test == 1) { $email = Config::get('alert.default_mail'); - if ($err = send_mail($email, 'Test email', 'Testing email from NMS')) { + if ($err = \LibreNMS\Util\Mail::send($email, 'Test email', 'Testing email from NMS', false)) { $validator->ok('Email has been sent'); } else { $validator->fail("Issue sending email to $email with error $err"); diff --git a/daily.php b/daily.php index 2ee8dd4451..5bc5fc2aac 100644 --- a/daily.php +++ b/daily.php @@ -342,11 +342,7 @@ if ($options['f'] === 'refresh_device_groups') { if ($options['f'] === 'notify') { if (\LibreNMS\Config::has('alert.default_mail')) { - send_mail( - \LibreNMS\Config::get('alert.default_mail'), - '[LibreNMS] Auto update has failed for ' . Config::get('distributed_poller_name'), - "We just attempted to update your install but failed. The information below should help you fix this.\r\n\r\n" . $options['o'] - ); + \LibreNMS\Util\Mail::send(\LibreNMS\Config::get('alert.default_mail'), '[LibreNMS] Auto update has failed for ' . Config::get('distributed_poller_name'), "We just attempted to update your install but failed. The information below should help you fix this.\r\n\r\n" . $options['o'], false); } } diff --git a/includes/common.php b/includes/common.php index d8000508ea..aada736a6e 100644 --- a/includes/common.php +++ b/includes/common.php @@ -168,21 +168,11 @@ 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'; - } -} - function get_entity_by_id_cache($type, $id) { global $entity_cache; - $table = table_from_entity_type($type); + $table = $type == 'storage' ? $type : $type . 's'; if (is_array($entity_cache[$type][$id])) { $entity = $entity_cache[$type][$id]; @@ -206,18 +196,6 @@ function get_port_by_id($port_id) } } -function get_sensor_by_id($sensor_id) -{ - if (is_numeric($sensor_id)) { - $sensor = dbFetchRow('SELECT * FROM `sensors` WHERE `sensor_id` = ?', [$sensor_id]); - if (is_array($sensor)) { - return $sensor; - } else { - return false; - } - } -} - function get_device_id_by_port_id($port_id) { if (is_numeric($port_id)) { @@ -297,26 +275,11 @@ function strgen($length = 16) 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; @@ -449,13 +412,6 @@ function get_graph_subtypes($type, $device = null) return $types; } // get_graph_subtypes -function get_smokeping_files($device) -{ - $smokeping = new \LibreNMS\Util\Smokeping(DeviceCache::get((int) $device['device_id'])); - - return $smokeping->findFiles(); -} - function generate_smokeping_file($device, $file = '') { $smokeping = new \LibreNMS\Util\Smokeping(DeviceCache::get((int) $device['device_id'])); @@ -489,26 +445,6 @@ function is_customoid_graph($type, $subtype) 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)) { - return $object_cache[$section][$obj]; - } else { - return false; - } -} // object_is_cached - function search_phrase_column($c) { global $searchPhrase; @@ -878,19 +814,6 @@ function get_vm_parent_id($device) return dbFetchCell('SELECT `device_id` FROM `vminfo` WHERE `vmwVmDisplayName` = ? OR `vmwVmDisplayName` = ?', [$device['hostname'], $device['hostname'] . '.' . Config::get('mydomain')]); } -/** - * Generate a class name from a lowercase string containing - or _ - * Remove - and _ and camel case words - * - * @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 - */ -function str_to_class($name, $namespace = null) -{ - return \LibreNMS\Util\StringHelpers::toClass($name, $namespace); -} - /** * Index an array by a column * diff --git a/includes/discovery/bgp-peers.inc.php b/includes/discovery/bgp-peers.inc.php index b8509fe822..44377dcd44 100644 --- a/includes/discovery/bgp-peers.inc.php +++ b/includes/discovery/bgp-peers.inc.php @@ -61,7 +61,7 @@ foreach (DeviceCache::getPrimary()->getVrfContexts() as $context_name) { $af_list = []; foreach ($peerlist as $peer) { - $peer['astext'] = get_astext($peer['as']); + $peer['astext'] = \LibreNMS\Util\AutonomousSystem::get($peer['as'])->name(); add_bgp_peer($device, $peer); diff --git a/includes/discovery/bgp-peers/dell-os10.inc.php b/includes/discovery/bgp-peers/dell-os10.inc.php index 6a45bd03b9..922bb3e56a 100644 --- a/includes/discovery/bgp-peers/dell-os10.inc.php +++ b/includes/discovery/bgp-peers/dell-os10.inc.php @@ -47,7 +47,7 @@ if ($device['os'] == 'dell-os10') { foreach ($peer as $address => $value) { // resolve AS number by DNS_TXT record - $astext = get_astext($value['os10bgp4V2PeerRemoteAs']); + $astext = \LibreNMS\Util\AutonomousSystem::get($value['os10bgp4V2PeerRemoteAs'])->name(); // FIXME - the `devices` table gets updated in the main bgp-peers.inc.php // Setting it here avoids the code that resets it to null if not found in BGP4-MIB. diff --git a/includes/discovery/bgp-peers/firebrick.inc.php b/includes/discovery/bgp-peers/firebrick.inc.php index 1df9af9953..c17eec4794 100644 --- a/includes/discovery/bgp-peers/firebrick.inc.php +++ b/includes/discovery/bgp-peers/firebrick.inc.php @@ -62,7 +62,7 @@ foreach ($bgpPeers as $vrfId => $vrf) { } foreach ($vrf as $address => $value) { $bgpLocalAs = $value['fbBgpPeerLocalAS'] ?? $bgpLocalAs; - $astext = get_astext($value['fbBgpPeerRemoteAS']); + $astext = \LibreNMS\Util\AutonomousSystem::get($value['fbBgpPeerRemoteAS'])->name(); if (! DeviceCache::getPrimary()->bgppeers()->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->exists()) { $peers = [ 'vrf_id' => $vrfId, diff --git a/includes/discovery/bgp-peers/timos.inc.php b/includes/discovery/bgp-peers/timos.inc.php index e112fed804..8472349586 100644 --- a/includes/discovery/bgp-peers/timos.inc.php +++ b/includes/discovery/bgp-peers/timos.inc.php @@ -44,7 +44,7 @@ if ($device['os'] == 'timos') { $vrfId = dbFetchCell('SELECT vrf_id from `vrfs` WHERE vrf_oid = ?', [$vrfOid]); d_echo($vrfId); foreach ($vrf as $address => $value) { - $astext = get_astext($value['tBgpPeerNgPeerAS4Byte']); + $astext = \LibreNMS\Util\AutonomousSystem::get($value['tBgpPeerNgPeerAS4Byte'])->name(); if (dbFetchCell('SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]) < '1') { $peers = [ diff --git a/includes/discovery/bgp-peers/vrp.inc.php b/includes/discovery/bgp-peers/vrp.inc.php index 5dc7d86951..b0d8bec4d3 100644 --- a/includes/discovery/bgp-peers/vrp.inc.php +++ b/includes/discovery/bgp-peers/vrp.inc.php @@ -83,7 +83,7 @@ if (count($bgpPeersCache) > 0 || count($bgpPeersCache_ietf) == 0) { $vrfId = $map_vrf['byName'][$vrfName]['vrf_id']; foreach ($vrf as $address => $value) { - $astext = get_astext($value['hwBgpPeerRemoteAs']); + $astext = \LibreNMS\Util\AutonomousSystem::get($value['hwBgpPeerRemoteAs'])->name(); if (! DeviceCache::getPrimary()->bgppeers()->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->exists()) { $peers = [ 'device_id' => $device['device_id'], diff --git a/includes/discovery/discovery-arp.inc.php b/includes/discovery/discovery-arp.inc.php index 04060fba47..ea0a778607 100644 --- a/includes/discovery/discovery-arp.inc.php +++ b/includes/discovery/discovery-arp.inc.php @@ -57,12 +57,12 @@ foreach (dbFetchRows($sql, [$deviceid]) as $entry) { } // Attempt discovery of each IP only once per run. - if (object_is_cached('arp_discovery', $ip)) { + if (Cache::get('arp_discovery:' . $ip)) { echo '.'; continue; } - object_add_cache('arp_discovery', $ip); + Cache::put('arp_discovery:' . $ip, true, 3600); $name = gethostbyaddr($ip); echo '+'; diff --git a/includes/functions.php b/includes/functions.php index 21e2ca67f0..c9b178585a 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -24,46 +24,6 @@ use LibreNMS\Exceptions\InvalidPortAssocModeException; use LibreNMS\Exceptions\SnmpVersionUnsupportedException; use LibreNMS\Modules\Core; -function array_sort_by_column($array, $on, $order = SORT_ASC) -{ - $new_array = []; - $sortable_array = []; - - if (count($array) > 0) { - foreach ($array as $k => $v) { - if (is_array($v)) { - foreach ($v as $k2 => $v2) { - if ($k2 == $on) { - $sortable_array[$k] = $v2; - } - } - } else { - $sortable_array[$k] = $v; - } - } - - switch ($order) { - case SORT_ASC: - asort($sortable_array); - break; - case SORT_DESC: - arsort($sortable_array); - break; - } - - foreach ($sortable_array as $k => $v) { - $new_array[$k] = $array[$k]; - } - } - - return $new_array; -} - -function only_alphanumeric($string) -{ - return preg_replace('/[^a-zA-Z0-9]/', '', $string); -} - /** * Parse cli discovery or poller modules and set config for this run * @@ -246,7 +206,7 @@ function addHost($host, $snmp_version = '', $port = 161, $transport = 'udp', $po } else { $ip = $host; } - if ($force_add !== true && $existing = device_has_ip($ip)) { + if ($force_add !== true && $existing = Device::findByIp($ip)) { throw new HostIpExistsException($host, $existing->hostname, $ip); } @@ -506,11 +466,6 @@ function snmp2ipv6($ipv6_snmp) return implode(':', $ipv6_2); } -function get_astext(string|int|null $asn): string -{ - return \LibreNMS\Util\AutonomousSystem::get($asn)->name(); -} - /** * Log events to the event table * @@ -530,17 +485,6 @@ function log_event($text, $device = null, $type = null, $severity = 2, $referenc \App\Models\Eventlog::log($text, $device, $type, Severity::tryFrom((int) $severity) ?? Severity::Info, $reference); } -// Parse string with emails. Return array with email (as key) and name (as value) -function parse_email($emails) -{ - return \LibreNMS\Util\Mail::parseEmails($emails); -} - -function send_mail($emails, $subject, $message, $html = false) -{ - return \LibreNMS\Util\Mail::send($emails, $subject, $message, $html); -} - function hex2str($hex) { $string = ''; @@ -564,28 +508,6 @@ function isHexString($str) return (bool) preg_match('/^[a-f0-9][a-f0-9]( [a-f0-9][a-f0-9])*$/is', trim($str)); } -// Include all .inc.php files in $dir -function include_dir($dir, $regex = '') -{ - global $device, $valid; - - if ($regex == '') { - $regex = "/\.inc\.php$/"; - } - - if ($handle = opendir(Config::get('install_dir') . '/' . $dir)) { - while (false !== ($file = readdir($handle))) { - if (filetype(Config::get('install_dir') . '/' . $dir . '/' . $file) == 'file' && preg_match($regex, $file)) { - d_echo('Including: ' . Config::get('install_dir') . '/' . $dir . '/' . $file . "\n"); - - include Config::get('install_dir') . '/' . $dir . '/' . $file; - } - } - - closedir($handle); - } -} - /** * Check if port is valid to poll. * Settings: empty_ifdescr, good_if, bad_if, bad_if_regexp, bad_ifname_regexp, bad_ifalias_regexp, bad_iftype, bad_ifoperstatus @@ -749,29 +671,6 @@ function normalize_snmp_ip_address($data) return preg_replace('/([0-9a-fA-F]{2}):([0-9a-fA-F]{2})/', '\1\2', explode('%', $data, 2)[0]); } -function guidv4($data) -{ - // http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid#15875555 - // From: Jack http://stackoverflow.com/users/1338292/ja%CD%A2ck - assert(strlen($data) == 16); - - $data[6] = chr(ord($data[6]) & 0x0F | 0x40); // set version to 0100 - $data[8] = chr(ord($data[8]) & 0x3F | 0x80); // set bits 6-7 to 10 - - return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); -} - -function target_to_id($target) -{ - if ($target[0] . $target[1] == 'g:') { - $target = 'g' . dbFetchCell('SELECT id FROM device_groups WHERE name = ?', [substr($target, 2)]); - } else { - $target = dbFetchCell('SELECT device_id FROM devices WHERE hostname = ?', [$target]); - } - - return $target; -} - function fix_integer_value($value) { if ($value < 0) { @@ -783,17 +682,6 @@ function fix_integer_value($value) return $return; } -/** - * Find a device that has this IP. Checks ipv4_addresses and ipv6_addresses tables. - * - * @param string $ip - * @return \App\Models\Device|false - */ -function device_has_ip($ip) -{ - return Device::findByIp($ip); -} - /** * Checks if the $hostname provided exists in the DB already * @@ -1122,24 +1010,6 @@ function getCIMCentPhysical($location, &$entphysical, &$index) } // end if - Level 1 } // end function -/* idea from https://php.net/manual/en/function.hex2bin.php comments */ -function hex2bin_compat($str) -{ - if (strlen($str) % 2 !== 0) { - trigger_error(__FUNCTION__ . '(): Hexadecimal input string must have an even length', E_USER_WARNING); - } - - return pack('H*', $str); -} - -if (! function_exists('hex2bin')) { - // This is only a hack - function hex2bin($str) - { - return hex2bin_compat($str); - } -} - function q_bridge_bits2indices($hex_data) { /* convert hex string to an array of 1-based indices of the nonzero bits @@ -1215,7 +1085,7 @@ function cache_peeringdb() $ix_data = json_decode($ix_json); $peers = $ix_data->{'data'}; foreach ($peers ?? [] as $index => $peer) { - $peer_name = get_astext($peer->{'asn'}); + $peer_name = \LibreNMS\Util\AutonomousSystem::get($peer->{'asn'})->name(); $tmp_peer = dbFetchRow('SELECT * FROM `pdb_ix_peers` WHERE `peer_id` = ? AND `ix_id` = ?', [$peer->{'id'}, $ixid]); if ($tmp_peer) { $peer_keep[] = $tmp_peer['pdb_ix_peers_id']; diff --git a/includes/html/graphs/device/smokeping_common.inc.php b/includes/html/graphs/device/smokeping_common.inc.php index ce9df8a4e4..1e11a4af36 100644 --- a/includes/html/graphs/device/smokeping_common.inc.php +++ b/includes/html/graphs/device/smokeping_common.inc.php @@ -1,3 +1,4 @@ findFiles(); diff --git a/includes/html/pages/device/loadbalancer/netscaler_vsvr.inc.php b/includes/html/pages/device/loadbalancer/netscaler_vsvr.inc.php index e9fef93d13..2039e9c223 100644 --- a/includes/html/pages/device/loadbalancer/netscaler_vsvr.inc.php +++ b/includes/html/pages/device/loadbalancer/netscaler_vsvr.inc.php @@ -138,12 +138,12 @@ if (is_numeric($vars['vsvr'])) { switch ($sort_key) { case 'vsvr_bps_in': case 'vsvr_bps_out': - $sort_direction = SORT_DESC; + $sort_descending = true; break; default: - $sort_direction = SORT_ASC; + $sort_descending = false; } - $vservers = array_sort_by_column($vservers, $sort_key, $sort_direction); + $vservers = collect($vservers)->sortBy($sort_key, descending: $sort_descending)->all(); $i = '0'; foreach ($vservers as $vsvr) { diff --git a/includes/html/pages/ports/graph.inc.php b/includes/html/pages/ports/graph.inc.php index 19569a1981..29691b7619 100644 --- a/includes/html/pages/ports/graph.inc.php +++ b/includes/html/pages/ports/graph.inc.php @@ -111,41 +111,41 @@ $ports = array_map(function ($value) { switch ($vars['sort'] ?? '') { case 'traffic': - $ports = array_sort_by_column($ports, 'ifOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifOctets_rate', descending: true); break; case 'traffic_in': - $ports = array_sort_by_column($ports, 'ifInOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifInOctets_rate', descending: true); break; case 'traffic_out': - $ports = array_sort_by_column($ports, 'ifOutOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifOutOctets_rate', descending: true); break; case 'packets': - $ports = array_sort_by_column($ports, 'ifUcastPkts_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifUcastPkts_rate', descending: true); break; case 'packets_in': - $ports = array_sort_by_column($ports, 'ifInUcastOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifInUcastOctets_rate', descending: true); break; case 'packets_out': - $ports = array_sort_by_column($ports, 'ifOutUcastOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifOutUcastOctets_rate', descending: true); break; case 'errors': - $ports = array_sort_by_column($ports, 'ifErrors_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifErrors_rate', descending: true); break; case 'speed': - $ports = array_sort_by_column($ports, 'ifSpeed', SORT_DESC); + $ports = collect($ports)->sortBy('ifSpeed', descending: true); break; case 'port': - $ports = array_sort_by_column($ports, 'ifDescr', SORT_ASC); + $ports = collect($ports)->sortBy('ifDescr'); break; case 'media': - $ports = array_sort_by_column($ports, 'ifType', SORT_ASC); + $ports = collect($ports)->sortBy('ifType'); break; case 'descr': - $ports = array_sort_by_column($ports, 'ifAlias', SORT_ASC); + $ports = collect($ports)->sortBy('ifAlias'); break; case 'device': default: - $ports = array_sort_by_column($ports, 'hostname', SORT_ASC); + $ports = collect($ports)->sortBy('hostname'); } foreach ($ports as $port) { diff --git a/includes/html/pages/routing/mpls-path-map.inc.php b/includes/html/pages/routing/mpls-path-map.inc.php index c8d76a1386..9fb2ecfc9f 100644 --- a/includes/html/pages/routing/mpls-path-map.inc.php +++ b/includes/html/pages/routing/mpls-path-map.inc.php @@ -26,20 +26,20 @@ $ar_list = dbFetchRows('SELECT * from `mpls_tunnel_ar_hops` where device_id = ? d_echo($ar_list); // first node is host self -$node = device_has_ip($ar_list[0]['mplsTunnelARHopRouterId']); +$node = \App\Models\Device::findByIp($ar_list[0]['mplsTunnelARHopRouterId']); if ($node) { - $node_id = $node['device_id']; - $label = $node['hostname']; + $node_id = $node->device_id; + $label = $node->displayName(); $first_node = $ar_list[0]['mplsTunnelARHopRouterId']; } else { $node_id = $label = $first_node; } foreach ($ar_list as $value) { - $node = device_has_ip($value['mplsTunnelARHopRouterId']); + $node = \App\Models\Device::findByIp($value['mplsTunnelARHopRouterId']); if ($node) { - $remote_node_id = $node['device_id']; - $remote_label = $node['hostname']; + $remote_node_id = $node->device_id; + $remote_label = $node->displayName(); } else { $remote_node_id = $remote_label = $value['mplsTunnelARHopRouterId']; } @@ -107,16 +107,16 @@ $filtered2 = $filtered->last()['mplsTunnelCHopListIndex']; $c_list = dbFetchRows('SELECT * from `mpls_tunnel_c_hops` where device_id = ? AND mplsTunnelCHopListIndex = ?', [$device_id, $filtered2]); // first node is host self -$node = device_has_ip($c_list[0]['mplsTunnelCHopRouterId']); +$node = \App\Models\Device::findByIp($c_list[0]['mplsTunnelCHopRouterId']); if ($node) { - $node_id = $node['device_id']; - $label = $node['hostname']; + $node_id = $node->device_id; + $label = $node->displayName(); } else { $node_id = $label = $c_list[0]['mplsTunnelCHopRouterId']; } foreach ($c_list as $value) { - $node = device_has_ip($value['mplsTunnelCHopRouterId']); + $node = \App\Models\Device::findByIp($value['mplsTunnelCHopRouterId']); if ($node) { $remote_node_id = $node['device_id']; $remote_label = $node['hostname']; diff --git a/includes/html/reports/ports.csv.inc.php b/includes/html/reports/ports.csv.inc.php index fa8de82902..42ac97cf70 100644 --- a/includes/html/reports/ports.csv.inc.php +++ b/includes/html/reports/ports.csv.inc.php @@ -106,52 +106,52 @@ $ports = dbFetchRows($query, $param); switch ($vars['sort']) { case 'traffic': - $ports = array_sort_by_column($ports, 'ifOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifOctets_rate', descending: true); break; case 'traffic_in': - $ports = array_sort_by_column($ports, 'ifInOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifInOctets_rate', descending: true); break; case 'traffic_out': - $ports = array_sort_by_column($ports, 'ifOutOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifOutOctets_rate', descending: true); break; case 'packets': - $ports = array_sort_by_column($ports, 'ifUcastPkts_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifUcastPkts_rate', descending: true); break; case 'packets_in': - $ports = array_sort_by_column($ports, 'ifInUcastOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifInUcastOctets_rate', descending: true); break; case 'packets_out': - $ports = array_sort_by_column($ports, 'ifOutUcastOctets_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifOutUcastOctets_rate', descending: true); break; case 'errors': - $ports = array_sort_by_column($ports, 'ifErrors_rate', SORT_DESC); + $ports = collect($ports)->sortBy('ifErrors_rate', descending: true); break; case 'speed': - $ports = array_sort_by_column($ports, 'ifSpeed', SORT_DESC); + $ports = collect($ports)->sortBy('ifSpeed', descending: true); break; case 'port': - $ports = array_sort_by_column($ports, 'ifDescr', SORT_ASC); + $ports = collect($ports)->sortBy('ifDescr'); break; case 'media': - $ports = array_sort_by_column($ports, 'ifType', SORT_ASC); + $ports = collect($ports)->sortBy('ifType'); break; case 'descr': - $ports = array_sort_by_column($ports, 'ifAlias', SORT_ASC); + $ports = collect($ports)->sortBy('ifAlias'); break; case 'device': default: - $ports = array_sort_by_column($ports, 'hostname', SORT_ASC); + $ports = collect($ports)->sortBy('hostname'); }//end switch $csv[] = [ diff --git a/includes/html/table/as-selection.inc.php b/includes/html/table/as-selection.inc.php index f824e4569d..8e558b8817 100644 --- a/includes/html/table/as-selection.inc.php +++ b/includes/html/table/as-selection.inc.php @@ -38,7 +38,7 @@ if ($rowCount != -1) { $sql = "SELECT `bgpLocalAs` $sql"; foreach (dbFetchRows($sql, $param) as $asn) { - $astext = get_astext($asn['bgpLocalAs']); + $astext = \LibreNMS\Util\AutonomousSystem::get($asn['bgpLocalAs'])->name(); $response[] = [ 'bgpLocalAs' => $asn['bgpLocalAs'], 'asname' => $astext, diff --git a/includes/polling/applications/bird2.inc.php b/includes/polling/applications/bird2.inc.php index 5bb4d9b9b0..1203ff668d 100644 --- a/includes/polling/applications/bird2.inc.php +++ b/includes/polling/applications/bird2.inc.php @@ -1,7 +1,6 @@ device_id = $device['device_id']; - $bgpPeer->astext = get_astext($protocol['neighbor_as']); + $bgpPeer->astext = \LibreNMS\Util\AutonomousSystem::get($protocol['neighbor_as'])->name(); $bgpPeer->bgpPeerIdentifier = $protocol['neighbor_id'] ?: '0.0.0.0'; $bgpPeer->bgpPeerRemoteAs = $protocol['neighbor_as']; $bgpPeer->bgpPeerState = strtolower($protocol['bgp_state']); diff --git a/tests/CommonFunctionsTest.php b/tests/CommonFunctionsTest.php index a27c1c4986..89bb12ab5e 100644 --- a/tests/CommonFunctionsTest.php +++ b/tests/CommonFunctionsTest.php @@ -29,6 +29,7 @@ use Illuminate\Support\Str; use LibreNMS\Config; use LibreNMS\Enum\PortAssociationMode; use LibreNMS\Util\Clean; +use LibreNMS\Util\StringHelpers; use LibreNMS\Util\Validate; class CommonFunctionsTest extends TestCase @@ -106,11 +107,11 @@ class CommonFunctionsTest extends TestCase public function testStringToClass(): void { - $this->assertSame('LibreNMS\OS\Os', str_to_class('OS', 'LibreNMS\\OS\\')); - $this->assertSame('SpacesName', str_to_class('spaces name')); - $this->assertSame('DashName', str_to_class('dash-name')); - $this->assertSame('UnderscoreName', str_to_class('underscore_name')); - $this->assertSame('LibreNMS\\AllOfThemName', str_to_class('all OF-thEm_NaMe', 'LibreNMS\\')); + $this->assertSame('LibreNMS\OS\Os', StringHelpers::toClass('OS', 'LibreNMS\\OS\\')); + $this->assertSame('SpacesName', StringHelpers::toClass('spaces name', null)); + $this->assertSame('DashName', StringHelpers::toClass('dash-name', null)); + $this->assertSame('UnderscoreName', StringHelpers::toClass('underscore_name', null)); + $this->assertSame('LibreNMS\\AllOfThemName', StringHelpers::toClass('all OF-thEm_NaMe', 'LibreNMS\\')); } public function testIsValidHostname(): void diff --git a/tests/Feature/SnmpTraps/BgpTrapTest.php b/tests/Feature/SnmpTraps/BgpTrapTest.php index 11388376b7..e428af0286 100644 --- a/tests/Feature/SnmpTraps/BgpTrapTest.php +++ b/tests/Feature/SnmpTraps/BgpTrapTest.php @@ -31,6 +31,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use LibreNMS\Config; use LibreNMS\Enum\Severity; use LibreNMS\Tests\Traits\RequiresDatabase; +use LibreNMS\Util\AutonomousSystem; class BgpTrapTest extends SnmpTrapTestCase { @@ -53,7 +54,7 @@ DISMAN-EVENT-MIB::sysUpTimeInstance 302:12:56:24.81 SNMPv2-MIB::snmpTrapOID.0 BGP4-MIB::bgpEstablished BGP4-MIB::bgpPeerLastError.$bgppeer->bgpPeerIdentifier \"04 00 \" BGP4-MIB::bgpPeerState.$bgppeer->bgpPeerIdentifier established\n", - "SNMP Trap: BGP Up $bgppeer->bgpPeerIdentifier " . get_astext($bgppeer->bgpPeerRemoteAs) . ' is now established', + "SNMP Trap: BGP Up $bgppeer->bgpPeerIdentifier " . AutonomousSystem::get($bgppeer->bgpPeerRemoteAs)->name() . ' is now established', 'Could not handle bgpEstablished', [Severity::Ok, 'bgpPeer', $bgppeer->bgpPeerIdentifier], $device, @@ -79,7 +80,7 @@ DISMAN-EVENT-MIB::sysUpTimeInstance 302:12:55:33.47 SNMPv2-MIB::snmpTrapOID.0 BGP4-MIB::bgpBackwardTransition BGP4-MIB::bgpPeerLastError.$bgppeer->bgpPeerIdentifier \"04 00 \" BGP4-MIB::bgpPeerState.$bgppeer->bgpPeerIdentifier idle\n", - "SNMP Trap: BGP Down $bgppeer->bgpPeerIdentifier " . get_astext($bgppeer->bgpPeerRemoteAs) . ' is now idle', + "SNMP Trap: BGP Down $bgppeer->bgpPeerIdentifier " . AutonomousSystem::get($bgppeer->bgpPeerRemoteAs)->name() . ' is now idle', 'Could not handle bgpBackwardTransition', [Severity::Error, 'bgpPeer', $bgppeer->bgpPeerIdentifier], $device,