How to install Nagios with Nginx on CentOS

How to install Nagios with Nginx on CentOS

Nagios is a monitoring utility system for you to check in to any servers and networks. It also monitors the various states of services such as space on the hard disk drives, CPU load, RAM and server status via ICMP protocol. It enables the user to set up email alerts so the system administrators can be alerted when something is not working properly. This tutorial covers the installation Nagios with Nginx on CentOS.

Installation procedure

Before starting the installation procedure, install Nginx with PHP and it' s required packages and press y to continue with the installation procedure.

[root@nagios ~]# yum install nginx php php-fpm php-common gcc glibc glibc-common gd gd-devel make net-snmp unzip -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos-hcm.viettelidc.com.vn
 * epel: mirror2.totbb.net
 * extras: mirrors.vonline.vn
 * updates: mirrors.aluhost.com
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Resolving Dependencies
-->  Running transaction check
--->  Package gcc.x86_64 0:4.4.7-18.el6 will be installed
-->  Processing Dependency: libgomp = 4.4.7-18.el6 for package: gcc-4.4.7-18.el6.x86_64
-->  Processing Dependency: cpp = 4.4.7-18.el6 for package: gcc-4.4.7-18.el6.x86_64
.
.
.
Dependency Updated:
  glibc-devel.x86_64 0:2.12-1.209.el6_9.2  glibc-headers.x86_64 0:2.12-1.209.el6_9.2  libX11.x86_64 0:1.6.4-3.el6  libX11-common.noarch 0:1.6.4-3.el6  libgcc.x86_64 0:4.4.7-18.el6  libgomp.x86_64 0:4.4.7-18.el6
  libxcb.x86_64 0:1.12-4.el6               net-snmp-libs.x86_64 1:5.5-60.el6      

Install the necessary tools which is to be used for Nagios.

[root@nagios nagios]# yum groupinstall ' Development Tools'  -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Loading mirror speeds from cached hostfile
 * base: centos-hcm.viettelidc.com.vn
 * epel: ftp.jaist.ac.jp
 * extras: mirrors.vonline.vn
 * updates: mirrors.aluhost.com
base/group_gz                                                                                                                                                                                | 226 kB     00:00    
epel/group_gz                                                                                                                                                                                | 150 kB     00:00    
Package gcc-4.4.7-18.el6.x86_64 already installed and latest version
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package patch-2.6-6.el6.x86_64 already installed and latest version
Package 1:pkgconfig-0.23-9.1.el6.x86_64 already installed and latest version
Package gettext-0.17-18.el6.x86_64 already installed and latest version
Package elfutils-0.164-2.el6.x86_64 already installed and latest version
Package cvs-1.11.23-16.el6.x86_64 already installed and latest version
Resolving Dependencies
-->  Running transaction check
--->  Package autoconf.noarch 0:2.63-5.1.el6 will be installed
--->  Package automake.noarch 0:1.11.1-4.el6 will be installed
--->  Package binutils.x86_64 0:2.20.51.0.2-5.44.el6 will be updated
--->  Package binutils.x86_64 0:2.20.51.0.2-5.47.el6_9.1 will be an update
--->  Package bison.x86_64 0:2.4.1-5.el6 will be installed
--->  Package byacc.x86_64 0:1.9.20070509-7.el6 will be installed
--->  Package cscope.x86_64 0:15.6-7.el6 will be installed
--->  Package ctags.x86_64 0:5.8-2.el6 will be installed
.
.
.
Dependency Updated:
  libstdc++.x86_64 0:4.4.7-18.el6                         perl.x86_64 4:5.10.1-144.el6                        perl-CGI.x86_64 0:3.51-144.el6                 perl-ExtUtils-MakeMaker.x86_64 0:6.55-144.el6       
  perl-ExtUtils-ParseXS.x86_64 1:2.2003.0-144.el6         perl-Module-Pluggable.x86_64 1:3.90-144.el6         perl-Pod-Escapes.x86_64 1:1.04-144.el6         perl-Pod-Simple.x86_64 1:3.13-144.el6               
  perl-Test-Harness.x86_64 0:3.17-144.el6                 perl-Test-Simple.x86_64 0:0.92-144.el6              perl-devel.x86_64 4:5.10.1-144.el6             perl-libs.x86_64 4:5.10.1-144.el6                   
  perl-version.x86_64 3:0.77-144.el6                      systemtap-runtime.x86_64 0:2.9-7.el6              
