Setting up Nagios 3.3.1, a scripted guide

Since I wanted to automate the task of setting up a clean install of Nagios, I’ve made a script which will setup Nagios with apache from source. This script can be used as a how-to guide for anyone who’s trying to setup nagios for the first time. However, the main purpose is to enable me to quickly deploy a Nagios installation using an existing script.

#
# Setting up nagios 3.3.1 + nagios plugins 1.4.15 How-To Guide
#
# This guide will compile nagios from source, configure apache to
# serve nagios from the /nagios/ directory, and basic configuration
# of nagios.
#
# Nagios will be installed in the default /usr/local/nagios/
# directory. Change this by providing the --prefix argument when
# configuring.
#
# Please read through the script throughly. Any known issues with
# this guide will be discussed in the troubleshooting section below.
#
# Requisites: Apache + PHP Setup, GCC, all running under CentOS 6
# Author: Leo Leung (Jan 15, 2012)
#
#######################################################################

# using /root as our working directory
cd ~

# download source
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.3.1.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz

# extract source
tar -xzf nagios-3*.tar.gz
tar -xzf nagios-plugins*.tar.gz

# add user 'nagios' as a system user, add apache to the group.
useradd -r -d /usr/local/nagios -s /bin/bash nagios
usermod -G nagios -a apache

# Compile nagios
cd ~/nagios
./configure --with-nagios-user=nagios --with-nagios-grp=nagios
make all
make install
make install-init
make install-config

# Compile nagios plugins
cd ~/nagios-plugins*
./configure
make install

# Configure apache
cd /etc/httpd/conf.d/

# vi nagios.conf (Put the following into nagios.conf):
# ScriptAlias /nagios/cgi-bin/ /usr/local/nagios/sbin/
#
# AllowOverride AuthConfig
# Options ExecCGI
# Allow from all
# Order allow,deny
#

#
# Alias /nagios/ /usr/local/nagios/share/
#
# Options None
# AllowOverride AuthConfig
# Order allow,deny
# Allow from all
#

echo "ScriptAlias /nagios/cgi-bin/ /usr/local/nagios/sbin/" >> nagios.conf
echo "" >> nagios.conf
echo " AllowOverride AuthConfig" >> nagios.conf
echo " Options ExecCGI" >> nagios.conf
echo " Allow from all" >> nagios.conf
echo " Order allow,deny" >> nagios.conf
echo "
" >> nagios.conf
echo "" >> nagios.conf
echo "Alias /nagios/ /usr/local/nagios/share/" >> nagios.conf
echo "" >> nagios.conf
echo " Options None" >> nagios.conf
echo " AllowOverride AuthConfig" >> nagios.conf
echo " Order allow,deny" >> nagios.conf
echo " Allow from all" >> nagios.conf
echo "
" >> nagios.conf

#
# Protect the nagios directory via htpasswd
#
# CONFIGURATION NOTE:
# The default nagios admin username is 'nagiosadmin'. If you wish
# to change this or add another user to have access to the nagios
# system, be sure to add the username to cgi.cfg as a comma
# delimited list.
#
cd /usr/local/nagios/share/

# vi .htaccess (Put the following into .htaccess)
# AuthName "Nagios Access"
# AuthType Basic
# AuthUserFile /usr/local/nagios/etc/htpasswd.users
# require valid-user
echo "AuthName \"Nagios Access\"" >> .htaccess
echo "AuthType Basic" >> .htaccess
echo "AuthUserFile /usr/local/nagios/etc/htpasswd.users" >> .htaccess
echo "require valid-user" >> .htaccess

cp .htaccess /usr/local/nagios/sbin/
cd /usr/local/nagios/etc/
htpasswd -bc htpasswd.users nagiosadmin defaultpassword
chmod 644 htpasswd.users
chown apache:apache htpasswd.users

# apply apache configuration
/etc/init.d/httpd restart

#
# Configuring Nagios
#
cd /usr/local/nagios/etc/

#
# CONFIGURATION NOTE:
# You will want to edit this file manually. However, to get nagios
# working through apache (via CGI), we will need to enable
# check_external_commands.
#
# vi nagios.cfg
# /check_external_commands
# * Set variable to 1 *
#
sed -i -e 's/check_external_commands=0/check_external_commands=1/g' nagios.cfg

# Create the var directory needed by nagios. If you change the paths in
# nagios.cfg to /var, please change the following command accordingly.
mkdir -p /usr/local/nagios/var/spool/checkresults
mkdir -p /usr/local/nagios/var/rw
chown -R nagios:nagios /usr/local/nagios/var/

#
# CONFIGURATION NOTE:
# As noted above, if you wish to add new users to the system, you
# must edit cgi.cfg and add the appropriate usernames for certain
# access roles.
# Example:
# authorized_for_system_information=nagiosadmin
# will let user 'nagiosadmin' access system information. To add user
# 'newadmin', change the line to:
# authoriozed_for_system_information=nagiosadmin,newadmin
# to let anyone who has authenticated, use *
#
# vi cgi.cfg
#
vi cgi.cfg

# Verify the configuration
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# If everything from above looks okay, then we should be set to go.
/etc/init.d/nagios start

#
# From here, you should have nagios setup. Going to http://server/nagios/
# should prompt for a username/password and display the nagios web panel.
#
# If you're seeing errors, please refer to the troubleshooting section.
#
# Your next order of business is to configure hosts and services to monitor.
# But that's beyond the scope of this guide.
#

####################################################################
#
# Troubleshooting
#
# PROBLEM 1: When running 'make install' on nagios, an error:
#
# /usr/bin/install: omitting directory `includes/rss/extlib'
# /usr/bin/install: omitting directory `includes/rss/htdocs'
# /usr/bin/install: omitting directory `includes/rss/scripts'
# make[1]: *** [install] Error 1
# make[1]: Leaving directory `/root/nagios/html'
# make: *** [install] Error 2
#
# When accessing main.php, you get a PHP error.
#
# SOLUTION 1:
# It appears that the make file isn't copying the php libraries over properly.
# To fix this, you'll need to copy the files manually:
#
# rm -rf /usr/local/nagios/share/includes/rss/
# cp -R /root/nagios/html/includes/rss/ /usr/local/nagios/share/includes/rss/
# chown -R nagios:nagios /usr/local/nagios/share/includes/rss/
#
# ------------------------
#
# PROBLEM 2: From the web panel, you get the following error when
# updating services or notification settings:
# Error: Could not open command file '/usr/local/nagios/var/rw/nagios.cmd' for update!
#
# SOLUTION 2:
# You need to add apache to the nagios group. Do this by running:
#
# usermod -G nagios -a apache
#
# You might need to restart apache for the changes to take affect.
# /etc/init.d/httpd restart
#
# ------------------------
#
# PROBLEM 3: The .cgi pages are giving 500 errors. error_log shows something similar
# to: (13)Permission denied: exec of '/usr/local/nagios/sbin/status.cgi' failed
#
# SOLUTION 3: There is a permission issue. Try disabling SELinux and see what happens.
#

Leave a Reply

Your email address will not be published.