只支持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