2 |
|
|
3 |
|
# |
4 |
|
# i-scream central monitoring system |
5 |
+ |
# http://www.i-scream.org |
6 |
|
# Copyright (C) 2000-2002 i-scream |
7 |
|
# |
8 |
|
# This program is free software; you can redistribute it and/or |
22 |
|
|
23 |
|
# ----------------------------------------------------------- |
24 |
|
# i-scream graph generation script |
25 |
< |
# http://www.i-scream.org.uk |
25 |
> |
# http://www.i-scream.org |
26 |
|
# |
27 |
|
# Generates graphs from rrd databases for i-scream data. |
28 |
|
# |
34 |
|
# possibly make more configurable? |
35 |
|
# -- allow configurable periods of graphs |
36 |
|
# -- comments, types, etc |
36 |
– |
# -- move all to external config file |
37 |
|
|
38 |
+ |
my($version) = '$Id$'; |
39 |
+ |
|
40 |
|
$| = 1; |
41 |
+ |
|
42 |
|
use strict; |
43 |
+ |
use Getopt::Std; |
44 |
|
use RRDs; |
45 |
|
|
46 |
< |
# Base directory for images |
47 |
< |
# (a directory will be constructed for each host under this) |
48 |
< |
my($imgdir) = "/home/pkg/iscream/public_html/graphs"; |
46 |
> |
# define variables that will be read from the config |
47 |
> |
# nb. keep this insync with the config file! |
48 |
> |
use vars qw{ |
49 |
> |
$imgdir $rrddir |
50 |
> |
$maxrrdage $maximgage $deleterrds $deleteimgs |
51 |
> |
$hex_slash $hex_underscore $hex_space $hex_colon $hex_bslash |
52 |
> |
$rrdstep $retry_wait |
53 |
> |
$verbose $quiet |
54 |
> |
}; |
55 |
|
|
56 |
< |
# Location of RRD databases |
57 |
< |
my($rrddir) = "/u1/i-scream/databases"; |
56 |
> |
# default locate of the config file |
57 |
> |
my($configfile) = "rrdgraphing.conf"; |
58 |
|
|
59 |
< |
# / converted to a decimal then hex'd |
60 |
< |
my($hex_slash) = "_2f"; |
61 |
< |
# _ converted to a decimal then hex'd |
52 |
< |
my($hex_underscore) = "_5f"; |
59 |
> |
# check for command line arguments |
60 |
> |
my(%opts); |
61 |
> |
my($ret) = getopts('hvqVc:', \%opts); |
62 |
|
|
63 |
< |
# maximum age (last modified) before an rrd or graph get cleaned up |
64 |
< |
# (in seconds) |
65 |
< |
my($maxrrdage) = 3600; # 1 hour |
66 |
< |
my($maxgraphage) = 3600; # 1 hour |
67 |
< |
|
63 |
> |
# if invalid argument given, $ret will not be 1 |
64 |
> |
&usage() if $ret != 1; |
65 |
> |
|
66 |
> |
# first process the arguments which might mean we exit now |
67 |
> |
|
68 |
> |
# -h is usage |
69 |
> |
if($opts{h}) { |
70 |
> |
&usage(); |
71 |
> |
} |
72 |
> |
# -V is version |
73 |
> |
if($opts{V}) { |
74 |
> |
print "graph.pl version: $version\n"; |
75 |
> |
exit(1); |
76 |
> |
} |
77 |
> |
|
78 |
> |
# Then try getting the config |
79 |
> |
|
80 |
> |
# -c specifies the config file location |
81 |
> |
if($opts{c}) { |
82 |
> |
$configfile = $opts{c}; |
83 |
> |
} |
84 |
> |
# suck in the config |
85 |
> |
&log("reading config from $configfile\n"); |
86 |
> |
do $configfile; |
87 |
> |
|
88 |
> |
# Then any options we might want to override the config with |
89 |
> |
|
90 |
> |
# -v is verbose |
91 |
> |
if($opts{v}) { |
92 |
> |
$verbose = $opts{v}; |
93 |
> |
} |
94 |
> |
# -q is verbose |
95 |
> |
if($opts{q}) { |
96 |
> |
$quiet = $opts{q}; |
97 |
> |
# if we're meant to be quiet, we can hardly be verbose! |
98 |
> |
$verbose = 0; |
99 |
> |
} |
100 |
> |
|
101 |
|
# Read the contents of the base directory |
102 |
|
# and pull out the list of subdirectories (except . and .. :) |
103 |
|
opendir(DIR, $rrddir); |
104 |
< |
my(@rrddirlist) = grep { -d "$rrddir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
104 |
> |
my(@rrddirlist) = sort grep { -d "$rrddir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
105 |
|
closedir DIR; |
106 |
|
|
107 |
< |
# look through each directoty, as they might |
107 |
> |
# look through each directory, as they might |
108 |
|
# contain rrds for a particular machine |
109 |
|
foreach my $machine (@rrddirlist) { |
110 |
|
# Read the contents of the directory |
120 |
|
$ctime,$blksize,$blocks) = stat("$rrddir/$machine/$rrd"); |
121 |
|
# check if it's old enough to be deleted |
122 |
|
if((time - $mtime) > $maxrrdage) { |
123 |
< |
print "pruning rrd $rrddir/$machine/$rrd\n"; |
124 |
< |
# if so, delete it |
125 |
< |
unlink("$rrddir/$machine/$rrd"); |
123 |
> |
# do we delete the rrd, or just ignore it? |
124 |
> |
if($deleterrds) { |
125 |
> |
# if so, delete it |
126 |
> |
unlink("$rrddir/$machine/$rrd"); |
127 |
> |
&log("deleted old rrd $rrddir/$machine/$rrd\n"); |
128 |
> |
} |
129 |
> |
else { |
130 |
> |
&log("ignored old rrd $rrddir/$machine/$rrd\n"); |
131 |
> |
} |
132 |
|
# no more processing required for this rrd |
133 |
|
next; |
134 |
|
} |
135 |
|
if($rrd =~ /^(cpu)\.rrd$/) { |
136 |
|
my(@data); |
137 |
|
my(@rawdata); |
138 |
< |
push @data, "LINE2:$1:idle:idle#00FF00:idle cpu"; |
139 |
< |
push @data, "LINE2:$1:user:user#0000FF:user cpu"; |
140 |
< |
push @data, "LINE2:$1:kernel:kernel#00FFFF:kernel cpu"; |
141 |
< |
push @data, "LINE2:$1:swap:swap#FF00FF:swap cpu"; |
142 |
< |
push @data, "LINE2:$1:iowait:iowait#FF0000:iowait cpu"; |
138 |
> |
push @data, "AREA:$1:swap:swap#FF00FF:OK:swap cpu "; |
139 |
> |
push @data, "STACK:$1:iowait:iowait#FF0000:OK:iowait cpu"; |
140 |
> |
push @data, "STACK:$1:kernel:kernel#00FFFF:OK:kernel cpu"; |
141 |
> |
push @data, "STACK:$1:user:user#0000FF:OK:user cpu "; |
142 |
> |
push @data, "LINE2:$1:idle:idle#00FF00:OK:idle cpu "; |
143 |
|
push @rawdata, "--upper-limit=100"; |
144 |
< |
&makegraph($machine, $1, "CPU Usage for $machine", \@data, \@rawdata); |
144 |
> |
&makegraph($machine, $1, "CPU Usage for $machine", "% cpu time", \@data, \@rawdata); |
145 |
|
} |
146 |
+ |
if($rrd =~ /^(uptime)\.rrd$/) { |
147 |
+ |
my(@data); |
148 |
+ |
push @data, "LINE2:$1:uptime:uptime#0000FF:OK:uptime"; |
149 |
+ |
&makegraph($machine, $1, "Uptime for $machine", "uptime in seconds", \@data); |
150 |
+ |
} |
151 |
|
if($rrd =~ /^(mem)\.rrd$/) { |
152 |
|
my(@data); |
153 |
|
my(@rawdata); |
154 |
< |
# we don't actually want to display free memory, |
155 |
< |
# although we need it to do inuse... |
156 |
< |
push @data, "NONE:$1:free:free#CCCCFF:free memory"; |
157 |
< |
push @data, "LINE2:$1:total:total#0000FF:total memory"; |
158 |
< |
# calculate inuse |
159 |
< |
push @rawdata, "CDEF:inuse=total,free,-"; |
154 |
> |
# we don't actually want to display free or total memory, |
155 |
> |
# although we need it to work out peruse... |
156 |
> |
push @data, "NONE:$1:free:free#CCCCFF:NONE:free memory"; |
157 |
> |
push @data, "NONE:$1:total:total#0000FF:NONE:total memory\\n"; |
158 |
> |
push @data, "NONE:$1:cache:cache#0000FF:NONE:cache memory\\n"; |
159 |
> |
# calculate peruse - note that we only use 'free' if it's less than total |
160 |
> |
# (this is to avoid negative percentages :) |
161 |
> |
push @rawdata, "CDEF:peruse=total,free,total,LT,free,0,IF,-,total,/,100,*"; |
162 |
> |
push @rawdata, "CDEF:percacuse=cache,total,LT,cache,0,IF,total,/,100,*"; |
163 |
|
# and add it to the graph |
164 |
< |
push @rawdata, "AREA:inuse#CCCCFF:memory in use"; |
164 |
> |
push @rawdata, "AREA:peruse#CCCCFF:% memory in use"; |
165 |
> |
&addlegend(\@rawdata, "peruse"); |
166 |
> |
push @rawdata, "LINE2:percacuse#0000FF:% memory cache "; |
167 |
> |
&addlegend(\@rawdata, "percacuse"); |
168 |
> |
push @rawdata, "--upper-limit=100"; |
169 |
|
push @rawdata, "--base=1024"; |
170 |
< |
&makegraph($machine, $1, "Memory Usage for $machine", \@data, \@rawdata); |
170 |
> |
# put the total memory on the graph so we can map percentages to real values |
171 |
> |
push @rawdata, "GPRINT:total:LAST:Current total memory\\: \%.2lf %sb\\c"; |
172 |
> |
&makegraph($machine, $1, "Memory Usage for $machine", "% memory in use", \@data, \@rawdata); |
173 |
|
} |
174 |
|
if($rrd =~ /^(load)\.rrd$/) { |
175 |
|
my(@data); |
176 |
< |
push @data, "LINE2:$1:load1:load1#CCCCFF:1 minute load average"; |
177 |
< |
push @data, "LINE2:$1:load5:load5#7777FF:5 minute load average"; |
178 |
< |
push @data, "LINE2:$1:load15:load15#0000FF:15 minute load average"; |
179 |
< |
&makegraph($machine, $1, "Loads for $machine", \@data); |
176 |
> |
push @data, "LINE2:$1:load1:load1#CCCCFF:OK: 1 min load average"; |
177 |
> |
push @data, "LINE2:$1:load5:load5#7777FF:OK: 5 min load average"; |
178 |
> |
push @data, "LINE2:$1:load15:load15#0000FF:OK:15 min load average"; |
179 |
> |
&makegraph($machine, $1, "Loads for $machine", "load average", \@data); |
180 |
|
} |
181 |
|
if($rrd =~ /^(proc)\.rrd$/) { |
182 |
|
my(@data); |
183 |
< |
push @data, "LINE2:$1:cpu:cpu#00FF00:cpu processes"; |
184 |
< |
push @data, "LINE2:$1:sleeping:sleeping#0000FF:sleeping processes"; |
185 |
< |
push @data, "LINE2:$1:stopped:stopped#00FFFF:stopped processes"; |
186 |
< |
push @data, "LINE2:$1:total:total#FF00FF:total processes"; |
187 |
< |
push @data, "LINE2:$1:zombie:zombie#FF0000:zombie processes"; |
188 |
< |
&makegraph($machine, $1, "Processes on $machine", \@data); |
183 |
> |
push @data, "AREA:$1:stopped:stopped#00FFFF:OK:stopped processes "; |
184 |
> |
push @data, "STACK:$1:zombie:zombie#FF0000:OK:zombie processes "; |
185 |
> |
push @data, "STACK:$1:cpu:cpu#00FF00:OK:cpu processes "; |
186 |
> |
push @data, "STACK:$1:sleeping:sleeping#0000FF:OK:sleeping processes"; |
187 |
> |
push @data, "LINE2:$1:total:total#FF00FF:OK:total processes "; |
188 |
> |
&makegraph($machine, $1, "Processes on $machine", "no. of processes", \@data); |
189 |
|
} |
190 |
|
if($rrd =~ /^(swap)\.rrd$/) { |
191 |
|
my(@data); |
192 |
|
my(@rawdata); |
193 |
< |
# we don't actually want to display free swap, |
194 |
< |
# although we need it to do inuse... |
195 |
< |
push @data, "NONE:$1:free:free#CCCCFF:free swap"; |
196 |
< |
push @data, "LINE2:$1:total:total#0000FF:total swap"; |
197 |
< |
# calculate inuse |
198 |
< |
push @rawdata, "CDEF:inuse=total,free,-"; |
193 |
> |
# we don't actually want to display free or total swap, |
194 |
> |
# although we need it to work out peruse... |
195 |
> |
push @data, "NONE:$1:free:free#CCCCFF:NONE:free swap"; |
196 |
> |
push @data, "NONE:$1:total:total#0000FF:NONE:total swap\\n"; |
197 |
> |
# calculate peruse - note that we only use 'free' if it's less than total |
198 |
> |
# (this is to avoid negative percentages :) |
199 |
> |
push @rawdata, "CDEF:peruse=total,free,total,LT,free,0,IF,-,total,/,100,*"; |
200 |
|
# and add it to the graph |
201 |
< |
push @rawdata, "AREA:inuse#CCCCFF:swap in use"; |
201 |
> |
push @rawdata, "AREA:peruse#CCCCFF:% swap in use"; |
202 |
> |
push @rawdata, "--upper-limit=100"; |
203 |
|
push @rawdata, "--base=1024"; |
204 |
< |
&makegraph($machine, $1, "Swap Usage for $machine", \@data, \@rawdata); |
204 |
> |
# add some nice values to the legend |
205 |
> |
&addlegend(\@rawdata, "peruse"); |
206 |
> |
# put the total swap on the graph so we can map percentages to real values |
207 |
> |
push @rawdata, "GPRINT:total:LAST:Current total swap\\: \%.2lf %sb\\c"; |
208 |
> |
&makegraph($machine, $1, "Swap Usage for $machine", "% swap in use", \@data, \@rawdata); |
209 |
|
} |
210 |
|
if($rrd =~ /^(users)\.rrd$/) { |
211 |
|
my(@data); |
212 |
< |
push @data, "AREA:$1:count:count#CCCCFF:user count"; |
213 |
< |
&makegraph($machine, $1, "User Count for $machine", \@data); |
212 |
> |
push @data, "AREA:$1:count:count#CCCCFF:OK:user count"; |
213 |
> |
&makegraph($machine, $1, "User Count for $machine", "no. of users", \@data); |
214 |
|
} |
215 |
< |
if($rrd =~ /^(disk)-(\S+).rrd$/) { |
215 |
> |
if($rrd =~ /^(paging)\.rrd$/) { |
216 |
|
my(@data); |
217 |
+ |
push @data, "AREA:$1:pageins:pageins#00FF00:OK:pages paged in "; |
218 |
+ |
push @data, "LINE2:$1:pageouts:pageouts#0000FF:OK:pages paged out"; |
219 |
+ |
&makegraph($machine, $1, "Paging on $machine", "pages per second", \@data); |
220 |
+ |
} |
221 |
+ |
if($rrd =~ /^(disk)-(\S+)\.rrd$/) { |
222 |
+ |
my(@data); |
223 |
|
my(@rawdata); |
224 |
< |
push @data, "LINE2:$1-$2:kbytes:kbytes#0000FF:total size"; |
225 |
< |
push @data, "AREA:$1-$2:used:used#CCCCFF:used"; |
224 |
> |
# we need this lot for our calculations, but we'll never show them |
225 |
> |
push @data, "NONE:$1-$2:total:total#0000FF:NONE:total size\\n"; |
226 |
> |
push @data, "NONE:$1-$2:used:used#CCCCFF:NONE:used space"; |
227 |
> |
push @data, "NONE:$1-$2:totalinodes:totalinodes#000000:NONE:total inodes"; |
228 |
> |
push @data, "NONE:$1-$2:freeinodes:freeinodes#000000:NONE:free inodes"; |
229 |
> |
# calculate peruse, add it to the graph, and add a legend |
230 |
> |
push @rawdata, "CDEF:peruse=used,total,/,100,*"; |
231 |
> |
push @rawdata, "AREA:peruse#CCCCFF:% disk used "; |
232 |
> |
&addlegend(\@rawdata, "peruse"); |
233 |
> |
# put the total space on the graph so we can map percentages to real values |
234 |
> |
push @rawdata, "GPRINT:total:LAST:Current total space\\: \%.2lf %sb\\c"; |
235 |
> |
# calculate perinodeuse, add it to the graph, and add a legend |
236 |
> |
push @rawdata, "CDEF:perinodeuse=totalinodes,freeinodes,totalinodes,LT,freeinodes,0,IF,-,totalinodes,/,100,*"; |
237 |
> |
push @rawdata, "LINE2:perinodeuse#FF4444:% inodes used"; |
238 |
> |
push @rawdata, "--upper-limit=100"; |
239 |
|
push @rawdata, "--base=1024"; |
240 |
+ |
&addlegend(\@rawdata, "perinodeuse"); |
241 |
+ |
# put the total inodes on the graph so we can map percentages to real values |
242 |
+ |
push @rawdata, "GPRINT:totalinodes:LAST:Current total inodes\\: \%.2lf %s\\c"; |
243 |
+ |
# some name tidting |
244 |
|
my($type) = $1; |
245 |
|
my($name) = $2; |
246 |
|
my($nicename) = $2; |
247 |
|
$nicename =~ s/$hex_slash/\//g; |
248 |
|
$nicename =~ s/$hex_underscore/_/g; |
249 |
< |
&makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", \@data, \@rawdata); |
249 |
> |
$nicename =~ s/$hex_bslash/\\/g; |
250 |
> |
$nicename =~ s/$hex_space/ /g; |
251 |
> |
$nicename =~ s/$hex_colon/:/g; |
252 |
> |
&makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", "% usage", \@data, \@rawdata); |
253 |
|
} |
254 |
+ |
if($rrd =~ /^(diskio)-(\S+)\.rrd$/) { |
255 |
+ |
my(@data); |
256 |
+ |
my(@rawdata); |
257 |
+ |
push @data, "AREA:$1-$2:rbytes:rbytes#00FF00:OK:read bytes "; |
258 |
+ |
push @data, "LINE2:$1-$2:wbytes:wbytes#0000FF:OK:write bytes"; |
259 |
+ |
push @rawdata, "--base=1024"; |
260 |
+ |
# some name tidting |
261 |
+ |
my($type) = $1; |
262 |
+ |
my($name) = $2; |
263 |
+ |
my($nicename) = $2; |
264 |
+ |
$nicename =~ s/$hex_slash/\//g; |
265 |
+ |
$nicename =~ s/$hex_underscore/_/g; |
266 |
+ |
$nicename =~ s/$hex_bslash/\\/g; |
267 |
+ |
$nicename =~ s/$hex_space/ /g; |
268 |
+ |
$nicename =~ s/$hex_colon/:/g; |
269 |
+ |
&makegraph($machine, "$type-$name", "Disk IO for $machine on $nicename", "bytes per second", \@data, \@rawdata); |
270 |
+ |
} |
271 |
+ |
if($rrd =~ /^(net)-(\S+)\.rrd$/) { |
272 |
+ |
my(@data); |
273 |
+ |
my(@rawdata); |
274 |
+ |
push @data, "AREA:$1-$2:rx:rx#00FF00:OK:received bytes "; |
275 |
+ |
push @data, "LINE2:$1-$2:tx:tx#0000FF:OK:transfered bytes"; |
276 |
+ |
push @rawdata, "--base=1024"; |
277 |
+ |
# some name tidting |
278 |
+ |
my($type) = $1; |
279 |
+ |
my($name) = $2; |
280 |
+ |
my($nicename) = $2; |
281 |
+ |
$nicename =~ s/$hex_slash/\//g; |
282 |
+ |
$nicename =~ s/$hex_underscore/_/g; |
283 |
+ |
$nicename =~ s/$hex_bslash/\\/g; |
284 |
+ |
$nicename =~ s/$hex_space/ /g; |
285 |
+ |
$nicename =~ s/$hex_colon/:/g; |
286 |
+ |
&makegraph($machine, "$type-$name", "Network IO for $machine on $nicename", "bytes per second", \@data, \@rawdata); |
287 |
+ |
} |
288 |
+ |
if($rrd =~ /^(mailq)-(\S+)\.rrd$/) { |
289 |
+ |
my(@data); |
290 |
+ |
my(@rawdata); |
291 |
+ |
push @data, "LINE2:$1-$2:size:size#0000FF:OK:messages"; |
292 |
+ |
&makegraph($machine, "$1-$2", "Mail Queue ($2) Size for $machine", "messages in queue", \@data, \@rawdata); |
293 |
+ |
} |
294 |
|
# probably a queue with a name like this :) |
295 |
|
if($rrd =~ /^(\d+)_0\.rrd$/) { |
296 |
|
my(@data); |
298 |
|
my($baserrd) = $1; |
299 |
|
my($i) = 0; |
300 |
|
while( -f "$rrddir/$machine/$baserrd\_$i.rrd" ) { |
301 |
< |
push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":queue$i size "; |
301 |
> |
push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":OK:queue$i size "; |
302 |
|
++$i; |
303 |
|
} |
304 |
< |
push @data, "LINE2:$baserrd\_0:total:total#FF0000:packets/sec - currently"; |
171 |
< |
push @rawdata, "GPRINT:total:LAST:%lf %spackets/sec"; |
304 |
> |
push @data, "LINE2:$baserrd\_0:total:total#FF0000:OK:packets/sec "; |
305 |
|
my($comment); |
306 |
|
if(-f "$rrddir/$machine/$baserrd.def") { |
307 |
|
open(DEF, "$rrddir/$machine/$baserrd.def"); |
309 |
|
chomp $comment if defined $comment; |
310 |
|
} |
311 |
|
$comment = "unknown queue" if not defined $comment; |
312 |
< |
&makegraph($machine, $baserrd, $comment, \@data, \@rawdata); |
312 |
> |
&makegraph($machine, $baserrd, $comment, "", \@data, \@rawdata); |
313 |
|
} |
314 |
|
} |
315 |
|
# have a last check, maybe we can remove the directory now? |
316 |
< |
# Read the contents of the directory |
317 |
< |
opendir(DIR, "$rrddir/$machine"); |
318 |
< |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
319 |
< |
closedir DIR; |
320 |
< |
if($#dirlist == -1) { |
321 |
< |
print "pruning rrddir $rrddir/$machine\n"; |
322 |
< |
rmdir "$rrddir/$machine"; |
316 |
> |
# (only if we're deleting stuff) |
317 |
> |
if($deleterrds) { |
318 |
> |
# Read the contents of the directory |
319 |
> |
opendir(DIR, "$rrddir/$machine"); |
320 |
> |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
321 |
> |
closedir DIR; |
322 |
> |
if($#dirlist == -1) { |
323 |
> |
rmdir "$rrddir/$machine"; |
324 |
> |
&log("deleting empty rrd directory $rrddir/$machine\n"); |
325 |
> |
} |
326 |
|
} |
327 |
|
} |
328 |
|
|
329 |
< |
# Read the contents of the graphs directory |
330 |
< |
# and pull out the list of subdirectories (except . and .. :) |
331 |
< |
opendir(DIR, $imgdir); |
332 |
< |
my(@imgdirlist) = grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
333 |
< |
closedir DIR; |
198 |
< |
|
199 |
< |
# look through each directoty, as they might |
200 |
< |
# contain images for a particular machine |
201 |
< |
foreach my $machine (@imgdirlist) { |
202 |
< |
# Read the contents of the directory |
203 |
< |
opendir(DIR, "$imgdir/$machine"); |
204 |
< |
my(@imglist) = grep { /\.png$/ && -f "$imgdir/$machine/$_" } readdir(DIR); |
329 |
> |
if($deleteimgs) { |
330 |
> |
# Read the contents of the graphs directory |
331 |
> |
# and pull out the list of subdirectories (except . and .. :) |
332 |
> |
opendir(DIR, $imgdir); |
333 |
> |
my(@imgdirlist) = sort grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
334 |
|
closedir DIR; |
335 |
|
|
336 |
< |
# See what rrd we have, and generate the graphs accordingly |
337 |
< |
foreach my $img (@imglist) { |
338 |
< |
chomp $img; |
339 |
< |
# stat the img |
340 |
< |
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, |
341 |
< |
$ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img"); |
342 |
< |
# check if it's old enough to be deleted |
343 |
< |
if((time - $mtime) > $maxgraphage) { |
344 |
< |
print "pruning img $imgdir/$machine/$img\n"; |
345 |
< |
# if so, delete it |
346 |
< |
unlink("$imgdir/$machine/$img"); |
336 |
> |
# look through each directory, as they might |
337 |
> |
# contain images for a particular machine |
338 |
> |
foreach my $machine (@imgdirlist) { |
339 |
> |
# Read the contents of the directory |
340 |
> |
opendir(DIR, "$imgdir/$machine"); |
341 |
> |
my(@imglist) = grep { /\.png$/ && -f "$imgdir/$machine/$_" } readdir(DIR); |
342 |
> |
closedir DIR; |
343 |
> |
|
344 |
> |
# See what rrd we have, and generate the graphs accordingly |
345 |
> |
foreach my $img (@imglist) { |
346 |
> |
chomp $img; |
347 |
> |
# stat the img |
348 |
> |
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, |
349 |
> |
$ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img"); |
350 |
> |
# check if it's old enough to be deleted |
351 |
> |
if((time - $mtime) > $maximgage) { |
352 |
> |
# if so, delete it |
353 |
> |
unlink("$imgdir/$machine/$img"); |
354 |
> |
&log("deleted old image $imgdir/$machine/$img\n"); |
355 |
> |
} |
356 |
|
} |
357 |
+ |
# have a last check, maybe we can remove the directory now? |
358 |
+ |
# Read the contents of the directory |
359 |
+ |
opendir(DIR, "$imgdir/$machine"); |
360 |
+ |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
361 |
+ |
closedir DIR; |
362 |
+ |
if($#dirlist == -1) { |
363 |
+ |
rmdir "$imgdir/$machine"; |
364 |
+ |
&log("deleted empty image directory $imgdir/$machine\n"); |
365 |
+ |
} |
366 |
|
} |
220 |
– |
# have a last check, maybe we can remove the directory now? |
221 |
– |
# Read the contents of the directory |
222 |
– |
opendir(DIR, "$imgdir/$machine"); |
223 |
– |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
224 |
– |
closedir DIR; |
225 |
– |
if($#dirlist == -1) { |
226 |
– |
print "pruning img dir $imgdir/$machine\n"; |
227 |
– |
rmdir "$imgdir/$machine"; |
228 |
– |
} |
367 |
|
} |
368 |
|
|
369 |
+ |
exit(0); |
370 |
+ |
|
371 |
+ |
|
372 |
|
# |
373 |
|
# subroutine to make some graphs |
374 |
|
# |
378 |
|
# (eg. cpu) |
379 |
|
# $title = the title for the graph |
380 |
|
# (eg. kernow CPU usage) |
381 |
+ |
# $vlabel = the vertical label to apply to the left side of the graph |
382 |
+ |
# (eg. kb/s) |
383 |
|
# $dataref = a reference to an array containing information for the graph |
384 |
< |
# elements of format: "gtype:rrdname:dsname:name#colour:comment with spaces" |
384 |
> |
# elements of format: "gtype:rrdname:dsname:name#colour:legend:comment with spaces" |
385 |
|
# (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted) |
386 |
+ |
# (if legend is "NONE" the latest/average/max/min legend won't be printed) |
387 |
|
# $rawcmdref = a reference to an array containing raw rrd commands |
388 |
|
# elements a single command each, no spaces |
389 |
|
# |
390 |
|
|
391 |
|
sub makegraph() { |
392 |
< |
my($machine, $type, $title, $dataref, $rawcmdref) = @_; |
392 |
> |
my($machine, $type, $title, $vlabel, $dataref, $rawcmdref) = @_; |
393 |
|
# pass in these arrays by reference |
394 |
|
my(@data) = @$dataref if defined $dataref; |
395 |
|
my(@rawcmd) = @$rawcmdref if defined $rawcmdref; |
397 |
|
if(! -d "$imgdir/$machine") { |
398 |
|
# not sure on this umask, but it seems to work? |
399 |
|
mkdir "$imgdir/$machine", 0777; |
400 |
+ |
&log("created directory $imgdir/$machine\n"); |
401 |
|
} |
402 |
|
my(@rrdcmd); |
403 |
|
foreach my $dataitem (@data) { |
404 |
< |
# dataitem should be: "gtype:rrdname:dsname:name#colour:comment with spaces" |
404 |
> |
# dataitem should be: "gtype:rrdname:dsname:name#colour:legend:comment with spaces" |
405 |
|
# (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted) |
406 |
< |
if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(.*)$/) { |
406 |
> |
# (if legend is "NONE" the latest/average/max/min legend won't be printed) |
407 |
> |
if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(\S+):(.*)$/) { |
408 |
|
push @rrdcmd, "DEF:$4=$rrddir/$machine/$2.rrd:$3:AVERAGE"; |
409 |
|
if($1 ne "NONE") { |
410 |
< |
push @rrdcmd, "$1:$4#$5:$6"; |
410 |
> |
push @rrdcmd, "$1:$4#$5:$7"; |
411 |
> |
if($6 ne "NONE") { |
412 |
> |
# add some nice values to the legend |
413 |
> |
&addlegend(\@rrdcmd, $4); |
414 |
> |
} |
415 |
|
} |
416 |
|
} |
417 |
|
} |
418 |
|
push @rrdcmd, "--title=$title"; |
419 |
|
push @rrdcmd, "--imgformat=PNG"; |
420 |
|
push @rrdcmd, "--lower-limit=0"; |
421 |
+ |
push @rrdcmd, "--vertical-label=$vlabel"; |
422 |
|
# not entirely convinced this is good... |
423 |
|
push @rrdcmd, "--alt-autoscale-max"; |
424 |
|
# add any further raw commands |
425 |
|
push @rrdcmd, @rawcmd; |
426 |
|
RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd); |
427 |
|
my($err_3h) = RRDs::error; |
428 |
< |
print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h; |
428 |
> |
&log("created $imgdir/$machine/$type-3h.png\n") unless $err_3h; |
429 |
> |
&error("Error generating 3h graph for $machine/$type: $err_3h\n") if $err_3h; |
430 |
|
RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd); |
431 |
|
my($err_1d) = RRDs::error; |
432 |
< |
print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d; |
432 |
> |
&log("created $imgdir/$machine/$type-1d.png\n") unless $err_1d; |
433 |
> |
&error("Error generating 1d graph for $machine/$type: $err_1d\n") if $err_1d; |
434 |
|
RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd); |
435 |
|
my($err_1w) = RRDs::error; |
436 |
< |
print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w; |
436 |
> |
&log("created $imgdir/$machine/$type-1w.png\n") unless $err_1w; |
437 |
> |
&error("Error generating 1w graph for $machine/$type: $err_1w\n") if $err_1w; |
438 |
|
RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd); |
439 |
|
my($err_1m) = RRDs::error; |
440 |
< |
print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m; |
440 |
> |
&log("created $imgdir/$machine/$type-1m.png\n") unless $err_1m; |
441 |
> |
&error("Error generating 1m graph for $machine/$type: $err_1m\n") if $err_1m; |
442 |
|
RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd); |
443 |
|
my($err_1y) = RRDs::error; |
444 |
< |
print STDERR "Error generating 1y graph for $machine/$type: $err_1y\n" if $err_1y; |
444 |
> |
&log("created $imgdir/$machine/$type-1y.png\n") unless $err_1y; |
445 |
> |
&error("Error generating 1y graph for $machine/$type: $err_1y\n") if $err_1y; |
446 |
|
return; |
447 |
|
} |
448 |
|
|
449 |
+ |
# subroutine to add a legend |
450 |
+ |
# accepts reference to an array and a name |
451 |
+ |
sub addlegend() { |
452 |
+ |
my($dataref, $name) = @_; |
453 |
+ |
push @$dataref, "GPRINT:$name:LAST:Current\\: \%8.2lf %s"; |
454 |
+ |
push @$dataref, "GPRINT:$name:AVERAGE:Average\\: \%8.2lf %s"; |
455 |
+ |
push @$dataref, "GPRINT:$name:MAX:Max\\: \%8.2lf %s\\n"; |
456 |
+ |
} |
457 |
+ |
|
458 |
|
# hacky subroutine to return a colour |
459 |
|
# could be done much better somehow :/ |
460 |
|
sub get_colour { |
477 |
|
else { |
478 |
|
return "#000066"; |
479 |
|
} |
480 |
+ |
} |
481 |
+ |
|
482 |
+ |
# prints out usage information then exits |
483 |
+ |
sub usage() { |
484 |
+ |
print "Usage: graph.pl [options]\n"; |
485 |
+ |
print "Options\n"; |
486 |
+ |
print " -c config Specifies the configuration file\n"; |
487 |
+ |
print " default: rrdgraphing.conf\n"; |
488 |
+ |
print " -v Be verbose about what's happening\n"; |
489 |
+ |
print " -q Be quiet, even supress errors\n"; |
490 |
+ |
print " -V Print version number\n"; |
491 |
+ |
print " -h Prints this help page\n"; |
492 |
+ |
exit(1); |
493 |
+ |
} |
494 |
+ |
|
495 |
+ |
# prints a log message if verbose is turned on |
496 |
+ |
sub log() { |
497 |
+ |
my($msg) = @_; |
498 |
+ |
print $msg if $verbose; |
499 |
+ |
} |
500 |
+ |
|
501 |
+ |
# prints an error message unless quiet is turned on |
502 |
+ |
sub error() { |
503 |
+ |
my($msg) = @_; |
504 |
+ |
print STDERR $msg unless $quiet; |
505 |
|
} |