Nagios is a free, open-source, and powerful monitoring tool that helps you to monitor your entire IT infrastructure, servers, services, and applications from the central location. It is based on a client-server architecture.
So you will need to install the Nagios agent on the client system and add it to the Nagios server for monitoring. If you are looking for a monitoring solution then Nagios is an essential tool for any production server environment.
Is there a way to do it by yourself? Of course there is. In this tutorial, we will show you how to install Nagios 4 and monitor your server on Ubuntu 20.04.
Table of Contents
Prerequisites
Two Ubuntu 20.04 VPS one for Nagios server and one for Nagios client.
Access to the root user account (or access to an admin account with root privileges)
Step 1: Log in to the Server & Update the Server OS Packages
First, log in to your Ubuntu 20.04 server via SSH as the root user:
ssh root@IP_Address -p Port_number
You will need to replace ‘IP_Address’ and ‘Port_number’ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.
Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:
apt-get update -y
apt-get upgrade -y
Step 2: Install Required Dependencies
Before starting, you will need to install some dependencies required to install Nagios in your system. You can install all of them with the following command:
apt-get install autoconf bc gawk dc build-essential gcc libc6 make wget unzip apache2 php libapache2-mod-php libgd-dev libmcrypt-dev make libssl-dev snmp libnet-snmp-perl gettext -y
Once all the dependencies are installed, you can proceed to download the Nagios 4 core.
Step 3: Download and Install Nagios 4
First, download the latest version of Nagios 4 from its official website using the following command:
Once the download is completed, extract the downloaded file with the following command:
tar -xvzf nagios-4.4.6.tar.gz
Next, change the directory to the extracted directory and configure it with the following command:
cd nagios-4.4.6
./configure --with-httpd-conf=/etc/apache2/sites-enabled
You should get the following output:
Nagios user/group: nagios,nagios
Command user/group: nagios,nagios
Event Broker: yes
Install ${prefix}: /usr/local/nagios
Install ${includedir}: /usr/local/nagios/include/nagios
Lock file: /run/nagios.lock
Check result directory: /usr/local/nagios/var/spool/checkresults
Init directory: /lib/systemd/system
Apache conf.d directory: /etc/apache2/sites-enabled
Mail program: /bin/mail
Host OS: linux-gnu
IOBroker Method: epoll
Web Interface Options:
------------------------
HTML URL: http://localhost/nagios/
CGI URL: http://localhost/nagios/cgi-bin/
Traceroute (used by WAP):
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.
Next, build the Nagios with the following command:
make all
Next, create a Nagios user and group and add it to the www-data group with the following command:
make install-groups-users
usermod -a -G nagios www-data
Next, run the following command to install Nagios binaries, daemon and command mode:
make install
make install-daemoninit
make install-commandmode
Next, install sample script config and Apache web configuration using the following command:
make install-config
make install-webconf
Next, enable the rewrite and cgi module with the following command:
a2enmod rewrite cgi
Next, restart the Apache service to apply the changes:
systemctl restart apache2
Step 4: Create a Nagios Admin User
Next, you will need to create a Nagios user to access the Nagios web interface. You can create it with the following command:
Once all the packages are installed, create a directory to store all configuration:
mkdir -p /usr/local/nagios/etc/servers
Next, edit the Nagios configuration file and define the server directory:
nano /usr/local/nagios/etc/nagios.cfg
Change the following line:
cfg_dir=/usr/local/nagios/etc/servers
Next, edit the Nagios resource.cfg and define the Nagios plugins path:
nano /usr/local/nagios/etc/resource.cfg
Change the following line:
$USER1$=/usr/lib/nagios/plugins
Save and close the file then edit the contacts.cfg and define your contact name and email address:
nano /usr/local/nagios/etc/objects/contacts.cfg
Change the following lines as per your needs:
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 ******
}
Save and close the file then edit the commands.cfg and define the nrpe check commands:
Finally, restart the Apache service to apply the changes:
systemctl restart apache2
Step 6: Access Nagios Web UI
Now, open your web browser and access the Nagios web interface using the URL http://nagios-server-ip/nagios/. You will be asked to provide admin username and password as shown below:
Provide your Nagios admin username, password and click on the SignIn button. You should see the Nagios dashboard on the following page:
Click on the services in the left pane, you should see all services on the following page:
Step 7: Add Monitoring Hosts to Nagios Server
Next, you will need to install the Nagios agent on the client system and add it to the Nagios server.
First, install the Nagios and NRPE package on the client system with the following command:
Save and close the file then restart the Nagios service to apply the changes:
systemctl restart nagios
At this point, Nagios server and client are installed and configured.
Step 9: Verify Nagios
Now, go back to Nagios web interface and click on the hosts in the left pane, you should see all newly added client host in the following page:
Now, click on the service tab in the left pane, you should see all services in the following page:
Of course, you don’t have to install Nagios on Ubuntu if you use one of our Managed hosting services, in which case you can simply ask our expert Linux admins to install this for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post on how to install Nagios on Ubuntu, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.
4 thoughts on “How To Install Nagios 4 and Monitor Your Servers on Ubuntu 20.04”
Hi,
In what order or what line do you change your IP here? If you can provide example that would be great.
Why do you suggest people download and install manually from an upstream tarball, rather than using the existng nagios4 packages already included in the official Ubuntu repos?
Steps 2, 4 and first half of step 5 can be entirely replaced with an «apt install nagios4 nagios-plugins-contrib nagios-nrpe-plugin monitoring-plugins» command.
Then All «/usr/local/nagios/etc/» paths when editing can be replaced with «/etc/nagios4/».
The only thing needed from step 5 would be the configuring of the contact name and email address, and adding clients using the nrpe editing lines.
There is No need to download and manually configure all of the above steps listed, when they are mostly automated by installing the nagios packages from the official Ubuntu repositores.
The latest versions of Nagios may not always be available on the Ubuntu repository. Following this tutorial, you can install the latest version or choose a specific version that you want to install of Nagios. But, of course, you can install it using the official repository of Ubuntu, if you prefer.
Hi,
In what order or what line do you change your IP here? If you can provide example that would be great.
server_address=client-ip-address
allowed_hosts=127.0.0.1,::1,server-ip-address
Also, do you need to change your IP here?
nano /etc/nagios/nrpe_local.cfg
And here?
nano /usr/local/nagios/etc/servers/nagiosclient.cfg
Thanks,
Peter
Change the word “server-ip-address” to your actual server IP address.
Why do you suggest people download and install manually from an upstream tarball, rather than using the existng nagios4 packages already included in the official Ubuntu repos?
Steps 2, 4 and first half of step 5 can be entirely replaced with an «apt install nagios4 nagios-plugins-contrib nagios-nrpe-plugin monitoring-plugins» command.
Then All «/usr/local/nagios/etc/» paths when editing can be replaced with «/etc/nagios4/».
The only thing needed from step 5 would be the configuring of the contact name and email address, and adding clients using the nrpe editing lines.
There is No need to download and manually configure all of the above steps listed, when they are mostly automated by installing the nagios packages from the official Ubuntu repositores.
The latest versions of Nagios may not always be available on the Ubuntu repository. Following this tutorial, you can install the latest version or choose a specific version that you want to install of Nagios. But, of course, you can install it using the official repository of Ubuntu, if you prefer.