#!/bin/bash
#通过命令行参数指定
if [ ! -z "$1" ];then
ip=$1
echo "手动指定IP:$ip"
else
#根据默认路由获取ip地址
iface=`route 2>/dev/null |grep default |awk '{print $8}'`
iface=${iface:-'bond0'}
ip=`ifconfig $iface 2>/dev/null |grep -oP '(?<=inet addr:)\d*\.\d*\.\d*\.\d*'`
echo "自动获取网卡${iface}的IP为:$ip"
fi
#如果两种方式都没获取到IP,则退出
if [ -z "$ip" ];then
echo 'IP地址为空,程序退出'
exit
fi
yum install -y zabbix-agent-3.0.1-1.el7.x86_64.rpm
if [ -e /etc/zabbix/zabbix_agentd.conf ];then
echo
echo "##########begin modify zabbix_agentd.conf##########"
sed -i 's/^Server=127.0.0.1/Server=10*.4*.12*.34*/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/^ServerActive=127.0.0.1/ServerActive=10*.4*.12*.34*:21222/g' /etc/zabbix/zabbix_agentd.conf
sed -i "s/Hostname=Zabbix server/Hostname=${ip}/g" /etc/zabbix/zabbix_agentd.conf
sed -i '/ListenPort=10050$/a\ListenPort=21224' /etc/zabbix/zabbix_agentd.conf
sed -i "/ListenIP=0.0.0.0$/a\ListenIP=${ip}" /etc/zabbix/zabbix_agentd.conf
sed -i '/Timeout=3$/a\Timeout=30' /etc/zabbix/zabbix_agentd.conf
grep -v '^#' /etc/zabbix/zabbix_agentd.conf |grep -v '^$'
echo "##########end modify zabbix_agentd.conf##########"
echo
else
echo
echo "##########zabbix_agentd.conf is not exit##########"
echo
exit 2
fi
if [ -d /etc/zabbix/zabbix_agentd.d ];then
cp -r weifeng.conf /etc/zabbix/zabbix_agentd.d
else
echo "##########/etc/zabbix/zabbix_agentd.d is not exist##########"
echo
exit 3
fi
echo "##########begin move scripts to /etc/zabbix/scripts##########"
if [ -d /etc/zabbix/scripts ];then
cp -r scripts/* /etc/zabbix/scripts
chmod +x /etc/zabbix/scripts/*
else
mkdir /etc/zabbix/scripts
cp -r scripts/* /etc/zabbix/scripts
chmod +x /etc/zabbix/scripts/*
fi
echo "##########end move scripts to /etc/zabbix/scripts##########"
echo
echo "##########begin modify /etc/sudoers##########"
sed -i 's/\(Defaults.*requiretty\)/#\1/g' /etc/sudoers
echo '...'
sed -i 's/!visiblepw/visiblepw/g' /etc/sudoers
echo '...'
echo 'zabbix ALL=(ALL) NOPASSWD:/etc/zabbix/scripts/*' >> /etc/sudoers
echo '...'
echo "##########end modify /etc/sudoers##########"
echo
systemctl enable zabbix-agent.service
systemctl restart zabbix-agent.service