How to set up Nagios in Ubuntu

  linux
# How to install and set up Nagios

# read how-to: http://nagios.sourceforge.net/docs/3_0/quickstart.html
sudo apt-get install apache2
sudo apt-get install libapache2-mod-php5
sudo apt-get install build-essential

# get data about your operating system:
uname -a
lsb_release -a

# With Ubuntu 6.10, install the gd2 library with this command:
# sudo apt-get install libgd2-dev
# With Ubuntu 7.10, the gd2 library name has changed, so you'll need to use the following:
# sudo apt-get install libgd2-xpm-dev

sudo apt-get install libgd2-xpm-dev

# add a new user
su
/usr/sbin/useradd -m -s /bin/bash nagios
passwd nagios

# disable SSH login for this user
nano /etc/ssh/sshd_config

# at the end of the file, add the entry, save, close the file
DenyUsers nagios

# restart ssh:
sudo /etc/init.d/ssh restart

# On older Ubuntu server editions (6.01 and earlier), you will need to also add a nagios group 
# (it's not created by default). You should be able to skip this step on desktop, or newer 
# server editions of Ubuntu.

# Add group "nagios" and add user "nagios" to the group "nagios"
/usr/sbin/groupadd nagios
/usr/sbin/usermod -G nagios nagios


# Create a new nagcmd group for allowing external commands to be submitted 
# through the web interface. Add both the nagios user and the apache user 
# to the group.

/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd www-data


#Download Nagios and the Plugins
#Create a directory for storing the downloads.
mkdir ~/downloads
cd ~/downloads

# download nagios
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.3.1.tar.gz

# untar
tar xvf nagios-3.3.1.tar.gz
cd nagios

# Run the Nagios configure script, passing the name of the group you created 
# earlier like so:
./configure --with-command-group=nagcmd

# Compile the Nagios source code.
make all

# Install binaries, init script, sample config files and set permissions on the 
# external command directory.
make install
make install-init
make install-config
make install-commandmode

# Don't start Nagios yet - there's still more that needs to be done... 

# edit config
nano /usr/local/nagios/etc/objects/contacts.cfg
# find nagiosadmin and change the email to your email address


# Install the Nagios web config file in the Apache conf.d directory.
make install-webconf

# Create a nagiosadmin account for logging into the Nagios web interface. 
# Remember the password you assign to this account - you'll need it later.
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

#Restart Apache to make the new settings take effect.
/etc/init.d/apache2 reload

# Extract the Nagios plugins source code tarball.
cd ~/downloads
tar xzf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15

# Compile and install the plugins.
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

# install mail
sudo apt-get install mailutils
sudo apt-get install postfix

#Start Nagios
#Configure Nagios to automatically start when the system boots.
ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios

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


# if you get an error, such as this one:
# Error in configuration file '/usr/local/nagios/etc/nagios.cfg' - 
# Line 465 (Check result path is not a valid directory)
nano /usr/local/nagios/etc/nagios.cfg
# if this is the line: check_result_path=/usr/local/nagios/var/spool/checkresults
# then create the missing directory
mkdir /usr/local/nagios/
mkdir /usr/local/nagios/var/
mkdir /usr/local/nagios/var/spool/
mkdir /usr/local/nagios/var/spool/checkresults
chown -R nagios:nagios /usr/local/nagios

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

# you hope to see this line:
# Things look okay - No serious problems were detected during the pre-flight check

#If there are no errors, start Nagios.
/etc/init.d/nagios start
#Starting nagios: done.

# Login to the Web Interface
# You should now be able to access the Nagios web interface at the URL below. 
# You'll be prompted for the username (nagiosadmin) and password you specified earlier.
http://localhost/nagios/
# or http://yourIP/nagios


# You'll have to edit the Nagios email notification commands found 
# in /usr/local/nagios/etc/objects/commands.cfg and change any '/bin/mail' 
# references to '/usr/bin/mail'. 
nano /usr/local/nagios/etc/objects/commands.cfg 

# Once you do that you'll need to restart Nagios 
# to make the configuration changes live.
sudo /etc/init.d/nagios restart