Skip to content

Commit

Permalink
JunOS BGP port to SnmpQuery
Browse files Browse the repository at this point in the history
Remove usage of snmpwalk_cache_long_oid
  • Loading branch information
murrant committed Dec 5, 2024
1 parent 3487028 commit 421bb96
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 68 deletions.
77 changes: 34 additions & 43 deletions includes/polling/bgp-peers.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\RRD\RrdDefinition;
use LibreNMS\Util\IP;
use LibreNMS\Util\Oid;

$peers = dbFetchRows('SELECT * FROM `bgpPeers` AS B LEFT JOIN `vrfs` AS V ON `B`.`vrf_id` = `V`.`vrf_id` WHERE `B`.`device_id` = ?', [$device['device_id']]);

Expand All @@ -23,7 +24,19 @@

$generic = false;
if ($device['os'] == 'junos') {
$peer_data_check = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerIndex', '.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.14', [], 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_check = SnmpQuery::mibDir('junos')->enumStrings()->abortOnFailure()->walk([
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerState',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerStatus',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerInUpdates',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerOutUpdates',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerInTotalMessages',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerOutTotalMessages',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerFsmEstablishedTime',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerLocalAddr',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerRemoteAddrType',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerLastErrorReceived',
'BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerLastErrorReceivedText',
])->valuesByIndex();
} elseif ($device['os_group'] === 'arista') {
$peer_data_check = snmpwalk_cache_oid($device, 'aristaBgp4V2PeerRemoteAs', [], 'ARISTA-BGP4V2-MIB');
} elseif ($device['os'] === 'dell-os10') {
Expand Down Expand Up @@ -97,56 +110,34 @@
];
} elseif ($device['os'] == 'junos') {
if (! isset($junos)) {
echo "\nCaching Oids...";

foreach ($peer_data_check as $hash => $index) {
$peer_ip_snmp = ltrim($index['orig'], '.');
$exploded_ip = explode('.', $peer_ip_snmp);
if (count($exploded_ip) > 11) {
// ipv6
$tmp_peer_ip = (string) IP::parse(snmp2ipv6($peer_ip_snmp), true);
} else {
// ipv4
$tmp_peer_ip = implode('.', array_slice($exploded_ip, -4));
}
$junos[$tmp_peer_ip]['hash'] = $hash;
$junos[$tmp_peer_ip]['index'] = $index['jnxBgpM2PeerIndex'];
// parse peer IP
$junos = [];
foreach ($peer_data_check as $peers => $jnx_peer_data) {
$exploded_ip = explode('.', $peers);
$ip_offset = count($exploded_ip) > 30 ? -16 : -4;
$tmp_peer_ip = Oid::of(implode('.', array_slice($exploded_ip, $ip_offset)))->toIp()->compressed();
$junos[$tmp_peer_ip] = $jnx_peer_data;
}
}

if (! isset($peer_data_tmp)) {
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerState', '.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.2', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerStatus', '.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.3', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerInUpdates', '.1.3.6.1.4.1.2636.5.1.1.2.6.1.1.1', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerOutUpdates', '.1.3.6.1.4.1.2636.5.1.1.2.6.1.1.2', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerInTotalMessages', '.1.3.6.1.4.1.2636.5.1.1.2.6.1.1.3', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerOutTotalMessages', '.1.3.6.1.4.1.2636.5.1.1.2.6.1.1.4', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerFsmEstablishedTime', '.1.3.6.1.4.1.2636.5.1.1.2.4.1.1.1', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerInUpdatesElapsedTime', '.1.3.6.1.4.1.2636.5.1.1.2.4.1.1.2', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerLocalAddr', '.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.7', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerRemoteAddrType', '.1.3.6.1.4.1.2636.5.1.1.2.1.1.1.10', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerLastErrorReceived', '.1.3.6.1.4.1.2636.5.1.1.2.2.1.1.1', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
$peer_data_tmp = snmpwalk_cache_long_oid($device, 'jnxBgpM2PeerLastErrorReceivedText', '.1.3.6.1.4.1.2636.5.1.1.2.2.1.1.5', $peer_data_tmp, 'BGP4-V2-MIB-JUNIPER', 'junos');
d_echo($peer_data_tmp);
}
$address = $peer_ip->compressed();
$peer_data = [
'bgpPeerState' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerState'] ?? null,
'bgpPeerAdminStatus' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerStatus'] ?? null,
'bgpPeerInUpdates' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerInUpdates'] ?? null,
'bgpPeerOutUpdates' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerOutUpdates'] ?? null,
'bgpPeerInTotalMessages' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerInTotalMessages'] ?? null,
'bgpPeerOutTotalMessages' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerOutTotalMessages'] ?? null,
'bgpPeerFsmEstablishedTime' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerFsmEstablishedTime'] ?? null,
'bgpPeerLastErrorText' => $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerLastErrorReceivedText'] ?? null,
];

$peer_hash = $junos[(string) $peer_ip]['hash'];
$peer_data = [];
$peer_data['bgpPeerState'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerState'];
$peer_data['bgpPeerAdminStatus'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerStatus'];
$peer_data['bgpPeerInUpdates'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerInUpdates'];
$peer_data['bgpPeerOutUpdates'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerOutUpdates'];
$peer_data['bgpPeerInTotalMessages'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerInTotalMessages'];
$peer_data['bgpPeerOutTotalMessages'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerOutTotalMessages'];
$peer_data['bgpPeerFsmEstablishedTime'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerFsmEstablishedTime'];
$peer_data['bgpPeerLastErrorText'] = $peer_data_tmp[$peer_hash]['jnxBgpM2PeerLastErrorReceivedText'];

$error_data = explode(' ', $peer_data_tmp[$peer_hash]['jnxBgpM2PeerLastErrorReceived']);
$error_data = explode(' ', $junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerLastErrorReceived']);
$peer_data['bgpPeerLastErrorCode'] = intval($error_data[0]);
$peer_data['bgpPeerLastErrorSubCode'] = intval($error_data[1]);

try {
$peer_data['bgpLocalAddr'] = IP::fromHexString($peer_data_tmp[$peer_hash]['jnxBgpM2PeerLocalAddr'])->uncompressed();
$peer_data['bgpLocalAddr'] = IP::fromHexString($junos[$address]['BGP4-V2-MIB-JUNIPER::jnxBgpM2PeerLocalAddr'])->compressed();
} catch (InvalidIpException $e) {
$peer_data['bgpLocalAddr'] = '';
}
Expand Down
25 changes: 0 additions & 25 deletions includes/snmp.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,31 +449,6 @@ function snmpwalk_cache_numerical_oid($device, $oid, $array = [], $mib = null, $
return $array;
}//end snmpwalk_cache_oid()

function snmpwalk_cache_long_oid($device, $oid, $noid, $array = [], $mib = null, $mibdir = null, $snmpflags = '-OQnU')
{
$data = snmp_walk($device, $oid, $snmpflags, $mib, $mibdir);

if (empty($data)) {
return $array;
}

foreach (explode("\n", $data) as $entry) {
[$tmp_oid,$value] = explode('=', $entry, 2);
$tmp_oid = trim($tmp_oid);
$value = trim($value);
$tmp_index = str_replace($noid, '', $tmp_oid);
$index = md5($tmp_index);
if (! empty($index) && ! empty($oid)) {
$array[$index][$oid] = $value;
if (empty($array[$index]['orig'])) {
$array[$index]['orig'] = $tmp_index;
}
}
}

return $array;
}//end snmpwalk_cache_oid()

/**
* Just like snmpwalk_cache_oid except that it returns the numerical oid as the index
* this is useful when the oid is indexed by the mac address and snmpwalk would
Expand Down

0 comments on commit 421bb96

Please sign in to comment.