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 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")