Install Nagios Core on CentOS from Source

From Bonus Bits
Jump to: navigation, search

Purpose

This article gives the steps to install the free open source version of Nagios "Core" on CentOS 6.x x64 from source files instead of RPM.


Environment

  • CentOS 6.x x64
  • Apache 2.2
  • Nagios 4.2.2
  • Nagios Plugins 2.1.2
  • NRPE 2.1.5


Download

  1. Download Nagios Core
  2. Download Nagios Plugins


Install Dependencies

yum install -y wget httpd php gcc glibc glibc-common gd gd-devel make net-snmp openssl openssl-devel


Add User and Group

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios

May want to give apache user access if moving around locations with symlinks.

usermod -a -G nagcmd apache
usermod -a -G nagios apache

Configure and Install

  1. Uncompress tar and change into it's directory
  2. Configure Installation
    ./configure --with-command-group=nagcmd
    
  3. Compile
    make all
    
  4. Install
    make install
    make install-init
    make install-config
    make install-commandmode
    make install-webconf
    
    OR
    make install && make install-init && make install-config && make install-commandmode && make install-webconf
    

Setup EventHandlers

cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers


Test Initial Config

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


Add Symlink for Nagios Binary (Optional)

ln -s /usr/local/nagios/bin/nagios /usr/bin/nagios


Install Nagios Plugins

This adds the regular check command binaries used to pull information. Example: /usr/local/nagios/libexec/check_nt

  1. Uncompress tar and change into it's directory
    tar -xzvf nagios-plugins-2.1.2.tar.gz
    
    cd nagios-plugins-2.1.2
    
  2. Configure Installation
    ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
    
  3. Compile
    make
    
  4. Install
    make install
    


Install Nagios Plugins NRPE (Optional)

  • NRPE allows you to remotely execute Nagios plugins on other Linux/Unix machines. This allows you to monitor remote machine metrics (disk usage, CPU load, etc.). NRPE can also communicate with Windows agent addons like NSClient++, so you can check metrics on remote Windows machines as well.
  • The Nagios server will need nagios-plugins-nrpe installed if planning to monitoring Linux systems other than the localhost.
  • It installed the check_nrpe command.
  1. Download NRPE Tarball
  2. Install openssl
    yum install openssl
    
  3. Uncompress tar and change into it's directory
    tar -xzvf nrpe-2.15.tar.gz
    
    cd nrpe-2.15
    
  4. Configure Installation
    ./configure --enable-ssl --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --libexecdir=/usr/local/nagios/libexec/ --bindir=/usr/local/nagios/bin/ --prefix=/usr/local/nagios
    
  5. Compile
    make
    
  6. Install
    make install
    


Start Services

Apache

service httpd start

Nagios

service nagios start


Set Services to Start on Boot

Apache

chkconfig httpd on

Nagios

chkconfig --add nagios
chkconfig nagios on


Configure Firewall if Needed

Configure iptables to Allow Access to Common Services on Linux

Apache on Nagios Server

TCP Port 80

Nagios Windows Client

TCP Port 5667

Nagios Linux NRPE Client

TCP Port 5666


Configure / Disable SELinux

SELinux defaults will cause issues with some CGI processes. i.e. Permission denied: exec of '/usr/local/nagios/sbin/status.cgi' failed (HowTo) Disable SELINUX on CentOS


Create Web Console Accounts

The password file location should already be set in the Apache nagios.conf file. /etc/httpd/conf.d/nagios.conf

Example

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

<Directory "/usr/local/nagios/sbin">
#  SSLRequireSSL
   Options ExecCGI
   AllowOverride None
   <IfVersion >= 2.3>
      <RequireAll>
         Require all granted
#        Require host 127.0.0.1

         AuthName "Nagios Access"
         AuthType Basic
         AuthUserFile /usr/local/nagios/etc/htpasswd.users
         Require valid-user
      </RequireAll>
   </IfVersion>
   <IfVersion < 2.3>
      Order allow,deny
      Allow from all
#     Order deny,allow
#     Deny from all
#     Allow from 127.0.0.1

      AuthName "Nagios Access"
      AuthType Basic
      AuthUserFile /usr/local/nagios/etc/htpasswd.users
      Require valid-user
   </IfVersion>
</Directory>

Alias /nagios "/usr/local/nagios/share"

<Directory "/usr/local/nagios/share">
#  SSLRequireSSL
   Options None
   AllowOverride None
   <IfVersion >= 2.3>
      <RequireAll>
         Require all granted
#        Require host 127.0.0.1

         AuthName "Nagios Access"
         AuthType Basic
         AuthUserFile /usr/local/nagios/etc/htpasswd.users
         Require valid-user
      </RequireAll>
   </IfVersion>
   <IfVersion < 2.3>
      Order allow,deny
      Allow from all
