http://elekslabs.com/2013/12/rrd-and-rrdtool-sar-graphs-using-pyrrd.html
http://thepiandi.blogspot.jp/2013/10/graphing-real-temperature-data-using.html
https://github.com/oubiwann-unsupported/pyrrd/tree/master/examples
https://hookrace.net/blog/server-statistics
http://qiita.com/kooshin/items/c032125157d79c222a4a
感觉效率比rrdtool模块低,尤其是在update的时候
create rrd
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pyrrd.rrd import RRD, RRA, DS
dss = []
rras = []
filename = 'memory.rrd'
ds1 = DS(dsName='buffer', dsType='GAUGE', heartbeat=120, minval='0', maxval='U')
ds2 = DS(dsName='cached', dsType='GAUGE', heartbeat=120, minval='0', maxval='U')
ds3 = DS(dsName='used', dsType='GAUGE', heartbeat=120, minval='0', maxval='U')
ds4 = DS(dsName='total', dsType='GAUGE', heartbeat=120, minval='0', maxval='U')
dss.extend([ds1, ds2, ds3, ds4])
rra_average_1 = RRA(cf='AVERAGE', xff=0.5, steps=1, rows=1440) # 60*60*24 / 60*1 (1d / (step * steps)
rra_average_2 = RRA(cf='AVERAGE', xff=0.5, steps=15, rows=672) # 60*60*24*7 / 60*15 (1w / (step * steps)
rra_average_3 = RRA(cf='AVERAGE', xff=0.5, steps=60, rows=744) # 60*60*24*31 / 60*60 (1m / (step * steps)
rra_average_4 = RRA(cf='AVERAGE', xff=0.5, steps=1440, rows=375) # 60*60*24*365 / 60*60*24 (1y / (step * steps)
rras.extend([rra_average_1, rra_average_2, rra_average_3, rra_average_4])
rra_min_1 = RRA(cf='MIN', xff=0.5, steps=1, rows=1440)
rra_min_2 = RRA(cf='MIN', xff=0.5, steps=15, rows=672)
rra_min_3 = RRA(cf='MIN', xff=0.5, steps=60, rows=744)
rra_min_4 = RRA(cf='MIN', xff=0.5, steps=1440, rows=375)
rras.extend([rra_min_1, rra_min_2, rra_min_3, rra_min_4])
rra_max_1 = RRA(cf='MAX', xff=0.5, steps=5, rows=1440)
rra_max_2 = RRA(cf='MAX', xff=0.5, steps=30, rows=672)
rra_max_3 = RRA(cf='MAX', xff=0.5, steps=120, rows=744)
rra_max_4 = RRA(cf='MAX', xff=0.5, steps=1440, rows=375)
rras.extend([rra_max_1, rra_max_2, rra_max_3, rra_max_4])
rra_last_1 = RRA(cf='LAST', xff=0.5, steps=5, rows=1440)
rra_last_2 = RRA(cf='LAST', xff=0.5, steps=30, rows=672)
rra_last_3 = RRA(cf='LAST', xff=0.5, steps=120, rows=744)
rra_last_4 = RRA(cf='LAST', xff=0.5, steps=1440, rows=375)
rras.extend([rra_last_1, rra_last_2, rra_last_3, rra_last_4])
rrd = RRD(filename, step=60, ds=dss, rra=rras, start='now-1y')
rrd.create(debug=True)
--------------------------------------------------------------
('memory.rrd', ['--start', u'now-1y', '--step', u'60', u'DS:buffer:GAUGE:120:0:U', u'DS:cached:GAUGE:120:0:U', u'DS:used:GAUGE:120:0:U', u'DS:total:GAUGE:120:0:U', u'RRA:AVERAGE:0.5:1:1440', u'RRA:AVERAGE:0.5:15:672', u'RRA:AVERAGE:0.5:60:744', u'RRA:AVERAGE:0.5:1440:375', u'RRA:MIN:0.5:5:600', u'RRA:MIN:0.5:30:720', u'RRA:MIN:0.5:120:750', u'RRA:MIN:0.5:1440:732', u'RRA:MAX:0.5:5:600', u'RRA:MAX:0.5:30:720', u'RRA:MAX:0.5:120:750', u'RRA:MAX:0.5:1440:732', u'RRA:LAST:0.5:5:600', u'RRA:LAST:0.5:30:720', u'RRA:LAST:0.5:120:750', u'RRA:LAST:0.5:1440:732'])
$ rrdtool fetch memory.rrd AVERAGE --start -1h
buffer cached used total
1468816440: -nan -nan -nan -nan
1468816500: -nan -nan -nan -nan
1468816560: -nan -nan -nan -nan
1468816620: -nan -nan -nan -nan
1468816680: -nan -nan -nan -nan
1468816740: -nan -nan -nan -nan
1468816800: -nan -nan -nan -nan
1468816860: -nan -nan -nan -nan
update rrd
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pyrrd.rrd import RRD
import datetime, time
import random
filename = 'memory.rrd'
total = 1024*1024*1024*16
rrd = RRD(filename)
now = datetime.datetime.now()
start = now - datetime.timedelta(hours=3)
start_time = int(time.mktime(start.timetuple()))
end_time = int(time.mktime(now.timetuple()))
for timestamp in xrange(start_time, end_time+60, 60):
buffer = random.randint(5, 10) * total / 100
cached = random.randint(60, 80) * total / 100
free = random.randint(5, 10) * total / 100
rrd.bufferValue(timestamp, buffer, cached, total - buffer - cached - free, total)
rrd.update(debug=True)
graph_rrd
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pyrrd.graph import DEF, CDEF, VDEF
from pyrrd.graph import LINE, AREA, GPRINT, COMMENT
from pyrrd.graph import ColorAttributes, Graph
rrdfile = 'memory.rrd'
imgfile = 'memory.png'
ca = ColorAttributes()
ca.back = '#333333'
ca.canvas = '#333333'
ca.shadea = '#000000'
ca.shadeb = '#111111'
ca.mgrid = '#CCCCCC'
ca.axis = '#FFFFFF'
ca.frame = '#AAAAAA'
ca.font = '#FFFFFF'
ca.arrow = '#FFFFFF'
g = Graph(imgfile, start='-3h', title='memory', vertical_label='Bytes', color=ca, width=480, height=200)
#g.x_grid='MINUTE:10:MINUTE:30:MINUTE:30:0:"%H:%M"'
#g.alt_y_grid=True
def_buffer = DEF(rrdfile=rrdfile, vname='buffer', dsName='buffer', cdef='AVERAGE')
def_cached = DEF(rrdfile=rrdfile, vname='cached', dsName='cached', cdef='AVERAGE')
def_used = DEF(rrdfile=rrdfile, vname='used', dsName='used', cdef='AVERAGE')
def_total = DEF(rrdfile=rrdfile, vname='total', dsName='total', cdef='AVERAGE')
g.data.extend([def_buffer, def_cached, def_used, def_total])
vdef_buffer_min = VDEF(vname='buffer_min', rpn='%s,MINIMUM' % 'buffer')
vdef_buffer_max = VDEF(vname='buffer_max', rpn='%s,MAXIMUM' % 'buffer')
vdef_buffer_avg = VDEF(vname='buffer_avg', rpn='%s,AVERAGE' % 'buffer')
vdef_buffer_last = VDEF(vname='buffer_last', rpn='%s,LAST' % 'buffer')
g.data.extend([vdef_buffer_min, vdef_buffer_max, vdef_buffer_avg, vdef_buffer_last])
vdef_cached_min = VDEF(vname='cached_min', rpn='%s,MINIMUM' % 'cached')
vdef_cached_max = VDEF(vname='cached_max', rpn='%s,MAXIMUM' % 'cached')
vdef_cached_avg = VDEF(vname='cached_avg', rpn='%s,AVERAGE' % 'cached')
vdef_cached_last = VDEF(vname='cached_last', rpn='%s,LAST' % 'cached')
g.data.extend([vdef_cached_min, vdef_cached_max, vdef_cached_avg, vdef_cached_last])
vdef_used_min = VDEF(vname='used_min', rpn='%s,MINIMUM' % 'used')
vdef_used_max = VDEF(vname='used_max', rpn='%s,MAXIMUM' % 'used')
vdef_used_avg = VDEF(vname='used_avg', rpn='%s,AVERAGE' % 'used')
vdef_used_last = VDEF(vname='used_last', rpn='%s,LAST' % 'used')
g.data.extend([vdef_used_min, vdef_used_max, vdef_used_avg, vdef_used_last])
vdef_total_min = VDEF(vname='total_min', rpn='%s,MINIMUM' % 'total')
vdef_total_max = VDEF(vname='total_max', rpn='%s,MAXIMUM' % 'total')
vdef_total_avg = VDEF(vname='total_avg', rpn='%s,AVERAGE' % 'total')
vdef_total_last = VDEF(vname='total_last', rpn='%s,LAST' % 'total')
g.data.extend([vdef_total_min, vdef_total_max, vdef_total_avg, vdef_total_last])
line_buffer = LINE(1, defObj=def_buffer, color='#FFFF00', legend='buffer')
line_cached = LINE(1, defObj=def_cached, color='#339933', legend='cached')
line_used = LINE(1, defObj=def_used, color='#FF6666', legend='used')
line_total = LINE(1, defObj=def_total, color='#0066CC', legend='total')
gcomment = COMMENT('\n', autoNewline=False)
gprint_buffer_min = GPRINT(vdef_buffer_min, 'MAX:%7.2lf %sB')
gprint_buffer_max = GPRINT(vdef_buffer_max, 'MIN:%7.2lf %sB')
gprint_buffer_avg = GPRINT(vdef_buffer_avg, 'AVG:%7.2lf %sB')
gprint_buffer_last = GPRINT(vdef_buffer_last, 'LAST:%7.2lf %sB')
g.data.extend([line_buffer, gprint_buffer_min, gprint_buffer_max, gprint_buffer_avg, gprint_buffer_last, gcomment])
gprint_cached_min = GPRINT(vdef_cached_min, 'MAX:%7.2lf %sB')
gprint_cached_max = GPRINT(vdef_cached_max, 'MIN:%7.2lf %sB')
gprint_cached_avg = GPRINT(vdef_cached_avg, 'AVG:%7.2lf %sB')
gprint_cached_last = GPRINT(vdef_cached_last, 'LAST:%7.2lf %sB')
g.data.extend([line_cached, gprint_cached_min, gprint_cached_max, gprint_cached_avg, gprint_cached_last, gcomment])
gprint_used_min = GPRINT(vdef_used_min, 'MAX:%7.2lf %sB')
gprint_used_max = GPRINT(vdef_used_max, 'MIN:%7.2lf %sB')
gprint_used_avg = GPRINT(vdef_used_avg, 'AVG:%7.2lf %sB')
gprint_used_last = GPRINT(vdef_used_last, 'LAST:%7.2lf %sB')
g.data.extend([line_used, gprint_used_min, gprint_used_max, gprint_used_avg, gprint_used_last, gcomment])
gprint_total_min = GPRINT(vdef_total_min, 'MAX:%7.2lf %sB')
gprint_total_max = GPRINT(vdef_total_max, 'MIN:%7.2lf %sB')
gprint_total_avg = GPRINT(vdef_total_avg, 'AVG:%7.2lf %sB')
gprint_total_last = GPRINT(vdef_total_last, 'LAST:%7.2lf %sB')
g.data.extend([line_total, gprint_total_min, gprint_total_max, gprint_total_avg, gprint_total_last, gcomment])
g.write(debug=False)