Scaleway Documentationtutorials
configure nagios monitoring

Jump toUpdate content

Configure a Nagios Monitoring System

Reviewed on 10 May 2021Published on 19 June 2018
  • compute
  • messaging
  • apache
  • sentry
  • error
  • error-tracking

Monitor your server with Nagios

Requirements:

This tutorial will explain how to configure a Nagios monitoring with an Apache web server to monitor your servers. It is an open source monitoring system which can automatically alert you in case of a server disfunction.

Installing Nagios

In this tutorial, we will install Nagios from its source to make sure that we have the latest version of the tool.

  1. Update the system and install the required packages:

    sudo apt-get update && sudo apt-get upgrade
    sudo apt-get install wget build-essential apache2 php apache2-mod-php7.0 php-gd libgd-dev sendmail unzip
  2. Create a user to run Nagios:

    useradd nagios
    groupadd nagcmd
    usermod -a -G nagcmd nagios
    usermod -a -G nagios,nagcmd www-data
  3. Download the sources of Nagios, untar them and enter the directory:

    wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.4.tar.gz
    tar -xzf nagios*.tar.gz
    cd nagios-*
  4. Before compiling the software, define the user and group to use:

    sudo ./configure --with-nagios-group=nagios --with-command-group=nagcmd
  5. Compile the software:

    sudo make all
  6. Once it has been compiled, run the following make commands to install the application, init scripts and configuration files:

    sudo make install
    sudo make install-commandmode
    sudo make install-init
    sudo make install-config

Installing Apache

We will use Apache as Webserver for the Nagios interface. Nagios provides a sample configuration file, that we will use.

  1. Copy the configuration file to the Apache directory:

    sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
  2. Install the Nagios Plugins to have a set of tools to monitor your different services:

    sudo apt-get install build-essential libssl-dev gcc
    wget https://nagios-plugins.org/download/nagios-plugins-2.2.0.tar.gz
    tar -xzf nagios-plugins*.tar.gz
    cd nagios-plugins*
  3. Configure, compile and install them with the following commands:

    sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
    sudo make
    sudo make install

Nagios and the required plugins are installed.

Configuring Nagios

To monitor servers, we have to enable it in the file /usr/local/nagios/etc/nagios.cfg.

  1. Uncommment the line cfg_dir=/usr/local/nagios/etc/servers and save the file.

  2. Create the folder to store configurations:

    mkdir -p /usr/local/nagios/etc/servers
  3. Configure the contact email address in the file /usr/local/nagios/etc/objects/contacts.cfg:

    define contact{
    contact_name nagiosadmin ; Short name of user
    use generic-contact ; Inherit default values from generic-contact template (defined above)
    alias Nagios Admin ; Full name of user
    email nagios@localhost ; <<**\*** CHANGE THIS TO YOUR EMAIL ADDRESS **\*\***
    }

Configuring Apache

  1. Start by enabling the required Apache modules:

    sudo a2enmod rewrite
    sudo a2enmod cgi
  2. Create a nagiosadmin user for the web interface with the htpasswd command:

    sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
  3. Enable the Apache vHost:

    sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/

Configuring the systemd Script

By default Nagios does not provide a systemd configuration file, so we have to create one by ourself.

This file contains the information where systemd can find the Nagios executable and configuration files and when to start it.

  1. Create the file:

    sudo nano /etc/systemd/system/nagios.service
  2. Put the following content in the file:

    [Unit]
    Description=Nagios
    BindTo=network.target

    [Install]
    WantedBy=multi-user.target

    [Service]
    Type=simple
    User=nagios
    Group=nagios
    ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
  3. Save the file and enable the service:

    sudo systemctl enable /etc/systemd/system/nagios.service
  4. Start the applications:

    sudo systemctl restart apache2
    sudo systemctl start nagios

    Your Nagios server is ready, and you can access the interface at http://your.server.ip.address/nagios.

    We use the nagiosadmin user and the password that you have created previously to log in.

  5. Once you are connected, click Hosts on the left to see what Nagios is monitoring. By default it will only monitor the local host.

Monitoring a Server with the NPRE Service

The NPRE service (Nagios Remote Plugin Executor) is an add-on that allows you to execute Nagios commands on a remote Linux servers. It is basically used to gather “local” information (like disk usage, RAM usage, CPU usage) of a remote machine. As these information are not broadcasted on the Internet, an agent has to run on the server which reports to the Nagios server.

Note:

These steps have to be done on the remote server.

  1. Install the software on the remote server:

    sudo apt-get install autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext
  2. Download and unpack NRPE:

    cd /tmp
    wget --no-check-certificate -O nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/archive/master.tar.gz
    tar xzf nrpe.tar.gz
  3. Compile it with the following commands:

    cd /tmp/nrpe-master/
    sudo ./configure --enable-command-args
    sudo make all
  4. Create users and groups:

    sudo make install-groups-users
  5. Install the configuration files:

    sudo make install-config
  6. Install NRPE:

    sudo make install
  7. Install the service, so NPRE can be managed by systemd:

    sudo make install-init
    sudo systemctl enable nrpe.service
  8. Edit the file /usr/local/nagios/etc/nrpe.cfg. Replace IP_of_your_Nagios_Server with the IP address of your Nagios host:

    allowed_hosts=127.0.0.1,IP_of_your_Nagios_Server
  9. Restart the service:

    sudo systemctl start nrpe.service
  10. Download and install the Nagios plugins:

    wget http://www.nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
    tar xfz nagios-plugins-2.2.1.tar.gz
    cd nagios-plugins-2.2.1/
    sudo ./configure
    sudo make all
    sudo make install
    Note:

    The following steps have to be done on the Nagios server

  11. On the Nagios server, create a configuration file in the directory /usr/local/nagios/etc/servers/ for each remote host that we want to monitor:

    nano /usr/local/nagios/etc/servers/remote_host.cfg
  12. Replace remote_host with the name of the remove server and put the following content in the file:


    # Remote Host configuration file

    define host {
    use linux-server
    host_name remote_host
    alias Remote Host
    address 163.172.168.167
    register 1
    }

    define service {
    host_name remote_host
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
    max_check_attempts 2
    check_interval 2
    retry_interval 2
    check_period 24x7
    check_freshness 1
    contact_groups admins
    notification_interval 2
    notification_period 24x7
    notifications_enabled 1
    register 1
    }

    define service {
    host_name remote_host
    service_description Disk Usage
    check_command check_local_disk!20%!10%!/
    max_check_attempts 2
    check_interval 2
    retry_interval 2
    check_period 24x7
    check_freshness 1
    contact_groups admins
    notification_interval 2
    notification_period 24x7
    notifications_enabled 1
    register 1
    }

    define service {
    host_name remote_host
    service_description SSH Service
    check_command check_ssh
    max_check_attempts 2
    check_interval 2
    retry_interval 2
    check_period 24x7
    check_freshness 1
    contact_groups admins
    notification_interval 2
    notification_period 24x7
    notifications_enabled 1
    register 1
    }

    This file will monitor if the remote host replies on ping, the disk usage of the host and if the SSH service is up. You can find more configuration examples in the file /usr/local/nagios/etc/objects/commands.cfg. Nagios allows you to monitor a wide range of services.

  13. Save the file and restart the application:

    sudo systemctl restart nagios

    The remote server will appear in your Nagios interface and you can see the status of the monitored services:

Nagios is widely used because of its flexibility and versatility. Don’t hesitate to refer to the Documentation of the software to find more out about further configuration options.