#!/usr/bin/perl -w use strict; use Getopt::Std; use Time::Local; use POSIX qw(strftime); $|++; my %Opt; my @Conns; &GetArgs(); &GetConns(); &GetLogs(); sub GetArgs { my $Usage = qq{Usage: $0 [options] -h : This help message. -c : Specific connector - default is to list all connectors. -d : Specific direction - default is to list all directions -n : Trap name - default is to list all names -t : Time in stamp format mm/dd/yy-hh:mm or mm/dd/yy + - show entries created after specified stamp If time is not given, defaults to 23:59 - - show entries created before specified stamp If time is not given, default to 00:00 = - show entries created on specified stamp If time is not given it is ignored (all day) +- - show entries created between specified stamps If time is not given on first stamp, 00:00 is used If time is not given on second stamp, 23:59 is used Note: This includes the day(s) specified -s : Size of files caught in bytes + - show entries with files larger than specified size - - show entries with files smaller than specified size = - show entries with files equal to specified size +- - show entries with files between specified sizes } . "\n"; getopts( 'hc:d:n:t:s:', \%Opt ) or die "$Usage"; die "$Usage" if $Opt{h}; if ($Opt{d}) { $Opt{d} = lc($Opt{d}); die "$Usage" if ($Opt{d} ne "in" && $Opt{d} ne "out" && $Opt{d} ne "both"); } } sub GetConns { open (CONNECTORS,"/var/wt400/conf/_wtd.cfg") or die "\nUnable to open connector file!\n"; while () { next unless ($_ =~ /^unit="(.*)"/); my $Conn = lc($1); next if ($Conn eq "ins" || $Conn eq "ins2" || $Conn eq "_wtd"); push @Conns , $Conn; } close (CONNECTORS); if ($Opt{c}) { $Opt{c} = lc($Opt{c}); if (grep /\b$Opt{c}\b/ , @Conns) { @Conns = $Opt{c}; } else { die "\nInvalid connector - $Opt{c} !\n"; } } } sub GetLogs { my @Logs; foreach my $Conn (@Conns) { my @Directions; if ($Opt{d}) { @Directions = $Opt{d}; } else { @Directions = (qw(in out both)); } foreach my $Dir (@Directions) { push @Logs , "/var/spool/wt400/log/$Conn/trap_${Dir}.log" if (-r "/var/spool/wt400/log/$Conn/trap_${Dir}.log" && -s _); } } unless (@Logs) { die "\nUnable to find any logs!\n"; } else { while (my $File = shift @Logs) { my($mon, $day, $year, $hour, $min); open(LOG,$File); LINE: while (my $Line = ) { chomp $Line; my @Fields = split " " , $Line; if ($Opt{n}) { next unless (lc($Opt{n}) eq lc($Fields[3])); } if ($Opt{t}) { $Opt{t} =~ s/\s+//; my $Stamp1; my $Stamp2; if ($Opt{t} =~ /^\+(.*)/) { ($mon, $day, $year, $hour, $min) = split ?[-/:]? , $1; ($hour,$min) = (23,59) unless ($hour && $min); $Stamp1 = timelocal(0, $min, $hour, $day, $mon - 1, $year + 100); next unless ($Fields[0] > $Stamp1); } elsif ($Opt{t} =~ /^\-(.*)/) { ($mon, $day, $year, $hour, $min) = split ?[-/:]? , $1; ($hour,$min) = (00,00) unless ($hour && $min); $Stamp1 = timelocal(0, $min, $hour, $day, $mon - 1, $year + 100); next unless ($Fields[0] < $Stamp1); } elsif ($Opt{t} =~ /^\=(.*)/) { ($mon, $day, $year, $hour, $min) = split ?[-/:]? , $1; ($hour,$min) = (00,00) unless ($hour && $min); $Stamp1 = timelocal(0, $min, $hour, $day, $mon - 1, $year + 100); ($hour,$min) = (23,59) unless ($hour && $min); $Stamp2 = timelocal(0, $min, $hour, $day, $mon - 1, $year + 100); next unless ($Fields[0] >= $Stamp1 && $Fields[0] <= $Stamp2 ); } elsif ($Opt{t} =~ /^(.*)\+\-(.*)/) { ($mon, $day, $year, $hour, $min) = split ?[-/:]? , $1; ($hour,$min) = (00,00) unless ($hour && $min); $Stamp1 = timelocal(0, $min, $hour, $day, $mon - 1, $year + 100); ($mon, $day, $year, $hour, $min) = split ?[-/:]? , $2; ($hour,$min) = (23,59) unless ($hour && $min); $Stamp2 = timelocal(0, $min, $hour, $day, $mon - 1, $year + 100); next unless ($Fields[0] >= $Stamp1 && $Fields[0] <= $Stamp2 ); } } if ($Opt{s}) { $Opt{s} =~ s/\s+//; if ($Opt{s} =~ /^\+(.*)/) { next unless ($Fields[2] > $1); } elsif ($Opt{s} =~ /^\-(.*)/) { next unless ($Fields[2] < $1); } elsif ($Opt{s} =~ /^\=(.*)/) { next unless ($Fields[2] == $1); } elsif ($Opt{s} =~ /^(.*)\+\-(.*)/) { next unless ($Fields[2] >= $1 && $Fields[2] <= $2 ); } } if ($File =~ /^.*\/(.*)\/trap_(.*)\.log/) { my $Conn = $1; my $Dir = $2; my $Time = strftime("[%x-%X]",localtime($Fields[0])); print "$Time $Conn $Dir $Fields[3] $Fields[1] $Fields[2]\n"; } } } } }