Last active
December 22, 2015 00:28
-
-
Save y0ug/e80e9197597a02a8b123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rrdtool | |
import os | |
import sys | |
import subprocess | |
import re | |
import time | |
import datetime | |
class GraphPing(object): | |
def __init__(self, target, reset=False): | |
self.filename_rrd = os.path.join('/tmp', 'ping-%s.rrd' % \ | |
target) | |
self.filename_graph = os.path.join('/tmp', 'ping-%s.png' % \ | |
target) | |
self.target = target | |
self.step = 30 | |
self.heartbeat = self.step * 2 | |
# 7 days of logs for the step of 2 by min | |
self.steps_db = 2 * (7 * 24 * 60 * 60) | |
if not os.path.isfile(self.filename_rrd) or reset: | |
self.create() | |
def create(self): | |
rrdtool.create(self.filename_rrd, | |
'--step', str(self.step), | |
'--start', 'N', | |
'DS:pl:GAUGE:%d:0:100' % (self.heartbeat), | |
'DS:rtt:GAUGE:%d:0:10000000' % (self.heartbeat), | |
'RRA:AVERAGE:0.5:1:%d' % (self.steps_db), | |
'RRA:MAX:0.5:1:%d' % (self.steps_db)) | |
def update(self, pl, rtt): | |
rrdtool.update(self.filename_rrd, | |
'N:%s:%s' % (pl, rtt)) | |
def graph(self): | |
rrdtool.graph(self.filename_graph, | |
'-a', 'PNG', | |
'--title', 'ping %s' % (self.target), | |
'--vertical-label', 'RTT (ms)', | |
'-h', '225', '-w', '600', | |
'--start', '-3600', | |
'--lower-limit', '0', | |
'--watermark', datetime.datetime.now().isoformat(), | |
#'--slope-mode', | |
#'--alt-y-grid', | |
#'--rigid', | |
'--x-grid', 'MINUTE:5:HOUR:1:MINUTE:30:0:%R', | |
'DEF:roundtrip=%s:rtt:AVERAGE' % (self.filename_rrd), | |
'DEF:packetloss=%s:pl:AVERAGE' % (self.filename_rrd), | |
'CDEF:PLNone=packetloss,0,0,LIMIT,UN,UNKN,INF,IF', | |
'CDEF:PL10=packetloss,1,10,LIMIT,UN,UNKN,INF,IF', | |
'CDEF:PL25=packetloss,10,25,LIMIT,UN,UNKN,INF,IF', | |
'CDEF:PL50=packetloss,25,50,LIMIT,UN,UNKN,INF,IF', | |
'CDEF:PL75=packetloss,50,75,LIMIT,UN,UNKN,INF,IF', | |
'CDEF:PL100=packetloss,75,100,LIMIT,UN,UNKN,INF,IF', | |
'AREA:roundtrip#0000ff:RTT (ms)', | |
'GPRINT:roundtrip:LAST:Cur\: %5.2lf', | |
'GPRINT:roundtrip:AVERAGE:Avg\: %5.2lf', | |
'GPRINT:roundtrip:MAX:Max\: %5.2lf', | |
r'GPRINT:roundtrip:MIN:Min\: %5.2lf\n', | |
'COMMENT:pkt loss (%)', | |
'AREA:PLNone#ffffff:0-1%:STACK', | |
'AREA:PL10#ffff00:1-10%:STACK', | |
'AREA:PL25#ffcc00:10-25%:STACK', | |
'AREA:PL50#ff8000:25-50%:STACK', | |
'AREA:PL75#ff0000:50-75%:STACK', | |
'AREA:PL100#bf0000:75-100%:STACK') | |
def ping(self, count, maxtime): | |
params = [ "ping", | |
"-c", str(count), | |
"-w", str(maxtime), | |
self.target] | |
p = subprocess.Popen(params, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = p.communicate() | |
# get RTT avg | |
m = re.search('(\d+.\d+)/(\d+.\d+)/(\d+.\d+)', out) | |
rtt = "0" | |
if m: rtt = m.group(2) | |
# get packet loss | |
m = re.search('(\d+)% packet loss', out) | |
pl = "100" | |
if m: pl = m.group(1) | |
return rtt, pl | |
def run(self): | |
while True: | |
start = time.time() | |
rtt, pl = self.ping(self.step, self.step) | |
print "%s, %s, %s, %s" % \ | |
(datetime.datetime.now().isoformat(), | |
self.target, rtt, pl) | |
self.update(pl, rtt) | |
self.graph() | |
stop = time.time() | |
remaning = self.step - (stop - start) | |
if remaning > 0: | |
time.sleep(remaning) | |
if __name__ == "__main__": | |
graph = GraphPing(sys.argv[1], | |
reset=False) | |
graph.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment