#!/bin/bash
#******************************************
# Author:       Jason Zhao
# Email:        zhaoshundong@opsany.com
# Organization: https://www.opsany.com/
# Description:  Zabbix Agent Install Script
#******************************************
#Data/Time
CTIME=$(date "+%Y-%m-%d-%H-%M")

#Shell ENV
SHELL_NAME="zabbix-agent.sh"
SHELL_LOG="/usr/local/zabbix-agent.log"
INSTALL_FAILED_MSG="zabbix agent 5.0 install failed. detail see: /usr/local/zabbix-agent.log"

#Log Record
shell_log(){
  LOG_INFO=$1
  echo "----------------$CTIME ${SHELL_NAME} : ${LOG_INFO}----------------"
  echo "$CTIME ${SHELL_NAME} : ${LOG_INFO}" >> ${SHELL_LOG}
}

# OS Type
OS_ARCH=$(LANG=C && lscpu | grep Architecture | awk -F ' ' '{print $2}')


if [ -f /etc/os-release ];then
    OS_TYPE=$(grep -E "^ID=" /etc/os-release | awk -F'=' '{print $2}')
    OS_VER=$(grep -E "^VERSION_ID=" /etc/os-release | awk -F'=' '{print $2}')
    OS=`echo ${OS_TYPE}_${OS_VER} | awk '{gsub(/"/, "", $1);print $1}'`

elif [ -f /etc/redhat-release ];then
    OS_TYPE="CENTOS"
    CENTOS6=$(cat /proc/version | grep 'el6')
    if [ -n "$CENTOS6" ];then
        OS_VER=6
	OS=${OS_TYPE}_${OS_VER}
    fi
else
    shell_log "This OS is not supported!"
fi

SUPPORT_OS_VER=("Ubuntu_14.04" \
                "Ubuntu_16.04" \
                "Ubuntu_18.04" \
                "Ubuntu_20.04" \
                "Ubuntu_22.04" \
                "CentOS_6" \
                "CentOS_7" \
                "CentOS_8" \
                   )
# LinLin added , for disable old zabbix-agent
zabbix_agent_old_exist_stop(){
    if systemctl is-active --quiet zabbix-agent ; then
        shell_log "detect old zabbix-agent running"
        systemctl disable zabbix-agent
        systemctl stop zabbix-agent
        shell_log "stop old zabbix-agent"
    else
        shell_log "this is a clean and new system"
    fi
}
# Install Zabbix Agent
zabbix_agent_install(){

    # for ubuntu
    if [ "$OS" == "ubuntu_14.04" ];then
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
	curl --insecure -o /usr/local/zabbix-agent_5.0.24-1+trusty_amd64.deb ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent_5.0.24-1+trusty_amd64.deb
        dpkg -i /usr/local/zabbix-agent_5.0.24-1+trusty_amd64.deb
        sed -i "s#Server=127.0.0.1#Server=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s#ServerActive=127.0.0.1#ServerActive=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agentd.conf
        /etc/init.d/zabbix-agent restart
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG 
        fi
    elif [ "$OS" == "ubuntu_16.04" ];then
        zabbix_agent_old_exist_stop
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
	curl --insecure -o /usr/local/zabbix-agent_5.0.24-1+xenial_amd64.deb ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent_5.0.24-1+xenial_amd64.deb
        dpkg -i /usr/local/zabbix-agent_5.0.24-1+xenial_amd64.deb
        sed -i "s/Server=.*/Server=$ZABBIX_SERVER,172.17.0.0\/16/g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s/ServerActive=.*/ServerActive=$ZABBIX_SERVER/g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agentd.conf
        systemctl enable zabbix-agent
        systemctl restart zabbix-agent
        sleep 3 
        systemctl is-active --quiet zabbix-agent
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent Install Succeed"
            status=`systemctl is-active zabbix-agent`
            shell_log "Zabbix Agent status: ${status}"
            shell_log "detail can see in ${SHELL_LOG}"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "ubuntu_18.04" ];then
        zabbix_agent_old_exist_stop
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
	curl --insecure -o /usr/local/zabbix-agent2_5.0.24-1+bionic_amd64.deb ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent2_5.0.24-1+bionic_amd64.deb
        dpkg -i /usr/local/zabbix-agent2_5.0.24-1+bionic_amd64.deb
        sed -i "s/Server=.*/Server=$ZABBIX_SERVER,172.17.0.0\/16/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/ServerActive=.*/ServerActive=$ZABBIX_SERVER/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agent2.conf
        systemctl enable zabbix-agent2
        systemctl restart zabbix-agent2
        sleep 3 
        systemctl is-active --quiet zabbix-agent2
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            status=`systemctl is-active zabbix-agent2`
            shell_log "Zabbix Agent2 status: ${status}"
            shell_log "detail can see in ${SHELL_LOG}"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "ubuntu_20.04" ];then
        zabbix_agent_old_exist_stop
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
	curl --insecure -o /usr/local/zabbix-agent2_5.0.24-1+focal_amd64.deb ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent2_5.0.24-1+focal_amd64.deb
        dpkg -i /usr/local/zabbix-agent2_5.0.24-1+focal_amd64.deb
        sed -i "s/Server=.*/Server=$ZABBIX_SERVER,172.17.0.0\/16/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/ServerActive=.*/ServerActive=$ZABBIX_SERVER/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agent2.conf
        systemctl enable zabbix-agent2
        systemctl restart zabbix-agent2
        sleep 3 
        systemctl is-active --quiet zabbix-agent2
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            status=`systemctl is-active zabbix-agent2`
            shell_log "Zabbix Agent2 status: ${status}"
            shell_log "detail can see in ${SHELL_LOG}"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "ubuntu_22.04" ];then
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
        zabbix_agent_old_exist_stop
	curl --insecure -o /usr/local/zabbix-release_5.0-2+ubuntu22.04_all.deb ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-release_5.0-2+ubuntu22.04_all.deb
        dpkg -i /usr/local/zabbix-release_5.0-2+ubuntu22.04_all.deb && apt install -y zabbix-agent

        sed -i "s/Server=.*/Server=$ZABBIX_SERVER,172.17.0.0\/16/g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s/ServerActive=.*/ServerActive=$ZABBIX_SERVER/g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agentd.conf

        systemctl enable zabbix-agent
        systemctl restart zabbix-agent
        sleep 3 
        systemctl is-active --quiet zabbix-agent
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent Install Succeed"
            status=`systemctl is-active zabbix-agent`
            shell_log "Zabbix Agent status: ${status}"
            shell_log "detail can see in ${SHELL_LOG}"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "debian_12" ];then
        zabbix_agent_old_exist_stop
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
	curl --insecure -o /usr/local/zabbix-release_5.0-2+debian11_all.deb ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-release_5.0-2+debian11_all.deb
        dpkg -i /usr/local/zabbix-release_5.0-2+debian11_all.deb && apt install -y zabbix-agent2
        sed -i "s#Server=127.0.0.1#Server=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s#ServerActive=127.0.0.1#ServerActive=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agentd.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agentd.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agentd.conf
        systemctl enable zabbix-agent2
        systemctl restart zabbix-agent2
        sleep 1
        systemctl is-active --quiet zabbix-agent2
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "debian_11" ];then
        zabbix_agent_old_exist_stop
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
	curl --insecure -o /usr/local/zabbix-release_5.0-2+debian11_all.deb ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-release_5.0-2+debian11_all.deb
        dpkg -i /usr/local/zabbix-release_5.0-2+debian11_all.deb && apt install -y zabbix-agent2
        sed -i "s#Server=127.0.0.1#Server=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agent2.conf 
        sed -i "s#ServerActive=127.0.0.1#ServerActive=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agent2.conf
        systemctl enable zabbix-agent2
        systemctl restart zabbix-agent2
        sleep 1
        systemctl is-active --quiet zabbix-agent2
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "CENTOS_6" ];then
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
	rpm -ivh ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent2-5.0.42-1.el6.x86_64.rpm
        sed -i "s/Server=.*/Server=$ZABBIX_SERVER,172.17.0.0\/16/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/ServerActive=.*/ServerActive=$ZABBIX_SERVER/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agent2.conf
	service zabbix-agent2 restart
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi    
    elif [ "$OS" == "centos_7" -o "$OS" == "euleros_2.0" ];then
        zabbix_agent_old_exist_stop
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
        rpm -ivh ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent2-5.0.42-1.el7.x86_64.rpm
        shell_log "rpm install finished"
        sed -i "s/Server=.*/Server=$ZABBIX_SERVER,172.17.0.0\/16/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/ServerActive=.*/ServerActive=$ZABBIX_SERVER/g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agent2.conf
        sed -i 's/^#[[:space:]]\+Timeout=.*/Timeout=30/' /etc/zabbix/zabbix_agent2.conf 
        sed -i 's/^#[[:space:]]\+RefreshActiveChecks=.*/RefreshActiveChecks=60/' /etc/zabbix/zabbix_agent2.conf 
        systemctl enable zabbix-agent2
        systemctl restart zabbix-agent2
	firewall-cmd --add-port=10050/tcp --permanent
	firewall-cmd --reload
        sleep 1 
        systemctl is-active --quiet zabbix-agent2
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            status=`systemctl is-active zabbix-agent2`
            shell_log "Zabbix Agent2 status: ${status}"
            shell_log "install detail can see in ${SHELL_LOG}"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "centos_8" -o "$OS" == "rocky_8.4" -o "$OS" == "rocky_8.5" -o "$OS" == "almalinux_8.3" -o "$OS" == "almalinux_8.4" ];then
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
        rpm -ivh ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent2-5.0.42-1.el8.x86_64.rpm
        shell_log "Install Succeed"
        sed -i "s#Server=127.0.0.1#Server=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s#ServerActive=127.0.0.1#ServerActive=$ZABBIX_SERVER#g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agent2.conf
        sed -n "/^[^#]/p" /etc/zabbix/zabbix_agent2.conf
        sed -n "/^[^#]/p" /etc/zabbix/zabbix_agent2.conf >> $SHELL_LOG
        systemctl restart zabbix-agent2
        sleep 2
        systemctl is-active --quiet zabbix-agent2
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    elif [ "$OS" == "centos_9" -o "$OS" == "rocky_9.0" -o "$OS" == "almalinux_9.0" ];then
        shell_log "Begin Install Agent OS: $OS_TYPE $OS_VER"
        rpm -ivh ${PAAS_URL}/uploads/agent/zabbix-agent/zabbix-agent2-5.0.42-1.el9.x86_64.rpm
        shell_log "Install Succeed"
        sed -i "s#Server=127.0.0.1#Server=$ZABBIX_SERVER,172.17.0.0/16#g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s#ServerActive=127.0.0.1#ServerActive=$ZABBIX_SERVER#g" /etc/zabbix/zabbix_agent2.conf
        sed -i "s/#\?[[:space:]]\?Hostname=.*$/Hostname=$AGENT_HOSTNAME/g" /etc/zabbix/zabbix_agent2.conf
        systemctl restart zabbix-agent2
        if [ $? -eq 0 ];then
            shell_log "Zabbix Agent2 Install Succeed"
            return 0
        else
            shell_log $INSTALL_FAILED_MSG
        fi
    else
	 shell_log "This OS -${OS}- is not support install"
    fi
}

