ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/reports/rrdgraphing/graph.pl
Revision: 1.3
Committed: Mon May 20 16:11:23 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +65 -2 lines
Log Message:
Added some cleaning up of old rrd files (ones that haven't been modified
recently) and old image files. Only problem is .def files not being deleted
on the queue graphs - this isn't too much of an issues because it doesn't
affect the webpages on the frontend (unlike old out-of-date rrds do).

File Contents

# User Rev Content
1 tdb 1.1 #!/usr/bin/perl -w
2    
3 tdb 1.2 #
4     # i-scream central monitoring system
5     # Copyright (C) 2000-2002 i-scream
6     #
7     # This program is free software; you can redistribute it and/or
8     # modify it under the terms of the GNU General Public License
9     # as published by the Free Software Foundation; either version 2
10     # of the License, or (at your option) any later version.
11     #
12     # This program is distributed in the hope that it will be useful,
13     # but WITHOUT ANY WARRANTY; without even the implied warranty of
14     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     # GNU General Public License for more details.
16     #
17     # You should have received a copy of the GNU General Public License
18     # along with this program; if not, write to the Free Software
19     # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20     #
21    
22 tdb 1.1 # -----------------------------------------------------------
23     # i-scream graph generation script
24     # http://www.i-scream.org.uk
25     #
26     # Generates graphs from rrd databases for i-scream data.
27     #
28     # $Author: tdb $
29 tdb 1.3 # $Id: graph.pl,v 1.2 2002/05/18 18:15:59 tdb Exp $
30 tdb 1.1 #------------------------------------------------------------
31    
32     ## TODO
33     # possibly make more configurable?
34     # -- allow configurable periods of graphs
35     # -- comments, types, etc
36     # -- move all to external config file
37    
38     $| = 1;
39     use strict;
40     use RRDs;
41    
42     # Base directory for images
43     # (a directory will be constructed for each host under this)
44     my($imgdir) = "/home/pkg/iscream/public_html/graphs";
45    
46     # Location of RRD databases
47     my($rrddir) = "/u1/i-scream/databases";
48    
49     # / converted to a decimal then hex'd
50     my($hex_slash) = "_2f";
51     # _ converted to a decimal then hex'd
52     my($hex_underscore) = "_5f";
53 tdb 1.3
54     # maximum age (last modified) before an rrd or graph get cleaned up
55     # (in seconds)
56     my($maxrrdage) = 3600; # 1 hour
57     my($maxgraphage) = 3600; # 1 hour
58    
59 tdb 1.1 # Read the contents of the base directory
60     # and pull out the list of subdirectories (except . and .. :)
61     opendir(DIR, $rrddir);
62     my(@rrddirlist) = grep { -d "$rrddir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR);
63     closedir DIR;
64    
65     # look through each directoty, as they might
66     # contain rrds for a particular machine
67     foreach my $machine (@rrddirlist) {
68     # Read the contents of the directory
69     opendir(DIR, "$rrddir/$machine");
70     my(@rrdlist) = grep { /\.rrd$/ && -f "$rrddir/$machine/$_" } readdir(DIR);
71     closedir DIR;
72    
73     # See what rrd we have, and generate the graphs accordingly
74     foreach my $rrd (@rrdlist) {
75     chomp $rrd;
76 tdb 1.3 # stat the file
77     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
78     $ctime,$blksize,$blocks) = stat("$rrddir/$machine/$rrd");
79     # check if it's old enough to be deleted
80     if((time - $mtime) > $maxrrdage) {
81     print "pruning rrd $rrddir/$machine/$rrd\n";
82     # if so, delete it
83     unlink("$rrddir/$machine/$rrd");
84     # no more processing required for this rrd
85     next;
86     }
87 tdb 1.1 if($rrd =~ /^(cpu)\.rrd$/) {
88     my(@data);
89     my(@rawdata);
90     push @data, "LINE2:$1:idle:idle#00FF00:idle cpu";
91     push @data, "LINE2:$1:user:user#0000FF:user cpu";
92     push @data, "LINE2:$1:kernel:kernel#00FFFF:kernel cpu";
93     push @data, "LINE2:$1:swap:swap#FF00FF:swap cpu";
94     push @data, "LINE2:$1:iowait:iowait#FF0000:iowait cpu";
95     push @rawdata, "--upper-limit=100";
96     &makegraph($machine, $1, "CPU Usage for $machine", \@data, \@rawdata);
97     }
98     if($rrd =~ /^(mem)\.rrd$/) {
99     my(@data);
100     my(@rawdata);
101     # we don't actually want to display free memory,
102     # although we need it to do inuse...
103     push @data, "NONE:$1:free:free#CCCCFF:free memory";
104     push @data, "LINE2:$1:total:total#0000FF:total memory";
105     # calculate inuse
106     push @rawdata, "CDEF:inuse=total,free,-";
107     # and add it to the graph
108     push @rawdata, "AREA:inuse#CCCCFF:memory in use";
109     push @rawdata, "--base=1024";
110     &makegraph($machine, $1, "Memory Usage for $machine", \@data, \@rawdata);
111     }
112     if($rrd =~ /^(load)\.rrd$/) {
113     my(@data);
114     push @data, "LINE2:$1:load1:load1#CCCCFF:1 minute load average";
115     push @data, "LINE2:$1:load5:load5#7777FF:5 minute load average";
116     push @data, "LINE2:$1:load15:load15#0000FF:15 minute load average";
117     &makegraph($machine, $1, "Loads for $machine", \@data);
118     }
119     if($rrd =~ /^(proc)\.rrd$/) {
120     my(@data);
121     push @data, "LINE2:$1:cpu:cpu#00FF00:cpu processes";
122     push @data, "LINE2:$1:sleeping:sleeping#0000FF:sleeping processes";
123     push @data, "LINE2:$1:stopped:stopped#00FFFF:stopped processes";
124     push @data, "LINE2:$1:total:total#FF00FF:total processes";
125     push @data, "LINE2:$1:zombie:zombie#FF0000:zombie processes";
126     &makegraph($machine, $1, "Processes on $machine", \@data);
127     }
128     if($rrd =~ /^(swap)\.rrd$/) {
129     my(@data);
130     my(@rawdata);
131     # we don't actually want to display free swap,
132     # although we need it to do inuse...
133     push @data, "NONE:$1:free:free#CCCCFF:free swap";
134     push @data, "LINE2:$1:total:total#0000FF:total swap";
135     # calculate inuse
136     push @rawdata, "CDEF:inuse=total,free,-";
137     # and add it to the graph
138     push @rawdata, "AREA:inuse#CCCCFF:swap in use";
139     push @rawdata, "--base=1024";
140     &makegraph($machine, $1, "Swap Usage for $machine", \@data, \@rawdata);
141     }
142     if($rrd =~ /^(users)\.rrd$/) {
143     my(@data);
144     push @data, "AREA:$1:count:count#CCCCFF:user count";
145     &makegraph($machine, $1, "User Count for $machine", \@data);
146     }
147     if($rrd =~ /^(disk)-(\S+).rrd$/) {
148     my(@data);
149     my(@rawdata);
150     push @data, "LINE2:$1-$2:kbytes:kbytes#0000FF:total size";
151     push @data, "AREA:$1-$2:used:used#CCCCFF:used";
152     push @rawdata, "--base=1024";
153     my($type) = $1;
154     my($name) = $2;
155     my($nicename) = $2;
156     $nicename =~ s/$hex_slash/\//g;
157     $nicename =~ s/$hex_underscore/_/g;
158     &makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", \@data, \@rawdata);
159     }
160     # probably a queue with a name like this :)
161     if($rrd =~ /^(\d+)_0\.rrd$/) {
162     my(@data);
163     my(@rawdata);
164     my($baserrd) = $1;
165     my($i) = 0;
166     while( -f "$rrddir/$machine/$baserrd\_$i.rrd" ) {
167     push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":queue$i size ";
168     ++$i;
169     }
170     push @data, "LINE2:$baserrd\_0:total:total#FF0000:packets/sec - currently";
171     push @rawdata, "GPRINT:total:LAST:%lf %spackets/sec";
172     my($comment);
173     if(-f "$rrddir/$machine/$baserrd.def") {
174     open(DEF, "$rrddir/$machine/$baserrd.def");
175     $comment = <DEF>;
176     chomp $comment if defined $comment;
177     }
178     $comment = "unknown queue" if not defined $comment;
179     &makegraph($machine, $baserrd, $comment, \@data, \@rawdata);
180     }
181 tdb 1.3 }
182     # have a last check, maybe we can remove the directory now?
183     # Read the contents of the directory
184     opendir(DIR, "$rrddir/$machine");
185     my(@dirlist) = grep { !/^\.$/ && !/^\.\.$/ } readdir(DIR);
186     closedir DIR;
187     if($#dirlist == -1) {
188     print "pruning rrddir $rrddir/$machine\n";
189     rmdir "$rrddir/$machine";
190     }
191     }
192    
193     # Read the contents of the graphs directory
194     # and pull out the list of subdirectories (except . and .. :)
195     opendir(DIR, $imgdir);
196     my(@imgdirlist) = grep { -d "$imgdir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR);
197     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);
205     closedir DIR;
206    
207     # See what rrd we have, and generate the graphs accordingly
208     foreach my $img (@imglist) {
209     chomp $img;
210     # stat the img
211     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
212     $ctime,$blksize,$blocks) = stat("$imgdir/$machine/$img");
213     # check if it's old enough to be deleted
214     if((time - $mtime) > $maxgraphage) {
215     print "pruning img $imgdir/$machine/$img\n";
216     # if so, delete it
217     unlink("$imgdir/$machine/$img");
218     }
219     }
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 tdb 1.1 }
229     }
230    
231     #
232     # subroutine to make some graphs
233     #
234     # $machine = name of the machine
235     # (eg. kernow.ukc.ac.uk)
236     # $type = the type of graph for the machine
237     # (eg. cpu)
238     # $title = the title for the graph
239     # (eg. kernow CPU usage)
240     # $dataref = a reference to an array containing information for the graph
241     # elements of format: "gtype:rrdname:dsname:name#colour:comment with spaces"
242     # (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
243     # $rawcmdref = a reference to an array containing raw rrd commands
244     # elements a single command each, no spaces
245     #
246    
247     sub makegraph() {
248     my($machine, $type, $title, $dataref, $rawcmdref) = @_;
249     # pass in these arrays by reference
250     my(@data) = @$dataref if defined $dataref;
251     my(@rawcmd) = @$rawcmdref if defined $rawcmdref;
252     # check if directory exists for images
253     if(! -d "$imgdir/$machine") {
254     # not sure on this umask, but it seems to work?
255     mkdir "$imgdir/$machine", 0777;
256     }
257     my(@rrdcmd);
258     foreach my $dataitem (@data) {
259     # dataitem should be: "gtype:rrdname:dsname:name#colour:comment with spaces"
260     # (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted)
261     if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(.*)$/) {
262     push @rrdcmd, "DEF:$4=$rrddir/$machine/$2.rrd:$3:AVERAGE";
263     if($1 ne "NONE") {
264     push @rrdcmd, "$1:$4#$5:$6";
265     }
266     }
267     }
268     push @rrdcmd, "--title=$title";
269     push @rrdcmd, "--imgformat=PNG";
270     push @rrdcmd, "--lower-limit=0";
271     # not entirely convinced this is good...
272     push @rrdcmd, "--alt-autoscale-max";
273     # add any further raw commands
274     push @rrdcmd, @rawcmd;
275     RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd);
276     my($err_3h) = RRDs::error;
277     print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h;
278     RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd);
279     my($err_1d) = RRDs::error;
280     print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d;
281     RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd);
282     my($err_1w) = RRDs::error;
283     print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w;
284     RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd);
285     my($err_1m) = RRDs::error;
286     print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m;
287     RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd);
288     my($err_1y) = RRDs::error;
289     print STDERR "Error generating 1y graph for $machine/$type: $err_1y\n" if $err_1y;
290     return;
291     }
292    
293     # hacky subroutine to return a colour
294     # could be done much better somehow :/
295     sub get_colour {
296     my($col) = @_;
297     if($col == 0) {
298     return "#0000FF";
299     }
300     elsif($col == 1) {
301     return "#00FF00";
302     }
303     elsif($col == 2) {
304     return "#FF00FF";
305     }
306     elsif($col == 3) {
307     return "#FFFF00";
308     }
309     elsif($col == 4) {
310     return "#00FFFF";
311     }
312     else {
313     return "#000066";
314     }
315     }