33 |
|
# possibly make more configurable? |
34 |
|
# -- allow configurable periods of graphs |
35 |
|
# -- comments, types, etc |
36 |
– |
# -- move all to external config file |
36 |
|
|
37 |
+ |
my($version) = '$Id$'; |
38 |
+ |
|
39 |
|
$| = 1; |
40 |
+ |
|
41 |
|
use strict; |
42 |
+ |
use Getopt::Std; |
43 |
|
use RRDs; |
44 |
|
|
45 |
< |
# Base directory for images |
46 |
< |
# (a directory will be constructed for each host under this) |
47 |
< |
my($imgdir) = "/home/pkg/iscream/public_html/graphs"; |
45 |
> |
# define variables that will be read from the config |
46 |
> |
# nb. keep this insync with the config file! |
47 |
> |
use vars qw{ |
48 |
> |
$imgdir $rrddir |
49 |
> |
$maxrrdage $maximgage $deleterrds $deleteimgs |
50 |
> |
$hex_slash $hex_underscore |
51 |
> |
$rrdstep $retry_wait |
52 |
> |
$verbose $quiet |
53 |
> |
}; |
54 |
|
|
55 |
< |
# Location of RRD databases |
56 |
< |
my($rrddir) = "/u1/i-scream/databases"; |
55 |
> |
# default locate of the config file |
56 |
> |
my($configfile) = "rrdgraphing.conf"; |
57 |
|
|
58 |
< |
# / converted to a decimal then hex'd |
59 |
< |
my($hex_slash) = "_2f"; |
60 |
< |
# _ converted to a decimal then hex'd |
61 |
< |
my($hex_underscore) = "_5f"; |
62 |
< |
|
58 |
> |
# check for command line arguments |
59 |
> |
my(%opts); |
60 |
> |
my($ret) = getopts('hvqVc:', \%opts); |
61 |
> |
|
62 |
> |
# if invalid argument given, $ret will not be 1 |
63 |
> |
&usage() if $ret != 1; |
64 |
> |
|
65 |
> |
# first process the arguments which might mean we exit now |
66 |
> |
|
67 |
> |
# -h is usage |
68 |
> |
if($opts{h}) { |
69 |
> |
&usage(); |
70 |
> |
} |
71 |
> |
# -V is version |
72 |
> |
if($opts{V}) { |
73 |
> |
print "graph.pl version: $version\n"; |
74 |
> |
exit(1); |
75 |
> |
} |
76 |
> |
|
77 |
> |
# Then try getting the config |
78 |
> |
|
79 |
> |
# -c specifies the config file location |
80 |
> |
if($opts{c}) { |
81 |
> |
$configfile = $opts{c}; |
82 |
> |
} |
83 |
> |
# suck in the config |
84 |
> |
&log("reading config from $configfile\n"); |
85 |
> |
do $configfile; |
86 |
> |
|
87 |
> |
# Then any options we might want to override the config with |
88 |
> |
|
89 |
> |
# -v is verbose |
90 |
> |
if($opts{v}) { |
91 |
> |
$verbose = $opts{v}; |
92 |
> |
} |
93 |
> |
# -q is verbose |
94 |
> |
if($opts{q}) { |
95 |
> |
$quiet = $opts{q}; |
96 |
> |
# if we're meant to be quiet, we can hardly be verbose! |
97 |
> |
$verbose = 0; |
98 |
> |
} |
99 |
> |
|
100 |
> |
|
101 |
|
# Read the contents of the base directory |
102 |
|
# and pull out the list of subdirectories (except . and .. :) |
103 |
|
opendir(DIR, $rrddir); |
115 |
|
# See what rrd we have, and generate the graphs accordingly |
116 |
|
foreach my $rrd (@rrdlist) { |
117 |
|
chomp $rrd; |
118 |
+ |
# stat the file |
119 |
+ |
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, |
120 |
+ |
$ctime,$blksize,$blocks) = stat("$rrddir/$machine/$rrd"); |
121 |
+ |
# check if it's old enough to be deleted |
122 |
+ |
if((time - $mtime) > $maxrrdage) { |
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); |
227 |
|
&makegraph($machine, $baserrd, $comment, \@data, \@rawdata); |
228 |
|
} |
229 |
|
} |
230 |
+ |
# have a last check, maybe we can remove the directory now? |
231 |
+ |
# (only if we're deleting stuff) |
232 |
+ |
if($deleterrds) { |
233 |
+ |
# Read the contents of the directory |
234 |
+ |
opendir(DIR, "$rrddir/$machine"); |
235 |
+ |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
236 |
+ |
closedir DIR; |
237 |
+ |
if($#dirlist == -1) { |
238 |
+ |
rmdir "$rrddir/$machine"; |
239 |
+ |
&log("deleting empty rrd directory $rrddir/$machine\n"); |
240 |
+ |
} |
241 |
+ |
} |
242 |
|
} |
243 |
|
|
244 |
+ |
if($deleteimgs) { |
245 |
+ |
# Read the contents of the graphs directory |
246 |
+ |
# and pull out the list of subdirectories (except . and .. :) |
247 |
+ |
opendir(DIR, $imgdir); |
248 |
+ |
my(@imgdirlist) = grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
249 |
+ |
closedir DIR; |
250 |
+ |
|
251 |
+ |
# look through each directoty, as they might |
252 |
+ |
# contain images for a particular machine |
253 |
+ |
foreach my $machine (@imgdirlist) { |
254 |
+ |
# Read the contents of the directory |
255 |
+ |
opendir(DIR, "$imgdir/$machine"); |
256 |
+ |
my(@imglist) = grep { /\.png$/ && -f "$imgdir/$machine/$_" } readdir(DIR); |
257 |
+ |
closedir DIR; |
258 |
+ |
|
259 |
+ |
# See what rrd we have, and generate the graphs accordingly |
260 |
+ |
foreach my $img (@imglist) { |
261 |
+ |
chomp $img; |
262 |
+ |
# stat the img |
263 |
+ |
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, |
264 |
+ |
$ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img"); |
265 |
+ |
# check if it's old enough to be deleted |
266 |
+ |
if((time - $mtime) > $maximgage) { |
267 |
+ |
# if so, delete it |
268 |
+ |
unlink("$imgdir/$machine/$img"); |
269 |
+ |
&log("deleted old image $imgdir/$machine/$img\n"); |
270 |
+ |
} |
271 |
+ |
} |
272 |
+ |
# have a last check, maybe we can remove the directory now? |
273 |
+ |
# Read the contents of the directory |
274 |
+ |
opendir(DIR, "$imgdir/$machine"); |
275 |
+ |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
276 |
+ |
closedir DIR; |
277 |
+ |
if($#dirlist == -1) { |
278 |
+ |
rmdir "$imgdir/$machine"; |
279 |
+ |
&log("deleted empty image directory $imgdir/$machine\n"); |
280 |
+ |
} |
281 |
+ |
} |
282 |
+ |
} |
283 |
+ |
|
284 |
+ |
exit(0); |
285 |
+ |
|
286 |
+ |
|
287 |
|
# |
288 |
|
# subroutine to make some graphs |
289 |
|
# |
309 |
|
if(! -d "$imgdir/$machine") { |
310 |
|
# not sure on this umask, but it seems to work? |
311 |
|
mkdir "$imgdir/$machine", 0777; |
312 |
+ |
&log("created directory $imgdir/$machine\n"); |
313 |
|
} |
314 |
|
my(@rrdcmd); |
315 |
|
foreach my $dataitem (@data) { |
331 |
|
push @rrdcmd, @rawcmd; |
332 |
|
RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd); |
333 |
|
my($err_3h) = RRDs::error; |
334 |
< |
print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h; |
334 |
> |
&log("created $imgdir/$machine/$type-3h.png\n") unless $err_3h; |
335 |
> |
&error("Error generating 3h graph for $machine/$type: $err_3h\n") if $err_3h; |
336 |
|
RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd); |
337 |
|
my($err_1d) = RRDs::error; |
338 |
< |
print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d; |
338 |
> |
&log("created $imgdir/$machine/$type-1d.png\n") unless $err_1d; |
339 |
> |
&error("Error generating 1d graph for $machine/$type: $err_1d\n") if $err_1d; |
340 |
|
RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd); |
341 |
|
my($err_1w) = RRDs::error; |
342 |
< |
print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w; |
342 |
> |
&log("created $imgdir/$machine/$type-1w.png\n") unless $err_1w; |
343 |
> |
&error("Error generating 1w graph for $machine/$type: $err_1w\n") if $err_1w; |
344 |
|
RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd); |
345 |
|
my($err_1m) = RRDs::error; |
346 |
< |
print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m; |
346 |
> |
&log("created $imgdir/$machine/$type-1m.png\n") unless $err_1m; |
347 |
> |
&error("Error generating 1m graph for $machine/$type: $err_1m\n") if $err_1m; |
348 |
|
RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd); |
349 |
|
my($err_1y) = RRDs::error; |
350 |
< |
print STDERR "Error generating 1y graph for $machine/$type: $err_1y\n" if $err_1y; |
350 |
> |
&log("created $imgdir/$machine/$type-1y.png\n") unless $err_1y; |
351 |
> |
&error("Error generating 1y graph for $machine/$type: $err_1y\n") if $err_1y; |
352 |
|
return; |
353 |
|
} |
354 |
|
|
374 |
|
else { |
375 |
|
return "#000066"; |
376 |
|
} |
377 |
+ |
} |
378 |
+ |
|
379 |
+ |
# prints out usage information then exits |
380 |
+ |
sub usage() { |
381 |
+ |
print "Usage: graph.pl [options]\n"; |
382 |
+ |
print "Options\n"; |
383 |
+ |
print " -c config Specifies the configuration file\n"; |
384 |
+ |
print " default: rrdgraphing.conf\n"; |
385 |
+ |
print " -v Be verbose about what's happening\n"; |
386 |
+ |
print " -q Be quiet, even supress errors\n"; |
387 |
+ |
print " -V Print version number\n"; |
388 |
+ |
print " -h Prints this help page\n"; |
389 |
+ |
exit(1); |
390 |
+ |
} |
391 |
+ |
|
392 |
+ |
# prints a log message if verbose is turned on |
393 |
+ |
sub log() { |
394 |
+ |
my($msg) = @_; |
395 |
+ |
print $msg if $verbose; |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
# prints an error message unless quiet is turned on |
399 |
+ |
sub error() { |
400 |
+ |
my($msg) = @_; |
401 |
+ |
print STDERR $msg unless $quiet; |
402 |
|
} |