In the previous posts we've seen installing and configuring Nagios server to monitor all your server's and network. It's not possible to check nagios web interface all the time to confirm your network is OK, then what's the use of it. If your Nagios server can send you e-mail alrets when some service or server goes down and your mail is configured in your mobile then .. that's the real purpose of Nagios. Let's look in to it.

To set up e-mail alerts, first you need to make sure the following things.
  • Install ssmtp and configure 
  • Make sure DNS is configured properly and  destinations mail domain and source domain are getting resolved.
  • Add your and destinations mail id's in contacts, contact groups.
Installing and configuring ssmtp.


[root@nagios ~]# yum install ssmtp -y
[root@nagios ~]# yum remove sendmail
[root@nagios ~]# rm -rvf /usr/sbin/sendmail
[root@nagios ~]# vim /etc/ssmtp/ssmtp.conf

root=nagadmin@geekinlinux.com
mailhub=mail.geekinlinux.com:25
rewriteDomain=geekinlinux.com
AuthUser=nagadmin
AuthPass=password
FromLineOverride=YES

[root@nagios ~]# ln -s /usr/sbin/ssmtp /usr/sbin/sendmail

check dns, status should be NOERROR and ANSWER must be other than 0, like below. see setting up DNS server.

[root@nagios ~]# dig geekinlinux.com mx

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15065
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2

[root@nagios ~]# dig gmail.com mx

[root@hawk VM-Servers]# dig gmail.com MX

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> gmail.com MX
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46293
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 4, ADDITIONAL: 3


since our destinations mail id is gmail in this example, i also checked dig for gmail.com. Now configuring Nagios.
[root@nagios ~]# vim /usr/local/nagios/etc/objects/contacts.cfg

define contact {
        contact_name       nagadmin
        use                generic-contact
        alias              Nagios Admin
        email              gil@gmail.com
}

define contactgroup{
       contactgroup_name    admins
       alias                Nagios Administrators
       members              nagadmin
}
 
Now you have to add this contact in each and every host file as shown below at the end of define host column.

[root@nagios ~]# vim /usr/local/nagios/etc/servers/linux-servers/web.cfg

define host{
        use               linux-server 
        host_name         web
        alias             apache
        address           192.168.85.10
        contact_groups    admins
}

Try stopping any service and check if Nagios could send e-mail alert for the failed service/host or not.