创建每个rrd文件的py模块如下,写的有点笨,有多少个不同的item组成的图形就得定义不同的创建函数,我找了一个下午都没有找到更好的传参方法,先
就用这个笨方法。记住有几种DS就需要写对应的Item函数,下面我只列出Item03,我是不会告诉你们我总共堆了24个一直到Item24
代码如下复制代码
#!/usr/bin/env python
#coding=utf-8
import rrdtool
def Item01(rrdname, startStamp, DS):
rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],
'RRA:AVERAGE:0.5:1:3000',
'RRA:AVERAGE:0.5:5:4200',
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/fuwuqi/)'RRA:AVERAGE:0.5:24:3800',
'RRA:AVERAGE:0.5:240:4400',
'RRA:MAX:0.5:1:3000',
'RRA:MAX:0.5:5:4200',
'RRA:MAX:0.5:24:3800',
'RRA:MAX:0.5:240:4400',
'RRA:MIN:0.5:1:3000',
'RRA:MIN:0.5:5:4200',
'RRA:MIN:0.5:24:3800',
'RRA:MIN:0.5:240:4400')
def Item02(rrdname, startStamp, DS):
rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],
DS[1],
'RRA:AVERAGE:0.5:1:3000',
'RRA:AVERAGE:0.5:5:4200',
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/fuwuqi/)'RRA:AVERAGE:0.5:24:3800',
'RRA:AVERAGE:0.5:240:4400',
'RRA:MAX:0.5:1:3000',
'RRA:MAX:0.5:5:4200',
'RRA:MAX:0.5:24:3800',
'RRA:MAX:0.5:240:4400',
'RRA:MIN:0.5:1:3000',
'RRA:MIN:0.5:5:4200',
'RRA:MIN:0.5:24:3800',
'RRA:MIN:0.5:240:4400')
def Item03(rrdname, startStamp, DS):
rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],
DS[1], DS[2],
'RRA:AVERAGE:0.5:1:3000',
'RRA:AVERAGE:0.5:5:4200',
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/fuwuqi/)'RRA:AVERAGE:0.5:24:3800',
'RRA:AVERAGE:0.5:240:4400',
'RRA:MAX:0.5:1:3000',
'RRA:MAX:0.5:5:4200',
'RRA:MAX:0.5:24:3800',
'RRA:MAX:0.5:240:4400',
'RRA:MIN:0.5:1:3000',
'RRA:MIN:0.5:5:4200',
'RRA:MIN:0.5:24:3800',
'RRA:MIN:0.5:240:4400')
基本创建rrd文件的模块写好了,然后组织数据、调用函数传递相应的参数就可以了,下面是我写的一个多线程程序,同时启用16个线程工作(CPU线程数的2倍)。可更改线程数适应不同的硬件环境。
代码如下复制代码
#!/usr/bin/env python
#coding=utf-8
from zabbixget import Zabbix
from time import ctime
import threading
import createsub
import os
import time, datetime
def rrd_create(grinfo):
#如果主机存放rrd图形目录不存在,则创建
bashdir = "/opt/rrd/"
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/fuwuqi/)path = bashdir + grinfo[0]['hostid'] + "/"
if not os.path.exists(path):
os.makedirs(path)
rrdname = str(path + grinfo[0]['graphid'] + '.rrd')
timeDaysAgo = (datetime.datetime.now() - datetime.timedelta(days = 730))
startStamp = str(int(time.mktime(timeDaysAgo.timetuple())))
DS = []
for sub in grinfo:
DStmp = str('DS:' + sub['itemid'] + ':GAUGE:120:0:U')
DS.append(DStmp)
if len(DS) == 1: createsub.Item01(rrdname, startStamp, DS)
elif len(DS) == 2: createsub.Item02(rrdname, startStamp, DS)
elif len(DS) == 3: createsub.Item03(rrdname, startStamp, DS)
elif len(DS) == 4: createsub.Item04(rrdname, startStamp, DS)
elif len(DS) == 5: createsub.Item05(rrdname, startStamp, DS)
elif len(DS) == 6: createsub.Item06(rrdname, startStamp, DS)
elif len(DS) == 7: createsub.Item07(rrdname, startStamp, DS)
elif len(DS) == 8: createsub.Item08(rrdname, startStamp, DS)
elif len(DS) == 9: createsub.Item09(rrdname, startStamp, DS)
elif len(DS) == 10: createsub.Item10(rrdname, startStamp, DS)
elif len(DS) == 11: createsub.Item11(rrdname, startStamp, DS)
elif len(DS) == 12: createsub.Item12(rrdname, startStamp, DS)
elif len(DS) == 13: createsub.Item13(rrdname, startStamp, DS)
elif len(DS) == 14: createsub.Item14(rrdname, startStamp, DS)
elif len(DS) == 15: createsub.Item15(rrdname, startStamp, DS)
elif len(DS) == 16: createsub.Item16(rrdname, startStamp, DS)
elif len(DS) == 17: createsub.Item17(rrdname, startStamp, DS)
elif len(DS) == 18: createsub.Item18(rrdname, startStamp, DS)
elif len(DS) == 19: createsub.Item19(rrdname, startStamp, DS)
elif len(DS) == 20: createsub.Item20(rrdname, startStamp, DS)
elif len(DS) == 21: createsub.Item21(rrdname, startStamp, DS)
elif len(DS) == 22: createsub.Item22(rrdname, startStamp, DS)
elif len(DS) == 23: createsub.Item23(rrdname, startStamp, DS)
elif len(DS) == 24: createsub.Item24(rrdname, startStamp, DS)
def rrd_update(rrdfile, data):
pass
def hosts_get():
global zb
zb = Zabbix()
hostsName = zb.hostsid_get()
return hostsName
#遍历每台主机每张图的每个项目的最新10的值,后来考虑到api的压力改成每张图的最新值(减少了一个for循环变量items)
def items_get(host):
bashdir = "/opt/rrd/"
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/fuwuqi/)graphsName = zb.hostgraph_get(host.keys())
for graph in graphsName:
grapitem = zb.graphitems_get(graph['graphid'])
rrdfile = bashdir + host.values()[0] + "/" + str(graph['graphid']) + '.rrd'
if os.path.isfile(rrdfile):
data = []
for item in grapitem:
tmp = {'itemid':item['itemid'],'lastvalue':item['lastvalue']}
data.append(tmp)
rrd_update(rrdfile, data)
print data
else:
grinfo = []
for item in grapitem:
tmp = {'hostid': host.values()[0], 'graphid': graph['graphid'], 'itemid': item['itemid']}
grinfo.append(tmp)
rrd_create(grinfo)
def main():
threads = []
keys = hosts_get()
numkey = len(keys)
loop = 0
for i in range(0, numkey, 16):
nkeys = range(loop*16, (loop+1)*16, 1)
for i in nkeys:
if i = numkey:
break
else:
t = threading.Thread(target=items_get, args=(keys[i],))
threads.append(t)
for i in nkeys:
if i = numkey:
break
else:
threads[i].start()
for i in nkeys:
if i = numkey:
break
threads[i].join()
loop = loop + 1
if __name__ == "__main__":
main()
上面还有更新数据的函数没有写完,用pass站位。过多的解释就不用了,对着相应的注释大家都应该看的懂。