]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/scripts/makegraphs
CGI fuer DHCP gefixt und if Skripte zurueckgesetzt
[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=`netstat -rn | grep ^0.0.0.0 | awk '{print \$2}'`;
380 my $NUMPINGS=10;
381 my $pingoutput = `ping -c $NUMPINGS -q $LQ_GATEWAY`;
382 chomp;
383 my @temp = split (/\/|\%|\s/, $pingoutput);
384 $packetloss = $temp[17];
385 $roundtrip = $temp[28];
386 RRDs::update ("$rrdlog/lq.rrd", "N:$packetloss:$roundtrip");
387 $ERROR = RRDs::error;
388 print "Error in RRD::update for line quality: $ERROR\n" if $ERROR;
389 }
390
391 sub updatehdddata
392 {
393 my $disk = $_[0];
394 if ( ! -e "$rrdlog/hddtemp-$disk.rrd")
395 {
396 # database did not exist -> create
397 RRDs::create ("$rrdlog/hddtemp-$disk.rrd", "--step=300",
398 "DS:temperature:GAUGE:600:0:100",
399 "RRA:AVERAGE:0.5:1:576",
400 "RRA:AVERAGE:0.5:6:672",
401 "RRA:AVERAGE:0.5:24:732",
402 "RRA:AVERAGE:0.5:144:1460");
403 $ERROR = RRDs::error;
404 print "Error in RRD::create for hdd-$disk: $ERROR\n" if $ERROR;
405 }
406
407 $temp = 0;
408 my $hdd_output = '';
409 my $smart_output = '';
410
411 if ( -e "$path_smartctl" )
412 {
413 system("$path_smartctl -iHA /dev/$disk > /var/log/smartctl_out_hddtemp-$disk");
414 }
415
416 if ( -e "$path_hddtemp" )
417 {
418 $hdd_output = `$path_hddtemp -qn /dev/$disk`;
419
420 # I know 4 response possible responses:
421 #
422 # /dev/harddisk: harddisk type: S.M.A.R.T. not available
423 # /dev/harddisk: harddisk type: no sensor
424 # /dev/harddisk: harddisk type: 37?C or ?F
425 # 37
426
427 if ( index($hdd_output, "S.M.A.R.T.") != -1 )
428 {
429 $temp = 0;
430 }
431 elsif ( index($hdd_output, "no sensor") != -1 )
432 {
433 $temp = 1;
434 }
435 elsif ( index($hdd_output, "$disk") != -1 )
436 {
437 $hdd_output =~ /.*:.*:\s*(\d+).*\s/;
438 $temp = $1;
439 }
440 else
441 {
442 $hdd_output =~ /(\d+)\s/;
443 $temp = $1;
444 }
445 }
446 elsif ( -e "/var/log/smartctl_out_hddtemp-$disk" )
447 {
448 $hdd_output = `cat /var/log/smartctl_out_hddtemp-$disk | grep Temperature_`;
449 my @t = split(/\s+/,$hdd_output);
450 $temp = $t[9];
451 }
452 else
453 {
454 $temp = 0;
455 }
456
457 # print "hdd $hdd_output \n";
458 # print "temp $temp \n";
459
460 RRDs::update ("$rrdlog/hddtemp-$disk.rrd", "-t", "temperature", "N:$temp");
461
462 $ERROR = RRDs::error;
463 print "Error in RRD::update for hdd-$disk: $ERROR\n" if $ERROR;
464 }
465
466 sub updatembmondata
467 {
468 if ( ! -e "$rrdlog/mbmon.rrd" )
469 {
470 # database did not exist -> create
471
472 @args = ("$rrdlog/mbmon.rrd");
473
474 push(@args, "--step=300");
475 foreach $key ( sort(keys %mbmon_values) )
476 {
477 push(@args, "DS:$key:GAUGE:600:U:U");
478 }
479 push(@args, "RRA:AVERAGE:0.5:1:576");
480 push(@args, "RRA:AVERAGE:0.5:6:672");
481 push(@args, "RRA:AVERAGE:0.5:24:732");
482 push(@args, "RRA:AVERAGE:0.5:144:1460");
483
484 print("create ". join( ", ", @args)) if ( $dbg );
485
486 RRDs::create (@args);
487 $ERROR = RRDs::error;
488 print("Error in RRD::create for mbmon: $ERROR\n") if $ERROR;
489 }
490
491 my @ds;
492 my @val;
493 my $template;
494
495 foreach $key ( sort(keys %mbmon_values) )
496 {
497 push(@ds, $key);
498 push(@val, $mbmon_values{$key});
499 }
500
501 $template = join(':', @ds);
502 $value = "N:".join(':', @val);
503
504 print("update template = '$template'\n") if ( $dbg );
505 print("update value = '$value'\n") if ( $dbg );
506
507 RRDs::update("$rrdlog/mbmon.rrd", "-t", $template, $value);
508 $ERROR = RRDs::error;
509 print("Error in RRD::update for mbmon: $ERROR\n") if $ERROR;
510 }
511
512 ## Update ipac logs
513 system ('/usr/sbin/fetchipac');
514 sleep 8;
515
516 ###
517 ### Squid Graphs
518 ###
519 if ( -e "/var/log/squid/access.log") {
520 system ("/usr/bin/squid-graph -o=/srv/web/ipfire/html/sgraph --tcp-only < /var/log/squid/access.log >/dev/null 2>&1");
521 }
522
523 ###
524 ### utf8 conversion
525 ###
526 if ((${Lang::language} eq 'cs') ||
527 (${Lang::language} eq 'hu') ||
528 (${Lang::language} eq 'pl') ||
529 (${Lang::language} eq 'sk')) {
530 # Czech, Hungarian, Polish and Slovak character set
531 foreach my $key(keys %Lang::tr) {
532 from_to($tr{$key}, "utf-8", "iso-8859-2");
533 }
534 } elsif (${Lang::language} eq 'tr') {
535 # Turkish
536 foreach my $key(keys %Lang::tr) {
537 from_to($tr{$key}, "utf-8", "iso-8859-9");
538 }
539 } else {
540 foreach my $key(keys %Lang::tr) {
541 from_to($tr{$key}, "utf-8", "iso-8859-1");
542 }
543 }
544
545 ###
546 ### System graphs
547 ###
548 updatecpudata();
549 updateloaddata();
550 updatememdata();
551
552 my @disks = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
553 print "\nFound following amount of disks:".@disks."\n";
554 foreach (@disks){
555 my $disk = $_;
556 chomp $disk;
557 print "Working on disk ".$disk.".\n";
558 updatediskdata($disk);
559 }
560
561 ###
562 ### Firewallhits
563 ###
564 updatefwhitsdata();
565
566 ###
567 ### Link Quality
568 ###
569 updatelq();
570
571 ###
572 ### HDDTEMP-Graphs for /dev/harddisk
573 ###
574
575 my @disks = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
576 print "\nFound following amount of disks for hddtemp:".@disks."\n";
577 foreach (@disks){
578 my $disk = $_;
579 chomp $disk;
580 updatehdddata ($disk);
581 }
582
583 ###
584 ### Network Graphs
585 ###
586 @ipacsum = `/usr/sbin/ipacsum --exact -s 5m 2>/dev/null`;
587 if (@ipacsum) {
588 updateifdata ("GREEN");
589 updateifdata ("RED");
590 if ($settings{'CONFIG_TYPE'} =~ /^(2|4)$/ ) {
591 updateifdata ("ORANGE");
592 }
593 if ($settings{'CONFIG_TYPE'} =~ /^(3|4)$/ ) {
594 updateifdata ("BLUE");
595 }
596 }