Zabbix Proxy Shell 安装脚本
脚本说明
- 机器必须要能上网,这样仓库才能下载Zabbix proxy等组件
- 我的Linux版本是CentOS8 Stream
- 数据库我用的是mysql8.0版本
- Zabbix Server的版本是6.0LTS
- 需要提前下载并上传到服务器上的mysql8.0 tar包到/opt目录下,文件命令为mysql8.0.3.tar即可,脚本会自动解压并安装MySQL
- 欢迎各位大佬批评指正!!!
shell">#!/usr/bin/env bash#-------------------------------------------------------
# This script is used to install the Proxy
# @ By Eleven
# @ Date 2024-06-16
#-------------------------------------------------------# Zabbix Databse Name
zabbix_db_name=zabbix_proxy# Zabbix user password
zabbix_password=zabbix_py3# Zabbix username
zabbix_username=zabbix_py3# MySQL Databse Password
mysql_password=mysqlwx_123# Function about install http server
InstallHttpServer(){yum -y install httpd &>> /dev/nullsystemctl start httpd.service &>> /dev/nullhttp_status=$(systemctl is-active httpd.service)if [ $http_status = "active" ];thensystemctl enable httpd.service &>> /dev/nullelseecho -e "\033[31mError: Http Server is starting fail!\033[0m"exitfiecho -e "\033[32mMsg: Http Server installed success!\033[0m"
}# Function about install database of MySQL
InstallMySQL(){# temporarily closed Selinux 0 setenforce 0 # tar mysql file and rpm installinstall_status=0if test -f "/opt/mysql8.0.3.tar";thentar -xvf /opt/mysql8.0.3.tar &>> /dev/nullcd /opt/mysql8.0.3/# 1.Install mysql-community-common-8.3.0-1.el8.x86_64one_mark=$(rpm -q "mysql-community-common-8.3.0-1.el8.x86_64" | awk '{print($NF)}')if [ $one_mark != 'installed' ];thenecho -e "\033[32mMsg: [mysql-community-common] package already install\033[0m"elserpm -ivh mysql-community-common-8.3.0-1.el8.x86_64.rpm &>> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[32mMsg: [mysql-community-common] package installed success!\033[0m"elseinstall_status=1echo -e "\033[31mError: [mysql-community-common] package installed fail!\033[0m" fifi# 2.Install mysql-community-client-plugins-8.3.0-1.el8.x86_64tow_mark=$(rpm -q "mysql-community-client-plugins-8.3.0-1.el8.x86_64" | awk '{print($NF)}')if [ $tow_mark != 'installed' ];thenecho -e "\033[32mMsg: [mysql-community-client-plugins] package already install\033[0m"elserpm -ivh mysql-community-client-plugins-8.3.0-1.el8.x86_64.rpm &>> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[32mMsg: [mysql-community-client-plugins] package installed success!\033[0m"elseinstall_status=1echo -e "\033[31mError: [mysql-community-client-plugins] package installed fail!\033[0m" fifi # 3.Install mysql-community-libs-8.3.0-1.el8.x86_64 three_mark=$(rpm -q "mysql-community-libs-8.3.0-1.el8.x86_64" | awk '{print($NF)}')if [ $three_mark != 'installed' ];thenecho -e "\033[32mMsg: [mysql-community-libs] package already install\033[0m"elserpm -ivh mysql-community-libs-8.3.0-1.el8.x86_64.rpm &>> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[32mMsg: [mysql-community-libs] package installed success!\033[0m"elseinstall_status=1echo -e "\033[31mError: [mysql-community-libs] package installed fail!\033[0m" fifi# 4.Install mysql-community-client-8.3.0-1.el8.x86_64four_mark=$(rpm -q "mysql-community-client-8.3.0-1.el8.x86_64" | awk '{print($NF)}')if [ $four_mark != 'installed' ];thenecho -e "\033[32mMsg: [mysql-community-client] package already install\033[0m"elserpm -ivh mysql-community-client-8.3.0-1.el8.x86_64.rpm &>> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[32mMsg: [mysql-community-client] package installed success!\033[0m"elseinstall_status=1echo -e "\033[31mError: [mysql-community-client] package installed fail!\033[0m" fifi# 5.Install mysql-community-icu-data-files-8.3.0-1.el8.x86_64five_mark=$(rpm -q "mysql-community-icu-data-files-8.3.0-1.el8.x86_64" | awk '{print($NF)}')if [ $five_mark != 'installed' ];thenecho -e "\033[32mMsg: [mysql-community-icu-data-files] package already install\033[0m"elserpm -ivh mysql-community-icu-data-files-8.3.0-1.el8.x86_64.rpm &>> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[32mMsg: [mysql-community-icu-data-files] package installed success!\033[0m"elseinstall_status=1echo -e "\033[31mError: [mysql-community-icu-data-files] package installed fail!\033[0m" fifi# 6.Install mysql-community-server-8.3.0-1.el8.x86_64six_mark=$(rpm -q "mysql-community-server-8.3.0-1.el8.x86_64" | awk '{print($NF)}')if [ $six_mark != 'installed' ];thenecho -e "\033[32mMsg: [mysql-community-server] package already install\033[0m"elserpm -ivh mysql-community-server-8.3.0-1.el8.x86_64.rpm &>> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[32mMsg: [mysql-community-server] package installed success!\033[0m"elseinstall_status=1echo -e "\033[31mError: [mysql-community-server] package installed fail!\033[0m" fifielseecho -e "\033[31mError: MySQL installation package not found!\033[0m"echo -e "\033[31mError: Please Put the MySQL installation package in the 【/opt】 directory\033[0m"exitfiif [ $install_status -eq 1 ];thenecho -e "\033[31mMsg: MySQL Database installed fail!\033[0m"exitelseecho -e "\033[32mMsg: MySQL Database installed success!\033[0m" fi}# Function about init Database of MySQL
InitMySQL(){pwd_status=0mysqld --initialize &>> /dev/nullsleep 10 chown -R mysql:mysql /var/lib/mysql &>> /dev/null chmod -R 755 /var/lib/mysql &>> /dev/null systemctl start mysqld.service &>> /dev/null mysql_status=$(systemctl is-active mysqld.service)if [ $mysql_status = "active" ];thensystemctl enable mysqld.service &>> /dev/nullecho -e "\033[32mMsg: MySQL Server is starting success!\033[0m"elseecho -e "\033[31mError: MySQL Server is starting fail!\033[0m"exitfitemporarily_password=$(cat /var/log/mysqld.log | grep root@localhost | awk '{print($NF)}')mysql -uroot -p"$temporarily_password" -e "quit"mysql --connect-expired-password -uroot -p"$temporarily_password" <<EOF
ALTER USER 'root'@'localhost' identified by '$mysql_password';
quit;
EOFif [ $? -eq 0 ];thenecho -e "\033[32mMsg: MySQL password changed success!\033[0m"elseecho -e "\033[31mMsg: MySQL password changed fail!\033[0m"pwd_status=1exitfiif [ $pwd_status -eq 0 ];thenecho -e "\033[32mMsg: Init MySQL Server is success!\033[0m"elseecho -e "\033[31mMsg: Init MySQL Server is fail!\033[0m"exitfi}# Function about install Proxy
InstallProxy() {yum -y install zabbix-proxy-mysql zabbix-sql-scripts zabbix-selinux-policy &>> /dev/nullif [ $? -eq 0 ];thenecho -e "\033[32mMsg: Zabbix Proxy install success!\033[0m"elseecho -e "\033[31mMsg: Zabbix Proxy install fail!\033[0m"fimysql -uroot -p$mysql_password <<EOF
create database $zabbix_db_name character set utf8mb4 collate utf8mb4_bin;
create user '$zabbix_username'@'localhost' identified with mysql_native_password by '$zabbix_password';
grant all privileges on $zabbix_db_name.* to $zabbix_username@localhost;
set global log_bin_trust_function_creators = 1;
quit;
EOFcat /usr/share/zabbix-sql-scripts/mysql/proxy.sql | mysql --default-character-set=utf8mb4 -u$zabbix_username -p$zabbix_password $zabbix_db_nameif [ $? -eq 0 ];thenecho -e "\033[32mMsg: zabbix mysql upload success\033[0m"elseecho -e "\033[31mMsg: zabbix mysql upload fail\033[0m" fimysql -uroot -p$mysql_password <<EOF
set global log_bin_trust_function_creators = 0;
quit;
EOF# set proxy servicesystemctl restart zabbix-proxy &>> /dev/nullproxy_status=$(systemctl is-active zabbix-proxy)if [ $proxy_status = "active" ];thensystemctl enable zabbix-proxy &>> /dev/nullecho -e "\033[32mMsg: Zabbix Proxy status is starting success!\033[0m"elseecho -e "\033[31mError: Zabbix Proxy status is starting fail!\033[0m"Proxy_start_status=1exitfi}# Function about open firewalld port and service
OpenPort(){# open 10051 161 httpfirewall-cmd --zone=public --add-port=10050/tcp --permanent &>> /dev/nullfirewall-cmd --zone=public --add-port=10051/tcp --permanent &>> /dev/nullfirewall-cmd --zone=public --add-port=161/tcp --permanent &>> /dev/nullfirewall-cmd --zone=public --add-service=http --permanen &>> /dev/nullfirewall-cmd --reload &>> /dev/nullecho -e "\033[32mMsg: Firewalld Port is opening!\033[0m"
}# Function about set Unified time zone
Time_synchronization(){#judgment chrony install? Complete=$(yum -y install chrony | grep Complete!)if [ $Complete = "Complete!" ];thenecho -e "\033[32mMsg: chrony is already install!\033[0m"elseecho ">>>>>> start install chrony!"yum -y install chronyif [ $? -eq 0 ];thenecho -e "\033[32mMsg: chrony install success\033[0m"systemctl enable chronyd --now elseecho -e "\033[31mMsg: chrony install fail\033[0m" fifiecho ">>>>>> start synchronization time"sed -i '/pool 2.centos.pool.ntp.org iburst/a\server 192.168.95.8 iburst' /etc/chrony.confsystemctl restart chronydecho -e "\033[32mMsg: synchronization time success\033[0m"
}echo ">>>>>> Check the local repository configuration"
yum -y install vim &>>/dev/null
if [ $? -ne 0 ];thenecho -e "\033[31mError: Please check the configuration of your local repository\033[0m"exit
elseecho -e "\033[32mMsg: Local repository check success!\033[0m"
fiecho ">>>>>> Install the necessary components"
tools_status=$( rpm -q net-tools | awk '{print($NF)}')
if [ $tools_status = "installed" ];thenyum -y install net-tools &>> /dev/null
fiperl_status=$( rpm -q perl | awk '{print($NF)}')
if [ $perl_status = "installed" ];thenyum -y install perl &>> /dev/null
fi
echo -e "\033[32mMsg: Check components success!\033[0m"echo ">>>>>> Starting install http service"
InstallHttpServerecho ">>>>>> Starting install MySQL server"
InstallMySQLecho ">>>>>> Starting init MySQL server"
InitMySQLecho ">>>>>> Starting install zabbix proxy server "
InstallProxyecho ">>>>>> Starting open firewalld port"
OpenPortecho ">>>>>> Set Time synchronization"
Time_synchronizationecho -e "\033[32mExecuted Success! Over\033[0m"