1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#!/usr/bin/env python
import os, sys, time
import sets, cgi, shlex
import ConfigParser
import rrdtool
from rrdui import *
# TODO: Temporary
if not os.environ.has_key("CONFDIR") or not os.environ.has_key("WORKDIR"):
raise "not setup properly. CONFDIR and WORKDIR env variables must be set"
CONFDIR = os.environ["CONFDIR"]
WORKDIR = os.environ["WORKDIR"]
class GraphDef:
filename = None
filedata = None
title = ""
height = 0
width = 0
options = ""
commands = ""
category = ""
name = ""
__config = None
def __init__(self, name):
self.filename = "%s/%s" % (CONFDIR, name)
self.filedata = "%s/%s.rrd" % (WORKDIR, name)
self.category = "All"
self.name = name
cfg = self.__config = ConfigParser.RawConfigParser()
cfg.read(self.filename)
# Loading general stuff
if cfg.has_option("general", "title"):
self.title = cfg.get("general", "title")
if cfg.has_option("general", "category"):
self.category = cfg.get("general", "category")
# Loading graph stuff
if cfg.has_option("graph", "width"):
self.width = int(cfg.get("graph", "width"))
if cfg.has_option("graph", "height"):
self.height = int(cfg.get("graph", "height"))
if cfg.has_option("graph", "options"):
self.options = cfg.get("graph", "options")
if not cfg.has_option("graph", "commands"):
raise "Missing commands attribute in: %s" % self.filename
self.commands = cfg.get("graph", "commands")
def getCreateInfo(self):
cfg = self.__config
rra = None
fields = {}
# The RRA info
if cfg.has_option("create", "rra"):
rra = cfg.get("create", "rra").split()
# The various fields
for field in cfg.options("create"):
if not field.startswith("field."):
continue
fieldname = field[6:]
fields[fieldname] = cfg.get("create", field)
return (fields, rra)
def getPollingInfo(self):
cfg = self.__config
interval = 300
fields = {}
# The interval
if cfg.has_option("poll", "interval"):
interval = int(cfg.get("poll", "interval"))
# The various fields
for field in cfg.options("poll"):
if not field.startswith("field."):
continue
fieldname = field[6:]
fields[fieldname] = cfg.get("poll", field)
return (fields, interval)
def loadGraphs():
# List files and add appropriate paths
graphs = []
for file in os.listdir(CONFDIR):
if os.path.isdir(file):
continue
graphs.append(GraphDef(file))
return graphs
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
item = GraphDef(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
|