# HG changeset patch # User Tero Marttila # Date 1264886139 -7200 # Node ID bfa9a0e11382cc31bb7378dc50a2d5138aab725d # Parent 30f866582b468fb3f14440cccc60ad2548d3c76f add -q/-v/-D args, generate_target, urljoin diff -r 30f866582b46 -r bfa9a0e11382 bin/rrdweb --- a/bin/rrdweb Sat Jan 30 23:15:22 2010 +0200 +++ b/bin/rrdweb Sat Jan 30 23:15:39 2010 +0200 @@ -12,19 +12,73 @@ parser = optparse.OptionParser( usage = "%prog [options] [...]" ) - + + # generic + parser.add_option('-q', "--quiet", help="No output in normal operation", + action='store_const', dest="loglvl", const=logging.WARNING, + ) + parser.add_option('-v', "--verbose", help="More output", + action='store_const', dest="loglvl", const=logging.INFO, + ) + parser.add_option('-D', "--debug", help="Even more output", + action='store_const', dest="loglvl", const=logging.DEBUG, + ) + + # paths parser.add_option('-R', "--rrd-dir", default="rrd", help="Path to directory containing .rrd files") parser.add_option('-I', "--img-dir", default="img", help="Path to directory containing .png files") parser.add_option('-W', "--web-dir", default="web", help="Path to directory containing .html files") parser.add_option('-T', "--tpl-dir", default="templates", help="Path to directory containing .html templates files") + + # urls parser.add_option('-P', "--url-prefix", default="", help="Prefix for URLs") parser.add_option( "--img-url", default="img", help="URL to directory containing .png files") parser.add_option( "--web-url", default="web", help="URL to directory containing .html files") + # defaults + parser.set_defaults( + loglvl = logging.INFO, + ) + + ## parse options, targets = parser.parse_args(args) + + ## apply + logging.basicConfig( + format = "[%(levelname)8s] %(funcName)20s : %(message)s", + level = options.loglvl, + ) + return targets + +def scan_targets () : + """ + 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) + + # skip dotfiles + if name.startswith('.') : + continue + + if os.path.isdir(path) : + # XXX: support trees.. + continue + + base, ext = os.path.splitext(name) + + if ext.lower() == '.rrd' : + # as target name + yield base + + def setup_img_dir (style, interval) : """ Generate output dir for given name @@ -56,6 +110,7 @@ for interval in ("daily", "weekly", "yearly") : setup_img_dir("detail", interval) + def target_graph (target, style, interval) : """ Generate .rrd -> .png for given target @@ -68,9 +123,12 @@ rrd_path = os.path.join(options.rrd_dir, target) + '.rrd' out_path = os.path.join(options.img_dir, style, interval, target) + '.png' + logging.debug("%s: %s -> %s", target, rrd_path, out_path) + # graph graph.mrtg(style, interval, title, rrd_path, out_path) + def target_html (target, formatter) : """ Generate .html for given target @@ -81,6 +139,8 @@ # build paths html_path = os.path.join(options.web_dir, target + '.html') + + logging.debug("%s: %s", target, html_path) # render open(html_path, 'w').write(formatter.target(target, title)) @@ -92,15 +152,37 @@ # paths overview_path = os.path.join(options.web_dir, "index.html") + + logging.debug("%s", overview_path) # as (target, title) pairs open(overview_path, 'w').write(formatter.overview((target, target) for target in targets)) + +def generate_target (target, formatter) : + + # overview + target_graph(target, 'overview', 'daily') + + # details + for interval in ('daily', 'weekly', 'yearly') : + target_graph(target, 'detail', interval) + + # html for target + target_html(target, formatter) + + def generate (targets, filters=None): + if not targets : + # autodetect + targets = list(scan_targets()) + + logging.info("Autodetected %d targets", len(targets)) + if filters : # filter targets targets = [target for target in targets if any(fnmatch.fnmatch(target.name, filter) for filter in filters)] - + # dirs setup_dirs() @@ -109,8 +191,8 @@ # overview formatter = html.Formatter( url_prefix = options.url_prefix, - img_url = "%(prefix)s/" + options.img_url + "/%(style)s/%(interval)s/%(target)s.png", - target_url = "%(prefix)s/" + options.web_url + "/%(target)s.html", + img_url = html.urljoin(options.url_prefix, options.img_url, "%(style)s", "%(interval)s", "%(target)s.png"), + target_url = html.urljoin(options.url_prefix, options.web_url, "%(target)s.html"), ) # html for overview @@ -119,15 +201,8 @@ for target in targets : logging.info("Target: %s", target) - # overview - target_graph(target, 'overview', 'daily') - - # details - for interval in ('daily', 'weekly', 'yearly') : - target_graph(target, 'detail', interval) - - # html for target - target_html(target, formatter) + # HTML and PNGs + generate_target(target, formatter) def main (args) : # parse args