]>
Commit | Line | Data |
---|---|---|
cd1a2927 MT |
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 | ############################################################################ | |
60cbd6e7 | 24 | |
cd1a2927 MT |
25 | use strict; |
26 | #use warnings; | |
27 | ||
28 | use RRDs; | |
986e08d9 | 29 | require "/var/ipfire/general-functions.pl"; |
cd1a2927 MT |
30 | require "${General::swroot}/lang.pl"; |
31 | ||
32 | my (%settings, @ipacsum, $iface, $ERROR); | |
33 | &General::readhash("${General::swroot}/ethernet/settings", \%settings); | |
52345790 MT |
34 | my %mbmon_settings = (); |
35 | &General::readhash("${General::swroot}/mbmon/settings", \%mbmon_settings); | |
36 | ||
cd1a2927 MT |
37 | # Added for conversion of utf-8 characters |
38 | use Encode 'from_to'; | |
39 | my %tr=(); | |
40 | ||
de53a053 CS |
41 | system("chmod 777 /srv/web/ipfire/html/graphs"); |
42 | ||
cd1a2927 MT |
43 | # Force language back to English (ugly hack!) |
44 | # Modified to only force if we are unable to convert charset | |
45 | # from utf-8 | |
375c0b3b CS |
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"`; | |
cd1a2927 | 54 | } else { |
375c0b3b CS |
55 | use translated version for other languages |
56 | %tr=%Lang::tr; | |
cd1a2927 MT |
57 | } |
58 | ||
59 | # Settings | |
60 | my $rrdlog = "/var/log/rrd"; | |
fd0763dc | 61 | my $graphs = "/srv/web/ipfire/html/graphs"; |
cd1a2927 | 62 | $ENV{PATH}="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"; |
60cbd6e7 | 63 | my $hdd_device = "/dev/harddisk"; |
a68fedca | 64 | my $temp = ''; |
52345790 MT |
65 | my %mbmon_values = (); |
66 | my $key; | |
67 | my $value; | |
68 | my @args = (); | |
69 | my $count = 0; | |
70 | my $ERROR; | |
71 | my $dbg = 0; | |
52345790 | 72 | my $path_smartctl = "/usr/sbin/smartctl"; |
e3a8510a | 73 | my $path_hddtemp = "/usr/sbin/hddtemp"; |
52345790 | 74 | |
4b4aec3a CS |
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); | |
52345790 MT |
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 | { | |
375c0b3b CS |
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"); | |
52345790 MT |
91 | } |
92 | close(FD); | |
93 | close(MBMON_OUT); | |
cd1a2927 MT |
94 | |
95 | sub gettraffic { | |
375c0b3b CS |
96 | my $interface = $_[0]; |
97 | ||
98 | my $bytesin=0; | |
99 | my $bytesout=0; | |
100 | ||
101 | foreach (@ipacsum){ | |
102 | # Incoming... | |
103 | $bytesin += $1 if (/^[\* ]\s+incoming\s+${interface}.+\:\s+(\d+)/); | |
104 | # Forwarded Incoming... | |
105 | $bytesin += $1 if (/^[\* ]\s+forwarded\s+incoming\s+${interface}.+\:\s+(\d+)/); | |
106 | # Outgoing... | |
107 | $bytesout += $1 if (/^[* ]\s+outgoing\s+${interface}.+\:\s+(\d+)/); | |
108 | # Forwarded Outgoing... | |
109 | $bytesout += $1 if (/^[* ]\s+forwarded\s+outgoing\s+${interface}.+\:\s+(\d+)/); | |
110 | } | |
111 | return "$bytesin:$bytesout"; | |
cd1a2927 MT |
112 | } |
113 | ||
114 | sub updatecpudata { | |
375c0b3b CS |
115 | if ( ! -e "$rrdlog/cpu.rrd") { |
116 | RRDs::create ("$rrdlog/cpu.rrd", "--step=300", | |
117 | "DS:user:COUNTER:600:0:500000000", | |
118 | "DS:system:COUNTER:600:0:500000000", | |
119 | "DS:idle:COUNTER:600:0:500000000", | |
120 | "DS:iowait:COUNTER:600:0:500000000", | |
121 | "DS:irq: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 | } | |
cd1a2927 | 129 | |
375c0b3b CS |
130 | my ($cpu, $user, $nice, $system, $idle, $iowait, $irq, $softirq); |
131 | open STAT, "/proc/stat"; | |
132 | while(<STAT>) { | |
133 | chomp; | |
134 | /^cpu\s/ or next; | |
135 | ($cpu, $user, $nice, $system, $idle, $iowait, $irq, $softirq) = split /\s+/; | |
136 | last; | |
137 | } | |
138 | close STAT; | |
139 | $user += $nice; | |
140 | $irq += $softirq; | |
141 | RRDs::update ("$rrdlog/cpu.rrd", | |
142 | "-t", "user:system:idle:iowait:irq", | |
143 | "N:$user:$system:$idle:$iowait:$irq"); | |
144 | $ERROR = RRDs::error; | |
145 | print "Error in RRD::update for cpu: $ERROR\n" if $ERROR; | |
cd1a2927 MT |
146 | } |
147 | ||
207cc1cf | 148 | sub updateloaddata { |
375c0b3b CS |
149 | if ( ! -e "$rrdlog/load.rrd") { |
150 | RRDs::create ("$rrdlog/load.rrd", "--step=60", | |
151 | "DS:load1:GAUGE:120:0:U", | |
152 | "DS:load5:GAUGE:120:0:U", | |
153 | "DS:load15:GAUGE:120:0:U", | |
154 | "RRA:AVERAGE:0.5:1:2160", | |
155 | "RRA:AVERAGE:0.5:5:2016", | |
156 | "RRA:AVERAGE:0.5:15:2880", | |
157 | "RRA:AVERAGE:0.5:60:8760"); | |
158 | $ERROR = RRDs::error; | |
159 | print "Error in RRD::create for cpu: $ERROR\n" if $ERROR; | |
160 | } | |
cd1a2927 MT |
161 | } |
162 | ||
163 | sub updatememdata { | |
375c0b3b CS |
164 | my ($memused, $memfree, $memshared, $membuffers, $memcache, $swapused, $swapfree, $swaptotal); |
165 | if ( ! -e "$rrdlog/mem.rrd") { | |
166 | RRDs::create ("$rrdlog/mem.rrd", "--step=300", | |
167 | "DS:memused:ABSOLUTE:600:0:5000000000", | |
168 | "DS:memfree:ABSOLUTE:600:0:5000000000", | |
169 | "DS:memshared:ABSOLUTE:600:0:5000000000", | |
170 | "DS:membuffers:ABSOLUTE:600:0:5000000000", | |
171 | "DS:memcache:ABSOLUTE:600:0:5000000000", | |
172 | "DS:swapused:ABSOLUTE:600:0:5000000000", | |
173 | "DS:swapfree:ABSOLUTE:600:0:5000000000", | |
174 | "RRA:AVERAGE:0.5:1:576", | |
175 | "RRA:AVERAGE:0.5:6:672", | |
176 | "RRA:AVERAGE:0.5:24:732", | |
177 | "RRA:AVERAGE:0.5:144:1460"); | |
178 | $ERROR = RRDs::error; | |
179 | print "Error in RRD::create for mem: $ERROR\n" if $ERROR; | |
180 | } | |
181 | ||
182 | open MEM, "/proc/meminfo"; | |
183 | while(<MEM>) { | |
184 | chomp; | |
185 | if ($_ =~ /^MemTotal:/) {my @temp = split (/\s+/, $_);$memused = $temp[1]; | |
186 | } elsif ($_ =~ /^MemFree:/) {my @temp = split (/\s+/, $_);$memfree = $temp[1]; | |
187 | } elsif ($_ =~ /^Cached:/) {my @temp = split (/\s+/, $_);$memcache = $temp[1]; | |
188 | } elsif ($_ =~ /^Buffers:/) {my @temp = split (/\s+/, $_);$membuffers = $temp[1]; | |
189 | } elsif ($_ =~ /^SwapTotal:/) {my @temp = split (/\s+/, $_);$swaptotal = $temp[1]; | |
190 | } elsif ($_ =~ /^SwapFree:/) {my @temp = split (/\s+/, $_);$swapfree = $temp[1];} | |
191 | } | |
192 | close MEM; | |
193 | ||
194 | system("/bin/df > /tmp/diskfree"); | |
195 | open DF, "/tmp/diskfree"; | |
196 | while(<DF>) { | |
197 | chomp; | |
198 | if ($_ =~ /^shm/) {my @temp = split (/\s+/, $_);$memshared = $temp[2];} | |
199 | } | |
200 | close DF; | |
201 | system("/bin/rm -f /tmp/diskfree"); | |
202 | $swapused = $swaptotal-$swapfree; | |
203 | RRDs::update ("$rrdlog/mem.rrd", | |
204 | "-t", "memused:memfree:memshared:membuffers:memcache:swapused:swapfree", | |
205 | "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"); | |
206 | $ERROR = RRDs::error; | |
207 | print "Error in RRD::update for mem: $ERROR\n" if $ERROR; | |
cd1a2927 MT |
208 | } |
209 | ||
210 | sub updatediskdata { | |
375c0b3b CS |
211 | my $disk = $_[0]; |
212 | my ($readsect, $writesect, $trash); | |
213 | my @array = split(/\//,$disk); | |
214 | if ( ! -e "$rrdlog/disk-".$array[$#array].".rrd") { | |
215 | RRDs::create ("$rrdlog/disk-".$array[$#array].".rrd", "--step=300", | |
216 | "DS:readsect:COUNTER:600:0:5000000000", | |
217 | "DS:writesect:COUNTER:600:0:5000000000", | |
218 | "RRA:AVERAGE:0.5:1:576", | |
219 | "RRA:AVERAGE:0.5:6:672", | |
220 | "RRA:AVERAGE:0.5:24:732", | |
221 | "RRA:AVERAGE:0.5:144:1460"); | |
222 | $ERROR = RRDs::error; | |
223 | print "Error in RRD::create for disk $disk: $ERROR\n" if $ERROR; | |
224 | } | |
225 | my $Zeilen = `/usr/bin/iostat $disk | tail -2 | head -1`; | |
226 | ($trash, $trash, $trash, $trash, $readsect, $writesect) = split(/\s+/,$Zeilen); | |
227 | print "\nread:".$readsect."write:".$writesect."\n"; | |
228 | if ($readsect && $writesect) { | |
229 | RRDs::update ("$rrdlog/disk-".$array[$#array].".rrd", | |
230 | "-t", "readsect:writesect", | |
231 | "N:$readsect:$writesect"); | |
232 | $ERROR = RRDs::error; | |
233 | print "Error in RRD::update for disk $disk: $ERROR\n" if $ERROR; | |
234 | } else {print "Error in RRD::update for disk: $disk no data available\n";} | |
cd1a2927 MT |
235 | } |
236 | ||
237 | sub updateifdata { | |
375c0b3b CS |
238 | my $interface = $_[0]; |
239 | if ( ! -e "$rrdlog/$interface.rrd") { | |
240 | RRDs::create ("$rrdlog/$interface.rrd", "--step=300", | |
241 | "DS:incoming:ABSOLUTE:600:0:12500000", | |
242 | "DS:outgoing:ABSOLUTE:600:0:12500000", | |
243 | "RRA:AVERAGE:0.5:1:576", | |
244 | "RRA:AVERAGE:0.5:6:672", | |
245 | "RRA:AVERAGE:0.5:24:732", | |
246 | "RRA:AVERAGE:0.5:144:1460"); | |
247 | $ERROR = RRDs::error; | |
248 | print "Error in RRD::create for $interface: $ERROR\n" if $ERROR; | |
249 | } | |
250 | my $traffic = gettraffic ($interface); | |
251 | RRDs::update ("$rrdlog/$interface.rrd", | |
252 | "-t", "incoming:outgoing", | |
253 | "N:$traffic"); | |
254 | $ERROR = RRDs::error; | |
255 | print "Error in RRD::update for $interface: $ERROR\n" if $ERROR; | |
df8c7810 MT |
256 | } |
257 | ||
258 | sub updatefwhitsdata { | |
375c0b3b CS |
259 | my $portamount=0; |
260 | my $alertaktuell=0; | |
261 | my $aktuell=0; | |
262 | my $portaktuell=0; | |
263 | my $skip=0; | |
264 | if (! -e "$rrdlog/firewallhits.rrd"){ | |
265 | RRDs::create ("$rrdlog/firewallhits.rrd", "--step=300", | |
266 | "DS:amount:GAUGE:600:0:U", | |
267 | "DS:portamount:GAUGE:600:0:U", | |
268 | "RRA:AVERAGE:0.5:1:576", | |
269 | "RRA:AVERAGE:0.5:6:672", | |
270 | "RRA:AVERAGE:0.5:24:732", | |
271 | "RRA:AVERAGE:0.5:144:1460"); | |
272 | $ERROR = RRDs::error; | |
273 | print "Error in RRD::create for cpu: $ERROR\n" if $ERROR; | |
274 | } | |
275 | system("logtailfwhits /var/log/messages /var/log/fwhits.messages.offset >/tmp/messages.fwhits"); | |
276 | if (!(open (FILE,'/tmp/messages.fwhits'))) {$skip=1;} | |
277 | $aktuell = 0; | |
278 | if (!$skip) { | |
279 | while (<FILE>) { | |
280 | if (/kernel:.*(IN=.*)$/) {$aktuell++;} | |
281 | } | |
282 | close (FILE); | |
283 | } | |
284 | system("logtailfwhits /var/log/snort/alert /var/log/snort/fwhits.alert.offset >/tmp/snort.fwhits"); | |
285 | if (!(open (FILE,'/tmp/snort.fwhits'))) {$skip=1;} | |
286 | $alertaktuell = 0; | |
287 | if (!$skip) { | |
288 | while (<FILE>) { | |
289 | if (/scan.*$/) {$alertaktuell++;} | |
290 | } | |
291 | close (FILE); | |
292 | } | |
293 | if (!(open (FILE,'/tmp/messages.fwhits'))) {$skip=1;} | |
294 | $portaktuell = 0; | |
295 | if (!$skip) { | |
296 | while (<FILE>) { | |
297 | if (/kernel:.*(Scan.*)$/) {$portaktuell++;} | |
298 | } | |
299 | close (FILE); | |
300 | } | |
301 | system("rm /tmp/messages.fwhits"); | |
302 | system("rm /tmp/snort.fwhits"); | |
303 | $portamount = $portaktuell + $alertaktuell; | |
304 | chomp($portamount); | |
305 | RRDs::update ("$rrdlog/firewallhits.rrd", | |
306 | "N:$aktuell:$portamount"); | |
307 | $ERROR = RRDs::error; | |
308 | print "Error in RRD::update for Firewallhits: $ERROR\n" if $ERROR; | |
df8c7810 MT |
309 | } |
310 | ||
072cd997 MT |
311 | # Creates and updates a link quality database |
312 | # ------------------------------------------- | |
313 | sub updatelq { | |
375c0b3b CS |
314 | if ( ! -e "$rrdlog/lq.rrd") { |
315 | RRDs::create ("$rrdlog/lq.rrd", "--step=300", | |
316 | "DS:loss:GAUGE:600:0:100", | |
317 | "DS:roundtrip:GAUGE:600:0:10000", | |
318 | "RRA:AVERAGE:0.5:1:576", | |
319 | "RRA:AVERAGE:0.5:6:672", | |
320 | "RRA:AVERAGE:0.5:24:732", | |
321 | "RRA:AVERAGE:0.5:144:1460"); | |
322 | $ERROR = RRDs::error; | |
323 | print "Error in RRD::create for link: $ERROR\n" if $ERROR; | |
324 | } | |
325 | my $packetloss=0; | |
326 | my $roundtrip=0; | |
327 | my $test=0; | |
328 | #LQ_GATEWAY is the ip of your isp's public ip facing you | |
329 | my $LQ_GATEWAY=`cat /var/ipfire/red/remote-ipaddress`; | |
330 | chomp($LQ_GATEWAY); | |
331 | my $NUMPINGS=5; | |
332 | my $pingoutput = `/usr/bin/ping -w 10 -c $NUMPINGS -q $LQ_GATEWAY | tail -2`; | |
333 | my @temp = split (/\/|\%|\s/, $pingoutput); | |
334 | $packetloss = $temp[5]; | |
335 | $roundtrip = $temp[17]; | |
336 | print "Paketlos->".$packetloss."<-Roundtrip->".$roundtrip."<-\n"; | |
337 | if ( $packetloss eq "100" ){ | |
338 | my $pingoutput = `/usr/bin/ping -w 10 -c $NUMPINGS -q ping.ipfire.org | tail -2`; | |
339 | my @temp = split (/\/|\%|\s/, $pingoutput); | |
340 | $packetloss = $temp[5]; | |
341 | $roundtrip = $temp[17]; | |
342 | print "Paketlos->".$packetloss."<-Roundtrip->".$roundtrip."<-\n"; | |
343 | } | |
344 | RRDs::update ("$rrdlog/lq.rrd", "N:$packetloss:$roundtrip"); | |
345 | $ERROR = RRDs::error; | |
346 | print "Error in RRD::update for line quality: $ERROR\n" if $ERROR; | |
072cd997 MT |
347 | } |
348 | ||
60cbd6e7 MT |
349 | sub updatehdddata |
350 | { | |
375c0b3b CS |
351 | my $disk = $_[0]; |
352 | my $standby; | |
353 | my @array = split(/\//,$disk); | |
354 | ||
355 | if ( ! -e "$rrdlog/hddshutdown-".$array[$#array].".rrd"){ | |
356 | # database did not exist -> create | |
357 | RRDs::create ("$rrdlog/hddshutdown-".$array[$#array].".rrd", "--step=300", | |
358 | "DS:standby:GAUGE:600:0:1", | |
359 | "RRA:AVERAGE:0.5:1:576", | |
360 | "RRA:AVERAGE:0.5:6:672", | |
361 | "RRA:AVERAGE:0.5:24:732", | |
362 | "RRA:AVERAGE:0.5:144:1460"); | |
363 | $ERROR = RRDs::error; | |
364 | print "Error in RRD::create for hddshutdown-".$array[$#array].": $ERROR\n" if $ERROR; | |
471a46f7 | 365 | } |
375c0b3b CS |
366 | |
367 | if (-e "/tmp/hddshutdown-".$array[$#array]) {$standby = 1;} | |
368 | else {$standby = 0;} | |
369 | ||
370 | RRDs::update ("$rrdlog/hddshutdown-".$array[$#array].".rrd", "-t", "standby", "N:$standby"); | |
371 | $ERROR = RRDs::error; | |
372 | print "Error in RRD::update for hddshutdown-".$array[$#array].": $ERROR\n" if $ERROR; | |
373 | ||
374 | if ( ! -e "$rrdlog/hddtemp-".$array[$#array].".rrd"){ | |
375 | # database did not exist -> create | |
376 | RRDs::create ("$rrdlog/hddtemp-".$array[$#array].".rrd", "--step=300", | |
377 | "DS:temperature:GAUGE:600:0:100", | |
378 | "RRA:AVERAGE:0.5:1:576", | |
379 | "RRA:AVERAGE:0.5:6:672", | |
380 | "RRA:AVERAGE:0.5:24:732", | |
381 | "RRA:AVERAGE:0.5:144:1460"); | |
382 | $ERROR = RRDs::error; | |
383 | print "Error in RRD::create for hdd-".$array[$#array].": $ERROR\n" if $ERROR; | |
471a46f7 | 384 | } |
375c0b3b CS |
385 | |
386 |