Started core37.
[people/pmueller/ipfire-2.x.git] / config / calamaris / mkreport
CommitLineData
069ae085
MT
1#!/usr/bin/perl
2#
3# This code is distributed under the terms of the GPL
4#
5# (c) 2005,2006 marco.s
6#
7# $Id: mkreport.pl,v 2.0 2006/03/12 00:00:00 marco.s Exp $
8#
9
10use strict;
11
12use Time::Local;
13
14my $swroot = "/var/ipfire";
15my $apdir = "$swroot/proxy/calamaris";
16my $squidlogdir = "/var/log/squid";
17my $calamlogdir = "/var/log/calamaris";
18my $reportdir = "$apdir/reports";
19
20unless (-e $reportdir) { mkdir($reportdir) }
21
22my $unique=time;
23
24my $commandline='';
25my $skip_gzlogs=0;
26
27my @now = localtime(time);
28my $year = $now[5]+1900;
29
30if (@ARGV[0] eq 'nogz')
31{
32 $skip_gzlogs=1;
33 shift(@ARGV);
34}
35
36if (@ARGV < 6) { die "ERROR: Too few arguments\n\n"; }
37
38my $day_begin=@ARGV[0];
39my $month_begin=@ARGV[1];
40my $year_begin=@ARGV[2];
41my $day_end=@ARGV[3];
42my $month_end=@ARGV[4];
43my $year_end=@ARGV[5];
44
45my $i=6;
46
47while ($i < @ARGV) { $commandline.=" @ARGV[$i++]"; }
48
49$commandline.=" $calamlogdir/squid-$unique.log >> $reportdir/calamaris-$unique.log";
50
51if (&processlogfiles($day_begin,$month_begin,$year_begin,$day_end,$month_end,$year_end) > 0)
52{
53 system("$apdir/bin/calamaris $commandline");
54 system("chown nobody.nobody $reportdir/calamaris-$unique.log");
55}
56
57if (-e "$calamlogdir/squid-$unique.log") { unlink("$calamlogdir/squid-$unique.log"); }
58
59# -------------------------------------------------------------------
60
61sub processlogfiles
62{
63 my $filestr='';
64
65 my $day_from = $_[0];
66 my $mon_from = $_[1];
67 my $year_from = $_[2];
68 my $day_to = $_[3];
69 my $mon_to = $_[4];
70 my $year_to = $_[5];
71
72 if (($mon_from =~ /(3|5|8|10)/) && ($day_from > 30)) { $day_from=30 }
73 if (($mon_to =~ /(3|5|8|10)/) && ($day_to > 30)) { $day_to=30 }
74 if (($mon_from == 1) && ($day_from > 28)) { if ($year_from%4==0) { $day_from=29 } else { $day_from=28 } }
75 if (($mon_to == 1) && ($day_to > 28)) { if ($year_to%4==0) { $day_to=29 } else { $day_to=28 } }
76
77 my $date_now = timelocal(0,0,0,$now[3],$now[4],$year);
78 my $date_from = timelocal(0,0,0,$day_from,$mon_from,$year_from);
79 my $date_to = timelocal(0,0,0,$day_to,$mon_to,$year_to);
80
81 # if (($date_from > $date_now) || ($date_from > $date_to)) { $year_from-- }
82
83 $day_from = $_[0];
84 if (($mon_from =~ /(3|5|8|10)/) && ($day_from > 30)) { $day_from=30 }
85 if (($mon_from == 1) && ($day_from > 28)) { if ($year_from%4==0) { $day_from=29 } else { $day_from=28 } }
86
87 my $date_from = timelocal(0,0,0,$day_from,$mon_from,$year_from);
88 my $date_to = timelocal(59,59,23,$day_to,$mon_to,$year_to);
89
90 open (TMPLOG,">>$calamlogdir/squid-$unique.log") or die "ERROR: Cannot write to $calamlogdir/squid-$unique.log\n";
91
92 unless ($skip_gzlogs) {
93 foreach $filestr (<$squidlogdir/*.gz>)
94 {
95 if ($filestr =~ /access\.log/) {
96 open (LOG,"gzip -dc $filestr |");
97 foreach (<LOG>) {
98 if (substr($_,0,10) >= $date_from) { if (substr($_,0,10) <= $date_to) { print TMPLOG "$_"; } }
99 }
100 close(LOG);
101 }
102 }
103 }
104
105 foreach $filestr (<$squidlogdir/*.log>)
106 {
107 if ($filestr =~ /access\.log/) {
108 open (LOG,$filestr);
109 foreach (<LOG>) {
110 if (substr($_,0,10) >= $date_from) { if (substr($_,0,10) <= $date_to) { print TMPLOG "$_"; } }
111 }
112 close(LOG);
113 }
114 }
115
116 close (TMPLOG);
117
118 return (-s "$calamlogdir/squid-$unique.log");
119
120}
121
122# -------------------------------------------------------------------