rework etc/generate.py -> bin/rrdweb, just use target names...
#!/usr/bin/env python
import os.path, fnmatch
import optparse, logging
from rrdweb import graph, html
def parse_args (args) :
global options
parser = optparse.OptionParser(
usage = "%prog [options] <target> [...]"
)
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")
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")
options, targets = parser.parse_args(args)
return targets
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 setup_dirs () :
"""
Generate output dirs
"""
setup_img_dir("overview", "daily")
for interval in ("daily", "weekly", "yearly") :
setup_img_dir("detail", interval)
def target_graph (target, style, interval) :
"""
Generate .rrd -> .png for given target
"""
# XXX: title..
title = target
# compose paths
rrd_path = os.path.join(options.rrd_dir, target) + '.rrd'
out_path = os.path.join(options.img_dir, style, interval, target) + '.png'
# graph
graph.mrtg(style, interval, title, rrd_path, out_path)
def target_html (target, formatter) :
"""
Generate .html for given target
"""
# XXX: title..
title = target
# build paths
html_path = os.path.join(options.web_dir, target + '.html')
# render
open(html_path, 'w').write(formatter.target(target, title))
def overview_html (targets, formatter) :
"""
Generate .html index
"""
# paths
overview_path = os.path.join(options.web_dir, "index.html")
# as (target, title) pairs
open(overview_path, 'w').write(formatter.overview((target, target) for target in targets))
def generate (targets, filters=None):
if filters :
# 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
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",
)
# html for overview
overview_html(targets, formatter)
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)
def main (args) :
# parse args
targets = parse_args(args)
# run
generate(targets)
if __name__ == '__main__' :
from sys import argv
main(argv[1:])