]>
Commit | Line | Data |
---|---|---|
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 | ||
38 | ||
39 | # Added for conversion of utf-8 characters | |
40 | use Encode 'from_to'; | |
41 | my %tr=(); | |
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 = "/home/httpd/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_rrd = "/var/log/rrd"; | |
72 | my $path_graphs = "/home/httpd/html/graphs"; | |
73 | my $path_smartctl = "/usr/sbin/smartctl"; | |
74 | ||
75 | my %colors = (); | |
76 | $colors{"1"} = "#0000FF"; | |
77 | $colors{"2"} = "#00FF00"; | |
78 | $colors{"3"} = "#FF0000"; | |
79 | $colors{"4"} = "#FFD700"; | |
80 | $colors{"5"} = "#CCCCCC"; | |
81 | $colors{"6"} = "#40E0D0"; | |
82 | $colors{"7"} = "#90EE90"; | |
83 | $colors{"8"} = "#F4A460"; | |
84 | ||
85 | open(MBMON_OUT, ">/var/log/mbmon-values"); | |
86 | open(FD, "/usr/bin/mbmon -rc1|" ) || die "ERROR: Cannot run mbmon\n" ; | |
87 | ||
88 | while( $_ = <FD> ) | |
89 | { | |
90 | next unless( /^([A-Za-z][^:\s]+)\s*:\s*([+\-]{0,1}[\d\.]+)/ ) ; | |
91 | $key = $1 ; | |
92 | $value = $2 ; | |
93 | $key =~ y/A-Z/a-z/ ; | |
94 | $mbmon_values{$key} = $value; | |
95 | print(MBMON_OUT "$key=$value\n"); | |
96 | } | |
97 | close(FD); | |
98 | close(MBMON_OUT); | |
99 | ||
100 | sub gettraffic { | |
101 | my $interface = $_[0]; | |
102 | ||
103 | my $bytesin=0; | |
104 | my $bytesout=0; | |
105 | ||
106 | foreach (@ipacsum) | |
107 | { | |
108 | # Incoming... | |
109 | $bytesin += $1 if (/^[\* ]\s+incoming\s+${interface}.+\:\s+(\d+)/); | |
110 | ||
111 | # Forwarded Incoming... | |
112 | $bytesin += $1 if (/^[\* ]\s+forwarded\s+incoming\s+${interface}.+\:\s+(\d+)/); | |
113 | ||
114 | # Outgoing... | |
115 | $bytesout += $1 if (/^[* ]\s+outgoing\s+${interface}.+\:\s+(\d+)/); | |
116 | ||
117 | # Forwarded Outgoing... | |
118 | $bytesout += $1 if (/^[* ]\s+forwarded\s+outgoing\s+${interface}.+\:\s+(\d+)/); | |
119 | } | |
120 | return "$bytesin:$bytesout"; | |
121 | } | |
122 | ||
123 | sub updatecpugraph { | |
124 | my $period = $_[0]; | |
125 | ||
126 | RRDs::graph ("$graphs/cpu-$period.png", | |
127 | "--start", "-1$period", "-aPNG", "-i", "-z", | |
128 | "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-u 100", "-r", | |
129 | "--color", "SHADEA#EAE9EE", | |
130 | "--color", "SHADEB#EAE9EE", | |
131 | "--color", "BACK#FFFFFF", | |
132 | "-t $tr{'cpu usage per'} $tr{$period}", | |
133 | "DEF:user=$rrdlog/cpu.rrd:user:AVERAGE", | |
134 | "DEF:system=$rrdlog/cpu.rrd:system:AVERAGE", | |
135 | "DEF:idle=$rrdlog/cpu.rrd:idle:AVERAGE", | |
136 | "CDEF:total=user,system,idle,+,+", | |
137 | "CDEF:userpct=100,user,total,/,*", | |
138 | "CDEF:systempct=100,system,total,/,*", | |
139 | "CDEF:idlepct=100,idle,total,/,*", | |
140 | "AREA:userpct#0000FF:$tr{'user cpu usage'}\\j", | |
141 | "STACK:systempct#FF0000:$tr{'system cpu usage'}\\j", | |
142 | "STACK:idlepct#00FF00:$tr{'idle cpu usage'}\\j", | |
143 | "GPRINT:userpct:MAX:$tr{'maximal'} $tr{'user cpu'}\\:%3.2lf%%", | |
144 | "GPRINT:userpct:AVERAGE:$tr{'average'} $tr{'user cpu'}\\:%3.2lf%%", | |
145 | "GPRINT:userpct:LAST:$tr{'current'} $tr{'user cpu'}\\:%3.2lf%%\\j", | |
146 | "GPRINT:systempct:MAX:$tr{'maximal'} $tr{'system cpu'}\\:%3.2lf%%", | |
147 | "GPRINT:systempct:AVERAGE:$tr{'average'} $tr{'system cpu'}\\:%3.2lf%%", | |
148 | "GPRINT:systempct:LAST:$tr{'current'} $tr{'system cpu'}\\:%3.2lf%%\\j", | |
149 | "GPRINT:idlepct:MAX:$tr{'maximal'} $tr{'idle cpu'}\\:%3.2lf%%", | |
150 | "GPRINT:idlepct:AVERAGE:$tr{'average'} $tr{'idle cpu'}\\:%3.2lf%%", | |
151 | "GPRINT:idlepct:LAST:$tr{'current'} $tr{'idle cpu'}\\:%3.2lf%%\\j"); | |
152 | $ERROR = RRDs::error; | |
153 | print "Error in RRD::graph for cpu: $ERROR\n" if $ERROR; | |
154 | } | |
155 | ||
156 | sub updatecpudata { | |
157 | if ( ! -e "$rrdlog/cpu.rrd") { | |
158 | RRDs::create ("$rrdlog/cpu.rrd", "--step=300", | |
159 | "DS:user:COUNTER:600:0:500000000", | |
160 | "DS:system:COUNTER:600:0:500000000", | |
161 | "DS:idle:COUNTER:600:0:500000000", | |
162 | "RRA:AVERAGE:0.5:1:576", | |
163 | "RRA:AVERAGE:0.5:6:672", | |
164 | "RRA:AVERAGE:0.5:24:732", | |
165 | "RRA:AVERAGE:0.5:144:1460"); | |
166 | $ERROR = RRDs::error; | |
167 | print "Error in RRD::create for cpu: $ERROR\n" if $ERROR; | |
168 | } | |
169 | ||
170 | my ($cpu, $user, $nice, $system, $idle); | |
171 | ||
172 | open STAT, "/proc/stat"; | |
173 | while(<STAT>) { | |
174 | chomp; | |
175 | /^cpu\s/ or next; | |
176 | ($cpu, $user, $nice, $system, $idle) = split /\s+/; | |
177 | last; | |
178 | } | |
179 | close STAT; | |
180 | $user += $nice; | |
181 | ||
182 | RRDs::update ("$rrdlog/cpu.rrd", | |
183 | "-t", "user:system:idle", | |
184 | "N:$user:$system:$idle"); | |
185 | $ERROR = RRDs::error; | |
186 | print "Error in RRD::update for cpu: $ERROR\n" if $ERROR; | |
187 | ||
188 | } | |
189 | ||
190 | sub updatememgraph { | |
191 | my $period = $_[0]; | |
192 | ||
193 | RRDs::graph ("$graphs/memory-$period.png", | |
194 | "--start", "-1$period", "-aPNG", "-i", "-z", | |
195 | "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-u 100", "-r", | |
196 | "--color", "SHADEA#EAE9EE", | |
197 | "--color", "SHADEB#EAE9EE", | |
198 | "--color", "BACK#FFFFFF", | |
199 | "-t $tr{'memory usage per'} $tr{$period}", | |
200 | "DEF:used=$rrdlog/mem.rrd:memused:AVERAGE", | |
201 | "DEF:free=$rrdlog/mem.rrd:memfree:AVERAGE", | |
202 | "DEF:shared=$rrdlog/mem.rrd:memshared:AVERAGE", | |
203 | "DEF:buffer=$rrdlog/mem.rrd:membuffers:AVERAGE", | |
204 | "DEF:cache=$rrdlog/mem.rrd:memcache:AVERAGE", | |
205 | "CDEF:total=used,free,+", | |
206 | "CDEF:used2=used,buffer,cache,shared,+,+,-", | |
207 | "CDEF:usedpct=100,used2,total,/,*", | |
208 | "CDEF:sharedpct=100,shared,total,/,*", | |
209 | "CDEF:bufferpct=100,buffer,total,/,*", | |
210 | "CDEF:cachepct=100,cache,total,/,*", | |
211 | "CDEF:freepct=100,free,total,/,*", | |
212 | "AREA:usedpct#0000FF:$tr{'used memory'}\\j", | |
213 | "STACK:sharedpct#FF0000:$tr{'shared memory'}\\j", | |
214 | "STACK:bufferpct#FF00FF:$tr{'buffered memory'}\\j", | |
215 | "STACK:cachepct#FFFF00:$tr{'cached memory'}\\j", | |
216 | "STACK:freepct#00FF00:$tr{'free memory'}\\j", | |
217 | "GPRINT:usedpct:MAX:$tr{'maximal'} $tr{'used memory'}\\:%3.2lf%%", | |
218 | "GPRINT:usedpct:AVERAGE:$tr{'average'} $tr{'used memory'}\\:%3.2lf%%", | |
219 | "GPRINT:usedpct:LAST:$tr{'current'} $tr{'used memory'}\\:%3.2lf%%\\j", | |
220 | "GPRINT:sharedpct:MAX:$tr{'maximal'} $tr{'shared memory'}\\:%3.2lf%%", | |
221 | "GPRINT:sharedpct:AVERAGE:$tr{'average'} $tr{'shared memory'}\\:%3.2lf%%", | |
222 | "GPRINT:sharedpct:LAST:$tr{'current'} $tr{'shared memory'}\\:%3.2lf%%\\j", | |
223 | "GPRINT:bufferpct:MAX:$tr{'maximal'} $tr{'buffered memory'}\\:%3.2lf%%", | |
224 | "GPRINT:bufferpct:AVERAGE:$tr{'average'} $tr{'buffered memory'}\\:%3.2lf%%", | |
225 | "GPRINT:bufferpct:LAST:$tr{'current'} $tr{'buffered memory'}\\:%3.2lf%%\\j", | |
226 | "GPRINT:cachepct:MAX:$tr{'maximal'} $tr{'cached memory'}\\:%3.2lf%%", | |
227 | "GPRINT:cachepct:AVERAGE:$tr{'average'} $tr{'cached memory'}\\:%3.2lf%%", | |
228 | "GPRINT:cachepct:LAST:$tr{'current'} $tr{'cached memory'}\\:%3.2lf%%\\j", | |
229 | "GPRINT:freepct:MAX:$tr{'maximal'} $tr{'free memory'}\\:%3.2lf%%", | |
230 | "GPRINT:freepct:AVERAGE:$tr{'average'} $tr{'free memory'}\\:%3.2lf%%", | |
231 | "GPRINT:freepct:LAST:$tr{'current'} $tr{'free memory'}\\:%3.2lf%%\\j"); | |
232 | $ERROR = RRDs::error; | |
233 | print "Error in RRD::graph for mem: $ERROR\n" if $ERROR; | |
234 | ||
235 | RRDs::graph ("$graphs/swap-$period.png", | |
236 | "--start", "-1$period", "-aPNG", "-i", "-z", | |
237 | "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-u 100", "-r", | |
238 | "--color", "SHADEA#EAE9EE", | |
239 | "--color", "SHADEB#EAE9EE", | |
240 | "--color", "BACK#FFFFFF", | |
241 | "-t $tr{'swap usage per'} $tr{$period}", | |
242 | "DEF:used=$rrdlog/mem.rrd:swapused:AVERAGE", | |
243 | "DEF:free=$rrdlog/mem.rrd:swapfree:AVERAGE", | |
244 | "CDEF:total=used,free,+", | |
245 | "CDEF:usedpct=100,used,total,/,*", | |
246 | "CDEF:freepct=100,free,total,/,*", | |
247 | "AREA:usedpct#0000FF:$tr{'used swap'}\\j", | |
248 | "STACK:freepct#00FF00:$tr{'free swap'}\\j", | |
249 | "GPRINT:usedpct:MAX:$tr{'maximal'} $tr{'used swap'}\\:%3.2lf%%", | |
250 | "GPRINT:usedpct:AVERAGE:$tr{'average'} $tr{'used swap'}\\:%3.2lf%%", | |
251 | "GPRINT:usedpct:LAST:$tr{'current'} $tr{'used swap'}\\:%3.2lf%%\\j", | |
252 | "GPRINT:freepct:MAX:$tr{'maximal'} $tr{'free swap'}\\:%3.2lf%%", | |
253 | "GPRINT:freepct:AVERAGE:$tr{'average'} $tr{'free swap'}\\:%3.2lf%%", | |
254 | "GPRINT:freepct:LAST:$tr{'current'} $tr{'free swap'}\\:%3.2lf%%\\j"); | |
255 | $ERROR = RRDs::error; | |
256 | print "Error in RRD::graph for swap: $ERROR\n" if $ERROR; | |
257 | } | |
258 | ||
259 | sub updatememdata { | |
260 | my ($memused, $memfree, $memshared, $membuffers, $memcache, $swapused, $swapfree); | |
261 | if ( ! -e "$rrdlog/mem.rrd") { | |
262 | RRDs::create ("$rrdlog/mem.rrd", "--step=300", | |
263 | "DS:memused:ABSOLUTE:600:0:5000000000", | |
264 | "DS:memfree:ABSOLUTE:600:0:5000000000", | |
265 | "DS:memshared:ABSOLUTE:600:0:5000000000", | |
266 | "DS:membuffers:ABSOLUTE:600:0:5000000000", | |
267 | "DS:memcache:ABSOLUTE:600:0:5000000000", | |
268 | "DS:swapused:ABSOLUTE:600:0:5000000000", | |
269 | "DS:swapfree:ABSOLUTE:600:0:5000000000", | |
270 | "RRA:AVERAGE:0.5:1:576", | |
271 | "RRA:AVERAGE:0.5:6:672", | |
272 | "RRA:AVERAGE:0.5:24:732", | |
273 | "RRA:AVERAGE:0.5:144:1460"); | |
274 | $ERROR = RRDs::error; | |
275 | print "Error in RRD::create for mem: $ERROR\n" if $ERROR; | |
276 | } | |
277 | ||
278 | open MEM, "/proc/meminfo"; | |
279 | while(<MEM>) { | |
280 | chomp; | |
281 | if ($_ =~ /^Mem:/) { | |
282 | my @temp = split (/\s+/, $_); | |
283 | $memused = $temp[2]; | |
284 | $memfree = $temp[3]; | |
285 | $memshared = $temp[4]; | |
286 | $membuffers = $temp[5]; | |
287 | $memcache = $temp[6]; | |
288 | } elsif ($_ =~ /^Swap:/) { | |
289 | my @temp = split (/\s+/, $_); | |
290 | $swapused = $temp[2]; | |
291 | $swapfree = $temp[3]; | |
292 | } | |
293 | } | |
294 | close MEM; | |
295 | ||
296 | RRDs::update ("$rrdlog/mem.rrd", | |
297 | "-t", "memused:memfree:memshared:membuffers:memcache:swapused:swapfree", | |
298 | "N:$memused:$memfree:$memshared:$membuffers:$memcache:$swapused:$swapfree"); | |
299 | $ERROR = RRDs::error; | |
300 | print "Error in RRD::update for mem: $ERROR\n" if $ERROR; | |
301 | } | |
302 | ||
303 | sub updatediskgraph { | |
304 | my $period = $_[0]; | |
305 | ||
306 | RRDs::graph ("$graphs/disk-$period.png", | |
307 | "--start", "-1$period", "-aPNG", "-i", "-z", | |
308 | "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-r", | |
309 | "--color", "SHADEA#EAE9EE", | |
310 | "--color", "SHADEB#EAE9EE", | |
311 | "--color", "BACK#FFFFFF", | |
312 | "-t $tr{'disk access per'} $tr{$period}", | |
313 | "DEF:read=$rrdlog/disk.rrd:readsect:AVERAGE", | |
314 | "DEF:write=$rrdlog/disk.rrd:writesect:AVERAGE", | |
315 | "AREA:read#0000FF:$tr{'sectors read from disk per second'}\\j", | |
316 | "STACK:write#00FF00:$tr{'sectors written to disk per second'}\\j", | |
317 | "GPRINT:read:MAX:$tr{'maximal'} $tr{'read sectors'}\\:%8.0lf", | |
318 | "GPRINT:read:AVERAGE:$tr{'average'} $tr{'read sectors'}\\:%8.0lf", | |
319 | "GPRINT:read:LAST:$tr{'current'} $tr{'read sectors'}\\:%8.0lf\\j", | |
320 | "GPRINT:write:MAX:$tr{'maximal'} $tr{'written sectors'}\\:%8.0lf", | |
321 | "GPRINT:write:AVERAGE:$tr{'average'} $tr{'written sectors'}\\:%8.0lf", | |
322 | "GPRINT:write:LAST:$tr{'current'} $tr{'written sectors'}\\:%8.0lf\\j"); | |
323 | $ERROR = RRDs::error; | |
324 | print "Error in RRD::graph for disk: $ERROR\n" if $ERROR; | |
325 | } | |
326 | ||
327 | sub updatediskdata { | |
328 | my ($readwritereq, $readreq, $readsect, $writereq, $writesect); | |
329 | if ( ! -e "$rrdlog/disk.rrd") { | |
330 | RRDs::create ("$rrdlog/disk.rrd", "--step=300", | |
331 | "DS:readsect:COUNTER:600:0:5000000000", | |
332 | "DS:writesect:COUNTER:600:0:5000000000", | |
333 | "RRA:AVERAGE:0.5:1:576", | |
334 | "RRA:AVERAGE:0.5:6:672", | |
335 | "RRA:AVERAGE:0.5:24:732", | |
336 | "RRA:AVERAGE:0.5:144:1460"); | |
337 | $ERROR = RRDs::error; | |
338 | print "Error in RRD::create for disk: $ERROR\n" if $ERROR; | |
339 | } | |
340 | ||
341 | my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, | |
342 | $atime, $mtime, $ctime, $blksize, $blocks) = stat("/dev/harddisk"); | |
343 | ||
344 | my $major = $rdev >> 8; | |
345 | my $minor = $rdev & 0xFF; | |
346 | ||
347 | open STAT, "/proc/stat"; | |
348 | my @diskstat = <STAT>; | |
349 | close (STAT); | |
350 | foreach my $line (@diskstat) | |
351 | { | |
352 | chomp ($line); | |
353 | my @temp = split(/\:\ /,$line); | |
354 | if ($temp[1]) { | |
355 | my @devicestat = split(/\ /,$temp[1]); | |
356 | foreach my $stats (@devicestat) | |
357 | { | |
358 | chomp ($stats); | |
359 | my @fields = split(/\((\d+),(\d+)\):\((\d+),(\d+),(\d+),(\d+),(\d+)/,$stats); | |
360 | if ($major eq $fields[1] and $minor eq $fields[2]) | |
361 | { | |
362 | $readwritereq = $fields[3]; | |
363 | $readreq = $fields[4]; | |
364 | $readsect = $fields[5]; | |
365 | $writereq = $fields[6]; | |
366 | $writesect = $fields[7]; | |
367 | } | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | if ($readsect && $writesect) { | |
373 | RRDs::update ("$rrdlog/disk.rrd", | |
374 | "-t", "readsect:writesect", | |
375 | "N:$readsect:$writesect"); | |
376 | $ERROR = RRDs::error; | |
377 | print "Error in RRD::update for disk: $ERROR\n" if $ERROR; | |
378 | } else { | |
379 | print "Error in RRD::update for disk: no data available\n"; | |
380 | } | |
381 | } | |
382 | ||
383 | sub updatediskgraphnew { | |
384 | my $disk = $_[0]; | |
385 | my $period = $_[1]; | |
386 | ||
387 | RRDs::graph ("$graphs/disk-$disk-$period.png", | |
388 | "--start", "-1$period", "-aPNG", "-i", "-z", | |
389 | "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-r", | |
390 | "--color", "SHADEA#EAE9EE", | |
391 | "--color", "SHADEB#EAE9EE", | |
392 | "--color", "BACK#FFFFFF", | |
393 | "-t $tr{'disk access per'} $tr{$period}", | |
394 | "DEF:read=$rrdlog/disk-$disk.rrd:readsect:AVERAGE", | |
395 | "DEF:write=$rrdlog/disk-$disk.rrd:writesect:AVERAGE", | |
396 | "DEF:sleep=$rrdlog/disk-$disk.rrd:sleeping:AVERAGE", | |
397 | "CDEF:sl_state=sleep,INF,*", | |
398 | ||
399 | "AREA:sl_state#a0a0a0:disk standby\\j", | |
400 | "AREA:read#0000FF:$tr{'sectors read from disk per second'}\\j", | |
401 | "STACK:write#00FF00:$tr{'sectors written to disk per second'}\\j", | |
402 | ||
403 | "GPRINT:read:MAX:$tr{'maximal'} $tr{'read sectors'}\\:%8.0lf", | |
404 | "GPRINT:read:AVERAGE:$tr{'average'} $tr{'read sectors'}\\:%8.0lf", | |
405 | "GPRINT:read:LAST:$tr{'current'} $tr{'read sectors'}\\:%8.0lf\\j", | |
406 | "GPRINT:write:MAX:$tr{'maximal'} $tr{'written sectors'}\\:%8.0lf", | |
407 | "GPRINT:write:AVERAGE:$tr{'average'} $tr{'written sectors'}\\:%8.0lf", | |
408 | "GPRINT:write:LAST:$tr{'current'} $tr{'written sectors'}\\:%8.0lf\\j"); | |
409 | $ERROR = RRDs::error; | |
410 | print "Error in RRD::graph for disk-$disk: $ERROR\n" if $ERROR; | |
411 | } | |
412 | ||
413 | sub updatediskdatanew { | |
414 | my $disk = $_[0]; | |
415 | ||
416 | my ($readwritereq, $readreq, $readsect, $writereq, $writesect); | |
417 | if ( ! -e "$rrdlog/disk-$disk.rrd") { | |
418 | RRDs::create ("$rrdlog/disk-$disk.rrd", "--step=300", | |
419 | "DS:readsect:COUNTER:600:0:5000000000", | |
420 | "DS:writesect:COUNTER:600:0:5000000000", | |
421 | "DS:sleeping:GAUGE:600:0:1", | |
422 | "RRA:AVERAGE:0.5:1:576", | |
423 | "RRA:AVERAGE:0.5:6:672", | |
424 | "RRA:AVERAGE:0.5:24:732", | |
425 | "RRA:AVERAGE:0.5:144:1460"); | |
426 | $ERROR = RRDs::error; | |
427 | print "Error in RRD::create for disk-$disk: $ERROR\n" if $ERROR; | |
428 | } | |
429 | ||
430 | my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, | |
431 | $atime, $mtime, $ctime, $blksize, $blocks) = stat("/dev/$disk"); | |
432 | ||
433 | my $major = $rdev >> 8; | |
434 | my $minor = ($rdev & 0xFF) >>6; | |
435 | ||
436 | open STAT, "/proc/stat"; | |
437 | my @diskstat = <STAT>; | |
438 | close (STAT); | |
439 | foreach my $line (@diskstat) | |
440 | { | |
441 | chomp ($line); | |
442 | my @temp = split(/\:\ /,$line); | |
443 | if ($temp[1]) { | |
444 | my @devicestat = split(/\ /,$temp[1]); | |
445 | foreach my $stats (@devicestat) | |
446 | { | |
447 | chomp ($stats); | |
448 | my @fields = split(/\((\d+),(\d+)\):\((\d+),(\d+),(\d+),(\d+),(\d+)/,$stats); | |
449 | if ($major eq $fields[1] and $minor eq $fields[2]) | |
450 | { | |
451 | $readwritereq = $fields[3]; | |
452 | $readreq = $fields[4]; | |
453 | $readsect = $fields[5]; | |
454 | $writereq = $fields[6]; | |
455 | $writesect = $fields[7]; | |
456 | } | |
457 | } | |
458 | } | |
459 | } | |
460 | ||
461 | my $sleeping=0; | |
462 | my $lastsleepstate=0; | |
463 | ||
464 | if ( -e "/tmp/hddshutdown-$disk" ) { | |
465 | open STAT,"/tmp/hddshutdown-$disk"; | |
466 | $lastsleepstate = <STAT>; | |
467 | close (STAT); | |
468 | if ($lastsleepstate==$readwritereq) { | |
469 | $sleeping=1; | |
470 | } | |
471 | } | |
472 | ||
473 | if ($readsect && $writesect) { | |
474 | RRDs::update ("$rrdlog/disk-$disk.rrd", | |
475 | "-t", "readsect:writesect:sleeping", | |
476 | "N:$readsect:$writesect:$sleeping"); | |
477 | $ERROR = RRDs::error; | |
478 | print "Error in RRD::update for disk-$disk: $ERROR\n" if $ERROR; | |
479 | } else { | |
480 | print "Error in RRD::update for disk-$disk: no data available\n"; | |
481 | } | |
482 | } | |
483 | ||
484 | sub updateifgraph { | |
485 | my $interface = $_[0]; | |
486 | my $period = $_[1]; | |
487 | ||
488 | RRDs::graph ("$graphs/$interface-$period.png", | |
489 | "--start", "-1$period", "-aPNG", "-i", "-z", | |
490 | "--alt-y-grid", "-w 600", "-h 100", | |
491 | "--color", "SHADEA#EAE9EE", | |
492 | "--color", "SHADEB#EAE9EE", | |
493 | "--color", "BACK#FFFFFF", | |
494 | "-t $tr{'traffic on'} $interface ($tr{'graph per'} $tr{$period})", | |
495 | "-v$tr{'bytes per second'}", | |
496 | "DEF:incoming=$rrdlog/$interface.rrd:incoming:AVERAGE", | |
497 | "DEF:outgoing=$rrdlog/$interface.rrd:outgoing:AVERAGE", | |
498 | "AREA:incoming#00FF00:$tr{'incoming traffic in bytes per second'}\\j", | |
499 | "LINE1:outgoing#0000FF:$tr{'outgoing traffic in bytes per second'}\\j", | |
500 | "GPRINT:incoming:MAX:$tr{'maximal'} $tr{'in'}\\:%8.3lf %sBps", | |
501 | "GPRINT:incoming:AVERAGE:$tr{'average'} $tr{'in'}\\:%8.3lf %sBps", | |
502 | "GPRINT:incoming:LAST:$tr{'current'} $tr{'in'}\\:%8.3lf %sBps\\j", | |
503 | "GPRINT:outgoing:MAX:$tr{'maximal'} $tr{'out'}\\:%8.3lf %sBps", | |
504 | "GPRINT:outgoing:AVERAGE:$tr{'average'} $tr{'out'}\\:%8.3lf %sBps", | |
505 | "GPRINT:outgoing:LAST:$tr{'current'} $tr{'out'}\\:%8.3lf %sBps\\j"); | |
506 | $ERROR = RRDs::error; | |
507 | print "Error in RRD::graph for $interface: $ERROR\n" if $ERROR; | |
508 | } | |
509 | ||
510 | sub updateifdata { | |
511 | my $interface = $_[0]; | |
512 | ||
513 | if ( ! -e "$rrdlog/$interface.rrd") { | |
514 | RRDs::create ("$rrdlog/$interface.rrd", "--step=300", | |
515 | "DS:incoming:ABSOLUTE:600:0:12500000", | |
516 | "DS:outgoing:ABSOLUTE:600:0:12500000", | |
517 | "RRA:AVERAGE:0.5:1:576", | |
518 | "RRA:AVERAGE:0.5:6:672", | |
519 | "RRA:AVERAGE:0.5:24:732", | |
520 | "RRA:AVERAGE:0.5:144:1460"); | |
521 | $ERROR = RRDs::error; | |
522 | print "Error in RRD::create for $interface: $ERROR\n" if $ERROR; | |
523 | } | |
524 | ||
525 | my $traffic = gettraffic ($interface); | |
526 | RRDs::update ("$rrdlog/$interface.rrd", | |
527 | "-t", "incoming:outgoing", | |
528 | "N:$traffic"); | |
529 | $ERROR = RRDs::error; | |
530 | print "Error in RRD::update for $interface: $ERROR\n" if $ERROR; | |
531 | } | |
532 | ||
533 | sub updatefwhitsgraph { | |
534 | my $interval = $_[0]; | |
535 | ||
536 | RRDs::graph ("$graphs/firewallhits-$interval-area.png", | |
537 | "--start", "-1$interval", "-aPNG", "-i", "-z", | |
538 | "--alt-y-grid", "-w 600", "-h 200", | |
539 | "--color", "SHADEA#EAE9EE", | |
540 | "--color", "SHADEB#EAE9EE", | |
541 | "--color", "BACK#FFFFFF", | |
542 | "-t firewall hits over the last $interval", | |
543 | "DEF:amount=$rrdlog/firewallhits.rrd:amount:AVERAGE", | |
544 | "AREA:amount#6464FF:firewallhits", | |
545 | "GPRINT:amount:MAX: $tr{'maximal'}\\: %2.2lf %S", | |
546 | "GPRINT:amount:AVERAGE: $tr{'average'}\\: %2.2lf %S", | |
547 | "GPRINT:amount:LAST: $tr{'current'}\\: %2.2lf %Shits/5 min\\n", | |
548 | "DEF:portamount=$rrdlog/firewallhits.rrd:portamount:AVERAGE", | |
549 | "AREA:portamount#FF6464:portscans", | |
550 | "GPRINT:portamount:MAX: $tr{'maximal'}\\: %2.2lf %S", | |
551 | "GPRINT:portamount:AVERAGE: $tr{'average'}\\: %2.2lf %S", | |
552 | "GPRINT:portamount:LAST: $tr{'current'}\\: %2.2lf %Shits/5 min"); | |
553 | $ERROR = RRDs::error; | |
554 | print "Error in RRD::graph for Firewallhits: $ERROR\n" if $ERROR; | |
555 | ||
556 | RRDs::graph ("$graphs/firewallhits-$interval-line.png", | |
557 | "--start", "-1$interval", "-aPNG", "-i", "-z", | |
558 | "--alt-y-grid", "-w 600", "-h 200", | |
559 | "--color", "SHADEA#EAE9EE", | |
560 | "--color", "SHADEB#EAE9EE", | |
561 | "--color", "BACK#FFFFFF", | |
562 | "-t firewall hits over the last $interval", | |
563 | "DEF:amount=$rrdlog/firewallhits.rrd:amount:AVERAGE", | |
564 | "LINE2:amount#6464FF:firewallhits", | |
565 | "GPRINT:amount:MAX: $tr{'maximal'}\\: %2.2lf %S", | |
566 | "GPRINT:amount:AVERAGE: $tr{'average'}\\: %2.2lf %S", | |
567 | "GPRINT:amount:LAST: $tr{'current'}\\: %2.2lf %Shits/5 min\\n", | |
568 | "DEF:portamount=$rrdlog/firewallhits.rrd:portamount:AVERAGE", | |
569 | "LINE2:portamount#FF6464:portscans", | |
570 | "GPRINT:portamount:MAX: $tr{'maximal'}\\: %2.2lf %S", | |
571 | "GPRINT:portamount:AVERAGE: $tr{'average'}\\: %2.2lf %S", | |
572 | "GPRINT:portamount:LAST: $tr{'current'}\\: %2.2lf %Shits/5 min"); | |
573 | $ERROR = RRDs::error; | |
574 | print "Error in RRD::graph for Firewallhits: $ERROR\n" if $ERROR; | |
575 | } | |
576 | ||
577 | sub updatefwhitsdata { | |
578 | my $portamount=0; | |
579 | my $alertaktuell=0; | |
580 | my $aktuell=0; | |
581 | my $portaktuell=0; | |
582 | my $skip=0; | |
583 | ||
584 | if (! -e "$rrdlog/firewallhits.rrd") | |
585 | { | |
586 | RRDs::create ("$rrdlog/firewallhits.rrd", "--step=300", | |
587 | "DS:amount:GAUGE:600:0:U", | |
588 | "DS:portamount:GAUGE:600:0:U", | |
589 | "RRA:AVERAGE:0.5:1:576", | |
590 | "RRA:AVERAGE:0.5:6:672", | |
591 | "RRA:AVERAGE:0.5:24:732", | |
592 | "RRA:AVERAGE:0.5:144:1460"); | |
593 | $ERROR = RRDs::error; | |
594 | print "Error in RRD::create for cpu: $ERROR\n" if $ERROR; | |
595 | } | |
596 | ||
597 | system("logtailfwhits /var/log/messages /var/log/fwhits.messages.offset >/tmp/messages.fwhits"); | |
598 | if (!(open (FILE,'/tmp/messages.fwhits'))) { | |
599 | $skip=1; | |
600 | } | |
601 | $aktuell = 0; | |
602 | if (!$skip) { | |
603 | while (<FILE>) { | |
604 | if (/kernel:.*(IN=.*)$/) { | |
605 | $aktuell++; | |
606 | } | |
607 | } | |
608 | close (FILE); | |
609 | } | |
610 | ||
611 | system("logtailfwhits /var/log/snort/alert /var/log/snort/fwhits.alert.offset >/tmp/snort.fwhits"); | |
612 | if (!(open (FILE,'/tmp/snort.fwhits'))) { | |
613 | $skip=1; | |
614 | } | |
615 | $alertaktuell = 0; | |
616 | if (!$skip) { | |
617 | while (<FILE>) { | |
618 | if (/scan.*$/) { | |
619 | $alertaktuell++; | |
620 | } | |
621 | } | |
622 | close (FILE); | |
623 | } | |
624 | ||
625 | if (!(open (FILE,'/tmp/messages.fwhits'))) { | |
626 | $skip=1; | |
627 | } | |
628 | $portaktuell = 0; | |
629 | if (!$skip) { | |
630 | while (<FILE>) { | |
631 | if (/kernel:.*(Scan.*)$/) { | |
632 | $portaktuell++; | |
633 | } | |
634 | } | |
635 | close (FILE); | |
636 | } | |
637 | ||
638 | system("rm /tmp/messages.fwhits"); | |
639 | system("rm /tmp/snort.fwhits"); | |
640 | ||
641 | $portamount = $portaktuell + $alertaktuell; | |
642 | chomp($portamount); | |
643 | RRDs::update ("$rrdlog/firewallhits.rrd", | |
644 | "N:$aktuell:$portamount"); | |
645 | $ERROR = RRDs::error; | |
646 | print "Error in RRD::update for Firewallhits: $ERROR\n" if $ERROR; | |
647 | } | |
648 | ||
649 | # Creates and updates a link quality database | |
650 | # ------------------------------------------- | |
651 | sub updatelq { | |
652 | if ( ! -e "$rrdlog/lq.rrd") { | |
653 | RRDs::create ("$rrdlog/lq.rrd", "--step=300", | |
654 | "DS:loss:GAUGE:600:0:100", | |
655 | "DS:roundtrip:GAUGE:600:0:10000", | |
656 | "RRA:AVERAGE:0.5:1:576", | |
657 | "RRA:AVERAGE:0.5:6:672", | |
658 | "RRA:AVERAGE:0.5:24:732", | |
659 | "RRA:AVERAGE:0.5:144:1460"); | |
660 | $ERROR = RRDs::error; | |
661 | print "Error in RRD::create for link: $ERROR\n" if $ERROR; | |
662 | } | |
663 | my $packetloss=0; | |
664 | my $roundtrip=0; | |
665 | my $test=0; | |
666 | # LQ_GATEWAY is the ip of your isp's public ip facing you | |
667 | my $LQ_GATEWAY='www.heise.de'; | |
668 | my $NUMPINGS=10; | |
669 | my $pingoutput = `ping -c $NUMPINGS -q $LQ_GATEWAY`; | |
670 | chomp; | |
671 | my @temp = split (/\/|\%|\s/, $pingoutput); | |
672 | $packetloss = $temp[17]; | |
673 | $roundtrip = $temp[28]; | |
674 | RRDs::update ("$rrdlog/lq.rrd", "N:$packetloss:$roundtrip"); | |
675 | $ERROR = RRDs::error; | |
676 | print "Error in RRD::update for line quality: $ERROR\n" if $ERROR; | |
677 | } | |
678 | ||
679 | sub updatelqgraph { | |
680 | my $period = $_[0]; | |
681 | RRDs::graph ("$graphs/lq-$period.png", | |
682 | "--start", "-1$period", "-aPNG", "-i", "-z", | |
683 | "--alt-y-grid", "-w 600", "-h 100", "-l 0", "-r", | |
684 | "-t $tr{'linkq'} ($tr{'graph per'} $tr{$period})", | |
685 | "--lazy", | |
686 | "--color", "SHADEA#EAE9EE", | |
687 | "--color", "SHADEB#EAE9EE", | |
688 | "--color", "BACK#FFFFFF", | |
689 | "-v ms / pkts (% x10)", | |
690 | "DEF:roundtrip=$rrdlog/lq.rrd:roundtrip:AVERAGE", | |
691 | "DEF:loss=$rrdlog/lq.rrd:loss:AVERAGE", | |
692 | "CDEF:roundavg=roundtrip,PREV(roundtrip),+,2,/", | |
693 | "CDEF:loss10=loss,10,*", | |
694 | "CDEF:r0=roundtrip,30,MIN", | |
695 | "CDEF:r1=roundtrip,70,MIN", | |
696 | "CDEF:r2=roundtrip,150,MIN", | |
697 | "CDEF:r3=roundtrip,300,MIN", | |
698 | "AREA:roundtrip#EE7000:>300 ms", | |
699 | "AREA:r3#D88E1B:150-300 ms", | |
700 | "AREA:r2#B9B63F:70-150 ms", | |
701 | "AREA:r1#99E064:30-70 ms", | |
702 | "AREA:r0#80FF80:<30 ms", | |
703 | "AREA:loss10#800000:Packet loss (x10)", | |
704 | "LINE1:roundtrip#707070:", | |
705 | "GPRINT:roundtrip:MAX:$tr{'maximal'}\\:%3.2lf ms", | |
706 | "GPRINT:roundtrip:AVERAGE:$tr{'average'}\\:%3.2lf ms", | |
707 | "GPRINT:roundtrip:LAST:$tr{'current'}\\:%3.2lf ms\\j", | |
708 | "GPRINT:loss:MAX:$tr{'maximal'} Loss\\:%3.2lf%%", | |
709 | "GPRINT:loss:AVERAGE:$tr{'average'} Loss\\:%3.2lf%%", | |
710 | "GPRINT:loss:LAST:$tr{'current'} Loss\\:%3.2lf%%\\j" | |
711 | ); | |
712 | $ERROR = RRDs::error; | |
713 | print "Error in RRD::graph for Link Quality: $ERROR\n" if $ERROR; | |
714 | } | |
715 | ||
716 | sub updatehdddata | |
717 | { | |
718 | if ( ! -e "$path_rrd/hddtemp.rrd") | |
719 | { | |
720 | # database did not exist -> create | |
721 | RRDs::create ("$path_rrd/hddtemp.rrd", "--step=300", | |
722 | "DS:temperature:GAUGE:600:0:100", | |
723 | "RRA:AVERAGE:0.5:1:576", | |
724 | "RRA:AVERAGE:0.5:6:672", | |
725 | "RRA:AVERAGE:0.5:24:732", | |
726 | "RRA:AVERAGE:0.5:144:1460"); | |
727 | $ERROR = RRDs::error; | |
728 | print "Error in RRD::create for hdd: $ERROR\n" if $ERROR; | |
729 | } | |
730 | ||
731 | $temp = 0; | |
732 | my $hdd_output = ''; | |
733 | my $smart_output = ''; | |
734 | ||
735 | if ( -e "$path_smartctl" ) | |
736 | { | |
737 | system("$path_smartctl -iHA $hdd_device > /var/log/hddgraph_smartctl_out"); | |
738 | } | |
739 | ||
740 | if ( -e "$path_hddtemp" ) | |
741 | { | |
742 | $hdd_output = `$path_hddtemp -qn $hdd_device`; | |
743 | ||
744 | # I know 4 response possible responses: | |
745 | # | |
746 | # /dev/harddisk: harddisk type: S.M.A.R.T. not available | |
747 | # /dev/harddisk: harddisk type: no sensor | |
748 |