Setting Up Email Alerts for Network Monitoring with Nagios

About Installation and Configuration Nagios, please refer to Nagios - Server Monitoring Scheme

Setting Up Email Alerts

What good is a network monitoring tool if you have to sit at a monitor and constantly be watching and waiting for trouble to occur. What you need is a monitoring system that will alert you when something is amiss. It is possible to set Nagios up for this feature. And it doesn't take too much time and effort to pull off. I will say that you must have a working email system up and running.

Nagios send warning messages is to use the local smtp service, you can view commands.cfg mail order regarding the definition, use the machine's mail command, which need to open the machine smtp service, in order to be safe on the firewall settings reject other machines connected port 25 of the machine.

Now we have a mail server inside the network, so they requested to use this existing mail server, do not turn the unit smtp service, which requires the use of third-party software to redefine the command sendEmail.

SendEmail is a lightweight, command line SMTP email client. If you have the need to send email from a command line, this free program is perfect: simple to use and feature rich. It was designed to be used in bash scripts, batch files, Perl programs and web sites, but is quite adaptable and will likely meet your requirements. SendEmail is written in Perl and is unique in that it requires NO MODULES. It has an intuitive and flexible set of command-line options, making it very easy to learn and use.

sendEmail Official Website caspian.dotconf.net/menu/Software/SendEmail

Install sendEmail

$ sudo apt-get install sendemail

By defaule, sendEmail has been installed in /usr/bin/sendEmail directory.

Using sendEmail to send a E-mail, for example:

$ sendemail -f [email protected] -t [email protected] -s smtp.mail.com -u "subject" -xu [email protected] -xp password -m contents

or

/usr/bin/sendEmail -f [email protected] -t [email protected] -s smtp.mail.com -u "subject" -xu [email protected] -xp password -m contents

Parameters explained

-f sender's mailbox
-t recipient's mailbox
-s SMTP server's domain name or ip
-u subject of the message
-xu SMTP authentication user name
-xp SMTP authentication password
-m message content

The configuration file you will be using is /etc/nagios3/conf.d/contacts_nagios2.cfg. Although we are working with Nagios3, the "2" in the configuration file name is correct. Within this file you will find a section that looks like:

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

The USERNAME you see above will be the text you need to configure for your alerts. If you need more than one email address to be alerted, you have to add a defined for each user. Most of the definitions above will be pretty obvious. The service_notification flags are defined as such:

w = notify on warning states
c = critical states
r = recovery
f = start/stop of flapping
d = notify on down states
u = notify on unreachable states
s = notify on stopped states

You can pick and choose what states you want to be alerted for.

Once you have edited this file, save it, close it, and restart Nagios with the command:

$ sudo /etc/init.d/nagios3 restart

You are now ready to move on. The next section will be to define a contact group. Contact groups allow you to group people together so it is easier to alert specific people to certain events. This way you can have web-admins, file-server-admins, firewall-admins, and so on. Each group would have a specific user (or users) associated with it who would be alerted if a problem arises.

Go back to the same file you were just editing and look for the section labeled CONTACT GROUPS. In this section you will define a group like so:

define contactgroup {
     contactgroup_name  GROUPNAME
     alias GROUP ALIAS
     members USERNAME1, USERNAME2
}

All fields in BOLD are user specific.

Once you have defined all of your groups, save that file and close it. Now you have to attach groups to services so those groups will be alerted when something is wrong with their specific service. To do this open up the file /etc/nagios3/conf.d/services_nagios2.cfg. In this file you will find a few pre-defined groups (HTTP, SSH, and PING). Let's say you created a contact group called Web-Admins and want to associate that group with all HTTP services. To do this look for the section:

define hostgroup {
    hostgroup_name http-servers
    service_description  HTTP
    check_command  check_http
    use generic-service
    notification_interval 0
    }

To this section add the following line:

contact_groups Web-Admins

Because nagios to use sendmail to send a warning message, so we should modify commands.cfg mail order regarding the definition, we can now modify the notify-by-email of this order, like this.

###############################################################################
# COMMANDS.CFG - SAMPLE COMMAND DEFINITIONS FOR NAGIOS
###############################################################################

################################################################################
# NOTIFICATION COMMANDS
################################################################################

# '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/sendEmail -f [email protected] -t $CONTACTEMAIL$ -s smtp.mail.com -xu [email protected] -xp password -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
}
[...]

Save the file and close it. Now restart Nagios again and your monitoring system will begin sending out any HTTP errors to everyone associated with the Web-Admin group.

0.00 avg. rating (0% score) - 0 votes