Import RRD data into Python Data Structures
Does anyone have a good method for importing rrd data into python? The only libraries I have found so far are just wrappers for the command line or provide importing data into rrd and graphing it.
I am aware of the export and dump options of rrd, but I am wondering if开发者_如何学C someone has already done the heavy lifting here.
Here's an excerpt from a script that I wrote to get cacti rrd data out. It's not likely to be exactly what you want, but it might give you a good start. The intent of my script is to turn Cacti into a data warehouse, so I tend to pull out a lot of averages, max, or min data. I also have some flags for tossing upper or lower range spikes, multiplying in case I want to turn "bytes/sec" into something more usable, like "mb/hour"...
If you want exact one-to-one data copy, you might need to tweak this a bit.
value_dict = {}
for file in files:
if file[0] == '':
continue
file = file[0]
value_dict[file] = {}
starttime = 0
endtime = 0
cmd = '%s fetch %s %s -s %s -e %s 2>&1' % (options.rrdtool, file, options.cf, options.start, options.end)
if options.verbose: print cmd
output = os.popen(cmd).readlines()
dsources = output[0].split()
if dsources[0].startswith('ERROR'):
if options.verbose:
print output[0]
continue
if not options.source:
source = 0
else:
try:
source = dsources.index(options.source)
except:
print "Invalid data source, options are: %s" % (dsources)
sys.exit(0)
data = output[3:]
for val in data:
val = val.split()
time = int(val[0][:-1])
val = float(val[source+1])
# make sure it's not invalid numerical data, and also an actual number
ok = 1
if options.lowerrange:
if val < options.lowerrange: ok = 0
if options.upperrange:
if val > options.upperrange: ok = 0
if ((options.toss and val != options.toss and val == val) or val == val) and ok:
if starttime == 0:
# this should be accurate for up to six months in the past
if options.start < -87000:
starttime = time - 1800
else:
starttime = time - 300
else:
starttime = endtime
endtime = time
filehash[file] = 1
val = val * options.multiply
values.append(val)
value_dict[file][time] = val
seconds = seconds + (endtime - starttime)
精彩评论