Skip to content

Instantly share code, notes, and snippets.

@zircote
Created August 6, 2013 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zircote/6169375 to your computer and use it in GitHub Desktop.
Save zircote/6169375 to your computer and use it in GitHub Desktop.
ping stats
import argparse
import ConfigParser
import subprocess
import shlex
import re
import time
'''Ping Class to monitor arbitrary ping times
'''
class Ping():
hostname = "localhost"
def __init__(self, hostname="localhost"):
self.hostname = hostname.strip()
def ping_host(self):
command = 'ping -c 1 %s' % self.hostname
args = shlex.split(command)
out = subprocess.check_output(args)
result = re.search('time=([0-9]+\.[0-9]+)', out)
return float(result.group(1))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="the config file to use example: [ --config=/etc/ping.ini ]", default=None)
parser.add_argument("-f", "--frequency", help="the frequency to poll example:[ -f 10 ]", default=60, type=float)
parser.add_argument("--hosts", help="the hosts to poll example:[ --hosts=localhosts,myserver.com ]", default="localhost", type=str)
args = parser.parse_args()
if args.config is not None:
config = ConfigParser.RawConfigParser()
config.read(args.config)
hosts = config.get('ping', 'hosts')
frequency = config.getfloat('ping', 'frequency')
else:
hosts = args.hosts
frequency = args.frequency
ping_hosts = []
for h in hosts.split(','):
ping_hosts.append(Ping(hostname=h))
while True:
for h in ping_hosts:
print "%s.ping" % h.hostname, h.ping_host()
time.sleep(frequency)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment