]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - src/scripts/makegraphs
CPU Graph wieder auf Ticks umgestellt und erweitert
[people/teissler/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 my %mbmon_settings = ();
35 &General::readhash("${General::swroot}/mbmon/settings", \%mbmon_settings);
36
37 # Added for conversion of utf-8 characters
38 use Encode 'from_to';
39 my %tr=();
40
41 # Force language back to English (ugly hack!)
42 # Modified to only force if we are unable to convert charset
43 # from utf-8
44 if ((${Lang::language} eq 'el') ||
45 (${Lang::language} eq 'fa') ||
46 (${Lang::language} eq 'ru') ||
47 (${Lang::language} eq 'th') ||
48 (${Lang::language} eq 'vi') ||
49 (${Lang::language} eq 'zh') ||
50 (${Lang::language} eq 'zt')) {
51 eval `/bin/cat "${General::swroot}/langs/en.pl"`;
52 } else {
53 %tr=%Lang::tr; # use translated version for other languages
54 }
55
56 # Settings
57 my $rrdlog = "/var/log/rrd";
58 my $graphs = "/srv/web/ipfire/html/graphs";
59 $ENV{PATH}="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
60 my $hdd_device = "/dev/harddisk";
61 my $temp = '';
62 my %mbmon_values = ();
63 my $key;
64 my $value;
65 my @args = ();
66 my $count = 0;
67 my $ERROR;
68 my $dbg = 0;
69 my $path_smartctl = "/usr/sbin/smartctl";
70 my $path_hddtemp = "/usr/sbin/hddtemp";
71
72 my %color = ();
73 my %mainsettings = ();
74 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
75 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
76
77 open(MBMON_OUT, ">/var/log/mbmon-values");
78 open(FD, "/usr/bin/mbmon -rc1|" ) || die "ERROR: Cannot run mbmon\n" ;
79
80 while( $_ = <FD> )
81 {
82 next unless( /^([A-Za-z][^:\s]+)\s*:\s*([+\-]{0,1}[\d\.]+)/ ) ;
83 $key = $1 ;
84 $value = $2 ;
85 $key =~ y/A-Z/a-z/ ;
86 $mbmon_values{$key} = $value;
87 print(MBMON_OUT "$key=$value\n");
88 }
89 close(FD);
90 close(MBMON_OUT);
91
92 sub gettraffic {
93 my $interface = $_[0];
94
95 my $bytesin=0;
96 my $bytesout=0;
97
98 foreach (@ipacsum)
99 {
100 # Incoming...
101 $bytesin += $1 if (/^[\* ]\s+incoming\s+${interface}.+\:\s+(\d+)/);
102
103 # Forwarded Incoming...
104 $bytesin += $1 if (/^[\* ]\s+forwarded\s+incoming\s+${interface}.+\:\s+(\d+)/);
105
106 # Outgoing...
107 $bytesout += $1 if (/^[* ]\s+outgoing\s+${interface}.+\:\s+(\d+)/);
108
109 # Forwarded Outgoing...
110 $bytesout += $1 if (/^[* ]\s+forwarded\s+outgoing\s+${interface}.+\:\s+(\d+)/);
111 }
112 return "$bytesin:$bytesout";
113 }
114
115 sub updatecpudata {
116 if ( ! -e "$rrdlog/cpu.rrd") {
117 RRDs::create ("$rrdlog/cpu.rrd", "--step=300",
118 "DS:user:COUNTER:600:0:500000000",
119 "DS:system:COUNTER:600:0:500000000",
120 "DS:idle:COUNTER:600:0:500000000",
121 "DS:iowait:COUNTER:600:0:500000000",
122 "DS:irq:COUNTER:600:0:500000000",
123 "RRA:AVERAGE:0.5:1:576",
124 "RRA:AVERAGE:0.5:6:672",
125 "RRA:AVERAGE:0.5:24:732",
126 "RRA:AVERAGE:0.5:144:1460");
127 $ERROR = RRDs::error;
128 print "Error in RRD::create for cpu: $ERROR\n" if $ERROR;
129 }
130
131 my ($cpu, $user, $nice, $system, $idle, $iowait, $irq, $softirq);
132
133 open STAT, "/proc/stat";
134 while(<STAT>) {
135 chomp;
136 /^cpu\s/ or next;
137 ($cpu, $user, $nice, $system, $idle, $iowait, $irq, $softirq) = split /\s+/;
138 last;
139 }
140 close STAT;
141 $user += $nice;
142 $irq += $softirq;
143
144
145 RRDs::update ("$rrdlog/cpu.rrd",
146 "-t", "user:system:idle:iowait:irq",
147 "N:$user:$system:$idle:$iowait:$irq");
148 $ERROR = RRDs::error;
149 print "Error in RRD::update for cpu: $ERROR\n" if $ERROR;
150
151 }
152
153 sub updateloaddata {
154 if ( ! -e "$rrdlog/load.rrd") {
155 RRDs::create ("$rrdlog/load.rrd", "--step=60",
156 "DS:load1:GAUGE:120:0:U",
157 "DS:load5:GAUGE:120:0:U",
158 "DS:load15:GAUGE:120:0:U",
159 "RRA:AVERAGE:0.5:1:2160",
160 "RRA:AVERAGE:0.5:5:2016",
161 "RRA:AVERAGE:0.5:15:2880",
162 "RRA:AVERAGE:0.5:60:8760");
163
164 $ERROR = RRDs::error;
165 print "Error in RRD::create for cpu: $ERROR\n" if $ERROR;
166 }
167 }
168
169 sub updatememdata {
170 my ($memused, $memfree, $memshared, $membuffers, $memcache, $swapused, $swapfree, $swaptotal);
171 if ( ! -e "$rrdlog/mem.rrd") {
172 RRDs::create ("$rrdlog/mem.rrd", "--step=300",
173 "DS:memused:ABSOLUTE:600:0:5000000000",
174 "DS:memfree:ABSOLUTE:600:0:5000000000",
175 "DS:memshared:ABSOLUTE:600:0:5000000000",
176 "DS:membuffers:ABSOLUTE:600:0:5000000000",
177 "DS:memcache:ABSOLUTE:600:0:5000000000",
178 "DS:swapused:ABSOLUTE:600:0:5000000000",
179 "DS:swapfree:ABSOLUTE:600:0:5000000000",
180 "RRA:AVERAGE:0.5:1:576",
181 "RRA:AVERAGE:0.5:6:672",
182 "RRA:AVERAGE:0.5:24:732",
183 "RRA:AVERAGE:0.5:144:1460");
184 $ERROR = RRDs::error;
185 print "Error in RRD::create for mem: $ERROR\n" if $ERROR;
186 }
187
188 open MEM, "/proc/meminfo";
189 while(<MEM>) {
190 chomp;
191 if ($_ =~ /^MemTotal:/) {
192 my @temp = split (/\s+/, $_);
193 $memused = $temp[1];
194 } elsif ($_ =~ /^MemFree:/) {
195 my @temp = split (/\s+/, $_);
196 $memfree = $temp[1];
197 } elsif ($_ =~ /^Cached:/) {
198 my @temp = split (/\s+/, $_);
199 $memcache = $temp[1];
200 } elsif ($_ =~ /^Buffers:/) {
201 my @temp = split (/\s+/, $_);
202 $membuffers = $temp[1];
203 } elsif ($_ =~ /^SwapTotal:/) {
204 my @temp = split (/\s+/, $_);
205 $swaptotal = $temp[1];
206 } elsif ($_ =~ /^SwapFree:/) {
207 my @temp = split (/\s+/, $_);
208 $swapfree = $temp[1];
209 }
210 }
211 close MEM;
212
213 system("/bin/df > /tmp/diskfree");
214 open DF, "/tmp/diskfree";
215 while(<DF>) {
216 chomp;
217 if ($_ =~ /^shm/) {
218 my @temp = split (/\s+/, $_);
219 $memshared = $temp[2];
220 }
221 }
222 close DF;
223 system("/bin/rm -f /tmp/diskfree");
224
225 $swapused = $swaptotal-$swapfree;
226 RRDs::update ("$rrdlog/mem.rrd",
227 "-t", "memused:memfree:memshared:membuffers:memcache:swapused:swapfree",
228 "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree");
229 $ERROR = RRDs::error;
230 print "Error in RRD::update for mem: $ERROR\n" if $ERROR;
231 }
232
233 sub updatediskdata {
234 my $disk = $_[0];
235 my ($readsect, $writesect, $trash);
236 if ( ! -e "$rrdlog/disk-$disk.rrd") {
237 RRDs::create ("$rrdlog/disk-$disk.rrd", "--step=300",
238 "DS:readsect:COUNTER:600:0:5000000000",
239 "DS:writesect:COUNTER:600:0:5000000000",
240 "RRA:AVERAGE:0.5:1:576",
241 "RRA:AVERAGE:0.5:6:672",
242 "RRA:AVERAGE:0.5:24:732",
243 "RRA:AVERAGE:0.5:144:1460");
244 $ERROR = RRDs::error;
245 print "Error in RRD::create for disk $disk: $ERROR\n" if $ERROR;
246 }
247
248 my $Zeilen = `/usr/bin/iostat $disk | tail -2 | head -1`;
249 ($trash, $trash, $trash, $trash, $readsect, $writesect) = split(/\s+/,$Zeilen);
250
251 print "\nread:".$readsect."write:".$writesect."\n";
252
253 if ($readsect && $writesect) {
254 RRDs::update ("$rrdlog/disk-$disk.rrd",
255 "-t", "readsect:writesect",
256 "N:$readsect:$writesect");
257 $ERROR = RRDs::error;
258 print "Error in RRD::update for disk $disk: $ERROR\n" if $ERROR;
259 } else {
260 print "Error in RRD::update for disk: $disk no data available\n";
261 }
262 }
263
264 sub updateifdata {
265 my $interface = $_[0];
266
267 if ( ! -e "$rrdlog/$interface.rrd") {
268 RRDs::create ("$rrdlog/$interface.rrd", "--step=300",
269 "DS:incoming:ABSOLUTE:600:0:12500000",
270 "DS:outgoing:ABSOLUTE:600:0:12500000",
271 "RRA:AVERAGE:0.5:1:576",
272 "RRA:AVERAGE:0.5:6:672",
273 "RRA:AVERAGE:0.5:24:732",
274 "RRA:AVERAGE:0.5:144:1460");
275 $ERROR = RRDs::error;
276 print "Error in RRD::create for $interface: $ERROR\n" if $ERROR;
277 }
278
279 my $traffic = gettraffic ($interface);
280 RRDs::update ("$rrdlog/$interface.rrd",
281 "-t", "incoming:outgoing",
282 "N:$traffic");
283 $ERROR = RRDs::error;
284 print "Error in RRD::update for $interface: $ERROR\n" if $ERROR;
285 }
286
287 sub updatefwhitsdata {
288 my $portamount=0;
289 my $alertaktuell=0;
290 my $aktuell=0;
291 my $portaktuell=0;
292 my $skip=0;
293
294 if (! -e "$rrdlog/firewallhits.rrd")
295 {
296 RRDs::create ("$rrdlog/firewallhits.rrd", "--step=300",
297 "DS:amount:GAUGE:600:0:U",
298 "DS:portamount:GAUGE:600:0:U",
299 "RRA:AVERAGE:0.5:1:576",
300 "RRA:AVERAGE:0.5:6:672",
301 "RRA:AVERAGE:0.5:24:732",
302 "RRA:AVERAGE:0.5:144:1460");
303 $ERROR = RRDs::error;
304 print "Error in RRD::create for cpu: $ERROR\n" if $ERROR;
305 }
306
307 system("logtailfwhits /var/log/messages /var/log/fwhits.messages.offset >/tmp/messages.fwhits");
308 if (!(open (FILE,'/tmp/messages.fwhits'))) {
309 $skip=1;
310 }
311 $aktuell = 0;
312 if (!$skip) {
313 while (<FILE>) {
314 if (/kernel:.*(IN=.*)$/) {
315 $aktuell++;
316 }
317 }
318 close (FILE);
319 }
320
321 system("logtailfwhits /var/log/snort/alert /var/log/snort/fwhits.alert.offset >/tmp/snort.fwhits");
322 if (!(open (FILE,'/tmp/snort.fwhits'))) {
323 $skip=1;
324 }
325 $alertaktuell = 0;
326 if (!$skip) {
327 while (<FILE>) {
328 if (/scan.*$/) {
329 $alertaktuell++;
330 }
331 }
332 close (FILE);
333 }
334
335 if (!(open (FILE,'/tmp/messages.fwhits'))) {
336 $skip=1;
337 }
338 $portaktuell = 0;
339 if (!$skip) {
340 while (<FILE>) {
341 if (/kernel:.*(Scan.*)$/) {
342 $portaktuell++;
343 }
344 }
345 close (FILE);
346 }
347
348 system("rm /tmp/messages.fwhits");
349 system("rm /tmp/snort.fwhits");
350
351 $portamount = $portaktuell + $alertaktuell;
352 chomp($portamount);
353 RRDs::update ("$rrdlog/firewallhits.rrd",
354 "N:$aktuell:$portamount");
355 $ERROR = RRDs::error;
356 print "Error in RRD::update for Firewallhits: $ERROR\n" if $ERROR;
357 }
358
359 # Creates and updates a link quality database
360 # -------------------------------------------
361 sub updatelq {
362 if ( ! -e "$rrdlog/lq.rrd") {
363 RRDs::create ("$rrdlog/lq.rrd", "--step=300",
364 "DS:loss:GAUGE:600:0:100",
365 "DS:roundtrip:GAUGE:600:0:10000",
366 "RRA:AVERAGE:0.5:1:576",
367 "RRA:AVERAGE:0.5:6:672",
368 "RRA:AVERAGE:0.5:24:732",
369 "RRA:AVERAGE:0.5:144:1460");
370 $ERROR = RRDs::error;
371 print "Error in RRD::create for link: $ERROR\n" if $ERROR;
372 }
373 my $packetloss=0;
374 my $roundtrip=0;
375 my $test=0;
376 # LQ_GATEWAY is the ip of your isp's public ip facing you
377 my $LQ_GATEWAY=`netstat -rn | grep ^0.0.0.0 | awk '{print \$2}'`;
378 my $NUMPINGS=10;
379 my $pingoutput = `ping -c $NUMPINGS -q $LQ_GATEWAY`;
380 chomp;
381 my @temp = split (/\/|\%|\s/, $pingoutput);
382 $packetloss = $temp[17];
383 $roundtrip = $temp[28];
384 RRDs::update ("$rrdlog/lq.rrd", "N:$packetloss:$roundtrip");
385 $ERROR = RRDs::error;
386 print "Error in RRD::update for line quality: $ERROR\n" if $ERROR;
387 }
388
389 sub updatehdddata
390 {
391 my $disk = $_[0];
392 if ( ! -e "$rrdlog/hddtemp-$disk.rrd")
393 {
394 # database did not exist -> create
395 RRDs::create ("$rrdlog/hddtemp-$disk.rrd", "--step=300",
396 "DS:temperature:GAUGE:600:0:100",
397 "RRA:AVERAGE:0.5:1:576",
398 "RRA:AVERAGE:0.5:6:672",
399 "RRA:AVERAGE:0.5:24:732",
400 "RRA:AVERAGE:0.5:144:1460");
401 $ERROR = RRDs::error;
402 print "Error in RRD::create for hdd-$disk: $ERROR\n" if $ERROR;
403 }
404
405 $temp = 0;
406 my $hdd_output = '';
407 my $smart_output = '';
408
409 if ( -e "$path_smartctl" )
410 {
411 system("$path_smartctl -iHA /dev/$disk > /var/log/smartctl_out_hddtemp-$disk");
412 }
413
414 if ( -e "$path_hddtemp" )
415 {
416 $hdd_output = `$path_hddtemp -qn /dev/$disk`;
417
418 # I know 4 response possible responses:
419 #
420 # /dev/harddisk: harddisk type: S.M.A.R.T. not available
421 # /dev/harddisk: harddisk type: no sensor
422 # /dev/harddisk: harddisk type: 37?C or ?F
423 # 37
424
425 if ( index($hdd_output, "S.M.A.R.T.") != -1 )
426 {
427 $temp = 0;
428 }
429 elsif ( index($hdd_output, "no sensor") != -1 )
430 {
431 $temp = 1;
432 }
433 elsif ( index($hdd_output, "$disk") != -1 )
434 {
435 $hdd_output =~ /.*:.*:\s*(\d+).*\s/;
436 $temp = $1;
437 }
438 else
439 {
440 $hdd_output =~ /(\d+)\s/;
441 $temp = $1;
442 }
443 }
444 elsif ( -e "/var/log/smartctl_out_hddtemp-$disk" )
445 {
446 $hdd_output = `cat /var/log/smartctl_out_hddtemp-$disk | grep Temperature_`;
447 my @t = split(/\s+/,$hdd_output);
448 $temp = $t[9];
449 }
450 else
451 {
452 $temp = 0;
453 }
454
455 # print "hdd $hdd_output \n";
456 # print "temp $temp \n";
457
458 RRDs::update ("$rrdlog/hddtemp-$disk.rrd", "-t", "temperature", "N:$temp");
459
460 $ERROR = RRDs::error;
461 print "Error in RRD::update for hdd-$disk: $ERROR\n" if $ERROR;
462 }
463
464 sub updatembmondata
465 {
466 if ( ! -e "$rrdlog/mbmon.rrd" )
467 {
468 # database did not exist -> create
469
470 @args = ("$rrdlog/mbmon.rrd");
471
472 push(@args, "--step=300");
473 foreach $key ( sort(keys %mbmon_values) )
474 {
475 push(@args, "DS:$key:GAUGE:600:U:U");
476 }
477 push(@args, "RRA:AVERAGE:0.5:1:576");
478 push(@args, "RRA:AVERAGE:0.5:6:672");
479 push(@args, "RRA:AVERAGE:0.5:24:732");
480 push(@args, "RRA:AVERAGE:0.5:144:1460");
481
482 print("create ". join( ", ", @args)) if ( $dbg );
483
484 RRDs::create (@args);
485 $ERROR = RRDs::error;
486 print("Error in RRD::create for mbmon: $ERROR\n") if $ERROR;
487 }
488
489 my @ds;
490 my @val;
491 my $template;
492
493 foreach $key ( sort(keys %mbmon_values) )
494 {
495 push(@ds, $key);
496 push(@val, $mbmon_values{$key});
497 }
498
499 $template = join(':', @ds);
500 $value = "N:".join(':', @val);
501
502 print("update template = '$template'\n") if ( $dbg );
503 print("update value = '$value'\n") if ( $dbg );
504
505 RRDs::update("$rrdlog/mbmon.rrd", "-t", $template, $value);
506 $ERROR = RRDs::error;
507 print("Error in RRD::update for mbmon: $ERROR\n") if $ERROR;
508 }
509
510 ## Update ipac logs
511 system ('/usr/sbin/fetchipac');
512 sleep 8;
513
514 ###
515 ### Squid Graphs
516 ###
517 if ( -e "/var/log/squid/access.log") {
518 system ("/usr/bin/squid-graph -o=/srv/web/ipfire/html/sgraph --tcp-only < /var/log/squid/access.log >/dev/null 2>&1");
519 }
520
521 ###
522 ### utf8 conversion
523 ###
524 if ((${Lang::language} eq 'cs') ||
525 (${Lang::language} eq 'hu') ||
526 (${Lang::language} eq 'pl') ||
527 (${Lang::language} eq 'sk')) {
528 # Czech, Hungarian, Polish and Slovak character set
529 foreach my $key(keys %Lang::tr) {
530 from_to($tr{$key}, "utf-8", "iso-8859-2");
531 }
532 } elsif (${Lang::language} eq 'tr') {
533 # Turkish
534 foreach my $key(keys %Lang::tr) {
535 from_to($tr{$key}, "utf-8", "iso-8859-9");
536 }
537 } else {
538 foreach my $key(keys %Lang::tr) {
539 from_to($tr{$key}, "utf-8", "iso-8859-1");
540 }
541 }
542
543 ###
544 ### System graphs
545 ###
546 updatecpudata();
547 updateloaddata();
548 updatememdata();
549
550 my @disks = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
551 print "\nFound following amount of disks:".@disks."\n";
552 foreach (@disks){
553 my $disk = $_;
554 chomp $disk;
555 print "Working on disk ".$disk.".\n";
556 updatediskdata($disk);
557 }
558
559 ###
560 ### Firewallhits
561 ###
562 updatefwhitsdata();
563
564 ###
565 ### Link Quality
566 ###
567 updatelq();
568
569 ###
570 ### HDDTEMP-Graphs for /dev/harddisk
571 ###
572
573 my @disks = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
574 print "\nFound following amount of disks for hddtemp:".@disks."\n";
575 foreach (@disks){
576 my $disk = $_;
577 chomp $disk;
578 updatehdddata ($disk);
579 }
580
581 ###
582 ### Network Graphs
583 ###
584 @ipacsum = `/usr/sbin/ipacsum --exact -s 5m 2>/dev/null`;
585 if (@ipacsum) {
586 updateifdata ("GREEN");
587 updateifdata ("RED");
588 if ($settings{'CONFIG_TYPE'} =~ /^(2|4)$/ ) {
589 updateifdata ("ORANGE");
590 }
591 if ($settings{'CONFIG_TYPE'} =~ /^(3|4)$/ ) {
592 updateifdata ("BLUE");
593 }
594 }