Zabbix与RRDtool绘图篇(2)_创建每台主机每张图的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
1#!/usr/bin/env python
2#coding=utf-8
3import rrdtool
4def Item01(rrdname, startStamp, DS):
5 rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],
6 'RRA:AVERAGE:0.5:1:3000',
7 'RRA:AVERAGE:0.5:5:4200',
8 'RRA:AVERAGE:0.5:24:3800',
9 'RRA:AVERAGE:0.5:240:4400',
10 'RRA:MAX:0.5:1:3000',
11 'RRA:MAX:0.5:5:4200',
12 'RRA:MAX:0.5:24:3800',
13 'RRA:MAX:0.5:240:4400',
14 'RRA:MIN:0.5:1:3000',
15 'RRA:MIN:0.5:5:4200',
16 'RRA:MIN:0.5:24:3800',
17 'RRA:MIN:0.5:240:4400')
18def Item02(rrdname, startStamp, DS):
19 rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],
20 DS[1],
21 'RRA:AVERAGE:0.5:1:3000',
22 'RRA:AVERAGE:0.5:5:4200',
23 'RRA:AVERAGE:0.5:24:3800',
24 'RRA:AVERAGE:0.5:240:4400',
25 'RRA:MAX:0.5:1:3000',
26 'RRA:MAX:0.5:5:4200',
27 'RRA:MAX:0.5:24:3800',
28 'RRA:MAX:0.5:240:4400',
29 'RRA:MIN:0.5:1:3000',
30 'RRA:MIN:0.5:5:4200',
31 'RRA:MIN:0.5:24:3800',
32 'RRA:MIN:0.5:240:4400')
33def Item03(rrdname, startStamp, DS):
34 rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],
35 DS[1], DS[2],
36 'RRA:AVERAGE:0.5:1:3000',
37 'RRA:AVERAGE:0.5:5:4200',
38 'RRA:AVERAGE:0.5:24:3800',
39 'RRA:AVERAGE:0.5:240:4400',
40 'RRA:MAX:0.5:1:3000',
41 'RRA:MAX:0.5:5:4200',
42 'RRA:MAX:0.5:24:3800',
43 'RRA:MAX:0.5:240:4400',
44 'RRA:MIN:0.5:1:3000',
45 'RRA:MIN:0.5:5:4200',
46 'RRA:MIN:0.5:24:3800',
47 'RRA:MIN:0.5:240:4400')
基本创建rrd文件的模块写好了,然后组织数据、调用函数传递相应的参数就可以了,下面是我写的一个多线程程序,同时启用16个线程工作(CPU线程数的2倍)。可更改线程数适应不同的硬件环境。
1#!/usr/bin/env python
2#coding=utf-8
3from zabbixget import Zabbix
4from time import ctime
5import threading
6import createsub
7import os
8import time, datetime
9def rrd_create(grinfo):
10 #如果主机存放rrd图形目录不存在,则创建
11 bashdir = "/opt/rrd/"
12 path = bashdir + grinfo[0]['hostid'] + "/"
13 if not os.path.exists(path):
14 os.makedirs(path)
15 rrdname = str(path + grinfo[0]['graphid'] + '.rrd')
16 timeDaysAgo = (datetime.datetime.now() - datetime.timedelta(days = 730))
17 startStamp = str(int(time.mktime(timeDaysAgo.timetuple())))
18 DS = []
19 for sub in grinfo:
20 DStmp = str('DS:' + sub['itemid'] + ':GAUGE:120:0:U')
21 DS.append(DStmp)
22 if len(DS) == 1: createsub.Item01(rrdname, startStamp, DS)
23 elif len(DS) == 2: createsub.Item02(rrdname, startStamp, DS)
24 elif len(DS) == 3: createsub.Item03(rrdname, startStamp, DS)
25 elif len(DS) == 4: createsub.Item04(rrdname, startStamp, DS)
26 elif len(DS) == 5: createsub.Item05(rrdname, startStamp, DS)
27 elif len(DS) == 6: createsub.Item06(rrdname, startStamp, DS)
28 elif len(DS) == 7: createsub.Item07(rrdname, startStamp, DS)
29 elif len(DS) == 8: createsub.Item08(rrdname, startStamp, DS)
30 elif len(DS) == 9: createsub.Item09(rrdname, startStamp, DS)
31 elif len(DS) == 10: createsub.Item10(rrdname, startStamp, DS)
32 elif len(DS) == 11: createsub.Item11(rrdname, startStamp, DS)
33 elif len(DS) == 12: createsub.Item12(rrdname, startStamp, DS)
34 elif len(DS) == 13: createsub.Item13(rrdname, startStamp, DS)
35 elif len(DS) == 14: createsub.Item14(rrdname, startStamp, DS)
36 elif len(DS) == 15: createsub.Item15(rrdname, startStamp, DS)
37 elif len(DS) == 16: createsub.Item16(rrdname, startStamp, DS)
38 elif len(DS) == 17: createsub.Item17(rrdname, startStamp, DS)
39 elif len(DS) == 18: createsub.Item18(rrdname, startStamp, DS)
40 elif len(DS) == 19: createsub.Item19(rrdname, startStamp, DS)
41 elif len(DS) == 20: createsub.Item20(rrdname, startStamp, DS)
42 elif len(DS) == 21: createsub.Item21(rrdname, startStamp, DS)
43 elif len(DS) == 22: createsub.Item22(rrdname, startStamp, DS)
44 elif len(DS) == 23: createsub.Item23(rrdname, startStamp, DS)
45 elif len(DS) == 24: createsub.Item24(rrdname, startStamp, DS)
46def rrd_update(rrdfile, data):
47 pass
48def hosts_get():
49 global zb
50 zb = Zabbix()
51 hostsName = zb.hostsid_get()
52 return hostsName
53#遍历每台主机每张图的每个项目的最新10的值,后来考虑到api的压力改成每张图的最新值(减少了一个for循环变量items)
54def items_get(host):
55 bashdir = "/opt/rrd/"
56 graphsName = zb.hostgraph_get(host.keys())
57 for graph in graphsName:
58 grapitem = zb.graphitems_get(graph['graphid'])
59 rrdfile = bashdir + host.values()[0] + "/" + str(graph['graphid']) + '.rrd'
60 if os.path.isfile(rrdfile):
61 data = []
62 for item in grapitem:
63 tmp = {'itemid':item['itemid'],'lastvalue':item['lastvalue']}
64 data.append(tmp)
65 rrd_update(rrdfile, data)
66 print data
67 else:
68 grinfo = []
69 for item in grapitem:
70 tmp = {'hostid': host.values()[0], 'graphid': graph['graphid'], 'itemid': item['itemid']}
71 grinfo.append(tmp)
72 rrd_create(grinfo)
73def main():
74 threads = []
75 keys = hosts_get()
76 numkey = len(keys)
77 loop = 0
78 for i in range(0, numkey, 16):
79 nkeys = range(loop*16, (loop+1)*16, 1)
80 for i in nkeys:
81 if i >= numkey:
82 break
83 else:
84 t = threading.Thread(target=items_get, args=(keys[i],))
85 threads.append(t)
86 for i in nkeys:
87 if i >= numkey:
88 break
89 else:
90 threads[i].start()
91 for i in nkeys:
92 if i >= numkey:
93 break
94 threads[i].join()
95 loop = loop + 1
96if __name__ == "__main__":
97 main()
上面还有更新数据的函数没有写完,用pass站位。过多的解释就不用了,对着相应的注释大家都应该看的懂。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/zabbix-rrdtool-2/4108.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.