home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / ssl < prev    next >
Encoding:
Internet Message Format  |  1989-10-26  |  4.2 KB

  1. Path: tut.cis.ohio-state.edu!mailrus!wuarchive!texbell!texsun!convex!convex.com!tchrist
  2. From: tchrist@convex.com (Tom Christiansen)
  3. Newsgroups: alt.sources
  4. Subject: sendmail syslog summarizer
  5. Keywords: accounting, sendmail, syslog, perl
  6. Message-ID: <1641@convex.UUCP>
  7. Date: 31 Aug 89 12:16:59 GMT
  8. Sender: news@convex.UUCP
  9. Reply-To: tchrist@convex.com (Tom Christiansen)
  10. Organization: Convex Computer Corporation, Richardson, Tx.
  11. Lines: 153
  12.  
  13. This is in response to article <848@radig.UUCP> by 
  14. peter@radig.UUCP (Peter Radig) in comp.mail.sendmail
  15. and comp.sources.wanted, who wanted: 
  16.  
  17. >is there any public domain accounting package for the sendmail kit?
  18. >I need it for charging my users against their outgoing mail.
  19.  
  20. I whipped together a quick perl script to do this.  I call it ssl.  It
  21. will summarize both incoming and outgoing mail by user according to
  22. bytes transferred and also according to message count.  It has some
  23. remedial address parsing to try to determine the person's mbox part of
  24. the address, This is the first crude attempt, but it works.  Things yet
  25. to do include:
  26.  
  27.     write a man page
  28.     merge some of the common To: and From: code
  29.         by using multi-dimensional arrays
  30.     add sorting options
  31.     add more intelligence to address parsing
  32.  
  33. --tom
  34.  
  35. #!/usr/local/bin/perl
  36. #
  37. # summarize sendmail syslog
  38. #
  39. # flags mean:
  40. #
  41. #    -o    outbound mail only
  42. #    -i    inbound  mail only
  43. #    -t    suppress printing of totals
  44. #    -e     print strange lines to stderr
  45. #    -m    reduce to local mbox is possible
  46.  
  47. ($program = $0) =~ s%.*/%%;
  48.  
  49.  
  50. while ($ARGV[0] =~ /^-/) {
  51.     $ARGV[0] =~ s/^-//; 
  52.     foreach $flag ( split (//,$ARGV[0]) ) {
  53.     if ( 'oitem' !~ /$flag/ ) {
  54.         printf stderr "unknown flag: %s\n", $flag;
  55.         die "usage: $program [-oitem] [syslog_file ...]\n";
  56.     } 
  57.     die "$0: '$flag' flag already set\n" if ($flags{$flag}++);
  58.     } 
  59.     shift;
  60. }
  61.  
  62. if ( !$flags{'o'} && !$flags{'i'} && !$flags{'t'}) {
  63.     $flags{'o'}++;
  64.     $flags{'i'}++;
  65.  
  66. do hash_passwd() if $flags{'m'};
  67.  
  68. while (<>) {
  69.     if (/: [A-Z][A-Z](\d+): from=(.*), size=(\d+)/) {
  70.     next unless $flags{'t'} || $flags{'o'};
  71.     ($id, $user, $size) = ($1, $2, $3);
  72.     $user =~ s/.*<(.*)>/$1/;
  73.  
  74.     if ($flags{'m'}) {
  75.         $ouser = $user;
  76.         $user = do strip($user);
  77.         $user = $ouser if ! $known{$user};
  78.     }
  79.  
  80.     $from_user_size{$user} += $size;
  81.     $id_size{$id} = $size;
  82.     $from_user_count{$user}++;
  83.     $total_from++;
  84.     } elsif (/: [A-Z][A-Z](\d+): to=(.*), delay=/) {
  85.     next unless $flags{'t'} || $flags{'i'};
  86.     $id = $1;
  87.     for (split(/,/, $2)) {
  88.         s/.*<(.*)>/$1/;
  89.         $to = $flags{'m'} ? do strip($_) : $_;
  90.         printf "adding %d bytes to %s from %s\n",
  91.         $id_size{$id},$to,$id; 
  92.         if (!$to) {
  93.         die "to no one: $_\n";
  94.  
  95.         } 
  96.         $to_user_size{$to} += $id_size{$id};
  97.         $to_user_count{$to}++;
  98.         $total_to++;
  99.     } 
  100.     } else {
  101.     study;
  102.     next if /message-id/;
  103.     next if /locked/;
  104.     next if /alias database (auto|)rebuilt/;
  105.     #next if /aliases/;
  106.     next if /rebuilding alias database/;
  107.     print stderr if $flags{'e'};
  108.     $errors++;
  109.     } 
  110.  
  111. printf stderr "Error lines: %d\n", $errors if $errors && ($flags{'e'}) && !($flags{'t'});
  112.  
  113.  
  114. if ($flags{'i'}) {
  115.     printf "To: %d\n", $total_to unless $flags{'t'};;
  116.     foreach $user ( keys(to_user_size) ) {
  117.     printf "%4d message%s %7d bytes %s\n",
  118.         $to_user_count{$user}, 
  119.         $to_user_count{$user} != 1 ? "s" : " ",
  120.         $to_user_size{$user}, 
  121.         $user;
  122.     } 
  123. }
  124.  
  125.  
  126. if ($flags{'o'}) {
  127.     printf "From: %d\n", $total_from unless $flags{'t'};;
  128.     foreach $user ( keys(from_user_size) ) {
  129.     printf "%4d message%s %7d bytes %s\n",
  130.         $from_user_count{$user}, 
  131.         $from_user_count{$user} != 1 ? "s" : " ",
  132.         $from_user_size{$user}, 
  133.         $user;
  134.     } 
  135. }
  136.  
  137. sub strip {
  138.     $_ = @_;
  139.     
  140.     s/@.*//;
  141.     s/.*!//;
  142.     s/\s*\(.*\)//;
  143.     tr/A-Z/a-z/;
  144.  
  145.     return $_;
  146.  
  147. sub hash_passwd {
  148.     chop($yp = `/bin/domainname`) if -x '/bin/domainname';
  149.     $passwd = $yp ? 'ypcat passwd |' : '/etc/passwd';
  150.     open passwd || die "$program: can't open $passwd: $!\n";
  151.     while (<passwd>) {
  152.     /^(\w+):[^:]+:(\d+):.*/;
  153.     ($who,$uid) = ($1, $2);
  154.     $uid = 'zero' if ! $uid;  # kludge for uid 0
  155.     $known{$who} = $uid;
  156.     } 
  157.     close passwd;
  158.  
  159.     Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
  160.     Convex Computer Corporation                            tchrist@convex.COM
  161.          "EMACS belongs in <sys/errno.h>: Editor too big!"
  162.  
  163.