NSLU2-Linux
view · edit · print · history

You can configure the Slug to send you email alerts when bad things happen, like failed backups and full disks. This is done through the Admin webpage and you can specify the destination email address and the subject line of the email. However, I have never gotten my Slug to send me email with via the normal mechanisms. This is the alert process as I've gleaned from doing strings on various binaries and libraries:

  1. A script which wants to issue an email alert calls /usr/sbin/set_log
  2. set_log calls some library routine in /lib/liblog.so
  3. This library routine calls /usr/sbin/smtpclient which is a binary SMTP mail client.

The sendmail server at my ISP blocks email sent by the standard NSLU2 smtpclient on my Slug because it appears Comcast-issued IP addresses are spam black-listed. If the Linksys-provided smtpclient could be instructed to use a mail relay (Comcast provides one) it shouldn't be blocked as spam, but unfortunately the -S argument of smtpclient, to specify a mail relay host, is not supported. However, nail, an available ipkg package, does support mail relays. Here are the steps to use nail instead of the Linksys-provided smtpclient to send alerts:

  1. Install nail (I got these steps from Nail)
    ipkg install nail
  2. Create a .mailrc for root in /root/.mailrc (or where ever your root's homedir is) containing
    set smtp=smtp.comcast.net
    set from=root@<my.dommainname.com>
  3. Create wrapper script for nail which translates smtpclient parameters to nail parameters and passes along whatever comes through stdin. I store this script in /opt/local/bin/nail_wrapper. I didn't want to store my wrapper script in the original location for first time I did that I forgot about it and my script got wiped out when I upgraded from Unslung 3.18 to 5.5. Script is listed below.
  4. Rename the original smtpclient program
    mv /usr/sbin/smtpclient /usr/sbin/smtpclient.orig
  5. Create a symbolic link pointing to the new script
    ln -s /opt/local/bin/nail_wrapper /usr/sbin/smtpclient

You can test the new mechanism by calling set_log. E.g:

/usr/sbin/set_log backup 0 1

This will send you a bogus email alert about a backup failure. The "backup" keyword is the name of a stanza in /etc/cgi_msg. The last number, 1, is an index to the actual error message in the backup stanza. The second number, 0, tells set_log to send an email rather than just log a message in /var/log/server.log. If it were a 1, no email would be sent. Try man set_log for a few more details.

/opt/local/bin/nail_wrapper script:

This script needs to translate the arguments liblog.so provides to smtpclient to those appropriate for nail. liblog.so calls smtpclient like this, sending the text of the email on standard input:

/usr/sbin/smtpclient -s <subject> -f admin@<IP of slug> <to_address>

where <subject> and <to_address> are provided in the NSLU2 Admin webpage. With this script I am basically throwing away the -f argument, which does something different with nail. E.g:

/opt/bin/nail -s <subject> <to_address>

The script logs what it does to /var/log/mail_alert.log. You can take this out if you don't like it. Also, because I was lazy and used getopts, the script requires bash to be installed. Also, make sure the script has execute permissions. Mine is 755 owned by root.

I would appreciate any feedback if this Howto is useful to you. Enjoy!


#!/bin/bash

log_file=/var/log/mail_alert.log

while getopts s:f: arg; do
  case $arg in
    s) subject="$OPTARG";;
    f) from_addr="$OPTARG";;
    ?) echo "Syntax error"
       exit 1;;
  esac
done
shift $(($OPTIND - 1))
to_addr="$*"

date >> $log_file
echo "Subject: $subject" >> $log_file
echo "From: $from_addr" >> $log_file
echo "To: $to_addr" >> $log_file

cat - | tee -a $log_file | /opt/bin/nail -s "$subject" "$to_addr"
view · edit · print · history · Last edited by Lars Persson Fink.
Based on work by Nathan and nellswor.
Originally by nellswor.
Page last modified on March 02, 2007, at 10:00 AM