-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix_server_setup.sh
117 lines (95 loc) · 4.09 KB
/
zabbix_server_setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
echo "==================================="
echo " ZABBIX SERVER SETUP"
echo "==================================="
# Add Zabbix repository
rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/zabbix-release-6.4-1.el9.noarch.rpm
dnf clean all
# Install Zabbix packages
dnf install -y zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent
echo "==================================="
echo " MARIADB SETUP"
echo "==================================="
# Create MariaDB repository file
cat <<EOF | sudo tee /etc/yum.repos.d/MariaDB.repo
# MariaDB 11.2 RedHatEnterpriseLinux repository list - created 2024-04-01 16:59 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
# rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# baseurl = https://rpm.mariadb.org/11.2/rhel/\$releasever/\$basearch
baseurl = https://mirrors.aliyun.com/mariadb/yum/11.2/rhel/\$releasever/\$basearch
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
EOF
# Install MariaDB
dnf install -y MariaDB-server MariaDB-client
# Start and enable MariaDB service
systemctl start mariadb
systemctl enable mariadb
echo "==================================="
echo " DATABASE SETUP"
echo "==================================="
# Prompt for the new root password
read -s -p "Enter the new root password: " root_password
# Execute the MySQL command to set the root password
/usr/bin/mariadb -uroot <<_EOF_
ALTER USER 'root'@'localhost' IDENTIFIED BY '$root_password';
FLUSH PRIVILEGES;
_EOF_
# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Root password set successfully."
else
echo "Error occurred while setting the root password."
fi
# Execute SQL commands to perform cleanup and security measures
/usr/bin/mariadb -uroot -p"$root_password" <<_EOF_
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db LIKE 'test\\_%';
FLUSH PRIVILEGES;
_EOF_
# Check if the commands were successful
if [ $? -eq 0 ]; then
echo -e"\nDatabase cleanup and security measures completed successfully."
else
echo -e "\nError occurred while performing database cleanup and security measures."
fi
# Prompt for the password for the 'zabbix' user
read -s -p "Enter the password for the 'zabbix' user: " zabbix_password
# Execute SQL commands to create database, user, grant privileges, and set global variable
/usr/bin/mariadb -uroot -p"$root_password" <<_EOF_
CREATE DATABASE IF NOT EXISTS zabbix CHARACTER SET utf8 COLLATE utf8_bin;
CREATE USER IF NOT EXISTS 'zabbix'@'localhost' IDENTIFIED BY '$zabbix_password';
GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost';
SET GLOBAL log_bin_trust_function_creators = 1;
FLUSH PRIVILEGES;
_EOF_
# Check if the commands were successful
if [ $? -eq 0 ]; then
echo -e "\nDatabase, user, privileges, and global variable set successfully."
else
echo -e "\nError occurred while performing database, user, privileges, and global variable setup."
fi
echo "==================================="
echo " IMPORT INITIAL SCHEMA"
echo "==================================="
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8 -uzabbix -p$zabbix_password zabbix
/usr/bin/mariadb -uroot -p"$root_password" <<_EOF_
set global log_bin_trust_function_creators = 0;
_EOF_
# Check if the commands were successful
if [ $? -eq 0 ]; then
echo -e "\nglobal log_bin_trust_function_creators set to 0."
else
echo -e "\nError occurred while setting lobal log_bin_trust_function_creators to 0."
fi
sed -i '/# DBPassword=/a DBPassword=$zabbix_password' /etc/zabbix/zabbix_server.conf
echo "==================================="
echo " RESTART ZABBIX SERVER"
echo "==================================="
systemctl restart zabbix-server zabbix-agent httpd php-fpm
systemctl enable zabbix-server zabbix-agent httpd php-fpm