RRDtool in Python the drawing function is Rrdtool.graph (), the function of the parameters of what the meaning is not to speak, mainly talk about ideas, the following affixed to the drawing letter
Number of code will be understood. It is a problem to draw these pictures in the ocean of thousands of surveillance charts in the shortest and most general way. I am currently due to the level of constraints can be thought of to solve the problem is this
Kind of.
1, all graphics have a limited item, the same number of item pictures with the same function to draw.
2, some special graphics, such as memory stack diagram (stacks), you can write a judgment statement in the function to configure the corresponding drawing parameters.
3, regarding the item color and the drawing Way (Line1,line2,area), needs the special care the graph to be possible to define in the drawdef this table, other graph's item uses automatically the default to be good.
4, tens of thousands of item drawings how to actually find the corresponding DS in the Rrd file, get the correct data. This was first designed, and each host's RRD files are placed in a text
folder, the folders are named after the host ID, each RRD file is named with a graphics ID and with the. rrd suffix, each rrd file's DS is named with Itemid. This will make it possible to find the data correctly.
The source.
Once you have solved these logical problems, you can heap the drawing code.
First locate the RRD file you want to draw based on the HostID and type of the GET request
Rrdtooldraw02
The function that handles the views of this GET request is this:
def zabbixdraw (Request):
Dir = r "/opt/rrd/"
Pngdir = r "/opt/www/diewu/static/images/rrdpng/"
#value = Request. Get
HostID = Request. get[' HostID ']
Type = Request. get[' type ']
sql = "Select HostID, Graphid, Graphname, hostname from Zabbixapp_drawtree where hostid=" +hostid+ "and Type=" +type+ "and D raw= ' 1 ' "
graphs = DrawTree.objects.getclass (SQL)
PNGs = []
Gdatas = []
Strtime = str (int (time.time ()-86400))
For graph in graphs:
HostID = graph[0]
Graphid = graph[1]
Graphname = graph[2]
hostname = Graph[3]
rpath = dir + HostID + R "/" + Graphid + R ". Rrd"
############################################
If not os.path.exists (Pngdir + HostID + R "/"):
Os.makedirs (Pngdir + HostID + R "/")
Pngname = Pngdir + HostID + R "/" + Graphid + R ". png"
sql = "Select Itemid,itemname,units from zabbixapp_drawgraphs where graphid=" +graphid
Pitem = DrawGraphs.objects.getdata (SQL)
# # # # #取自定义颜色和绘制方法
sql = "SELECT cols from Zabbixapp_drawdef where graphid=" +graphid
cols = DrawDef.objects.getdata (SQL)
If cols:
cols = (Cols[0][0].split (":"),)
sql = "Select types from Zabbixapp_drawdef where graphid=" +graphid
itypes = DrawDef.objects.getdata (SQL)
If Itypes:
Itypes = (Itypes[0][0].split (":"),)
Gdata = {' PName ':p ngname, ' gname ': graphname, ' Rrdpath ': rpath, ' Pitem ':p item, ' Graphid ': Graphid,
' cols ': cols, ' itypes ': itypes, ' host ': hostname, ' stime ': strtime, ' flag ': ' Daily '}
Gdatas.append (Gdata)
Pngs.append ({' Pngpath ': str (pngname). Replace (R "/opt/www/diewu", ""), ' Graphid ': Graphid})
Drawrrd.drawmain (Gdatas)
#value = Gdatas
#avg = {' Privatetitle ': ' Zabbix monitor data display ', ' static_url ': '/static ', ' value ': value, ' PNGs ':p NGS}
Avg = {' Privatetitle ': ' Zabbix monitor data display ', ' static_url ': '/static ', ' PNGs ':p NGS}
Return Render_to_response (' zabbixapp/zabbixdraw.html ', avg)
The custom drawing function Drawrrd.drawmain () function is mainly used to traverse the drawing, according to the number of items provided by the graph data, and then call the corresponding drawing function to draw the corresponding PNG graphics.
Paste the real drawing function, draw a function of the item graph and draw 2 item graphics.
#!/usr/bin/env python
#coding =utf-8
Import RRDtool
def dItem01 (data):
Pngname = str (data[' pname '))
Start = data[' Stime ']
Graphname = str (data[' gname '] + ("+ data[' Graphid '] +") "+ data[' host ' +" ("+ data[' flag '] +") "
DEF = str (r "def:a=" +data[' Rrdpath ']+r ': ' +data[' pitem '][0][0]+r ': AVERAGE ')
If data[' cols '] or data[' itypes ':
If not data[' cols ':
Dtype = str (data[' itypes '][0][0]+r ": a#eaaf00ff:" +data[' Pitem '][0][1])
Elif not data[' itypes ']:
Dtype = str (r "area:a" +data[' cols '][0][0]+r ":" +data[' Pitem '][0][1])
Else
Dtype = str (data[' itypes '][0][0]+r ": A" +data[' cols '][0][0]+r ":" +data[' Pitem '][0][1])
Else
Dtype = str (r "area:a#eaaf00ff:" +data[' Pitem '][0][1])
Unit = str (data[' Pitem '][0][2])
If not unit:
Unit = '
max = ' Gprint:a:max:max\:%.2lf%s '
min = ' Gprint:a:min:min\:%.2lf%s '
Avg = ' Gprint:a:average:avg\:%.2lf%s '
now = ' Gprint:a:last:now\:%.2lf%s '
Rrdtool.graph (Pngname, '-w ', ' ', ', '-h ', ' 144 ', '-l ', ' 0 ', '-s ', start,
'-t ', graphname, '-V ', Unit, DEF, ' comment:\\n ', Dtype, now, Avg, Min, max, ' comment:\\n '
############################################################################################################### ####
def dItem02 (data):
Pngname = str (data[' pname '))
Start = data[' Stime ']
Graphname = str (data[' gname '] + ("+ data[' Graphid '] +") "+ data[' host ' +" ("+ data[' flag '] +") "
DEFa = str (r "def:a=" +data[' Rrdpath ']+r ': ' +data[' pitem '][0][0]+r ': AVERAGE ")
DEFB = str (r "def:b=" +data[' Rrdpath ']+r ': ' +data[' pitem '][1][0]+r ': AVERAGE ")
Unit = str (data[' Pitem '][0][2])
If not unit:
Unit = '
If data[' cols '] or data[' itypes ':
If not data[' cols ':
Dtypea = str (data[' itypes '][0][0]+r ": a#00cf00ff:" +data[' Pitem '][0][1])
DTYPEB = str (data[' itypes '][0][1]+r ": b#002a97ff:" +data[' Pitem '][1][1])
Elif not data[' itypes ']:
Dtypea = str (r "area:a" +data[' cols '][0][0]+r ":" +data[' Pitem '][0][1])
DTYPEB = str (r "line1:b" +data[' cols '][0][1]+r ":" +data[' Pitem '][1][1])
Else
Dtypea = str (data[' itypes '][0][0]+r ": A" +data[' cols '][0][0]+r ":" +data[' Pitem '][0][1])
DTYPEB = str (data[' itypes '][0][1]+r ": B" +data[' cols '][0][1]+r ' ":" +data[' Pitem '][1][1])
Else
Dtypea = str (r "area:a#00cf00ff:" +data[' Pitem '][0][1])
DTYPEB = str (r "line1:b#002a97ff:" +data[' Pitem '][1][1])
Maxa = ' Gprint:a:max:max\:%.2lf%s '
Mina = ' Gprint:a:min:min\:%.2lf%s '
AVGA = ' Gprint:a:average:avg\:%.2lf%s '
Nowa = ' Gprint:a:last:now\:%.2lf%s '
MAXB = ' Gprint:b:max:max\:%.2lf%s '
MINB = ' Gprint:b:min:min\:%.2lf%s '
AVGB = ' Gprint:b:average:avg\:%.2lf%s '
NOWB = ' Gprint:b:last:now\:%.2lf%s '
Rrdtool.graph (Pngname, '-w ', ' ', ', '-h ', ' 144 ', '-l ', ' 0 ', '-s ', start,
'-t ', graphname, '-V ', Unit, DEFa, DEFB, ' comment:\\n ', Dtypea, Nowa, AVGA, Mina, Maxa, ' comment:\\n ',
Dtypeb, NOWB, AVGB, Minb, MAXB, ' comment:\\n ')
If there are 24 item graphics need to draw, it seems that I do not think of a better method, had to slowly carefully heap code. If you think of a better way, please let me know. Thank you!