]>
Commit | Line | Data |
---|---|---|
d81292e0 CS |
1 | #!/usr/bin/perl |
2 | # | |
3 | # This code is distributed under the terms of the GPL | |
4 | # | |
5 | # (c) Achim Weber 2006 | |
6 | # | |
7 | # $Id: monitorTraffic.pl,v 1.14 2006/12/15 14:43:57 dotzball Exp $ | |
8 | # | |
9 | # | |
10 | ||
11 | use strict; | |
12 | ||
13 | # enable only the following on debugging purpose | |
14 | use warnings; | |
15 | ||
fd5c3064 | 16 | require '/var/ipcop/general-functions.pl'; |
d81292e0 CS |
17 | require "${General::swroot}/lang.pl"; |
18 | require "${General::swroot}/net-traffic/net-traffic-lib.pl"; | |
19 | require "${General::swroot}/net-traffic/net-traffic-admin.pl"; | |
20 | ||
21 | my @dummy = (${Traffic::red_in},${Traffic::red_out}); | |
22 | undef(@dummy); | |
23 | ||
24 | # Debug level: | |
25 | # 0 - send email (if enabled), no print | |
26 | # 1 - send email (if enabled), print | |
27 | # 2 - only print | |
28 | my $debugLevel = 0; | |
29 | # Debug | |
30 | ||
31 | my %log = (); | |
32 | $log{'CALC_VOLUME_TOTAL'} = 0; | |
33 | $log{'CALC_VOLUME_IN'} = 0; | |
34 | $log{'CALC_VOLUME_OUT'} = 0; | |
35 | $log{'CALC_WEEK_TOTAL'} = 0; | |
36 | $log{'CALC_WEEK_IN'} = 0; | |
37 | $log{'CALC_WEEK_OUT'} = 0; | |
38 | $log{'CALC_LAST_RUN'} = 0; | |
39 | $log{'CALC_PERCENT'} = 0; | |
40 | $log{'WARNMAIL_SEND'} = 'no'; | |
41 | ||
42 | # current time == endtime | |
43 | my $currentTime = time; | |
44 | ||
45 | # on force we don't load the log data | |
46 | unless(defined($ARGV[0]) && $ARGV[0] eq '--force') { | |
47 | &General::readhash($NETTRAFF::logfile, \%log); | |
48 | } | |
49 | ||
50 | ||
51 | # Only send email? | |
52 | if(defined($ARGV[0]) && ($ARGV[0] eq '--testEmail' || $ARGV[0] eq '--warnEmail')) | |
53 | { | |
54 | print "Send testmail\n" if($debugLevel > 0); | |
55 | # send (test|warn) email | |
56 | my $return = &sendEmail($ARGV[0]); | |
57 | print "$return\n"; | |
58 | exit 0; | |
59 | } | |
60 | ||
61 | ||
62 | # should we recalculate? | |
63 | # calc seconds for one interval | |
64 | my $intervalTime = $NETTRAFF::settings{'CALC_INTERVAL'} * 60; | |
65 | # next time, we have to calculate | |
66 | my $nextRunTime = $log{'CALC_LAST_RUN'} + $intervalTime; | |
67 | ||
68 | if ($debugLevel > 0) | |
69 | { | |
70 | my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($log{'CALC_LAST_RUN'}); | |
71 | my $lastRun = sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min); | |
72 | print "last run: $lastRun\n"; | |
73 | ||
74 | ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($nextRunTime); | |
75 | my $nextRun = sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min); | |
76 | print "next run: $nextRun\n"; | |
77 | ||
78 | ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($currentTime); | |
79 | my $current = sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min); | |
80 | print "current time: $current\n"; | |
81 | } | |
82 | ||
83 | # use a little time buffer in case the last run started some seconds earlier | |
84 | if($currentTime < ($nextRunTime - 60) ) | |
85 | { | |
86 | # nothing to do | |
87 | if ($debugLevel > 0) | |
88 | { | |
89 | my $infoMsg = "Net-Traffic: nothing to do, do next calculation later."; | |
90 | print "$infoMsg\n"; | |
91 | &General::log($infoMsg); | |
92 | } | |
93 | exit 0; | |
94 | } | |
95 | elsif($debugLevel > 0) | |
96 | { | |
97 | my $infoMsg = "Net-Traffic: Calc traffic now."; | |
98 | print "$infoMsg\n"; | |
99 | &General::log($infoMsg); | |
100 | } | |
101 | ||
102 | #### | |
103 | # Calculate Traffic | |
104 | # | |
105 | ||
106 | $log{'CALC_LAST_RUN'} = $currentTime; | |
107 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($currentTime); | |
108 | ||
109 | ||
110 | ##################### | |
111 | # this month traffic | |
112 | ### | |
113 | my $startDay = '1'; | |
114 | my $startMonth = $mon + 1; | |
115 | my $startYear = $year + 1900; | |
116 | ||
117 | if($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on') | |
118 | { | |
119 | $startDay = $NETTRAFF::settings{'STARTDAY'}; | |
120 | } | |
121 | ||
122 | # this periode started last month | |
123 | if ($mday < $startDay) | |
124 | { | |
125 | # when current month is january we start in last year december | |
126 | if ($startMonth == 1) { | |
127 | $startYear--; | |
128 | $startMonth = 12; | |
129 | } | |
130 | else | |
131 | { | |
132 | $startMonth--; | |
133 | } | |
134 | } | |
135 | $startMonth = $startMonth < 10 ? $startMonth = "0".$startMonth : $startMonth; | |
136 | $startDay = $startDay < 10 ? $startDay = "0".$startDay : $startDay; | |
137 | ||
138 | my $start = "$startYear$startMonth$startDay"; | |
139 | ||
140 | my %month = &getTrafficData($start, $currentTime); | |
141 | ||
142 | $log{'CALC_VOLUME_TOTAL'} = $month{'TOTAL'}; | |
143 | $log{'CALC_VOLUME_IN'} = $month{'IN'}; | |
144 | $log{'CALC_VOLUME_OUT'} = $month{'OUT'}; | |
145 | ##################### | |
146 | ||
147 | ||
148 | ##################### | |
149 | # this week traffic | |
150 | ### | |
151 | $startMonth = $mon; | |
152 | $startYear = $year + 1900; | |
153 | $startDay = $mday-($wday >0 ? $wday-1 : 6); | |
154 | # borrowed from ipacsum | |
155 | my @mofg = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); | |
156 | ||
157 | if ($startDay < 1) { | |
158 | $startMonth--; | |
159 | if ($startMonth < 0) { | |
160 | $startMonth += 12; | |
161 | $startYear--; | |
162 | } | |
163 | $startDay += $mofg[$startMonth]; | |
164 | } | |
165 | ||
166 | # $mon starts at 0 but we have to start at 1 | |
167 | $startMonth++; | |
168 | ||
169 | $startMonth = $startMonth < 10 ? $startMonth = "0".$startMonth : $startMonth; | |
170 | $startDay = $startDay < 10 ? $startDay = "0".$startDay : $startDay; | |
171 | ||
172 | $start = "$startYear$startMonth$startDay"; | |
173 | ||
174 | my %week = &getTrafficData($start, $currentTime); | |
175 | $log{'CALC_WEEK_TOTAL'} = $week{'TOTAL'}; | |
176 | $log{'CALC_WEEK_IN'} = $week{'IN'}; | |
177 | $log{'CALC_WEEK_OUT'} = $week{'OUT'}; | |
178 | #################### | |
179 | ||
180 | ||
181 | ||
182 | my $infoMsg = "Reached: $log{'CALC_VOLUME_TOTAL'} MB\n"; | |
183 | $infoMsg .= "start: $start"; | |
184 | print "$infoMsg\n" if ($debugLevel > 0); | |
185 | ||
186 | ||
187 | # monthly traffic volume? | |
188 | if ($NETTRAFF::settings{'MONTHLY_VOLUME_ON'} eq 'on') | |
189 | { | |
190 | $log{'CALC_PERCENT'} = sprintf("%d", ($log{'CALC_VOLUME_TOTAL'} / $NETTRAFF::settings{'MONTHLY_VOLUME'} * 100)); | |
191 | ||
192 | my $infoMsg = "Used (\%): $log{'CALC_PERCENT'} \% - Max.: $NETTRAFF::settings{'MONTHLY_VOLUME'} MB"; | |
193 | print "$infoMsg\n" if ($debugLevel > 0); | |
194 | ||
195 | ||
196 | if($NETTRAFF::settings{'WARN_ON'} eq 'on' | |
197 | && $log{'CALC_PERCENT'} >= $NETTRAFF::settings{'WARN'}) | |
198 | { | |
199 | # warnlevel is reached | |
200 | if ($debugLevel > 0) | |
201 | { | |
202 | my $warnMsg = "Net-Traffic warning: $infoMsg"; | |
203 | print "$warnMsg\n"; | |
204 | &General::log($warnMsg); | |
205 | } | |
206 | ||
207 | if($debugLevel < 2) | |
208 | { | |
209 | if($NETTRAFF::settings{'SEND_EMAIL_ON'} eq 'on' | |
210 | && $log{'WARNMAIL_SEND'} ne 'yes') | |
211 | { | |
212 | # send warn email | |
213 | my $return = &sendEmail('--warnEmail'); | |
214 | ||
215 | if($return =~ /Email was sent successfully!/) | |
216 | { | |
217 | $log{'WARNMAIL_SEND'} = 'yes'; | |
218 | } | |
219 | else { | |
220 | $log{'WARNMAIL_SEND'} = 'no'; | |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | } | |
226 | else | |
227 | { | |
228 | # warnlevel not reached, reset warnmail send | |
229 | $log{'WARNMAIL_SEND'} = 'no'; | |
230 | } | |
231 | } | |
232 | ||
233 | &General::writehash($NETTRAFF::logfile, \%log); | |
234 | ||
235 | exit 0; | |
236 | ||
237 | ||
238 | sub getTrafficData | |
239 | { | |
240 | my $p_start = shift; | |
241 | my $p_currentTime = shift; | |
242 | ||
243 | if($debugLevel > 0) | |
244 | { | |
245 | print "----------------------\n"; | |
246 | print "start: $p_start\n"; | |
247 | print "current time: $p_currentTime\n"; | |
248 | } | |
249 | ||
250 | #my $displayMode = "exactTimeframe"; | |
251 | my $displayMode = "exactEnd"; | |
252 | ||
253 | my %allDaysBytes = (); | |
254 | my @allDays = &Traffic::calcTraffic(\%allDaysBytes, $p_start, $p_currentTime, $displayMode); | |
255 | ||
256 | my %traff = (); | |
257 | $traff{'IN'} = 0; | |
258 | $traff{'OUT'} = 0; | |
259 | $traff{'TOTAL'} = 0; | |
260 | ||
261 | foreach my $day (@allDays) | |
262 | { | |
263 | if($debugLevel > 0) | |
264 | { | |
265 | print "day: $day\n"; | |
266 | print "in: $allDaysBytes{$day}{${Traffic::red_in}}\n"; | |
267 | print "out: $allDaysBytes{$day}{${Traffic::red_out}}\n"; | |
268 | } | |
269 | ||
270 | $traff{'IN'} += $allDaysBytes{$day}{${Traffic::red_in}}; | |
271 | $traff{'OUT'} += $allDaysBytes{$day}{${Traffic::red_out}}; | |
272 | } | |
273 | ||
274 | $traff{'TOTAL'} = $traff{'IN'} + $traff{'OUT'}; | |
275 | ||
276 | # formating | |
277 | $traff{'TOTAL'} = sprintf("%.2f", ($traff{'TOTAL'}/1048576)); | |
278 | $traff{'IN'} = sprintf("%.2f", ($traff{'IN'}/1048576)); | |
279 | $traff{'OUT'} = sprintf("%.2f", ($traff{'OUT'}/1048576)); | |
280 | ||
281 | if($debugLevel > 0) | |
282 | { | |
283 | print "IN: $traff{'IN'}\n"; | |
284 | print "OUT: $traff{'OUT'}\n"; | |
285 | print "TOTAL: $traff{'TOTAL'}\n"; | |
286 | print "----------------------\n"; | |
287 | } | |
288 | ||
289 | return %traff; | |
290 | } | |
291 | ||
292 | ||
293 | sub sendEmail | |
294 | { | |
295 | my $mailtyp = shift; | |
296 | ||
297 | my $template = ""; | |
298 | ||
fd5c3064 CS |
299 | my %ipcopSettings = (); |
300 | &General::readhash("${General::swroot}/main/settings", \%ipcopSettings); | |
301 | my $host = "$ipcopSettings{'HOSTNAME'}.$ipcopSettings{'DOMAINNAME'}"; | |
d81292e0 CS |
302 | |
303 | my $subject = "[Net-Traffic] $host: "; | |
304 | ||
305 | if($mailtyp eq '--warnEmail') | |
306 | { | |
307 | $subject .= $Lang::tr{'subject warn'}; | |
308 | $template = "warn"; | |
309 | } | |
310 | else | |
311 | { | |
312 | $subject .= $Lang::tr{'subject test'}; | |
313 | $template = "test"; | |
314 | } | |
315 | ||
316 | if(-e "${General::swroot}/net-traffic/templates/$template.${Lang::language}") | |
317 | { | |
318 | $template .= ".${Lang::language}"; | |
319 | } | |
320 | else | |
321 | { | |
322 | $template .= ".en"; | |
323 | } | |
324 | ||
325 | # read template | |
326 | open(FILE, "${General::swroot}/net-traffic/templates/$template"); | |
327 | my @temp = <FILE>; | |
328 | close(FILE); | |
329 | ||
330 | my $date_current = &NETTRAFF::getFormatedDate($currentTime); | |
331 | my $date_lastrun = &NETTRAFF::getFormatedDate($log{'CALC_LAST_RUN'}); | |
332 | ||
333 | my $message = ""; | |
334 | foreach my $line (@temp) | |
335 | { | |
336 | chomp($line); | |
337 | $line =~ s/__HOSTNAME__/$host/; | |
338 | $line =~ s/__CALC_VOLUME_TOTAL__/$log{'CALC_VOLUME_TOTAL'}/; | |
339 | $line =~ s/__CALC_PERCENT__/$log{'CALC_PERCENT'}/; | |
340 | $line =~ s/__MONTHLY_VOLUME__/$NETTRAFF::settings{'MONTHLY_VOLUME'}/; | |
341 | $line =~ s/__STARTDAY__/$NETTRAFF::settings{'STARTDAY'}/; | |
342 | $line =~ s/__CURRENT_DATE__/$date_current/; | |
343 | $line =~ s/__LAST_RUN__/$date_lastrun/; | |
344 | ||
345 | $message .= "$line\n"; | |
346 | } | |
347 | ||
348 | ||
349 | my $cmd = "/usr/local/bin/sendEmail_nettraffic -f $NETTRAFF::settings{'EMAIL_FROM'} "; | |
350 | $cmd .= " -t $NETTRAFF::settings{'EMAIL_TO'} "; | |
351 | $cmd .= " -u \"$subject\" "; | |
352 | $cmd .= " -m \"$message\" "; | |
353 | $cmd .= " -s $NETTRAFF::settings{'EMAIL_SERVER'} "; | |
354 | ||
355 | if($NETTRAFF::settings{'EMAIL_USR'} ne '') { | |
356 | $cmd .= " -xu $NETTRAFF::settings{'EMAIL_USR'} "; | |
357 | } | |
358 | if($NETTRAFF::settings{'EMAIL_PW'} ne '') { | |
359 | $cmd .= " -xp $NETTRAFF::settings{'EMAIL_PW'} "; | |
360 | } | |
361 | ||
362 | my $return = `$cmd`; | |
363 | ||
364 | return $return; | |
365 | } | |
366 | ||
367 |