1 |
tdb |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
tdb |
1.2 |
# |
4 |
|
|
# i-scream central monitoring system |
5 |
tdb |
1.8 |
# 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 scripts |
25 |
tdb |
1.8 |
# http://www.i-scream.org |
26 |
tdb |
1.1 |
# |
27 |
|
|
# Generates rrd databases for i-scream data by connecting to |
28 |
|
|
# the i-scream server and collecting data. |
29 |
|
|
# |
30 |
|
|
# $Author: tdb $ |
31 |
tdb |
1.9 |
# $Id: watch.pl,v 1.8 2004/08/01 10:40:32 tdb Exp $ |
32 |
tdb |
1.1 |
#------------------------------------------------------------ |
33 |
|
|
|
34 |
tdb |
1.9 |
my($version) = '$Id: watch.pl,v 1.8 2004/08/01 10:40:32 tdb Exp $'; |
35 |
tdb |
1.1 |
|
36 |
|
|
$| = 1; |
37 |
|
|
|
38 |
|
|
use strict; |
39 |
tdb |
1.3 |
use Getopt::Std; |
40 |
|
|
use IO::Socket; |
41 |
tdb |
1.1 |
use iscream::XMLParser; |
42 |
|
|
use RRDs; |
43 |
|
|
|
44 |
tdb |
1.3 |
# define variables that will be read from the config |
45 |
|
|
# nb. keep this insync with the config file! |
46 |
tdb |
1.9 |
use vars qw{ |
47 |
|
|
$imgdir $rrddir |
48 |
tdb |
1.3 |
$maxrrdage $maximgage $deleterrds $deleteimgs |
49 |
tdb |
1.9 |
$hex_slash $hex_underscore $hex_space $hex_colon $hex_bslash |
50 |
tdb |
1.3 |
$rrdstep $retry_wait |
51 |
|
|
$verbose $quiet |
52 |
|
|
}; |
53 |
|
|
|
54 |
|
|
# default locate of the config file |
55 |
|
|
my($configfile) = "rrdgraphing.conf"; |
56 |
|
|
|
57 |
|
|
# check for command line arguments |
58 |
|
|
my(%opts); |
59 |
|
|
my($ret) = getopts('hvqVc:', \%opts); |
60 |
|
|
|
61 |
|
|
# if invalid argument given, $ret will not be 1 |
62 |
|
|
&usage() if $ret != 1; |
63 |
|
|
|
64 |
|
|
# first process the arguments which might mean we exit now |
65 |
|
|
|
66 |
|
|
# -h is usage |
67 |
|
|
if($opts{h}) { |
68 |
|
|
&usage(); |
69 |
|
|
} |
70 |
|
|
# -V is version |
71 |
|
|
if($opts{V}) { |
72 |
|
|
print "watch.pl version: $version\n"; |
73 |
|
|
exit(1); |
74 |
|
|
} |
75 |
tdb |
1.1 |
|
76 |
tdb |
1.3 |
# Then try getting the config |
77 |
tdb |
1.1 |
|
78 |
tdb |
1.3 |
# -c specifies the config file location |
79 |
|
|
if($opts{c}) { |
80 |
|
|
$configfile = $opts{c}; |
81 |
|
|
} |
82 |
|
|
# suck in the config |
83 |
|
|
&log("reading config from $configfile\n"); |
84 |
|
|
do $configfile; |
85 |
|
|
|
86 |
|
|
# Then any options we might want to override the config with |
87 |
|
|
|
88 |
|
|
# -v is verbose |
89 |
|
|
if($opts{v}) { |
90 |
|
|
$verbose = $opts{v}; |
91 |
|
|
} |
92 |
|
|
# -q is verbose |
93 |
|
|
if($opts{q}) { |
94 |
|
|
$quiet = $opts{q}; |
95 |
|
|
# if we're meant to be quiet, we can hardly be verbose! |
96 |
|
|
# $verbose = 0; |
97 |
|
|
} |
98 |
tdb |
1.1 |
|
99 |
tdb |
1.3 |
# Finally check for required arguments |
100 |
|
|
|
101 |
|
|
# check we still have two arguments left |
102 |
tdb |
1.1 |
if (@ARGV != 2) { |
103 |
tdb |
1.3 |
&usage(); |
104 |
tdb |
1.1 |
} |
105 |
|
|
|
106 |
|
|
# user supplied client interface server and port |
107 |
|
|
my($addr) = $ARGV[0]; |
108 |
|
|
my($cport) = $ARGV[1]; |
109 |
|
|
|
110 |
tdb |
1.3 |
|
111 |
|
|
# Main program loop |
112 |
tdb |
1.1 |
while(1) { |
113 |
|
|
|
114 |
tdb |
1.3 |
&log("Connecting control channel to port $cport on $addr...\n"); |
115 |
tdb |
1.1 |
|
116 |
|
|
# attempt to connect the control channel |
117 |
|
|
my($csock) = new IO::Socket::INET( |
118 |
|
|
PeerAddr => $addr, |
119 |
|
|
PeerPort => $cport, |
120 |
|
|
Proto => 'tcp' |
121 |
|
|
); |
122 |
|
|
|
123 |
|
|
# if socket isn't defined connection failed |
124 |
|
|
if (!defined $csock) { |
125 |
tdb |
1.3 |
&error("ERROR: Could not connect control channel to $addr:$cport.\n"); |
126 |
|
|
&error("Please check that there is an i-scream server at this address.\n"); |
127 |
tdb |
1.1 |
&wait_then_retry(); |
128 |
|
|
next; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
my($response); |
132 |
|
|
|
133 |
|
|
# client interface should send it's protocol ID |
134 |
|
|
# we know about "PROTOCOL 1.1", and will only accept the same |
135 |
|
|
$response = <$csock>; |
136 |
tdb |
1.3 |
&log("CLI sent: $response"); |
137 |
tdb |
1.1 |
if ($response && $response ne "PROTOCOL 1.1\n") { |
138 |
tdb |
1.3 |
&error("The i-scream server sent an unexpected protocol ID: $response\n"); |
139 |
tdb |
1.1 |
close($csock); |
140 |
|
|
&wait_then_retry(); |
141 |
|
|
next; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
# send our identifier to the client interface |
145 |
|
|
print $csock "rrdgraphing\n"; |
146 |
tdb |
1.3 |
&log("we sent: rrdgraphing\n"); |
147 |
tdb |
1.1 |
$response = <$csock>; |
148 |
tdb |
1.3 |
&log("CLI sent: $response"); |
149 |
tdb |
1.1 |
if ($response && $response ne "OK\n") { |
150 |
tdb |
1.3 |
&error("Received unexpected response: $response\n"); |
151 |
tdb |
1.1 |
close($csock); |
152 |
|
|
&wait_then_retry(); |
153 |
|
|
next; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
# tell the client interface we'd like to start the data channel |
157 |
|
|
print $csock "STARTDATA\n"; |
158 |
tdb |
1.3 |
&log("we sent: STARTDATA\n"); |
159 |
|
|
|
160 |
tdb |
1.1 |
# the response should be the socket to connect the data channel to |
161 |
|
|
$response = <$csock>; |
162 |
tdb |
1.3 |
&log("CLI sent: $response"); |
163 |
tdb |
1.1 |
chomp $response; |
164 |
|
|
|
165 |
|
|
my($dport) = $response; |
166 |
tdb |
1.3 |
&log("Connecting data channel to port $dport on $addr...\n"); |
167 |
tdb |
1.1 |
|
168 |
|
|
# attempt to connect the data channel |
169 |
|
|
my($dsock) = new IO::Socket::INET( |
170 |
|
|
PeerAddr => $addr, |
171 |
|
|
PeerPort => $dport, |
172 |
|
|
Proto => 'tcp' |
173 |
|
|
) or die "arse?"; |
174 |
|
|
|
175 |
|
|
# if socket isn't defined connection failed |
176 |
|
|
if (!defined $dsock) { |
177 |
tdb |
1.3 |
&error("ERROR: Could not connect data channel to $addr:$dport.\n"); |
178 |
|
|
&error("Failure in communications.\n"); |
179 |
tdb |
1.1 |
close($csock); |
180 |
|
|
&wait_then_retry(); |
181 |
|
|
next; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
# the data channel should now be sending us data! |
185 |
|
|
|
186 |
|
|
# call sub to process data being received over the data channel |
187 |
|
|
&processdata($dsock); |
188 |
|
|
|
189 |
|
|
# data processing has stopped, close sockets |
190 |
|
|
close($csock); |
191 |
|
|
close($dsock); |
192 |
|
|
|
193 |
|
|
# wait before retrying |
194 |
|
|
&wait_then_retry(); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
# we'll never reach here... unless 1 becomes false for some reason ;) |
198 |
|
|
exit 0; |
199 |
|
|
|
200 |
|
|
|
201 |
|
|
# |
202 |
|
|
# wait for a while before retrying |
203 |
|
|
# |
204 |
|
|
sub wait_then_retry() { |
205 |
tdb |
1.3 |
&error("Will retry connection to i-scream server in $retry_wait seconds.\n\n"); |
206 |
tdb |
1.1 |
sleep $retry_wait; |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
# |
210 |
|
|
# Given the socket of the data channel will process all |
211 |
|
|
# the incoming XML data, creating and updating the appropriate |
212 |
|
|
# database files. |
213 |
|
|
# |
214 |
|
|
# $dsock = socket connected to the data channel |
215 |
|
|
# |
216 |
|
|
sub processdata() { |
217 |
|
|
# the socket connected to the data channel |
218 |
|
|
my($dsock) = @_; |
219 |
|
|
# save us recreating this variable each time we loop |
220 |
|
|
my($xml); |
221 |
|
|
|
222 |
|
|
while(1) { |
223 |
|
|
# read data |
224 |
|
|
$xml = <$dsock>; |
225 |
|
|
|
226 |
|
|
# something odd has happened |
227 |
|
|
last if not defined $xml; |
228 |
|
|
|
229 |
|
|
# attempt to parse the data |
230 |
|
|
my($err, %xmlhash) = &iscream::XMLParser::parse($xml); |
231 |
|
|
if($err) { |
232 |
tdb |
1.3 |
&error("Skipped, XML did not parse: $xml"); |
233 |
tdb |
1.1 |
next; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
# standard data packet |
237 |
|
|
if($xmlhash{"packet.attributes.type"} eq "data") { |
238 |
|
|
my($machine) = $xmlhash{"packet.attributes.machine_name"}; |
239 |
|
|
my($date) = $xmlhash{"packet.attributes.date"}; |
240 |
|
|
|
241 |
|
|
# make directory for machine |
242 |
|
|
if(! -d "$rrddir/$machine") { |
243 |
|
|
# not sure on this umask, but it seems to work? |
244 |
|
|
mkdir "$rrddir/$machine", 0777; |
245 |
tdb |
1.3 |
&log("created directory $rrddir/$machine\n"); |
246 |
tdb |
1.1 |
} |
247 |
|
|
|
248 |
|
|
my(@data); |
249 |
|
|
|
250 |
|
|
# cpu |
251 |
|
|
@data = ( "packet.cpu.idle:idle:GAUGE", |
252 |
|
|
"packet.cpu.user:user:GAUGE", |
253 |
|
|
"packet.cpu.kernel:kernel:GAUGE", |
254 |
|
|
"packet.cpu.swap:swap:GAUGE", |
255 |
|
|
"packet.cpu.iowait:iowait:GAUGE", |
256 |
|
|
); |
257 |
|
|
&updaterrd($machine, "cpu", $date, $rrdstep, \%xmlhash, @data); |
258 |
|
|
|
259 |
tdb |
1.9 |
# uptime |
260 |
|
|
@data = ( "packet.os.uptime:uptime:GAUGE" ); |
261 |
|
|
&updaterrd($machine, "uptime", $date, $rrdstep, \%xmlhash, @data); |
262 |
|
|
|
263 |
tdb |
1.1 |
# mem |
264 |
|
|
@data = ( "packet.memory.free:free:GAUGE", |
265 |
|
|
"packet.memory.total:total:GAUGE", |
266 |
tdb |
1.7 |
"packet.memory.cache:cache:GAUGE", |
267 |
tdb |
1.1 |
); |
268 |
|
|
&updaterrd($machine, "mem", $date, $rrdstep, \%xmlhash, @data); |
269 |
|
|
|
270 |
|
|
# load |
271 |
|
|
@data = ( "packet.load.load1:load1:GAUGE", |
272 |
|
|
"packet.load.load5:load5:GAUGE", |
273 |
|
|
"packet.load.load15:load15:GAUGE", |
274 |
|
|
); |
275 |
|
|
&updaterrd($machine, "load", $date, $rrdstep, \%xmlhash, @data); |
276 |
|
|
|
277 |
|
|
# processes |
278 |
|
|
@data = ( "packet.processes.cpu:cpu:GAUGE", |
279 |
|
|
"packet.processes.sleeping:sleeping:GAUGE", |
280 |
|
|
"packet.processes.stopped:stopped:GAUGE", |
281 |
|
|
"packet.processes.total:total:GAUGE", |
282 |
|
|
"packet.processes.zombie:zombie:GAUGE", |
283 |
|
|
); |
284 |
|
|
&updaterrd($machine, "proc", $date, $rrdstep, \%xmlhash, @data); |
285 |
|
|
|
286 |
|
|
# swap |
287 |
|
|
@data = ( "packet.swap.free:free:GAUGE", |
288 |
|
|
"packet.swap.total:total:GAUGE", |
289 |
|
|
); |
290 |
|
|
&updaterrd($machine, "swap", $date, $rrdstep, \%xmlhash, @data); |
291 |
|
|
|
292 |
|
|
# users |
293 |
|
|
@data = ( "packet.users.count:count:GAUGE", |
294 |
|
|
); |
295 |
|
|
&updaterrd($machine, "users", $date, $rrdstep, \%xmlhash, @data); |
296 |
tdb |
1.5 |
|
297 |
|
|
# paging |
298 |
tdb |
1.6 |
@data = ( "packet.pages.pageins:pageins:GAUGE", |
299 |
|
|
"packet.pages.pageouts:pageouts:GAUGE", |
300 |
tdb |
1.5 |
); |
301 |
|
|
&updaterrd($machine, "paging", $date, $rrdstep, \%xmlhash, @data); |
302 |
|
|
|
303 |
tdb |
1.1 |
# disk |
304 |
|
|
my($i) = 0; |
305 |
|
|
while(defined $xmlhash{"packet.disk.p$i.attributes.mount"}) { |
306 |
|
|
my($mount) = $xmlhash{"packet.disk.p$i.attributes.mount"}; |
307 |
|
|
$mount =~ s/_/$hex_underscore/g; |
308 |
|
|
$mount =~ s/\//$hex_slash/g; |
309 |
tdb |
1.9 |
$mount =~ s/\\/$hex_bslash/g; |
310 |
|
|
$mount =~ s/ /$hex_space/g; |
311 |
|
|
$mount =~ s/:/$hex_colon/g; |
312 |
tdb |
1.6 |
@data = ( "packet.disk.p$i.attributes.total:total:GAUGE", |
313 |
tdb |
1.1 |
"packet.disk.p$i.attributes.used:used:GAUGE", |
314 |
tdb |
1.5 |
"packet.disk.p$i.attributes.totalinodes:totalinodes:GAUGE", |
315 |
|
|
"packet.disk.p$i.attributes.freeinodes:freeinodes:GAUGE", |
316 |
tdb |
1.1 |
); |
317 |
|
|
&updaterrd($machine, "disk-$mount", $date, $rrdstep, \%xmlhash, @data); |
318 |
|
|
++$i; |
319 |
tdb |
1.6 |
} |
320 |
|
|
|
321 |
|
|
# diskio |
322 |
|
|
$i = 0; |
323 |
|
|
while(defined $xmlhash{"packet.diskio.p$i.attributes.name"}) { |
324 |
|
|
my($name) = $xmlhash{"packet.diskio.p$i.attributes.name"}; |
325 |
tdb |
1.9 |
$name =~ s/_/$hex_underscore/g; |
326 |
|
|
$name =~ s/\//$hex_slash/g; |
327 |
|
|
$name =~ s/\\/$hex_bslash/g; |
328 |
|
|
$name =~ s/ /$hex_space/g; |
329 |
|
|
$name =~ s/:/$hex_colon/g; |
330 |
tdb |
1.6 |
@data = ( "packet.diskio.p$i.attributes.rbytes:rbytes:GAUGE", |
331 |
|
|
"packet.diskio.p$i.attributes.wbytes:wbytes:GAUGE", |
332 |
|
|
); |
333 |
|
|
&updaterrd($machine, "diskio-$name", $date, $rrdstep, \%xmlhash, @data); |
334 |
|
|
++$i |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
# net |
338 |
|
|
$i = 0; |
339 |
|
|
while(defined $xmlhash{"packet.net.p$i.attributes.name"}) { |
340 |
|
|
my($name) = $xmlhash{"packet.net.p$i.attributes.name"}; |
341 |
tdb |
1.9 |
$name =~ s/_/$hex_underscore/g; |
342 |
|
|
$name =~ s/\//$hex_slash/g; |
343 |
|
|
$name =~ s/\\/$hex_bslash/g; |
344 |
|
|
$name =~ s/ /$hex_space/g; |
345 |
|
|
$name =~ s/:/$hex_colon/g; |
346 |
tdb |
1.6 |
@data = ( "packet.net.p$i.attributes.rx:rx:GAUGE", |
347 |
|
|
"packet.net.p$i.attributes.tx:tx:GAUGE", |
348 |
|
|
); |
349 |
tdb |
1.7 |
&updaterrd($machine, "net-$name", $date, $rrdstep, \%xmlhash, @data); |
350 |
|
|
++$i |
351 |
|
|
} |
352 |
|
|
|
353 |
|
|
# mailq |
354 |
|
|
$i = 0; |
355 |
|
|
while(defined $xmlhash{"packet.mailq.p$i.attributes.name"}) { |
356 |
|
|
my($name) = $xmlhash{"packet.mailq.p$i.attributes.name"}; |
357 |
tdb |
1.9 |
$name =~ s/\s+//g; |
358 |
tdb |
1.7 |
@data = ( "packet.mailq.p$i.attributes.size:size:GAUGE", |
359 |
|
|
); |
360 |
|
|
&updaterrd($machine, "mailq-$name", $date, $rrdstep, \%xmlhash, @data); |
361 |
tdb |
1.6 |
++$i |
362 |
tdb |
1.1 |
} |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
# queue statistics packet |
366 |
|
|
elsif($xmlhash{"packet.attributes.type"} eq "queueStat") { |
367 |
|
|
# psuedo machine for internal server stuff |
368 |
|
|
my($machine) = "i-scream-server"; |
369 |
|
|
# make directory |
370 |
|
|
if(! -d "$rrddir/$machine") { |
371 |
|
|
# not sure on this umask, but it seems to work? |
372 |
|
|
mkdir "$rrddir/$machine", 0777; |
373 |
tdb |
1.3 |
&log("created directory $rrddir/$machine\n"); |
374 |
tdb |
1.1 |
} |
375 |
|
|
my($hash) = $xmlhash{"packet.attributes.hashCode"}; |
376 |
|
|
my($date) = $xmlhash{"packet.attributes.date"}; |
377 |
|
|
my($name) = $xmlhash{"packet.attributes.name"}; |
378 |
|
|
# take a look to see if we have a shutdown packet... |
379 |
|
|
if($xmlhash{"packet.attributes.shutdown"} && $xmlhash{"packet.attributes.shutdown"} eq "true") { |
380 |
|
|
unlink <$rrddir/$machine/$hash\_*.rrd>, "$rrddir/$machine/$hash.def", <$imgdir/$machine/$hash*.png>; |
381 |
tdb |
1.3 |
&log("deleted $rrddir/$machine/$hash\_*.rrd $rrddir/$machine/$hash.def $imgdir/$machine/$hash*.png\n"); |
382 |
tdb |
1.1 |
next; |
383 |
|
|
} |
384 |
|
|
# look through to see how many internal queues we have |
385 |
|
|
my($i) = 0; |
386 |
|
|
while(defined $xmlhash{"packet.queue.attributes.queue$i"}) { |
387 |
|
|
# see if the queue has been removed |
388 |
|
|
if($xmlhash{"packet.queue.attributes.queue$i"} eq "[deleted]") { |
389 |
|
|
# delete the queues rrd |
390 |
|
|
unlink "$rrddir/$machine/$hash\_$i.rrd"; |
391 |
tdb |
1.3 |
&log("deleted $rrddir/$machine/$hash\_$i.rrd\n"); |
392 |
tdb |
1.1 |
# are there any other rrd's left on this queue? if not, cleanup. |
393 |
|
|
# get a list of any that may be still there.. |
394 |
|
|
opendir(DIR, "$rrddir/$machine"); |
395 |
|
|
my(@rrdcountfiles) = grep { -f "$rrddir/$machine/$_" && /^$hash\_\d+.rrd$/ } readdir(DIR); |
396 |
|
|
closedir DIR; |
397 |
|
|
# count them (+1 because an empty array is size -1) |
398 |
|
|
my($rrdcount) = $#rrdcountfiles + 1; |
399 |
|
|
if($rrdcount == 0) { |
400 |
|
|
# clean up the def file and any images |
401 |
|
|
unlink "$rrddir/$machine/$hash.def", <$imgdir/$machine/$hash*.png>; |
402 |
tdb |
1.3 |
&log("deleted $rrddir/$machine/$hash.def $imgdir/$machine/$hash*.png\n"); |
403 |
tdb |
1.1 |
} |
404 |
|
|
++$i; |
405 |
|
|
next; |
406 |
|
|
} |
407 |
|
|
# the &updaterrd will also do this check, but we want |
408 |
|
|
# to write our def file out first |
409 |
|
|
if( ! -f "$rrddir/$machine/$hash\_$i.rrd" ) { |
410 |
|
|
open(DEF, ">$rrddir/$machine/$hash.def"); |
411 |
|
|
print DEF $name; |
412 |
|
|
close DEF; |
413 |
tdb |
1.3 |
&log("created $rrddir/$machine/$hash.def\n"); |
414 |
tdb |
1.1 |
} |
415 |
|
|
my(@data) = ( "packet.queue.attributes.queue$i:size:GAUGE", |
416 |
|
|
"packet.queue.attributes.total:total:COUNTER", |
417 |
|
|
); |
418 |
|
|
&updaterrd($machine, "$hash\_$i", $date, $rrdstep, \%xmlhash, @data); |
419 |
|
|
++$i; |
420 |
|
|
} |
421 |
|
|
} |
422 |
|
|
else { |
423 |
tdb |
1.3 |
#&error("SKIPPED: valid xml, but not a data or statistics packet\n"); |
424 |
tdb |
1.1 |
} |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
# we'll now return from this sub and reconnect |
428 |
tdb |
1.3 |
&error("Data channel socket gave no data, bailing out...\n"); |
429 |
tdb |
1.1 |
} |
430 |
|
|
|
431 |
|
|
# |
432 |
|
|
# sub to update an rrd file |
433 |
|
|
# |
434 |
|
|
# $machine = name of the machine |
435 |
|
|
# (eg. kernow.ukc.ac.uk) |
436 |
|
|
# $type = the type of graph for the machine |
437 |
|
|
# (eg. cpu) |
438 |
|
|
# $date = the date of the item we want to add |
439 |
|
|
# (in seconds since the epoch) |
440 |
|
|
# $step = the interval at which the database steps |
441 |
|
|
# used when we create a new rrd |
442 |
|
|
# $xmlref = reference to the xml data packet |
443 |
|
|
# @data = array containing data items to add |
444 |
|
|
# (eg. "packet.cpu.user:user:GAUGE") |
445 |
|
|
# |
446 |
|
|
sub updaterrd() { |
447 |
|
|
my($machine, $type, $date, $step, $xmlref, @data) = @_; |
448 |
|
|
# get hold of the xmlhash we have a reference to |
449 |
|
|
my(%xmlhash) = %$xmlref; |
450 |
|
|
# check if we need to create a new rrd |
451 |
|
|
if( ! -f "$rrddir/$machine/$type.rrd") { |
452 |
|
|
my(@createdata); |
453 |
|
|
# pull the details out of the data we've been given |
454 |
|
|
foreach my $dataitem (@data) { |
455 |
|
|
if($dataitem =~ /^\S+:(\S+):(\S+)$/) { |
456 |
|
|
push @createdata, "$1:$2"; |
457 |
|
|
} |
458 |
|
|
} |
459 |
|
|
# call the &makerrd to actually create the rrd |
460 |
tdb |
1.3 |
&log("making new rrd for $rrddir/$machine/$type.rrd\n"); |
461 |
tdb |
1.1 |
&makerrd($machine, $type, $date, $step, @createdata); |
462 |
|
|
} |
463 |
|
|
# get the details out of the data we've been given |
464 |
|
|
my($updateparams) = "$date"; |
465 |
|
|
foreach my $dataitem (@data) { |
466 |
|
|
if($dataitem =~ /^(\S+):\S+:\S+$/) { |
467 |
|
|
# pull the values straight out of the xmlhash |
468 |
|
|
my($value) = $xmlhash{$1}; |
469 |
|
|
# if it's undefined we'll set it to 0 |
470 |
|
|
# this probably shouldn't happen, but it'd be best to handle it "nicely" :) |
471 |
|
|
$value = "0" if not defined $value; |
472 |
|
|
$updateparams .= ":$value"; |
473 |
|
|
} |
474 |
|
|
} |
475 |
|
|
# perform the update |
476 |
|
|
RRDs::update ("$rrddir/$machine/$type.rrd", $updateparams); |
477 |
tdb |
1.3 |
&log("updating $rrddir/$machine/$type.rrd\n"); |
478 |
tdb |
1.1 |
my($err) = RRDs::error; |
479 |
tdb |
1.3 |
&error("Error updating $rrddir/$machine/$type.rrd: $err\n") if $err; |
480 |
tdb |
1.1 |
} |
481 |
|
|
|
482 |
|
|
# |
483 |
|
|
# sub to create a new rrd file |
484 |
|
|
# |
485 |
|
|
# $machine = name of the machine |
486 |
|
|
# (eg. kernow.ukc.ac.uk) |
487 |
|
|
# $type = the type of graph for the machine |
488 |
|
|
# (eg. cpu) |
489 |
|
|
# $start = the date of the first item we want to add |
490 |
|
|
# (in seconds since the epoch) |
491 |
|
|
# $step = the interval at which the database steps |
492 |
|
|
# @data = the data items we want to put in the rrd |
493 |
|
|
# in the form: $dsname:dstype |
494 |
|
|
# (eg. "size:GAUGE") |
495 |
|
|
# |
496 |
|
|
sub makerrd() { |
497 |
|
|
my($machine, $type, $start, $step, @data) = @_; |
498 |
|
|
# check if directory exists for rrd |
499 |
|
|
if(! -d "$rrddir/$machine") { |
500 |
|
|
# not sure on this umask, but it seems to work? |
501 |
|
|
mkdir "$rrddir/$machine", 0777; |
502 |
tdb |
1.3 |
&log("created directory $rrddir/$machine\n"); |
503 |
tdb |
1.1 |
} |
504 |
|
|
my(@rrdcmd); |
505 |
|
|
# we'll want to add our first data item at $start, |
506 |
|
|
# so we start our rrd $step before that. |
507 |
|
|
$start -= $step; |
508 |
|
|
push @rrdcmd, "$rrddir/$machine/$type.rrd"; |
509 |
|
|
push @rrdcmd, "--start=$start"; |
510 |
|
|
push @rrdcmd, "--step=$step"; |
511 |
|
|
foreach my $dataitem (@data) { |
512 |
|
|
# dataitem should be: "dsname:dstype" |
513 |
|
|
if($dataitem =~ /^(\S+):(\S+)$/) { |
514 |
|
|
push @rrdcmd, "DS:$1:$2:600:U:U"; |
515 |
|
|
} |
516 |
|
|
} |
517 |
|
|
push @rrdcmd, ( |
518 |
|
|
# 3h in 15s samples |
519 |
|
|
"RRA:AVERAGE:0.5:1:720", |
520 |
|
|
"RRA:MAX:0.5:1:720", |
521 |
|
|
# 1d in 2m samples |
522 |
|
|
"RRA:AVERAGE:0.5:8:720", |
523 |
|
|
"RRA:MAX:0.5:8:720", |
524 |
|
|
# 1w in 15m samples |
525 |
|
|
"RRA:AVERAGE:0.5:60:672", |
526 |
|
|
"RRA:MAX:0.5:60:672", |
527 |
|
|
# 1m in 1hr samples |
528 |
|
|
"RRA:AVERAGE:0.5:240:744", |
529 |
|
|
"RRA:MAX:0.5:240:744", |
530 |
|
|
# 1y in 12hr samples |
531 |
|
|
"RRA:AVERAGE:0.5:2880:730", |
532 |
|
|
"RRA:MAX:0.5:2880:730", |
533 |
|
|
); |
534 |
|
|
RRDs::create (@rrdcmd); |
535 |
|
|
my($err) = RRDs::error; |
536 |
tdb |
1.3 |
&error("Error creating rrd for $rrddir/$machine/$type: $err\n") if $err; |
537 |
|
|
} |
538 |
|
|
|
539 |
|
|
# prints out usage information then exits |
540 |
|
|
sub usage() { |
541 |
|
|
print "Usage: watch.pl [options] i-scream_client_interface port\n"; |
542 |
|
|
print "Options\n"; |
543 |
|
|
print " -c config Specifies the configuration file\n"; |
544 |
|
|
print " default: rrdgraphing.conf\n"; |
545 |
|
|
print " -v Be verbose about what's happening\n"; |
546 |
|
|
print " -q Be quiet, even supress errors\n"; |
547 |
|
|
print " -V Print version number\n"; |
548 |
|
|
print " -h Prints this help page\n"; |
549 |
|
|
exit(1); |
550 |
|
|
} |
551 |
|
|
|
552 |
|
|
# prints a log message if verbose is turned on |
553 |
|
|
sub log() { |
554 |
|
|
my($msg) = @_; |
555 |
|
|
print $msg if $verbose; |
556 |
|
|
} |
557 |
|
|
|
558 |
|
|
# prints an error message unless quiet is turned on |
559 |
|
|
sub error() { |
560 |
|
|
my($msg) = @_; |
561 |
|
|
print STDERR $msg unless $quiet; |
562 |
tdb |
1.1 |
} |