rrdweb/rrd.py
changeset 32 47e977c23ba2
parent 29 c756e522c9ac
equal deleted inserted replaced
31:cd9ca8068b09 32:47e977c23ba2
     2     Friendly wrapper around the rrdtool python interface
     2     Friendly wrapper around the rrdtool python interface
     3 """
     3 """
     4 
     4 
     5 import rrdtool
     5 import rrdtool
     6 
     6 
       
     7 import tempfile
     7 import logging
     8 import logging
       
     9 import os
     8 
    10 
     9 log = logging.getLogger('rrdweb.rrd')
    11 log = logging.getLogger('rrdweb.rrd')
    10 
    12 
    11 def normalize_option_key (key) :
    13 def normalize_option_key (key) :
    12     """
    14     """
    87 
    89 
    88     log.debug('rrdtool %s %s', func.__name__, args)
    90     log.debug('rrdtool %s %s', func.__name__, args)
    89 
    91 
    90     return func(*args)
    92     return func(*args)
    91 
    93 
    92 def graph (out_path, *args, **opts) :
    94 def graph (out, *args, **opts) :
    93     """
    95     """
    94         Create a graph from data stored in one or several RRDs.
    96         Render a graph image and/or print a report from data stored in one or several RRDs.
    95 
    97 
    96         Graph image output is written to the given path.
    98         If the output path is given, the graph data is written to the given file, otherwise a temporary file is
       
    99         created. The resulting output file is returned as an opened file object, ready to read() the graph data.
    97 
   100 
    98         Returns... something to do with the image's dimensions, or even the data itself?
   101         In the temporary file case, the returned file is unlinked before being returned.
       
   102 
       
   103         If out is explicitly given as False, no graph output is generated, and graph_file is returned as None.
       
   104 
       
   105         Returns a (width, height, [ report output lines ], graph_file) tuple.
    99     """
   106     """
       
   107     
       
   108     # open/tempfile
       
   109     if out is None :
       
   110         # tempfile
       
   111         tmp_fd, out_path = tempfile.mkstemp('.png')
   100 
   112 
   101     return run_cmd(rrdtool.graph, (out_path, ), opts, args)
   113         # disregard the tmp_fd, we re-open the file after it's been written out
       
   114         os.close(tmp_fd)
       
   115         
       
   116     elif out is False :
       
   117         # no output
       
   118         tmp_fd = None
       
   119         out_path = ''
       
   120 
       
   121     else :
       
   122         # direct output
       
   123         tmp_fd = None
       
   124         out_path = out
       
   125     
       
   126     # render
       
   127     try :
       
   128         # invoke
       
   129         width, height, output = run_cmd(rrdtool.graph, (out_path, ), opts, args)
       
   130         
       
   131         # return resulting file
       
   132         if out_path :
       
   133             out_file = open(out_path, 'r')
       
   134 
       
   135         else :
       
   136             # no graph output
       
   137             out_file = None
       
   138 
       
   139         return width, height, output, out_file
       
   140 
       
   141     finally :
       
   142         if tmp_fd :
       
   143             # cleanup tempfile
       
   144             # XXX: definately not portable to windows
       
   145             os.unlink(out_path)
   102 
   146 
   103 def create (rrd_path, *args, **opts) :
   147 def create (rrd_path, *args, **opts) :
   104     """
   148     """
   105         Set up a new Round Robin Database (RRD).
   149         Set up a new Round Robin Database (RRD).
   106     """
   150     """