Complete!

Next create a new Nginx configuration for Nagios under the name of conf.d directory as shown below. Enter the following contents into the file and save it.

[root@nagios ~]# vim /etc/nginx/conf.d/nagios.conf
server {
 listen 80 
 server_name nagios_local 
 access_log /var/log/nginx/nagios-access.log 
 error_log /var/log/nginx/nagios-error.log info 
 root /usr/local/nagios/share 
 index index.php 
 auth_basic " Nagios Restricted Access"  
 auth_basic_user_file /usr/local/nagios/passwd 
 location /data-stylesheets {
 alias /usr/local/nagios/share/data-stylesheets 
 }
 location ~ .cgi$ {
     root /usr/local/nagios/sbin/ 
     include fastcgi_params 
     rewrite ^/nagios/cgi-bin/(.*).cgi /$1.cgi break 
     fastcgi_param AUTH_USER $remote_user 
     fastcgi_param REMOTE_USER $remote_user 
     fastcgi_pass unix:/var/run/fcgiwrap.socket 
     fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin/$fastcgi_script_name 
     fastcgi_param PATH_INFO $fastcgi_script_name 
 }
 location ~ .php$ {
    include fastcgi_params 
    fastcgi_pass unix:/var/run/php-fpm/nagios.socket 
    fastcgi_index index.php 
    fastcgi_param SCRIPT_FILENAME /usr/local/nagios/share$fastcgi_script_name 
   fastcgi_param PATH_INFO $fastcgi_script_name 
 }
 location ~ (.*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf))$ {
    root /usr/local/nagios/share/ 
    rewrite ^/nagios/(.*) /$1 break 
    access_log off  expires max 
 }
}

Rename the Nginx default configuration by executing the following command.

