Automate ‘periodic daily’ e-mail reading

Many systems send status e-mails from their periodic {daily,weekly,monthly} cronjobs. But just as with log messages you probably stop reading them because there are too many mails (one or two mails per day and host) and they are very repetitious (on healthy systems they should not change too much). — This demands automation.

A simple solution is to eliminate variable fields from the periodic output and only send an e-mail if the output is different from the expected or the previous output.

The first step includes tweaking your periodic.conf and disable the output of disk usage and netstat. This is also the place to configure periodic to write into a file instead of stdout. On my FreeBSD systems my local periodic.conf looks like this:

daily_clean_rwho_verbose="NO"
daily_clean_hoststat_verbose="NO"
daily_status_disks_enable="NO"
daily_status_network_enable="NO"
daily_status_rwho_enable="NO"
 
daily_status_security_inline="YES"
daily_output=/var/log/periodic_daily

(Unfortunately this does not work as well on NetBSD. Most options are similar and configurable in /etc/{daily,monthly}.conf, but there is no switch to exclude the output of “uptime“.)

So once we have the periodic output in a file and do not expect it to change every day we can write a small wrapper arround cmp or diff; mine is called cmp.sh:

#! /bin/sh
infile=$1
normfile=${infile}.norm
lastfile=${infile}.last
 
cmp -s $infile $normfile || cat $infile
mv $infile $lastfile

In my crontab I replaced “periodic daily” with “periodic daily && /root/bin/cmp.sh /var/log/periodic_daily“.

Now what happens? The script expects the output in /var/log/periodic_daily and compares it with the expected output in /var/log/periodic_daily.norm. If these are identical then everything is fine and no further output is produced. But if there are differences then the daily status is written to stdout, thus causing cron to send an e-mail.

This also works with all kinds of custom scripts run by periodic daily. For example one can check the RAID status or have a “logtail postgresql.log“.

Comments are closed.