Zabbix_Proxy自动化安装脚本
Zabbix Proxy Shell 安装脚本
脚本说明
- 机器必须要能上网,这样仓库才能下载Zabbix proxy等组件
- 我的Linux版本是CentOS8 Stream
- 数据库我用的是mysql8.0版本
- Zabbix Server的版本是6.0LTS
- 需要提前下载并上传到服务器上的mysql8.0 tar包到/opt目录下,文件命令为mysql8.0.3.tar即可,脚本会自动解压并安装MySQL
- 欢迎各位大佬批评指正!!!
#!/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/null
systemctl start httpd.service &>> /dev/null
http_status=$(systemctl is-active httpd.service)
if [ $http_status = "active" ];then
systemctl enable httpd.service &>> /dev/null
else
echo -e "\033[31mError: Http Server is starting fail!\033[0m"
exit
fi
echo -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 install
install_status=0
if test -f "/opt/mysql8.0.3.tar";then
tar -xvf /opt/mysql8.0.3.tar &>> /dev/null
cd /opt/mysql8.0.3/
# 1.Install mysql-community-common-8.3.0-1.el8.x86_64
one_mark=$(rpm -q "mysql-community-common-8.3.0-1.el8.x86_64" | awk '{print($NF)}')
if [ $one_mark != 'installed' ];then
echo -e "\033[32mMsg: [mysql-community-common] package already install\033[0m"
else
rpm -ivh mysql-community-common-8.3.0-1.el8.x86_64.rpm &>> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: [mysql-community-common] package installed success!\033[0m"
else
install_status=1
echo -e "\033[31mError: [mysql-community-common] package installed fail!\033[0m"
fi
fi
# 2.Install mysql-community-client-plugins-8.3.0-1.el8.x86_64
tow_mark=$(rpm -q "mysql-community-client-plugins-8.3.0-1.el8.x86_64" | awk '{print($NF)}')
if [ $tow_mark != 'installed' ];then
echo -e "\033[32mMsg: [mysql-community-client-plugins] package already install\033[0m"
else
rpm -ivh mysql-community-client-plugins-8.3.0-1.el8.x86_64.rpm &>> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: [mysql-community-client-plugins] package installed success!\033[0m"
else
install_status=1
echo -e "\033[31mError: [mysql-community-client-plugins] package installed fail!\033[0m"
fi
fi
# 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' ];then
echo -e "\033[32mMsg: [mysql-community-libs] package already install\033[0m"
else
rpm -ivh mysql-community-libs-8.3.0-1.el8.x86_64.rpm &>> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: [mysql-community-libs] package installed success!\033[0m"
else
install_status=1
echo -e "\033[31mError: [mysql-community-libs] package installed fail!\033[0m"
fi
fi
# 4.Install mysql-community-client-8.3.0-1.el8.x86_64
four_mark=$(rpm -q "mysql-community-client-8.3.0-1.el8.x86_64" | awk '{print($NF)}')
if [ $four_mark != 'installed' ];then
echo -e "\033[32mMsg: [mysql-community-client] package already install\033[0m"
else
rpm -ivh mysql-community-client-8.3.0-1.el8.x86_64.rpm &>> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: [mysql-community-client] package installed success!\033[0m"
else
install_status=1
echo -e "\033[31mError: [mysql-community-client] package installed fail!\033[0m"
fi
fi
# 5.Install mysql-community-icu-data-files-8.3.0-1.el8.x86_64
five_mark=$(rpm -q "mysql-community-icu-data-files-8.3.0-1.el8.x86_64" | awk '{print($NF)}')
if [ $five_mark != 'installed' ];then
echo -e "\033[32mMsg: [mysql-community-icu-data-files] package already install\033[0m"
else
rpm -ivh mysql-community-icu-data-files-8.3.0-1.el8.x86_64.rpm &>> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: [mysql-community-icu-data-files] package installed success!\033[0m"
else
install_status=1
echo -e "\033[31mError: [mysql-community-icu-data-files] package installed fail!\033[0m"
fi
fi
# 6.Install mysql-community-server-8.3.0-1.el8.x86_64
six_mark=$(rpm -q "mysql-community-server-8.3.0-1.el8.x86_64" | awk '{print($NF)}')
if [ $six_mark != 'installed' ];then
echo -e "\033[32mMsg: [mysql-community-server] package already install\033[0m"
else
rpm -ivh mysql-community-server-8.3.0-1.el8.x86_64.rpm &>> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: [mysql-community-server] package installed success!\033[0m"
else
install_status=1
echo -e "\033[31mError: [mysql-community-server] package installed fail!\033[0m"
fi
fi
else
echo -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"
exit
fi
if [ $install_status -eq 1 ];then
echo -e "\033[31mMsg: MySQL Database installed fail!\033[0m"
exit
else
echo -e "\033[32mMsg: MySQL Database installed success!\033[0m"
fi
}
# Function about init Database of MySQL
InitMySQL(){
pwd_status=0
mysqld --initialize &>> /dev/null
sleep 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" ];then
systemctl enable mysqld.service &>> /dev/null
echo -e "\033[32mMsg: MySQL Server is starting success!\033[0m"
else
echo -e "\033[31mError: MySQL Server is starting fail!\033[0m"
exit
fi
temporarily_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;
EOF
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: MySQL password changed success!\033[0m"
else
echo -e "\033[31mMsg: MySQL password changed fail!\033[0m"
pwd_status=1
exit
fi
if [ $pwd_status -eq 0 ];then
echo -e "\033[32mMsg: Init MySQL Server is success!\033[0m"
else
echo -e "\033[31mMsg: Init MySQL Server is fail!\033[0m"
exit
fi
}
# Function about install Proxy
InstallProxy() {
yum -y install zabbix-proxy-mysql zabbix-sql-scripts zabbix-selinux-policy &>> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: Zabbix Proxy install success!\033[0m"
else
echo -e "\033[31mMsg: Zabbix Proxy install fail!\033[0m"
fi
mysql -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;
EOF
cat /usr/share/zabbix-sql-scripts/mysql/proxy.sql | mysql --default-character-set=utf8mb4 -u$zabbix_username -p$zabbix_password $zabbix_db_name
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: zabbix mysql upload success\033[0m"
else
echo -e "\033[31mMsg: zabbix mysql upload fail\033[0m"
fi
mysql -uroot -p$mysql_password <<EOF
set global log_bin_trust_function_creators = 0;
quit;
EOF
# set proxy service
systemctl restart zabbix-proxy &>> /dev/null
proxy_status=$(systemctl is-active zabbix-proxy)
if [ $proxy_status = "active" ];then
systemctl enable zabbix-proxy &>> /dev/null
echo -e "\033[32mMsg: Zabbix Proxy status is starting success!\033[0m"
else
echo -e "\033[31mError: Zabbix Proxy status is starting fail!\033[0m"
Proxy_start_status=1
exit
fi
}
# Function about open firewalld port and service
OpenPort(){
# open 10051 161 http
firewall-cmd --zone=public --add-port=10050/tcp --permanent &>> /dev/null
firewall-cmd --zone=public --add-port=10051/tcp --permanent &>> /dev/null
firewall-cmd --zone=public --add-port=161/tcp --permanent &>> /dev/null
firewall-cmd --zone=public --add-service=http --permanen &>> /dev/null
firewall-cmd --reload &>> /dev/null
echo -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!" ];then
echo -e "\033[32mMsg: chrony is already install!\033[0m"
else
echo ">>>>>> start install chrony!"
yum -y install chrony
if [ $? -eq 0 ];then
echo -e "\033[32mMsg: chrony install success\033[0m"
systemctl enable chronyd --now
else
echo -e "\033[31mMsg: chrony install fail\033[0m"
fi
fi
echo ">>>>>> start synchronization time"
sed -i '/pool 2.centos.pool.ntp.org iburst/a\server 192.168.95.8 iburst' /etc/chrony.conf
systemctl restart chronyd
echo -e "\033[32mMsg: synchronization time success\033[0m"
}
echo ">>>>>> Check the local repository configuration"
yum -y install vim &>>/dev/null
if [ $? -ne 0 ];then
echo -e "\033[31mError: Please check the configuration of your local repository\033[0m"
exit
else
echo -e "\033[32mMsg: Local repository check success!\033[0m"
fi
echo ">>>>>> Install the necessary components"
tools_status=$( rpm -q net-tools | awk '{print($NF)}')
if [ $tools_status = "installed" ];then
yum -y install net-tools &>> /dev/null
fi
perl_status=$( rpm -q perl | awk '{print($NF)}')
if [ $perl_status = "installed" ];then
yum -y install perl &>> /dev/null
fi
echo -e "\033[32mMsg: Check components success!\033[0m"
echo ">>>>>> Starting install http service"
InstallHttpServer
echo ">>>>>> Starting install MySQL server"
InstallMySQL
echo ">>>>>> Starting init MySQL server"
InitMySQL
echo ">>>>>> Starting install zabbix proxy server "
InstallProxy
echo ">>>>>> Starting open firewalld port"
OpenPort
echo ">>>>>> Set Time synchronization"
Time_synchronization
echo -e "\033[32mExecuted Success! Over\033[0m"