pinger.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env -S php
  2. <?php
  3. #-
  4. # Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. # SUCH DAMAGE.
  27. #
  28. #
  29. include_once '/usr/local/www/apache22/data/db.inc.php';
  30. include_once 'swreg.inc.php';
  31. require_once 'MDB2.php';
  32. require_once 'Log.php';
  33. $dsn = "mysql://$db_user:$db_pass@$db_host/$db_name";
  34. $options = array('result_buffering', false);
  35. $dbh = MDB2::factory($dsn, $options);
  36. if (PEAR::isError($dbh)) {
  37. die($dbh->getMessage());
  38. }
  39. $dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);
  40. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Pinger');
  41. if ($logger === FALSE) {
  42. die("Failed to open logfile.\n");
  43. }
  44. $mask = Log::UPTO(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';
  48. $res = $dbh->query($sql);
  49. if (PEAR::isError($res)) {
  50. $logger->crit("Failed to execute '$sql': {$res->getUserInfo()}");
  51. exit(1);
  52. }
  53. while ($row = $res->fetchRow()) {
  54. $status = 0;
  55. $output = NULL;
  56. $result = 0;
  57. exec("$FPING -q -C 1 {$row['ip_address']} 2>/dev/null", $output, $result);
  58. exec("$FPING -q -C 1 {$row['ip_address']} 2>/dev/null", $output, $result);
  59. if ($result == 0) {
  60. $status = 2;
  61. } else {
  62. if ($row['status'] == 2 || $row['status'] == 1) {
  63. $status = 1;
  64. }
  65. }
  66. $sql = 'UPDATE DEVICE_MAP SET device_status = ? WHERE serial_number = ?';
  67. $sth = $dbh->prepare($sql);
  68. if (PEAR::isError($sth)) {
  69. $logger->error("Failed to prepapre query '$sql' : {$sth->getUserInfo()}");
  70. continue;
  71. }
  72. $lres = $sth->execute(array($status, $row['serial_number']));
  73. $sth->free();
  74. if (PEAR::isError($lres)) {
  75. $logger->error("Failed to execute query '$sql' : {$lres->getUserInfo()}");
  76. continue;
  77. }
  78. }
  79. $dbh->disconnect();
  80. $logger->close();
  81. ?>