#     Order deny,allow
#     Deny from all
#     Allow from 127.0.0.1

      AuthName "Nagios Access"
      AuthType Basic
      AuthUserFile /usr/local/nagios/etc/htpasswd.users
      Require valid-user
   </IfVersion>
</Directory>

Admin Account

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

Tip: The -c parameter is used to create the file. So only use for the first user and file creation. Then exclude the parameter for subsequently added users.

Guest Account

htpasswd /usr/local/nagios/etc/htpasswd.users guest

NOC Account'

htpasswd /usr/local/nagios/etc/htpasswd.users noc

Configure User Permissions

vim /usr/local/nagios/etc/cgi.cfg

Test Nagios Web Console Access

http://<hostname>/nagios


Change Apache Site Access to Virtual Host Based (Optional)

Create Symbolic Link in Apache Root Directory to Nagios Web Site

ln -s /usr/share/nagios/ /var/www/html/nagios

Configure CGI

  1. Change URL HTML Path to root
    vim /usr/local/nagios/etc/cgi.cfg
    
    From url_html_path=/nagios To url_html_path=/

Uncomment the Following Line

NameVirtualHost *:80

Add Virtual Host Info to Nagios Apache Configuration File

vim /etc/httpd/conf.d/nagios.conf


Examples

<VirtualHost *:80>
DocumentRoot /var/www/html/nagios
ServerName nagios.yourdomain.com
Errorlog /var/log/httpd/nagios.log
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /var/www/html/nagios
ServerName nagios
Errorlog /var/log/httpd/nagios.log
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /var/www/html/nagios
ServerName hostname
Errorlog /var/log/httpd/nagios.log
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /var/www/html/nagios
ServerName hostip
Errorlog /var/log/httpd/nagios.log
</VirtualHost>

Test Virtual Host URLs

http://nagios.yourdomain.com


Custom Icons (Optional)

  1. Copy any custom icon logos to /usr/local/nagios/share/images/logos
  2. When making your own icons you'll probably need gd-progs which include the binary pngtogd2 converter.
    yum install gd-progs
    
    pngtogd2 filename.png filename.gd2 0 1
    


Setup Sendmail (Optional)

I recommend setting up sendmail so you can masquerade and setup mail relays as needed.

  1. Install Dependancies
    yum install sendmail sendmail-cf
    
  2. Edit the sendmail config
    vim /etc/mail/sendmail.mc
    
define(`SMART_HOST', `relay.domain.com')dnl
MASQUERADE_AS(`mydomain.com')dnl
MASQUERADE_DOMAIN(localhost)dnl
MASQUERADE_DOMAIN(localhost.localdomain)dnl
  1. Build Config
    /etc/mail/make
    


Upgrade Script Example

#!/bin/bash

# Variables
VERSION=1.0.1
DATE_TIME=$(date +%Y%m%d-%H%M)
TAR_TMP=/tmp/
LOG_FILE=/var/log/nagios/backups.log
OLD_NAGIOS_VER=4.2.2
NEW_NAGIOS_VER=4.2.4
NEW_PLUGIN_VER=2.2.0
BACKUP_DIR=/home/levon/backups/sentinel
BACKUP_TAR=nagios_${OLD_NAGIOS_VER}_backup_${DATE_TIME}.tar
BACKUP_TAR_PATH=${BACKUP_DIR}/${BACKUP_TAR}

# Stop Service
service nagios stop

# Backup Configs
tar -cf $BACKUP_TAR_PATH /etc/httpd/conf.d/nagios.conf
tar -rf $BACKUP_TAR_PATH /etc/init.d/nagios
tar -rf $BACKUP_TAR_PATH /etc/nagios
tar -rf $BACKUP_TAR_PATH /usr/local/nagios
gzip ${BACKUP_TAR_PATH}

# Upgrade Nagios Core
cd $TAR_TMP
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-${NEW_NAGIOS_VER}.tar.gz
tar -zxvf nagios-${NEW_NAGIOS_VER}.tar.gz
cd nagios-${NEW_NAGIOS_VER}
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf

# Upgrade Nagios Plugins
cd $TAR_TMP
wget https://nagios-plugins.org/download/nagios-plugins-${NEW_PLUGIN_VER}.tar.gz
tar -zxvf nagios-plugins-${NEW_PLUGIN_VER}.tar.gz
cd nagios-plugins-${NEW_PLUGIN_VER}
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

service httpd restart
service nagios restart

echo ""
echo "COMPLETED!"
echo ""
echo "Don't forget to edit the following files are the install renames and replaces them with generic configurations"
echo "vim /usr/local/nagios/etc/cgi.cfg"
echo "vim /usr/local/nagios/etc/nagios.cfg"
echo "vim /etc/httpd/conf.d/nagios.conf"
echo ""

Related Articles


Sources