Handle hostnames with upper-case letters
[webmin.git] / bandwidth / rotate.pl
1 #!/usr/local/bin/perl
2 # Parse the firewall log and rotate it
3
4 $no_acl_check++;
5 require './bandwidth-lib.pl';
6 use Time::Local;
7
8 # Detect firewall system if needed
9 if (!$config{'firewall_system'}) {
10         $sys = &detect_firewall_system();
11         if ($sys) {
12                 $config{'firewall_system'} = $sys;
13                 &save_module_config();
14                 }
15         else {
16                 die "Failed to detect firewall system!";
17                 }
18         }
19
20 # See if this process is already running
21 if ($pid = &check_pid_file($pid_file)) {
22         print STDERR "rotate.pl process $pid is already running\n";
23         exit;
24         }
25 open(PID, ">$pid_file");
26 print PID $$,"\n";
27 close(PID);
28
29 $time_now = time();
30 @time_now = localtime($time_now);
31 @hours = ( );
32
33 # Scan the entries in the log file
34 &pre_process();
35 open(LOG, $bandwidth_log);
36 while(<LOG>) {
37         if (&process_line($_, \@hours, $time_now)) {
38                 # Found a valid line
39                 $lastline = $_;
40                 }
41         elsif (/last\s+message\s+repeated\s+(\d+)/) {
42                 # re-process the last line N-1 times
43                 for($i=0; $i<$1-1; $i++) {
44                         &process_line($lastline, \@hours, $time_now);
45                         }
46                 }
47         else {
48                 #print "skipping $_";
49                 }
50         }
51 close(LOG);
52
53 # Save all hours
54 foreach $hour (@hours) {
55         &save_hour($hour);
56         }
57
58 # Truncate the file and notify syslog
59 open(LOG, ">$bandwidth_log");
60 close(LOG);
61 &foreign_call($syslog_module, "signal_syslog");
62
63 # Remove PID file
64 unlink($pid_file);
65