zabbix_agent_remove(){
    if [ "$OS_TYPE" == "CENTOS" -a "$OS_VER" -eq 6 ];then
        service zabbix-agent2 stop
        yum remove -y zabbix-agent2
    elif [ "$OS_TYPE" == "CENTOS" -a "$OS_VER" -eq 7 ];then
        service zabbix-agent2 stop
        yum remove -y zabbix-agent2
    elif [ "$OS_TYPE" == "CENTOS" -a "$OS_VER" -eq 8 ];then
        service zabbix-agent2 stop
        yum remove -y zabbix-agent2
    elif [ "$OS" == "Ubuntu_14.04" ];then
        /etc/init.d/zabbix-agent stop
        apt-get remove zabbix-agent -y
    elif [ "$OS" == "Ubuntu_16.04" ];then
        systemctl stop zabbix-agent
        apt-get remove zabbix-agent -y 
    elif [ "$OS" == "Ubuntu_18.04" -o "$OS" == "Ubuntu_20.04" ];then
        systemctl stop zabbix-agent2
        apt-get remove zabbix-agent2 -y
    fi
}

#Usage
usage(){
    echo "Usage: $0 [install|restart|start|stop|remove] zabbix_server_ip minionid paas_url"
}

main(){
    ZABBIX_SERVER=$2
    AGENT_HOSTNAME=$3
    PAAS_URL=$4
    shell_log "agent hostname: ${AGENT_HOSTNAME}"
    shell_log "zabbix_server : ${ZABBIX_SERVER}"
    case $1 in
        install)
            zabbix_agent_install;
           # install_clean;
            ;;
        update)
            zabbix_agent_update;
            ;;
        remove)
            zabbix_agent_remove;
            ;;
        *)
            usage;
    esac
}

main $1 $2 $3 $4
