Statistics

The sendmail program provides the ability to gather information that can be used to produce valuable statistics. As you will see, the StatusFile (S) option (see StatusFile (S)) is used to specify a file into which delivery agent statistics can be saved. The mailstats(1) program (see "Viewing Statistics: mailstats") prints a summary of those statistics.

The sendmail.st File

The sendmail program can maintain an ongoing record of the total number and total sizes of all outgoing and incoming mail messages handled by each delivery agent. This ability is enabled by using the StatusFile (S) option (see ):

OS/path <-prior to V8.7 O StatusFile=/path <-beginning with V8.7

The /path is the full pathname of the file into which statistics are saved. Most vendors provide configuration files that specify /path as

/etc/sendmail.st

Just declaring the StatusFile (S) option is not enough, however, for if the file does not exist (or if it is unwritable), sendmail silently ignores that option and does not save statistics. You must also create the empty file

% touch /etc/sendmail.st

Note that the gathering of statistics can later be turned off merely by renaming or removing the file.

If the StatusFile (S) option has not already been declared, you need to declare it and then kill and restart the sendmail daemon for that declaration to take effect.

Viewing Statistics: mailstats

The mailstats program is supplied with sendmail to provide a convenient way to print the contents of the sendmail.st file. The output of the mailstats program varies depending on the version of sendmail installed. For V8.8 sendmail the output looks like this:

Statistics from Fri May 10 11:23:55 1996 M msgsfr bytes_from msgsto bytes_to Mailer 1 0 0K 43 5913K *file* 3 26 546K 96 639K local 4 421 2996K 3271 78233K smtp ======================================== T 447 3542K 3410 84785K

The first line shows the time the statistics file was begun. The lines that follow show the number of messages and the total size in kilobytes of those messages both received (msgsfr) and sent (msgsto) for each delivery agent. The M column shows the index into the internal array of delivery agents, and the Mailer shows the symbolic name. The last line shows totals. Note that if a delivery agent handled no traffic, it is excluded from the report.

Using cron for Daily and Weekly Statistics

The mailstats program prints the contents of the sendmail.st file, but it does not zero the counters in that file. To clear (zero) that file, you need to truncate it. One easy way to truncate the sendmail.st file is

% cp /dev/null /etc/sendmail.st

When sendmail discovers an empty sendmail.st file, it begins gathering statistics all over again. One use for truncation is to collect daily reports from mailstats. Consider the following simple shell script:

#!/bin/sh ST=/etc/sendmail.st MS=/usr/ucb/mailstats if [ -s $ST -a -f $MS ]; then $MS | mail -s "Daily mail stats" postmaster cp /dev/null $ST fi exit 0

When run, this script checks to see whether a nonempty sendmail.st file and program mailstats exist. If they do, mailstats is run, printing the statistics, which are then mailed to postmaster. The sendmail.st file is then truncated to a size of zero. Such a script could be run once per night using the cron(8) facility with a crontab(5) entry like this:

* * * sh /usr/ucb/mailstats.script >/dev/null 2>&1

Here, mailstats.script is the name given to the above shell script, and the 0 causes that script to be executed once per day at midnight.

Some versions of mailstats allow you to specify a different location for the statistics file. The form of that specification varies with the version of sendmail being run (see mailstats(8)). Yours may look like one of the following:

% mailstats /var/log/statlog % mailstats -f /var/log/statlog <-V8 uses this form

If your version of mailstats allows a different location (and name) for the statistics file, you can move that file to the new location by revising the StatusFile (S) option in the sendmail program's configuration file:

OS/var/log/statlog

Note that V8 mailstats(8) automatically parses the configuration file to find the location of its statistics file.

Moving and renaming the statistics file allows one to automatically collect daily copies of that file. Consider the following variation on the previous shell script:

#!/bin/sh DIR=/var/log ST=statlog MS=/usr/ucb/mailstats if [ -d $DIR ]; then cd $DIR if [ -s $ST -a -f $MS ]; then test -f ${ST}.5 && mv ${ST}.5 ${ST}.6 test -f ${ST}.4 && mv ${ST}.4 ${ST}.5 test -f ${ST}.3 && mv ${ST}.3 ${ST}.4 test -f ${ST}.2 && mv ${ST}.2 ${ST}.3 test -f ${ST}.1 && mv ${ST}.1 ${ST}.2 test -f ${ST}.0 && mv ${ST}.0 ${ST}.1 test -f ${ST} && mv ${ST} ${ST}.0 touch ${ST} $MS -f $DIR/${ST}.0 | mail -s "Daily mail stats" postmaster fi fi exit 0

As before, the statistics are mailed to postmaster. But instead of being truncated, the sendmail.st file is renamed sendmail.st.0. A series of renames (mv(1)) are used to maintain a week's worth of copies. These copies allow the ambitious administrator to create a program for gathering weekly summaries from seven archived daily copies.