#!/bin/bash
# Filename:    zbx-agtctyun.sh
# Revision:    2.1
# Date:        2022/06/28
# Author:      persimmon_lz
# Description: 一键安装zabbix-agent

time=$(date -d "today" +%Y%m%d)
ZABBIX_SERVER=192.169.175.30,134.175.119.172
ZABBIX_PORT=10050
AGENT_HOSTNAME=$1
vernum=zabbix-agent-4.0.32-1.el8.x86_64.rpm
sysverion=`cat /etc/redhat-release|awk -F " " '{print $1,$2}'`

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

if [ -z ${AGENT_HOSTNAME} ];
then
echo "-------------------------------------------"
echo "   请输入监控主机名："
echo "   例如：./$0 leso-web-01"
exit $?
fi


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

  #安装Zabbix agent
echo  "********安装Zabbix agent 4.0.32********" 
  chmod u+x /tmp/${vernum}
#  rpm -ivh /tmp/${vernum}
cd /tmp/
yum install ${vernum} -y

  
  #修改zabbix agent配置文件
cp -a /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak${time}
cat > /etc/zabbix/zabbix_agentd.conf << EOF
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=10
Server=${ZABBIX_SERVER}
ServerActive=${ZABBIX_SERVER}
ListenPort=${ZABBIX_PORT}
Hostname=${AGENT_HOSTNAME}
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
EOF

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

        systemctl enable zabbix-agent
        systemctl restart zabbix-agent
        systemctl status zabbix-agent
  
  
  echo -e "\033[33m**************************************************完成zabbix-agent安装***************************************************\033[0m"
cat > /tmp/zabbix-agent.log << EOF
zabbix-agent版本：   ${vernum}
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 ""