[root@nagios ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.old

Next restart and enable the Nginx service by running the following commands

[root@nagios ~]# service nginx start
Starting nginx:                                            [  OK  ]
[root@nagios ~]# chkconfig nginx on

Open HTTP port (80) on iptables by running the commands.

[root@nagios ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@nagios ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

Now configure PHP-FPM for Nagios by executing the vim editor along with the Nagios configuration file. Enter the following contents in the file.

[root@nagios ~]# vim /etc/php-fpm.d/nagios.conf

[nagios]
 listen = 127.0.0.1:9000
listen  /var/run/php-fpm/nagios.socket
listen.owner = nginx
listen.group = nginx
listen.mode=0660listen.allowed_clients = 127.0.0.1

user = nagios
group = nagios
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

 request_slowlog_timeout = 0
slowlog = /var/log/php-fpm/www-slow.log

 php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/php-fpm/nagios-error.log
php_admin_flag[log_errors] = on
 php_admin_value[memory_limit] = 128M
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

Now start the PHP-FPM service by running the following command.

[root@nagios ~]# service php-fpm start
Starting php-fpm: [14-Jul-2017 16:07:48] ERROR: [pool nagios] cannot get uid for user ' nagios' 
[14-Jul-2017 16:07:48] ERROR: FPM initialization failed
                                                           [FAILED]

You will get error like shown above, because the Nagios user was not created yet. So create a new user for Nagios by executing the following commands.

[root@nagios ~]# useradd nagios
[root@nagios ~]# usermod -G nagios nginx

Now start and enable PHP-FPM service by running the following commands.

[root@nagios ~]# service php-fpm start
Starting php-fpm:                                          [  OK  ]
[root@nagios ~]# chkconfig php-fpm on

Next download the latest Nagios tarball package from its official website and then extract it by running the tar command followed by the downloaded package.

[root@nagios ~]# tar -xzf nagios-4.3.2.tar.gz

Now compile and install Nagios using the below command.

[root@nagios ~]# cd nagios-4* & &  ./configure & &  make all & &  make install & &  make install-init & &  make install-config & &  make install-commandmode & &  make install-webconf
checking for a BSD-compatible install... /usr/bin/install -c
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether make sets $(MAKE)... yes
checking whether ln -s works... yes
checking for strip... /usr/bin/strip
checking how to run the C preprocessor... gcc -E
.
.
.
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/printer.cfg /usr/local/nagios/etc/objects/printer.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/switch.cfg /usr/local/nagios/etc/objects/switch.cfg

*** Config files installed ***
Remember, these are *SAMPLE* config files.  You' ll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.

/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw
*** External command directory configured ***

/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
if [ 0 -eq 1 ]  then 
                        ln -s /etc/httpd/conf.d/nagios.conf /etc/apache2/sites-enabled/nagios.conf  
            fi
*** Nagios/Apache conf file installed ***

If some error occurs while using make or make install command, try after installing gcc and glibc package through yum command.

Verify the Nagios configuration by running the following command.

[root@nagios nagios-4.3.2]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.3.2
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2017-05-09
License: GPL
Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...
Checking objects...
            Checked 8 services.
            Checked 1 hosts.
            Checked 1 host groups.
            Checked 0 service groups.
            Checked 1 contacts.
            Checked 1 contact groups.
            Checked 24 commands.
            Checked 5 time periods.
            Checked 0 host escalations.
            Checked 0 service escalations.
Checking for circular paths...
            Checked 1 hosts
            Checked 0 service dependencies
            Checked 0 host dependencies
            Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0
Things look okay - No serious problems were detected during the pre-flight check

Next install the required Nagios plugins and press y to continue with the installation process.

[root@nagios ~]# yum install nagios-plugins-all -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos-hcm.viettelidc.com.vn
 * epel: ftp.jaist.ac.jp
 * extras: mirrors.vonline.vn
 * updates: mirrors.aluhost.com
Resolving Dependencies
-->  Running transaction check
--->  Package nagios-plugins-all.x86_64 0:2.2.1-1.el6 will be installed
-->  Processing Dependency: nagios-plugins-wave for package: nagios-plugins-all-2.2.1-1.el6.x86_64
-->  Processing Dependency: nagios-plugins-users for package: nagios-plugins-all-2.2.1-1.el6.x86_64
-->  Processing Dependency: nagios-plugins-ups for package: nagios-plugins-all-2.2.1-1.el6.x86_64
-->  Processing Dependency: nagios-plugins-time for package: nagios-plugins-all-2.2.1-1.el6.x86_64
-->  Processing Dependency: nagios-plugins-tcp for package: nagios-plugins-all-2.2.1-1.el6.x86_64
-->  Processing Dependency: nagios-plugins-swap for package: nagios-plugins-all-2.2.1-1.el6.x86_64
.
.
.
-1.el6             nagios-plugins-swap.x86_64 0:2.2.1-1.el6            nagios-plugins-tcp.x86_64 0:2.2.1-1.el6           
  nagios-plugins-time.x86_64 0:2.2.1-1.el6             nagios-plugins-ups.x86_64 0:2.2.1-1.el6             nagios-plugins-users.x86_64 0:2.2.1-1.el6           nagios-plugins-wave.x86_64 0:2.2.1-1.el6          
  net-snmp-utils.x86_64 1:5.5-60.el6                   postgresql-libs.x86_64 0:8.4.20-7.el6               qstat.x86_64 0:2.11-9.20080912svn311.el6          
Complete!

Follow the below steps to create softlink for Nagios plugins, before that remove the default contents of libexec. Now change the ownership permissions for those plugin directories.

[root@nagios ~]# rm -rf /usr/local/nagios/libexec
[root@nagios ~]# ln -s /usr/lib64/nagios/plugins /usr/local/nagios/libexec
[root@nagios ~]# chown -R nagios:nagios /usr/local/nagios/libexec

Start and enable nagios service by running the following command.

[root@nagios ~]# service nagios start
Starting nagios: done.
[root@nagios ~]# chkconfig nagios on

Create password for nagiosadmin user and enter the required password for the admin user.

[root@nagios ~]# htpasswd -c /usr/local/nagios/passwd nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin

Next create softlink as shown below by creating a directory and running the following commands.

[root@nagios ~]# mkdir /usr/local/nagios/share/nagios
[root@nagios ~]# cd /usr/local/nagios/share/nagios/
[root@nagios nagios]# ln -s /usr/local/nagios/share/data-stylesheets/ data-stylesheets
[root@nagios nagios]# ln -s /usr/local/nagios/share/js js

Install the spawn-fcgi for the Nagios and press y to continue with the installation procedure.

[root@nagios nagios]# yum install fcgi-devel spawn-fcgi -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos-hcm.viettelidc.com.vn
 * epel: ftp.jaist.ac.jp
 * extras: mirrors.vonline.vn
 * updates: mirrors.aluhost.com
Resolving Dependencies
-->  Running transaction check
--->  Package fcgi-devel.x86_64 0:2.4.0-12.el6 will be installed
-->  Processing Dependency: fcgi = 2.4.0-12.el6 for package: fcgi-devel-2.4.0-12.el6.x86_64
-->  Processing Dependency: libfcgi.so.0()(64bit) for package: fcgi-devel-2.4.0-12.el6.x86_64
-->  Processing Dependency: libfcgi++.so.0()(64bit) for package: fcgi-devel-2.4.0-12.el6.x86_64
--->  Package spawn-fcgi.x86_64 0:1.6.3-1.el6 will be installed
-->  Running transaction check
--->  Package fcgi.x86_64 0:2.4.0-12.el6 will be installed
-->  Finished Dependency Resolution
.
.
.
Installed:
  fcgi-devel.x86_64 0:2.4.0-12.el6                                                                          spawn-fcgi.x86_64 0:1.6.3-1.el6                                                                         
Dependency Installed:
  fcgi.x86_64 0:2.4.0-12.el6                                                                                                                                                                                       

Complete!

Next download the fcgiwrap and compile it by executing the following command.

[root@nagios nagios]# cd /usr/local/src/ & &  git clone https://github.com/gnosek/fcgiwrap.git & &  cd fcgiwrap & &  autoreconf -i & &  ./configure & &  make & &  make install
Initialized empty Git repository in /usr/local/src/fcgiwrap/.git
remote: Counting objects: 270, done.
remote: Total 270 (delta 0), reused 0 (delta 0), pack-reused 270
Receiving objects: 100% (270/270), 63.35 KiB | 60 KiB/s, done.
Resolving deltas: 100% (139/139), done.
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for FCGX_Init in -lfcgi... yes
checking for systemd... no
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
.
.
.
config.status: creating Makefile
config.status: creating config.h
cc -std=gnu99 -Wall -Wextra -Werror -pedantic -O2 -g3    fcgiwrap.c  -lfcgi  -o fcgiwrap
install -d -m 755 /usr/local/sbin
install -m 755 fcgiwrap /usr/local/sbin
install -d -m 755 /usr/local/man/man8
install -m 644 fcgiwrap.8 /usr/local/man/man8

Now edit the spawn-fcgi configuration using the vim editor and enter the following contents into the file.

[root@nagios ~]# vim /etc/sysconfig/spawn-fcgi

FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS=" -M 0700" 
OPTIONS=" -u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM" 

Restart and enable the spawn-fcgi service by running the following command.

[root@nagios ~]# service spawn-fcgi start
Starting spawn-fcgi:                                       [  OK  ]
[root@nagios ~]# chkconfig spawn-fcgi on

Trigger the browser with http://< IP-Addr> /nagios and login with the respective credentials. You will get the home page on the screen.

The Nagios with Nginx on CentOS installation is complete.

Comment
patricioavero
Mar 03 2018
Hi Mason, Just to let you know that there are a couple of typos in regrds of the php-fpm's pool configuration file syntax: listen = /var/run/php-fpm/nagios.socket (missing equal sign) listen.mode=0660 (two lines togheter) listen.allowed_clients = 127.0.0.1 (two lines togheter) Regads. PATO.
Add a comment
FAQ
Q
What can Nagios do when it detects a change in a host or service status?
A
It can do many things, from sending an email or text notification, to opening a ticket in your external application and executing a remote script which does anything you want.
Q
What should I do after extracting the package, which package using to install the Nagios?
A
Go into the extraction directory of Nagios, then you should run the following script file as "./configure --with-command-group=nagcmd"
Q
Does Nagios XI have an audit log?
A
Yes, the Enterprise edition features this under the Admin -> Audit Log page
Q
What is the purpose of using the Nagios?
A
Nagios is an Open Source monitoring tool that track on system/network. It monitors your remote hosts and services on a single window. It detects the problems in the server and their by reduce
Q
What can Nagios do when it detects a change in a host or service status?
A
It can do many things, from sending an email or text notification, to opening a ticket in your external application, and executing a remote script which does anything you want. Some customers