NPRE is deprecated. Use this feature at your own risk.
Configuring a Nagios Monitoring System on Scaleway
- Nagios
- monitoring
- Apache
Monitoring your resources is an indispensable building block for the success of your SaaS or e-commerce application. An unnoticed system failure can have serious financial consequences and negatively impact your image. Therefore, you will learn by following this tutorial how to install and configure a Nagios monitoring application with an Apache web server to monitor your servers. It is an open-source monitoring system that can automatically alert you in case of a server dysfunction.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- An SSH key
- At least 2 Instances or 2 Elastic Metal servers
Installing Nagios
In this tutorial, we will install and configure Nagios from its source to make sure that we have the latest version of the tool.
- Update the system and install the required packages:
apt update && apt upgradeapt install wget build-essential apache2 php apache2-mod-php7.0 php-gd libgd-dev sendmail unzip
- Create a user to run Nagios:
useradd nagiosgroupadd nagcmdusermod -a -G nagcmd nagiosusermod -a -G nagios,nagcmd www-data
- Download the sources of Nagios, unpack the downloaded file and enter the directory:
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.10.tar.gztar -xzf nagios*.tar.gzcd nagios-*
- Before compiling the software, define the user and group to use:
./configure --with-nagios-group=nagios --with-command-group=nagcmd
- Compile the software:
make all
- Once it has been compiled, run the following
make
commands to install the application, init scripts and configuration files:make installmake install-commandmodemake install-initmake install-config
Installing Apache
In this tutorial, we use Apache as a web server for the Nagios interface. Nagios provides a sample configuration file, that we will use.
- Copy the configuration file to the Apache directory:
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
- Install the Nagios Plugins to have a set of tools to monitor your different services:
apt install build-essential libssl-dev gccwget https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.4.3/nagios-plugins-2.4.3.tar.gztar -xzf nagios-plugins*.tar.gzcd nagios-plugins*
- Configure, compile, and install them with the following commands:
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-opensslmakemake install
Nagios and the required plugins are installed.
Configuring Nagios
To monitor servers, we have to configure Nagios by editing the file /usr/local/nagios/etc/nagios.cfg.
- Uncomment the line
cfg_dir=/usr/local/nagios/etc/servers
and save the file. - Create the folder to store configurations:
mkdir -p /usr/local/nagios/etc/servers
- Configure the contact email address in the file
/usr/local/nagios/etc/objects/contacts.cfg
:define contact{contact_name nagiosadmin ; Short name of useruse generic-contact ; Inherit default values from generic-contact template (defined above)alias Nagios Admin ; Full name of useremail nagios@localhost ; <<**\*** CHANGE THIS TO YOUR EMAIL ADDRESS **\*\***}
Configuring Apache
We are using the Apache web server as an application to serve the Nagios web interface.
- Start by enabling the required Apache modules:
a2enmod rewritea2enmod cgi
- Create a nagiosadmin user for the web interface with the
htpasswd
command:htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin - Enable the Apache vHost:
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 ourselves.
This file contains the information where systemd can find the Nagios executable and configuration files and when to start it.
-
Create the file:
nano /etc/systemd/system/nagios.service -
Put the following content in the file:
[Unit]Description=NagiosBindTo=network.target[Install]WantedBy=multi-user.target[Service]Type=simpleUser=nagiosGroup=nagiosExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg -
Save the file and enable the service:
systemctl daemon-reloadsystemctl enable nagios.service -
Start the applications:
systemctl restart apache2.servicesystemctl start nagios.serviceYour 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.
-
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 remote Linux servers. It is used to gather “local” information (like disk usage, RAM usage, CPU usage) of a remote machine. As this information is not broadcasted on the Internet, an agent has to run on the server which reports to the Nagios server.
These steps have to be done on the remote server.
-
Install the software on the remote server:
apt install autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext -
Download and unpack NRPE:
cd /tmpwget --no-check-certificate -O nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/archive/master.tar.gztar xzf nrpe.tar.gz -
Compile it with the following commands:
cd /tmp/nrpe-master/./configure --enable-command-argsmake all -
Create users and groups:
make install-groups-users -
Install the configuration files:
make install-config -
Install NRPE:
make install -
Install the service, so NPRE can be managed by systemd:
make install-initsystemctl enable nrpe.service -
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 -
Restart the service:
systemctl start nrpe.service -
Download and install the Nagios plugins:
wget http://www.nagios-plugins.org/download/nagios-plugins-2.4.0.tar.gztar xfz nagios-plugins-2.2.1.tar.gzcd nagios-plugins-2.2.1/./configuremake allmake installNoteThe following steps have to be done on the Nagios server
-
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 -
Replace remote_host with the name of the remote server and put the following content in the file:
# Remote Host configuration filedefine host {use linux-serverhost_name remote_hostalias Remote Hostaddress 163.172.168.167register 1}define service {host_name remote_hostservice_description PINGcheck_command check_ping!100.0,20%!500.0,60%max_check_attempts 2check_interval 2retry_interval 2check_period 24x7check_freshness 1contact_groups adminsnotification_interval 2notification_period 24x7notifications_enabled 1register 1}define service {host_name remote_hostservice_description Disk Usagecheck_command check_local_disk!20%!10%!/max_check_attempts 2check_interval 2retry_interval 2check_period 24x7check_freshness 1contact_groups adminsnotification_interval 2notification_period 24x7notifications_enabled 1register 1}define service {host_name remote_hostservice_description SSH Servicecheck_command check_sshmax_check_attempts 2check_interval 2retry_interval 2check_period 24x7check_freshness 1contact_groups adminsnotification_interval 2notification_period 24x7notifications_enabled 1register 1}This file will monitor if the remote host replies on
ping
, the disk usage of the host, and if theSSH
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. -
Save the file and restart the application:
systemctl restart nagios.serviceThe 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. Do not hesitate to refer to the official documentation of the software to find more out about further configuration options.