PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/discovery/cisco-vrf.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 165 lines | 128 code | 27 blank | 10 comment | 19 complexity | 945f3e224a369d9d529a287677a3afa4 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. if ($config['enable_vrfs'])
  3. {
  4. if ($device['os_group'] == "cisco" || $device['os_group'] == "junos" || $device['os'] == "ironware")
  5. {
  6. unset($vrf_count);
  7. echo("VRFs : ");
  8. /*
  9. There are 2 MIBs for VPNs : MPLS-VPN-MIB (oldest) and MPLS-L3VPN-STD-MIB (newest)
  10. Unfortunately, there is no simple way to find out which one to use, unless we reference
  11. all the possible devices and check what they support.
  12. Therefore we start testing the MPLS-L3VPN-STD-MIB that is prefered if available.
  13. */
  14. // Grab all the info first, then use it in the code.
  15. // It removes load on the device and makes things much faster
  16. $rds = snmp_walk($device, "mplsL3VpnVrfRD", "-Osqn", "MPLS-L3VPN-STD-MIB", NULL);
  17. if (empty($rds))
  18. {
  19. $rds = snmp_walk($device, "mplsVpnVrfRouteDistinguisher", "-Osqn", "MPLS-VPN-MIB", NULL);
  20. $vpnmib = "MPLS-VPN-MIB";
  21. $rds = str_replace(".1.3.6.1.3.118.1.2.2.1.3.", "", $rds);
  22. $descrs_oid = ".1.3.6.1.3.118.1.2.2.1.2";
  23. $ports_oid = ".1.3.6.1.3.118.1.2.1.1.2";
  24. }
  25. else
  26. {
  27. $vpnmib = "MPLS-L3VPN-STD-MIB";
  28. $rds = str_replace(".1.3.6.1.2.1.10.166.11.1.2.2.1.4.", "", $rds);
  29. $descrs_oid = ".1.3.6.1.2.1.10.166.11.1.2.2.1.3";
  30. $ports_oid = ".1.3.6.1.2.1.10.166.11.1.2.1.1.2";
  31. }
  32. if ($debug)
  33. {
  34. echo("\n[DEBUG]\nUsing $vpnmib\n[/DEBUG]\n");
  35. echo("\n[DEBUG OIDS]\n$rds\n[/DEBUG]\n");
  36. }
  37. $rds = trim($rds);
  38. $descrs = snmp_walk($device, $descrs_oid, "-Osqn", $vpnmib, NULL);
  39. $ports = snmp_walk($device, $ports_oid, "-Osqn", $vpnmib, NULL);
  40. $descrs = trim(str_replace("$descrs_oid.", "", $descrs));
  41. $ports = trim(str_replace("$ports_oid.", "", $ports));
  42. $descr_table = array();
  43. $port_table = array();
  44. foreach (explode("\n", $descrs) as $descr)
  45. {
  46. $t = explode(" ", $descr, 2);
  47. $descr_table[$t[0]] = $t[1];
  48. }
  49. foreach (explode("\n", $ports) as $port)
  50. {
  51. $t = explode(" ", $port);
  52. $dotpos = strrpos($t[0], ".");
  53. $vrf_oid = substr($t[0], 0, $dotpos);
  54. $port_id = substr($t[0], $dotpos+1);
  55. if (empty($port_table[$vrf_oid])) { $port_table[$vrf_oid][0] = $port_id; }
  56. else {array_push($port_table[$vrf_oid], $port_id);}
  57. }
  58. foreach (explode("\n", $rds) as $oid)
  59. {
  60. echo "\n";
  61. if ($oid)
  62. {
  63. // 8.49.53.48.56.58.49.48.48 "1508:100"
  64. // First digit gives number of chars in VRF Name, then it's ASCII
  65. list($vrf_oid, $vrf_rd) = explode(" ", $oid);
  66. $oid_values = explode(".", $vrf_oid);
  67. $vrf_name = "";
  68. for ($i = 1; $i <= $oid_values[0]; $i++) {$vrf_name .= chr($oid_values[$i]);}
  69. echo "\n [VRF $vrf_name] OID - $vrf_oid";
  70. echo "\n [VRF $vrf_name] RD - $vrf_rd";
  71. echo "\n [VRF $vrf_name] DESC - ".$descr_table[$vrf_oid];
  72. if (@mysql_result(mysql_query("SELECT count(*) FROM vrfs WHERE `device_id` = '".$device['device_id']."'
  73. AND `vrf_oid`='$vrf_oid'"),0))
  74. {
  75. $update_query = "UPDATE `vrfs` SET `mplsVpnVrfDescription` = '$descr_table[$vrf_oid]', `mplsVpnVrfRouteDistinguisher` = '$vrf_rd' ";
  76. $update_query .= "WHERE device_id = '".$device['device_id']."' AND vrf_oid = '$vrf_oid'";
  77. mysql_query($update_query);
  78. }
  79. else
  80. {
  81. $insert_query = "INSERT INTO `vrfs` (`vrf_oid`,`vrf_name`,`mplsVpnVrfRouteDistinguisher`,`mplsVpnVrfDescription`,`device_id`) ";
  82. $insert_query .= "VALUES ('$vrf_oid','$vrf_name','$vrf_rd','".$descr_table[$vrf_oid]."','".$device['device_id']."')";
  83. mysql_query($insert_query);
  84. }
  85. $vrf_id = @mysql_result(mysql_query("SELECT vrf_id FROM vrfs WHERE `device_id` = '".$device['device_id']."' AND `vrf_oid`='$vrf_oid'"),0);
  86. $valid_vrf[$vrf_id] = 1;
  87. echo "\n [VRF $vrf_name] PORTS - ";
  88. foreach ($port_table[$vrf_oid] as $if_id)
  89. {
  90. $interface = mysql_fetch_assoc(mysql_query("SELECT * FROM ports WHERE ifIndex = '$if_id' AND device_id = '" . $device['device_id'] . "'"));
  91. echo(makeshortif($interface['ifDescr']) . " ");
  92. mysql_query("UPDATE ports SET ifVrf = '$vrf_id' WHERE interface_id = '".$interface['interface_id']."'");
  93. $if = $interface['interface_id'];
  94. $valid_vrf_if[$vrf_id][$if] = 1;
  95. }
  96. }
  97. }
  98. echo "\n";
  99. $sql = "SELECT * FROM ports WHERE device_id = '" . $device['device_id'] . "'";
  100. $data = mysql_query($sql);
  101. while ($row = mysql_fetch_assoc($data))
  102. {
  103. $if = $row['interface_id'];
  104. $vrf_id = $row['ifVrf'];
  105. if ($row['ifVrf'])
  106. {
  107. if (!$valid_vrf_if[$vrf_id][$if])
  108. {
  109. echo("-");
  110. $query = @mysql_query("UPDATE ports SET `ifVrf` = NULL WHERE interface_id = '$if'");
  111. }
  112. else
  113. {
  114. echo(".");
  115. }
  116. }
  117. }
  118. $sql = "SELECT * FROM vrfs WHERE device_id = '" . $device['device_id'] . "'";
  119. $data = mysql_query($sql);
  120. while ($row = mysql_fetch_assoc($data))
  121. {
  122. $vrf_id = $row['vrf_id'];
  123. if (!$valid_vrf[$vrf_id])
  124. {
  125. echo("-");
  126. $query = @mysql_query("DELETE FROM vrfs WHERE vrf_id = '$vrf_id'");
  127. }
  128. else
  129. {
  130. echo(".");
  131. }
  132. }
  133. unset($valid_vrf_if);
  134. unset($valid_vrf);
  135. echo("\n");
  136. } # cisco/junos/ironware
  137. } # enable_vrfs
  138. ?>