#!/bin/bash
#
##  @Synopsis   Install Script for Naemonbox project
##  Developped by : Mark GADI aka MG-MONITORING
###################################################################
# Naemonbox is developped with GPL Licence 2.0
#
# GPL License: http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
# Naemonbox Install Script
# Usage: bash install.sh
#
###################################################################
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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.
###################################################################
# init variables
rouge="\033[31m"
vert="\033[32m"
jaune="\033[33m"
bleu="\033[34m"
magenta="\033[35m"
defaut="\033[00m"
task=""
nocolor=""

#line="------------------------------------------------------------------------"
line="$magenta------------------------------------------------------------------------\n$defaut"
export line

version="0.0.4"
echo "
####################################################################
#                                                                  #
#                                                                  #
#                                                                  #
#                    Thanks for using Naemonbox                    #
#                       by MG-MONITORING                           #
#                                                                  #
#                        v:$version                                   #
####################################################################
"

###################################
# Create log file
###################################
eval `date "+day=%d; month=%m; year=%Y"`
log_file=/tmp/naemonbox-install-`date +%d%m%Y`.log
###################################
# Variables
###################################

## Versions
nrpe_version="2.15"
nsca_version="2.9.1"
ndoutils_version="1.5.2"
nagvis_version="1.8b1"
nagios_business_process_addon_version="0.9.6"
language_pack_fr_version="0.9.3"
nagtrap_version="1.5.0"
mediawiki_version="1.23.6"
cacti_version="cacti-0.8.8b"
glpi_version="glpi-0.83.8"
fusioninventory_version="metapackage_0.83+2.2.1"
webmin_version="1.710_all"
## Commands
apt="apt-get -q -y --force-yes"
wget="wget --no-check-certificate -c"
check_x64=`uname -a | grep -e "_64"`
server=`hostname -f`
# patch Eric Coquard 06/12/2014
server_short=`hostname`
tar="tar zxf"
ip=`ifconfig eth0 | grep "inet adr" | sed 's/.*adr:\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*/\1/'`

## Script Dir
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  dir="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done

dir="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
bindir="$dir/bin"
###################################
# BEGIN
# Function: installation
###################################

# patch Eric Coquard 29/11/2014
function upgrade_apt () {

echo 'deb http://ftp.fr.debian.org/debian/ wheezy non-free
	deb-src http://ftp.fr.debian.org/debian/ wheezy non-free' > /etc/apt/sources.list.d/wheezy_nonfree.list

apt-get update
}
upgrade_apt


installation() {
touch $log_file
echo "Installation started on "`date` | tee -a $log_file

# Prerequisite
printf "$line"
echo "Install MySQL specific packages and settings          " | tee -a $log_file
printf "$line"

 dbpass="$1"
        [ -n "$dbpass" ] || read -s -p "Enter MySQL root password: " dbpass

$apt install debconf-utils tasksel -y

echo -e "\n---\n"
printf "$bleu Running from $dir\n$defaut"

echo -e "\n--- Install MySQL specific packages and settings ---\n"
echo "postfix postfix/main_mailer_type        select Internet Site" | debconf-set-selections
echo "postfix postfix/mailname string /etc/mailname" | debconf-set-selections
echo "mysql-server mysql-server/root_password password $dbpass" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password $dbpass" | debconf-set-selections
echo "phpmyadmin phpmyadmin/dbconfig-install boolean true" | debconf-set-selections
echo "phpmyadmin phpmyadmin/app-password-confirm password $dbpass" | debconf-set-selections
echo "phpmyadmin phpmyadmin/mysql/admin-pass password $dbpass" | debconf-set-selections
echo "phpmyadmin phpmyadmin/mysql/app-pass password $dbpass" | debconf-set-selections
echo "phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2" | debconf-set-selections

$apt install apache2 wget libapache2-mod-php5 bind9-host dnsutils libbind9-80 libdns88 libisc84 libisccc80 libisccfg82 liblwres80 qstat snmp snmpd snmp-mibs-downloader snmptt | tee -a $log_file
$apt install nmap bc libapache2-mod-fcgid libfontconfig1 libgd2-xpm libjpeg8 libmysqlclient18 libpng12-0 libxpm4 xvfb nagios-plugins rrdtool librrds-perl g++ build-essential php5-cli php5-gd | tee -a $log_file
$apt install libconfig-inifiles-perl libsnmp-session-perl php5-gd php5-sqlite php-pear php-gettext php5-common php5 php5-cgi php5-curl php5-dev php5-imap php5-imagick php5-ldap php5-mcrypt php5-memcache memcached php5-snmp php5-sybase php5-xmlrpc php-apc php5-mysql mysql-server phpmyadmin graphviz libmysqlclient-dev php5-sqlite | tee -a $log_file
$apt install libconfig-inifiles-perl libsnmp-session-perl php5-gd php5-sqlite php-pear php-gettext php5-common php5 php5-cgi php5-curl php5-dev php5-imap php5-imagick php5-ldap php5-mcrypt php5-memcache php5-snmp php5-sybase php5-xmlrpc mysql-server phpmyadmin graphviz libmysqlclient-dev php5-sqlite | tee -a $log_file
$apt install sudo mlocate bsd-mailx mailutils postfix daemon | tee -a $log_file
$apt install rsync sqlite3 libmodule-install-perl perl-modules libparse-edid-perl perl ucf libnet-ip-perl libwww-perl libnet-ssleay-perl libproc-daemon-perl libuniversal-require-perl libproc-pid-file-perl hdparm libfile-which-perl libxml-treepp-perl libyaml-perl libtext-template-perl libjson-perl pciutils libhttp-daemon-perl libio-socket-ssl-perl smartmontools read-edid | tee -a $log_file

ln -s /usr/bin/mail /bin/mail

mkdir /root/mibs_removed
mv /usr/share/mibs/ietf/IPATM-IPMC-MIB /root/mibs_removed/
mv /usr/share/mibs/ietf/SNMPv2-PDU /root/mibs_removed/ 
mv /usr/share/mibs/ietf/IPSEC-SPD-MIB /root/mibs_removed/
mv /usr/share/mibs/iana/IANA-IPPM-METRICS-REGISTRY-MIB /root/mibs_removed/
cp $bindir/default/snmpd /etc/default/
cp $dir/snmp/* /etc/snmp/
cp $bindir/init.d/snmpd  /etc/init.d/snmpd
# patch Eric Coquard 29/11/2014
update-rc.d -f snmpd remove
update-rc.d snmpd defaults

cd $bindir

# Install Naemon Core
printf "$line"
echo "Install Naemon Core                                 " | tee -a $log_file
printf "$line"

dpkg -i naemon/naemon*.deb | tee -a $log_file
cp init.d/naemon /etc/init.d/
update-rc.d -f naemon remove
update-rc.d naemon defaults
cp $dir/contrib/plugins/* /usr/lib/nagios/plugins/


# /usr/bin/naemon -v /etc/naemon/naemon.cfg | tee -a $log_file
# service naemon start

printf "$line"
echo "Install Addon PNP4Nagios                            "| tee -a $log_file
printf "$line"

$tar pnp4nagios-0.6.21.tar.gz
cd pnp4nagios-0.6.21/
./configure --with-nagios-user=naemon --with-nagios-group=naemon | tee -a $log_file
make all | tee -a $log_file
make install | tee -a $log_file
make install-webconf | tee -a $log_file
make install-config | tee -a $log_file
make install-init | tee -a $log_file
update-rc.d npcd defaults
rm /usr/local/pnp4nagios/share/install.php
# patch Eric Coquard 29/11/2014
rm -r $bindir/pnp4nagios-0.6.21/
rm -r /etc/naemon/conf.d/*
cd $dir
cp -r contrib/naemon/* /etc/naemon/ | tee -a $log_file
chown -R naemon.naemon /etc/naemon/ | tee -a $log_file 
cp -r contrib/logos /usr/share/naemon/themes/themes-available/Classic/images/ | tee -a $log_file
cp -r contrib/eventhandlers/ /usr/lib/nagios/plugins/ | tee -a $log_file
cp etc/pnp4nagios.conf /etc/apache2/conf.d/ | tee -a $log_file
service apache2 restart
#cp $dir/contrib/etc/* /etc/naemon/ 
cp $bindir/wkhtmltopdf-amd64  /usr/share/naemon/script/wkhtmltopdf | tee -a $log_file
chmod +x /usr/share/naemon/script/wkhtmltopdf

chmod 644 /etc/naemon/cgi.cfg 
chmod 664 /etc/naemon/conf.d/*.cfg
chmod 664 /etc/naemon/conf.d/templates/*.cfg
chown -R naemon.naemon /etc/naemon/
echo '$USER2$=public' >> /etc/naemon/resource.cfg
cp /usr/share/naemon/root/favicon.ico /var/www/
# installation de wmi
# Prerequisite
$apt install autoconf python libdatetime-perl libconfig-inifiles-perl | tee -a $log_file

# wmic installation for x64
cd $bindir
cp wmi/wmic /usr/bin/ | tee -a $log_file
cp -r wmi/check_wmi_plus.* /usr/lib/nagios/plugins/ | tee -a $log_file

# patch Eric Coquard 06/12/2014
# replace localhost for hostname
mv /etc/naemon/conf.d/localhost.cfg /etc/naemon/conf.d/$server_short.cfg
sed -i "s/localhost/$server_short/g" /etc/naemon/conf.d/$server_short.cfg
service naemon restart
}

# Function: Verify Naemon config files
check() {
printf "$line"
echo "Check Naemon configuration                          " | tee -a $log_file
printf "$line"
/usr/bin/naemon -v /etc/naemon/naemon.cfg | tee -a $log_file
}

# Function: Start Naemon
start() {
printf "$line"
echo "Starting Naemon                                     "| tee -a $log_file
printf "$line"
service npcd start
service apache2 restart
service naemon start
service snmpd restart
echo "Go to URL: http://$ip/naemon/"
echo "User: admin"
snmptrap -v2c -c public 127.0.0.1 '' .1.3.6.1.6.3.1.1.5.2 ifIndex i 2 ifadminStatus i 1 ifOperStatus i 1
}
installation
check
start

# Function: installation_homepage
installation_homepage(){

cd $bindir
$tar html.tgz

printf "$line"
echo "Install Naemonbox Homepage                          " | tee -a $log_file
printf "$line"

mv html /var/www

cat << EOF > /var/www/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="refresh" content="0; url=html/index.php" />
</head>
</html>
EOF
service apache2 restart
}
installation_homepage

# Function: installation_nagvis
installation_nagvis() {

# Prerequisites
printf "$line"
echo "Start Nagvis Installation                           " | tee -a $log_file
printf "$line"
cd $bindir 
$tar nagvis-$nagvis_version.tar.gz 
cd nagvis-$nagvis_version
#./install.sh -s Nagios -n /usr/local/nagios -b /usr/local/bin -p /usr/local/nagvis -W /nagvis -u www-data -g www-data -w /etc/apache2/conf.d -i mklivestatus -l unix:/usr/local/nagios/var/rw/live -q
./install.sh -n /etc/naemon -p /usr/local/nagvis -l "unix:/var/cache/naemon/live" -b mklivestatus -u www-data -g www-data -w /etc/apache2/conf.d -i mklivestatus -a y -o -q

# create a default map
cp $dir/contrib/nagvis/nagvis.ini.php /usr/local/nagvis/etc/
#rm /usr/local/nagvis/share/userfiles/images/maps/*
#rm /usr/local/nagvis/etc/maps/demo-*
cp $dir/contrib/nagvis/monitoring.png /usr/local/nagvis/share/userfiles/images/maps 
cp $dir/contrib/nagvis/monitoring.cfg /usr/local/nagvis/etc/maps
chmod 664 /usr/local/nagvis/share/userfiles/images/maps/monitoring.png
chmod 664 /usr/local/nagvis/etc/maps/monitoring.cfg
rm -r $bindir/nagvis-$nagvis_version
# patch Eric Coquard 06/12/2014
# replace localhost for hostname
sed -i "s/localhost/$server_short/g" /usr/local/nagvis/etc/maps/monitoring.cfg
service apache2 restart
}
installation_nagvis

# Function: installation_glpi
installation_glpi(){
# Prerequisite
printf "$line"
echo "Install common libs and Configure GLPI              " | tee -a $log_file
printf "$line"
$apt install libmodule-install-perl perl-modules libparse-edid-perl perl ucf libnet-ip-perl libwww-perl libnet-ssleay-perl libproc-daemon-perl libuniversal-require-perl libproc-pid-file-perl hdparm libfile-which-perl libxml-treepp-perl libyaml-perl libtext-template-perl libjson-perl pciutils libhttp-daemon-perl libio-socket-ssl-perl smartmontools read-edid | tee -a $log_file
cd $bindir
 
$tar glpi.tar.gz
mv glpi /var/www/
chown -R www-data:www-data /var/www/glpi/
#cp -r $dir/contrib/glpi-plugins/* /var/www/glpi/plugins/
mysqladmin -u root -p"$dbpass" create glpi
mysql -u root -p"$dbpass" glpi < /var/www/glpi/glpi.sql
chown -R www-data:www-data /var/www/glpi/
dpkg -i fusioninventory-agent_2.2.3-3_amd64.deb | tee -a $log_file
#for debian 6
#sed -i "s/#server = http:\/\/server.domain.com\/glpi/server = http:\/\/$ip\/glpi/" /usr/local/etc/fusioninventory/agent.cfg
#for debian 7 on .deb install 
sed -i "s/#server = http:\/\/server.domain.com\/glpi/server = http:\/\/$ip\/glpi/g" /etc/fusioninventory/agent.cfg
sed -i "s/http:\/\/192.168.0.19\/naemon/http:\/\/$ip\/naemon/g" /var/www/glpi/plugins/webservices/scripts/glpi7a.php
# patch Eric Coquard 29/11/2014
sed -i "s/var \$dbpassword= 'secret';/var \$dbpassword= '$dbpass';/g" /var/www/glpi/config/config_db.php
service apache2 restart | tee -a $log_file
fusioninventory-agent --debug --debug
}
installation_glpi

# Function: installation_webmin
installation_webmin(){

printf "$line"
echo "Start Webmin Installation                              " | tee -a $log_file
printf "$line"

# Required Packages 
$apt install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python
dpkg -i webmin_$webmin_version.deb

echo "Go to the URL : https://$server:10000/"
}
installation_webmin

# Function: installation_mediawiki
installation_mediawiki(){

printf "$line"
echo "Start Mediawiki Installation                        " | tee -a $log_file
printf "$line"

cd $bindir 

$tar wiki.tar.gz

mv wiki/ /var/www/
chown -R www-data:www-data /var/www/wiki
mysqladmin -u root -p"$dbpass" create wiki
mysql -u root -p"$dbpass" wiki < /var/www/wiki/wiki.sql

sed -i "s/http:\/\/192.168.0.19/http:\/\/$ip/g" /var/www/wiki/LocalSettings.php
sed -i "s/apache@192.168.0.19/apache@$ip/g" /var/www/wiki/LocalSettings.php
# patch Eric Coquard 29/11/2014
sed -i "s/"secret"/"$dbpass"/g" /var/www/wiki/LocalSettings.php

#echo "Go to URL: http://$ip/wiki/"
#echo "to finish the setup"

}
installation_mediawiki

# Function: installation backup-manager
installation_backup(){
echo "backup-manager  backup-manager/directories  string  /etc /home" | debconf-set-selections
echo "backup-manager  backup-manager/backup-repository string /var/archives" | debconf-set-selections

$apt install backup-manager

cat << EOF > /etc/backup-manager.sh && chmod 700 /etc/backup-manager.sh
#!/bin/sh
test -x /usr/sbin/backup-manager || exit 0
/usr/sbin/backup-manager
EOF
cp /etc/backup-manager.conf /etc/backup-manager.conf.original
cp $dir/contrib/backup-manager.conf /etc/
$dir/test.sh
echo "Go to URL: http://$ip/"
}
installation_backup
