Configuring Nagios Alerts on Debian 10 and Ubuntu 20.04

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Nagios, a popular tool for monitoring servers, comes with a robust web interface to help you effectively manage your server. Its dashboard makes it relatively easy to check in on the hosts and services running on your machine and quickly learn of any issues.

However, you can get even more out of Nagios by setting it up to deliver alerts and notifications when you need them. This guide provides instructions for setting up email alerts from Nagios and configuring regular status updates via Internet Relay Chat (IRC).

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.

  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

  3. Install and configure Nagios. Follow the steps in the Install Nagios on Debian 10 and Ubuntu 20.04 guide. Alternatively, the official Nagios Installation Guide provides steps for installing Nagios from source code on a wide range of Linux distributions.

  4. Replace example.com throughout this guide with your machine’s domain name, and replace fqdn.example.com with your machine’s fully qualified domain name (FQDN).

Note
This guide is written for non-root users. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, see the Linux Users and Groups guide.

Set Up Nagios Email Alerts

Email restrictions on the Linode Platform
In an effort to fight spam originating from our platform, outbound connections on ports 25, 465, and 587 are blocked by default on Compute Instances for some new accounts. These restrictions prevent applications from sending email. If you intend to send email from a Compute Instance, review the Send Email on the Linode Platform guide to learn more about our email policies and to request the removal of these restrictions.
Note
This guide also provides instructions for configuring local emails, which are not subject to the above restriction. Under this configuration, emails are only delivered to users on the same machine as Nagios.

Install Email Services

  1. Install packages for handling emails.

     sudo apt install mailutils postfix
    
    • You should be prompted for information about your mail server configuration. If you are not or if you need to re-enter the information, you can run the following command after the installation has finished:

      sudo dpkg-reconfigure postfix
      
    • Select Internet Site from the options that appear.

    Postfix configuration type

    • Enter the domain name to be used for your email addresses. This may be either your machine’s fully qualified domain name (FQDN) or base domain name. It can be localhost if you are configuring Postfix for local emails only.

    Postfix system mail name

    • Provide the username of your primary user on the machine.

    Entering primary user for Postfix

    • When prompted for destinations for which to accept mail, you can leave the field blank to only accept emails directed to the system mail name. If you have a domain name configured and want to ensure that emails are accepted for a wider range of domains, you can enter the following: localhost, example.com, fqdn.example.com, mail.example.com, localhost.example.com.

    Postfix accepted destinations

  2. Test the email configuration with the following command below; replace example-user with the username of a local user that you can log in as.

     echo "Body of the test email." | mail -s "Test Email" example-user@localhost
    
    • Log in as example-user, and run the mail command. Verify that the user received the test email.

    • You can use a similar command to the one above to test outbound emails. Replace example-user@localhost with an external email address to which you have access.

Configure Nagios

  1. Using your preferred text editor, open the Nagios commands configuration file, located at /etc/nagios4/objects/commands.cfg. Identify the command definitions for notify-host-by-email and notify-service-by-email. For each, verify that the location of the mail binary is /usr/bin/mail, as in the following example:

    File: /etc/nagios4/objects/commands.cfg
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    # '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/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
            }
    
    # '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$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
            }
        
  2. Open the Nagios contacts.cfg configuration file, located at /etc/nagios4/objects/contacts.cfg. Identify the nagiosadmin contact definition. In the email field, enter the email address where you would like to receive Nagios notifications.

    You can configure Nagios to send notifications to a local mailbox using example-user@localhost, where example-user is the local user you want to receive Nagios alerts.

  3. Open the Nagios templates.cfg configuration file, located at /etc/nagios/objects/templates.cfg. Find the generic-host definition, and ensure that it has the following line:

    File: /etc/nagios4/objects/templates.cfg
    1
    2
    
    contact_groups      admins
        

    Similarly, find the generic-service definition, and ensure that the same line is present.

  4. Restart the Nagios service.

     sudo systemctl restart nagios4
    

Test Nagios Email Alerts

You can use the following steps to verify that Nagios is delivering alerts by email.

  1. In a web browser, navigate to your Nagios interface, and log in as the nagiosadmin user.

  2. Select Services from the menu on the left, and select any of the services for which notifications are not disabled.

  3. Choose the Send custom service notification option from the Service Commands menu on the right. Enter a comment, and select Commit.

  4. Check the email inbox that you configured Nagios to send notifications to, and verify that you received your custom notification.

    If your Nagios alerts are sent to a local user, log in as that user, and use the mail command to check the user’s inbox.

Set Up Nagios IRC Alerts

This guide uses NagIRCBot, an application designed to routinely read Nagios’s status information and post updates to an Internet Relay Chat (IRC) channel.

Build and Install NagIRCBot

  1. Install the requisites for building NagIRCBot.

     sudo apt-get install -y autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext
    
  2. Navigate to the NagIRCBot Exchange directory. Replace the version number in the following examples with the version you find.

  3. In the /opt directory, download the package containing the files needed to build the bot; extract the files, and change them into the resulting directory.

     cd /opt
     sudo wget https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/nagircbot/0.0.33-2/nagircbot_0.0.33.orig.tar.gz
     sudo tar -zxvf nagircbot_0.0.33.orig.tar.gz
     cd nagircbot-0.0.33
     sudo make
     sudo make install
     sudo cp nagircbot /usr/local/bin
    
  4. Build the NagIRCBot application.

     sudo make
     sudo make install
    

Run and Test NagIRCBot

  1. Use a version of the following command to start NagIRCBot.

     sudo nagircbot -f /var/lib/nagios4/status.dat -s irc.example.com:6667 -c \#example-channel -C -n nagircbot -u nagircbot-username -U NagIRCBot-Name -I 900
    

    Replace irc.example.com with the hostname of the IRC network or localhost for a local IRC network. Replace nagircbot, nagircbot-username, and NagIRCBot-Name with the nickname, username, and real name, respectively, to be used for the bot. Change the 900 as needed; it defines how frequently, in seconds, the bot checks and sends Nagios status information.

  2. Connect to the IRC channel, and verify that you are receiving status updates at the expected interval.

  3. By default, NagIRCBot must be re-initiated using the command described above after each system reboot. Refer to the Use systemd to Start a Linux Service at Boot or the Schedule Tasks with Cron guide for instructions on how to schedule tasks to run automatically.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.