+
-------------------------------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);# || ¬ify_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.
#
¬ify_custodian ("$i Messages were deleted from $MAILFILE") if ($i > 0);
#
# Write out the messages to be deleted to $SAVEFILE.
#
open (FILE, ">$SAVEFILE") || ¬ify_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") || ¬ify_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;