]> git.decadent.org.uk Git - dak.git/blob - dak/graph.py
rrd foo
[dak.git] / dak / graph.py
1 #!/usr/bin/env python
2
3 """ Produces a set of graphs of NEW/BYHAND/DEFERRED
4
5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2011 Paul Wise <pabs@debian.org>
7 """
8
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23 import os
24 import sys
25
26 import rrdtool
27 import apt_pkg
28
29 from daklib import utils
30 from daklib.dak_exceptions import *
31
32 Cnf = None
33 default_names = ["byhand", "new", "deferred"]
34
35 ################################################################################
36
37 def usage(exit_code=0):
38     print """Usage: dak graph
39 Graphs the number of packages in queue directories (usually new and byhand).
40
41   -h, --help                show this help and exit.
42   -r, --rrd=key             Directory where rrd files to be updated are stored
43   -x, --extra-rrd=key       File containing extra options to rrdtool graphing
44   -i, --images=key          Directory where image graphs to be updated are stored
45   -n, --names=key           A comma seperated list of rrd files to be scanned
46
47 """
48     sys.exit(exit_code)
49
50 ################################################################################
51
52 def graph(rrd_dir, image_dir, name, extra_args, graph, title, start, year_lines=False):
53     image_file = os.path.join(image_dir, "%s-%s.png" % (name, graph))
54     rrd_file = os.path.join(rrd_dir, "%s.rrd" % name)
55     rrd_args = [image_file, "--start", start]
56     rrd_args += ("""
57 --end
58 now
59 --width
60 600
61 --height
62 150
63 --vertical-label
64 packages
65 --title
66 Package count: %s
67 --lower-limit
68 0
69 -E
70 """ % title).strip().split("\n")
71
72     if year_lines:
73         rrd_args += ["--x-grid", "MONTH:1:YEAR:1:YEAR:1:31536000:%Y"]
74
75     rrd_args += ("""
76 DEF:ds1=%s:ds1:AVERAGE
77 LINE2:ds1#D9382B:Total package count:
78 VDEF:lds1=ds1,LAST
79 VDEF:minds1=ds1,MINIMUM
80 VDEF:maxds1=ds1,MAXIMUM
81 VDEF:avgds1=ds1,AVERAGE
82 GPRINT:lds1:%%3.0lf
83 GPRINT:minds1:\tMin\\: %%3.0lf
84 GPRINT:maxds1:\tMax\\: %%3.0lf
85 GPRINT:avgds1:\tAvg\\: %%3.0lf\\j
86 DEF:ds0=%s:ds0:AVERAGE
87 VDEF:lds0=ds0,LAST
88 VDEF:minds0=ds0,MINIMUM
89 VDEF:maxds0=ds0,MAXIMUM
90 VDEF:avgds0=ds0,AVERAGE
91 LINE2:ds0#3069DA:Package count in %s:
92 GPRINT:lds0:%%3.0lf
93 GPRINT:minds0:\tMin\\: %%3.0lf
94 GPRINT:maxds0:\tMax\\: %%3.0lf
95 GPRINT:avgds0:\tAvg\\: %%3.0lf\\j
96 """ % (rrd_file, rrd_file, name.upper())).strip().split("\n")
97
98     rrd_args += extra_args
99     rrdtool.graph(*rrd_args)
100
101 ################################################################################
102
103 def main():
104     global Cnf
105
106     Cnf = utils.get_conf()
107     Arguments = [('h',"help","Graph::Options::Help"),
108                  ('x',"extra-rrd","Graph::Options::Extra-Rrd", "HasArg"),
109                  ('r',"rrd","Graph::Options::Rrd", "HasArg"),
110                  ('i',"images","Graph::Options::Images", "HasArg"),
111                  ('n',"names","Graph::Options::Names", "HasArg")]
112     for i in [ "help" ]:
113         if not Cnf.has_key("Graph::Options::%s" % (i)):
114             Cnf["Graph::Options::%s" % (i)] = ""
115
116     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
117
118     Options = Cnf.SubTree("Graph::Options")
119     if Options["Help"]:
120         usage()
121
122     names = []
123
124     if Cnf.has_key("Graph::Options::Names"):
125         for i in Cnf["Graph::Options::Names"].split(","):
126             names.append(i)
127     elif Cnf.has_key("Graph::Names"):
128         names = Cnf.ValueList("Graph::Names")
129     else:
130         names = default_names
131
132     extra_rrdtool_args = []
133
134     if Cnf.has_key("Graph::Options::Extra-Rrd"):
135         for i in Cnf["Graph::Options::Extra-Rrd"].split(","):
136             f = open(i)
137             extra_rrdtool_args.extend(f.read().strip().split("\n"))
138             f.close()
139     elif Cnf.has_key("Graph::Extra-Rrd"):
140         for i in Cnf.ValueList("Graph::Extra-Rrd"):
141             f = open(i)
142             extra_rrdtool_args.extend(f.read().strip().split("\n"))
143             f.close()
144
145     if Cnf.has_key("Graph::Options::Rrd"):
146         rrd_dir = Cnf["Graph::Options::Rrd"]
147     elif Cnf.has_key("Dir::Rrd"):
148         rrd_dir = Cnf["Dir::Rrd"]
149     else:
150         print >> sys.stderr, "No directory to read RRD files from\n"
151         sys.exit(1)
152
153     if Cnf.has_key("Graph::Options::Images"):
154         image_dir = Cnf["Graph::Options::Images"]
155     else:
156         print >> sys.stderr, "No directory to write graph images to\n"
157         sys.exit(1)
158
159     for name in names:
160         stdargs = [rrd_dir, image_dir, name, extra_rrdtool_args]
161         graph(*(stdargs+['day', 'day', 'now-1d']))
162         graph(*(stdargs+['week', 'week', 'now-1w']))
163         graph(*(stdargs+['month', 'month', 'now-1m']))
164         graph(*(stdargs+['year', 'year', 'now-1y']))
165         graph(*(stdargs+['5years', '5 years', 'now-5y', True]))
166         graph(*(stdargs+['10years', '10 years', 'now-10y', True]))
167
168 ################################################################################
169
170 if __name__ == '__main__':
171     main()