This article is an update of our old article “How to install Nagios local server on Ubuntu 16.04“.
In the article, we will describe how to install Nagios local server – step by step. You will need the following packages – Nagios 4.3.4 and Nagios Plugins 2.2.1, and Ubuntu 18.04.
It is assumed, that the user is familiar with Linux and already has a workstation with Ubuntu 18.04.
1. Initial settings
1.1. Login as a root
Some commands require the user to have administrative rights, therefore before downloading and installing the packages, the following command should be used in the console:
sudo su
A password will be required, after that, the console enters root mode as shown in the picture.
After this execute follows:
sudo apt-get update
sudo apt-get upgrade
1.2. Packages installation
You should install also:
sudo apt-get install php7.2
sudo apt-get install php7.2-gd
sudo apt-get install build-essential
sudo apt-get install apache2
sudo apt-get install libapache2-mod-php7.2
1.3. Creating a new user
It is necessary to create a new user and group to which it belongs. Use the following commands:
sudo useradd nagios
sudo groupadd nagios
sudo /usr/sbin/usermod -G nagios nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
usermod -a -G nagcmd www-data
1.4. Creating a directory for downloading Nagios packages
mkdir ~/downloads
cd ~/downloads
sudo wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.4.tar.gz
sudo wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
2. Inastalation of Nagios 4.3.4
2.1. Nagios configuration
cd ~/downloads
tar zxfv nagios-4.3.4.tar.gz
cd nagios-4.3.4/
./configure –with-command-group=nagcmd
After a successful Nagios configuration, the user should see the following message:
2.2. Nagios installation
make all
sudo make install
sudo make install-init
sudo make install-config
sudo make install-commandmode
2.3. WEB configuration files
A directory is created to install the web configuration files for Nagios:
sudo mkdir -p /etc/httpd/conf.d/nagios.conf
sudo make install-webconf
2.4. Post-installation configuration
sudo cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
sudo chown -R nagios:nagcmd /usr/local/nagios/libexec/eventhandlers
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
The last command checks Nagios configuration for errors. If none, the user will get to the following result:
2.5. Enabling Nagios web interface
sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-enabled/nagios.conf
sudo a2enmod cgi
sudo service apache2 restart
2.6. Starting Nagios process and setting password for Nagiosadmin
sudo /etc/init.d/nagios start
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
The user should set the password for nagiosadmin user of the web interface:
2.7. Setting system to run Nagios process on start up.
sudo ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios
3. Nagios initialization
Depending on the system configuration, the Nagios process may not be started. In this case, the following commands are executed:
3.1. The system file nagios.service creation
cd /etc/systemd/system/
sudo nano nagios.service
The following lines are written in the system file:
[Unit]
Description=Nagios
BindTo=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=nagios
Group=nagios
Type=simple
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
To save changes you should use Ctrl+X combination, after this Y and finally ENTER.
3.2. The following configuration file is changed
sudo nano /etc/init.d/nagios
3.3. New lines in the configuration file
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
3.4. Changing permissions and starting Nagios
sudo chmod +x /etc/init.d/nagios
sudo service nagios start
4. Changing the email address for program messages
The following configuration file is changed.
nano /usr/local/nagios/etc/objects/contacts.cfg
The address nagios@localhost is replaced with the desired email address. In order to save the changes press Ctrl+X, then Y and finally ENTER. The document is saved under the same name.
5. Compiling and installing Nagios plug-ins
cd ~/downloads
tar zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1
./configure –with-nagios-user=nagios –with-nagios-group=nagios
make
sudo make install
sudo service nagios restart
6. Log into Nagios through the web browser
The user interface can be accessed from:
localhost/nagios/
127.0.0.1/nagios
The user should write the username (nagiosadmin) and the password (set in step 2.6).
7. Adding a host to Nagios
In order to add a host to the system, several configuration files should be changed. The changes are made by using an editor, in the console. It is recommended that the Nagios process is stopped during the changes:
sudo service nagios stop
A good approach would be after every change made in a system file, to check the configuration using the following command:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
This way, if an error occurs during configuration, finding and fixing it comes down to a check only of the last edited configuration file.
The first change is made in the main Nagios configuration file. As the basic Nagios installation includes monitoring of the local host only (the local computer), Nagios should be permitted to access other hosts as well – switches, routers, and other servers.
sudo nano /usr/local/nagios/etc/nagios.cfg
After this command, the editor will be opened from the following line:
#cfg_file=/usr/local/nagios/etc/objects/switch.cfg
The symbol # should be deleted, which will allow the Nagios process to access the configuration file, where the host data is recorded.
The second step requires adding information for the new host. This is done in the following configuration file:
sudo nano /usr/local/nagios/etc/objects/switch.cfg
At the end of this configuration file the following lines are added:
define host{
use generic-switch
host_name TCW220
alias Teracom Ethernet Controller TCW220
address 192.168.1.2
}
After saving the changes done to the configuration file, the Nagios process must be restarted:
sudo service nagios restart
The new host is now available in the web interface:
The basic installation of Nagios includes only host tracking. In order to add another service, a plugin is needed to execute the service and show its result in the user interface.