下面这个Zabbix与RRDtool绘图之创建每台主机每张图的rrd文件教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!
【 tulaoshi.com - 服务器 】
下面我们一起来看一个关于Zabbix与RRDtool绘图之创建每台主机每张图的rrd文件文章,希望这个例子可以给你有帮助。
RRDtool对于图形展示有多优秀,想必懂的人都知道。
兵马未动粮草先行。搞IT的得手册先行RRDtool的官方手册地址http://oss.oetiker.ch/rrdtool/doc/index.en.html 还有ailms整理的RRDtool简体中文教程 v1.01 该教材通俗易懂,先膜拜下!基本看了alims的 那个教程就对RRDtool清楚了。
我创建每台主机每张rrd文件用的是多线程,遍历每张图的时间大概12秒的样子,千张图的rrd文件创建大概2分钟的样子,主要受硬件的限制,如果
有更快的硬盘和更多的线程应该会快不少。rrd文件分类参考了cacti的风格,创建的rrd文件按主机ID分文件夹存放(hostid),每张图的名称
由图形ID和.rrd后缀组成(graphid.rrd)。
创建每个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',
'RRA:AVERAGE:0.5:24:3800',
'RRA:AVERAGE:0.5:240:4400',
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/fuwuqi/)'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',
'RRA:AVERAGE:0.5:24:3800',
'RRA:AVERAGE:0.5:240:4400',
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/fuwuqi/)'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',
'RRA:AVERAGE:0.5:24:3800',
'RRA:AVERAGE:0.5:240:4400',
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/fuwuqi/)'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
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/fuwuqi/)import os
import time, datetime
def rrd_create(grinfo):
#如果主机存放rrd图形目录不存在,则创建
bashdir = "/opt/rrd/"
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/"
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站位。过多的解释就不用了,对着相应的注释大家都应该看的懂。
来源:http://www.tulaoshi.com/n/20160331/2051281.html
看过《Zabbix与RRDtool绘图之创建每台主机每张图的rrd文件》的人还看了以下文章 更多>>