Skip to content

Instantly share code, notes, and snippets.

@sourceperl
Created December 11, 2014 08:42
Show Gist options
  • Save sourceperl/c806639dd141c5de4a16 to your computer and use it in GitHub Desktop.
Save sourceperl/c806639dd141c5de4a16 to your computer and use it in GitHub Desktop.
Find peak in value of an RRDtool base.
import rrdtool
import datetime
# retrieve RRD data (3 days past from now)
rrd = rrdtool.fetch('/home/pi/rrd/flow.rrd', 'AVERAGE', '-s -3d')
# timestamp start/end
start = rrd[0][0]
end = rrd[0][1]
step = rrd[0][2]
# array of data
data = rrd[2]
# c is current value, last_c is c at cycle -1
c = None
last_c = None
def ts2str(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
# start/stop timestamp banner
print("start: %d (%s), end %d (%s)" %(start, ts2str(start), end, ts2str(end)))
print("")
# display only value with high change rate
for i, item in enumerate(data):
if item[0] is not None:
c = item[0]
if last_c is None:
last_c = c
chg_rate = 100*abs(c - last_c)/c
# display value if change rate more than 10%
if (chg_rate > 10.0):
tstamp = (i*step) + start
print("%s: %f (%.1f%% last %.2f)" % (ts2str(tstamp), item[0], chg_rate, last_c))
last_c = c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment