#!/bin/sh

#############################################################################
#
# This MRTG installation script has been developed
# by Jesper Knudsen at http://sme.swerts-knudsen.dk
#
# It installs SNMP and MRTG and creates a HTML index page in the mrtg
# ibay.
#
# Revision History:
#
# Jan 5, 2004: 	Initial revision history. Added check for server only
#		Installation
# Jan 21, 2004: Make sure that the all-ip will be overwritten and not 
#		appended to.
# Jan 26, 2004: Added support for Clam Antivirus and SpamAssassin
#		statistics if found installed.
# July 6, 2004: Added support for download of RPMs from mirror site
#               sme.swerts-knudsen.com if primary site is not
#               responding
#############################################################################


if [ -d /home/e-smith/files/ibays/mrtg ]
then

cls()
{
for (( i=1 ; i<35 ; i++ ))
do
echo ""
done
}

cls
echo "========================================================"
echo "= MRTG Installation Script                             ="
echo "=                                                      ="
echo "= This script will install MRTG on your system and     ="
echo "= monitor your CPU utilization, LAN/WAN links and      ="
echo "= if either SpamAssassin or ClamAV is installed also   ="
echo "= monitor them.                                        ="
echo "=                                                      ="
echo "= No express or implied warranties are provided and its="
echo "= usage is at your own risk.                           ="
echo "=                                                      ="
echo "= If you feel confortable with the above then press    ="
echo "= enter if not pres Ctrl+C to abort the installation   ="
echo "= script.                                              ="
echo "=                                                      ="
echo "========================================================"
read x 
cls 

PRIMARY=http://sme.swerts-knudsen.com
SECONDARY=http://sme.swerts-knudsen.dk

# determine download server....
mkdir downloadtest
cd downloadtest
wget -q --timeout=4 $PRIMARY/index.html
if [ -f index.html ]
then
# download from primary site OK
SITE=$PRIMARY
else
wget -q --timeout=4 $SECONDARY/index.html
if [ -f index.html ]
then
# download from mirror site
SITE=$SECONDARY
fi
fi
echo "Downloading RPMs and files from $SITE"
cd ..
rm -rf downloadtest

echo "Installing MRTG Monitoring for LAN/WAN, Disk and CPU in mrtg ibay"

rpm -qa > rpmlist

if [ `grep -c spamassassin rpmlist` -eq 0 ]
then
echo "No Spamassassin Installed"
else
echo "SpamAssassin found - Installing MRTG Stats for SpamAssassin"
SA_FOUND="YES"
fi

if [ `grep -c clamav rpmlist` -eq 0 ]
then
echo "No ClamAV Installed"
else
echo "Clam Antivirus found - Installing MRTG Stats for Clam Antivirus"
CLAM_FOUND="YES"
fi

# First install the RPM's
if [ `grep -c ucd-snmp rpmlist` -eq 0 ]
then
rpm -Uvh $SITE/downloads/MRTG/ucd-snmp-4.2.5-7.73.0.i386.rpm
fi

if [ `grep -c ucd-snmp-utils rpmlist` -eq 0 ]
then
rpm -Uvh $SITE/downloads/MRTG/ucd-snmp-utils-4.2.5-7.73.0.i386.rpm
fi

if [ `grep -c mrtg rpmlist` -eq 0 ]                                                         
then
rpm -Uvh $SITE/downloads/MRTG/mrtg-2.9.17-3.i386.rpm
fi

# remove the rpmlist again
rm -f rpmlist

# then change hosts.allow to allow local access to the SNMP deamon
mkdir -p /etc/e-smith/templates-custom/etc/hosts.allow
echo "snmpd: 127.0.0.1" > /etc/e-smith/templates-custom/etc/hosts.allow/snmpd
/sbin/e-smith/expand-template /etc/hosts.allow

# now we need to modify or create the snmp config file
# first make a backup of the old 
cd /etc/snmp
mv -f snmpd.conf snmpd.conf.old
wget $SITE/downloads/MRTG/snmpd.conf

# You now want to make sure that the SNMP deamon is restarted when/if the server is rebooted
cd /etc/rc.d/rc7.d
ln -s /etc/rc.d/init.d/snmpd S95snmpd
/sbin/e-smith/config set snmpd service InitscriptOrder 95 status enabled

