status_api.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. //-
  3. // Copyright (c) 2011-2023 Joe Clarke <jclarke@marcuscom.com>
  4. // All rights reserved.
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions
  7. // are met:
  8. // 1. Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // 2. Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. // SUCH DAMAGE.
  24. include_once 'db.inc.php';
  25. include_once 'swreg/swreg.inc.php';
  26. include_once 'swreg_creds.inc.php';
  27. require_once 'Log.php';
  28. require_once 'functions.php';
  29. function error_to_exception($severity, $message, $file, $line)
  30. {
  31. if (!(error_reporting() & $severity)) {
  32. return;
  33. }
  34. throw new ErrorException($message, 0, $severity, $file, $line);
  35. }
  36. set_error_handler('error_to_exception');
  37. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  38. $options = [
  39. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  40. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  41. PDO::ATTR_EMULATE_PREPARES => false,
  42. ];
  43. header("Content-type:application/json;charset=utf-8");
  44. try {
  45. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  46. } catch (PDOException $e) {
  47. http_response_code(500);
  48. echo json_encode(["error" => $e->getMessage()]);
  49. }
  50. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : API');
  51. if ($logger === false) {
  52. http_response_code(500);
  53. echo json_encode(["error" => "Failed to open logfile."]);
  54. }
  55. $mask = Log::MAX(LOG_LEVEL);
  56. $logger->setMask($mask);
  57. $sql = "SELECT * FROM DEVICE_MAP WHERE checked_out='1' AND (device_status='1' OR device_status='2') ORDER BY LPAD(lower(assigned_switch), 10, 0)";
  58. $res = null;
  59. try {
  60. $res = $dbh->query($sql);
  61. } catch (PDOException $e) {
  62. $msg = "Error querying for physical switches: {$e->getMessage()}";
  63. $logger->error($msg);
  64. http_response_code(500);
  65. echo json_encode(["error" => $msg]);
  66. exit(1);
  67. }
  68. $infra = [];
  69. while ($row = $res->fetch()) {
  70. $target = [];
  71. $target['name'] = $row['assigned_switch'];
  72. $sql = 'SELECT ip_address, location, pid FROM SWITCHES WHERE name = :name';
  73. $sw_res = null;
  74. try {
  75. $sw_res = $dbh->prepare($sql);
  76. $sw_res->execute(['name' => $row['assigned_switch']]);
  77. } catch (PDOException $e) {
  78. $logger->warning("Failed to get details for {$row['assigned_switch']}: {$e->getMessage()}");
  79. continue;
  80. }
  81. $sw_row = $sw_res->fetch();
  82. if ($sw_row['location'] === null || $sw_row['location'] == '') {
  83. continue;
  84. }
  85. $target['ipv4'] = $sw_row['ip_address'];
  86. $target['location'] = $sw_row['location'];
  87. $target['pid'] = $sw_row['pid'];
  88. $target['platform'] = "ietfcisco";
  89. array_push($infra, $target);
  90. }
  91. echo json_encode($infra);
  92. cleanup();
  93. ?>