Monday, June 29, 2009

nagios email notification in gmail problem and solution

step 1 : check in templates.cfg . The configuration should be similar to the one below(in objects folder in my case)

# Generic contact definition template - This is NOT a real contact, just a template!

define contact{
name generic-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
register 0
}

step 2 : check contacts.cfg

define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit from generic-conta
alias Bishal Acharya ; Full name of user

email abc@gmail.com

}


# We only have one contact in this simple configuration file, so there is
# no need to create more than one contact group.

define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}

Make sure we have contact and cotactgroup defined in contacts.cfg. In my case it is with my denominations.


step 3 : Check the commands.cfg file . Make sure we make /usr/bin/mailx in the configuration under Ubuntu systems. Also do install sendmail prior to doing this by using(sudo apt-get install sendmail) and check by command line that you can send mail by using command (mail abc@gmail.com) . If you successfully sent your mail then your nagios too can send mail or else we again need to troubleshoot.


# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $ADMINEMAIL$
}

# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
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$" | /usr/bin/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $ADMINEMAIL$
}



Make sure ADMINEMAIL has been set accordingly in nagios.cfg file as

admin_email=abc@gmail.com



step 4 : Time to check few things
check whether notifications are set or not in nagios.cfg file if not set it to 1, similarly also make notifications to 1 in services and host definitions, Nagios cant sent notifications unless you enable them.



Now reload your nagios and test by stopping one of the services like ssh or other anyone If you are lucky you will receive notifications according to the timing settings. If not check your log file.

Common problem :

Warning: Contact 'nagiosadmin' service notification command '/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: PROBLEM\n\nService: SSH\nHost: bishal\nAddress: 127.0.0.1\nState: CRITICAL\n\nDate/Time: Mon Jun 29 17:24:26 JST 2009\n\nAdditional Info:\n\nConnection refused" | /usr/bin/mail -s "** PROBLEM Service Alert: bishal/SSH is CRITICAL **" abc@gmail.com' timed out after 60 seconds

Solution for Nagios notification Timeout problem :
Go to nagios.cfg and increase the notification_timeout to some larger value, In my case i made it 600 from 60


service_check_timeout=60
host_check_timeout=30
event_handler_timeout=30
notification_timeout=600
ocsp_timeout=5
perfdata_timeout=5


Now again reload nagios and you should be alerted when any service goes down. Test it by stopping any service also we are notified when any service recovers. We may give our mobile phone email address so that we are alerted to our mobile phones when problem occurs instead of our computer.


Good Luck ! Comments are welcomed