def output(self, months=3): """Render out the image of the rrd""" def1 = DEF(rrdfile=self.datafile, vname='queue', dsName=self.ds1.name) line1 = LINE(defObj=def1, color='#01FF13', legend='Queue Depth', stack=True) # area1 = AREA(defObj=def1, color='#FFA902', legend='Bookmarks') # Let's configure some custom colors for the graph ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' # Now that we've got everything set up, let's make a graph start_date = time.mktime((today - timedelta(days=7)).timetuple()) end_date = time.mktime(today.timetuple()) g = Graph(self.outputfile, start=int(start_date), end=int(end_date), vertical_label='count', color=ca) g.data.extend([def1, line1]) if not os.path.exists(os.path.dirname(self.outputfile)): os.makedirs(os.path.dirname(self.outputfile)) g.write()
def graph_request(self, period='day'): def1 = DEF(rrdfile=self.rrdfile, vname='request', dsName="requests", cdef="AVERAGE") vdef1 = VDEF(vname='max', rpn='request,MAXIMUM') vdef2 = VDEF(vname='avg', rpn='request,AVERAGE') vdef3 = VDEF(vname='last', rpn='request,LAST') area1 = AREA(defObj=def1, color='#336600', legend='Requests') gprint1 = GPRINT(vdef1, "Max\\: %5.1lf %S") gprint2 = GPRINT(vdef2, "Avg\\: %5.1lf %S") gprint3 = GPRINT(vdef3, "Current\\: %5.1lf %Sreq/sec") ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' img = "request-%s.png" % period imgname = self.static_path +"/"+ img start = '-1'+period g = Graph(imgname, imgformat='PNG', step=start, vertical_label='request/sec', color=ca, width=700, height=150) g.data.extend([def1, vdef1, vdef2, vdef3, area1, gprint1, gprint2, gprint3]) g.write()
def drawsimplegraph(req, rrdpathname, rrd, ds, rra, height=600, width=1200, start='default', end='default' ): filename = Rrdpath.objects.get(name=str(rrdpathname)).path fullfilename = filename + '/' + rrd if os.path.isfile(fullfilename): rrdobj = RRD(fullfilename, mode='r') info = rrdobj.getData() ds = str(ds) rra = str(rra) #step = info['step'] if (start == 'default'): start = info['lastupdate'] - (7 * 86400) if (end == 'default'): end = info['lastupdate'] gitems = [] gitems.append(DEF(rrdfile=fullfilename, vname="fudge", dsName=ds)) gitems.append(CDEF(vname='kmh', rpn='%s' % gitems[0].vname)) gitems.append(AREA(defObj=gitems[1], color='#006600', legend=rrd+" "+str(start)+" "+str(end))) g = Graph('-', imgformat='png', start=str(start), end=str(end), vertical_label=ds) g.title = rrd + '/' + ds + '/' + rra g.full_size_mode = True g.only_graph = req.GET.get('onlygraph', False) g.no_legend = req.GET.get('nolegend', True) g.height = req.GET.get('height', 600) g.width = req.GET.get('width', 1200) g.data.extend(gitems) a = g.write() return HttpResponse(a,content_type="image/png")
def render(stringName, key, startTime, endTime): if debug: print "Enter Function render(filename, value)" #balken zeichnen def1 = DEF(rrdfile = baseDir + stringName + "_" + key + ".rrd", vname='kW', dsName="kW") #command fetches data from the rrd area1 = AREA(defObj=def1, color='#FFA902', legend='kW') #mittelwert linie zeichnen (muss noch berechnet werden line1 = LINE(value=100, color='#990000', legend='Average') # Let's configure some custom colors for the graph ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' # Now that we've got everything set up, let's make a graph #startTime = endTime - (10 * 60 * 60) #10h anzeigen, sollte noch variabel sein g = Graph(baseDir + stringName + "_" + key + ".png", start=startTime, end=endTime, vertical_label='data', color=ca) g.data.extend([def1, area1, line1]) g.width = 800 g.height = 400 g.write()
def rrd_stuff(): filename = 'test.rrd' #dataSource = from_file() #def1 = DEF(rrdfile=filename, vname='myspeed', # dsName=dataSource.name) def1 = DEF(rrdfile=filename, vname='myspeed', dsName='speed') cdef1 = CDEF(vname='kmh', rpn='%s,3600,*' % def1.vname) cdef2 = CDEF(vname='fast', rpn='kmh,100,GT,kmh,0,IF') cdef3 = CDEF(vname='good', rpn='kmh,100,GT,0,kmh,IF') vdef1 = VDEF(vname='mymax', rpn='%s,MAXIMUM' % def1.vname) vdef2 = VDEF(vname='myavg', rpn='%s,AVERAGE' % def1.vname) line1 = LINE(value=100, color='#990000', legend='Maximum Allowed') area1 = AREA(defObj=cdef3, color='#006600', legend='Good Speed') area2 = AREA(defObj=cdef2, color='#CC6633', legend='Too Fast') line2 = LINE(defObj=vdef2, color='#000099', legend='My Average', stack=True) gprint1 = GPRINT(vdef2, '%6.2lf kph') from pyrrd.graph import ColorAttributes ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' from pyrrd.graph import Graph graphfile = "rrdgraph.png" g = Graph(graphfile, start=920805000, end=920810000, vertical_label='km/h', color=ca) g.data.extend([def1, cdef1, cdef2, cdef3, vdef1, vdef2, line1, area1, area2, line2, gprint1]) g.write()
area3 = AREA(defObj=def3, color='#BD4902', legend='Raw Data 2') area4 = AREA(defObj=def4, color='#A32001', legend='Raw Data 1') line1 = LINE(defObj=vdef1, color='#01FF13', legend='Average', stack=True) # Let's configure some custom colors for the graph ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' # Now that we've got everything set up, let's make a graph startTime = endTime - 3 * month g = Graph(graphfile, start=startTime, end=endTime, vertical_label='data', color=ca) g.data.extend([def1, def2, def3, def4, vdef1, area4, area3, area2, area1]) g.write() g.filename = graphfileLg g.width = 800 g.height = 400 g.write()
line1 = LINE(value=100, color='#990000', legend='Maximum Allowed') area1 = AREA(defObj=cdef3, color='#006600', legend='Good Speed') area2 = AREA(defObj=cdef2, color='#CC6633', legend='Too Fast') line2 = LINE(defObj=vdef2, color='#000099', legend='My Average', stack=True) gprint1 = GPRINT(vdef2, '%6.2lf kph') from pyrrd.graph import ColorAttributes ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' from pyrrd.graph import Graph graphfile = "/tmp/rrdgraph.png" g = Graph(graphfile, start=920805000, end=920810000, vertical_label='km/h', color=ca) g.data.extend([ def1, cdef1, cdef2, cdef3, vdef1, vdef2, line1, area1, area2, line2, gprint1 ]) g.write()
def graph_connection(self, period='day'): def1 = DEF(rrdfile=self.rrdfile, vname='connections', dsName="connections", cdef="AVERAGE") def2 = DEF(rrdfile=self.rrdfile, vname='reading', dsName="reading", cdef="AVERAGE") def3 = DEF(rrdfile=self.rrdfile, vname='writing', dsName="writing", cdef="AVERAGE") def4 = DEF(rrdfile=self.rrdfile, vname='waiting', dsName="waiting", cdef="AVERAGE") # TOTAL vdef1 = VDEF(vname='max', rpn='connections,MAXIMUM') vdef2 = VDEF(vname='avg', rpn='connections,AVERAGE') vdef3 = VDEF(vname='last', rpn='connections,LAST') vdef4 = VDEF(vname='min', rpn='connections,MINIMUM') line1 = LINE(1, defObj=def1, color='#22FF22', legend='Total') gprint1 = GPRINT(vdef1, "Max\\: %5.1lf %S") gprint2 = GPRINT(vdef2, "Avg\\: %5.1lf %S") gprint3 = GPRINT(vdef3, "Current\\: %5.1lf %S") gprint4 = GPRINT(vdef4, "Min\\: %5.1lf %S\\n") # READING reading_vdef1 = VDEF(vname='rmax', rpn='reading,MAXIMUM') reading_vdef2 = VDEF(vname='ravg', rpn='reading,AVERAGE') reading_vdef3 = VDEF(vname='rlast', rpn='reading,LAST') reading_vdef4 = VDEF(vname='rmin', rpn='reading,MINIMUM') line2 = LINE(1, defObj=def2, color='#0022FF', legend='Reading') reading_gprint1 = GPRINT(reading_vdef1, "Max\\: %5.1lf %S") reading_gprint2 = GPRINT(reading_vdef2, "Avg\\: %5.1lf %S") reading_gprint3 = GPRINT(reading_vdef3, "Current\\: %5.1lf %S") reading_gprint4 = GPRINT(reading_vdef4, "Min\\: %5.1lf %S\\n") # writing writing_vdef1 = VDEF(vname='wmax', rpn='writing,MAXIMUM') writing_vdef2 = VDEF(vname='wavg', rpn='writing,AVERAGE') writing_vdef3 = VDEF(vname='wlast', rpn='writing,LAST') writing_vdef4 = VDEF(vname='wmin', rpn='writing,MINIMUM') line3 = LINE(1, defObj=def3, color='#FF0000', legend='Writing') writing_gprint1 = GPRINT(writing_vdef1, "Max\\: %5.1lf %S") writing_gprint2 = GPRINT(writing_vdef2, "Avg\\: %5.1lf %S") writing_gprint3 = GPRINT(writing_vdef3, "Current\\: %5.1lf %S") writing_gprint4 = GPRINT(writing_vdef4, "Min\\: %5.1lf %S\\n") # WAITING waiting_vdef1 = VDEF(vname='wamax', rpn='waiting,MAXIMUM') waiting_vdef2 = VDEF(vname='waavg', rpn='waiting,AVERAGE') waiting_vdef3 = VDEF(vname='walast', rpn='waiting,LAST') waiting_vdef4 = VDEF(vname='wamin', rpn='waiting,MINIMUM') line4 = LINE(1, defObj=def4, color='#00AAAA', legend='Waiting') waiting_gprint1 = GPRINT(waiting_vdef1, "Max\\: %5.1lf %S") waiting_gprint2 = GPRINT(waiting_vdef2, "Avg\\: %5.1lf %S") waiting_gprint3 = GPRINT(waiting_vdef3, "Current\\: %5.1lf %S") waiting_gprint4 = GPRINT(waiting_vdef4, "Min\\: %5.1lf %S\\n") ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' img = "connection-%s.png" % period imgname = self.static_path +"/"+ img start = '-1'+period g = Graph(imgname, imgformat='PNG', step=start, vertical_label='connections', color=ca, width=700, height=150) g.data.extend([def1, vdef1, vdef2, vdef3, vdef4, line1, gprint1, gprint2, gprint3, gprint4]) g.data.extend([def2, reading_vdef1, reading_vdef2, reading_vdef3, reading_vdef4, line2, reading_gprint1, reading_gprint2, reading_gprint3, reading_gprint4]) g.data.extend([def3, writing_vdef1, writing_vdef2, writing_vdef3, writing_vdef4, line3, writing_gprint1, writing_gprint2, writing_gprint3, writing_gprint4]) g.data.extend([def4, waiting_vdef1, waiting_vdef2, waiting_vdef3, waiting_vdef4, line4, waiting_gprint1, waiting_gprint2, waiting_gprint3, waiting_gprint4]) g.write()
def drawgraph(req, graphid): now = int(time.time()) ginfo = Rrdgraph.objects.get(pk=graphid) gobjects = GraphItems.objects.filter(graph__id=graphid).order_by('seq') gitems = [] secondsago = secsago(req) if secondsago == 0: secondsago = 3600 # secsago = ginfo.timespan end = int(req.GET.get('end', now)) start = int(req.GET.get('start', end - secondsago)) for gobject in gobjects: #cycle through graph items...need to order this above if gobject.itemtype == 'S': #Handle Static RRD DataSources rootdir = gobject.rrdds.rootdir.path subpath = gobject.rrdds.subpath rrdds = gobject.rrdds.ds filename = rootdir + '/' + gobject.rrdds.subpath rra = gobject.rra namesuff = str(gobject.seq) legendtext = subpath+" "+rrdds+" "+rra+ " " gitems.append(DEF(rrdfile=filename, vname='d'+namesuff, dsName=rrdds)) gitems.append(CDEF(vname='c'+namesuff, rpn='%s' % 'd'+namesuff)) linetype = gobject.linetype.upper() mycolor = '#' + gobject.color + gobject.transparency if linetype == 'A': gitems.append(AREA(value='c'+namesuff, color=mycolor, legend=legendtext, stack=gobject.stack)) elif linetype[:1] == 'L': gitems.append(LINE(linetype[-1:], 'c'+namesuff, color=mycolor, legend=legendtext, stack=gobject.stack)) else: gitems.append(LINE(0, 'c'+namesuff, color=mycolor, legend=legendtext, stack=gobject.stack)) if gobject.itemtype == 'R': #Handle Regex regtextarr = gobject.option_text.rsplit(' ',2) rrddslist = Rrdfiles.objects.filter(rootdir__name__regex=regtextarr[0]).filter(subpath__regex=regtextarr[1]).filter(ds__regex=regtextarr[2]) i = 0 colors = [] colorset = GraphItemColorCycleColor.objects.filter(name='weeee').order_by('seq') for x in colorset: colors.append(str('#' + x.color)) for rrdds in rrddslist: rootdir = rrdds.rootdir.path subpath = rrdds.subpath rrdds = rrdds.ds filename = rootdir + subpath rra = gobject.rra linetype = gobject.linetype.upper() mycolor = colors[i % len(colors)] namesuff = str(gobject.seq) + '_' + str(i) legendtext = subpath+" "+rrdds+" ("+rra+ ") " gitems.append(DEF(rrdfile=filename, vname='d'+namesuff, dsName=rrdds)) gitems.append(CDEF(vname='c'+namesuff, rpn='%s' % 'd'+namesuff)) if linetype == 'A': gitems.append(AREA(value='c'+namesuff, color=mycolor, legend=legendtext, stack=gobject.stack)) elif linetype[:1] == 'L': gitems.append(LINE(linetype[-1:], 'c'+namesuff, color=mycolor, legend=legendtext, stack=gobject.stack)) else: gitems.append(LINE(0, 'c'+namesuff, color=mycolor, legend=legendtext, stack=gobject.stack)) prnFmt = '%6.2lf' vdef = VDEF(vname='va'+namesuff, rpn='%s,AVERAGE' % ('d'+namesuff) ) gitems.append(vdef) gitems.append(GPRINT(vdef, ('Avg\:'+prnFmt))) vdef = VDEF(vname='vn'+namesuff, rpn='%s,MINIMUM' % ('d'+namesuff) ) gitems.append(vdef) gitems.append(GPRINT(vdef, ('Min\:'+prnFmt))) vdef = VDEF(vname='vx'+namesuff, rpn='%s,MAXIMUM' % ('d'+namesuff) ) gitems.append(vdef) gitems.append(GPRINT(vdef, ('Max\:'+prnFmt))) vdef = VDEF(vname='vl'+namesuff, rpn='%s,LAST' % ('d'+namesuff) ) gitems.append(vdef) gitems.append(GPRINT(vdef, ('LAST\:'+prnFmt+'\\n'))) #gitems.append(COMMENT('\\n', False)) i = i + 1 if gobject.itemtype == 'C': #Handle Custom CDEFS pass if gobject.itemtype == 'V': #Handle Custom VDEFs pass cs = req.GET.get('cs', ginfo.gcolorscheme) colsch = GraphColorScheme.objects.get(pk=cs) ca = ColorAttributes() ca.back = '#' + colsch.cback + colsch.tback ca.canvas = '#' + colsch.ccanvas + colsch.tcanvas ca.shadea = '#' + colsch.cshadea + colsch.tshadea ca.shadeb = '#' + colsch.cshadeb + colsch.tshadeb ca.mgrid = '#' + colsch.cmgrid + colsch.tmgrid ca.axis = '#' + colsch.caxis + colsch.taxis ca.frame = '#' + colsch.cframe + colsch.tframe ca.font = '#' + colsch.cfont + colsch.tfont ca.arrow = '#' + colsch.carrow + colsch.tarrow #make a pyrrd Graph object, destination standard out (-) g = Graph('-', imgformat='png', start=start, end=end, color=ca, vertical_label='"'+ginfo.vertical_label+'"') #populate it with our url params, defaulting to Rrdgraph instance (ginfo) options fullsizemode = req.GET.get('fullsizemode') if (fullsizemode in ['0', 'False' , 'false', 'no', 'No']): g.full_size_mode = False else: g.full_size_mode = True graphonly = req.GET.get('graphonly') if (graphonly in ['1', 'True' , 'true', 'yes']): g.only_graph = True noleg = req.GET.get('nolegend') if (noleg in ['1', 'True' , 'true', 'yes']): g.no_legend = True log = req.GET.get('logarithmic') if (log in ['1', 'True' , 'true', 'yes', 'Yes']): g.logarithmic = True elif (log in ['0', 'False' , 'false', 'no', 'No']): g.logarithmic = False else: g.logarithmic = getattr(ginfo, 'logarithmic', False) g.disable_rrdtool_tags = True g.height = req.GET.get('height', 600) g.width = req.GET.get('width', 1200) g.title = '"'+ginfo.name+'"' g.data.extend(gitems) #write in our gitems we generated a = g.write() #gets the binary image finally #minetype #just a thing to cause an error and debug return HttpResponse(a,mimetype="image/png")
area2 = AREA(defObj=cdef2, color='#A32001', legend='Bits Out') # Let's configure some custom colors for the graph ca = ColorAttributes() ca.back = '#333333' ca.canvas = '#333333' ca.shadea = '#000000' ca.shadeb = '#111111' ca.mgrid = '#CCCCCC' ca.axis = '#FFFFFF' ca.frame = '#AAAAAA' ca.font = '#FFFFFF' ca.arrow = '#FFFFFF' # Now that we've got everything set up, let's make a graph g = Graph('dummy.png', end=endTime, vertical_label='Bits', color=ca) g.data.extend([def1, def2, cdef1, cdef2, area2, area1]) g.title = '"In- and Out-bound Traffic Across Local Router"' #g.logarithmic = ' ' # Iterate through the different resoltions for which we want to # generate graphs. for time, step in times: # First, the small graph g.filename = graphfile % (exampleNum, time) g.width = 400 g.height = 100 g.start = endTime - time g.step = step g.write(debug=False)