Remove snmp functions that are barely used (#15377)

* Remove snmp functions that are barely used
Replace them with SnmpQuery calls

* Apply fixes from StyleCI

* Fix missing namespace

* Import Oid

* No more variables left may be repeated multiple times at the end with snmpsim, remove them all

* update data fixed by SnmpResponse bugfix

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
Tony Murray
2023-10-05 01:29:22 -05:00
committed by GitHub
parent e0444bffcf
commit be24993cbb
15 changed files with 101 additions and 229 deletions

View File

@@ -252,7 +252,7 @@ class SnmpResponse
{ {
return (string) preg_replace([ return (string) preg_replace([
'/^.*No Such Instance currently exists.*$/m', '/^.*No Such Instance currently exists.*$/m',
'/\n[^\r\n]+No more variables left[^\r\n]+$/s', '/(\n[^\r\n]+No more variables left[^\r\n]+)+$/',
], '', $this->raw); ], '', $this->raw);
} }

View File

@@ -93,7 +93,7 @@ class Processor extends Model implements DiscoveryModule, PollerModule, Discover
// handle string indexes // handle string indexes
if (Str::contains($oid, '"')) { if (Str::contains($oid, '"')) {
$oid = preg_replace_callback('/"([^"]+)"/', function ($matches) { $oid = preg_replace_callback('/"([^"]+)"/', function ($matches) {
return string_to_oid($matches[1]); return \LibreNMS\Util\Oid::ofString($matches[1]);
}, $oid); }, $oid);
} }
$proc->processor_oid = '.' . ltrim($oid, '.'); $proc->processor_oid = '.' . ltrim($oid, '.');

View File

@@ -39,47 +39,44 @@ class Foundry extends OS implements ProcessorDiscovery
*/ */
public function discoverProcessors() public function discoverProcessors()
{ {
$processors_data = snmpwalk_cache_triple_oid($this->getDeviceArray(), 'snAgentCpuUtilTable', [], 'FOUNDRY-SN-AGENT-MIB');
$module_descriptions = $this->getCacheByIndex('snAgentConfigModuleDescription', 'FOUNDRY-SN-AGENT-MIB'); $module_descriptions = $this->getCacheByIndex('snAgentConfigModuleDescription', 'FOUNDRY-SN-AGENT-MIB');
$processors = []; return \SnmpQuery::walk('FOUNDRY-SN-AGENT-MIB::snAgentCpuUtilTable')->mapTable(function ($entry, $slot, $cpu, $interval) use ($module_descriptions) {
foreach ($processors_data as $index => $entry) { // only discover 5min
// use the 5 minute readings if ($interval == 300) {
if ($entry['snAgentCpuUtilInterval'] != 300) { $module_description = '';
continue; if (isset($module_descriptions[$slot])) {
$module_description = $module_descriptions[$slot];
[$module_description] = explode(' ', $module_description);
}
$descr = "Slot $slot $module_description [$cpu]";
$index = "$slot.$cpu.$interval";
if (is_numeric($entry['FOUNDRY-SN-AGENT-MIB::snAgentCpuUtil100thPercent'])) {
return Processor::discover(
$this->getName(),
$this->getDeviceId(),
'.1.3.6.1.4.1.1991.1.1.2.11.1.1.6.' . $index,
$index,
$descr,
100,
$entry['FOUNDRY-SN-AGENT-MIB::snAgentCpuUtil100thPercent'] / 100
);
} elseif (is_numeric($entry['FOUNDRY-SN-AGENT-MIB::snAgentCpuUtilPercent'])) {
return Processor::discover(
$this->getName(),
$this->getDeviceId(),
'.1.3.6.1.4.1.1991.1.1.2.11.1.1.4.' . $index,
$index,
$descr,
1,
$entry['FOUNDRY-SN-AGENT-MIB::snAgentCpuUtilPercent']
);
}
} }
if (is_numeric($entry['snAgentCpuUtil100thPercent'])) { return null;
$usage_oid = '.1.3.6.1.4.1.1991.1.1.2.11.1.1.6.' . $index; })->filter()->values()->all();
$precision = 100;
$usage = $entry['snAgentCpuUtil100thPercent'] / $precision;
} elseif (is_numeric($entry['snAgentCpuUtilValue'])) {
$usage_oid = '.1.3.6.1.4.1.1991.1.1.2.11.1.1.4.' . $index;
$precision = 1;
$usage = $entry['snAgentCpuUtilValue'] / $precision;
} else {
continue;
}
$module_description = null;
if (isset($module_descriptions[$entry['snAgentCpuUtilSlotNum']])) {
$module_description = $module_descriptions[$entry['snAgentCpuUtilSlotNum']];
[$module_description] = explode(' ', $module_description);
}
$descr = "Slot {$entry['snAgentCpuUtilSlotNum']} $module_description [{$entry['snAgentCpuUtilSlotNum']}]";
$processors[] = Processor::discover(
$this->getName(),
$this->getDeviceId(),
$usage_oid,
$index,
$descr,
$precision,
$usage
);
}
return $processors;
} }
} }

