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
- Monitoring and security
- Written for CentOS
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-Currentversionstands for the version you download. Replace it with the real file and folder names.
Install the required packages
yum install httpd gcc glibc glibc-common gd gd-devel phpThis 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:
useradd -m nagiosCreate 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:
groupadd nagcmdusermod -a -G nagcmd nagiosusermod -a -G nagcmd apacheusermod -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:
mkdir /opt/NagiosDownload 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
cd /opt/Nagiostar xzf nagios-Currentversion.gzcd nagios-Currentversion.gzCorrection: the archive ends in .tar.gz, and you cd into the extracted folder, not the archive file:
cd /opt/Nagiostar xzf nagios-Currentversion.tar.gzcd nagios-CurrentversionRun ls after extracting if you are not sure of the folder name.
Compile and configure Nagios
./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:
make allInstall the binaries, init script and sample config files:
make installmake install-initmake install-configmake install-commandmodeDo not start Nagios yet.
Nagios has now been installed to the directory /usr/local/nagios/.
Install the Nagios web interface
make install-webconfThe 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.
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadminThis 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:
service httpd restartInstall the Nagios plugins
Extract the files:
cd /opt/Nagiostar xzf nagios-plugins-1.4.13.tar.gzcd nagios-plugins1.4.13Correction: the folder name has a hyphen before the version number:
cd nagios-plugins-1.4.13Compile and install the plugins:
./configure --with-nagios-user=nagios --with-nagios-group=nagiosmakemake 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
vi /usr/local/nagios/etc/objects/contacts.cfgFind the email line in the nagiosadmin contact definition and change it to the address that should receive alerts:
email [email protected](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
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgThe -v option checks the configuration without starting Nagios. At the end you want to see:
Total Warnings: 0Total Errors: 0If 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:
chkconfig --add nagioschkconfig nagios onchkconfig httpd on(As with configure, the original showed –add with a long dash; it should be --add.)
Start Nagios:
service nagios startLog 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 onandservice nagios startbecome:Terminal window sudo systemctl enable nagiossudo systemctl start nagios -
Packages:
yumbecamednfon newer Red Hat-family systems; Debian and Ubuntu useapt. -
Firewall: on Rocky Linux and AlmaLinux, open port 80 with
sudo firewall-cmd --permanent --add-service=httpthensudo firewall-cmd --reload. -
Security: the
htpasswdlogin is HTTP basic authentication, so over plainhttp://the password travels unencrypted. On a server reachable from the internet, serve Nagios over HTTPS or limit access to trusted IP addresses.