从mysql导出数据生成rrd,再根据rrd生成图形 _MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1809 | 回复: 0   主题: 从mysql导出数据生成rrd,再根据rrd生成图形         下一篇 
大红薯
注册用户
等级:少校
经验:1440
发帖:159
精华:0
注册:2011-7-21
状态:离线
发送短消息息给大红薯 加好友    发送短消息息给大红薯 发消息
发表于: IP:您无权察看 2014-12-17 9:42:59 | [全部帖] [楼主帖] 楼主

[root@ish-ffbmcn42 ~]# cat rrd_graph.py
#!/usr/bin/python
import subprocess,os,sys
import MySQLdb
rrdtool='/usr/bin/rrdtool'
# get the rrd data from mysql server and build the rrd database
#
# check the date info to make sure get the correct info from command line
#
myrrd = []
graph_start = sys.argv[1]
graph_end   = sys.argv[2]
numbers = '0123456789'
if len(graph_start) <> 8 or len(graph_end) <> 8:
print "wrong date format,use 4 digit for year and 2 digit for month,2 digit for day: such as 20100101 "
sys.exit(1)
for a in graph_start:
if a not in numbers:
print "bad graph_start date format ,use 20100101 for 2010-01-01"
sys.exit(2)
for b in graph_end:
if b not in numbers:
print "bad graph_end date format,use 20100101 for 2010-01-01 "
sys.exit(2)
if int(graph_start) > int(graph_end):
print "bad sequence ,graph_start should be less than graph_end"
sys.exit(3)
start_new = graph_start[0:4]+"-"+graph_start[4:6]+"-"+graph_start[6:]
end_new   = graph_end[0:4]+"-"+graph_end[4:6]+"-"+graph_end[6:]
print start_new,end_new
start_seco = subprocess.Popen(["date","-d",start_new,"+%s"],stdout=subprocess.PIPE).communicate()[0]
start_second = int(start_seco)
end_seco = subprocess.Popen(["date","-d",end_new,"+%s"],stdout=subprocess.PIPE).communicate()[0]
end_second = int(end_seco)+ 86400
print start_second
print end_second
#commnnicate with the mysql server
conn = MySQLdb.connect( host="localhost",user="root",passwd="passw0rd",db="rrd")
cursor =conn.cursor()
sql =" select * from lps where second_id between %d and %d " %(start_second,end_second)
cursor.execute(sql)
while(1):
row=cursor.fetchone()
if row == None:
break
else:
myrrd.append(row)
cursor.close()
conn.commit()
conn.close()
#
# handle the sql query result
#
# RRA1 for day report ,and RRA2 for week ,RRA3 month ,RRA for quarter !
DS1 = "DS:LPS1:GAUGE:70:U:U"
DS2 = "DS:LPS2:GAUGE:70:U:U"
DS3 = "DS:LPS3:GAUGE:70:U:U"
DS4 = "DS:LPS4:GAUGE:70:U:U"
RRA1 = "RRA:MAX:0.5:1:1450"
RRA2 = "RRA:MAX:0.5:5:2010"
RRA3 = "RRA:MAX:0.5:30:1450"
RRA4 = "RRA:MAX:0.5:120:1200"
rrd_file='/root/'+'From'+str(start_new)+'To'+str(end_new)+'.rrd'
print rrd_file
if os.path.exists(rrd_file):
subprocess.Popen(["rm", "-f",rrd_file],stdout=subprocess.PIPE).communicate()
subprocess.Popen([rrdtool,"create",rrd_file,"--step","60","--start", str(start_second),DS1,DS2,DS3,DS4,RRA1,RRA2,RRA3,RRA4],stdout=subprocess.PIPE).communicate()
#start update the date to the RRD file#
for a in myrrd:
if len(a) == 5:
second_id=a[0]
lps=a[1]
lps2=a[2]
lps3=a[3]
lps4=a[4]
record=str(second_id)+":"+str(lps)+":"+str(lps2)+":"+str(lps3)+":"+str(lps4)
#       print record
subprocess.Popen([rrdtool,"update",rrd_file,record],stdout=subprocess.PIPE).communicate()
graph_file = 'From_'+str(start_new)+'_TO_'+str(end_new)+'.png'
if os.path.exists(graph_file):
subprocess.Popen(["rm", "-f",graph_file],stdout=subprocess.PIPE).communicate()
vtitle =str(start_new)+'_TO_'+str(end_new)+'Trend'
def1 ='DEF:t1='+rrd_file+':LPS1:MAX'
def2 ='DEF:t2='+rrd_file+':LPS2:MAX'
def3 ='DEF:t3='+rrd_file+':LPS3:MAX'
def4 ='DEF:t4='+rrd_file+':LPS4:MAX'
myline1='LINE1:t1#FF0000:"LPS1"'
myline2='LINE1:t2#00FF00:"LPS2"'
myline3='LINE1:t3#FF00FF:"LPS3"'
myline4='LINE1:t4#0000FF:"LPS4"'
subprocess.Popen([rrdtool,"graph",graph_file,"--title",vtitle,def1,def2,def3,def4,myline1,myline2,myline3,myline4,"-s", str(start_second),"-e",str(end_second)],stdout=subprocess.PIPE).communicate()
[root@ish-ffbmcn42 ~]#


可以实现日报表,时间一样即可采用rrd1值,
可以实现周报表,时间间隔为一周采用rrd2值,
可以实现月报表,时间间隔为一月,采用rrd3值
可以实现季度报表,时间间隔为一月,采用rrd4值

LINE[width]:value[#color][:[legend][:STACK]][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]]


LINE后面的数字是象数,代表画图的精细

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论