From b9dc7773d7a5e1082a8a8ac19f89d6314c5bee0a Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Sat, 2 Dec 2017 17:18:45 -0600 Subject: [PATCH] refactor: Don't do unneeded snmp queries (#7841) * Don't do uneeded snmp queries sensors discovery 26 -> 16 wrapped some poller modules in db checks. The same could be done for some others, but more work is needed. * travis fix --- .../entity-physical/entity-physical.inc.php | 7 +- .../discovery/sensors/charge/linux.inc.php | 3 +- .../sensors/cisco-entity-sensor.inc.php | 2 +- .../discovery/sensors/current/linux.inc.php | 3 +- .../discovery/sensors/entity-sensor.inc.php | 55 ++++--- .../sensors/temperature/linux.inc.php | 4 +- .../discovery/sensors/voltage/linux.inc.php | 3 +- includes/polling/cipsec-tunnels.inc.php | 4 +- includes/polling/cisco-mac-accounting.inc.php | 142 +++++++++--------- includes/polling/entity-physical/ios.inc.php | 7 +- includes/polling/mef.inc.php | 58 +++---- includes/polling/os/apex-lynx.inc.php | 2 +- 12 files changed, 152 insertions(+), 138 deletions(-) diff --git a/includes/discovery/entity-physical/entity-physical.inc.php b/includes/discovery/entity-physical/entity-physical.inc.php index 876aef5641..5004603197 100644 --- a/includes/discovery/entity-physical/entity-physical.inc.php +++ b/includes/discovery/entity-physical/entity-physical.inc.php @@ -13,8 +13,11 @@ if ($device['os'] == 'junos') { $entity_array = array(); echo ' entPhysicalEntry'; $entity_array = snmpwalk_cache_oid($device, 'entPhysicalEntry', $entity_array, 'ENTITY-MIB:CISCO-ENTITY-VENDORTYPE-OID-MIB'); - echo ' entAliasMappingIdentifier'; - $entity_array = snmpwalk_cache_twopart_oid($device, 'entAliasMappingIdentifier', $entity_array, 'ENTITY-MIB:IF-MIB'); + + if (!empty($entity_array)) { + echo ' entAliasMappingIdentifier'; + $entity_array = snmpwalk_cache_twopart_oid($device, 'entAliasMappingIdentifier', $entity_array, 'ENTITY-MIB:IF-MIB'); + } } foreach ($entity_array as $entPhysicalIndex => $entry) { diff --git a/includes/discovery/sensors/charge/linux.inc.php b/includes/discovery/sensors/charge/linux.inc.php index c91dbb784e..65044be7bf 100644 --- a/includes/discovery/sensors/charge/linux.inc.php +++ b/includes/discovery/sensors/charge/linux.inc.php @@ -1,7 +1,6 @@ 'voltage', + 'voltsAC' => 'voltage', + 'amperes' => 'current', + 'watts' => 'power', + 'hertz' => 'freq', + 'percentRH' => 'humidity', + 'rpm' => 'fanspeed', + 'celsius' => 'temperature', + 'dBm' => 'dbm', + ); + foreach ($oids as $index => $entry) { $low_limit = null; $low_warn_limit = null; diff --git a/includes/discovery/sensors/temperature/linux.inc.php b/includes/discovery/sensors/temperature/linux.inc.php index 26703e8b3f..34339cb209 100644 --- a/includes/discovery/sensors/temperature/linux.inc.php +++ b/includes/discovery/sensors/temperature/linux.inc.php @@ -42,9 +42,7 @@ if (starts_with($sysObjectId, '.1.3.6.1.4.1.232.')) { } } -$chip = snmp_get($device, '.1.3.6.1.2.1.1.1.0', '-Oqv'); - -if (preg_match("/(Linux).+(ntc)/", $chip)) { +if (preg_match("/(Linux).+(ntc)/", $poll_device['sysDescr'])) { $sensor_type = "chip_axp209_temperature"; $oid = '.1.3.6.1.4.1.8072.1.3.2.4.1.2.10.112.111.119.101.114.45.115.116.97.'; $lowlimit = -40; diff --git a/includes/discovery/sensors/voltage/linux.inc.php b/includes/discovery/sensors/voltage/linux.inc.php index 4af07c1c27..ce6bcb1b39 100644 --- a/includes/discovery/sensors/voltage/linux.inc.php +++ b/includes/discovery/sensors/voltage/linux.inc.php @@ -65,8 +65,7 @@ foreach (explode("\n", $oids) as $data) { }//end if } -$chip = snmp_get($device, '.1.3.6.1.2.1.1.1.0', '-Oqv'); -if (preg_match("/(Linux).+(ntc)/", $chip)) { +if (preg_match("/(Linux).+(ntc)/", $poll_device['sysDescr'])) { $sensor_type = "chip_volts"; $oid = '.1.3.6.1.4.1.8072.1.3.2.4.1.2.10.112.111.119.101.114.45.115.116.97.'; $lowlimit = 3.8; diff --git a/includes/polling/cipsec-tunnels.inc.php b/includes/polling/cipsec-tunnels.inc.php index 07a77edea3..d1fa7f2d3d 100644 --- a/includes/polling/cipsec-tunnels.inc.php +++ b/includes/polling/cipsec-tunnels.inc.php @@ -5,7 +5,9 @@ use LibreNMS\RRD\RrdDefinition; if ($device['os_group'] == 'cisco') { // FIXME - seems to be broken. IPs appear with leading zeroes. $ipsec_array = snmpwalk_cache_oid($device, 'cipSecTunnelEntry', array(), 'CISCO-IPSEC-FLOW-MONITOR-MIB'); - $ike_array = snmpwalk_cache_oid($device, 'cikeTunnelEntry', array(), 'CISCO-IPSEC-FLOW-MONITOR-MIB'); + if (!empty($ipsec_array)) { + $ike_array = snmpwalk_cache_oid($device, 'cikeTunnelEntry', array(), 'CISCO-IPSEC-FLOW-MONITOR-MIB'); + } $tunnels_db = dbFetchRows('SELECT * FROM `ipsec_tunnels` WHERE `device_id` = ?', array($device['device_id'])); foreach ($tunnels_db as $tunnel) { diff --git a/includes/polling/cisco-mac-accounting.inc.php b/includes/polling/cisco-mac-accounting.inc.php index 40a63b39c6..21584988e5 100644 --- a/includes/polling/cisco-mac-accounting.inc.php +++ b/includes/polling/cisco-mac-accounting.inc.php @@ -2,96 +2,96 @@ use LibreNMS\RRD\RrdDefinition; -// FIXME -- we're walking, so we can discover here too. if ($device['os_group'] == 'cisco') { - $cip_oids = array( - 'cipMacHCSwitchedBytes', - 'cipMacHCSwitchedPkts', - ); - $cip_array = array(); - - foreach ($cip_oids as $oid) { - echo "$oid "; - $cip_array = snmpwalk_cache_cip($device, $oid, $cip_array, 'CISCO-IP-STAT-MIB'); - } - - $polled = time(); - - $mac_entries = 0; - $acc_rows = dbFetchRows('SELECT *, A.poll_time AS poll_time FROM `mac_accounting` as A, `ports` AS I where A.port_id = I.port_id AND I.device_id = ?', array($device['device_id'])); - foreach ($acc_rows as $acc) { - $device_id = $acc['device_id']; - $ifIndex = $acc['ifIndex']; - $mac = $acc['mac']; + if (!empty($acc_rows)) { + $cip_oids = array( + 'cipMacHCSwitchedBytes', + 'cipMacHCSwitchedPkts', + ); + $cip_array = array(); - $polled_period = ($polled - $acc['poll_time']); + foreach ($cip_oids as $oid) { + echo "$oid "; + $cip_array = snmpwalk_cache_cip($device, $oid, $cip_array, 'CISCO-IP-STAT-MIB'); + } - if ($cip_array[$ifIndex][$mac]) { - $acc['update']['poll_time'] = $polled; - $acc['update']['poll_prev'] = $acc['poll_time']; - $acc['update']['poll_period'] = $polled_period; + $polled = time(); - $mac_entries++; + $mac_entries = 0; - $b_in = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedBytes']['input']; - $b_out = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedBytes']['output']; - $p_in = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedPkts']['input']; - $p_out = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedPkts']['output']; + foreach ($acc_rows as $acc) { + $device_id = $acc['device_id']; + $ifIndex = $acc['ifIndex']; + $mac = $acc['mac']; - $this_ma = &$cip_array[$ifIndex][$mac]; + $polled_period = ($polled - $acc['poll_time']); - // Update metrics - foreach ($cip_oids as $oid) { - foreach (array('input', 'output') as $dir) { - $oid_dir = $oid.'_'.$dir; - $acc['update'][$oid_dir] = $this_ma[$oid][$dir]; - $acc['update'][$oid_dir.'_prev'] = $acc[$oid_dir]; - $oid_prev = $oid_dir.'_prev'; - if ($this_ma[$oid][$dir]) { - $oid_diff = ($this_ma[$oid][$dir] - $acc[$oid_dir]); - $oid_rate = ($oid_diff / $polled_period); - $acc['update'][$oid_dir.'_rate'] = $oid_rate; - $acc['update'][$oid_dir.'_delta'] = $oid_diff; - d_echo("\n $oid_dir ($oid_diff B) $oid_rate Bps $polled_period secs\n"); + if ($cip_array[$ifIndex][$mac]) { + $acc['update']['poll_time'] = $polled; + $acc['update']['poll_prev'] = $acc['poll_time']; + $acc['update']['poll_period'] = $polled_period; + + $mac_entries++; + + $b_in = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedBytes']['input']; + $b_out = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedBytes']['output']; + $p_in = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedPkts']['input']; + $p_out = $cip_array[$ifIndex][$mac]['cipMacHCSwitchedPkts']['output']; + + $this_ma = &$cip_array[$ifIndex][$mac]; + + // Update metrics + foreach ($cip_oids as $oid) { + foreach (array('input', 'output') as $dir) { + $oid_dir = $oid . '_' . $dir; + $acc['update'][$oid_dir] = $this_ma[$oid][$dir]; + $acc['update'][$oid_dir . '_prev'] = $acc[$oid_dir]; + $oid_prev = $oid_dir . '_prev'; + if ($this_ma[$oid][$dir]) { + $oid_diff = ($this_ma[$oid][$dir] - $acc[$oid_dir]); + $oid_rate = ($oid_diff / $polled_period); + $acc['update'][$oid_dir . '_rate'] = $oid_rate; + $acc['update'][$oid_dir . '_delta'] = $oid_diff; + d_echo("\n $oid_dir ($oid_diff B) $oid_rate Bps $polled_period secs\n"); + } } } - } - d_echo("\n".$acc['hostname'].' '.$acc['ifDescr']." $mac -> $b_in:$b_out:$p_in:$p_out "); + d_echo("\n" . $acc['hostname'] . ' ' . $acc['ifDescr'] . " $mac -> $b_in:$b_out:$p_in:$p_out "); - $rrd_name = array('cip', $ifIndex, $mac); - $rrd_dev = RrdDefinition::make() - ->addDataset('IN', 'COUNTER', 0, 12500000000) - ->addDataset('OUT', 'COUNTER', 0, 12500000000) - ->addDataset('PIN', 'COUNTER', 0, 12500000000) - ->addDataset('POUT', 'COUNTER', 0, 12500000000); + $rrd_name = array('cip', $ifIndex, $mac); + $rrd_dev = RrdDefinition::make() + ->addDataset('IN', 'COUNTER', 0, 12500000000) + ->addDataset('OUT', 'COUNTER', 0, 12500000000) + ->addDataset('PIN', 'COUNTER', 0, 12500000000) + ->addDataset('POUT', 'COUNTER', 0, 12500000000); - // FIXME - use memcached to make sure these values don't go backwards? - $fields = array( - 'IN' => $b_in, - 'OUT' => $b_out, - 'PIN' => $p_in, - 'POUT' => $p_out, - ); + // FIXME - use memcached to make sure these values don't go backwards? + $fields = array( + 'IN' => $b_in, + 'OUT' => $b_out, + 'PIN' => $p_in, + 'POUT' => $p_out, + ); - $tags = compact('ifIndex', 'mac', 'rrd_name', 'rrd_def'); - data_update($device, 'cip', $tags, $fields); + $tags = compact('ifIndex', 'mac', 'rrd_name', 'rrd_def'); + data_update($device, 'cip', $tags, $fields); - if ($acc['update']) { - // Do Updates - dbUpdate($acc['update'], 'mac_accounting', '`ma_id` = ?', array($acc['ma_id'])); - } //end if - }//end if - }//end foreach + if ($acc['update']) { + // Do Updates + dbUpdate($acc['update'], 'mac_accounting', '`ma_id` = ?', array($acc['ma_id'])); + } //end if + }//end if + }//end foreach - unset($cip_array); + unset($cip_array); - if ($mac_entries) { - echo " $mac_entries MAC accounting entries\n"; + if ($mac_entries) { + echo " $mac_entries MAC accounting entries\n"; + } } - echo "\n"; }//end if diff --git a/includes/polling/entity-physical/ios.inc.php b/includes/polling/entity-physical/ios.inc.php index ab415e5ef7..2fb0e90a4f 100644 --- a/includes/polling/entity-physical/ios.inc.php +++ b/includes/polling/entity-physical/ios.inc.php @@ -5,8 +5,6 @@ use LibreNMS\RRD\RrdDefinition; echo "Cisco Cat6xxx/76xx Crossbar : \n"; $mod_stats = snmpwalk_cache_oid($device, 'cc6kxbarModuleModeTable', array(), 'CISCO-CAT6K-CROSSBAR-MIB'); -$chan_stats = snmpwalk_cache_oid($device, 'cc6kxbarModuleChannelTable', array(), 'CISCO-CAT6K-CROSSBAR-MIB'); -$chan_stats = snmpwalk_cache_oid($device, 'cc6kxbarStatisticsTable', $chan_stats, 'CISCO-CAT6K-CROSSBAR-MIB'); foreach ($mod_stats as $index => $entry) { $group = 'c6kxbar'; @@ -16,6 +14,11 @@ foreach ($mod_stats as $index => $entry) { } } +$chan_stats = snmpwalk_cache_oid($device, 'cc6kxbarModuleChannelTable', array(), 'CISCO-CAT6K-CROSSBAR-MIB'); +if (!empty($chan_stats)) { + $chan_stats = snmpwalk_cache_oid($device, 'cc6kxbarStatisticsTable', $chan_stats, 'CISCO-CAT6K-CROSSBAR-MIB'); +} + foreach ($chan_stats as $index => $entry) { list($index,$subindex) = explode('.', $index, 2); $group = 'c6kxbar'; diff --git a/includes/polling/mef.inc.php b/includes/polling/mef.inc.php index c9f33e2679..9027443980 100644 --- a/includes/polling/mef.inc.php +++ b/includes/polling/mef.inc.php @@ -7,42 +7,44 @@ echo 'MEF Links: '; */ $db_info_list = dbFetchRows('SELECT id, mefID, mefType, mefIdent, mefMTU, mefAdmState, mefRowState FROM mefinfo WHERE device_id = ?', array($device['device_id'])); -$current_mefinfo = snmpwalk_cache_multi_oid($device, 'MefServiceEvcCfgEntry', array(), 'MEF-UNI-EVC-MIB'); -foreach ($db_info_list as $db_info) { - $mef_info = array(); +if (!empty($db_info_list)) { + $current_mefinfo = snmpwalk_cache_multi_oid($device, 'MefServiceEvcCfgEntry', array(), 'MEF-UNI-EVC-MIB'); - $mef_info['mefType'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgServiceType']; - $mef_info['mefIdent'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgIdentifier']; - $mef_info['mefMTU'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgMtuSize']; - $mef_info['mefAdmState'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgAdminState']; - $mef_info['mefRowState'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgRowStatus']; + foreach ($db_info_list as $db_info) { + $mef_info = array(); - /* - * Coriant MEF-EVC is quite strange, MTU is sometime set to 0 so we can set it into 1600 instead - * According to Coriant this should be fixed in Nov 2017 - */ - if (($mef_info['mefMTU'] == 0) && ($device['os'] == 'coriant')) { - $mef_info['mefMTU'] = 1600; - } + $mef_info['mefType'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgServiceType']; + $mef_info['mefIdent'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgIdentifier']; + $mef_info['mefMTU'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgMtuSize']; + $mef_info['mefAdmState'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgAdminState']; + $mef_info['mefRowState'] = $current_mefinfo[$db_info['mefID']]['mefServiceEvcCfgRowStatus']; - /* - * Process all the MEF properties. - */ - foreach ($mef_info as $property => $value) { /* - * Check the property for any modifications. + * Coriant MEF-EVC is quite strange, MTU is sometime set to 0 so we can set it into 1600 instead + * According to Coriant this should be fixed in Nov 2017 */ - if ($mef_info[$property] != $db_info[$property]) { - // FIXME - this should loop building a query and then run the query after the loop (bad geert!) - dbUpdate(array($property => mres($mef_info[$property])), 'mefinfo', '`id` = ?', array($db_info['id'])); - if ($db_info['mefIdent'] != null) { - log_event("MEF Link : ".mres($db_info['mefIdent']) . ' (' . preg_replace('/^mef/', '', mres($db_info[$property])) . ') -> ' . $mef_info[$property], $device); + if (($mef_info['mefMTU'] == 0) && ($device['os'] == 'coriant')) { + $mef_info['mefMTU'] = 1600; + } + + /* + * Process all the MEF properties. + */ + foreach ($mef_info as $property => $value) { + /* + * Check the property for any modifications. + */ + if ($mef_info[$property] != $db_info[$property]) { + // FIXME - this should loop building a query and then run the query after the loop (bad geert!) + dbUpdate(array($property => mres($mef_info[$property])), 'mefinfo', '`id` = ?', array($db_info['id'])); + if ($db_info['mefIdent'] != null) { + log_event("MEF Link : " . mres($db_info['mefIdent']) . ' (' . preg_replace('/^mef/', '', mres($db_info[$property])) . ') -> ' . $mef_info[$property], $device); + } } } - } -}//end foreach - + }//end foreach +} /* * Finished discovering MEF Links information. */ diff --git a/includes/polling/os/apex-lynx.inc.php b/includes/polling/os/apex-lynx.inc.php index b6b08e0941..9204dee60c 100644 --- a/includes/polling/os/apex-lynx.inc.php +++ b/includes/polling/os/apex-lynx.inc.php @@ -1,3 +1,3 @@