当前位置: 首页 > 工具软件 > zabbix-backup > 使用案例 >

Linux:shell脚本自动安装zabbix-agent

葛成双
2023-12-01

                                                     Linux:shell脚本自动安装zabbix-agent

 

#!/bin/bash
# 获取当前shell脚本所在的文件夹位置
basepath=$(cd `dirname $0`; pwd)

# 获取当前shell脚本所在的位置
SHELL_DIR="${basepath}/shell"
# 安装包所在的路径
PACKAGE_DIR="${basepath}/package"
echo "${PACKAGE_DIR}"
# zabbix repo
# https://repo.zabbix.com/zabbix/
# 获取本机IP地址 此处获取ens 接口的IP地址
ipaddr=$(ip a show |grep ens|grep inet |awk '{print $2}'|awk -F '/' '{print $1}')
zabbix_server='192.168.5.191'

########################安装zabbix-agent############################
yum -y install wget

echo -e "请给出要安装的zabbix版本号,建议使用4.x的版本  \033[31musage:./zabbix_aliyun.sh 4.0|4.4|4.5 \033[0m"
echo "例如要安装4.4版本,在命令行写上 ./zabbix_install.sh 4.4"
if [ -z $1 ];then
    echo "未检测到有输入zabbix-agent版本号,默认为4.0"
    VERSION=4.0
else
   VERSION=$1
fi
if [ -f /etc/yum.repos.d/zabbix.repo ];then
    rm -rf /etc/repos.d/zabbix.repo
fi
rpm -qa | grep zabbix-release && rpm -e zabbix-release
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/$VERSION/rhel/7/x86_64/zabbix-release-$VERSION-1.el7.noarch.rpm
sed -i "s@zabbix/.*/rhel@zabbix/$VERSION/rhel@g" /etc/yum.repos.d/zabbix.repo
sed -i 's@repo.zabbix.com@mirrors.aliyun.com/zabbix@g' /etc/yum.repos.d/zabbix.repo
echo "阿里云的zabbix源替换成功"
#yum clean all
#yum makecache
rm -rf /var/cache/yum
yum clean all
yum makecache fast
rpm -e zabbix-agent

yum -y install zabbix-agent

#####################修改zabbix_agentd.conf配置#####################
if [ -e /etc/zabbix/zabbix_agentd.conf ];then
echo
echo "##########begin modify zabbix_agentd.conf##########"
sed -i "s/^Server=127.0.0.1/Server=${zabbix_server}/g" /etc/zabbix/zabbix_agentd.conf
sed -i "s/^ServerActive=127.0.0.1/ServerActive=${zabbix_server}/g" /etc/zabbix/zabbix_agentd.conf
sed -i "s/Hostname=Zabbix server/Hostname=${ipaddr}/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 $PACKAGE_DIR/yuwang.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 $PACKAGE_DIR/scripts/* /etc/zabbix/scripts
    chmod +x /etc/zabbix/scripts/*
else
    mkdir /etc/zabbix/scripts
    cp -r $PACKAGE_DIR/scripts/* /etc/zabbix/scripts
    chmod +x /etc/zabbix/scripts/*
fi
echo "##########end move scripts to /etc/zabbix/scripts##########"

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##########"
!
###############防火墙放行zabbix-agentd端口并设置开机自启################
firewall-cmd --permanent --add-port=10050-10051/tcp
firewall-cmd --reload

echo "设置开机自启"
systemctl enable zabbix-agent
systemctl start zabbix-agent


zabbixagentpid=`ps aux|grep zabbix_agentd|grep -v "grep"|awk '{print $2}'`

if [ "$zabbixagentpid" ];then
      echo "success ! zabbix agent is running now"
fi

参考一:https://blog.csdn.net/plokday/article/details/80706421

参考二:https://blog.csdn.net/xjjj064/article/details/106000270

参考三:https://www.cnblogs.com/weifeng1463/p/8808849.html

 类似资料: