add --rrd-type, and trees of targets
authorTero Marttila <terom@fixme.fi>
Sat, 30 Jan 2010 23:44:28 +0200
changeset 18 eaf06ae5df16
parent 17 a1608b66be45
child 19 58df27d54d2e
child 20 86bbabd10ff6
add --rrd-type, and trees of targets
bin/rrdweb
--- a/bin/rrdweb	Sat Jan 30 23:43:57 2010 +0200
+++ b/bin/rrdweb	Sat Jan 30 23:44:28 2010 +0200
@@ -23,6 +23,9 @@
     parser.add_option('-D', "--debug", help="Even more output",
         action='store_const', dest="loglvl", const=logging.DEBUG,
     )
+
+    # options
+    parser.add_option(      "--rrd-type", default="mrtg", help="RRD style to use: mrtg, collectd_ifoctets")
     
     # paths
     parser.add_option('-R', "--rrd-dir", default="rrd", help="Path to directory containing .rrd files")
@@ -57,59 +60,43 @@
     """
         Scan for targets from rrd_dir
     """
-
-    dir = options.rrd_dir
-
-    # XXX: support trees...
-    for name in os.listdir(dir) :
-        path = os.path.join(dir, name)
+    
+    # root dir for searching
+    rrd_dir = options.rrd_dir
+    
+    # recurse
+    for dirpath, dirs, files in os.walk(rrd_dir) :
+        for name in files :
+            # full path
+            path = os.path.join(dirpath, name)
 
-        # skip dotfiles
-        if name.startswith('.') :
-            continue
+            # relative path from rrd_dir
+            relpath = path[len(os.path.commonprefix([rrd_dir, path])):]
 
-        if os.path.isdir(path) :
-            # XXX: support trees..
-            continue
-
-        base, ext = os.path.splitext(name)
-        
-        if ext.lower() == '.rrd' :
-            # as target name
-            yield base
+            # skip dotfiles
+            if name.startswith('.') :
+                continue
+            
+            # foo/bar, .rrd
+            target, ext = os.path.splitext(relpath)
+            
+            if ext.lower() == '.rrd' :
+                # as target name
+                yield target
 
 
-def setup_img_dir (style, interval) :
-    """
-        Generate output dir for given name
-    """
-
-    base_dir = options.img_dir
-    style_dir = os.path.join(base_dir, style)
-    interval_dir = os.path.join(base_dir, style, interval)
-
-    if not os.path.exists(options.img_dir) :
-        raise Exception("img_dir does not exist: " + options.img_dir)
-    
 
-    if not os.path.exists(style_dir) :
-        logging.warn("Create img_dir for style=%s: %s" % (style, style_dir))
-        os.mkdir(style_dir)
-
-    if not os.path.exists(interval_dir) :
-        logging.warn("Create img_dir for style=%s, interval=%s: %s" % (style, interval, interval_dir))
-        os.mkdir(interval_dir)
+def check_output_file (path) :
+    """
+        Create the directory for the given output path if missing
+    """
+    
+    dir, file = os.path.split(path)
 
-def setup_dirs () :
-    """
-        Generate output dirs
-    """
+    if not os.path.exists(dir) :
+        logging.warn("Create directory for output file %s: %s", file, dir)
 
-    setup_img_dir("overview", "daily")
-
-    for interval in ("daily", "weekly", "yearly") :
-        setup_img_dir("detail", interval)
-
+        os.makedirs(dir)
 
 def target_graph (target, style, interval) : 
     """
@@ -123,10 +110,16 @@
     rrd_path = os.path.join(options.rrd_dir, target) + '.rrd'
     out_path = os.path.join(options.img_dir, style, interval, target) + '.png'
 
+    # create out path if not exists
+    check_output_file(out_path)
+
     logging.debug("%s: %s -> %s", target, rrd_path, out_path)
+    
+    # graph function to use
+    graph_func = getattr(graph, options.rrd_type)
 
     # graph
-    graph.mrtg(style, interval, title, rrd_path, out_path)
+    graph_func(style, interval, title, rrd_path, out_path)
 
 
 def target_html (target, formatter) :
@@ -139,6 +132,9 @@
     
     # build paths
     html_path = os.path.join(options.web_dir, target + '.html')
+    
+    # create out path if not exists
+    check_output_file(html_path)
 
     logging.debug("%s: %s", target, html_path)
     
@@ -153,6 +149,9 @@
     # paths
     overview_path = os.path.join(options.web_dir, "index.html")
 
+    # create out path if not exists
+    check_output_file(overview_path)
+
     logging.debug("%s", overview_path)
     
     # as (target, title) pairs
@@ -183,9 +182,6 @@
         # filter targets
         targets = [target for target in targets if any(fnmatch.fnmatch(target.name, filter) for filter in filters)]
     
-    # dirs
-    setup_dirs()
-    
     logging.info("Updating %d targets", len(targets))
 
     # overview