icinga logo

Icinga2 email via ssmtp

I have been playing around with Icinga2 for some monitoring at home. I wanted to monitor a few of my external services. Since my email is one (or the primary) of them I needed alert notifications to be sent via something else. So I setup ssmtp and have email sent through a gmail account.

Make sure to enable the account to be accessible by less secure methods for this to work. If anyone comes across a way to not have to enable that, please let me know.

ssmtp.conf:
root=some-account@gmail.com
hostname=host.mydomain.com
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
AuthUser=some-account
AuthPass=much-secret--very-password
AuthMethod=LOGIN
UseTLS=Yes
UseSTARTTLS=Yes
FromLineOverride=YES

As I don’t want all potential email from this specific system to be sent out I chose not to replace the sendmail binary but rather use the smtp binary directly. Hence I had to just slightly tweak the example scripts for sending email in order to make them work with ssmtp;

mail-host-notification.sh:
#!/bin/sh

template=`cat <<TEMPLATE
To: $USEREMAIL
From: some-account@gmail.com
Subject: $NOTIFICATIONTYPE - $HOSTDISPLAYNAME is $HOSTSTATE
***** Icinga *****

Notification Type: $NOTIFICATIONTYPE

Host: $HOSTALIAS
Address: $HOSTADDRESS
State: $HOSTSTATE

Date/Time: $LONGDATETIME

Additional Info: $HOSTOUTPUT

Comment: [$NOTIFICATIONAUTHORNAME] $NOTIFICATIONCOMMENT
TEMPLATE
`
/usr/bin/printf "%b" "$template" | /usr/local/sbin/ssmtp $USEREMAIL

mail-service-notification.sh:
#!/bin/sh
template=`cat <<TEMPLATE
To: $USEREMAIL
From: some-account@gmail.com
Subject: $NOTIFICATIONTYPE - $HOSTDISPLAYNAME - $SERVICEDISPLAYNAME is $SERVICESTATE
***** Icinga *****

Notification Type: $NOTIFICATIONTYPE

Service: $SERVICEDESC
Host: $HOSTALIAS
Address: $HOSTADDRESS
State: $SERVICESTATE

Date/Time: $LONGDATETIME

Additional Info: $SERVICEOUTPUT

Comment: [$NOTIFICATIONAUTHORNAME] $NOTIFICATIONCOMMENT
TEMPLATE
`
/usr/bin/printf "%b" "$template" | /usr/local/sbin/ssmtp $USEREMAIL

Share

Leave a Reply

Your email address will not be published. Required fields are marked *