#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
Stack=$1
if [ "${Stack}" = "" ]; then
    Stack="install"
else
    Stack=$1
fi
echo "+---------------------------------------+";
echo "Creation time:2017/09/24,Update time:2018/05/08";
echo "tiewangwei website:http://www.tieww.com"
echo "Usage method:install(sh $0) Uninstall(sh $0 u)"
echo "Combination baota linux panel installation zabbix script";
echo "+---------------------------------------+";
install_zabbix()
{
pwd=`pwd`;
zabbix_url="http://pkgs.fedoraproject.org/repo/pkgs/zabbix/zabbix-3.0.1.tar.gz/890d9eec69304ad552959fabe0a5d122/";
zabbix_d="zabbix-3.0.1";
zabbix_v="zabbix-3.0.1.tar.gz";
read -p "Enter zabbix database password:" zabbix_pass
read -p "zabbix site directory, for example (/www/default):" webdir
if [[ "$webdir" == "" ]] || [[ ! -e $webdir ]]; then
echo "The input directory is empty or does not exist.";
exit
fi
yum install net-snmp-devel curl curl-devel -y
if [ ! -e  $pwd/$zabbix_v ]; then
wget $zabbix_url$zabbix_v;
fi
tar zxf $pwd/$zabbix_v;
mysql -uzabbix -p$zabbix_pass -e "use zabbix;source $pwd/$zabbix_d/database/mysql/schema.sql;";
echo -e "Prompt:　\033[32mschema.sql import was successful.\033[0m";
mysql -uzabbix -p$zabbix_pass -e "use zabbix;source $pwd/$zabbix_d/database/mysql/images.sql;";
echo -e "Prompt:　\033[32mimages.sql import was successful.\033[0m";
mysql -uzabbix -p$zabbix_pass -e "use zabbix;source $pwd/$zabbix_d/database/mysql/data.sql;";
echo -e "Prompt:\033[32mdata.sql import was successful.\033[0m";
sleep 5
groupadd zabbix && useradd zabbix -g zabbix -s /bin/false
ln -s /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2 && /sbin/ldconfig
cd $pwd/$zabbix_d
mysql_config=`find / -name mysql_config |awk 'NR==1'`;
if [ $mysql_config == '' ]; then
echo "Zabbix exit installation.";
echo "Mysql_config does not exist, please check whether mysql has been installed?";
exit
fi
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-net-snmp --with-libcurl --enable-proxy --with-mysql=$mysql_config && make && make install
if [ "$?" -eq "0" ]; then
echo -e "Prompt:\033[32mzabbix compiled successfully,Start configuring ZABBIX.\033[0m";
else
echo "zabbix failed to compile.";
exit
fi
sleep 3
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/ && ln -s /usr/local/zabbix/bin/* /usr/local/bin/
cp /etc/services /etc/servicesbak
cat >>/etc/services<<EOF
# Zabbix
zabbix-agent 10050/tcp # Zabbix Agent
zabbix-agent 10050/udp # Zabbix Agent
zabbix-trapper 10051/tcp # Zabbix Trapper
zabbix-trapper 10051/udp # Zabbix Trapper
EOF
sed -i '/# DBHost=localhost/a\DBHost=localhost' /usr/local/zabbix/etc/zabbix_server.conf
sed -i '/# DBPassword=/a\'DBPassword=$zabbix_pass'' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's/^# DBSocket=\/t\mp\/\mysql.sock/DBSocket=\/t\mp\/\mysql.sock/g' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's/^# AlertScriptsPath=${datadir}\/\zabbix\/a\lertscripts/AlertScriptsPath=\/u\sr\/l\ocal\/\zabbix\/\share\/\zabbix\/al\ertscripts/g' /usr/local/zabbix/etc/zabbix_server.conf

sed -i '0,/# Include=/{//s/.*/Include=\/u\sr\/l\ocal\/\zabbix\/\etc\/\zabbix_agentd.conf.d\/\ \n&/}' /usr/local/zabbix/etc/zabbix_agentd.conf
sed -i '/# UnsafeUserParameters=0/a\UnsafeUserParameters=1' /usr/local/zabbix/etc/zabbix_agentd.conf
cp $pwd/zabbix-3.0.1/misc/init.d/fedora/core/zabbix_server /etc/rc.d/init.d/zabbix_server
cp $pwd/zabbix-3.0.1/misc/init.d/fedora/core/zabbix_agentd /etc/rc.d/init.d/zabbix_agentd
chmod +x /etc/rc.d/init.d/zabbix_server && chmod +x /etc/rc.d/init.d/zabbix_agentd
chkconfig zabbix_server on && chkconfig zabbix_agentd on
sed -i "1a BASEDIR=/usr/local/zabbix/" /etc/rc.d/init.d/zabbix_server /etc/rc.d/init.d/zabbix_agentd
cp -r $pwd/zabbix-3.0.1/frontends/php/* $webdir/
chattr -i $webdir/.user.ini
chown www.www -R $webdir/
chattr +i $webdir/.user.ini
if [ -e /usr/bin/systemctl ]; then
systemctl daemon-reload
fi
service zabbix_server start && service zabbix_server restart 1>/dev/null 2>&1
service zabbix_agentd start && service zabbix_agentd restart 1>/dev/null 2>&1
if [[ -f /tmp/zabbix_server.pid ]] && [[ -f /tmp/zabbix_agentd.pid ]]; then
echo -e "Prompt:\033[32mzabbix installed successfully.\033[0m";
echo "Zabbix website installation path:"$webdir;
echo "Tutorial address http://www.tieww.com/2017/09/24/688.html";
else
echo -e "Prompt:\033[31mzabbix installation failed.\033[0m";
echo "Tutorial address http://www.tieww.com/2017/09/24/688.html";
fi
}
uninstall_zabbix()
{
	if [[ -f /etc/rc.d/init.d/zabbix_server ]] && [[ -f /etc/rc.d/init.d/zabbix_agentd ]]; then
		read -p "zabbix site directory, for example (/www/default):" zabbixdir
		service zabbix_server stop && service zabbix_agentd stop
		userdel -r zabbix && groupdel zabbix
		rm -rf /usr/lib/libiconv.so.2 && /sbin/ldconfig
		rm -rf /usr/local/sbin/zabbix_agentd /usr/local/sbin/zabbix_server /usr/local/sbin/zabbix_proxy /usr/local/bin/zabbix_get /usr/local/bin/zabbix_sender
		if [ -f /etc/servicesbak ]; then
		rm -rf /etc/services && mv /etc/servicesbak /etc/services
		fi
		rm -rf /usr/local/zabbix /etc/rc.d/init.d/zabbix_server /etc/rc.d/init.d/zabbix_agentd 
		chattr -i $zabbixdir/.user.ini && mv $zabbixdir/.user.ini /home
		rm -rf $zabbixdir/* && mv /home/.user.ini $zabbixdir/.user.ini
		echo -e "Prompt:\033[32mzabbix uninstall successfully.\033[0m";
		echo "Tutorial address http://www.tieww.com/2017/09/24/688.html";
	else
		echo "Zabbix_server, zabbix_agentd service does not exist";
		echo "Tutorial address http://www.tieww.com/2017/09/24/688.html";
	fi
	
}

case "${Stack}" in
    install)
        install_zabbix
        ;;
	u)
		uninstall_zabbix
        ;;
    *)
       echo "Usage: $0 {install|u}"
        ;;
esac
