Nagios configure alerts

Configuring alerts

When we have a working Nagios configuration, and we can see any problems in the web interface. However, it is more useful if we get notifications instead. By default, Nagios assumes that our system has a /bin/mail command using which it can deliver emails. However, in this, we will deliver our emails through SMTP, with a service like Gmail.

First, if you want to use a service like Gmail or yahoo, ensure that access for “less secure apps” is enabled. This is required so that non-interactive applications can send out mail.

Then, open the /usr/local/nagios/etc/objects/contacts.cfg file. Inside this file, there will be a contact block with an email directive. Find the line and add the email address where you want alerts to be delivered.

define contact{

        ......

        email                           

}

Save this file and exit the editor. The next step is to download a command-line SMTP client

If you use Ubuntu, you have to install a few dependencies first.

#apt install php php-cli php-gd php-curl php-zip php-intl php-mbstring php-xml

However, if you are on CentOS, you should run these commands to upgrade your PHP version, and then the necessary dependencies:

#yum install yum-utils http://rpms.remirepo.net/enterprise/remi-release-7.rpm

#yum-config-manager --enable remi-php70

#yum install php php-cli php-gd php-curl php-zip php-intl php-mbstring php-xml

Download the SMTP client, and extract it

#wget https://github.com/boolean-world/smtp-cli/archive/master.zip

#unzip -d /opt master.zip

we will install a few things that the SMTP client needs. Run the following commands:

#cd /opt/smtp-cli-master

#composer install

In this directory, create a config.json file, with the sender’s details. This includes the sending SMTP server and the sender’s email address. For example, if you want to send emails from Gmail, the configuration file would look like this:

{

 "host": "smtp.gmail.com",

 "username": "[email protected]",

 "password": "the-password-of-the-account",

 "secure": "tls",

 "port": 587

}

Now, open the /usr/local/nagios/etc/objects/commands.cfg file. Here, find the block with the command_name value set to notify-host-by-email. This block contains commands that are used to send out emails when there is a problem with a host. Below the command_name line, there is a command-line that looks like this:

#command /usr/bin/printf "%b" ... | /bin/mail -s ...

Replace the part containing /bin/mail -s with /opt/smtp-cli-master/smtp-cli.php. Repeat this process for the block containing the notify-service-by-email.

Now we can test the setup by triggering a critical condition. For example, if the monitored host has 30 GB disk space available, you can fill it up to 28 GB by running the following command:

#dd if=/dev/zero of=zero.bin bs=1M count=$((28*1024))


we have to wait for a few minutes till Nagios performs the check and sends out an alert. Later, when we get a mail, we can simply remove the zero.bin file.




Relevant Blogs:

Nagios triggers 

Nagios installation and configuration 

Beginners guide for Jenkins pipeline 

chef cookbooks

Recent Comments

No comments

Leave a Comment