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

/includes/discovery/ports.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 69 lines | 53 code | 10 blank | 6 comment | 18 complexity | 5b405232bf8f0e7470a00b2185b41ad6 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. # Discover ports
  3. echo("Ports : ");
  4. $ports = array();
  5. $ports = snmpwalk_cache_oid($device, "ifDescr", $ports, "IF-MIB");
  6. #$ports = snmpwalk_cache_oid($device, "ifName", $ports, "IF-MIB");
  7. #$ports = snmpwalk_cache_oid($device, "ifType", $ports, "IF-MIB");
  8. $interface_ignored = 0;
  9. $interface_added = 0;
  10. foreach ($ports as $ifIndex => $port)
  11. {
  12. if (is_port_valid($port, $device))
  13. {
  14. if ($device['os'] == "vmware" && preg_match("/Device ([a-z0-9]+) at .*/", $port['ifDescr'], $matches)) { $port['ifDescr'] = $matches[1]; }
  15. $port['ifDescr'] = fixifName($port['ifDescr']);
  16. if ($debug) echo("\n $if ");
  17. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) == '0')
  18. {
  19. mysql_query("INSERT INTO `ports` (`device_id`,`ifIndex`,`ifDescr`) VALUES ('".$device['device_id']."','$ifIndex','".mres($port['ifDescr'])."')");
  20. # Add Interface
  21. echo("+");
  22. } else {
  23. mysql_query("UPDATE `ports` SET `deleted` = '0' WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'");
  24. echo(".");
  25. }
  26. $int_exists[] = "$ifIndex";
  27. } else {
  28. # Ignored Interface
  29. if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) != '0')
  30. {
  31. mysql_query("UPDATE `ports` SET `deleted` = '1' WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'");
  32. # Delete Interface
  33. echo("-"); ## Deleted Interface
  34. } else {
  35. echo("X"); ## Ignored Interface
  36. }
  37. }
  38. }
  39. $sql = "SELECT * FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `deleted` = '0'";
  40. $query = mysql_query($sql);
  41. while ($test_if = mysql_fetch_assoc($query))
  42. {
  43. unset($exists);
  44. $i = 0;
  45. while ($i < count($int_exists) && !isset($exists))
  46. {
  47. $this_if = $test_if['ifIndex'];
  48. if ($int_exists[$i] == $this_if) { $exists = 1; }
  49. $i++;
  50. }
  51. if (!$exists)
  52. {
  53. echo("-");
  54. mysql_query("UPDATE `ports` SET `deleted` = '1' WHERE interface_id = '" . $test_if['interface_id'] . "'");
  55. }
  56. }
  57. unset($temp_exists);
  58. echo("\n");
  59. ?>