Ubuntu 、CentOS zabbix agent 2安装shell脚本

本文介绍了一个针对CentOS7、8和Ubuntu20、22系统的bash脚本,用于安装和配置ZabbixAgent,包括修改配置文件、设置Zabbix服务器IP和开放防火墙端口。脚本要求用户以root权限运行,并针对不同Linux版本执行相应的包管理操作。
摘要由CSDN通过智能技术生成

只支持centOS7、8和在Ubuntu20、22;

Ubuntu 系统运行脚本的时候,注意只能用sudo bash xxx.sh 而不能用sudo sh xxx.sh,因为Ubuntu的sh 是dash而不是bash。

根据自身环境修改zabbix服务器IP:zabbix_server='192.168.1.245'

Linux 初学者,脚本写的不好请见谅。

#!/bin/bash
#只支持centOS7、8和在Ubuntu20、22
if [ "$EUID" -ne 0 ]; then
  echo "请以root用户运行 Please run as root"
  exit 1
fi

if [ -f /etc/centos-release ]; then
  os_version=$(cat /etc/centos-release | awk '{print $4}' | awk -F . '{print $1}')
elif [ -f /etc/lsb-release ]; then
  os_version=$(lsb_release -r | awk '{print $2}' | awk -F . '{print $1}')
else
  echo "不支持的操作系统 Unsupported operating system"
  exit 1
fi

# Install Zabbix agent
if [ "$os_version" == "7" ]; then
  yum install -y https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm
  sed -i 's@^baseurl=https://repo.zabbix.com@baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix@g' /etc/yum.repos.d/zabbix.repo
  sed -i 's@^baseurl=https://repo.zabbix.com@baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix@g' /etc/yum.repos.d/zabbix-agent2-plugins.repo
  yum install -y zabbix-agent2
elif [ "$os_version" == "8" ]; then
  mkdir /etc/yum.repos.d/yum.repos.d.bak
  mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/yum.repos.d.bak/
  curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  yum makecache
  rpm -Uvh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
  sed -i 's@^baseurl=https://repo.zabbix.com@baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix@g' /etc/yum.repos.d/zabbix.repo
  sed -i 's@^baseurl=https://repo.zabbix.com@baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix@g' /etc/yum.repos.d/zabbix-agent2-plugins.repo
  yum install -y zabbix-agent2
elif [ "$os_version" == "20" ]; then
  wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4%2Bubuntu20.04_all.deb
  dpkg -i zabbix-release_6.0-4+ubuntu20.04_all.deb
  cat > /etc/apt/sources.list.d/zabbix-agent2-plugins.list <<-EOF
# Zabbix main repository
deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix-agent2-plugins/1/ubuntu focal main
deb-src [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix-agent2-plugins/1/ubuntu focal main
EOF
  apt update
  apt install -y zabbix-agent2
elif [ "$os_version" == "22" ]; then
  wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4%2Bubuntu22.04_all.deb
  dpkg -i zabbix-release_6.0-4+ubuntu22.04_all.deb
  cat > /etc/apt/sources.list.d/zabbix-agent2-plugins.list <<-EOF
# Zabbix main repository
deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix-agent2-plugins/1/ubuntu jammy main
deb-src [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix-agent2-plugins/1/ubuntu jammy main
EOF
  apt update
  apt install -y zabbix-agent2
else
  echo "不支持的操作系统 Unsupported operating system"
  exit 1
fi


echo "修改zabbix-agent配置文件"
zabbix_server='192.168.1.245'
hostname_var=$(hostname)

sed -i "s/^Server=127.0.0.1/Server=${zabbix_server}/g" /etc/zabbix/zabbix_agent2.conf
sed -i "s/^ServerActive=127.0.0.1/ServerActive=${zabbix_server}/g" /etc/zabbix/zabbix_agent2.conf
sed -i "s/Hostname=Zabbix server/Hostname=${hostname_var}/g" /etc/zabbix/zabbix_agent2.conf
sed -i "s/# HostMetadata=/HostMetadata=Linux/g" /etc/zabbix/zabbix_agent2.conf



echo "防火墙放行zabbix-agent端口"
if [ "$os_version" == "7" ]; then
  firewall-cmd --permanent --add-port=10050/tcp
  firewall-cmd --reload
elif [ "$os_version" == "8" ]; then
  firewall-cmd --permanent --add-port=10050/tcp
  firewall-cmd --reload
elif [ "$os_version" == "20" ]; then
  ufw allow 10050/tcp
  ufw reload
elif [ "$os_version" == "22" ]; then
  ufw allow 10050/tcp
  ufw reload
else
  echo "不支持的操作系统 Unsupported operating system"
  exit 1
fi
echo "启动zabbix-agent2服务"
systemctl restart zabbix-agent2 && systemctl enable zabbix-agent2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值