Jason R. Coombs avatar Jason R. Coombs committed 13d6d82

Add a routine for monitoring hosts

Comments (0)

Files changed (1)

jaraco/net/icmp.py

+from __future__ import print_function
+
+import sys
 import socket
 import select
 import struct
 import operator
 import random
 import datetime
+import functools
 
 from jaraco.util.timing import Stopwatch
 
 		bytes = ''.join((bytes, '\x00'))
 	n_values = len(bytes)/2
 	values = struct.unpack('%dH' % n_values, bytes)
-	sum = reduce(operator.add, values)
+	sum = functools.reduce(operator.add, values)
 	sum = (sum >> 16) + (sum & 0xffff)
 	sum += (sum >> 16)
 	return (~sum) & 0xffff
 			pass
 	return datetime.datetime.utcnow()
 
+def monitor_cmd():
+	try:
+		monitor_hosts(sys.argv[1:])
+	except KeyboardInterrupt:
+		pass
+
+def monitor_hosts(hosts):
+	while True:
+		for host in hosts:
+			try:
+				delay = ping(host)
+			except socket.timeout:
+				delay = None
+			save_result(host, delay)
+		time.sleep(3)
+
+def save_result(host, delay):
+	with open('ping-results.txt', 'a') as res:
+		ts = datetime.datetime.now()
+		msg = 'time: {ts}, host: {host}, res: {delay}'.format(
+			ts=ts, host=host, delay=delay)
+		print(msg, file=res)
+
+
+if __name__ == '__main__':
+	monitor_cmd()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.