get_logicsw_details.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. //-
  3. // Copyright (c) 2017 Joe Clarke <jclarke@cisco.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. require_once 'Log.php';
  27. require_once 'functions.php';
  28. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  29. $options = [
  30. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  31. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  32. PDO::ATTR_EMULATE_PREPARES => false,
  33. ];
  34. try {
  35. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  36. } catch (PDOException $e) {
  37. die($e->getMessage());
  38. }
  39. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : AJAX Get Logical Switch Details');
  40. if ($logger === false) {
  41. die("Failed to open logfile.\n");
  42. }
  43. $mask = Log::MAX(LOG_LEVEL);
  44. $logger->setMask($mask);
  45. $sql = 'SELECT name, location, is_idf, ip_address, pid FROM SWITCHES';
  46. $sth = null;
  47. try {
  48. $sth = $dbh->query($sql);
  49. } catch (PDOException $e) {
  50. header('HTTP/1.1 500 Internal Server Error');
  51. header('Content-Type: application/json');
  52. echo json_encode(array('status' => 'ERROR', 'message' => "Failed to get switch details: '{$e->getMessage()}'"));
  53. $logger->crit("Failed to get switch details: '{$e->getMessage()}'");
  54. exit(1);
  55. }
  56. header('Content-Type: application/json');
  57. $result = array();
  58. $result['status'] = 'SUCCESS';
  59. $result['response'] = array();
  60. while ($row = $sth->fetch()) {
  61. $tbase = '';
  62. if (file_exists(PORT_TMPL_DIR . '/devices/' . "{$row['name']}-ports.tmpl")) {
  63. $targ = readlink(PORT_TMPL_DIR . '/devices/' . "{$row['name']}-ports.tmpl");
  64. $tbase = basename($targ);
  65. }
  66. $result['response'][$row['name']] = array(
  67. 'location' => (is_null($row['location']) ? '' : $row['location']),
  68. 'is_idf' => $row['is_idf'],
  69. 'ip_address' => (is_null($row['ip_address']) ? '' : $row['ip_address']),
  70. 'pid' => (is_null($row['pid']) ? '' : $row['pid']),
  71. 'exception' => $tbase
  72. );
  73. }
  74. echo json_encode($result);
  75. cleanup();