Created
January 20, 2009 07:52
-
-
Save qingfeng/49391 to your computer and use it in GitHub Desktop.
rrdtool example. Project move to http://github.com/qingfeng/rrdtool_server/tree/master
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import rrdtool | |
def create_rrd(fname): | |
DSs = ['DS:in:GAUGE:600:U:U','DS:out:GAUGE:600:U:U'] | |
RRAs = [ | |
'RRA:AVERAGE:0.5:1:603', | |
'RRA:AVERAGE:0.5:6:603', | |
'RRA:AVERAGE:0.5:24:603', | |
'RRA:AVERAGE:0.5:288:800', | |
'RRA:MAX:0.5:1:603', | |
'RRA:MAX:0.5:6:603', | |
'RRA:MAX:0.5:24:603', | |
'RRA:MAX:0.5:288:800', | |
] | |
rrdtool.create(fname, | |
'--step', '300', | |
'--start', '1232582400', | |
*(DSs + RRAs) | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# encoding: utf-8 | |
import rrdtool | |
FONT_PATH = 'msyh.ttf' | |
def draw(defs): | |
rrdtool.graph('data.png', | |
'--imgformat', 'PNG', | |
#'--step', str(self.range/2), | |
'--start', 'now-32h', | |
'--width', '700', | |
'--height', '200', | |
'--title', '播客后台流量', | |
'--vertical-label', 'bit/s', | |
# '--x-grid', xgrid, | |
'-n', 'DEFAULT:10:%s' % FONT_PATH, | |
*defs) | |
if __name__ == '__main__': | |
defs = [ | |
'DEF:port1=rrd_files/fc332e4a33cc154bb15d079a0edcf9c0.rrd:out:MAX', | |
'AREA:port1#FF0000:西单网通', | |
'DEF:port2=rrd_files/ffddcf48e3536414965114ca655cfdd5.rrd:in:MAX', | |
'AREA:port2#00FF00:皂君庙', | |
] | |
draw(defs) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
rrdtool graph data.png \ | |
--title "Xx流量" \ | |
DEF:port1=data.rrd:port1:MAX \ | |
AREA:port1#FF0000:"port1" \ | |
DEF:port2=data.rrd:port2:MAX \ | |
AREA:port2#00FF00:"port2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
from __future__ import with_statement | |
from datetime import datetime,timedelta | |
from create_data import create_rrd | |
from decimal import Decimal | |
import os | |
import time | |
import hashlib | |
import MySQLdb | |
import rrdtool | |
import syslog | |
PATH = '/home/yanxu/rrdtool_ex/rrd_files/' | |
def log(msg): | |
#syslog.syslog(syslog.LOG_NOTICE|syslog.LOG_USER,msg) | |
print msg | |
def mktime(savetime): | |
""" | |
>>> mktime(datetime.strptime('2009-01-21 14:00:00',"%Y-%m-%d %H:%M:%S")) | |
1232546400.0 | |
""" | |
return time.mktime( savetime.timetuple() ) | |
def now(): | |
d = datetime.now()-timedelta(0,5*60) | |
return datetime(d.year,d.month,d.day, | |
d.hour,(d.minute-d.minute%5),0) | |
def update(fname,timestamp,traff_in,traff_out): | |
fname = os.path.join(PATH,fname) | |
print timestamp,traff_in,traff_out | |
try: | |
rrdtool.update(fname, | |
'%s:%s:%s' % ( timestamp,-traff_in*Decimal(1000),traff_out*Decimal(1000) ) | |
) | |
log("update data: %s" % fname) | |
except: | |
log("create data: %s" % fname) | |
create_rrd(fname) | |
rrdtool.update(fname, | |
'%s:%s:%s' % ( timestamp,traff_in,traff_out ) | |
) | |
if __name__ == '__main__': | |
#import doctest | |
#doctest.testmod() | |
nowtime = now() | |
log("nowtime: %s" % nowtime) | |
conn = MySQLdb.connect(user='root', | |
passwd='', | |
host='10.88.15.61', | |
db='realdb') | |
with conn as cursor: | |
cursor.execute("SET NAMES 'utf8'") | |
sql=''' | |
SELECT sys,idc,savetime,traff_in,traff_out | |
FROM report_product | |
WHERE savetime = %s | |
''' | |
cursor.execute(sql,(nowtime,)) | |
for sv,idc,savetime,traff_in,traff_out in cursor.fetchall(): | |
fname = "%s.rrd" % hashlib.md5( "%s_%s" % (sv,idc) ).hexdigest() | |
print sv,idc,savetime,traff_in,traff_out,fname | |
update(fname,mktime(savetime),traff_in,traff_out) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment