Xref: feenix.metronet.com comp.unix.admin:3427
Path: feenix.metronet.com!news.utdallas.edu!convex!convex!convex!darwin.sura.net!howland.reston.ans.net!math.ohio-state.edu!uwm.edu!ogicse!netnews.nwnet.net!pnl-oracle!pnl-bbs!toysrus!d3c572
From: d3c572@nsisb2.pnl.gov (DS Curtis, ISB2 108, 375-2152)
Newsgroups: comp.unix.admin
Subject: Re: aging mail in the mail spool
Message-ID: <20a5ga$cto@bbs.pnl.gov>
Date: 23 Jun 93 17:56:25 GMT
Article-I.D.: bbs.20a5ga$cto
References: <1993Jun22.045238.23251@mont.cs.missouri.edu>
Reply-To: d3c572@nsisb2.pnl.gov
Distribution: world
Organization: Battelle Pacific Northwest Laboroatories
Lines: 268
NNTP-Posting-Host: toysrus.pnl.gov

In article 23251@mont.cs.missouri.edu, c559165@monad.missouri.edu (H. Paul Hammann) writes:
>Hello,
>
>   I have a question regarding aging mail in the mail spool.  As in how
>do you do it?  I understand how to age files in the print spool, that's
>easy, they are all seperate files, but a users mail is all kept in
>usr/spool/mail/user_id.  I am on a NeXT machine running NeXTStep 2.1 
>If anyone could point me in the right direction, or if someone has some
>scripts that they could send me I would be most appreciative. I am 
>sorry if this is a FAQ.  Please send any mail to
>c559165@monad.missouri.edu.  Thanks to anyone who replies.
>
>Sincerely,
>
>H. Paul Hammann
>c559165@monad.missouri.edu


The attached is a *HARDCODED* perl script that reduces the number of mail messages
if there are more than 1500 (or the first argument to the script).  There is one
exception to deleting all but 1500 (or first argument) messages.  If all of the
messages are less than a month old, then no deletions take place.  This script
is provided *AS IS*.  With a little work you could modify it to take any number
of arguments and work on the whole mail directory or add a second argument and
write a shell script that performed a foreach on each file in /var/spool/mail.
The script was written in a couple hours so the documentation may not be the
best but at least it has some comments that you should be able to figure out
what the script is doing if you want to write a C program or shell script to
perform the same function.

I hope this can be of use to you.

+--------------------------------------------------------------------------+
| Name:     Darren Curtis        |  Disclaimer:  My opinion is my own!     |
| Company:  Battelle PNL         |  Theory:  If it is not broken then      |
| Phone:    (509) 375-2152       |           make it faster!               |
| E-mail:   ds_curtis@pnl.gov    |  This space left blank intentionally!   |
+--------------------------------------------------------------------------+

-------------------------------CUT HERE------------------------------------
#!/bin/perl

#$DEBUG      = 1;
#$EARLY_EXIT = 1;
#$SAVE_MESS  = 1;

;#
;#  $Id$
;#  Abstract:
;#    Keep the number of messages in the /var/spool/mail/opmail file under
;#    1500 or the number entered as the first argument to the script.
;#    The script will be run out of cron on a regular basis to keep the
;#    number of messages in the file under control.  This script will exit
;#    as fast as possible if the number of messages in the opmail file are
;#    less than 1500 or the command line argument.
;#  Usage:
;#    opslog_mail_monitor.pl [ number ]
;#      number - (integer), brief_desc
;#  Input file(s):
;#    /var/spool/mail/opmail
;#  Output file(s):
;#    /var/spool/mail/opmail
;#  Return Value(s);
;#    0 - (integer), Successfull execution.
;#    1 - (integer), Unsuccessfull execution.
;#  Manifest:
;#    NONE
;#  Operating system(s):
;#    SunOS 4.1.2
;#  Author:  3C572  Date of origin:  06-03-93
;#  $Log$
;#


@COMMAND       = split (/\//, $0);        ;# Parse command path and name.
$COMMAND       = $COMMAND[$#COMMAND];     ;# Assign $COMMAND to program name.
$CUSTODIAN     = "root";                  ;# Person to send mail to.
$ECHO          = "/bin/echo";             ;# Hardcoded shell echo program.
$MAIL          = "/usr/ucb/Mail";         ;# Hardcoded mail program.


#
#  Specify the number of messages to keep determined by the first argument
#  on the command line or default to 1500.
#
$SAVE_NUMBER = (defined ($ARGV[0]) && $ARGV[0] > 0) ? $ARGV[0] : 1500;
 
 
#
#  Build array of months in the year.
#
@MONTHS = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
 
 
#
#  Find out what month and day is today.
#
($SEC, $MIN, $HOUR, $MDAY, $MON, $YEAR, $WDAY, $YDAY, $ISDST) = gmtime (time());
 
 
#
#  The file to be checked and possibly modified.
#
$CURR_MONTH = $MON;
$PREV_MONTH = $MON >= 1 ? $MON-1 : 11;
$TODAY      = $MON + 1;
$TODAY      = sprintf ("%.2d_%.2d_%.2d", $TODAY, $MDAY, $YEAR);
&DEBUG && print ("TODAY      = $TODAY\n");
exit 0;
 
$MAILDIR    = '/var/spool/mail';
$MAILFILE   = "$MAILDIR/opmail";
$SAVEDIR    = '/home/opslog';
$SAVEFILE   = "$SAVEDIR/logfiles_opmail_$TODAY";
 
$DEBUG && print ("MAILFILE   = $MAILFILE\n");
$DEBUG && print ("SAVEFILE   = $SAVEFILE\n");
 
 
#
#  Set the current month and previous month.  Special case for previous
#  month if the current month is January then the previous month is set
#  to December.
#
$CURR_MONTH = $MONTHS[$MON];
$PREV_MONTH = $MON >= 1 ? $MONTHS[$MON-1] : $MONTHS[11];
 
$DEBUG && print ("CURR_MONTH = $CURR_MONTH\n");
$DEBUG && print ("PREV_MONTH = $PREV_MONTH\n");
 
 
sub notify_custodian {
;#
;#  Description:
;#    Subroutine to build the body of a mail message that will be
;#    sent to $CUSTODIAN.
;#  Parameters:
;#    $SUBJECT - (string), subject of mail message.
;#  Return Value(s):
;#    NONE
;#  Author:  3C572  Date:  05-19-92
;#
  local ($SUBJECT) = @_;
 
  $DEBUG && print ("notify_custodian\n");
  push (@MAIL_FILE, "$SUBJECT\n");
  }
 
 
sub mail_custodian {
;#
;#  Description:
;#    Subroutine to send a mail message (if necessary) to $CUSTODIAN.
;#  Parameters:
;#    $SUBJECT - (string), subject of mail message.
;#  Return Value(s):
;#    NONE
;#  Author:  3C572  Date:  05-19-92
;#
  local ($SUBJECT) = @_;
 
  $DEBUG && print ("mail_custodian\n");
  $DEBUG && print ("$ECHO \" @MAIL_FILE\"\n");
  if ( (defined (@MAIL_FILE)) && (! defined ($DEBUG)) ) {
    system ("$ECHO \" @MAIL_FILE\" \| $MAIL -s \"$SUBJECT\" $CUSTODIAN");
    }
  }
 
 
#
#  Open the $MAILFILE and build and array of all the mail messages in the
#  file.  Note the special case at the end to get the last message.
#
open (FILE, $MAILFILE);# || &notify_custodian ("Can't open $MAILFILE: $!");
$LINE = <FILE>;
push (@MESSAGE, $LINE);
while (<FILE>) {
  if (/^From /) {
    #
    #  Start of a new message so join all the parts of the previous
    #  message and insert the previous message into the array of messages.
    #
    $MESSAGE = join ('', @MESSAGE);
    push (@FILE, $MESSAGE);
    undef (@MESSAGE);
    }
  push (@MESSAGE, $_);
  }
#
#  Special case to insert the last message into the array of messages.
#
$MESSAGE = join ('', @MESSAGE);
push (@FILE, $MESSAGE);
close (FILE);
 
 
#
#  Exit if the number of messages is less than $SAVE_NUMBER.
#
exit 0 if (($#FILE < $SAVE_NUMBER) && (defined ($EARLY_EXIT)));
 
 
#
#  Keep each message that is in the current month or that is less than
#  30 days old in the array @MESSAGES.  Keep the messages that are older
#  than 30 days in the array @DEL_MESSAGES.
#
foreach (@FILE) {
  ($FROM, $USER, $WDAY, $MONTH, $DAY, $TIME, $YEAR, @REST) = split(/\s+/, $_);
  if (($MONTH eq $CURR_MONTH) || (($MONTH eq $PREV_MONTH) && ($DAY >= $MDAY))) {
    push (@MESSAGES, $_);
    }
  else {
    push (@DEL_MESSAGES, $_);
    }
  }
 
 
#
#  Keep all messages if the number of messages is less than $SAVE_NUMBER,
#  otherwise only keep $SAVE_NUMBER messages.
#
$i = $#MESSAGES >= $SAVE_NUMBER ? $#MESSAGES - $SAVE_NUMBER : 0;
 
$DEBUG && print ("#MESSAGES  = $#MESSAGES\n");
$DEBUG && print ("i          = $i\n");
 
 
#
#  Notify the custodian if any messages were deleted.
#
&notify_custodian ("$i Messages were deleted from $MAILFILE") if ($i > 0);
 
 
#
#  Write out the messages to be deleted to $SAVEFILE.
#
open (FILE, ">$SAVEFILE") || &notify_custodian ("Can't open $SAVEFILE: $!");
foreach (@DEL_MESSAGES) {
  print (FILE $_);
  }
$j = 0;
while ($j < $i) {
  print (FILE $MESSAGES[$j]);
  $j++;
  }
close (FILE);
 
 
#
#  Write out the messages to $MAILFILE.
#
open (FILE, ">$MAILFILE") || &notify_custodian ("Can't open $MAILFILE: $!");
while ($i <= $#MESSAGES) {
  print (FILE $MESSAGES[$i]);
  $i++;
  }
close (FILE);
 
 
#
#  Send a summary message to the custodian.
#
&mail_custodian ("Site Data System - $COMMAND - Summary Report:");
 
 
exit 0;
