terom@5: import rrd terom@5: import time terom@5: terom@5: """ terom@5: RRDTool graph output terom@5: """ terom@5: terom@5: def timestamp () : terom@5: return time.strftime("%Y/%m/%d %H:%M:%S %Z") terom@5: terom@5: def common_opts () : terom@5: """ terom@5: Common options for all views terom@5: """ terom@5: terom@5: return dict( terom@5: # output terom@5: imgformat = "PNG", terom@5: # lazy = True, terom@5: terom@5: color = [ terom@5: # disable border terom@5: # border = 0, terom@5: "SHADEA#ffffff00", terom@5: "SHADEB#ffffff00", terom@5: terom@5: # keep background transparent terom@5: "BACK#ffffff00", terom@5: "SHADEB#ffffff00", terom@5: ], terom@5: terom@5: # labels terom@5: vertical_label = "bits/s", terom@5: units = "si", terom@5: terom@5: # use logarithmic scaling terom@5: logarithmic = True, terom@5: terom@5: # smooth out lines terom@5: slope_mode = True, terom@5: ) terom@5: terom@5: def overview_opts () : terom@5: """ terom@5: Common options for the overview graph terom@5: """ terom@5: terom@5: return dict( terom@5: width = 600, terom@5: height = 50, terom@5: ), [ terom@7: "CDEF:all=in,out,+", terom@7: terom@7: "VDEF:max=all,MAXIMUM", terom@7: "VDEF:avg=all,AVERAGE", terom@7: "VDEF:min=all,MINIMUM", terom@7: terom@7: "LINE1:in#0000FF:In", terom@7: "LINE1:out#00CC00:Out", terom@7: terom@7: "GPRINT:max:%6.2lf %Sbps max", terom@7: "GPRINT:avg:%6.2lf %Sbps avg", terom@7: "GPRINT:min:%6.2lf %Sbps min\\l", terom@5: ] terom@5: terom@5: def detail_opts () : terom@5: """ terom@5: Common options for the detail graph terom@5: """ terom@5: terom@5: return dict( terom@5: # dimensions terom@5: width = 600, terom@5: height = 200, terom@5: ), [ terom@5: # values terom@5: 'VDEF:in_max=in,MAXIMUM', terom@5: 'VDEF:in_avg=in,AVERAGE', terom@5: 'VDEF:in_min=in,MINIMUM', terom@5: 'VDEF:out_max=out,MAXIMUM', terom@5: 'VDEF:out_avg=out,AVERAGE', terom@5: 'VDEF:out_min=out,MINIMUM', terom@5: terom@5: # legend/graph terom@5: "COMMENT:%4s" % "", terom@5: "COMMENT:%11s" % "Maximum", terom@5: "COMMENT:%11s" % "Average", terom@5: "COMMENT:%11s\\l" % "Minimum", terom@5: terom@5: "LINE1:in#0000FF:%4s" % "In", terom@5: 'GPRINT:in_max:%6.2lf %Sbps', terom@5: 'GPRINT:in_avg:%6.2lf %Sbps', terom@5: 'GPRINT:in_min:%6.2lf %Sbps\\l', terom@5: terom@5: "LINE1:out#00CC00:%4s" % "Out", terom@5: 'GPRINT:out_max:%6.2lf %Sbps', terom@5: 'GPRINT:out_avg:%6.2lf %Sbps', terom@5: 'GPRINT:out_min:%6.2lf %Sbps\\l', terom@5: terom@5: # mark terom@5: "COMMENT:Generated %s\\r" % timestamp().replace(':', '\\:'), terom@5: ] terom@5: terom@5: STYLE_DEFS = { terom@5: 'overview': overview_opts, terom@5: 'detail': detail_opts, terom@5: } terom@5: terom@5: def daily_opts (title) : terom@5: """ terom@5: Common options for the 'daily' view terom@5: """ terom@5: terom@5: return dict( terom@5: # labels terom@5: x_grid = "MINUTE:15:HOUR:1:HOUR:4:0:%H:%M", terom@5: terom@5: # general info terom@5: title = "Daily %s" % (title, ), terom@5: terom@5: # interval terom@5: start = "-24h", terom@5: ) terom@5: terom@5: def weekly_opts (title) : terom@5: return dict( terom@5: # labels terom@5: # x_grid = "MINUTE:15:HOUR:1:HOUR:4:0:%H:%M", terom@5: terom@5: # general info terom@5: title = "Weekly %s" % (title, ), terom@5: terom@5: # interval terom@5: start = "-7d", terom@5: ) terom@5: terom@5: def yearly_opts (title) : terom@5: return dict( terom@5: # labels terom@5: # x_grid = "MINUTE:15:HOUR:1:HOUR:4:0:%H:%M", terom@5: terom@5: # general info terom@5: title = "Yearly %s" % (title, ), terom@5: terom@5: # interval terom@5: start = "-1y", terom@5: ) terom@5: terom@5: terom@5: INTERVAL_DEFS = { terom@5: 'daily': daily_opts, terom@5: 'weekly': weekly_opts, terom@5: 'yearly': yearly_opts, terom@5: } terom@5: terom@5: def mrtg_data (rrd_path) : terom@5: """ terom@5: Data sources for network in/out from an MRTG rrd terom@5: """ terom@5: terom@5: return [ terom@5: # data sources, bytes/s terom@5: r'DEF:in0=%s:ds0:AVERAGE' % rrd_path, terom@5: r'DEF:out0=%s:ds1:AVERAGE' % rrd_path, terom@5: terom@5: # data, bits/s terom@5: 'CDEF:in=in0,8,*', terom@5: 'CDEF:out=out0,8,*', terom@5: terom@5: ] terom@5: terom@5: def mrtg (style, interval, title, rrd_path, out_path) : terom@5: style_opts, style_vars = STYLE_DEFS[style]() terom@5: interval_opts = INTERVAL_DEFS[interval](title) terom@5: terom@5: opts = rrd.merge_opts(common_opts(), style_opts, interval_opts) terom@5: data = mrtg_data(rrd_path) + style_vars terom@5: terom@5: return rrd.graph(out_path, *data, **opts) terom@5: