From f60b7c4fc8e8da78db81b09f53a8ce24091c2de2 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 23 Sep 2014 21:30:27 +0200 Subject: plot.py: template support for rrd creation; argmax support Signed-off-by: Florian Pritz --- plot.py | 72 ++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/plot.py b/plot.py index 6d9d304..a8dcc6d 100755 --- a/plot.py +++ b/plot.py @@ -13,6 +13,7 @@ import sys import subprocess import os +from string import Template if len(sys.argv) != 3: print("Usage: plot.py ", file=sys.stderr) @@ -21,6 +22,7 @@ if len(sys.argv) != 3: log_file = sys.argv[1] png_file = sys.argv[2] rrd_file = "tmp.rrd" +argmax = 50000 #### start of config @@ -28,6 +30,12 @@ rrd_file = "tmp.rrd" # Also note that samples have to be in the same order every time (the key is currently ignored). samples_per_line=3 +template_values = { + # 0 = everything from the input + # otherwise = only the last n + "total_samples": 0, +} + # The 4th field of the DS lines is the heartbeat. If you sample at an # interval < that value you will not see a graph. Simply adjust if necessary. create = [ @@ -39,9 +47,9 @@ create = [ 'DS:temp2:GAUGE:50:U:U', 'DS:hum1:GAUGE:50:U:U', - 'RRA:AVERAGE:0.5:1:20000', - 'RRA:MIN:0.5:1:20000', - 'RRA:MAX:0.5:1:20000', + 'RRA:AVERAGE:0.5:1:$total_samples', + 'RRA:MIN:0.5:1:$total_samples', + 'RRA:MAX:0.5:1:$total_samples', ] graph = [ @@ -72,45 +80,55 @@ graph = [ #### end of config +def chunker(seq, size): + return (seq[pos:pos + size] for pos in range(0, len(seq), size)) + +calc_total_from_input = False +if template_values["total_samples"] == 0: + calc_total_from_input = True # load samples samples = [] -f = open(log_file,"r") -for line in f: - elem = line.split(' ') - values = [] - # timestamp - values.append(int(elem[0])) - # values - for i in range(1, samples_per_line + 1): - values.append(float(elem[i].split("=")[1])) - samples.append(values) -f.close() - +with open(log_file,"r") as f: + for line in f: + if calc_total_from_input: + template_values["total_samples"] += 1 + + elem = line.split(' ') + values = [] + # timestamp + values.append(int(elem[0])) + # values + for i in range(1, samples_per_line + 1): + values.append(float(elem[i].split("=")[1])) + samples.append(values) # create rrd start_ts = samples[0][0] end_ts = samples[-1][0] create[6] = str(start_ts - 1) + +cmd = [] +for elem in create: + cmd.append(Template(elem).substitute(template_values)) #print("creating rrdb:",create) -ret = subprocess.call(create) +ret = subprocess.call(cmd) if ret != 0: #print("ERROR calling: {}".format(" ".join(create))) sys.exit(1) # update rrd -# TODO: multiple calls in case of too many arguments -n = len(samples) -cmd = ['rrdupdate', rrd_file, '--'] -for i in range(n): - entry = ":".join(map(str, samples[i])) - cmd.append(entry); - -ret = subprocess.call(cmd) -if ret != 0: - #print("ERROR calling: {}".format(" ".join(cmd))) - sys.exit(1) +for group in chunker(samples, argmax): + cmd = ['rrdupdate', rrd_file, '--'] + for sample in group: + entry = ":".join(map(str, sample)) + cmd.append(entry); + + ret = subprocess.call(cmd) + if ret != 0: + #print("ERROR calling: {}".format(" ".join(cmd))) + sys.exit(1) # graph -- cgit v1.2.3-24-g4f1b