How to set-up and configure of Nagios Core on Ubuntu 16.04?
Nagios, or now Nagios Core is an open source software application that can monitor infrastructure, networks and systems. Nagios offers monitoring and alerting services for servers, switches, applications and services. It first alerts users when things go wrong and alerts them a second time when the problem has been resolved.
• Ubuntu 64 bit server with 2GB of RAM, it will be named nagios-server
• Ubuntu 64 bit client with 1 GB of RAM, it will be named nagios-client
Nagios requires the build-essentials package for the compilation, LAMP (Apache, PHP, MySQL) for the Nagios web interface and Sendmail to send alerts from the server. To install all those packages, run this command:
sudo apt-get install wget build-essential apache2 php apache2-mod-php7.0 php-gd libgd-dev sendmail unzip
To make Nagios running, we need to create a user for nagios. Our user here will be "nagios" and a group named "nagcmd". And add the user "nagios" to the group "nagcmd":
useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagios,nagcmd www-data
First we need to download Nagios:
cd ~
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.0.tar.gz
tar -xzf nagios*.tar.gz
cd nagios-4.2.0
We need to set the configuration to make the installation with the user and group nagios:
./configure --with-nagios-group=nagios --with-command-group=nagcmd
Now let's install:
make all
sudo make install
sudo make install-config
sudo make install-init
sudo make install-commandmode
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
Copy evenhandler directory to the nagios directory:
cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
Download and extract the Nagios plugins:
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install
Let's install Nagios plugins now:
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install
What you need to know now, is that Nagios is installed in /usr/local/nagios. We need to configure it to add servers:
vim /usr/local/nagios/etc/nagios.cfg
Check the line 51 and uncomment this line:
cfg_dir=/usr/local/nagios/etc/servers
Configure the email address you want Nagios to send alerts to. It can be configured in /usr/local/nagios/etc/objects/contact.cfg, line 34:
email nagios@localhost
Let's configure apache to make Nagios run:
sudo a2enmod rewrite
sudo a2enmod cgi
We can use htpasswd to create a login page for Nagios:
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
And type your password.
Now we enable Nagios VHost:
sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/
cp /etc/init.d/skeleton /etc/init.d/nagios
vim /etc/init.d/nagios
Add inside:
DESC="Nagios"
NAME=nagios
DAEMON=/usr/local/nagios/bin/$NAME
DAEMON_ARGS="-d /usr/local/nagios/etc/nagios.cfg"
PIDFILE=/usr/local/nagios/var/$NAME.lock
Now let's start Nagios:
chmod +x /etc/init.d/nagios
service apache2 restart
/etc/init.d/nagios start
Now you can access your server by doing, in my case my IP is 192.168.1.22 so I shall do:
http://192.168.1.22/nagios
Nagios Remote Plugin Executor (NRPE) is a Nagios agent that allows remote system monitoring using scripts that are hosted on the remote systems. It allows for monitoring of resources such as disk usage, system load or the number of users currently logged in. Nagios periodically polls the agent on remote system using the check_nrpe plugin.
Here, our servers will have these IP:
Nagios Server IP: 192.168.1.22
Ubuntu Host IP: 192.168.1.23
On our Nagios server, do:
sudo apt install nagios-nrpe-plugin
It will download the check_nrpe plugin used by Nagios to communicate with remote servers.
We need to copy the NRPE plugin in our Nagios plugin directory:
cp /usr/lib/nagios/plugins/check_nrpe /usr/local/nagios/libexec
Now ssh on your remote server and let's install and configure NRPE
sudo apt install nagios-nrpe-server
vim /etc/nagios/nrpe.cfg
Add your Nagios server address to the allowed server like this:
Now let's go back on our Nagios server.
vim /usr/local/nagios/etc/servers/nagios_client.cfg
Add these lines:
define host {
use linux-server
host_name nagios-client
alias nagios-client
address 192.168.1.23
}
define service {
host_name nagios-client
use generic-service
service_description PING
check_command check_ping!100.0,20%!500.0,60%
contact_groups admins
}
define service {
host_name nagios-client
use generic-service
service_description Check Users
check_command check_local_users!20!50
contact_groups admins
}
define service {
host_name nagios-client
use generic-service
service_description Local Disk
check_command check_local_disk!20%!10%!/
contact_groups admins
}
define service {
host_name nagios-client
use generic-service
service_description Check SSH
check_command check_ssh
contact_groups admins
}
define service {
host_name nagios-client
use generic-service
service_description Total Process
check_command check_local_procs!250!400!RSZDT
contact_groups admins
}
Now you just have to restart apache and Nagios:
sudo service apache2 restart
/etc/init.d/nagios restart
Go to your Nagios webpage in host and see:
Just wait a little bit, to let Nagios check your remote server and this is it!
If you wonder how to install some nagios services, let's see it here. We want to check the memory of our Nagios-client, to do it, go to this github here, copy the script on your remote server and make it excutable with chmod, like this:
chmod +x check_mem.pl
Once this is done, put it in the NRPE service library:
mv check_mem.pl check_mem
mv check_mem /usr/lib/nagios/plugins
After:
vim /etc/nagios/nrpe.cfg
Add this line:
command[check_mem]=/usr/lib/nagios/plugins/check_mem -fC -w 5 -c 4
This line uses the script to send a warning alert if the memory of your server is under 5% and a critical alert if it goes under 4%. -C counts cache memory.
Restart the NRPE service:
sudo service nagios-nrpe-restart
Now let's go back on our Nagios server. Go to your server configuration in:
vim /usr/local/nagios/etc/servers/nagios_client.cfg
Add these lines:
define service{
use generic-service
host_name nagios-client
service_description Memory
check_command check_nrpe!check_mem
contact_groups admins
}
Restart Nagios:
/etc/init.d/nagios restart
If you refresh your Nagios webpage, you'll see that now the memory service is monitored.
Create a new file and edit it:
vim /usr/local/nagios/etc/objects/hostgroup.cfg
Add these lines with our client inside:
define hostgroup{
hostgroup_name client
alias nagios client
members nagios-client,
}
Now edit the Nagios config file:
vim /usr/local/nagios/etc/nagios.cfg
Let's add this line to include our futur file in Nagios:
cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg
Restart nagios:
/etc/init.d/nagios restart
And here we have:
And voila, your server is now in a group!