Wednesday 9 March 2016

Setting up email notifications for NAGIOS Core with Exchange

For this demonstration I will be configuring NAGIOS to route it's notifications through an Exchange server.

We should firstly define a contact definition - this is simply a set of information that tells NAGIOS when and where to send notifications - once we have defined a contact we can then associate it with hosts, hostgroups, service groups and so on.

So we should edit the contacts definitions:

sudo vi /usr/local/nagios/etc/objects/contacts.cfg

define contact {
        contact_name                            Administrator1
        alias                                   Administrator 1
        email                                   [email protected]
        service_notification_period             24x7
        service_notification_options            w,u,c,r,f,s
        service_notification_commands           notify-service-by-email
        host_notification_period                24x7
        host_notification_options               d,u,r,f,s
        host_notification_commands              notify-host-by-email
}

We can also create a group as well to house multiple recipients:

define contactgroup{
        contactgroup_name                   MyContactGroup
        alias                               GroupAlias
        members                             Administrator1,Administrator2,Administrator3
}

We can then define this contact within - lets say a specific host:

define host{
   name                         mailserver
   use                          linux-server
   notifications_enabled        1
   notification_period          24x7
   notification_interval        120
   notification_options         d,u,r,f,s
   register                     0
   contact_groups              Group1
   contacts                     Contact1
   }
 
Our next task is too setup a MTA so we can send our email to the contacts specified - now as I was using the 'Core' edition of CentOS I had instal the 'mail' command:

yum install mailx mutt

The mail command that is issued by NAGIOS is defined within:

sudo vi /usr/local/nagios/etc/objects/commands.cfg

We are looking specifically at the following lines:

command_line  /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time:  $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

command_line  /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$

Although the command being invoked e.g.:

/bin/mail -s "testing subject" [email protected] < message.txt

does not route the mail through the smarthost and will attempt to deliver it directly - so we must define the '-S' switch to manually specify the smarthost (Exchange.) You should also ensure that a receieve connector is setup and configured correctly in your Exchange environment.

To test your SMTP config we can issue something like:

echo Test | mailx -v -s "Test Subject" -S smtp=mail.domain.com:25 [email protected]

I end up changing the 'command_line' variables (as above) to:

command_line  /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time:  $LONGDATETIME$\n" | /bin/mail -S smtp=mail.myhost.com:25 -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

command_line  /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -S smtp=mail.myhost.com:25 -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$

Finally restart nagios and trigger an alert:

sudo service nagios restart


0 comments:

Post a Comment