#/bin/bash
#Setup zabbix agent.
#configure zabbix_agent.conf.
#Check zabbix agent process,and install zabbix agent.
zabbix_num=`ps -ef |grep 'zabbix_agentd: listener' |grep -v grep |wc -l`
if [ $zabbix_num -gt 1 ];then
echo 'zabbix agent setup successed'
exit
else
yum -y -q install zabbix-agent
fi
#Get host ip,used for zabbix agent hostname.
ip=`ip a |grep -E '255 scope global eth0|255 scope global noprefixroute eth0' |awk -F ' ' '{print $2}'|awk -F '/' '{print $1}' |awk 'NR==1{print}'`
zabbix_agent_name="DevTest_"$ip
#echo $zabbix_agent_name
zabbix_agent_name_g=`echo $zabbix_agent_name |grep '\.' |wc -l`
if [ $zabbix_agent_name_g -ne 1 ];then
echo 'zabbix agent setup failed,cannot set zabbix agent host name.'
exit
fi
#Configure zabbix_agent.conf.
sed -i '115{s/127.0.0.1/zabbixproxy.xx.com/}' /etc/zabbix_agentd.conf
sed -i '169{s/127.0.0.1/zabbixproxyi.xx.com/}' /etc/zabbix_agentd.conf
sed -i "180{s/Zabbix server/$zabbix_agent_name/}" /etc/zabbix_agentd.conf
#Check Zabbix_agent.conf.
zbx_server_g=`grep 'zabbixproxy' /etc/zabbix_agentd.conf |wc -l`
zbx_server_g2=`grep "$zabbix_agent_name" /etc/zabbix_agentd.conf |wc -l`
if [ $zbx_server_g -ne 2 -o $zbx_server_g2 -ne 1 ];then
echo 'zabbix agent setup failed,configure zabbix_agent.conf failed.'
exit
fi
sleep 1
systemctl restart zabbix-agent
sleep 1
#Check zabbix agent service status.
zabbix_status=`systemctl is-active zabbix-agent`
if [ $zabbix_status == "active" ];then
echo "zabbix agent setup successed"
else
echo "zabbix agent setup failed"
fi