1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# |
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 |
# ----------------------------------------------------------- |
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 |
# $Id: graph.pl,v 1.1 2002/03/18 13:24:31 tdb Exp $ |
30 |
#------------------------------------------------------------ |
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 |
|
54 |
# Read the contents of the base directory |
55 |
# and pull out the list of subdirectories (except . and .. :) |
56 |
opendir(DIR, $rrddir); |
57 |
my(@rrddirlist) = grep { -d "$rrddir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
58 |
closedir DIR; |
59 |
|
60 |
# look through each directoty, as they might |
61 |
# contain rrds for a particular machine |
62 |
foreach my $machine (@rrddirlist) { |
63 |
# Read the contents of the directory |
64 |
opendir(DIR, "$rrddir/$machine"); |
65 |
my(@rrdlist) = grep { /\.rrd$/ && -f "$rrddir/$machine/$_" } readdir(DIR); |
66 |
closedir DIR; |
67 |
|
68 |
# See what rrd we have, and generate the graphs accordingly |
69 |
foreach my $rrd (@rrdlist) { |
70 |
chomp $rrd; |
71 |
if($rrd =~ /^(cpu)\.rrd$/) { |
72 |
my(@data); |
73 |
my(@rawdata); |
74 |
push @data, "LINE2:$1:idle:idle#00FF00:idle cpu"; |
75 |
push @data, "LINE2:$1:user:user#0000FF:user cpu"; |
76 |
push @data, "LINE2:$1:kernel:kernel#00FFFF:kernel cpu"; |
77 |
push @data, "LINE2:$1:swap:swap#FF00FF:swap cpu"; |
78 |
push @data, "LINE2:$1:iowait:iowait#FF0000:iowait cpu"; |
79 |
push @rawdata, "--upper-limit=100"; |
80 |
&makegraph($machine, $1, "CPU Usage for $machine", \@data, \@rawdata); |
81 |
} |
82 |
if($rrd =~ /^(mem)\.rrd$/) { |
83 |
my(@data); |
84 |
my(@rawdata); |
85 |
# we don't actually want to display free memory, |
86 |
# although we need it to do inuse... |
87 |
push @data, "NONE:$1:free:free#CCCCFF:free memory"; |
88 |
push @data, "LINE2:$1:total:total#0000FF:total memory"; |
89 |
# calculate inuse |
90 |
push @rawdata, "CDEF:inuse=total,free,-"; |
91 |
# and add it to the graph |
92 |
push @rawdata, "AREA:inuse#CCCCFF:memory in use"; |
93 |
push @rawdata, "--base=1024"; |
94 |
&makegraph($machine, $1, "Memory Usage for $machine", \@data, \@rawdata); |
95 |
} |
96 |
if($rrd =~ /^(load)\.rrd$/) { |
97 |
my(@data); |
98 |
push @data, "LINE2:$1:load1:load1#CCCCFF:1 minute load average"; |
99 |
push @data, "LINE2:$1:load5:load5#7777FF:5 minute load average"; |
100 |
push @data, "LINE2:$1:load15:load15#0000FF:15 minute load average"; |
101 |
&makegraph($machine, $1, "Loads for $machine", \@data); |
102 |
} |
103 |
if($rrd =~ /^(proc)\.rrd$/) { |
104 |
my(@data); |
105 |
push @data, "LINE2:$1:cpu:cpu#00FF00:cpu processes"; |
106 |
push @data, "LINE2:$1:sleeping:sleeping#0000FF:sleeping processes"; |
107 |
push @data, "LINE2:$1:stopped:stopped#00FFFF:stopped processes"; |
108 |
push @data, "LINE2:$1:total:total#FF00FF:total processes"; |
109 |
push @data, "LINE2:$1:zombie:zombie#FF0000:zombie processes"; |
110 |
&makegraph($machine, $1, "Processes on $machine", \@data); |
111 |
} |
112 |
if($rrd =~ /^(swap)\.rrd$/) { |
113 |
my(@data); |
114 |
my(@rawdata); |
115 |
# we don't actually want to display free swap, |
116 |
# although we need it to do inuse... |
117 |
push @data, "NONE:$1:free:free#CCCCFF:free swap"; |
118 |
push @data, "LINE2:$1:total:total#0000FF:total swap"; |
119 |
# calculate inuse |
120 |
push @rawdata, "CDEF:inuse=total,free,-"; |
121 |
# and add it to the graph |
122 |
push @rawdata, "AREA:inuse#CCCCFF:swap in use"; |
123 |
push @rawdata, "--base=1024"; |
124 |
&makegraph($machine, $1, "Swap Usage for $machine", \@data, \@rawdata); |
125 |
} |
126 |
if($rrd =~ /^(users)\.rrd$/) { |
127 |
my(@data); |
128 |
push @data, "AREA:$1:count:count#CCCCFF:user count"; |
129 |
&makegraph($machine, $1, "User Count for $machine", \@data); |
130 |
} |
131 |
if($rrd =~ /^(disk)-(\S+).rrd$/) { |
132 |
my(@data); |
133 |
my(@rawdata); |
134 |
push @data, "LINE2:$1-$2:kbytes:kbytes#0000FF:total size"; |
135 |
push @data, "AREA:$1-$2:used:used#CCCCFF:used"; |
136 |
push @rawdata, "--base=1024"; |
137 |
my($type) = $1; |
138 |
my($name) = $2; |
139 |
my($nicename) = $2; |
140 |
$nicename =~ s/$hex_slash/\//g; |
141 |
$nicename =~ s/$hex_underscore/_/g; |
142 |
&makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", \@data, \@rawdata); |
143 |
} |
144 |
# probably a queue with a name like this :) |
145 |
if($rrd =~ /^(\d+)_0\.rrd$/) { |
146 |
my(@data); |
147 |
my(@rawdata); |
148 |
my($baserrd) = $1; |
149 |
my($i) = 0; |
150 |
while( -f "$rrddir/$machine/$baserrd\_$i.rrd" ) { |
151 |
push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":queue$i size "; |
152 |
++$i; |
153 |
} |
154 |
push @data, "LINE2:$baserrd\_0:total:total#FF0000:packets/sec - currently"; |
155 |
push @rawdata, "GPRINT:total:LAST:%lf %spackets/sec"; |
156 |
my($comment); |
157 |
if(-f "$rrddir/$machine/$baserrd.def") { |
158 |
open(DEF, "$rrddir/$machine/$baserrd.def"); |
159 |
$comment = <DEF>; |
160 |
chomp $comment if defined $comment; |
161 |
} |
162 |
$comment = "unknown queue" if not defined $comment; |
163 |
&makegraph($machine, $baserrd, $comment, \@data, \@rawdata); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
# |
169 |
# subroutine to make some graphs |
170 |
# |
171 |
# $machine = name of the machine |
172 |
# (eg. kernow.ukc.ac.uk) |
173 |
# $type = the type of graph for the machine |
174 |
# (eg. cpu) |
175 |
# $title = the title for the graph |
176 |
# (eg. kernow CPU usage) |
177 |
# $dataref = a reference to an array containing information for the graph |
178 |
# elements of format: "gtype:rrdname:dsname:name#colour:comment with spaces" |
179 |
# (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted) |
180 |
# $rawcmdref = a reference to an array containing raw rrd commands |
181 |
# elements a single command each, no spaces |
182 |
# |
183 |
|
184 |
sub makegraph() { |
185 |
my($machine, $type, $title, $dataref, $rawcmdref) = @_; |
186 |
# pass in these arrays by reference |
187 |
my(@data) = @$dataref if defined $dataref; |
188 |
my(@rawcmd) = @$rawcmdref if defined $rawcmdref; |
189 |
# check if directory exists for images |
190 |
if(! -d "$imgdir/$machine") { |
191 |
# not sure on this umask, but it seems to work? |
192 |
mkdir "$imgdir/$machine", 0777; |
193 |
} |
194 |
my(@rrdcmd); |
195 |
foreach my $dataitem (@data) { |
196 |
# dataitem should be: "gtype:rrdname:dsname:name#colour:comment with spaces" |
197 |
# (if gtype is "NONE" only a DEF of 'name' will be defined, no line will be plotted) |
198 |
if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(.*)$/) { |
199 |
push @rrdcmd, "DEF:$4=$rrddir/$machine/$2.rrd:$3:AVERAGE"; |
200 |
if($1 ne "NONE") { |
201 |
push @rrdcmd, "$1:$4#$5:$6"; |
202 |
} |
203 |
} |
204 |
} |
205 |
push @rrdcmd, "--title=$title"; |
206 |
push @rrdcmd, "--imgformat=PNG"; |
207 |
push @rrdcmd, "--lower-limit=0"; |
208 |
# not entirely convinced this is good... |
209 |
push @rrdcmd, "--alt-autoscale-max"; |
210 |
# add any further raw commands |
211 |
push @rrdcmd, @rawcmd; |
212 |
RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd); |
213 |
my($err_3h) = RRDs::error; |
214 |
print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h; |
215 |
RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd); |
216 |
my($err_1d) = RRDs::error; |
217 |
print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d; |
218 |
RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd); |
219 |
my($err_1w) = RRDs::error; |
220 |
print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w; |
221 |
RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd); |
222 |
my($err_1m) = RRDs::error; |
223 |
print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m; |
224 |
RRDs::graph ("$imgdir/$machine/$type-1y.png", "--start=-31536000", @rrdcmd); |
225 |
my($err_1y) = RRDs::error; |
226 |
print STDERR "Error generating 1y graph for $machine/$type: $err_1y\n" if $err_1y; |
227 |
return; |
228 |
} |
229 |
|
230 |
# hacky subroutine to return a colour |
231 |
# could be done much better somehow :/ |
232 |
sub get_colour { |
233 |
my($col) = @_; |
234 |
if($col == 0) { |
235 |
return "#0000FF"; |
236 |
} |
237 |
elsif($col == 1) { |
238 |
return "#00FF00"; |
239 |
} |
240 |
elsif($col == 2) { |
241 |
return "#FF00FF"; |
242 |
} |
243 |
elsif($col == 3) { |
244 |
return "#FFFF00"; |
245 |
} |
246 |
elsif($col == 4) { |
247 |
return "#00FFFF"; |
248 |
} |
249 |
else { |
250 |
return "#000066"; |
251 |
} |
252 |
} |