#!/bin/bash
# Filename:    $0
# Revision:    1.0
# Date:        2022/06/28
# Author:      persimmon_lz
# Description: 一键安装zabbix-agent

time=$(date -d "today" +%Y%m%d)
ZABBIX_SERVER=8.134.10.16
ZABBIX_PORT=10050
vernum=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
sysverion=`cat /etc/redhat-release|awk -F " " '{print $1,$2}'`
agentver=`rpm -qa |grep zabbix-agent |awk -F "-" '{print $3}'|awk -F "." '{print $3}'`
nowagver=`rpm -qa |grep zabbix-agent |awk -F "-" '{print $3}'`

if [ ${vernum} -ge 5 -a ${vernum} -le 8 ];then 
	echo ""
	echo -e "\033[33m*************************************************当前系统版本为 ${sysverion} ${vernum}**************************************************\033[0m"
else
	echo ""
	echo -e "\033[33m*************************************************该脚本不支持当前版本**************************************************\033[0m"
    exit $?
fi

if [ ${agentver} = 23 ];then
		echo ""
        echo -e "\033[33m*******************************************当前zabbix-agent版本为 zabbix-agen ${nowagver} 无需重新安装********************************************\033[0m"
        exit $?
else
		echo ""
        echo -e "\033[33m*******************************************当前zabbix-agent版本为 zabbix-agen ${nowagver} 需要重新安装********************************************\033[0m"
fi


  echo ""
  echo -e "\033[33m****************************************************安装Zabbix agent 5.0.23*****************************************************\033[0m" 

#卸载zabbix-agent
rpm -e zabbix-agent

  #下载包
  if [ -f /tmp/zabbix-agent-5.0.23-1.el${vernum}.x86_64.rpm ] ;then
	  echo "*****存在zabbix agent安装包，无需下载*****"
  else
      curl http://test.leso114.com:8082 >/dev/null 2>&1
      if [ $? -eq 0 ];then
      	wget http://test.leso114.com:8082/tools/zabbix-agent/${vernum}/zabbix-agent-5.0.23-1.el${vernum}.x86_64.rpm -O /tmp/zabbix-agent-5.0.23-1.el${vernum}.x86_64.rpm
      else
        echo "please download zabbix-agent-5.0.23 package manual !"
		exit $?
      fi 
  fi

  #安装Zabbix agent
echo  "********安装Zabbix agent 5.0.23********" 
chmod u+x /tmp/zabbix-agent-5.0.23-1.el${vernum}.x86_64.rpm
#  rpm -ivh /tmp/zabbix-agent-5.0.23-1.el${vernum}.x86_64.rpm
cd /tmp/
yum install zabbix-agent-5.0.23-1.el${vernum}.x86_64.rpm -y

  
  #修改zabbix agent配置文件
cp -a /etc/zabbix/zabbix_agentd.conf.rpmsave /etc/zabbix/zabbix_agentd.conf

#授权
#chown -R zabbix:zabbix /etc/zabbix/ && chmod -R 755   /etc/zabbix/
echo  "********启动Zabbix agent********" 


if [ ${vernum} -ge 7 -a ${vernum} -le 8 ] ;then
		systemctl enable zabbix-agent
		systemctl restart zabbix-agent
		systemctl status zabbix-agent
  else
        if [ ${vernum} -le 6 -a ${vernum} -ge 5 ] ;then
			service zabbix-agent restart
			service zabbix-agent status
			chkconfig zabbix-agent on
			chkconfig --list zabbix-agent
        else
                echo "该脚本不支持当前版本"
                exit $?
        fi
fi
newagver=`rpm -qa |grep zabbix-agent |awk -F "-" '{print $3}'`
AGENT_HOSTNAME=`cat /etc/zabbix/zabbix_agentd.conf|grep Hostname|awk -F "=" '{print $2}'`

  echo -e "\033[33m**************************************************完成zabbix-agent安装***************************************************\033[0m"
cat > /tmp/zabbix-agent.log << EOF
zabbix-agent版本：   ${newagver}
zabbix-server：      ${ZABBIX_SERVER}
zabbix-agent端口：   ${ZABBIX_PORT}
zabbix-agent IP：    ${IPADDR}
zabbix-agent主机名： ${AGENT_HOSTNAME}
EOF
  cat /tmp/zabbix-agent.log
  echo -e "\e[1;31m 以上信息10秒后消失，保存在/tmp/zabbix-agent.log文件下 \e[0m"
  echo -e "\033[33m************************************************************************************************************************\033[0m"
  echo ""


