summaryrefslogtreecommitdiff
path: root/tools/rrdui-cgi.py
diff options
context:
space:
mode:
authorStef Walter <stef@memberwebs.com>2006-01-20 23:10:33 +0000
committerStef Walter <stef@memberwebs.com>2006-01-20 23:10:33 +0000
commit9a813d3324c2dae32fcfd690591a9e31cdad649d (patch)
tree2b96e4e2008143f261721187293c2b517285400a /tools/rrdui-cgi.py
Initial import
Diffstat (limited to 'tools/rrdui-cgi.py')
-rwxr-xr-xtools/rrdui-cgi.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/tools/rrdui-cgi.py b/tools/rrdui-cgi.py
new file mode 100755
index 0000000..8b376f5
--- /dev/null
+++ b/tools/rrdui-cgi.py
@@ -0,0 +1,109 @@
+#!/usr/bin/python
+
+import os, sys, time
+import sets, cgi, shlex
+import rrdtool
+
+from rrdui import *
+
+def listGraphs():
+
+ graphs = loadGraphs()
+ categories = {}
+ for item in graphs:
+ if not categories.has_key(item.category):
+ categories[item.category] = []
+ categories[item.category].append(item)
+
+ groups = categories.keys()
+ groups.sort()
+
+ print "Content-Type: text/xml\n"
+ print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ print "<data>"
+ for group in groups:
+ print " <category name=\"%s\">" % group
+ categories[group].sort()
+ for item in categories[group]:
+ print " <graph name=\"%s\" width=\"%d\" height=\"%d\" title=\"%s\"/>" % \
+ (item.name, item.width, item.height, item.title)
+ print " </category>"
+ print "</data>"
+
+
+def displayGraph():
+
+ # print "Content-Type: text/plain"
+ print "Content-Type: image/png"
+ print ""
+
+ form = cgi.FieldStorage()
+ if not form.has_key("category") or not form.has_key("name"):
+ raise "Required arguments not specified"
+
+ name = form["name"].value
+ category = form["category"].value
+ item = GraphDef(category, name)
+
+ # Default to one day display
+ end = int(time.time())
+ start = end - 86400
+ if form.has_key("start"):
+ start = int(form["start"].value)
+ if form.has_key("end"):
+ end = int(form["end"].value)
+
+ # Default to Height and Width in graph
+ height = item.height
+ width = item.width
+ if form.has_key("width"):
+ width = int(form["width"].value)
+ if form.has_key("height"):
+ height = int(form["height"].value)
+
+
+ args = ["-", "--imgformat=PNG", "--rigid",
+ "--start=%d" % start,
+ "--end=%d" % end,
+ "--title=%s" % item.title,
+ "--height=%d" % height,
+ "--width=%d" % width ]
+
+ # TODO Check color syntax
+ if form.has_key("color"):
+ colors = form.getlist("color");
+ for color in colors:
+ args.append("--color")
+ args.append(color.replace(":", "#"))
+
+ commands = item.commands.replace("{START}",
+ time.strftime("%Y-%m-%d %H\\:%M", time.localtime(start)))
+ commands = commands.replace("{END}",
+ time.strftime("%Y-%m-%d %H\\:%M", time.localtime(end)))
+ commands = commands.replace("{RRD}", item.filedata)
+ args.extend(shlex.split(commands))
+
+ args.extend(shlex.split(item.options))
+
+ print >> sys.stderr, str(args)
+ rrdtool.graph(*args)
+
+
+if not os.environ.has_key("PATH_INFO"):
+ raise "PATH_INFO not set"
+
+path = os.environ["PATH_INFO"].strip("/")
+parts = path.split("/")
+
+method = parts[0]
+del parts[0]
+if not method:
+ method = "list"
+
+
+if method == "list":
+ listGraphs()
+elif method == "graph":
+ displayGraph()
+else:
+ raise "Invalid request: %s" % method