]> git.ipfire.org Git - ipfire-2.x.git/blob - src/scripts/makegraphs
8cd4de0cf5984478d3a6e86e3e4f4847a6b65504
[ipfire-2.x.git] / src / scripts / makegraphs
1 #!/usr/bin/perl
2
3 ############################################################################
4 # #
5 # This file is part of the IPCop Firewall. #
6 # #
7 # IPCop is free software; you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation; either version 2 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # IPCop 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 IPCop; if not, write to the Free Software #
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
20 # #
21 # Copyright (C) 2004-01-19 Mark Wormgoor <mark@wormgoor.com>. #
22 # #
23 ############################################################################
24
25 use strict;
26 #use warnings;
27
28 use RRDs;
29 require "/var/ipfire/general-functions.pl";
30 require "${General::swroot}/lang.pl";
31
32 my (%settings, @ipacsum, $iface, $ERROR);
33 &General::readhash("${General::swroot}/ethernet/settings", \%settings);
34
35 # Added for conversion of utf-8 characters
36 use Encode 'from_to';
37 my %tr=();
38
39 # Force language back to English (ugly hack!)
40 # Modified to only force if we are unable to convert charset
41 # from utf-8
42 if ((${Lang::language} eq 'el') ||
43 (${Lang::language} eq 'fa') ||
44 (${Lang::language} eq 'ru') ||
45 (${Lang::language} eq 'th') ||
46 (${Lang::language} eq 'vi') ||
47 (${Lang::language} eq 'zh') ||
48 (${Lang::language} eq 'zt')) {
49 eval `/bin/cat "${General::swroot}/langs/en.pl"`;
50 } else {
51 %tr=%Lang::tr; # use translated version for other languages
52 }
53
54 # Settings
55 my $rrdlog = "/var/log/rrd";
56 my $graphs = "/home/httpd/html/graphs";
57 $ENV{PATH}="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
58 my $hdd_device = "/dev/harddisk";
59
60 sub gettraffic {
61 my $interface = $_[0];
62
63 my $bytesin=0;
64 my $bytesout=0;
65
66 foreach (@ipacsum)
67 {
68 # Incoming...
69 $bytesin += $1 if (/^[\* ]\s+incoming\s+${interface}.+\:\s+(\d+)/);
70
71 # Forwarded Incoming...
72 $bytesin += $1 if (/^[\* ]\s+forwarded\s+incoming\s+${interface}.+\:\s+(\d+)/);
73
74 # Outgoing...
75 $bytesout += $1 if (/^[* ]\s+outgoing\s+${interface}.+\:\s+(\d+)/);
76
77 # Forwarded Outgoing...
78 $bytesout += $1 if (/^[* ]\s+forwarded\s+outgoing\s+${interface}.+\:\s+(\d+)/);
79 }
80 return "$bytesin:$bytesout";
81 }
82
83 sub updatecpugraph {
84 my $period = $_[0];
85
86 RRDs::graph ("$graphs/cpu-$period.png",
87 "--start", "-1$period", "-aPNG", "-i", "-z",
88 "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-u 100", "-r",
89 "--color", "SHADEA#EAE9EE",
90 "--color", "SHADEB#EAE9EE",
91 "--color", "BACK#FFFFFF",
92 "-t $tr{'cpu usage per'} $tr{$period}",
93 "DEF:user=$rrdlog/cpu.rrd:user:AVERAGE",
94 "DEF:system=$rrdlog/cpu.rrd:system:AVERAGE",
95 "DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE",
96 "CDEF:total=user,system,idle,+,+",
97 "CDEF:userpct=100,user,total,/,*",
98 "CDEF:systempct=100,system,total,/,*",
99 "CDEF:idlepct=100,idle,total,/,*",
100 "AREA:userpct#0000FF:$tr{'user cpu usage'}\\j",
101 "STACK:systempct#FF0000:$tr{'system cpu usage'}\\j",
102 "STACK:idlepct#00FF00:$tr{'idle cpu usage'}\\j",
103 "GPRINT:userpct:MAX:$tr{'maximal'} $tr{'user cpu'}\\:%3.2lf%%",
104 "GPRINT:userpct:AVERAGE:$tr{'average'} $tr{'user cpu'}\\:%3.2lf%%",
105 "GPRINT:userpct:LAST:$tr{'current'} $tr{'user cpu'}\\:%3.2lf%%\\j",
106 "GPRINT:systempct:MAX:$tr{'maximal'} $tr{'system cpu'}\\:%3.2lf%%",
107 "GPRINT:systempct:AVERAGE:$tr{'average'} $tr{'system cpu'}\\:%3.2lf%%",
108 "GPRINT:systempct:LAST:$tr{'current'} $tr{'system cpu'}\\:%3.2lf%%\\j",
109 "GPRINT:idlepct:MAX:$tr{'maximal'} $tr{'idle cpu'}\\:%3.2lf%%",
110 "GPRINT:idlepct:AVERAGE:$tr{'average'} $tr{'idle cpu'}\\:%3.2lf%%",
111 "GPRINT:idlepct:LAST:$tr{'current'} $tr{'idle cpu'}\\:%3.2lf%%\\j");
112 $ERROR = RRDs::error;
113 print "Error in RRD::graph for cpu: $ERROR\n" if $ERROR;
114 }
115
116 sub updatecpudata {
117 if ( ! -e "$rrdlog/cpu.rrd") {
118 RRDs::create ("$rrdlog/cpu.rrd", "--step=300",
119 "DS:user:COUNTER:600:0:500000000",
120 "DS:system:COUNTER:600:0:500000000",
121 "DS:idle:COUNTER:600:0:500000000",
122 "RRA:AVERAGE:0.5:1:576",
123 "RRA:AVERAGE:0.5:6:672",
124 "RRA:AVERAGE:0.5:24:732",
125 "RRA:AVERAGE:0.5:144:1460");
126 $ERROR = RRDs::error;
127 print "Error in RRD::create for cpu: $ERROR\n" if $ERROR;
128 }
129
130 my ($cpu, $user, $nice, $system, $idle);
131
132 open STAT, "/proc/stat";
133 while(<STAT>) {
134 chomp;
135 /^cpu\s/ or next;
136 ($cpu, $user, $nice, $system, $idle) = split /\s+/;
137 last;
138 }
139 close STAT;
140 $user += $nice;
141
142 RRDs::update ("$rrdlog/cpu.rrd",
143 "-t", "user:system:idle",
144 "N:$user:$system:$idle");
145 $ERROR = RRDs::error;
146 print "Error in RRD::update for cpu: $ERROR\n" if $ERROR;
147
148 }
149
150 sub updatememgraph {
151 my $period = $_[0];
152
153 RRDs::graph ("$graphs/memory-$period.png",
154 "--start", "-1$period", "-aPNG", "-i", "-z",
155 "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-u 100", "-r",
156 "--color", "SHADEA#EAE9EE",
157 "--color", "SHADEB#EAE9EE",
158 "--color", "BACK#FFFFFF",
159 "-t $tr{'memory usage per'} $tr{$period}",
160 "DEF:used=$rrdlog/mem.rrd:memused:AVERAGE",
161 "DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE",
162 "DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE",
163 "DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE",
164 "DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE",
165 "CDEF:total=used,free,+",
166 "CDEF:used2=used,buffer,cache,shared,+,+,-",
167 "CDEF:usedpct=100,used2,total,/,*",
168 "CDEF:sharedpct=100,shared,total,/,*",
169 "CDEF:bufferpct=100,buffer,total,/,*",
170 "CDEF:cachepct=100,cache,total,/,*",
171 "CDEF:freepct=100,free,total,/,*",
172 "AREA:usedpct#0000FF:$tr{'used memory'}\\j",
173 "STACK:sharedpct#FF0000:$tr{'shared memory'}\\j",
174 "STACK:bufferpct#FF00FF:$tr{'buffered memory'}\\j",
175 "STACK:cachepct#FFFF00:$tr{'cached memory'}\\j",
176 "STACK:freepct#00FF00:$tr{'free memory'}\\j",
177 "GPRINT:usedpct:MAX:$tr{'maximal'} $tr{'used memory'}\\:%3.2lf%%",
178 "GPRINT:usedpct:AVERAGE:$tr{'average'} $tr{'used memory'}\\:%3.2lf%%",
179 "GPRINT:usedpct:LAST:$tr{'current'} $tr{'used memory'}\\:%3.2lf%%\\j",
180 "GPRINT:sharedpct:MAX:$tr{'maximal'} $tr{'shared memory'}\\:%3.2lf%%",
181 "GPRINT:sharedpct:AVERAGE:$tr{'average'} $tr{'shared memory'}\\:%3.2lf%%",
182 "GPRINT:sharedpct:LAST:$tr{'current'} $tr{'shared memory'}\\:%3.2lf%%\\j",
183 "GPRINT:bufferpct:MAX:$tr{'maximal'} $tr{'buffered memory'}\\:%3.2lf%%",
184 "GPRINT:bufferpct:AVERAGE:$tr{'average'} $tr{'buffered memory'}\\:%3.2lf%%",
185 "GPRINT:bufferpct:LAST:$tr{'current'} $tr{'buffered memory'}\\:%3.2lf%%\\j",
186 "GPRINT:cachepct:MAX:$tr{'maximal'} $tr{'cached memory'}\\:%3.2lf%%",
187 "GPRINT:cachepct:AVERAGE:$tr{'average'} $tr{'cached memory'}\\:%3.2lf%%",
188 "GPRINT:cachepct:LAST:$tr{'current'} $tr{'cached memory'}\\:%3.2lf%%\\j",
189 "GPRINT:freepct:MAX:$tr{'maximal'} $tr{'free memory'}\\:%3.2lf%%",
190 "GPRINT:freepct:AVERAGE:$tr{'average'} $tr{'free memory'}\\:%3.2lf%%",
191 "GPRINT:freepct:LAST:$tr{'current'} $tr{'free memory'}\\:%3.2lf%%\\j");
192 $ERROR = RRDs::error;
193 print "Error in RRD::graph for mem: $ERROR\n" if $ERROR;
194
195 RRDs::graph ("$graphs/swap-$period.png",
196 "--start", "-1$period", "-aPNG", "-i", "-z",
197 "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-u 100", "-r",
198 "--color", "SHADEA#EAE9EE",
199 "--color", "SHADEB#EAE9EE",
200 "--color", "BACK#FFFFFF",
201 "-t $tr{'swap usage per'} $tr{$period}",
202 "DEF:used=$rrdlog/mem.rrd:swapused:AVERAGE",
203 "DEF:free=$rrdlog/mem.rrd:swapfree:AVERAGE",
204 "CDEF:total=used,free,+",
205 "CDEF:usedpct=100,used,total,/,*",
206 "CDEF:freepct=100,free,total,/,*",
207 "AREA:usedpct#0000FF:$tr{'used swap'}\\j",
208 "STACK:freepct#00FF00:$tr{'free swap'}\\j",
209 "GPRINT:usedpct:MAX:$tr{'maximal'} $tr{'used swap'}\\:%3.2lf%%",
210 "GPRINT:usedpct:AVERAGE:$tr{'average'} $tr{'used swap'}\\:%3.2lf%%",
211 "GPRINT:usedpct:LAST:$tr{'current'} $tr{'used swap'}\\:%3.2lf%%\\j",
212 "GPRINT:freepct:MAX:$tr{'maximal'} $tr{'free swap'}\\:%3.2lf%%",
213 "GPRINT:freepct:AVERAGE:$tr{'average'} $tr{'free swap'}\\:%3.2lf%%",
214 "GPRINT:freepct:LAST:$tr{'current'} $tr{'free swap'}\\:%3.2lf%%\\j");
215 $ERROR = RRDs::error;
216 print "Error in RRD::graph for swap: $ERROR\n" if $ERROR;
217 }
218
219 sub updatememdata {
220 my ($memused, $memfree, $memshared, $membuffers, $memcache, $swapused, $swapfree);
221 if ( ! -e "$rrdlog/mem.rrd") {
222 RRDs::create ("$rrdlog/mem.rrd", "--step=300",
223 "DS:memused:ABSOLUTE:600:0:5000000000",
224 "DS:memfree:ABSOLUTE:600:0:5000000000",
225 "DS:memshared:ABSOLUTE:600:0:5000000000",
226 "DS:membuffers:ABSOLUTE:600:0:5000000000",
227 "DS:memcache:ABSOLUTE:600:0:5000000000",
228 "DS:swapused:ABSOLUTE:600:0:5000000000",
229 "DS:swapfree:ABSOLUTE:600:0:5000000000",
230 "RRA:AVERAGE:0.5:1:576",
231 "RRA:AVERAGE:0.5:6:672",
232 "RRA:AVERAGE:0.5:24:732",
233 "RRA:AVERAGE:0.5:144:1460");
234 $ERROR = RRDs::error;
235 print "Error in RRD::create for mem: $ERROR\n" if $ERROR;
236 }
237
238 open MEM, "/proc/meminfo";
239 while(<MEM>) {
240 chomp;
241 if ($_ =~ /^Mem:/) {
242 my @temp = split (/\s+/, $_);
243 $memused = $temp[2];
244 $memfree = $temp[3];
245 $memshared = $temp[4];
246 $membuffers = $temp[5];
247 $memcache = $temp[6];
248 } elsif ($_ =~ /^Swap:/) {
249 my @temp = split (/\s+/, $_);
250 $swapused = $temp[2];
251 $swapfree = $temp[3];
252 }
253 }
254 close MEM;
255
256 RRDs::update ("$rrdlog/mem.rrd",
257 "-t", "memused:memfree:memshared:membuffers:memcache:swapused:swapfree",
258 "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree");
259 $ERROR = RRDs::error;
260 print "Error in RRD::update for mem: $ERROR\n" if $ERROR;
261 }
262
263 sub updatediskgraph {
264 my $period = $_[0];
265
266 RRDs::graph ("$graphs/disk-$period.png",
267 "--start", "-1$period", "-aPNG", "-i", "-z",
268 "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-r",
269 "--color", "SHADEA#EAE9EE",
270 "--color", "SHADEB#EAE9EE",
271 "--color", "BACK#FFFFFF",
272 "-t $tr{'disk access per'} $tr{$period}",
273 "DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE",
274 "DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE",
275 "AREA:read#0000FF:$tr{'sectors read from disk per second'}\\j",
276 "STACK:write#00FF00:$tr{'sectors written to disk per second'}\\j",
277 "GPRINT:read:MAX:$tr{'maximal'} $tr{'read sectors'}\\:%8.0lf",
278 "GPRINT:read:AVERAGE:$tr{'average'} $tr{'read sectors'}\\:%8.0lf",
279 "GPRINT:read:LAST:$tr{'current'} $tr{'read sectors'}\\:%8.0lf\\j",
280 "GPRINT:write:MAX:$tr{'maximal'} $tr{'written sectors'}\\:%8.0lf",
281 "GPRINT:write:AVERAGE:$tr{'average'} $tr{'written sectors'}\\:%8.0lf",
282 "GPRINT:write:LAST:$tr{'current'} $tr{'written sectors'}\\:%8.0lf\\j");
283 $ERROR = RRDs::error;
284 print "Error in RRD::graph for disk: $ERROR\n" if $ERROR;
285 }
286
287 sub updatediskdata {
288 my ($readwritereq, $readreq, $readsect, $writereq, $writesect);
289 if ( ! -e "$rrdlog/disk.rrd") {
290 RRDs::create ("$rrdlog/disk.rrd", "--step=300",
291 "DS:readsect:COUNTER:600:0:5000000000",
292 "DS:writesect:COUNTER:600:0:5000000000",
293 "RRA:AVERAGE:0.5:1:576",
294 "RRA:AVERAGE:0.5:6:672",
295 "RRA:AVERAGE:0.5:24:732",
296 "RRA:AVERAGE:0.5:144:1460");
297 $ERROR = RRDs::error;
298 print "Error in RRD::create for disk: $ERROR\n" if $ERROR;
299 }
300
301 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
302 $atime, $mtime, $ctime, $blksize, $blocks) = stat("/dev/harddisk");
303
304 my $major = $rdev >> 8;
305 my $minor = $rdev & 0xFF;
306
307 open STAT, "/proc/stat";
308 my @diskstat = <STAT>;
309 close (STAT);
310 foreach my $line (@diskstat)
311 {
312 chomp ($line);
313 my @temp = split(/\:\ /,$line);
314 if ($temp[1]) {
315 my @devicestat = split(/\ /,$temp[1]);
316 foreach my $stats (@devicestat)
317 {
318 chomp ($stats);
319 my @fields = split(/\((\d+),(\d+)\):\((\d+),(\d+),(\d+),(\d+),(\d+)/,$stats);
320 if ($major eq $fields[1] and $minor eq $fields[2])
321 {
322 $readwritereq = $fields[3];
323 $readreq = $fields[4];
324 $readsect = $fields[5];
325 $writereq = $fields[6];
326 $writesect = $fields[7];
327 }
328 }
329 }
330 }
331
332 if ($readsect && $writesect) {
333 RRDs::update ("$rrdlog/disk.rrd",
334 "-t", "readsect:writesect",
335 "N:$readsect:$writesect");
336 $ERROR = RRDs::error;
337 print "Error in RRD::update for disk: $ERROR\n" if $ERROR;
338 } else {
339 print "Error in RRD::update for disk: no data available\n";
340 }
341 }
342
343 sub updatediskgraphnew {
344 my $disk = $_[0];
345 my $period = $_[1];
346
347 RRDs::graph ("$graphs/disk-$disk-$period.png",
348 "--start", "-1$period", "-aPNG", "-i", "-z",
349 "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-r",
350 "--color", "SHADEA#EAE9EE",
351 "--color", "SHADEB#EAE9EE",
352 "--color", "BACK#FFFFFF",
353 "-t $tr{'disk access per'} $tr{$period}",
354 "DEF:read=$rrdlog/disk-$disk.rrd:readsect:AVERAGE",
355 "DEF:write=$rrdlog/disk-$disk.rrd:writesect:AVERAGE",
356 "DEF:sleep=$rrdlog/disk-$disk.rrd:sleeping:AVERAGE",
357 "CDEF:sl_state=sleep,INF,*",
358
359 "AREA:sl_state#a0a0a0:disk standby\\j",
360 "AREA:read#0000FF:$tr{'sectors read from disk per second'}\\j",
361 "STACK:write#00FF00:$tr{'sectors written to disk per second'}\\j",
362
363 "GPRINT:read:MAX:$tr{'maximal'} $tr{'read sectors'}\\:%8.0lf",
364 "GPRINT:read:AVERAGE:$tr{'average'} $tr{'read sectors'}\\:%8.0lf",
365 "GPRINT:read:LAST:$tr{'current'} $tr{'read sectors'}\\:%8.0lf\\j",
366 "GPRINT:write:MAX:$tr{'maximal'} $tr{'written sectors'}\\:%8.0lf",
367 "GPRINT:write:AVERAGE:$tr{'average'} $tr{'written sectors'}\\:%8.0lf",
368 "GPRINT:write:LAST:$tr{'current'} $tr{'written sectors'}\\:%8.0lf\\j");
369 $ERROR = RRDs::error;
370 print "Error in RRD::graph for disk-$disk: $ERROR\n" if $ERROR;
371 }
372
373 sub updatediskdatanew {
374 my $disk = $_[0];
375
376 my ($readwritereq, $readreq, $readsect, $writereq, $writesect);
377 if ( ! -e "$rrdlog/disk-$disk.rrd") {
378 RRDs::create ("$rrdlog/disk-$disk.rrd", "--step=300",
379 "DS:readsect:COUNTER:600:0:5000000000",
380 "DS:writesect:COUNTER:600:0:5000000000",
381 "DS:sleeping:GAUGE:600:0:1",
382 "RRA:AVERAGE:0.5:1:576",
383 "RRA:AVERAGE:0.5:6:672",
384 "RRA:AVERAGE:0.5:24:732",
385 "RRA:AVERAGE:0.5:144:1460");
386 $ERROR = RRDs::error;
387 print "Error in RRD::create for disk-$disk: $ERROR\n" if $ERROR;
388 }
389
390 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
391 $atime, $mtime, $ctime, $blksize, $blocks) = stat("/dev/$disk");
392
393 my $major = $rdev >> 8;
394 my $minor = ($rdev & 0xFF) >>6;
395
396 open STAT, "/proc/stat";
397 my @diskstat = <STAT>;
398 close (STAT);
399 foreach my $line (@diskstat)
400 {
401 chomp ($line);
402 my @temp = split(/\:\ /,$line);
403 if ($temp[1]) {
404 my @devicestat = split(/\ /,$temp[1]);
405 foreach my $stats (@devicestat)
406 {
407 chomp ($stats);
408 my @fields = split(/\((\d+),(\d+)\):\((\d+),(\d+),(\d+),(\d+),(\d+)/,$stats);
409 if ($major eq $fields[1] and $minor eq $fields[2])
410 {
411 $readwritereq = $fields[3];
412 $readreq = $fields[4];
413 $readsect = $fields[5];
414 $writereq = $fields[6];
415 $writesect = $fields[7];
416 }
417 }
418 }
419 }
420
421 my $sleeping=0;
422 my $lastsleepstate=0;
423
424 if ( -e "/tmp/hddshutdown-$disk" ) {
425 open STAT,"/tmp/hddshutdown-$disk";
426 $lastsleepstate = <STAT>;
427 close (STAT);
428 if ($lastsleepstate==$readwritereq) {
429 $sleeping=1;
430 }
431 }
432
433 if ($readsect && $writesect) {
434 RRDs::update ("$rrdlog/disk-$disk.rrd",
435 "-t", "readsect:writesect:sleeping",
436 "N:$readsect:$writesect:$sleeping");
437 $ERROR = RRDs::error;
438 print "Error in RRD::update for disk-$disk: $ERROR\n" if $ERROR;
439 } else {
440 print "Error in RRD::update for disk-$disk: no data available\n";
441 }
442 }
443
444 sub updateifgraph {
445 my $interface = $_[0];
446 my $period = $_[1];
447
448 RRDs::graph ("$graphs/$interface-$period.png",
449 "--start", "-1$period", "-aPNG", "-i", "-z",
450 "--alt-y-grid", "-w 600", "-h 100",
451 "--color", "SHADEA#EAE9EE",
452 "--color", "SHADEB#EAE9EE",
453 "--color", "BACK#FFFFFF",
454 "-t $tr{'traffic on'} $interface ($tr{'graph per'} $tr{$period})",
455 "-v$tr{'bytes per second'}",
456 "DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE",
457 "DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE",
458 "AREA:incoming#00FF00:$tr{'incoming traffic in bytes per second'}\\j",
459 "LINE1:outgoing#0000FF:$tr{'outgoing traffic in bytes per second'}\\j",
460 "GPRINT:incoming:MAX:$tr{'maximal'} $tr{'in'}\\:%8.3lf %sBps",
461 "GPRINT:incoming:AVERAGE:$tr{'average'} $tr{'in'}\\:%8.3lf %sBps",
462 "GPRINT:incoming:LAST:$tr{'current'} $tr{'in'}\\:%8.3lf %sBps\\j",
463 "GPRINT:outgoing:MAX:$tr{'maximal'} $tr{'out'}\\:%8.3lf %sBps",
464 "GPRINT:outgoing:AVERAGE:$tr{'average'} $tr{'out'}\\:%8.3lf %sBps",
465 "GPRINT:outgoing:LAST:$tr{'current'} $tr{'out'}\\:%8.3lf %sBps\\j");
466 $ERROR = RRDs::error;
467 print "Error in RRD::graph for $interface: $ERROR\n" if $ERROR;
468 }
469
470 sub updateifdata {
471 my $interface = $_[0];
472
473 if ( ! -e "$rrdlog/$interface.rrd") {
474 RRDs::create ("$rrdlog/$interface.rrd", "--step=300",
475 "DS:incoming:ABSOLUTE:600:0:12500000",
476 "DS:outgoing:ABSOLUTE:600:0:12500000",
477 "RRA:AVERAGE:0.5:1:576",
478 "RRA:AVERAGE:0.5:6:672",
479 "RRA:AVERAGE:0.5:24:732",
480 "RRA:AVERAGE:0.5:144:1460");
481 $ERROR = RRDs::error;
482 print "Error in RRD::create for $interface: $ERROR\n" if $ERROR;
483 }
484
485 my $traffic = gettraffic ($interface);
486 RRDs::update ("$rrdlog/$interface.rrd",
487 "-t", "incoming:outgoing",
488 "N:$traffic");
489 $ERROR = RRDs::error;
490 print "Error in RRD::update for $interface: $ERROR\n" if $ERROR;
491 }
492
493 sub updatefwhitsgraph {
494 my $interval = $_[0];
495
496 RRDs::graph ("$graphs/firewallhits-$interval-area.png",
497 "--start", "-1$interval", "-aPNG", "-i", "-z",
498 "--alt-y-grid", "-w 600", "-h 200",
499 "--color", "SHADEA#EAE9EE",
500 "--color", "SHADEB#EAE9EE",
501 "--color", "BACK#FFFFFF",
502 "-t firewall hits over the last $interval",
503 "DEF:amount=$rrdlog/firewallhits.rrd:amount:AVERAGE",
504 "AREA:amount#6464FF:firewallhits",
505 "GPRINT:amount:MAX: Max\\: %2.2lf %S",
506 "GPRINT:amount:AVERAGE: Avg\\: %2.2lf %S",
507 "GPRINT:amount:LAST: Current\\: %2.2lf %Shits/5 min\\n",
508 "DEF:portamount=$rrdlog/firewallhits.rrd:portamount:AVERAGE",
509 "AREA:portamount#FF6464:portscans",
510 "GPRINT:portamount:MAX: Max\\: %2.2lf %S",
511 "GPRINT:portamount:AVERAGE: Avg\\: %2.2lf %S",
512 "GPRINT:portamount:LAST: Current\\: %2.2lf %Shits/5 min");
513 $ERROR = RRDs::error;
514 print "Error in RRD::graph for Firewallhits: $ERROR\n" if $ERROR;
515
516 RRDs::graph ("$graphs/firewallhits-$interval-line.png",
517 "--start", "-1$interval", "-aPNG", "-i", "-z",
518 "--alt-y-grid", "-w 600", "-h 200",
519 "--color", "SHADEA#EAE9EE",
520 "--color", "SHADEB#EAE9EE",
521 "--color", "BACK#FFFFFF",
522 "-t firewall hits over the last $interval",
523 "DEF:amount=$rrdlog/firewallhits.rrd:amount:AVERAGE",
524 "LINE2:amount#6464FF:firewallhits",
525 "GPRINT:amount:MAX: Max\\: %2.2lf %S",
526 "GPRINT:amount:AVERAGE: Avg\\: %2.2lf %S",
527 "GPRINT:amount:LAST: Current\\: %2.2lf %Shits/5 min\\n",
528 "DEF:portamount=$rrdlog/firewallhits.rrd:portamount:AVERAGE",
529 "LINE2:portamount#FF6464:portscans",
530 "GPRINT:portamount:MAX: Max\\: %2.2lf %S",
531 "GPRINT:portamount:AVERAGE: Avg\\: %2.2lf %S",
532 "GPRINT:portamount:LAST: Current\\: %2.2lf %Shits/5 min");
533 $ERROR = RRDs::error;
534 print "Error in RRD::graph for Firewallhits: $ERROR\n" if $ERROR;
535 }
536
537 sub updatefwhitsdata {
538 my $portamount=0;
539 my $alertaktuell=0;
540 my $aktuell=0;
541 my $portaktuell=0;
542 my $skip=0;
543
544 if (! -e "$rrdlog/firewallhits.rrd")
545 {
546 RRDs::create ("$rrdlog/firewallhits.rrd", "--step=300",
547 "DS:amount:GAUGE:600:0:U",
548 "DS:portamount:GAUGE:600:0:U",
549 "RRA:AVERAGE:0.5:1:576",
550 "RRA:AVERAGE:0.5:6:672",
551 "RRA:AVERAGE:0.5:24:732",
552 "RRA:AVERAGE:0.5:144:1460");
553 $ERROR = RRDs::error;
554 print "Error in RRD::create for cpu: $ERROR\n" if $ERROR;
555 }
556
557 system("logtailfwhits /var/log/messages /var/log/fwhits.messages.offset >/tmp/messages.fwhits");
558 if (!(open (FILE,'/tmp/messages.fwhits'))) {
559 $skip=1;
560 }
561 $aktuell = 0;
562 if (!$skip) {
563 while (<FILE>) {
564 if (/kernel:.*(IN=.*)$/) {
565 $aktuell++;
566 }
567 }
568 close (FILE);
569 }
570
571 system("logtailfwhits /var/log/snort/alert /var/log/snort/fwhits.alert.offset >/tmp/snort.fwhits");
572 if (!(open (FILE,'/tmp/snort.fwhits'))) {
573 $skip=1;
574 }
575 $alertaktuell = 0;
576 if (!$skip) {
577 while (<FILE>) {
578 if (/scan.*$/) {
579 $alertaktuell++;
580 }
581 }
582 close (FILE);
583 }
584
585 if (!(open (FILE,'/tmp/messages.fwhits'))) {
586 $skip=1;
587 }
588 $portaktuell = 0;
589 if (!$skip) {
590 while (<FILE>) {
591 if (/kernel:.*(Scan.*)$/) {
592 $portaktuell++;
593 }
594 }
595 close (FILE);
596 }
597
598 system("rm /tmp/messages.fwhits");
599 system("rm /tmp/snort.fwhits");
600
601 $portamount = $portaktuell + $alertaktuell;
602 chomp($portamount);
603 RRDs::update ("$rrdlog/firewallhits.rrd",
604 "N:$aktuell:$portamount");
605 $ERROR = RRDs::error;
606 print "Error in RRD::update for Firewallhits: $ERROR\n" if $ERROR;
607 }
608
609 # Creates and updates a link quality database
610 # -------------------------------------------
611 sub updatelq {
612 if ( ! -e "$rrdlog/lq.rrd") {
613 RRDs::create ("$rrdlog/lq.rrd", "--step=300",
614 "DS:loss:GAUGE:600:0:100",
615 "DS:roundtrip:GAUGE:600:0:10000",
616 "RRA:AVERAGE:0.5:1:576",
617 "RRA:AVERAGE:0.5:6:672",
618 "RRA:AVERAGE:0.5:24:732",
619 "RRA:AVERAGE:0.5:144:1460");
620 $ERROR = RRDs::error;
621 print "Error in RRD::create for link: $ERROR\n" if $ERROR;
622 }
623 my $packetloss=0;
624 my $roundtrip=0;
625 my $test=0;
626 # LQ_GATEWAY is the ip of your isp's public ip facing you
627 my $LQ_GATEWAY='www.heise.de';
628 my $NUMPINGS=10;
629 my $pingoutput = `ping -c $NUMPINGS -q $LQ_GATEWAY`;
630 chomp;
631 my @temp = split (/\/|\%|\s/, $pingoutput);
632 $packetloss = $temp[17];
633 $roundtrip = $temp[28];
634 RRDs::update ("$rrdlog/lq.rrd", "N:$packetloss:$roundtrip");
635 $ERROR = RRDs::error;
636 print "Error in RRD::update for line quality: $ERROR\n" if $ERROR;
637 }
638
639 sub updatelqgraph {
640 my $period = $_[0];
641 RRDs::graph ("$graphs/lq-$period.png",
642 "--start", "-1$period", "-aPNG", "-i", "-z",
643 "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-r",
644 "-t $tr{'linkq'} ($tr{'graph per'} $tr{$period})",
645 "--lazy",
646 "--color", "SHADEA#EAE9EE",
647 "--color", "SHADEB#EAE9EE",
648 "--color", "BACK#FFFFFF",
649 "-v ms / pkts (% x10)",
650 "DEF:roundtrip=$rrdlog/lq.rrd:roundtrip:AVERAGE",
651 "DEF:loss=$rrdlog/lq.rrd:loss:AVERAGE",
652 "CDEF:roundavg=roundtrip,PREV(roundtrip),+,2,/",
653 "CDEF:loss10=loss,10,*",
654 "CDEF:r0=roundtrip,30,MIN",
655 "CDEF:r1=roundtrip,70,MIN",
656 "CDEF:r2=roundtrip,150,MIN",
657 "CDEF:r3=roundtrip,300,MIN",
658 "AREA:roundtrip#EE7000:>300 ms",
659 "AREA:r3#D88E1B:150-300 ms",
660 "AREA:r2#B9B63F:70-150 ms",
661 "AREA:r1#99E064:30-70 ms",
662 "AREA:r0#80FF80:<30 ms",
663 "AREA:loss10#800000:Packet loss (x10)",
664 "LINE1:roundtrip#707070:",
665 "GPRINT:roundtrip:MAX:$tr{'maximal'} $tr{'linkq'}\\:%3.2lf ms",
666 "GPRINT:roundtrip:AVERAGE:$tr{'average'} $tr{'linkq'}\\:%3.2lf ms",
667 "GPRINT:roundtrip:LAST:$tr{'current'} $tr{'linkq'}\\:%3.2lf ms\\j",
668 "GPRINT:loss:MAX:$tr{'maximal'} Loss\\:%3.2lf%%",
669 "GPRINT:loss:AVERAGE:$tr{'average'} Loss\\:%3.2lf%%",
670 "GPRINT:loss:LAST:$tr{'current'} Loss\\:%3.2lf%%\\j"
671 );
672 $ERROR = RRDs::error;
673 print "Error in RRD::graph for Link Quality: $ERROR\n" if $ERROR;
674 }
675
676 sub updatehdddata
677 {
678 if ( ! -e "$rrdlog/hddtemp.rrd")
679 {
680 # database did not exist -> create
681 RRDs::create ("$rrdlog/hddtemp.rrd", "--step=300",
682 "DS:temperature:GAUGE:600:0:100",
683 "RRA:AVERAGE:0.5:1:576",
684 "RRA:AVERAGE:0.5:6:672",
685 "RRA:AVERAGE:0.5:24:732",
686 "RRA:AVERAGE:0.5:144:1460");
687 $ERROR = RRDs::error;
688 print "Error in RRD::create for hdd: $ERROR\n" if $ERROR;
689 }
690
691 my $hdd_output = `/usr/sbin/hddtemp -qn $hdd_device`;
692
693 # I know 4 response possible responses:
694 #
695 # /dev/harddisk: harddisk type: S.M.A.R.T. not available
696 # /dev/harddisk: harddisk type: no sensor
697 # /dev/harddisk: harddisk type: 37°C or °F
698 # 37
699
700 if ( index($hdd_output, "S.M.A.R.T.") != -1 )
701 {
702 $temp = 0;
703 }
704 elsif ( index($hdd_output, "no sensor") != -1 )
705 {
706 $temp = 1;
707 }
708 elsif ( index($hdd_output, "$hdd_device") != -1 )
709 {
710 $hdd_output =~ /.*:.*:\s*(\d+).*\s/;
711 $temp = $1;
712 }
713 else
714 {
715 $hdd_output =~ /(\d+)\s/;
716 $temp = $1;
717 }
718
719 RRDs::update ("$rrdlog/hddtemp.rrd", "-t", "temperature", "N:$temp");
720
721 $ERROR = RRDs::error;
722 print "Error in RRD::update for hdd: $ERROR\n" if $ERROR;
723 }
724
725 sub updatehddgraph {
726 my $period = $_[0];
727
728 RRDs::graph ("$graphs/hddtemp-$period.png",
729 "--start", "-1$period", "-aPNG", "-i", "-z",
730 "--alt-y-grid", "-w 600", "-h 100",
731 "--color", "SHADEA#EAE9EE",
732 "--color", "SHADEB#EAE9EE",
733 "--color", "BACK#FFFFFF",
734 "-t $tr{'harddisk temperature'} ($tr{'graph per'} $tr{$period})",
735 "DEF:temperature=$rrdlog/hddtemp.rrd:temperature:AVERAGE",
736 "LINE2:temperature#0000FF:$tr{'hdd temperature in'} °C",
737 "GPRINT:temperature:MAX:$tr{'maximal'}\\:%3.0lf °C",
738 "GPRINT:temperature:AVERAGE:$tr{'average'}\\:%3.0lf °C",
739 "GPRINT:temperature:LAST:$tr{'current'}\\:%3.0lf °C",
740 );
741 $ERROR = RRDs::error;
742 print "Error in RRD::graph for cpu: $ERROR\n" if $ERROR;
743 }
744
745 ## Update ipac logs
746 system ('/usr/sbin/fetchipac');
747 sleep 8;
748
749 ###
750 ### Squid Graphs
751 ###
752 if ( -e "/var/log/squid/access.log") {
753 system ("/usr/bin/squid-graph -o=/home/httpd/html/sgraph --tcp-only < /var/log/squid/access.log >/dev/null 2>&1");
754 }
755
756 ###
757 ### utf8 conversion
758 ###
759 if ((${Lang::language} eq 'cs') ||
760 (${Lang::language} eq 'hu') ||
761 (${Lang::language} eq 'pl') ||
762 (${Lang::language} eq 'sk')) {
763 # Czech, Hungarian, Polish and Slovak character set
764 foreach my $key(keys %Lang::tr) {
765 from_to($tr{$key}, "utf-8", "iso-8859-2");
766 }
767 } elsif (${Lang::language} eq 'tr') {
768 # Turkish
769 foreach my $key(keys %Lang::tr) {
770 from_to($tr{$key}, "utf-8", "iso-8859-9");
771 }
772 } else {
773 foreach my $key(keys %Lang::tr) {
774 from_to($tr{$key}, "utf-8", "iso-8859-1");
775 }
776 }
777
778 ###
779 ### System graphs
780 ###
781 updatecpudata();
782 updatecpugraph ("day");
783 updatecpugraph ("week");
784 updatecpugraph ("month");
785 updatecpugraph ("year");
786
787 updatememdata();
788 updatememgraph ("day");
789 updatememgraph ("week");
790 updatememgraph ("month");
791 updatememgraph ("year");
792
793 updatediskdata();
794 updatediskgraph ("day");
795 updatediskgraph ("week");
796 updatediskgraph ("month");
797 updatediskgraph ("year");
798
799 if (open STAT,"/dev/hda") {
800 close STAT;
801 updatediskdatanew ("hda");
802 updatediskgraphnew ("hda","day");
803 updatediskgraphnew ("hda","week");
804 updatediskgraphnew ("hda","month");
805 updatediskgraphnew ("hda","year");
806 } else {
807 if (-e "$graphs/disk-hda-day.png") {
808 system("rm $graphs/disk-hda-day.png");
809 }
810 }
811
812 if (open STAT,"/dev/hdb") {
813 close STAT;
814 updatediskdatanew ("hdb");
815 updatediskgraphnew ("hdb","day");
816 updatediskgraphnew ("hdb","week");
817 updatediskgraphnew ("hdb","month");
818 updatediskgraphnew ("hdb","year");
819 } else {
820 if (-e "$graphs/disk-hdb-day.png") {
821 system("rm $graphs/disk-hdb-day.png");
822 }
823 }
824
825 if (open STAT,"/dev/hdc") {
826 close STAT;
827 updatediskdatanew ("hdc");
828 updatediskgraphnew ("hdc","day");
829 updatediskgraphnew ("hdc","week");
830 updatediskgraphnew ("hdc","month");
831 updatediskgraphnew ("hdc","year");
832 } else {
833 if (-e "$graphs/disk-hdc-day.png") {
834 system("rm $graphs/disk-hdc-day.png");
835 }
836 }
837
838 if (open STAT,"/dev/hdd") {
839 close STAT;
840 updatediskdatanew ("hdd");
841 updatediskgraphnew ("hdd","day");
842 updatediskgraphnew ("hdd","week");
843 updatediskgraphnew ("hdd","month");
844 updatediskgraphnew ("hdd","year");
845 } else {
846 if (-e "$graphs/disk-hdd-day.png") {
847 system("rm $graphs/disk-hdd-day.png");
848 }
849 }
850
851 if (open STAT,"/dev/hde") {
852 close STAT;
853 updatediskdatanew ("hde");
854 updatediskgraphnew ("hde","day");
855 updatediskgraphnew ("hde","week");
856 updatediskgraphnew ("hde","month");
857 updatediskgraphnew ("hde","year");
858 } else {
859 if (-e "$graphs/disk-hde-day.png") {
860 system("rm $graphs/disk-hde-day.png");
861 }
862 }
863
864 if (open STAT,"/dev/hdf") {
865 close STAT;
866 updatediskdatanew ("hdf");
867 updatediskgraphnew ("hdf","day");
868 updatediskgraphnew ("hdf","week");
869 updatediskgraphnew ("hdf","month");
870 updatediskgraphnew ("hdf","year");
871 } else {
872 if (-e "$graphs/disk-hdf-day.png") {
873 system("rm $graphs/disk-hdf-day.png");
874 }
875 }
876
877 if (open STAT,"/dev/hdg") {
878 close STAT;
879 updatediskdatanew ("hdg");
880 updatediskgraphnew ("hdg","day");
881 updatediskgraphnew ("hdg","week");
882 updatediskgraphnew ("hdg","month");
883 updatediskgraphnew ("hdg","year");
884 } else {
885 if (-e "$graphs/disk-hdg-day.png") {
886 system("rm $graphs/disk-hdg-day.png");
887 }
888 }
889
890 if (open STAT,"/dev/hdh") {
891 close STAT;
892 updatediskdatanew ("hdh");
893 updatediskgraphnew ("hdh","day");
894 updatediskgraphnew ("hdh","week");
895 updatediskgraphnew ("hdh","month");
896 updatediskgraphnew ("hdh","year");
897 } else {
898 if (-e "$graphs/disk-hdh-day.png") {
899 system("rm $graphs/disk-hdh-day.png");
900 }
901 }
902
903 ###
904 ### Firewallhits
905 ###
906 updatefwhitsdata();
907 updatefwhitsgraph ("day");
908 updatefwhitsgraph ("week");
909 updatefwhitsgraph ("month");
910 updatefwhitsgraph ("year");
911
912 ###
913 ### Link Quality
914 ###
915 updatelq();
916 sleep 2;
917 updatelqgraph("day");
918 updatelqgraph("week");
919 updatelqgraph("month");
920 updatelqgraph("year");
921
922 ###
923 ### HDDTEMP-Graphs for /dev/harddisk
924 ###
925 updatehdddata();
926 updatehddgraph ("day");
927 updatehddgraph ("week");
928 updatehddgraph ("month");
929 updatehddgraph ("year");
930
931 ###
932 ### Network Graphs
933 ###
934 @ipacsum = `/usr/sbin/ipacsum --exact -s 5m 2>/dev/null`;
935 if (@ipacsum) {
936 updateifdata ("GREEN");
937 updateifdata ("RED");
938 if ($settings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/ ) {
939 updateifdata ("ORANGE");
940 }
941 if ($settings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/ ) {
942 updateifdata ("BLUE");
943 }
944 }
945 if ( -e "$rrdlog/GREEN.rrd") {
946 updateifgraph ("GREEN", "day");
947 updateifgraph ("GREEN", "week");
948 updateifgraph ("GREEN", "month");
949 updateifgraph ("GREEN", "year");
950 }
951
952 if ( -e "$rrdlog/RED.rrd") {
953 updateifgraph ("RED", "day");
954 updateifgraph ("RED", "week");
955 updateifgraph ("RED", "month");
956 updateifgraph ("RED", "year");
957 }
958
959 if ($settings{'CONFIG_TYPE'} =~ /^(1|3|5|7)$/ && -e "$rrdlog/ORANGE.rrd") {
960 updateifgraph ("ORANGE", "day");
961 updateifgraph ("ORANGE", "week");
962 updateifgraph ("ORANGE", "month");
963 updateifgraph ("ORANGE", "year");
964 }
965
966 if ($settings{'CONFIG_TYPE'} =~ /^(4|5|6|7)$/ && -e "$rrdlog/BLUE.rrd") {
967 updateifgraph ("BLUE", "day");
968 updateifgraph ("BLUE", "week");
969 updateifgraph ("BLUE", "month");
970 updateifgraph ("BLUE", "year");
971 }
972
973 #chmod 777 /home/httpd/html/{graphs,sgraph} -R
974 system("chmod -R 0777 /home/httpd/html/graphs");
975 system("chmod -R 0777 /home/httpd/html/sgraph");
976