#!/bin/bash



Host=`date  | cut -b 12-19`
ServerIP="103.233.11.19"

check_zabbix(){
	check=`rpm -qa |grep zabbix-agent | cut -b 1-12`
	if [ $check == 'zabbix-agent'  ];then
		echo "OK"
	else	rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm 
		yum install zabbix-agent -y 
		modify_file
		start_zabbix
		Set_port
		Obtain_ip
		
	fi 
}

modify_file(){
	if [ -f  /etc/zabbix/zabbix_agentd.conf ];then
		sed -i 's/Server=127.0.0.1/Server=103.233.11.19/g' /etc/zabbix/zabbix_agentd.conf
		sed -i 's/ServerActive=127.0.0.1/ServerActive=103.233.11.19/g' /etc/zabbix/zabbix_agentd.conf
		sed -i 's/Hostname=Zabbix server/Hostname=client/g' /etc/zabbix/zabbix_agentd.conf
		check_file1=`cat /etc/zabbix/zabbix_agentd.conf |grep Server=103.233.11.19 |wc -l`
		check_file2=`cat /etc/zabbix/zabbix_agentd.conf |grep  ServerActive=103.233.11.19 |wc -l`
		check_file=`cat /etc/zabbix/zabbix_agentd.conf |grep Hostname=client  |wc -l`
		if [ check_file != 0 ];then
			echo "ServerIP OK" 
		else
			echo "Server NO"
			exit
		fi
		
		if [ check_file1 != 0 ];then
			echo "ServerActive OK" 
		else
			echo "Server NO"
			exit
		fi
		
		if [ check_file2 != 0 ];then
	 		echo  "HostName OK"
		fi
	else
		echo "good bey "
		exit
	fi 
	
}

start_zabbix(){
	if [ -f /etc/init.d/zabbix-agent ];then
	     /etc/init.d/zabbix-agent start
	else
	   echo "false"
	   exit 
	fi 
}


Set_port()
{
    # 乌班图的开放端口
	if [ -f "/usr/sbin/ufw" ];then

		ufw allow $1/tcp
		ufw allow $1/udp
		ufw reload
	fi
	# Centos7 的开放防火墙的方式
	if [ -f "/etc/sysconfig/firewalld" ];then
		firewall-cmd --permanent --zone=public --add-port=10050/tcp
		firewall-cmd --permanent --zone=public --add-port=10050/udp
		firewall-cmd --reload
	fi
	#Centos6 的防火墙开放方式
	if [ -f "/etc/init.d/iptables" ];then
		iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
		iptables -I INPUT -p udp -m state --state NEW -m udp --dport 10050 -j ACCEPT
		/etc/init.d/iptables save
	fi
}


Obtain_ip(){
	yum install curl 
	ip=	`curl members.3322.org/dyndns/getip`
	echo $ip

}
mkdir /var/run/zabbix/ && chmod 777 /var/run/zabbix/
echo "/etc/init.d/zabbix-agent restart" >>/etc/rc.local 

check_zabbix
mkdir -p /var/run/zabbix/
chown zabbix.zabbix /var/run/zabbix/
systemctl restart zabbix-agent.service


