#/bin/sh
#卸載mariadb
rpm -e --nodeps `rpm -qa|grep mariadb*`
#a. Install Zabbix repository
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum clean all
#替換阿里雲鏡像源
sed -i 's/repo.zabbix.com/mirrors.aliyun.com\/zabbix/g' /etc/yum.repos.d/zabbix.repo
#b. Install Zabbix server and agent
yum install -y zabbix-server-mysql zabbix-agent
#c. Install Zabbix frontend
#Enable Red Hat Software Collections
yum install -y centos-release-scl
#編輯配置文件 /etc/yum.repos.d/zabbix.repo and enable zabbix-frontend repository.
sed -i '11s/enabled=0/enabled=1/' /etc/yum.repos.d/zabbix.repo
#Install Zabbix frontend packages.
yum install -y zabbix-web-mysql-scl zabbix-apache-conf-scl
#安裝mysql數據庫
#配置清華大學yum源,默認安裝最新,安裝指定版本可以將其他版本的enabled修改為0
#下載GPG-KEY導入rpm,防止報錯
curl -o /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql https://repo.mysql.com/RPM-GPG-KEY-mysql
rpm --import /etc/pki/rpm-gpg/RPM*
cat > /etc/yum.repos.d/mysql-community.repo <<EOF
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7-\$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7-\$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-5.6-community]
name=MySQL 5.6 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-\$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-\$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-8.0-community]
name=MySQL 8.0 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-\$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
EOF
yum makecache
#數據庫默認安裝mysql8.0
yum -y install mysql-server
systemctl start mysqld
systemctl enable mysqld
echo "mysql安裝完成"
#數據庫操作,更改root密碼
#獲取安裝時的臨時密碼賦值給mysql_pwd
old_pwd=`grep 'temporary password' /var/log/mysqld.log |rev|cut -c-12|rev`
#mysql_pwd=Zabbix@12345
new_pwd=Zabbix@123
#修改root密碼
mysql --connect-expired-password -uroot -p"$old_pwd" -e "alter user 'root'@'localhost' identified by '$new_pwd';" 2> /dev/null
mysql --connect-expired-password -uroot -p$new_pwd -e "flush privileges;" 2> /dev/null
#創建數據庫zabbix
mysql --connect-expired-password -uroot -p$new_pwd -e "create database zabbix character set utf8 collate utf8_bin;" 2> /dev/null
#創建zabbix用戶
mysql --connect-expired-password -uroot -p$new_pwd -e "create user zabbix@localhost identified by 'Zabbix@12345';" 2> /dev/null
#賦予權限
mysql --connect-expired-password -uroot -p$new_pwd -e "grant all privileges on zabbix.* to zabbix@localhost;" 2> /dev/null
mysql --connect-expired-password -uroot -p$new_pwd -e "flush privileges;" 2> /dev/null
echo "數據庫賬號設置完成"
#導入初始架構
#編輯/usr/share/doc/zabbix-server-mysql*/create.sql.gz在首行添加use zabbix;
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pZabbix@12345 zabbix 2> /dev/null
echo "數據導入完成"
#編輯配置文件 /etc/zabbix/zabbix_server.conf
sed -i '/DBPassword=/aDBPassword=Zabbix@12345' /etc/zabbix/zabbix_server.conf
#為Zabbix前端配置PHP
sed -i 's#^.*date.timezone.*$#php_value[date.timezone] = Asia/Shanghai#g' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
#啟動Zabbix server和agent進程,並為它們設置開機自啟
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
配置清華大學grafana源
cat > /etc/yum.repos.d/grafana.repo << EOF
[grafana]
name=grafana
baseurl=https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm
repo_gpgcheck=0
enabled=1
gpgcheck=0
EOF
yum makecache
yum -y install grafana
#安裝zabbix插件
grafana-cli plugins install alexanderzobnin-zabbix-app
#啟動grafana
systemctl start grafana-server
systemctl enable grafana-server
#登錄地址zabbix http://ip/zabbix/
#登錄地址grafana http://ip:3000
#zabbix默認賬號Admin/zabbix
#grafana默認賬號admin/admin
