pinger.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env -S php
  2. <?php
  3. //-
  4. // Copyright (c) 2011-2016 Joe Clarke <jclarke@cisco.com>
  5. // All rights reserved.
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. // 1. Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // 2. Redistributions in binary form must reproduce the above copyright
  12. // notice, this list of conditions and the following disclaimer in the
  13. // documentation and/or other materials provided with the distribution.
  14. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. // SUCH DAMAGE.
  25. include_once '../db.inc.php';
  26. include_once 'swreg.inc.php';
  27. require_once '../functions.php';
  28. require_once 'Log.php';
  29. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  30. $options = [
  31. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  32. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  33. PDO::ATTR_EMULATE_PREPARES => false,
  34. ];
  35. try {
  36. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  37. } catch (PDOException $e) {
  38. die($e->getMessage());
  39. }
  40. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Pinger');
  41. if ($logger === false) {
  42. die("Failed to open logfile.\n");
  43. }
  44. $mask = Log::MAX(LOG_LEVEL);
  45. $logger->setMask($mask);
  46. $FPING = FPING_PROGRAM;
  47. $sql = 'SELECT ps.serial_number AS serial_number, ls.ip_address AS ip_address, ps.device_status AS status FROM DEVICE_MAP ps, SWITCHES ls WHERE ps.assigned_switch = ls.name AND ps.provisioned_status=?';
  48. $sth = null;
  49. try {
  50. $sth = $dbh->prepare($sql);
  51. $sth->execute(array(PROVISION_SUCCESS));
  52. } catch (PDOException $e) {
  53. $logger->crit("Failed get list of provisioned devices: {$e->getMessage()}");
  54. exit(1);
  55. }
  56. while ($sth !== null && $row = $sth->fetch()) {
  57. $status = REACHABILITY_NEVER_REACHABLE;
  58. $output = null;
  59. $result = 0;
  60. exec("$FPING -q -C 1 {$row['ip_address']} 2>/dev/null", $output, $result);
  61. exec("$FPING -q -C 1 {$row['ip_address']} 2>/dev/null", $output, $result);
  62. if ($result == 0) {
  63. $status = REACHABILITY_REACHABLE;
  64. if ($row['status'] != REACHABILITY_REACHABLE) {
  65. call_hook("PINGER:REACHABLE", array($row['serial_number'], $row['ip_address']));
  66. }
  67. } else {
  68. if ($row['status'] == REACHABILITY_REACHABLE || $row['status'] == REACHABILITY_NOW_UNREACHABLE) {
  69. $status = REACHABILITY_NOW_UNREACHABLE;
  70. if ($row['status'] == REACHABILITY_REACHABLE) {
  71. call_hook("PINGER:UNREACHABLE", array($row['serial_number'], $row['ip_address']));
  72. }
  73. }
  74. }
  75. $sql = 'UPDATE DEVICE_MAP SET device_status = ? WHERE serial_number = ?';
  76. try {
  77. $isth = $dbh->prepare($sql);
  78. $isth->execute(array($status, $row['serial_number']));
  79. $isth->closeCursor();
  80. } catch (PDOException $e) {
  81. $logger->error("Failed to update switch status : {$e->getMessage()}");
  82. continue;
  83. }
  84. }
  85. $sth->closeCursor();
  86. $logger->close();
  87. ?>