Skip to content

Instantly share code, notes, and snippets.

@dkilcy
Created January 23, 2015 00:08
Show Gist options
  • Save dkilcy/9c9dd0e4d69a3b18cb81 to your computer and use it in GitHub Desktop.
Save dkilcy/9c9dd0e4d69a3b18cb81 to your computer and use it in GitHub Desktop.
RRDtool sample program 1
import calendar
import datetime
import random
import rrdtool
import time
from rrdtool import update as rrd_update
current_time = 0
start = 0
filename = None
def __init__(self):
self.filename = "data1.rrd"
#current_time = calendar.timegm(time.gmtime())
#current_time = time.time()
self.current_time = datetime.datetime.now()
one_week_ago = datetime.timedelta(weeks=1)
diff = current_time - one_week_ago
self.start = int(time.mktime(diff.timetuple()))
self.current_time = int(time.mktime(current_time.timetuple()))
print "current_time", self.current_time
print "start", self.start
def get_metrics(self):
return random.randint(1,10);
def do_work(self):
print "current_time", self.current_time
print "start", self.start
filename = "data1.rrd"
ret = rrdtool.create(filename, "--step", "300", "--start", str(self.start-300),
"DS:metric1:GAUGE:900:U:U",
"RRA:AVERAGE:0.5:1:2000",
"RRA:MAX:0.5:1:2000",
"RRA:LAST:0.5:1:2000" )
count = 0
t = self.start
while t < self.current_time:
metric = get_metrics()
print count, t, self.current_time, metric
ret = rrd_update(filename, '%d:%s' % (t, metric) )
t += 300
count += 1
print self.start, t
if __name__ == '__main__':
init()
do_work()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment