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