]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/scripts/makegraphs
Corrected guardian
[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 #use translated version for other languages
56 %tr=%Lang::tr;
57 }
58
59 # Settings
60 my $rrdlog = "/var/log/rrd";
61 my $graphs = "/srv/web/ipfire/html/graphs";
62 $ENV{PATH}="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
63 my $hdd_device = "/dev/harddisk";
64 my $temp = '';
65 my %mbmon_values = ();
66 my $key;
67 my $value;
68 my @args = ();
69 my $count = 0;
70 my $ERROR;
71 my $dbg = 0;
72 my $path_smartctl = "/usr/sbin/smartctl";
73 my $path_hddtemp = "/usr/sbin/hddtemp";
74
75 my %color = ();
76 my %mainsettings = ();
77 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
78 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
79
80 open(MBMON_OUT, ">/var/log/mbmon-values");
81 open(FD, "/usr/bin/mbmon -rc1|" ) || die "ERROR: Cannot run mbmon\n" ;
82
83 while( $_ = <FD> )
84 {
85 next unless( /^([A-Za-z][^:\s]+)\s*:\s*([+\-]{0,1}[\d\.]+)/ ) ;
86 $key = $1 ;
87 $value = $2 ;
88 $key =~ y/A-Z/a-z/ ;
89 $mbmon_values{$key} = $value;
90 print(MBMON_OUT "$key=$value\n");
91 }
92 close(FD);
93 close(MBMON_OUT);
94
95 sub updatehdddata
96 {
97 my $disk = $_[0];
98 my $standby;
99 my @array = split(/\//,$disk);
100
101 if ( ! -e "$rrdlog/hddshutdown-".$array[$#array].".rrd"){
102 # database did not exist -> create
103 RRDs::create ("$rrdlog/hddshutdown-".$array[$#array].".rrd", "--step=300",
104 "DS:standby:GAUGE:600:0:1",
105 "RRA:AVERAGE:0.5:1:576",
106 "RRA:AVERAGE:0.5:6:672",
107 "RRA:AVERAGE:0.5:24:732",
108 "RRA:AVERAGE:0.5:144:1460");
109 $ERROR = RRDs::error;
110 print "Error in RRD::create for hddshutdown-".$array[$#array].": $ERROR\n" if $ERROR;
111 }
112
113 if (-e "/tmp/hddshutdown-".$array[$#array]) {$standby = 1;}
114 else {$standby = 0;}
115
116 RRDs::update ("$rrdlog/hddshutdown-".$array[$#array].".rrd", "-t", "standby", "N:$standby");
117 $ERROR = RRDs::error;
118 print "Error in RRD::update for hddshutdown-".$array[$#array].": $ERROR\n" if $ERROR;
119
120 if ( ! -e "$rrdlog/hddtemp-".$array[$#array].".rrd"){
121 # database did not exist -> create
122 RRDs::create ("$rrdlog/hddtemp-".$array[$#array].".rrd", "--step=300",
123 "DS:temperature:GAUGE:600:0:100",
124 "RRA:AVERAGE:0.5:1:576",
125 "RRA:AVERAGE:0.5:6:672",
126 "RRA:AVERAGE:0.5:24:732",
127 "RRA:AVERAGE:0.5:144:1460");
128 $ERROR = RRDs::error;
129 print "Error in RRD::create for hdd-".$array[$#array].": $ERROR\n" if $ERROR;
130 }
131
132 # Temperaturlesen wÃrde die Platte aufwecken!!!
133 if (!$standby){
134 $temp = 0;
135 my $hdd_output = '';
136 my $smart_output = '';
137 if ( -e "$path_smartctl" ){system("$path_smartctl -iHA -d ata /dev/$disk > /var/log/smartctl_out_hddtemp-$disk");}
138 if ( -e "/var/log/smartctl_out_hddtemp-".$array[$#array] ){
139 $hdd_output = `cat /var/log/smartctl_out_hddtemp-$array[$#array] | grep Temperature_`;
140 my @t = split(/\s+/,$hdd_output);
141 $temp = $t[9];
142 }else{$temp = 0;}
143 print "Temperature for ".$array[$#array]."->".$temp."<-\n";
144 system("echo \"Temperature for $array[$#array] -> $temp <-\n\" >>/tmp/debug");
145 # Nur ins RDD wenn nicht 0 (sonst klappt die min Anzeige nicht)
146 if ($temp){
147 RRDs::update ("$rrdlog/hddtemp-".$array[$#array].".rrd", "-t", "temperature", "N:$temp");
148 $ERROR = RRDs::error;
149 print "Error in RRD::update for hdd-".$array[$#array].": $ERROR\n" if $ERROR;
150 }
151 }
152 }
153
154 sub updatembmondata
155 {
156 if ( ! -e "$rrdlog/mbmon.rrd" ){
157 # database did not exist -> create
158
159 @args = ("$rrdlog/mbmon.rrd");
160 push(@args, "--step=300");
161 foreach $key ( sort(keys %mbmon_values) ){push(@args, "DS:$key:GAUGE:600:U:U");}
162 push(@args, "RRA:AVERAGE:0.5:1:576");
163 push(@args, "RRA:AVERAGE:0.5:6:672");
164 push(@args, "RRA:AVERAGE:0.5:24:732");
165 push(@args, "RRA:AVERAGE:0.5:144:1460");
166
167 print("create ". join( ", ", @args)) if ( $dbg );
168 RRDs::create (@args);
169 $ERROR = RRDs::error;
170 print("Error in RRD::create for mbmon: $ERROR\n") if $ERROR;
171 }
172
173 my @ds;
174 my @val;
175 my $template;
176
177 foreach $key ( sort(keys %mbmon_values) ){
178 push(@ds, $key);
179 push(@val, $mbmon_values{$key});
180 }
181
182 $template = join(':', @ds);
183 $value = "N:".join(':', @val);
184
185 print("update template = '$template'\n") if ( $dbg );
186 print("update value = '$value'\n") if ( $dbg );
187
188 RRDs::update("$rrdlog/mbmon.rrd", "-t", $template, $value);
189 $ERROR = RRDs::error;
190 print("Error in RRD::update for mbmon: $ERROR\n") if $ERROR;
191 }
192
193 ## Update ipac logs
194 system ('/usr/sbin/fetchipac');
195 sleep 8;
196
197 ###
198 ### Squid Graphs
199 ###
200 if ( -e "/var/log/squid/access.log") {system ("/usr/bin/squid-graph -o=/srv/web/ipfire/html/sgraph --tcp-only < /var/log/squid/access.log >/dev/null 2>&1");}
201
202 ###
203 ### utf8 conversion
204 ###
205 if ((${Lang::language} eq 'cs') || (${Lang::language} eq 'hu') || (${Lang::language} eq 'pl') || (${Lang::language} eq 'sk')) {
206 # Czech, Hungarian, Polish and Slovak character set
207 foreach my $key(keys %Lang::tr) {from_to($tr{$key}, "utf-8", "iso-8859-2");}
208 } elsif (${Lang::language} eq 'tr') {
209 # Turkish
210 foreach my $key(keys %Lang::tr) {from_to($tr{$key}, "utf-8", "iso-8859-9");}
211 } else {
212 foreach my $key(keys %Lang::tr) {from_to($tr{$key}, "utf-8", "iso-8859-1");}
213 }
214
215 ###
216 ### HDD graphs
217 ###
218
219
220 my @disks = `kudzu -qps -c HD | grep device: | cut -d" " -f2 | sort | uniq`;
221 system("unlink /tmp/hddstatus && touch /tmp/hddstatus");
222 print "\nFound following amount of disks:".@disks."\n";
223 foreach (@disks){
224 my $disk = $_;
225 chomp $disk;
226 print "Working on disk ".$disk.".\n";
227
228 my $status = "";
229 my $diskstats = "";
230 my $newdiskstats = "";
231 my @array = split(/\//,$disk);
232
233 $diskstats = `cat /tmp/hddstats-$array[$#array]`;
234 chomp $diskstats;
235 my $newdiskstats = `/usr/bin/iostat -d -t $disk | tail -2 | head -1 | awk '{ print \$5","\$6}'`;
236 chomp $newdiskstats;
237 my $status = `hdparm -C /dev/$disk | tail -1 | cut -d: -f2`;
238 chomp $status;
239
240 if ($status !~/standby/ || $diskstats ne $newdiskstats){
241 if (-e "/tmp/hddshutdown-".$array[$#array]){system("unlink /tmp/hddshutdown-".$array[$#array]);}
242 }
243
244 if (-e "/tmp/hddshutdown-".$array[$#array]){$status = " standby\n";}
245 else{$status = " active\n";}
246
247 open(DATEI, ">>/tmp/hddstatus") || die "Datei nicht gefunden";
248 print DATEI $disk."-".$status;
249 close(DATEI);
250
251 updatehdddata($disk);
252
253 }
254
255 ###
256 ### Mbmon Data
257 ###
258 updatembmondata();