#!/bin/bash

# kiem tra quyen root
if [ "$EUID" -ne 0 ]; then
  echo "Vui long chay voi quyen root."
  exit
fi

# ham tao pass ngau nhien
generate_password() {
  echo $(openssl rand -base64 12)
}

# Yeu cau nhap mat khau hoac tao mat khau tu dong
echo "Ban co muon nhap mat khau MySQL cho user Zabbix? (y/n)"
read -r use_custom_password

if [ "$use_custom_password" == "y" ]; then
  echo "Nhap mat khau cho user Zabbix:"
  read -s zabbix_password
else
  echo "Tao mat khau tu dong cho user Zabbix..."
  zabbix_password=$(generate_password)
  echo "Mat khau tu dong la: $zabbix_password"
fi

# Cat nhap he thong
echo "cap nhat he thong..."
apt update -y && apt upgrade -y

# Them repository của Zabbix 6.0
echo "Them repository cua Zabbix 6.0..."
wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest+ubuntu22.04_all.deb
dpkg -i zabbix-release_latest+ubuntu22.04_all.deb
apt update -y

# Cai dat Zabbix server, frontend và Zabbix agent
echo "Cai dat Zabbix server, frontend và agent..."
apt install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent -y

# Cai dat MariaDB
echo "Cai dat MariaDB..."
apt install mariadb-server -y

# Start va enable MariaDB
systemctl start mariadb
systemctl enable mariadb

# Cau hinh database cho Zabbix
echo "Cau hinh database cho Zabbix..."
mysql -uroot -e "CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;"
mysql -uroot -e "CREATE USER 'zabbix'@'localhost' IDENTIFIED BY '$zabbix_password';"
mysql -uroot -e "GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"

# Import du lieu ban dau cho Zabbix
echo "Import du lieu ban dau cho Zabbix..."
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p"$zabbix_password" zabbix
mysql -uroot -e "set global log_bin_trust_function_creators = 0;"

# Cau hinh Zabbix server
echo "Cau hinh Zabbix server..."
sed -i "s|# DBPassword=|DBPassword=$zabbix_password|" /etc/zabbix/zabbix_server.conf

# Cau hinh PHP cho Zabbix frontend
echo "Cau hinh PHP cho Zabbix frontend..."
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

# Thiet lap mui gio cho PHP
sudo sed -i "s/^;date.timezone =.*/date.timezone = \"Asia\/Ho_Chi_Minh\"/" /etc/php/8.1/fpm/php.ini

# Khoi dong lai Zabbix server và cac dich vu lien quan
echo "Khoi dong lai Zabbix server và cac dich vu lien quan..."
systemctl restart zabbix-server zabbix-agent nginx php8.1-fpm
systemctl enable zabbix-server zabbix-agent nginx php8.1-fpm

# Mo cong tren firewall
echo "Mo cong tren firewall..."
ufw allow 8080/tcp
#ufw allow 443/tcp
ufw reload

#Thay password Admin/zabbix default
Admin_password=$(generate_password)
mysql -uroot -e "UPDATE users SET passwd=md5('$Admin_password') WHERE username='Admin';" zabbix

ip_server=$(curl -s ifconfig.me)

# In lai thong tin
echo "===================================================="
echo "Install Zabbix Complete!"
echo "===================================================="
echo "Thong tin dang nhap co so du lieu user Zabbix:"
echo "User: zabbix"
echo "Password: $zabbix_password"
echo "Hay truy cap http://$ip_server:8080 de hoan tat cau hinh"
echo "Thong tin dang nhap Zabbix:"
echo "User: Admin"
echo "Password: $Admin_password"
