Now let's take a look at a rrd file article about Zabbix and RRDtool drawing to create each of each of the hosts, and hopefully this example will help you.
RRDtool for the graphics show how excellent, presumably understand the people know.
The troops did not move the fodder first. The manual for it is first rrdtool the official manual address http://oss.oetiker.ch/rrdtool/doc/index.en.html also has ailms the "RRDtool Simplified Chinese Course v1.01" The textbook is easy to understand, Worship First! The basic view of the Alims tutorial on the RRDtool clear.
I create each host each RRD file with a multithreaded, traversing each picture of the time of about 12 seconds, the thousand figure rrd file created about 2 minutes, mainly by the limitations of the hardware, if
Having faster hard drives and more threads should be faster. RRD file categories refer to the cacti style, create RRD files by Host ID folder (HostID), the name of each picture
Consists of a graphics ID and a. rrd suffix (graphid.rrd).
The PY module to create each RRD file is as follows, write a bit stupid, how many different item shapes have to define a different creation function, I have been looking for an afternoon did not find a better way to pass the argument, first
Just use this stupid method. Remember that there are several DS that need to write the corresponding item function, I'll just list Item03, I'm not going to tell you that I've piled up 24 to Item24
Code to copy the code as follows
#!/usr/bin/env python
#coding =utf-8
Import RRDtool
def Item01 (Rrdname, Startstamp, DS):
Rrdtool.create (rrdname, '--step ', ' o ', '--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 ',
' 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 ', ' o ', '--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 ',
' 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 ', ' o ', '--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 ',
' 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 ')
The module that basically creates the Rrd file is written, then organizes the data, the call function passes the corresponding parameter to be OK, below is I write a multithreaded program, simultaneously enables 16 threads to work (twice times CPU thread number). You can change the number of threads to accommodate different hardware environments.
Code to copy the code as follows
#!/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图形目录不存在, you create
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的值, and then take into account the pressure of the API to change to the latest value of each graph (reduced by A For loop variable 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 ()
There is also the function of updating the data is not finished, with pass position. Too much explanation is not necessary, to the corresponding annotation everyone should see understand.