In this article, we are going to install Nagios Core on Ubuntu 20.04 server. Nagios core formally known as Nagios is a free and open-source software application that monitors systems, networks, and infrastructure. Nagios offers monitoring and alerting services for servers, switches, applications, and services. It alerts users when something goes wrong and alerts again when it has been rectified.
Here is what you will end up with
- Nagios and Nagios plugins will be installed underneath /usr/local/nagios.
- Nagios will be installed to monitor few aspects of your system (cpu load, disk usage, etc.)
- The nagios web interface will be accesible at http://localhost/nagios/
Required packages
- PHP
- Apache2
- GCC compiler and development libraries
- GD development libraries
Check out this article on how to install Apache 2 and PHP on Ubuntu 20.04 Ubuntu server
Step 1: Update Ubuntu 20.04 server
Login to your server using the following command.
$ ssh [email protected]_Address
Once logged in issue the following commands to update the package list
$ sudo apt update
Next, we can install the packages necessary for building Nagios 4 and Nagios plugins.
$ sudo apt install nagios4 nagios-plugins-contrib nagios-nrpe-plugin
Sample output
Setting up libmonitoring-plugin-perl (0.40-1) ...
Setting up ruby (1:2.7+1) ...
Setting up rake (13.0.1-4) ...
Setting up liblwp-protocol-https-perl (6.07-2ubuntu2) ...
Setting up libwww-perl (6.43-1) ...
Setting up libruby2.7:amd64 (2.7.0-5ubuntu1.5) ...
Setting up liblwp-useragent-determined-perl (1.07-1) ...
Setting up libxml-parser-perl (2.46-1) ...
Setting up ruby2.7 (2.7.0-5ubuntu1.5) ...
Setting up libwebinject-perl (1.94-1) ...
Setting up libxml-sax-expat-perl (0.51-1) ...
update-perl-sax-parsers: Registering Perl SAX parser XML::SAX::Expat with priority 50...
update-perl-sax-parsers: Updating overall Perl SAX parser modules info file...
Replacing config file /etc/perl/XML/SAX/ParserDetails.ini with new version
Processing triggers for install-info (6.7.0.dfsg.2-5) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
Processing triggers for rsyslog (8.2001.0-1ubuntu1.1) ...
Processing triggers for ufw (0.36-6ubuntu1) ...
Processing triggers for systemd (245.4-4ubuntu3.13) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for php7.4-cli (7.4.3-4ubuntu2.7) ...
Processing triggers for libapache2-mod-php7.4 (7.4.3-4ubuntu2.7) ...
The above command will install a bunch of commands including Apache2, postfix mail servers if you require, Nagios core, Nagios plugins.
We have an Apache configuration file that comes with Nagios depending on mod_authz_groupfile and mod_auth_digest modules, which are not enabled by default.
mod_authz_groupfile module provides authorization capabilities so that authenticated users can be allowed or denied access to portions of the website by group membership. mod_auth_digest implements HTTP digest authentication and provides an alternative to mod_auth_basic where the password is not transmitted as cleartext.The following command enables the mod_authz_groupfile and mod_auth_digest modules on our system
$ sudo a2enmod authz_groupfile auth_digest
Sample output
Considering dependency authz_core for authz_groupfile:
Module authz_core already enabled
Enabling module authz_groupfile.
Considering dependency authn_core for auth_digest:
Module authn_core already enabled
Enabling module auth_digest.
To activate the new configuration, you need to run:
systemctl restart apache2
Apache default configuration allows Nagios access from the localhost and private IPs only. Now we need to change the configuration so that only authenticated users can view the interface and issue commands.
Open your preferred text editor to make the necessary changes
$ sudo nano /etc/apache2/conf-enabled/nagios4-cgi.conf
Sample output
#Require ip ::1/128 fc00::/7 fe80::/10 10.0.0.0/8 127.0.0.0/8 169.254.0.0/16 172.16.0.0/12 192.168.0.0/16
<Files "cmd.cgi">
AuthDigestDomain "Nagios4"
AuthDigestProvider file
AuthUserFile "/etc/nagios4/htdigest.users"
AuthGroupFile "/etc/group"
AuthName "Nagios4"
AuthType Digest
#Require all granted
Require valid-user
</Files>
</DirectoryMatch>
<Directory /usr/share/nagios4/htdocs>
Options +ExecCGI
</Directory>
From the above comment outline starting with Require IP, <Files “cmd.cgi”> and Require all granted. Uncomment the line containing Require valid-user. Save the file and exit nano.
The final results will look like the above
Now restart Apache for changes to take effect
$ sudo systemctl restart apache2
You can now verify that both Apache and Nagios4 are running by using the following commands:
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-11-20 13:14:03 UTC; 16s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 31569 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 31590 (apache2)
Tasks: 6 (limit: 2344)
Memory: 10.1M
CGroup: /system.slice/apache2.service
├─31590 /usr/sbin/apache2 -k start
├─31591 /usr/sbin/apache2 -k start
├─31592 /usr/sbin/apache2 -k start
├─31593 /usr/sbin/apache2 -k start
├─31594 /usr/sbin/apache2 -k start
└─31595 /usr/sbin/apache2 -k start
Nov 20 13:14:03 ubuntu-20 systemd[1]: Starting The Apache HTTP Server...
Nov 20 13:14:03 ubuntu-20 apachectl[31587]: AH00558: apache2: Could not reliably determine the server's fully qu>
Nov 20 13:14:03 ubuntu-20 systemd[1]: Started The Apache HTTP Server.
$ sudo systemctl status nagios4
● nagios4.service - LSB: nagios host/service/network monitoring and management system
Loaded: loaded (/etc/init.d/nagios4; generated)
Active: active (running) since Sat 2021-11-20 13:04:30 UTC; 11min ago
Docs: man:systemd-sysv-generator(8)
Tasks: 6 (limit: 2344)
Memory: 3.7M
CGroup: /system.slice/nagios4.service
├─26535 /usr/sbin/nagios4 -d /etc/nagios4/nagios.cfg
├─26536 /usr/sbin/nagios4 --worker /var/lib/nagios4/rw/nagios.qh
├─26537 /usr/sbin/nagios4 --worker /var/lib/nagios4/rw/nagios.qh
├─26538 /usr/sbin/nagios4 --worker /var/lib/nagios4/rw/nagios.qh
├─26539 /usr/sbin/nagios4 --worker /var/lib/nagios4/rw/nagios.qh
└─26561 /usr/sbin/nagios4 -d /etc/nagios4/nagios.cfg
Nov 20 13:09:52 ubuntu-20 nagios4[26535]: SERVICE ALERT: localhost;Swap Usage;CRITICAL;SOFT;2;SWAP CRITICAL - 0%>
Nov 20 13:10:52 ubuntu-20 nagios4[26535]: SERVICE ALERT: localhost;Swap Usage;CRITICAL;SOFT;3;SWAP CRITICAL - 0%>
Nov 20 13:11:52 ubuntu-20 nagios4[26535]: SERVICE ALERT: localhost;Swap Usage;CRITICAL;HARD;4;SWAP CRITICAL - 0%>
Nov 20 13:11:52 ubuntu-20 nagios4[26535]: SERVICE NOTIFICATION: nagiosadmin;localhost;Swap Usage;CRITICAL;notify>
Nov 20 13:11:52 ubuntu-20 postfix/sendmail[31551]: fatal: open /etc/postfix/main.cf: No such file or directory
Nov 20 13:11:52 ubuntu-20 nagios4[26535]: wproc: NOTIFY job 4 from worker Core Worker 26539 is a non-check helpe>
Nov 20 13:11:52 ubuntu-20 nagios4[26535]: wproc: host=localhost; service=Swap Usage; contact=nagiosadmin
Nov 20 13:11:52 ubuntu-20 nagios4[26535]: wproc: early_timeout=0; exited_ok=1; wait_status=256; error_code=0;
Nov 20 13:11:52 ubuntu-20 nagios4[26535]: wproc: stderr line 01: send-mail: fatal: open /etc/postfix/main.cf: >
Nov 20 13:11:52 ubuntu-20 nagios4[26535]: wproc: stderr line 02: Can't send mail: sendmail process failed with>
Postfix is not running because I did not want to send emails but you can allow that when prompted during installation.
Creating a User Account
Nagios is configured by default to grant administrative privileges to the user called nagiosadmin. We use htdigest to create users.
Use the following command to create a new user:
$ sudo htdigest -c /etc/nagios4/htdigest.users Nagios4 nagiosadmin
Adding password for nagiosadmin in realm Nagios4.
New password: <password>
Re-type new password: <password>
Restart Apache service for effects to take place
Configure Firewall
Configure a firewall with UFW configuration tool and allow HTTP and HTTPS to pass through to allow Apache to be reachable.
$ sudo ufw allow Apache
Rules updated
Rules updated (v6)
Accessing Nagios Web Interface
To access Nagios from the web interface use the following command:
$ http://<IP_Address>/nagios4
Conclusion
From the above tutorial you are now in a position to install Nagios in your server to help you monitor your services. You can read more by reading Nagios Documentation