zabbix-agent一键部署脚本(windos/linux)

#!/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

shell脚本过滤得到centos7的ip地址

ip a show dev ens33|grep -w inet|awk '{print $2}'|sed 's/\/.*//'

ip a show dev ens33|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'

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

yum 安装zabbix-agent报错:transfer closed with 333939 bytes remaining to read (更换源为阿里云源)

 今天测试shell脚本安装zabbix-agent并修改zabbix-agent.conf配置文件,在下载zabbix-agent时报错:transfer closed with 333939 bytes remaining to read !

    由于起初使用的是zabbix的官方源,官方源地址为:https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm

    替换为阿里云源:rpm -ivh https://mirrors.aliyun.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm

    各自根据安装的zabbix版本替换!

    以下为替换过程,参考:https://www.cnblogs.com/syscal/p/12461620.html

   下载repo文件

 

[root@localhost ~]# wget http://mirrors.aliyun.com/repo/Centos-7.repo

    备份并替换系统的repo文件

   [root@localhost ~]# cp Centos-7.repo /etc/yum.repos.d/
   [root@localhost ~]# cd /etc/yum.repos.d/
   [root@localhost ~]# mv CentOS-Base.repo CentOS-Base.repo.bak 
   [root@localhost ~]# mv Centos-7.repo CentOS-Base.repo

 执行yum源更新命令

[root@localhost ~]# yum clean all
 
[root@localhost ~]# yum makecache
 
[root@localhost ~]# yum update
更换zabbix国内源

vim /etc/yum.repos.d/zabbix.repo

如需安装4.0:将下面改为3.0改成4.0即可

[zabbix]
name=Zabbix Official Repository - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
  
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/\$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1

添加gpgkey

curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
 
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX

添加之后即可使用,更新源

yum makecache -y
 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值