2 |
|
|
3 |
|
# |
4 |
|
# i-scream central monitoring system |
5 |
+ |
# http://www.i-scream.org.uk |
6 |
|
# Copyright (C) 2000-2002 i-scream |
7 |
|
# |
8 |
|
# This program is free software; you can redistribute it and/or |
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 |
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 |
62 |
< |
my($hex_underscore) = "_5f"; |
63 |
< |
|
59 |
> |
# check for command line arguments |
60 |
> |
my(%opts); |
61 |
> |
my($ret) = getopts('hvqVc:', \%opts); |
62 |
> |
|
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 |
> |
|
102 |
|
# Read the contents of the base directory |
103 |
|
# and pull out the list of subdirectories (except . and .. :) |
104 |
|
opendir(DIR, $rrddir); |
116 |
|
# See what rrd we have, and generate the graphs accordingly |
117 |
|
foreach my $rrd (@rrdlist) { |
118 |
|
chomp $rrd; |
119 |
+ |
# stat the file |
120 |
+ |
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, |
121 |
+ |
$ctime,$blksize,$blocks) = stat("$rrddir/$machine/$rrd"); |
122 |
+ |
# check if it's old enough to be deleted |
123 |
+ |
if((time - $mtime) > $maxrrdage) { |
124 |
+ |
# do we delete the rrd, or just ignore it? |
125 |
+ |
if($deleterrds) { |
126 |
+ |
# if so, delete it |
127 |
+ |
unlink("$rrddir/$machine/$rrd"); |
128 |
+ |
&log("deleted old rrd $rrddir/$machine/$rrd\n"); |
129 |
+ |
} |
130 |
+ |
else { |
131 |
+ |
&log("ignored old rrd $rrddir/$machine/$rrd\n"); |
132 |
+ |
} |
133 |
+ |
# no more processing required for this rrd |
134 |
+ |
next; |
135 |
+ |
} |
136 |
|
if($rrd =~ /^(cpu)\.rrd$/) { |
137 |
|
my(@data); |
138 |
|
my(@rawdata); |
228 |
|
&makegraph($machine, $baserrd, $comment, \@data, \@rawdata); |
229 |
|
} |
230 |
|
} |
231 |
+ |
# have a last check, maybe we can remove the directory now? |
232 |
+ |
# (only if we're deleting stuff) |
233 |
+ |
if($deleterrds) { |
234 |
+ |
# Read the contents of the directory |
235 |
+ |
opendir(DIR, "$rrddir/$machine"); |
236 |
+ |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
237 |
+ |
closedir DIR; |
238 |
+ |
if($#dirlist == -1) { |
239 |
+ |
rmdir "$rrddir/$machine"; |
240 |
+ |
&log("deleting empty rrd directory $rrddir/$machine\n"); |
241 |
+ |
} |
242 |
+ |
} |
243 |
|
} |
244 |
|
|
245 |
+ |
if($deleteimgs) { |
246 |
+ |
# Read the contents of the graphs directory |
247 |
+ |
# and pull out the list of subdirectories (except . and .. :) |
248 |
+ |
opendir(DIR, $imgdir); |
249 |
+ |
my(@imgdirlist) = grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
250 |
+ |
closedir DIR; |
251 |
+ |
|
252 |
+ |
# look through each directoty, as they might |
253 |
+ |
# contain images for a particular machine |
254 |
+ |
foreach my $machine (@imgdirlist) { |
255 |
+ |
# Read the contents of the directory |
256 |
+ |
opendir(DIR, "$imgdir/$machine"); |
257 |
+ |
my(@imglist) = grep { /\.png$/ && -f "$imgdir/$machine/$_" } readdir(DIR); |
258 |
+ |
closedir DIR; |
259 |
+ |
|
260 |
+ |
# See what rrd we have, and generate the graphs accordingly |
261 |
+ |
foreach my $img (@imglist) { |
262 |
+ |
chomp $img; |
263 |
+ |
# stat the img |
264 |
+ |
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, |
265 |
+ |
$ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img"); |
266 |
+ |
# check if it's old enough to be deleted |
267 |
+ |
if((time - $mtime) > $maximgage) { |
268 |
+ |
# if so, delete it |
269 |
+ |
unlink("$imgdir/$machine/$img"); |
270 |
+ |
&log("deleted old image $imgdir/$machine/$img\n"); |
271 |
+ |
} |
272 |
+ |
} |
273 |
+ |
# have a last check, maybe we can remove the directory now? |
274 |
+ |
# Read the contents of the directory |
275 |
+ |
opendir(DIR, "$imgdir/$machine"); |
276 |
+ |
my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
277 |
+ |
closedir DIR; |
278 |
+ |
if($#dirlist == -1) { |
279 |
+ |
rmdir "$imgdir/$machine"; |
280 |
+ |
&log("deleted empty image directory $imgdir/$machine\n"); |
281 |
+ |
} |
282 |
+ |
} |
283 |
+ |
} |
284 |
+ |
|
285 |
+ |
exit(0); |
286 |
+ |
|
287 |
+ |
|
288 |
|
# |
289 |
|
# subroutine to make some graphs |
290 |
|
# |
310 |
|
if(! -d "$imgdir/$machine") { |
311 |
|
# not sure on this umask, but it seems to work? |
312 |
|
mkdir "$imgdir/$machine", 0777; |
313 |
+ |
&log("created directory $imgdir/$machine\n"); |
314 |
|
} |
315 |
|
my(@rrdcmd); |
316 |
|
foreach my $dataitem (@data) { |
332 |
|
push @rrdcmd, @rawcmd; |
333 |
|
RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd); |
334 |
|
my($err_3h) = RRDs::error; |
335 |
< |
print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h; |
335 |
> |
&log("created $imgdir/$machine/$type-3h.png\n") unless $err_3h; |
336 |
> |
&error("Error generating 3h graph for $machine/$type: $err_3h\n") if $err_3h; |
337 |
|
RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd); |
338 |
|
my($err_1d) = RRDs::error; |
339 |
< |
print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d; |
339 |
> |
&log("created $imgdir/$machine/$type-1d.png\n") unless $err_1d; |
340 |
> |
&error("Error generating 1d graph for $machine/$type: $err_1d\n") if $err_1d; |
341 |
|
RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd); |
342 |
|
my($err_1w) = RRDs::error; |
343 |
< |
print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w; |
343 |
> |
&log("created $imgdir/$machine/$type-1w.png\n") unless $err_1w; |
344 |
> |
&error("Error generating 1w graph for $machine/$type: $err_1w\n") if $err_1w; |
345 |
|
RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd); |
346 |
|
my($err_1m) = RRDs::error; |
347 |
< |
print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m; |
347 |
> |
&log("created $imgdir/$machine/$type-1m.png\n") unless $err_1m; |
348 |
> |
&error("Error generating 1m graph for $machine/$type: $err_1m\n") if $err_1m; |
349 |
|
RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd); |
350 |
|
my($err_1y) = RRDs::error; |
351 |
< |
print STDERR "Error generating 1y graph for $machine/$type: $err_1y\n" if $err_1y; |
351 |
> |
&log("created $imgdir/$machine/$type-1y.png\n") unless $err_1y; |
352 |
> |
&error("Error generating 1y graph for $machine/$type: $err_1y\n") if $err_1y; |
353 |
|
return; |
354 |
|
} |
355 |
|
|
375 |
|
else { |
376 |
|
return "#000066"; |
377 |
|
} |
378 |
+ |
} |
379 |
+ |
|
380 |
+ |
# prints out usage information then exits |
381 |
+ |
sub usage() { |
382 |
+ |
print "Usage: graph.pl [options]\n"; |
383 |
+ |
print "Options\n"; |
384 |
+ |
print " -c config Specifies the configuration file\n"; |
385 |
+ |
print " default: rrdgraphing.conf\n"; |
386 |
+ |
print " -v Be verbose about what's happening\n"; |
387 |
+ |
print " -q Be quiet, even supress errors\n"; |
388 |
+ |
print " -V Print version number\n"; |
389 |
+ |
print " -h Prints this help page\n"; |
390 |
+ |
exit(1); |
391 |
+ |
} |
392 |
+ |
|
393 |
+ |
# prints a log message if verbose is turned on |
394 |
+ |
sub log() { |
395 |
+ |
my($msg) = @_; |
396 |
+ |
print $msg if $verbose; |
397 |
+ |
} |
398 |
+ |
|
399 |
+ |
# prints an error message unless quiet is turned on |
400 |
+ |
sub error() { |
401 |
+ |
my($msg) = @_; |
402 |
+ |
print STDERR $msg unless $quiet; |
403 |
|
} |