脚本#!/bin/bash
db_password=China123
package_zabbix_url=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
dir=$(pwd)
echo "############################ 安装zabbix仓库... ############################"
wget $package_zabbix_url
dpkg -i zabbix-release_6.0-4+ubuntu22.04_all.deb
echo "############################ 修改zabbix仓库为清华软件源... ############################"
cat > /etc/apt/sources.list.d/zabbix.list <<-EOF
# Zabbix main repository
deb https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu jammy main
deb-src https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu jammy main
EOF
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
echo "############################ 更新软件源 ############################"
apt-get clean all
apt-get update
echo "############################ 安装zabbix软件包 ############################"
apt install -y zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent
echo "############################ 安装, 初始化mysql-server,创建zabbix初始数据库 ############################"
apt -y install mysql-server
echo 初始化数据库
db_username=$(cat /etc/mysql/debian.cnf |grep -m 1 user|awk '{print $3}')
db_password1=$(cat /etc/mysql/debian.cnf |grep -m 1 password|awk '{print $3}')
cat > $dir/db_init.txt <<-EOF
create database zabbix character set utf8mb4 collate utf8mb4_bin;
create user zabbix@localhost identified by '$db_password';
grant all privileges on zabbix.* to zabbix@localhost;
set global log_bin_trust_function_creators = 1;
flush privileges;
EOF
mysql -u$db_username -p$db_password1 < $dir/db_init.txt --connect-expired-password
echo "############################ 安装Zabbix server,Web前端,agent ############################"
apt -y install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent
echo "############################ 恢复软件源 ############################"
cp /etc/apt/sources.list.bak1 /etc/apt/sources.list
apt-get clean all
apt-get update
echo "############################ 导入初始架构和数据 ############################"
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p$db_password zabbix
cat > $dir/db_init2.txt <<-EOF
set global log_bin_trust_function_creators = 0;
EOF
mysql -u$db_username -p$db_password1 < $dir/db_init2.txt --connect-expired-password
echo "############################ 为Zabbix server配置数据库 ############################"
sed -i "s/# DBPassword=/DBPassword=$db_password/" /etc/zabbix/zabbix_server.conf
echo "############################ 为Zabbix前端配置PHP ############################"
sed -i "s/^# listen 8080/ listen 8080/" /etc/zabbix/nginx.conf
sed -i "s/^# server_name example.com/ server_name example.com/" /etc/zabbix/nginx.conf
echo "############################ 添加中文环境,解决zabbix页面显示中文问题 ############################"
locale-gen zh_CN.UTF-8
echo "############################ 启动Zabbix server和agent进程 ############################"
systemctl enable zabbix-server zabbix-agent nginx php8.1-fpm
systemctl restart zabbix-server zabbix-agent nginx php8.1-fpm
echo "############################ 访问入口 ############################"
server_interface=$(ip a|grep BROADCAST|awk '{print $2}'|awk -F ':' '{print $1}')
server_ip=`ip -brief address show $server_interface | perl -F'\s+|\/' -nlae 'print $F[2]'`
echo url:http:/$server_ip:8080/ 默认用户名:Admin 默认密码:zabbix