# Now make sure to start the SNMP deamon to have the new setting take effect
service snmpd restart

# Create an HTML index file showing the monitored stats
cd /home/e-smith/files/ibays/mrtg/html
rm -f index.html
rm -f index.htm 
wget $SITE/downloads/MRTG/mrtg-l.png
wget $SITE/downloads/MRTG/mrtg-m.png
wget $SITE/downloads/MRTG/mrtg-r.png

# now place some MRTG configuration files
cd /etc/mrtg
mv -f mrtg.cfg mrtg.cfg.old

# now check whether this is a server-gateway or just a server
# by checking for 2 NICs (eth1 is the second)
if [ `ifconfig | grep -c eth1 ` -eq 1 ]
then
TYPE="Server-Gateway"
echo "Server-Gateway Installation"

wget $SITE/downloads/MRTG/mrtg.cfg

# get the correct HTML index file
cd /home/e-smith/files/ibays/mrtg/html

wget $SITE/downloads/MRTG/index.html.top 

if [ $SA_FOUND ]
then
wget $SITE/downloads/MRTG/spam.index 
cat spam.index >> index.html.top
fi  

if [ $CLAM_FOUND ]
then
wget $SITE/downloads/MRTG/virus.index 
cat virus.index >> index.html.top
fi

wget $SITE/downloads/MRTG/index.html.footer 
cat index.html.footer >> index.html.top

mv -f index.html.top index.html

else
TYPE="Server"
echo "Server only Installation"

wget $SITE/downloads/MRTG/mrtg_server.cfg
mv -f mrtg_server.cfg mrtg.cfg

cd /home/e-smith/files/ibays/mrtg/html
wget $SITE/downloads/MRTG/index.html.server.top

if [ $SA_FOUND ]
then
wget $SITE/downloads/MRTG/spam.index
cat spam.index >> index.html.server.top
fi

if [ $CLAM_FOUND ]
then
wget $SITE/downloads/MRTG/virus.index
cat virus.index >> index.html.server.top
fi  

wget $SITE/downloads/MRTG/index.html.footer
cat index.html.footer >> index.html.server.top

mv -f index.html.server.top index.html

fi

# create an update file
echo "/usr/bin/mrtg /etc/mrtg/mrtg.cfg;" > /etc/mrtg/all-ip

if [ $SA_FOUND ]
then

cd /etc/mrtg
wget $SITE/downloads/MRTG/spamassassin.cfg
wget $SITE/downloads/MRTG/spam-stats
wget $SITE/downloads/MRTG/spamstats.pl
chmod 755 spamstats.pl
chmod 755 spam-stats
echo "/usr/bin/mrtg /etc/mrtg/spamassassin.cfg;" >> /etc/mrtg/all-ip

fi

if [ $CLAM_FOUND ]
then

cd /etc/mrtg
wget $SITE/downloads/MRTG/virus.cfg
wget $SITE/downloads/MRTG/virus-stats  
wget $SITE/downloads/MRTG/virusstats.pl
chmod 755 virusstats.pl
chmod 755 virus-stats
echo "/usr/bin/mrtg /etc/mrtg/virus.cfg;" >> /etc/mrtg/all-ip

fi

# now make sure stats are updated every 5 minutes via a crontab entry
chmod 755 /etc/mrtg/all-ip
mkdir -p /etc/e-smith/templates-custom/etc/crontab/
echo "0-59/5 * * * * root /etc/mrtg/all-ip" > /etc/e-smith/templates-custom/etc/crontab/mrtg
/sbin/e-smith/expand-template /etc/crontab


# now run all-ip twice to get rid of the error messages in the two first runs.
cd /etc/mrtg
./all-ip > /dev/null
./all-ip > /dev/null

# Now notify Swerts-Knudsen that you have installed - only so that I can enhance
# the scripts that are most in use               

ERR=`wget -q -t 1 -T 2 --delete-after http://sme.swerts-knudsen.dk/cgi-bin/egometer?MRTG_Script`

#rpm -qa | grep SMEServer | mail install@swerts-knudsen.dk -s "MRTG Installed - $TYPE (SA=$SA_FOUND, CLAM=$CLAM_FOUND, SITE=$SITE)"

else
echo MRTG ibays does not exist - exiting!!!
fi

