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