diff options
Diffstat (limited to 'tools/rrd_graphs/rrd-plot.py')
-rwxr-xr-x | tools/rrd_graphs/rrd-plot.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/tools/rrd_graphs/rrd-plot.py b/tools/rrd_graphs/rrd-plot.py new file mode 100755 index 0000000..0a1d719 --- /dev/null +++ b/tools/rrd_graphs/rrd-plot.py @@ -0,0 +1,101 @@ +#!/usr/bin/python + +import rrdtool + +# create RRD database +def create_db(start, end, rrd_db='ts.rrd'): + # DS says that this is a data set. Check rrd manpage for details. + # RRA directive defines how many values the the RRD database will archive and + # for how long. + # + # MAX normally means to only accept the maximum value if multiple values + # are available over multiple "steps". We are using MAX simply to say that + # we have variable which will contain one number and it should not be + # changed. + # + # 0.5 is an internal resolution value and should _not_ be changed. + # + # + # 1 specifies how many steps should be averaged before storing the value. + # 1500 is how many "steps" we will store in the db. + # 1 seconds (--step 1) we will store 1500 samples times 1 seconds + # Total 1500 seconds. + + data_sources = ['DS:tdiff:GAUGE:2:0:10000000000', + 'DS:pps:GAUGE:2:0:100000000000', + 'RRA:MAX:0.5:1:1500'] + + startime = long(start) - 1 + rrdtool.create(rrd_db, '--step', '1', '--start', str(startime), data_sources) + return 0 + +# Update rrd databses with logged data. +# data format epoch:avg time between samples:value +def update_db(pkg_log='pkg-gen-stats', rrd_db='ts.rrd'): + ret = 0 + try: + with open(pkg_log, 'r') as f: + for line in f: + epoch_time, tdiff, pps = line.split(":") + ret = rrdtool.update(rrd_db, '%s:%s:%lu' % (epoch_time, tdiff, + long(pps))) + except Exception as e: + print(e) + + + return ret + +def plot_db(start, end, output, rrd_db='ts.rrd'): + ret = 0 + plot_sources = ['DEF:pps=sample_rrd:pps:MAX', + 'AREA:pps#00FF00:Packets per second', + 'GPRINT:pps:MAX:Max\: %lf%spps', + 'GPRINT:pps:MIN:Min\: %lf%spps', + 'GPRINT:pps:AVERAGE:Avg\: %lf%spps'] + + plot_sources = [x.replace('sample_rrd', rrd_db) for x in plot_sources] + + ret = rrdtool.graph(output, + "--start", start, + "--end", end, + '--slope-mode', + '--title', "Netmap packets per second", + "--vertical-label=PPS", + '-w' '1024', '-h', '384', + '--lower-limit', '0', + '--x-grid', 'SECOND:15:MINUTE:1:SECOND:10:0:%T', + #GTM:GST:MTM:MST:LTM:LST:LPR:LFM + plot_sources) + + return ret + +# Open file and get first-last time from epoch +# @start: first entry +# @end: last entry +def get_tstamps(pkg_log='pkg-gen-stats'): + ret = (-1, -1) + + # FIXME add checks for failures + try: + with open(pkg_log, 'r') as f: + start_time_arr = f.readline().split(":") + end_time_arr = f.readlines()[-1].split(":") + except Exception as e: + print(e) + return ret + + start = start_time_arr[0] + end = end_time_arr[0] + + return (start, end) + +def main(): + output = 'graph.png' + start, end = get_tstamps() + create_db(start, end) + update_db() + plot_db(start,end, output) + print("Saved {}") . format(output) + +if __name__ == '__main__': + main() |