*** empty log message ***
[freeside.git] / etc / acp_logfile-parse
1 #!/usr/bin/perl
2
3 ###
4 # WHO WROTE THIS???
5 ###
6
7 #require "perldb.pl";
8
9 #    Compute SLIP/PPP log times
10 #     Arguments    -a   Process entire file with totals
11 #                  -t   Process only totals
12 #                  -f   File to be processed if not current
13 #                  -d   processing start date (default is entire file)
14 #                  -l   to return all totals for dayuse
15 #                  -w   name of tmp work file for dayuse
16 #                  user names
17
18 require "time.pl";
19
20 $space='        ';
21
22 unless (@ARGV[0]) {
23        print "Missing Arguments\n";
24        print    "-a - entire file\n";
25        print    "-t - totals only\n";
26        print    "-f - file name to be processed\n";
27        print    "-d - processing start date (yymmdd)\n";
28        print    "-l - return totals for dayuse\n";
29        print    "-w - tmp work file for dayuse\n";
30        exit;
31 }     # end if test for missing arguments
32
33 $infile = "/usr/annex/acp_logfile";
34 $tmpfile = "/tmp/ppp";
35 $n = $#ARGV;
36 $start_yymmdd = "";
37 for ($i = 0; $i <= $n; $i++) {
38     if ($ARGV[$i] eq "-a") {
39              $allflag = "true";
40         }
41        elsif ($ARGV[$i] eq "-t") {
42                       $totalflag = "true";
43              }
44        elsif ($ARGV[$i] eq "-f") {
45         $i++;
46           $infile = $ARGV[$i];
47              }
48         elsif ($ARGV[$i] eq "-d") {
49           $i++;
50           $start_yymmdd = $ARGV[$i];
51           }   #end start yymmdd
52         elsif ($ARGV[$i] eq "-l") {
53            $logflag = "true";
54            $totalflag = "true";
55          }  #  end log 
56        elsif ($ARGV[$i] eq "-w") {
57         $i++;
58           $tmpfile = $ARGV[$i];
59              } #  end tmp file 
60         else    {
61             ($arg_user,$arg_yymmdd) = split (/:/, $ARGV[$i]);
62                  $ip_user_date {$arg_user} = $ARGV[$i];
63              $userflag = "true";
64                 }   # end else
65  } # end for 1 = 1 to n
66
67 open (IN,$infile)
68         || die "Can't open acp_logfile";
69
70 NEXTUSER: while (<IN>) {        
71         chop;
72         ($add,$ether,$port,$date,$time,$type,$action,$user) = split(/:/);
73
74         if ($logflag) {
75           $start_yymmdd = '';
76           if ($ip_user_date{$user}) {
77              ($ip_user, $start_yymmdd) = 
78                       split (/:/, $ip_user_date{$user});
79            }  # end get date
80         }   #  end log flag
81         if ($start_yymmdd) {
82            if ($date < $start_yymmdd) {
83                next NEXTUSER;
84            }  #end date compare
85         }  #end if date
86         if ($userflag){
87           if (!$ip_user_date{$user}) {
88                next NEXTUSER;
89           }  #  end user test
90         }  #   end by user or all
91         if (($totalflag) ||
92            ($allflag) ||
93            ($ip_user_date{$user})) {
94          if (($type eq 'ppp') || ($type eq 'slip'))  {
95
96             if ($action eq 'login') {
97                         $login{$user} = "$time:$date";
98
99                 }
100                   elsif ($action eq 'logout') {
101                      if (!$login{$user}) {
102                           $login{$user} = "010101:$date";
103                       } #end pad user if carry over
104                         ($stime,$sdate) = split(':',$login{$user});
105                         $start = &annex2sec($stime);
106                         $end = &annex2sec($time);
107                         
108                         #If we went through midnight, add a day;
109                         if ($end < $start) {$end += 86400;}
110                         $timeon = $end - $start;
111
112                         $elapsed{$user} += $timeon;
113                         
114                       if (!$totalflag) {
115                         print (&fmt_user($user),
116                               '  ', &fmt_date($sdate), '  In: ', 
117                                 &fmt_time($stime),'  Out: ',
118                                 &fmt_time($time),
119                        '  Elapsed: ', &fmt_sec($timeon), "\n");
120                       }  # end total test
121                 }  #end elsif action
122         }  #  type = ppp of slip
123     }  #  check arguments
124
125 close IN;
126
127 if ($logflag) {
128     open (TMPPPP, ">$tmpfile")
129                || die "Can't open ppp tmp file";
130     foreach $user ( sort((keys(%elapsed))) ) {
131         $log_time = &fmt_sec($elapsed{$user});
132         $tmp = join (':',
133                     $user, 
134                     $log_time);
135         print (TMPPPP "$tmp\n");
136     }
137     close (TMPPPP);
138 }
139     else {
140         print "\n\nTotal Time On For Period:\n";
141         print     "-------------------------\n";
142
143         foreach $user ( sort((keys(%elapsed))) ) {
144            print (&fmt_user($user), "  ",&fmt_sec($elapsed{$user}), "\n");
145         }
146     }
147 exit(0);
148
149 #-------------------------------------------------------
150 #--------------- Subroutines Start Here ----------------
151 #-------------------------------------------------------
152
153 sub annex2sec {
154         local($time) = @_;
155         return( &time2sec( &break_annex($time) ) );
156 }
157
158 sub fmt_date {
159         local($date) = @_;
160
161         return( substr($date,2,2).'/'.substr($date,4,2).'/'.substr($date,0,2) );
162 }
163
164 sub fmt_time {
165         local($time) = @_;
166         local($s,$m,$h) = &break_annex($time);
167         return ("$h:$m:$s");
168 }
169
170
171 sub break_annex {
172         local($time) = @_;
173         local($h,$m,$s);
174
175         $h=substr($time,0,2);
176         $m=substr($time,2,2);
177         $s=substr($time,4,2);
178
179         return ($s,$m,$h);
180 }       
181
182 sub fmt_sec {
183         local(@t) = &sec2time(@_);
184         @t[2] += (@t[3]*24);
185
186         foreach $a (@t) {
187                 if ($a < 10) {$a = "0$a";}
188         }
189
190         return ("@t[2]:@t[1]:@t[0]");
191 }
192
193 sub fmt_user {
194         local($user) = @_;
195         return( $user.substr($space,0,8 - length($user) ).'  ' );
196 }
197