PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/discovery/arp-table.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 71 lines | 59 code | 11 blank | 1 comment | 14 complexity | 08daff1c43ebc42855586666d7a1f8b0 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. unset ($mac_table);
  3. echo("ARP Table : ");
  4. $ipNetToMedia_data = snmp_walk($device, 'ipNetToMediaPhysAddress', '-Oq', 'IP-MIB');
  5. $ipNetToMedia_data = str_replace("ipNetToMediaPhysAddress.", "", trim($ipNetToMedia_data));
  6. $ipNetToMedia_data = str_replace("IP-MIB::", "", trim($ipNetToMedia_data));
  7. foreach (explode("\n", $ipNetToMedia_data) as $data)
  8. {
  9. list($oid, $mac) = explode(" ", $data);
  10. list($if, $first, $second, $third, $fourth) = explode(".", $oid);
  11. $ip = $first .".". $second .".". $third .".". $fourth;
  12. if ($ip != '...')
  13. {
  14. $interface = mysql_fetch_assoc(mysql_query("SELECT * FROM ports WHERE device_id = '".$device['device_id']."' AND ifIndex = '".$if."'"));
  15. list($m_a, $m_b, $m_c, $m_d, $m_e, $m_f) = explode(":", $mac);
  16. $m_a = zeropad($m_a);$m_b = zeropad($m_b);$m_c = zeropad($m_c);$m_d = zeropad($m_d);$m_e = zeropad($m_e);$m_f = zeropad($m_f);
  17. $md_a = hexdec($m_a);$md_b = hexdec($m_b);$md_c = hexdec($m_c);$md_d = hexdec($m_d);$md_e = hexdec($m_e);$md_f = hexdec($m_f);
  18. $mac = "$m_a:$m_b:$m_c:$m_d:$m_e:$m_f";
  19. $mac_table[$if][$mac]['ip'] = $ip;
  20. $mac_table[$if][$mac]['ciscomac'] = "$m_a$m_b.$m_c$m_d.$m_e$m_f";
  21. $clean_mac = $m_a . $m_b . $m_c . $m_d . $m_e . $m_f;
  22. $mac_table[$if][$mac]['cleanmac'] = $clean_mac;
  23. $interface_id = $interface['interface_id'];
  24. $mac_table[$interface_id][$clean_mac] = 1;
  25. if (mysql_result(mysql_query("SELECT COUNT(*) from ipv4_mac WHERE interface_id = '".$interface['interface_id']."' AND ipv4_address = '$ip'"),0))
  26. {
  27. $sql = "UPDATE `ipv4_mac` SET `mac_address` = '$clean_mac' WHERE interface_id = '".$interface['interface_id']."' AND ipv4_address = '$ip'";
  28. $old_mac = mysql_fetch_row(mysql_query("SELECT mac_address from ipv4_mac WHERE ipv4_address='$ip' AND interface_id = '".$interface['interface_id']."'"));
  29. if ($clean_mac != $old_mac[0] && $clean_mac != '' && $old_mac[0] != '')
  30. {
  31. if ($debug) { echo("Changed mac address for $ip from $old_mac[0] to $clean_mac\n"); }
  32. log_event("MAC change: $ip : " . mac_clean_to_readable($old_mac[0]) . " -> " . mac_clean_to_readable($clean_mac), $device, "interface", $interface['interface_id']);
  33. }
  34. mysql_query($sql);
  35. echo(".");
  36. }
  37. else
  38. {
  39. echo("+");
  40. #echo("Add MAC $mac\n");
  41. mysql_query("INSERT INTO `ipv4_mac` (interface_id, mac_address, ipv4_address) VALUES ('".$interface['interface_id']."','$clean_mac','$ip')");
  42. }
  43. }
  44. }
  45. $sql = "SELECT * from ipv4_mac AS M, ports as I WHERE M.interface_id = I.interface_id and I.device_id = '".$device['device_id']."'";
  46. $query = mysql_query($sql);
  47. while ($entry = mysql_fetch_assoc($query))
  48. {
  49. $entry_mac = $entry['mac_address'];
  50. $entry_if = $entry['interface_id'];
  51. if (!$mac_table[$entry_if][$entry_mac])
  52. {
  53. mysql_query("DELETE FROM ipv4_mac WHERE interface_id = '".$entry_if."' AND mac_address = '".$entry_mac."'");
  54. if ($debug) { echo("Removing MAC $entry_mac from interface ".$interface['ifName']); }
  55. echo("-");
  56. }
  57. }
  58. echo("\n");
  59. unset($mac);
  60. ?>