PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/discovery/discovery-protocols.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 170 lines | 149 code | 21 blank | 0 comment | 33 complexity | bcc07cd4b34d04c934e937ae3f61792d MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. echo("Discovery protocols:");
  3. global $link_exists;
  4. $community = $device['community'];
  5. if ($device['os'] == "ironware")
  6. {
  7. echo(" Brocade FDP: ");
  8. $fdp_array = snmpwalk_cache_twopart_oid($device, "snFdpCacheEntry", array(), "FOUNDRY-SN-SWITCH-GROUP-MIB");
  9. if ($fdp_array)
  10. {
  11. unset($fdp_links);
  12. foreach (array_keys($fdp_array) as $key)
  13. {
  14. $interface = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '".$key."'"));
  15. $fdp_if_array = $fdp_array[$key];
  16. foreach (array_keys($fdp_if_array) as $entry_key)
  17. {
  18. $fdp = $fdp_if_array[$entry_key];
  19. $remote_device_id = @mysql_result(mysql_query("SELECT `device_id` FROM `devices` WHERE `sysName` = '".$fdp['snFdpCacheDeviceId']."' OR `hostname`='".$fdp['snFdpCacheDeviceId']."'"), 0);
  20. if (!$remote_device_id)
  21. {
  22. $remote_device_id = discover_new_device($fdp['snFdpCacheDeviceId']);
  23. if ($remote_device_id)
  24. {
  25. $int = ifNameDescr($interface);
  26. log_event("Device autodiscovered through FDP on " . $device['hostname'] . " (port " . $int['label'] . ")", $remote_device_id, 'interface', $int['interface_id']);
  27. }
  28. }
  29. if ($remote_device_id)
  30. {
  31. $if = $fdp['snFdpCacheDevicePort'];
  32. $remote_interface_id = @mysql_result(mysql_query("SELECT interface_id FROM `ports` WHERE (`ifDescr` = '$if' OR `ifName`='$if') AND `device_id` = '".$remote_device_id."'"),0);
  33. } else { $remote_interface_id = "0"; }
  34. discover_link($interface['interface_id'], $fdp['snFdpCacheVendorId'], $remote_interface_id, $fdp['snFdpCacheDeviceId'], $fdp['snFdpCacheDevicePort'], $fdp['snFdpCachePlatform'], $fdp['snFdpCacheVersion']);
  35. }
  36. }
  37. }
  38. }
  39. echo(" CISCO-CDP-MIB: ");
  40. unset($cdp_array);
  41. $cdp_array = snmpwalk_cache_twopart_oid($device, "cdpCache", array(), "CISCO-CDP-MIB");
  42. if ($cdp_array)
  43. {
  44. unset($cdp_links);
  45. foreach (array_keys($cdp_array) as $key)
  46. {
  47. $interface = dbFetchRow("SELECT * FROM `ports` WHERE device_id = ? AND `ifIndex` = ?", array($device['device_id'], $key));
  48. $cdp_if_array = $cdp_array[$key];
  49. foreach (array_keys($cdp_if_array) as $entry_key)
  50. {
  51. $cdp = $cdp_if_array[$entry_key];
  52. if (is_valid_hostname($cdp['cdpCacheDeviceId']))
  53. {
  54. $remote_device_id = dbFetchCell("SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ?", array($cdp['cdpCacheDeviceId'], $cdp['cdpCacheDeviceId']));
  55. if (!$remote_device_id)
  56. {
  57. $remote_device_id = discover_new_device($cdp['cdpCacheDeviceId']);
  58. if ($remote_device_id)
  59. {
  60. $int = ifNameDescr($interface);
  61. log_event("Device autodiscovered through CDP on " . $device['hostname'] . " (port " . $int['label'] . ")", $remote_device_id, 'interface', $int['interface_id']);
  62. }
  63. }
  64. if ($remote_device_id)
  65. {
  66. $if = $cdp['cdpCacheDevicePort'];
  67. $remote_interface_id = dbFetchCell("SELECT interface_id FROM `ports` WHERE (`ifDescr` = ? OR `ifName` = ?) AND `device_id` = ?", array($if, $if, $remote_device_id));
  68. } else { $remote_interface_id = "0"; }
  69. if ($interface['interface_id'] && $cdp['cdpCacheDeviceId'] && $cdp['cdpCacheDevicePort'])
  70. {
  71. discover_link($interface['interface_id'], 'cdp', $remote_interface_id, $cdp['cdpCacheDeviceId'], $cdp['cdpCacheDevicePort'], $cdp['cdpCachePlatform'], $cdp['cdpCacheVersion']);
  72. }
  73. }
  74. else
  75. {
  76. echo("X");
  77. }
  78. }
  79. }
  80. }
  81. echo(" LLDP-MIB: ");
  82. unset($lldp_array);
  83. $lldp_array = snmpwalk_cache_threepart_oid($device, "lldpRemoteSystemsData", array(), "LLDP-MIB");
  84. $dot1d_array = snmpwalk_cache_oid($device, "dot1dBasePortIfIndex", array(), "BRIDGE-MIB");
  85. if ($lldp_array)
  86. {
  87. $lldp_links = "";
  88. foreach (array_keys($lldp_array) as $key)
  89. {
  90. $lldp_if_array = $lldp_array[$key];
  91. foreach (array_keys($lldp_if_array) as $entry_key)
  92. {
  93. if (is_numeric($dot1d_array[$entry_key]['dot1dBasePortIfIndex']))
  94. {
  95. $ifIndex = $dot1d_array[$entry_key]['dot1dBasePortIfIndex'];
  96. } else {
  97. $ifIndex = $entry_key;
  98. }
  99. $interface = mysql_fetch_assoc(mysql_query("SELECT * FROM `ports` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '".$ifIndex."'"));
  100. $lldp_instance = $lldp_if_array[$entry_key];
  101. foreach (array_keys($lldp_instance) as $entry_instance)
  102. {
  103. $lldp = $lldp_instance[$entry_instance];
  104. $remote_device_id = @mysql_result(mysql_query("SELECT `device_id` FROM `devices` WHERE `sysName` = '".$lldp['lldpRemSysName']."' OR `hostname`='".$lldp['lldpRemSysName']."'"), 0);
  105. if (!$remote_device_id && is_valid_hostname($lldp['lldpRemSysName']))
  106. {
  107. $remote_device_id = discover_new_device($lldp['lldpRemSysName']);
  108. if ($remote_device_id)
  109. {
  110. $int = ifNameDescr($interface);
  111. log_event("Device autodiscovered through LLDP on " . $device['hostname'] . " (port " . $int['label'] . ")", $remote_device_id, 'interface', $int['interface_id']);
  112. }
  113. }
  114. if ($remote_device_id)
  115. {
  116. $if = $lldp['lldpRemPortDesc']; $id = $lldp['lldpRemPortId'];
  117. $remote_interface_id = @mysql_result(mysql_query("SELECT interface_id FROM `ports` WHERE (`ifDescr` = '$if' OR `ifName`='$if' OR `ifDescr`= '$id' OR `ifName`='$id') AND `device_id` = '".$remote_device_id."'"),0);
  118. } else {
  119. $remote_interface_id = "0";
  120. }
  121. if (is_numeric($interface['interface_id']) && isset($lldp['lldpRemSysName']) && isset($lldp['lldpRemPortId']))
  122. {
  123. discover_link($interface['interface_id'], 'lldp', $remote_interface_id, $lldp['lldpRemSysName'], $lldp['lldpRemPortId'], NULL, $lldp['lldpRemSysDesc']);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. if ($debug) { print_r($link_exists); }
  130. $sql = "SELECT * FROM `links` AS L, `ports` AS I WHERE L.local_interface_id = I.interface_id AND I.device_id = '".$device['device_id']."'";
  131. if ($query = mysql_query($sql))
  132. {
  133. while ($test = mysql_fetch_assoc($query))
  134. {
  135. $local_interface_id = $test['local_interface_id'];
  136. $remote_hostname = $test['remote_hostname'];
  137. $remote_port = $test['remote_port'];
  138. if ($debug) { echo("$local_interface_id -> $remote_hostname -> $remote_port \n"); }
  139. if (!$link_exists[$local_interface_id][$remote_hostname][$remote_port])
  140. {
  141. echo("-");
  142. mysql_query("DELETE FROM `links` WHERE id = '" . $test['id'] . "'");
  143. if ($debug) { echo(mysql_affected_rows()." deleted "); }
  144. }
  145. }
  146. }
  147. unset($link_exists);
  148. echo("\n");
  149. ?>