ag@linux:~$

history 21 of 36

Install Nagios on CentOS: Users, Web Login, Alerts and Checks

A fuller Nagios Core source install on CentOS: nagios user and nagcmd group, web interface login, plugins, alert email address and a config check.

Written

From my Linux study notes, 2015. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.

These are my notes for installing Nagios Core and the Nagios plugins from source on a CentOS server, including the user and group setup, the alert email address and a configuration check before starting. For a shorter command list written for CentOS 6 with Nagios 3.4.4, see my other post, “Install Nagios Core 3 and Plugins on CentOS 6: Quick Steps”.

Before you start

  • Run all commands as root (or with sudo).
  • Use a test server first.
  • In the commands below, nagios-Currentversion stands for the version you download. Replace it with the real file and folder names.

Install the required packages

Terminal window
yum install httpd gcc glibc glibc-common gd gd-devel php

This installs Apache (httpd), PHP, the C compiler (gcc) and the GD graphics library that Nagios uses for its web graphs.

Create the Nagios user and group

Create the Nagios user account:

Terminal window
useradd -m nagios

Create the nagcmd group, which allows external commands to be submitted through the web interface, and add both the nagios user and the apache user to it:

Terminal window
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache

usermod -a -G adds the user to the group without removing them from their other groups. Leaving out -a would replace their existing supplementary groups.

Download Nagios and the plugins

Create a directory to store the Nagios installation files:

Terminal window
mkdir /opt/Nagios

Download Nagios Core and the Nagios plugins from the Nagios website and save the files to /opt/Nagios. (The download page I used at the time was http://www.nagios.org/download/download.php; today, start from https://www.nagios.org/.)

Extract the files

Terminal window
cd /opt/Nagios
tar xzf nagios-Currentversion.gz
cd nagios-Currentversion.gz

Correction: the archive ends in .tar.gz, and you cd into the extracted folder, not the archive file:

Terminal window
cd /opt/Nagios
tar xzf nagios-Currentversion.tar.gz
cd nagios-Currentversion

Run ls after extracting if you are not sure of the folder name.

Compile and configure Nagios

Terminal window
./configure --with-command-group=nagcmd

(In the original post the two hyphens had become a single long dash, –with-command-group. The shell does not treat that as an option; it needs two normal hyphens.)

Compile Nagios:

Terminal window
make all

Install the binaries, init script and sample config files:

Terminal window
make install
make install-init
make install-config
make install-commandmode

Do not start Nagios yet.

Nagios has now been installed to the directory /usr/local/nagios/.

Install the Nagios web interface

Terminal window
make install-webconf

The web interface is installed to /usr/local/nagios/share, and an Apache config file is added at /etc/httpd/conf.d/nagios.conf.

Create the web interface login user

We will create the user nagiosadmin. Remember the password you assign.

Terminal window
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

This is not the most secure method (see the notes at the end). The -c option creates a new file; do not use -c again when adding more users, or the file is overwritten.

Restart Apache so it loads the new Nagios config:

Terminal window
service httpd restart

Install the Nagios plugins

Extract the files:

Terminal window
cd /opt/Nagios
tar xzf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins1.4.13

Correction: the folder name has a hyphen before the version number:

Terminal window
cd nagios-plugins-1.4.13

Compile and install the plugins:

Terminal window
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

(As above, the original showed long dashes in –with-nagios-user and –with-nagios-group; they are two normal hyphens here.)

The plugins are the programs Nagios runs to perform each check. They are installed to /usr/local/nagios/libexec/.

Set the nagiosadmin email address for alerts

Terminal window
vi /usr/local/nagios/etc/objects/contacts.cfg

Find the email line in the nagiosadmin contact definition and change it to the address that should receive alerts:

(My original post showed a real address here; use your own.) In Nagios object files, anything after a ; on a line is a comment, so a trailing ; does no harm.

Verify the Nagios configuration

Terminal window
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

The -v option checks the configuration without starting Nagios. At the end you want to see:

Total Warnings: 0
Total Errors: 0

If there are errors, the output names the file and line to fix. Run this check every time you change the configuration.

Start Nagios

Enable Nagios and Apache to start at system boot:

Terminal window
chkconfig --add nagios
chkconfig nagios on
chkconfig httpd on

(As with configure, the original showed –add with a long dash; it should be --add.)

Start Nagios:

Terminal window
service nagios start

Log in to the web interface

Open http://ip-address/nagios/ in a browser, replacing ip-address with your server’s IP address, and log in as nagiosadmin.

Notes for today

  • CentOS end of life: CentOS 6 reached end of life on 30 November 2020 and CentOS 7 on 30 June 2024. Rocky Linux and AlmaLinux are the common free replacements.

  • Versions: plugins 1.4.13 are very old, and the Nagios Core and plugin build steps have changed in newer releases. Check the official documentation at https://www.nagios.org/ for your version.

  • Services: on systemd systems, chkconfig nagios on and service nagios start become:

    Terminal window
    sudo systemctl enable nagios
    sudo systemctl start nagios
  • Packages: yum became dnf on newer Red Hat-family systems; Debian and Ubuntu use apt.

  • Firewall: on Rocky Linux and AlmaLinux, open port 80 with sudo firewall-cmd --permanent --add-service=http then sudo firewall-cmd --reload.

  • Security: the htpasswd login is HTTP basic authentication, so over plain http:// the password travels unencrypted. On a server reachable from the internet, serve Nagios over HTTPS or limit access to trusted IP addresses.