PageRenderTime 74ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/discovery/bgp-peers.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 209 lines | 180 code | 22 blank | 7 comment | 34 complexity | d6e771dacc6b612ae731f89340d2bc67 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. if ($config['enable_bgp'])
  3. {
  4. ### Discover BGP peers
  5. echo("BGP Sessions : ");
  6. $bgpLocalAs = trim(snmp_walk($device, ".1.3.6.1.2.1.15.2", "-Oqvn", "BGP4-MIB", $config['mibdir']));
  7. if (is_numeric($bgpLocalAs))
  8. {
  9. echo("AS$bgpLocalAs ");
  10. if ($bgpLocalAs != $device['bgpLocalAs'])
  11. {
  12. mysql_query("UPDATE devices SET bgpLocalAs = '$bgpLocalAs' WHERE device_id = '".$device['device_id']."'"); echo("Updated AS ");
  13. }
  14. $peers_data = snmp_walk($device, "BGP4-MIB::bgpPeerRemoteAs", "-Oq", "BGP4-MIB", $config['mibdir']);
  15. if ($debug) { echo("Peers : $peers_data \n"); }
  16. $peers = trim(str_replace("BGP4-MIB::bgpPeerRemoteAs.", "", $peers_data));
  17. foreach (explode("\n", $peers) as $peer)
  18. {
  19. list($peer_ip, $peer_as) = explode(" ", $peer);
  20. if ($peer && $peer_ip != "0.0.0.0")
  21. {
  22. if ($debug) { echo("Found peer $peer_ip (AS$peer_as)\n"); }
  23. $peerlist[] = array('ip' => $peer_ip, 'as' => $peer_as);
  24. }
  25. } # Foreach
  26. if ($device['os'] == "junos")
  27. {
  28. ## Juniper BGP4-V2 MIB
  29. ## FIXME: needs a big cleanup! also see below.
  30. # FIXME: is .0.ipv6 the only possible value here?
  31. $result = snmp_walk($device, "jnxBgpM2PeerRemoteAs.0.ipv6", "-Onq", "BGP4-V2-MIB-JUNIPER", $config['install_dir']."/mibs/junos");
  32. $peers = trim(str_replace(".1.3.6.1.4.1.2636.5.1.1.2.1.1.1.13.0.","", $result));
  33. foreach (explode("\n", $peers) as $peer)
  34. {
  35. list($peer_ip_snmp, $peer_as) = explode(" ", $peer);
  36. # Magic! Basically, takes SNMP form and finds peer IPs from the walk OIDs.
  37. $peer_ip = Net_IPv6::compress(snmp2ipv6(implode('.',array_slice(explode('.',$peer_ip_snmp),count(explode('.',$peer_ip_snmp))-16))));
  38. if ($peer)
  39. {
  40. if ($debug) echo("Found peer $peer_ip (AS$peer_as)\n");
  41. $peerlist[] = array('ip' => $peer_ip, 'as' => $peer_as);
  42. }
  43. } # Foreach
  44. } # OS junos
  45. } else {
  46. echo("No BGP on host");
  47. if ($device['bgpLocalAs'])
  48. {
  49. mysql_query("UPDATE devices SET bgpLocalAs = NULL WHERE device_id = '".$device['device_id']."'"); echo(" (Removed ASN) ");
  50. } # End if
  51. } # End if
  52. ## Process disovered peers
  53. if (isset($peerlist))
  54. {
  55. foreach ($peerlist as $peer)
  56. {
  57. $astext = get_astext($peer['as']);
  58. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `bgpPeers` WHERE `device_id` = '".$device['device_id']."' AND bgpPeerIdentifier = '".$peer['ip']."'"),0) < '1')
  59. {
  60. $add = mysql_query("INSERT INTO bgpPeers (`device_id`, `bgpPeerIdentifier`, `bgpPeerRemoteAS`) VALUES ('".$device['device_id']."','".$peer['ip']."','".$peer['as']."')");
  61. echo("+");
  62. } else {
  63. $update = mysql_query("UPDATE `bgpPeers` SET bgpPeerRemoteAs = " . $peer['as'] . ", astext = '" . mres($astext) . "' WHERE `device_id` = '".$device['device_id']."' AND bgpPeerIdentifier = '".$peer['ip']."'");
  64. echo(".");
  65. }
  66. if ($device['os_group'] == "cisco" || $device['os'] == "junos")
  67. {
  68. if ($device['os_group'] == "cisco")
  69. {
  70. ## Get afi/safi and populate cbgp on cisco ios (xe/xr)
  71. unset($af_list);
  72. $af_data = snmp_walk($device, "cbgpPeerAddrFamilyName." . $peer['ip'], "-OsQ", "CISCO-BGP4-MIB", $config['mibdir']);
  73. if ($debug) { echo("afi data :: $af_data \n"); }
  74. $afs = trim(str_replace("cbgpPeerAddrFamilyName.".$peer['ip'].".", "", $af_data));
  75. foreach (explode("\n", $afs) as $af)
  76. {
  77. if ($debug) { echo("AFISAFI = $af\n"); }
  78. list($afisafi, $text) = explode(" = ", $af);
  79. list($afi, $safi) = explode(".", $afisafi);
  80. if ($afi && $safi)
  81. {
  82. $af_list[$afi][$safi] = 1;
  83. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `bgpPeers_cbgp` WHERE `device_id` = '".$device['device_id']."' AND bgpPeerIdentifier = '".$peer['ip']."' AND afi = '$afi' AND safi = '$safi'"),0) == 0)
  84. {
  85. mysql_query("INSERT INTO `bgpPeers_cbgp` (device_id,bgpPeerIdentifier, afi, safi) VALUES ('".$device['device_id']."','".$peer['ip']."','$afi','$safi')");
  86. }
  87. }
  88. }
  89. } # os_group=cisco
  90. if ($device['os'] == "junos")
  91. {
  92. $safis[1] = "unicast";
  93. $safis[2] = "multicast";
  94. if (!isset($peerIndexes))
  95. {
  96. $j_bgp = snmpwalk_cache_multi_oid($device, "jnxBgpM2PeerTable", $jbgp, "BGP4-V2-MIB-JUNIPER", $config['install_dir']."/mibs/junos");
  97. foreach ($j_bgp as $index => $entry)
  98. {
  99. switch ($entry['jnxBgpM2PeerRemoteAddrType'])
  100. {
  101. case 'ipv4':
  102. $ip = long2ip(hexdec($entry['jnxBgpM2PeerRemoteAddr']));
  103. $j_peerIndexes[$ip] = $entry['jnxBgpM2PeerIndex'];
  104. break;
  105. case 'ipv6':
  106. $ip6 = trim(str_replace(' ','',$entry['jnxBgpM2PeerRemoteAddr']),'"');
  107. $ip6 = substr($ip6,0,4) . ':' . substr($ip6,4,4) . ':' . substr($ip6,8,4) . ':' . substr($ip6,12,4) . ':' . substr($ip6,16,4) . ':' . substr($ip6,20,4) . ':' . substr($ip6,24,4) . ':' . substr($ip6,28,4);
  108. $ip6 = Net_IPv6::compress($ip6);
  109. $j_peerIndexes[$ip6] = $entry['jnxBgpM2PeerIndex'];
  110. break;
  111. default:
  112. echo("HALP? Don't know RemoteAddrType " . $entry['jnxBgpM2PeerRemoteAddrType'] . "!\n");
  113. break;
  114. }
  115. }
  116. }
  117. if (!isset($j_afisafi))
  118. {
  119. $j_prefixes = snmpwalk_cache_multi_oid($device, "jnxBgpM2PrefixCountersTable", $jbgp, "BGP4-V2-MIB-JUNIPER", $config['install_dir']."/mibs/junos");
  120. foreach (array_keys($j_prefixes) as $key)
  121. {
  122. list($index,$afisafi) = explode('.',$key,2);
  123. $j_afisafi[$index][] = $afisafi;
  124. }
  125. }
  126. foreach ($j_afisafi[$j_peerIndexes[$peer['ip']]] as $afisafi)
  127. {
  128. list ($afi,$safi) = explode('.',$afisafi); $safi = $safis[$safi];
  129. $af_list[$afi][$safi] = 1;
  130. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `bgpPeers_cbgp` WHERE `device_id` = '".$device['device_id']."' AND bgpPeerIdentifier = '".$peer['ip']."' AND afi = '$afi' AND safi = '$safi'"),0) == 0)
  131. {
  132. mysql_query("INSERT INTO `bgpPeers_cbgp` (device_id,bgpPeerIdentifier, afi, safi) VALUES ('".$device['device_id']."','".$peer['ip']."','$afi','$safi')");
  133. }
  134. }
  135. } # os=junos
  136. $af_query = mysql_query("SELECT * FROM bgpPeers_cbgp WHERE `device_id` = '".$device['device_id']."' AND bgpPeerIdentifier = '".$peer['ip']."'");
  137. while ($entry = mysql_fetch_assoc($af_query))
  138. {
  139. $afi = $entry['afi'];
  140. $safi = $entry['safi'];
  141. if (!$af_list[$afi][$safi])
  142. {
  143. mysql_query("DELETE FROM `bgpPeers_cbgp` WHERE `device_id` = '".$device['device_id']."' AND bgpPeerIdentifier = '".$peer['ip']."' AND afi = '$afi' AND safi = '$safi'");
  144. }
  145. } # AF list
  146. } # os=cisco|junos
  147. } # Foreach
  148. unset($j_afisafi);
  149. unset($j_prefixes);
  150. unset($j_bgp);
  151. unset($j_peerIndexes);
  152. } # isset
  153. ### Delete removed peers
  154. $sql = "SELECT * FROM bgpPeers AS B, devices AS D WHERE B.device_id = D.device_id AND D.device_id = '".$device['device_id']."'";
  155. $query = mysql_query($sql);
  156. while ($entry = mysql_fetch_assoc($query))
  157. {
  158. unset($exists);
  159. $i = 0;
  160. while ($i < count($peerlist) && !isset($exists))
  161. {
  162. if ($peerlist[$i]['ip'] == $entry['bgpPeerIdentifier']) { $exists = 1; }
  163. $i++;
  164. }
  165. if (!isset($exists))
  166. {
  167. mysql_query("DELETE FROM bgpPeers WHERE bgpPeer_id = '" . $entry['bgpPeer_id'] . "'");
  168. mysql_query("DELETE FROM bgpPeers_cbgp WHERE bgpPeer_id = '" . $entry['bgpPeer_id'] . "'");
  169. echo("-");
  170. }
  171. }
  172. unset($peerlist);
  173. echo("\n");
  174. }
  175. ?>