#!/bin/bash
# This script comes with no warranty ...use at own risk
# Copyright (C) 2010   Mike Weber
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307   USA
#####################################################################
# The purpose of the script is to set up a working Nagios Server on
# a CentOS 5.5 distribution.  The script was specifically designed
# to work on the RackSpace.com Cloud
# Installs based on RPM Repository
# Created September 21, 2010
# USE AT YOUR OWN RISK
#####################################################################
nagios="/etc/nagios/nagios.cfg"
contacts="/etc/nagios/objects/contacts.cfg"
index="/var/www/html/index.html"
hosts="/etc/nagios/objects/hosts.cfg"
services="/etc/nagios/objects/services.cfg"

nagiosbk="/etc/nagios/nagios.cfg_bk"
contactsbk="/etc/nagios/objects/contacts.cfg_bk"
hostsbk="/etc/nagios/objects/hosts.cfg"
servicesbk="/etc/nagios/objects/services.cfg"

# Script Must Run as root
if [[ $EUID -ne 0 ]]; then
	echo "This script must be run as root" 1>&2
	exit 1
fi

# Installing Applications
yum install -y httpd php
rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
yum install -y nagios nagios-plugins


echo "Making Backup Copies of Files"


if [ -f $nagiosbk ]
        then
        echo "Nagios Backup Exists"
        else
        cp $nagios $nagiosbk
fi
if [ -f $contactsbk ]
        then
        echo "Contacts Backup Exists"
        else
        cp $contacts $contactsbk
fi


echo "Please enter the nagios admin email."
read admin
sed 's/nagios@localhost/'$admin/ $contacts > /tmp/contacts
mv /tmp/contacts $contacts
rm -f /tmp/contacts

echo "#####################################################"
echo "STARTING APACHE AND CHECKING PORT"
echo "#####################################################"

service httpd start > /dev/null
service httpd reload

m=$(/bin/netstat -aunt | grep -vE '^Active|Proto' | grep "80" | awk '{ print $4}' | cut -d: -f4)

echo "Found Port# $m"

if [ "$m" -eq "80" ];
        then
        echo "APACHE UP AND RUNNING :)"
        else
        echo "APACHE DEAD :("
fi
echo "Please enter your IP Address for web access."
read ip
iptables -I INPUT -p tcp -s $ip --dport 80 -j ACCEPT
iptables -I INPUT -p tcp -s 127.0.0.1 --dport 80 -j ACCEPT

echo "#####################################################"
echo "STARTING NAGIOS AND CONFIGURING ACCESS"
echo "#####################################################"

service nagios start > /dev/null
service nagios reload

echo " Please enter the Password for the Nagios admin, username is nagiosadmin."
username=nagiosadmin
cd /etc/nagios
htpasswd -c htpasswd.users $username

chown apache:apache /etc/nagios/htpasswd.users
chmod 600 /etc/nagios/htpasswd.users

echo "#####################################################"
echo "CHECKING FOR INDEX.HTML"
echo "#####################################################"

if [ -f $index ]
        then
        echo "Index.html Exists"
        else
        touch $index
        chown apache:apache $index
	chmod 755 $index
fi
echo "#####################################################"
echo "SETTING UP MONITORING FOR WEB,MAIL, AND SSH"
echo "#####################################################"


if [ -f $hosts ]
        then
        echo "Hosts File Exists"
        else
        touch $hosts
echo "
define host{
        use                             generic-host
        host_name                       web
        alias                           Web Server
        address                         72.14.213.106
        check_command                   check_http
        max_check_attempts              10
        notification_interval           120
        notification_period             24x7
        notification_options            d,u,r
        contact_groups                  admins
}
define host{
        use                             generic-host
        host_name                       mail
        alias                           Mail Server
        address                         google.com.s9a1.psmtp.com
        check_command                   check_smtp
        max_check_attempts              10
        notification_interval           120
        notification_period             24x7
        notification_options            d,u,r
        contact_groups                  admins
}" > $hosts
fi
if [ -f $hostsbk ]
        then
        echo "Hosts File Backup Exists"
        else
        cp $hosts $hostsbk
fi

if [ -f $services ]
        then
        echo "Services File Exists"
        else
        touch $services
echo "
define service{
        use                             generic-service
        host_name                       web
        service_description             HTTP
        check_command                   check_http
}
define service{
        use                             generic-service
        host_name                       mail
        service_description             SMTP
        check_command                   check_smtp
}" > $services
fi
cp $nagios /tmp/nagios
echo "cfg_file=/etc/nagios/objects/hosts.cfg" >> /tmp/nagios
echo "cfg_file=/etc/nagios/objects/services.cfg" >> /tmp/nagios
mv /tmp/nagios $nagios
rm -f /tmp/nagios

service nagios restart > /dev/null 2>&1



