/utils/PingMonitor.py

https://github.com/ifreecarve/MOOS-python-utils · Python · 120 lines · 45 code · 27 blank · 48 comment · 7 complexity · 81be91e87dd0087753ca0a0586806fa5 MD5 · raw file

  1. #!/usr/bin/env python
  2. ###########################################################################
  3. #
  4. # Written in 2009 by Ian Katz <ijk5@mit.edu>
  5. # Terms: WTFPL (http://sam.zoy.org/wtfpl/)
  6. # See COPYING and WARRANTY files included in this distribution
  7. #
  8. ###########################################################################
  9. # this program provides an audible alert for any change in the
  10. # connected/disconnected state of a named host
  11. #
  12. # It takes 2 command line arguments:
  13. # 1. the hostname/IP
  14. # 2. the name to be spoken by the program as up/down
  15. ###
  16. #
  17. # note from Ian:
  18. #
  19. # these spoken phrases were crafted carefully to make good use of the
  20. # digital voice (i.e., not sound too cheesy). each voice needs its own
  21. # slight tweak of wording to minimize odd-sounding speech. this script
  22. # was verbally optimized for "voice_nitech_us_slt_arctic_hts".
  23. #
  24. #
  25. # i installed a bunch of voices as per
  26. # http://ubuntuforums.org/showthread.php?t=751169
  27. #
  28. # a script called install-speech-software.sh is provided
  29. # for your convenience
  30. #
  31. # this is my /etc/festival.scm
  32. #
  33. # (Parameter.set 'Audio_Method 'esdaudio)
  34. # (set! voice_default 'voice_rab_diphone)
  35. # (set! voice_default 'voice_nitech_us_rms_arctic_hts)
  36. # (set! voice_default 'voice_nitech_us_clb_arctic_hts)
  37. # (set! voice_default 'voice_nitech_us_slt_arctic_hts)
  38. #
  39. ###
  40. import sys
  41. import os
  42. import subprocess
  43. import time
  44. # simple ping
  45. def ping(ip):
  46. """pings IP"""
  47. return 0 == subprocess.call("ping -c 1 -W 1 %s" % ip,
  48. shell=True,
  49. stdout=open('/dev/null', 'w'),
  50. stderr=subprocess.STDOUT)
  51. # use the big boy words... text to speech!
  52. def say(things):
  53. print time.strftime("%H:%M:%S"), things
  54. """Speaks words"""
  55. subprocess.call("echo %s | festival --tts " % things,
  56. shell=True,
  57. stdout=open('/dev/null', 'w'),
  58. stderr=subprocess.STDOUT)
  59. class PingMonitor(object):
  60. def __init__(self):
  61. pass
  62. def main(self, remote_ip, name):
  63. self.remote_ip = remote_ip
  64. self.name = name
  65. #flags, states, and counters
  66. self.laststate = True
  67. self.i = 0
  68. # main mega loop
  69. while (True):
  70. is_up = ping(self.remote_ip)
  71. print remote_ip, self.i, "state is", is_up
  72. if not is_up:
  73. self.processDown()
  74. else: #OH GOOD
  75. self.processUp()
  76. self.laststate = is_up
  77. self.i = self.i + 1
  78. def processDown(self):
  79. #alerts for various moments; just a dip in the wave, or a submerge?
  80. if (self.laststate):
  81. say(self.name + " is down")
  82. def processUp(self):
  83. if not self.laststate:
  84. say(self.name + " is up")
  85. ######################################################################3
  86. #run it
  87. if __name__ == "__main__":
  88. if len(sys.argv) != 3:
  89. print "Usage: " + sys.argv[0] + "<IP> <name>"
  90. exit(1)
  91. monitor = PingMonitor()
  92. monitor.main(sys.argv[1], sys.argv[2])
  93. print "\n\n\nexited cleanly!\n\n"