1 |
tdb |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
# ----------------------------------------------------------- |
4 |
|
|
# i-scream graph generation script |
5 |
|
|
# http://www.i-scream.org.uk |
6 |
|
|
# |
7 |
|
|
# Generates graphs from rrd databases for i-scream data. |
8 |
|
|
# |
9 |
tdb |
1.4 |
# $Author: tdb $ |
10 |
|
|
# $Id: graph.pl,v 1.3 2002/03/10 03:19:57 tdb Exp $ |
11 |
tdb |
1.1 |
#------------------------------------------------------------ |
12 |
|
|
|
13 |
|
|
## TODO |
14 |
tdb |
1.4 |
# allow specification of lower/upper graph bounds |
15 |
|
|
# -- then replace mem/swap/disk "total" line ;) |
16 |
tdb |
1.1 |
# possibly make more configurable? |
17 |
|
|
# -- allow configurable periods of graphs |
18 |
|
|
# -- comments, types, etc |
19 |
|
|
# -- move all to external config file |
20 |
|
|
|
21 |
|
|
$| = 1; |
22 |
|
|
use strict; |
23 |
|
|
use RRDs; |
24 |
|
|
|
25 |
|
|
# Base directory for images |
26 |
|
|
# (a directory will be constructed for each host under this) |
27 |
|
|
my($imgdir) = "/home/tdb/public_html/rrd"; |
28 |
|
|
|
29 |
|
|
# Location of RRD databases |
30 |
|
|
my($rrddir) = "/u1/i-scream/rrd"; |
31 |
|
|
|
32 |
|
|
# / converted to a decimal then hex'd |
33 |
|
|
my($hex_slash) = "_2f"; |
34 |
|
|
# _ converted to a decimal then hex'd |
35 |
|
|
my($hex_underscore) = "_5f"; |
36 |
|
|
|
37 |
|
|
# Read the contents of the base directory |
38 |
|
|
# and pull out the list of subdirectories (except . and .. :) |
39 |
|
|
opendir(DIR, $rrddir); |
40 |
|
|
my(@rrddirlist) = grep { -d "$rrddir/$_" && !/^\.$/ && !/^\.\.$/ } readdir(DIR); |
41 |
|
|
closedir DIR; |
42 |
|
|
|
43 |
|
|
# look through each directoty, as they might |
44 |
|
|
# contain rrds for a particular machine |
45 |
|
|
foreach my $machine (@rrddirlist) { |
46 |
|
|
# Read the contents of the directory |
47 |
|
|
opendir(DIR, "$rrddir/$machine"); |
48 |
|
|
my(@rrdlist) = grep { /\.rrd$/ && -f "$rrddir/$machine/$_" } readdir(DIR); |
49 |
|
|
closedir DIR; |
50 |
|
|
|
51 |
|
|
# See what rrd we have, and generate the graphs accordingly |
52 |
|
|
foreach my $rrd (@rrdlist) { |
53 |
|
|
chomp $rrd; |
54 |
|
|
if($rrd =~ /^(cpu)\.rrd$/) { |
55 |
|
|
my(@data); |
56 |
tdb |
1.3 |
push @data, "LINE2:$1:idle:idle#00FF00:idle cpu"; |
57 |
|
|
push @data, "LINE2:$1:user:user#0000FF:user cpu"; |
58 |
|
|
push @data, "LINE2:$1:kernel:kernel#00FFFF:kernel cpu"; |
59 |
|
|
push @data, "LINE2:$1:swap:swap#FF00FF:swap cpu"; |
60 |
|
|
push @data, "LINE2:$1:iowait:iowait#FF0000:iowait cpu"; |
61 |
tdb |
1.1 |
&makegraph($machine, $1, "CPU Usage for $machine", \@data); |
62 |
|
|
} |
63 |
|
|
if($rrd =~ /^(mem)\.rrd$/) { |
64 |
|
|
my(@data); |
65 |
tdb |
1.3 |
push @data, "LINE2:$1:free:free#00FF00:free memory"; |
66 |
|
|
push @data, "LINE2:$1:total:total#0000FF:total memory"; |
67 |
tdb |
1.1 |
&makegraph($machine, $1, "Memory Usage for $machine", \@data); |
68 |
|
|
} |
69 |
|
|
if($rrd =~ /^(load)\.rrd$/) { |
70 |
|
|
my(@data); |
71 |
tdb |
1.3 |
push @data, "LINE2:$1:load1:load1#00FF00:1 minute load average"; |
72 |
|
|
push @data, "LINE2:$1:load5:load5#0000FF:5 minute load average"; |
73 |
|
|
push @data, "LINE2:$1:load15:load15#FF0000:15 minute load average"; |
74 |
tdb |
1.1 |
&makegraph($machine, $1, "Loads for $machine", \@data); |
75 |
|
|
} |
76 |
|
|
if($rrd =~ /^(proc)\.rrd$/) { |
77 |
|
|
my(@data); |
78 |
tdb |
1.3 |
push @data, "LINE2:$1:cpu:cpu#00FF00:cpu processes"; |
79 |
|
|
push @data, "LINE2:$1:sleeping:sleeping#0000FF:sleeping processes"; |
80 |
|
|
push @data, "LINE2:$1:stopped:stopped#00FFFF:stopped processes"; |
81 |
|
|
push @data, "LINE2:$1:total:total#FF00FF:total processes"; |
82 |
|
|
push @data, "LINE2:$1:zombie:zombie#FF0000:zombie processes"; |
83 |
tdb |
1.1 |
&makegraph($machine, $1, "Processes on $machine", \@data); |
84 |
|
|
} |
85 |
|
|
if($rrd =~ /^(swap)\.rrd$/) { |
86 |
|
|
my(@data); |
87 |
tdb |
1.3 |
push @data, "LINE2:$1:free:free#00FF00:free swap"; |
88 |
|
|
push @data, "LINE2:$1:total:total#0000FF:total swap"; |
89 |
tdb |
1.1 |
&makegraph($machine, $1, "Swap Usage for $machine", \@data); |
90 |
|
|
} |
91 |
|
|
if($rrd =~ /^(users)\.rrd$/) { |
92 |
|
|
my(@data); |
93 |
tdb |
1.3 |
push @data, "LINE2:$1:count:count#00FF00:user count"; |
94 |
tdb |
1.1 |
&makegraph($machine, $1, "User Count for $machine", \@data); |
95 |
|
|
} |
96 |
|
|
if($rrd =~ /^(disk)-(\S+).rrd$/) { |
97 |
|
|
my(@data); |
98 |
tdb |
1.3 |
push @data, "LINE2:$1-$2:kbytes:kbytes#0000FF:total size"; |
99 |
|
|
push @data, "LINE2:$1-$2:used:used#00FF00:used"; |
100 |
tdb |
1.1 |
my($type) = $1; |
101 |
|
|
my($name) = $2; |
102 |
|
|
my($nicename) = $2; |
103 |
|
|
$nicename =~ s/$hex_slash/\//g; |
104 |
|
|
$nicename =~ s/$hex_underscore/_/g; |
105 |
|
|
&makegraph($machine, "$type-$name", "Disk Usage for $machine on $nicename", \@data); |
106 |
|
|
} |
107 |
|
|
# probably a queue with a name like this :) |
108 |
|
|
if($rrd =~ /^(\d+)_0\.rrd$/) { |
109 |
|
|
my(@data); |
110 |
|
|
my(@rawdata); |
111 |
|
|
my($baserrd) = $1; |
112 |
|
|
my($i) = 0; |
113 |
|
|
while( -f "$rrddir/$machine/$baserrd\_$i.rrd" ) { |
114 |
tdb |
1.3 |
push @data, "LINE2:$baserrd\_$i:size:size$i" . &get_colour($i) . ":queue$i size "; |
115 |
tdb |
1.1 |
++$i; |
116 |
|
|
} |
117 |
tdb |
1.3 |
push @data, "LINE2:$baserrd\_0:total:total#FF0000:packets/sec - currently"; |
118 |
tdb |
1.2 |
push @rawdata, "GPRINT:total:LAST:%lf %spackets/sec"; |
119 |
tdb |
1.1 |
my($comment); |
120 |
|
|
if(-f "$rrddir/$machine/$baserrd.def") { |
121 |
|
|
open(DEF, "$rrddir/$machine/$baserrd.def"); |
122 |
|
|
$comment = <DEF>; |
123 |
|
|
chomp $comment if defined $comment; |
124 |
|
|
} |
125 |
|
|
$comment = "unknown queue" if not defined $comment; |
126 |
|
|
&makegraph($machine, $baserrd, $comment, \@data, \@rawdata); |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
# |
132 |
|
|
# subroutine to make some graphs |
133 |
|
|
# |
134 |
|
|
# $machine = name of the machine |
135 |
|
|
# (eg. kernow.ukc.ac.uk) |
136 |
|
|
# $type = the type of graph for the machine |
137 |
|
|
# (eg. cpu) |
138 |
|
|
# $title = the title for the graph |
139 |
|
|
# (eg. kernow CPU usage) |
140 |
|
|
# $dataref = a reference to an array containing information for the graph |
141 |
tdb |
1.3 |
# elements of format: "gtype:rrdname:dsname:name#colour:comment with spaces" |
142 |
tdb |
1.1 |
# $rawcmdref = a reference to an array containing raw rrd commands |
143 |
|
|
# elements a single command each, no spaces |
144 |
|
|
# |
145 |
tdb |
1.2 |
|
146 |
tdb |
1.1 |
sub makegraph() { |
147 |
|
|
my($machine, $type, $title, $dataref, $rawcmdref) = @_; |
148 |
|
|
# pass in these arrays by reference |
149 |
|
|
my(@data) = @$dataref if defined $dataref; |
150 |
|
|
my(@rawcmd) = @$rawcmdref if defined $rawcmdref; |
151 |
|
|
# check if directory exists for images |
152 |
|
|
if(! -d "$imgdir/$machine") { |
153 |
|
|
# not sure on this umask, but it seems to work? |
154 |
|
|
mkdir "$imgdir/$machine", 0777; |
155 |
|
|
} |
156 |
|
|
my(@rrdcmd); |
157 |
|
|
foreach my $dataitem (@data) { |
158 |
tdb |
1.3 |
# dataitem should be: "gtype:rrdname:dsname:name#colour:comment with spaces" |
159 |
|
|
if($dataitem =~ /^(\S+):(\S+):(\S+):(\S+)#(.{6}):(.*)$/) { |
160 |
|
|
push @rrdcmd, "DEF:$4=$rrddir/$machine/$2.rrd:$3:MAX"; |
161 |
|
|
push @rrdcmd, "$1:$4#$5:$6"; |
162 |
tdb |
1.1 |
} |
163 |
|
|
} |
164 |
|
|
push @rrdcmd, "--title=$title"; |
165 |
|
|
push @rrdcmd, "--imgformat=PNG"; |
166 |
|
|
push @rrdcmd, "--lower-limit=0"; |
167 |
|
|
# add any further raw commands |
168 |
|
|
push @rrdcmd, @rawcmd; |
169 |
|
|
RRDs::graph ("$imgdir/$machine/$type-3h.png", "--start=-10800", @rrdcmd); |
170 |
|
|
my($err_3h) = RRDs::error; |
171 |
tdb |
1.2 |
print STDERR "Error generating 3h graph for $machine/$type: $err_3h\n" if $err_3h; |
172 |
tdb |
1.1 |
RRDs::graph ("$imgdir/$machine/$type-1d.png", "--start=-86400", @rrdcmd); |
173 |
|
|
my($err_1d) = RRDs::error; |
174 |
tdb |
1.2 |
print STDERR "Error generating 1d graph for $machine/$type: $err_1d\n" if $err_1d; |
175 |
tdb |
1.1 |
RRDs::graph ("$imgdir/$machine/$type-1w.png", "--start=-604800", @rrdcmd); |
176 |
|
|
my($err_1w) = RRDs::error; |
177 |
tdb |
1.2 |
print STDERR "Error generating 1w graph for $machine/$type: $err_1w\n" if $err_1w; |
178 |
tdb |
1.1 |
RRDs::graph ("$imgdir/$machine/$type-1m.png", "--start=-2678400", @rrdcmd); |
179 |
|
|
my($err_1m) = RRDs::error; |
180 |
tdb |
1.2 |
print STDERR "Error generating 1m graph for $machine/$type: $err_1m\n" if $err_1m; |
181 |
tdb |
1.1 |
return; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
# hacky subroutine to return a colour |
185 |
|
|
# could be done much better somehow :/ |
186 |
|
|
sub get_colour { |
187 |
|
|
my($col) = @_; |
188 |
|
|
if($col == 0) { |
189 |
|
|
return "#0000FF"; |
190 |
|
|
} |
191 |
|
|
elsif($col == 1) { |
192 |
|
|
return "#00FF00"; |
193 |
|
|
} |
194 |
|
|
elsif($col == 2) { |
195 |
|
|
return "#FF00FF"; |
196 |
|
|
} |
197 |
|
|
elsif($col == 3) { |
198 |
|
|
return "#FFFF00"; |
199 |
|
|
} |
200 |
|
|
elsif($col == 4) { |
201 |
|
|
return "#00FFFF"; |
202 |
|
|
} |
203 |
|
|
else { |
204 |
|
|
return "#000066"; |
205 |
|
|
} |
206 |
|
|
} |