If you want nagios email notifications but don’t want to run a local MTA (like postfix, exim, qmail, or sendmail), you’ll need a command-line SMTP client. Such a program can connect to an SMTP server, authenticate, and generate a message based on input arguments and file handles. One such program is sendEmail (note, not “sendmail”).

I found myself wanting to use sendEmail with nagios, and I found these helpful resources:

I made some changes and came up with these instructions which show using nagios hosted on an ubuntu machine notifying via sendemail through a gmail account:

  • Create the gmail account
  • On your server (Ubuntu in this case) run: sudo apt-get install sendemail (you can use the installation instructions on the sendEmail website if you aren’t on ubuntu or debian)
  • Edit /etc/nagios3/resource.cfg, adding these lines (using your account info instead of the example account info below):

    # The "from address" for notifications
    $USER5$=example@gmail.com
        
    # The SMTP server
    $USER7$=smtp.gmail.com
        
    # the SMTP authentication username and password
    $USER9$=example@gmail.com
    $USER10$=examplepassword
  • Add these commands to /etc/nagios3/commands.cfg:

    # Lots of line-continuation characters are used in these next two commands
    # for improved readability.
    # If you change the number of arguments to printf, make sure you change the
    # corresponding number of "%b"s in the first argument
        
        
    define command{
    	command_name	notify-host-by-email
    	command_line	/usr/bin/printf "%b%b%b%b%b%b%b" \
    "***** Nagios *****\n\n" \
    "Notification Type: $NOTIFICATIONTYPE$\n" \
    "Host: $HOSTNAME$\n" \
    "State: $HOSTSTATE$\n" \
    "Address: $HOSTADDRESS$\n" \
    "Info: $HOSTOUTPUT$\n\n" \
    "Date/Time: $LONGDATETIME$\n" \
    | /usr/bin/sendemail -s $USER7$ -xu $USER9$ -xp $USER10$ \
    -l /var/log/sendemail \
    -f $USER5$ \
    -t $CONTACTEMAIL$ \
    -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
    	}
    
    
    define command{
    	command_name	notify-service-by-email
    	command_line	/usr/bin/printf "%b%b%b%b%b%b%b%b%b" \
    "***** Nagios *****\n\n" \
    "Notification Type: $NOTIFICATIONTYPE$\n\n" \
    "Service: $SERVICEDESC$\n" \
    "Host: $HOSTALIAS$\n" \
    "Address: $HOSTADDRESS$\n" \
    "State: $SERVICESTATE$\n\n" \
    "Date/Time: $LONGDATETIME$\n\n" \
    "Additional Info:\n\n" \
    "$SERVICEOUTPUT$" \
    | /usr/bin/sendemail -s $USER7$ -xu $USER9$ -xp $USER10$ \
    -l /var/log/sendemail \
    -f $USER5$ \
    -t $CONTACTEMAIL$ \
    -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **"
    }
  • Comment-out the old definitions of “notify-host-by-email” and “notify-service-by-email”

  • Create the log file with: touch /var/log/sendemail and chown nagios /var/log/sendemail

  • Restart nagios, possibly with service nagios3 restart