Re: [exim] shouldn't exigrep sort the grepped file(names) by…

Góra strony
Delete this message
Reply to this message
Autor: Heiko Schlittermann
Data:  
Dla: exim users
Temat: Re: [exim] shouldn't exigrep sort the grepped file(names) by date?
Nigel Metheringham <nigel.metheringham@???> (Mi 09 Jun 2010 14:19:42 CEST):
> OK, next time *read* more carefully before reply....
>
> However this still breaks if you gzip your log files after
> a period....
>
> Can I suggest this be made a selectable option (maybe default on,
> but at least possible to switch off).


I've changed it (and added a POD part at the end. This way it should be easy to create a man page):

Usage:
    exigrep [-I] [-l] [-s <key>] [-t <seconds>] [-v] pattern [<log file>] ...
    exigrep {-h|m}


Options:
    -I  Do a case sensitive search. (default: off)


    -l  Do not interpret the pattern as regex. Use it for a literal search.
        (default: off)


    -s *key*
        Sort the output according to the *key*. The *key* can be


        file
            Sort according the modification times of the files searched.
            This is the fastest approach, but it may fail.


        first
            Sort by the time stamp of first entry per message. (not
            implemented yet)


        last
            Sort by the time stamp of the last entry per message. (not
            implemented yet)


        (default: do not sort)


    -t *seconds*
        Use the the message time in the queue as an additional parameter.
        (default: unused)


    -h | -m
        Print the short refernce help resp. the manpage. (default: unused)


        NOTE: This options may not be available on systems missing the
        Pod::Usage perl module.


The diff is attached.
--
Heiko
--- /usr/sbin/exigrep    2008-09-30 20:57:42.000000000 +0200
+++ /tmp/exigrep    2010-06-09 22:45:37.000000000 +0200
@@ -34,6 +34,7 @@


use Getopt::Std qw(getopts);
use POSIX qw(mktime);
+eval { use Pod::Usage };


# This subroutine converts a time/date string from an Exim log line into
@@ -59,7 +60,7 @@
# This subroutine processes a single line (in $_) from a log file. Program
# defensively against short lines finding their way into the log.

-my (%saved, %id_list, $pattern, $queue_time, $insensitive, $invert);
+my (%saved, %id_list, $pattern, $queue_time, $insensitive, $invert, $sortby);

sub do_line {

@@ -130,13 +131,23 @@
# are quoted if the -l flag is given. The -t flag gives a time-on-queue value
# which is an additional condition.

-getopts('Ilvt:',\my %args);
+# shouldn't we switch to GetOpt::Long?
+my %args;
+getopts('Ilvt:s:' . (exists(&pod2usage) ? 'hm' : ''), \%args)
+    and (@ARGV || $args{h} || $args{m})
+    or exists(&pod2usage) 
+    ? pod2usage() 
+    : die("usage: exigrep [-I] [-l] [-s <key>] [-t <seconds>] [-v] <pattern> [<log file>]...\n");
+
+if (exists &pod2usage) {
+    pod2usage(-verbose => 1, -exit => 0) if $args{h};
+    pod2usage(-verbose => 2, -exit => 0) if $args{m};
+}
+
 $queue_time  = $args{'t'}? $args{'t'} : -1;
 $insensitive = $args{'I'}? 0 : 1;
 $invert      = $args{'v'}? 1 : 0;
-
-die "usage: exigrep [-I] [-l] [-t <seconds>] [-v] <pattern> [<log file>]...\n"
-  if ($#ARGV < 0);
+$sortby      = $args{'s'}? $args{'s'} : '';


$pattern = shift @ARGV;
$pattern = quotemeta $pattern if $args{l};
@@ -147,7 +158,8 @@

 if (@ARGV)
   {
-  foreach (@ARGV)
+  @ARGV = sort { -M $b <=> -M $a } @ARGV if $sortby eq "file";
+  foreach (@ARGV) 
     {
     my $filename = $_;
     if ($filename =~ /\.(?:gz)$/o)
@@ -176,3 +188,90 @@
   }


# End of exigrep
+__END__
+
+=head1 NAME
+
+exigrep - search Exim's main log(s)
+
+=head1 SYNOPSIS
+
+exigrep [-I] [-l] [-s <key>] [-t <seconds>] [-v] pattern [<log file>] ...
+
+exigrep {-h|m}
+
+=head1 DESCRIPTION
+
+The exigrep utility is a Perl script that searches one or more main log files for
+entries that match a given pattern. When it finds a match, it extracts all the log
+entries for the relevant message, not just those that match the pattern. Thus, exigrep
+can extract complete log entries for a given message, or all mail for a given user, or
+for a given host, for example.
+
+If no file names are given on the command line, the standard input is read.
+
+If the location of a zcat command is known from the definition of ZCAT_COMMAND in
+Local/Makefile, exigrep automatically passes any file whose name ends in COMPRESS_SUFFIX
+through zcat as it searches it.
+
+=head1 OPTIONS
+
+=over
+
+=item B<-I>
+
+Do a case sensitive search. (default: off)
+
+=item B<-l>
+
+Do not interpret the pattern as regex. Use it for a literal search. (default: off)
+
+=item B<-s> I<key>
+
+Sort the output according to the I<key>. The I<key> can be
+
+=over
+
+=item B<file>
+
+Sort according the modification times of the files searched. This is the fastest
+approach, but it may fail.
+
+=item B<first>
+
+Sort by the time stamp of first entry per message. (not implemented yet)
+
+=item B<last>
+
+Sort by the time stamp of the last entry per message. (not implemented yet)
+
+
+=back
+
+(default: do not sort)
+
+=item B<-t> I<seconds>
+
+Use the the message time in the queue as an additional parameter. (default: unused)
+
+=item B<-h> | B<-m>
+
+Print the short refernce help resp. the manpage. (default: unused)
+
+B<NOTE:> This options may not be available on systems missing the L<Pod::Usage>
+perl module.
+
+=back
+
+=head1 SEE ALSO
+
+L<exim(8)>, L<perlre(1)>, F</usr/share/doc/exim4-base/>
+
+=head1 AUTHOR
+
+This manual page was stitched together from spec.txt by Andreas Metzler <ametzler at
+downhill.at.eu.org>, for the Debian GNU/Linux system (but may be used by others).
+Heiko Schlittermann converted this to POD and sticked the POD to the end of the exigrep utility
+itself.
+
+=cut