View File

@@ -85,4 +85,17 @@ class Oid
return $numeric_oid; return $numeric_oid;
} }
/**
* Convert a string to an oid encoded string
*/
public static function ofString(string $string): string
{
$oid = strlen($string);
for ($i = 0; $i != strlen($string); $i++) {
$oid .= '.' . ord($string[$i]);
}
return $oid;
}
} }

View File

@@ -1,7 +1,6 @@
<?php <?php
$cefs = []; $cefs = SnmpQuery::hideMib()->walk('CISCO-CEF-MIB::cefSwitchingPath')->table(3);
$cefs = snmpwalk_cache_threepart_oid($device, 'CISCO-CEF-MIB::cefSwitchingPath', $cefs);
d_echo($cefs); d_echo($cefs);
if (is_array($cefs)) { if (is_array($cefs)) {

View File

@@ -13,7 +13,7 @@ if (! is_array($ns_sensor_array)) {
foreach ($ns_sensor_array as $descr => $data) { foreach ($ns_sensor_array as $descr => $data) {
$current = $data['sysHealthCounterValue']; $current = $data['sysHealthCounterValue'];
$oid = '.1.3.6.1.4.1.5951.4.1.1.41.7.1.2.' . string_to_oid($descr); $oid = '.1.3.6.1.4.1.5951.4.1.1.41.7.1.2.' . \LibreNMS\Util\Oid::ofString($descr);
$divisor = 1; $divisor = 1;
if (str_contains($descr, 'Temp')) { if (str_contains($descr, 'Temp')) {

View File

@@ -11,7 +11,7 @@ if (! \LibreNMS\Config::get('enable_bgp')) {
return; return;
} }
$birdOutput = snmp_get($device, 'nsExtendOutputFull.' . string_to_oid($name), '-Oqv', 'NET-SNMP-EXTEND-MIB'); $birdOutput = snmp_get($device, 'nsExtendOutputFull.' . \LibreNMS\Util\Oid::ofString($name), '-Oqv', 'NET-SNMP-EXTEND-MIB');
// make sure we actually get something back // make sure we actually get something back
if (empty($birdOutput)) { if (empty($birdOutput)) {

View File

@@ -15,25 +15,19 @@
use LibreNMS\RRD\RrdDefinition; use LibreNMS\RRD\RrdDefinition;
if ($device['os_group'] == 'cisco' && ($device['os'] == 'asa' || $device['os'] == 'ftd') && $device['type'] == 'firewall') { if ($device['os_group'] == 'cisco' && ($device['os'] == 'asa' || $device['os'] == 'ftd') && $device['type'] == 'firewall') {
$oid_list = 'cfwConnectionStatValue.protoIp.currentInUse'; $connections = SnmpQuery::get('CISCO-FIREWALL-MIB::cfwConnectionStatValue.protoIp.currentInUse')->value();
$temp_data = snmpwalk_cache_double_oid($device, $oid_list, [], 'CISCO-FIREWALL-MIB');
foreach ($temp_data as $oid => $result) {
$oid = substr(strchr($oid, '.'), 1);
$data[$oid]['data'] = $result['cfwConnectionStatValue'];
$asa_db = dbFetchCell('SELECT `ciscoASA_id` FROM `ciscoASA` WHERE `device_id` = ? AND `oid` = ?', [$device['device_id'], $oid]);
if (! is_numeric($asa_db)) {
$asa_db = dbInsert(['device_id' => $device['device_id'], 'oid' => $oid, 'data' => $result['cfwConnectionStatValue']], 'ciscoASA');
} else {
$asa_db = dbUpdate(['data' => $result['cfwConnectionStatValue']], 'ciscoASA', 'device_id=?', [$device['device_id']]);
}
$data[$oid]['db_id'] = $asa_db; if ($connections) {
} DB::table('ciscoASA')->updateOrInsert([
'device_id' => $device['device_id'],
'oid' => 'currentInUse',
], [
'data' => $connections,
]);
if ($data['currentInUse']) {
$rrd_def = RrdDefinition::make()->addDataset('connections', 'GAUGE', 0); $rrd_def = RrdDefinition::make()->addDataset('connections', 'GAUGE', 0);
$fields = [ $fields = [
'connections' => $data['currentInUse']['data'], 'connections' => $connections,
]; ];
$tags = compact('rrd_def'); $tags = compact('rrd_def');
@@ -43,5 +37,5 @@ if ($device['os_group'] == 'cisco' && ($device['os'] == 'asa' || $device['os'] =
echo ' ASA Connections'; echo ' ASA Connections';
} }
unset($data, $rrd_def); unset($connections, $data, $rrd_def, $fields, $tags);
}//end if }//end if

View File

@@ -3,8 +3,7 @@
use LibreNMS\RRD\RrdDefinition; use LibreNMS\RRD\RrdDefinition;
if ($device['os_group'] == 'cisco') { if ($device['os_group'] == 'cisco') {
$cefs = []; $cefs = SnmpQuery::hideMib()->walk('CISCO-CEF-MIB::cefSwitchingStatsTable')->table(3);
$cefs = snmpwalk_cache_threepart_oid($device, 'CISCO-CEF-MIB::cefSwitchingStatsEntry', $cefs, 'CISCO-CEF-MIB');
$polled = time(); $polled = time();
$cefs_query = dbFetchRows('SELECT * FROM `cef_switching` WHERE `device_id` = ?', [$device['device_id']]); $cefs_query = dbFetchRows('SELECT * FROM `cef_switching` WHERE `device_id` = ?', [$device['device_id']]);

View File

@@ -10,32 +10,29 @@ if ($device['os_group'] == 'cisco') {
'cipMacHCSwitchedBytes', 'cipMacHCSwitchedBytes',
'cipMacHCSwitchedPkts', 'cipMacHCSwitchedPkts',
]; ];
$cip_array = [];
foreach (array_merge($cip_oids, ['cipMacSwitchedBytes', 'cipMacSwitchedPkts']) as $oid) { $cip_response = SnmpQuery::walk([
echo "$oid "; 'CISCO-IP-STAT-MIB::cipMacHCSwitchedBytes',
$cip_array = snmpwalk_cache_cip($device, $oid, $cip_array, 'CISCO-IP-STAT-MIB'); 'CISCO-IP-STAT-MIB::cipMacHCSwitchedPkts',
]);
if (! $cip_response->isValid()) {
$cip_response = SnmpQuery::walk([
'CISCO-IP-STAT-MIB::cipMacSwitchedBytes',
'CISCO-IP-STAT-MIB::cipMacSwitchedPkts',
]);
} }
// Normalize cip_array // Normalize cip_array
$cip_array = array_map(function ($entries) { $cip_array = [];
return array_map(function ($entry) { foreach ($cip_response->table(3) as $ifIndex => $port_data) {
$new_entry = []; foreach ($port_data as $direction => $dir_data) {
foreach ($dir_data as $mac => $mac_data) {
foreach (['Bytes', 'Pkts'] as $unit) { $mac = \LibreNMS\Util\Rewrite::macToHex($mac);
$returned_oid = (array_key_exists('cipMacHCSwitched' . $unit, $entry)) ? 'cipMacHCSwitched' : 'cipMacSwitched'; $cip_array[$ifIndex][$mac]['cipMacHCSwitchedBytes'][$direction] = $mac_data['CISCO-IP-STAT-MIB::cipMacHCSwitchedBytes'] ?? $mac_data['CISCO-IP-STAT-MIB::cipMacSwitchedBytes'] ?? null;
$new_value = []; $cip_array[$ifIndex][$mac]['cipMacHCSwitchedPkts'][$direction] = $mac_data['CISCO-IP-STAT-MIB::cipMacHCSwitchedPkts'] ?? $mac_data['CISCO-IP-STAT-MIB::cipMacSwitchedPkts'] ?? null;
foreach ($entry[$returned_oid . $unit] as $key => $value) {
$new_value[$key] = intval($value);
}
$new_entry['cipMacHCSwitched' . $unit] = $new_value;
} }
}
return $new_entry; }
}, $entries);
}, $cip_array);
$polled = time(); $polled = time();
@@ -117,6 +114,7 @@ if ($device['os_group'] == 'cisco') {
unset( unset(
$cip_oids, $cip_oids,
$cip_response,
$oid, $oid,
$polled, $polled,
$mac_entries, $mac_entries,

View File

@@ -441,7 +441,7 @@ function update_application($app, $response, $metrics = [], $status = '')
*/ */
function json_app_get($device, $extend, $min_version = 1) function json_app_get($device, $extend, $min_version = 1)
{ {
$output = snmp_get($device, 'nsExtendOutputFull.' . string_to_oid($extend), '-Oqv', 'NET-SNMP-EXTEND-MIB'); $output = snmp_get($device, 'nsExtendOutputFull.' . \LibreNMS\Util\Oid::ofString($extend), '-Oqv', 'NET-SNMP-EXTEND-MIB');
// save for returning if not JSON // save for returning if not JSON
$orig_output = $output; $orig_output = $output;

View File

@@ -19,20 +19,9 @@ use App\Models\Device;
use App\Polling\Measure\Measurement; use App\Polling\Measure\Measurement;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Util\Debug;
use LibreNMS\Util\Oid; use LibreNMS\Util\Oid;
use Symfony\Component\Process\Exception\ProcessTimedOutException; use Symfony\Component\Process\Exception\ProcessTimedOutException;
function string_to_oid($string)
{
$oid = strlen($string);
for ($i = 0; $i != strlen($string); $i++) {
$oid .= '.' . ord($string[$i]);
}
return $oid;
}//end string_to_oid()
function prep_snmp_setting($device, $setting) function prep_snmp_setting($device, $setting)
{ {
if (isset($device[$setting]) && is_numeric($device[$setting]) && $device[$setting] > 0) { if (isset($device[$setting]) && is_numeric($device[$setting]) && $device[$setting] > 0) {
@@ -435,65 +424,6 @@ function snmp_walk($device, $oid, $options = null, $mib = null, $mibdir = null)
return $data; return $data;
}//end snmp_walk() }//end snmp_walk()
function snmpwalk_cache_cip($device, $oid, $array = [], $mib = 0)
{
$cmd = gen_snmpwalk_cmd($device, $oid, '-OsnQ', $mib);
$data = trim(external_exec($cmd));
if (empty($data)) {
return $array;
}
// echo("Caching: $oid\n");
foreach (explode("\n", $data) as $entry) {
[$this_oid, $this_value] = preg_split('/=/', $entry);
$this_oid = trim($this_oid);
$this_value = trim($this_value);
$this_oid = substr($this_oid, 30);
[$ifIndex, $dir, $a, $b, $c, $d, $e, $f] = explode('.', $this_oid);
$h_a = zeropad(dechex($a));
$h_b = zeropad(dechex($b));
$h_c = zeropad(dechex($c));
$h_d = zeropad(dechex($d));
$h_e = zeropad(dechex($e));
$h_f = zeropad(dechex($f));
$mac = "$h_a$h_b$h_c$h_d$h_e$h_f";
if ($dir == '1') {
$dir = 'input';
} elseif ($dir == '2') {
$dir = 'output';
}
if ($mac && $dir) {
$array[$ifIndex][$mac][$oid][$dir] = $this_value;
}
}//end foreach
return $array;
}//end snmpwalk_cache_cip()
function snmp_cache_ifIndex($device)
{
// FIXME: this is not yet using our own snmp_*
$cmd = gen_snmpwalk_cmd($device, 'ifIndex', '-OQs', 'IF-MIB');
$data = trim(external_exec($cmd));
$array = [];
foreach (explode("\n", $data) as $entry) {
[$this_oid, $this_value] = preg_split('/=/', $entry);
[$this_oid, $this_index] = explode('.', $this_oid, 2);
$this_index = trim($this_index);
$this_oid = trim($this_oid);
$this_value = trim($this_value);
if (! strstr($this_value, 'at this OID') && $this_index) {
$array[] = $this_value;
}
}
return $array;
}//end snmp_cache_ifIndex()
function snmpwalk_cache_oid($device, $oid, $array = [], $mib = null, $mibdir = null, $snmpflags = '-OQUs') function snmpwalk_cache_oid($device, $oid, $array = [], $mib = null, $mibdir = null, $snmpflags = '-OQUs')
{ {
$data = snmp_walk($device, $oid, $snmpflags, $mib, $mibdir); $data = snmp_walk($device, $oid, $snmpflags, $mib, $mibdir);
@@ -626,28 +556,6 @@ function snmpwalk_cache_multi_oid($device, $oid, $array = [], $mib = null, $mibd
return $cache['snmp'][$device['device_id']][$oid]; return $cache['snmp'][$device['device_id']][$oid];
}//end snmpwalk_cache_multi_oid() }//end snmpwalk_cache_multi_oid()
function snmpwalk_cache_double_oid($device, $oid, $array = [], $mib = null, $mibdir = null)
{
$data = snmp_walk($device, $oid, '-OQUs', $mib, $mibdir);
if (empty($data)) {
return $array;
}
foreach (explode("\n", $data) as $entry) {
[$oid,$value] = explode('=', $entry, 2);
$oid = trim($oid);
$value = trim($value);
[$oid, $first, $second] = explode('.', $oid);
if (! strstr($value, 'at this OID') && isset($oid) && isset($first) && isset($second)) {
$double = $first . '.' . $second;
$array[$double][$oid] = $value;
}
}
return $array;
}//end snmpwalk_cache_double_oid()
function snmpwalk_cache_index($device, $oid, $array = [], $mib = null, $mibdir = null) function snmpwalk_cache_index($device, $oid, $array = [], $mib = null, $mibdir = null)
{ {
$data = snmp_walk($device, $oid, '-OQUs', $mib, $mibdir); $data = snmp_walk($device, $oid, '-OQUs', $mib, $mibdir);
@@ -669,28 +577,6 @@ function snmpwalk_cache_index($device, $oid, $array = [], $mib = null, $mibdir =
return $array; return $array;
}//end snmpwalk_cache_double_oid() }//end snmpwalk_cache_double_oid()
function snmpwalk_cache_triple_oid($device, $oid, $array = [], $mib = null, $mibdir = null)
{
$data = snmp_walk($device, $oid, '-OQUs', $mib, $mibdir);
if (empty($data)) {
return $array;
}
foreach (explode("\n", $data) as $entry) {
[$oid,$value] = explode('=', $entry, 2);
$oid = trim($oid);
$value = trim($value);
[$oid, $first, $second, $third] = explode('.', $oid);
if (! strstr($value, 'at this OID') && isset($oid) && isset($first) && isset($second)) {
$index = $first . '.' . $second . '.' . $third;
$array[$index][$oid] = $value;
}
}
return $array;
}//end snmpwalk_cache_triple_oid()
/** /**
* Walk an snmp mib oid and group items together based on the index. * Walk an snmp mib oid and group items together based on the index.
* This is intended to be used with a string based oid. * This is intended to be used with a string based oid.
@@ -784,34 +670,6 @@ function snmpwalk_cache_twopart_oid($device, $oid, $array = [], $mib = 0, $mibdi
return $array; return $array;
}//end snmpwalk_cache_twopart_oid() }//end snmpwalk_cache_twopart_oid()
function snmpwalk_cache_threepart_oid($device, $oid, $array = [], $mib = 0)
{
$cmd = gen_snmpwalk_cmd($device, $oid, '-OQUs', $mib);
$data = trim(external_exec($cmd));
if (empty($data)) {
return $array;
}
foreach (explode("\n", $data) as $entry) {
[$oid,$value] = explode('=', $entry, 2);
$oid = trim($oid);
$value = trim($value);
$value = str_replace('"', '', $value);
[$oid, $first, $second, $third] = explode('.', $oid);
if (Debug::isEnabled()) {
echo "$entry || $oid || $first || $second || $third\n";
}
if (! strstr($value, 'at this OID') && isset($oid) && isset($first) && isset($second) && isset($third)) {
$array[$first][$second][$third][$oid] = $value;
}
}
return $array;
}//end snmpwalk_cache_threepart_oid()
/** /**
* generate snmp auth arguments * generate snmp auth arguments
* *

View File

@@ -147,7 +147,7 @@ if (! isset($options['S'])) {
// Output snmprec data for snmpsim for use with testing. // Output snmprec data for snmpsim for use with testing.
if (isset($options['s'])) { if (isset($options['s'])) {
$oid = string_to_oid($options['S']); $oid = \LibreNMS\Util\Oid::ofString($options['S']);
echo "1.3.6.1.2.1.1.1.0|4|Linux server 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64\n" . echo "1.3.6.1.2.1.1.1.0|4|Linux server 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64\n" .
"1.3.6.1.2.1.1.2.0|6|1.3.6.1.4.1.8072.3.2.10\n" . "1.3.6.1.2.1.1.2.0|6|1.3.6.1.4.1.8072.3.2.10\n" .
"1.3.6.1.2.1.1.3.0|67|77550514\n" . "1.3.6.1.2.1.1.3.0|67|77550514\n" .

View File

@@ -7210,6 +7210,20 @@
"designatedPort": 14, "designatedPort": 14,
"forwardTransitions": 1, "forwardTransitions": 1,
"ifIndex": 10624 "ifIndex": 10624
},
{
"vlan": 98,
"port_index": 80,
"priority": 128,
"state": "forwarding",
"enable": "enabled",
"pathCost": 4,
"designatedRoot": "0c85255ce500",
"designatedCost": 0,
"designatedBridge": "0c85255ce500",
"designatedPort": 80,
"forwardTransitions": 1,
"ifIndex": 10624
} }
] ]
}, },

View File

@@ -22051,7 +22051,7 @@
"processor_index": "2.1.300", "processor_index": "2.1.300",
"processor_type": "ironware", "processor_type": "ironware",
"processor_usage": 1, "processor_usage": 1,
"processor_descr": "Slot 2 4x10G [2]", "processor_descr": "Slot 2 4x10G [1]",
"processor_precision": 100, "processor_precision": 100,
"processor_perc_warn": 75 "processor_perc_warn": 75
}, },
@@ -22062,7 +22062,7 @@
"processor_index": "3.1.300", "processor_index": "3.1.300",
"processor_type": "ironware", "processor_type": "ironware",
"processor_usage": 1, "processor_usage": 1,
"processor_descr": "Slot 3 [3]", "processor_descr": "Slot 3 [1]",
"processor_precision": 100, "processor_precision": 100,
"processor_perc_warn": 75 "processor_perc_warn": 75
} }