add logging of PC92A ip addresses
[spider.git] / perl / grepdbg
1 #!/usr/bin/perl
2 #
3 # Program to do a grep with dates and times on the debug
4 # files
5 #
6 # grepdbg [nn] [-mm] <regular expression>
7 #
8
9 # nn - is the day you what to look at: 1 is yesterday, 0 is today
10 # and is optional if there is only one argument
11 #
12 # -mmm - print the mmm lines before the match. So -10 will print
13 # ten lines including the line matching the regular expression. 
14 #
15 # <regexp> is the regular expression you are searching for, 
16 # a caseless search is done
17 #
18 #
19
20 require 5.004;
21
22 # search local then perl directories
23 BEGIN {
24         # root of directory tree for this system
25         $root = "/spider"; 
26         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
27         
28         unshift @INC, "$root/perl";     # this IS the right way round!
29         unshift @INC, "$root/local";
30 }
31
32 use DXVars;
33 use DXUtil;
34 use DXLog;
35 use Julian;
36
37 use strict;
38
39 use vars qw(@list $fp $today $string);
40
41 $fp = DXLog::new('debug', 'dat', 'd');
42 $today = $fp->unixtoj(time()); 
43 my $nolines = 1;
44 my @prev;
45
46 for my $arg (@ARGV) {
47         if ($arg =~ /^-/) {
48                 $arg =~ s/^-//o;
49                 push @list, $arg;
50         } elsif ($arg =~ /^\d+$/) {
51                 $nolines = $arg;
52         } else {
53                 $string = $arg;
54                 last;
55         }
56 }
57 die "usage: grepdbg [nn] [[-nnn] ..] <regexp>\n" unless  $string;
58
59 push @list, "0" unless @list;
60 for my $entry (@list) {
61         my $now = $today->sub($entry); 
62         my $fh = $fp->open($now); 
63         my $line;
64         if ($fh) {
65                 while (<$fh>) {
66                         my $line = $_;
67                         chomp $line;
68                         push @prev, $line;
69                         shift @prev while @prev > $nolines;
70                         if ($line =~ m{$string}io) {
71                                 for (@prev) {
72                                         s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg; 
73                                         my ($t, $l) =  split /\^/, $_, 2;
74                                         print atime($t), ' ', $l, "\n"; 
75                                 }
76                                 @prev = ();
77                         }
78                 }
79                 $fp->close();
80         }
81 }
82 exit(0);