﹝Linux﹞Zabbix 5.0 環境安裝&優化調整

(截至於Zabbix官網)

前言:

  1. 本文為本人整理之筆記及複習用,以避免原網站停止服務
  2. 本站無營利作用,如有侵權冒犯,請立即告知,會立即下架
  3. 請支持原出處網站,因此會將參考資料放在頁首

系統規格:

VM namevCPUvMemoryvDiskvOS
Zabbix2 cores2 GB400 GBCentOS7
vDiskThin DiskVirtual DeviceSizeDevice NameFile systemMount
☐FlashDisk 0SCSI .050 GiB/dev/sdaext4/
☐FlashDisk 1SCSI .150 GiB/dev/sdbxfs/var/log
☐FlashDisk 2SCSI .2100GiB/dev/sdcext4/mariabackup
☐FlashDisk 3SCSI .3200 GiB/dev/sddxfs/var/lib/mysql

Zabbix Server

FilePath
Zabbix Config/etc/zabbix
Zabbix log/var/log/zabbix
JVM Config/etc/zabbix/zabbix_java_gateway.conf

MariaDB Server

FilePath
Mariadb Config/etc/my.cnf.d/server.cnf
Mariadb log/var/log/mariadb
MariaDB backup/mariabackup
Maraidb lib/var/lib/mysql
Service
Zabbix Serversystemctl status zabbix-server
Zabbix Agentsystemctl status zabbix-agent
Zabbix Agent versionzabbix_agentd -V
MariaDBsystemctl status mariadb
JMXsystemctl status zabbix-java-gateway

*參考 Zabbix官方網站Documentation 

系統配置:

Logrotate

vi /etc/logrotate.d/zabbix-server

/var/log/zabbix/zabbix_server.log
 {
 su root root
 create 0733 root root
 compress
 copytruncate
 daily
 dateext
 dateformat -%Y-%m-%d
 delaycompress
 missingok
 notifempty
 rotate 14
}

vi /etc/logrotate.d/zabbix-agent

/var/log/zabbix/zabbix_agentd.log 
{
 su root root
 compress
 copytruncate
 daily
 dateext
 dateformat -%Y-%m-%d
 delaycompress
 missingok
 notifempty
 rotate 14
       create 0664 zabbix zabbix
}
磁碟與檔案系統管理

yum install lvm2 -y

pvcreate /dev/sdb /dev/sdc /dev/sdd

vgcreate VG_log /dev/sdb
vgcreate VG_backup /dev/sdc
vgcreate VG_mysql /dev/sdd

lvcreate -l 100%FREE -n LV_log VG_log
lvcreate -l 100%FREE -n LV_backup VG_backup
lvcreate -l 100%FREE -n LV_mysql VG_mysql

lvdisplay
lvchange -r 0 /dev/VG_log/LV_log
lvchange -r 0 /dev/VG_backup/LV_backup
lvchange -r 0 /dev/VG_mysql/LV_mysql
mkfs -t xfs /dev/VG_log/LV_log
mkfs -t xfs /dev/VG_backup/LV_backup
mkfs -t xfs /dev/VG_mysql/LV_mysql

blkid /dev/VG_log/LV_log >> /etc/fstab
blkid /dev/VG_backup/LV_backup >> /etc/fstab
blkid /dev/VG_mysql/LV_mysql >> /etc/fstab

mkdir -p /var/lib/mysql
mkdir -p /mariabackup
mkdir log
mv /var/log/* log/
mount -a
mv log/* /var/log/
rm -rf log/


Zabbix Server 5.0 安裝:

rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

yum clean all
yum install zabbix-server-mysql zabbix-agent -y

yum install centos-release-scl -y

vi /etc/yum.repos.d/zabbix.repo

[zabbix-frontend]
...
enabled=1
...

#啟用 zabbix-frontend 存儲庫

yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y

yum -y install mariadb-server
systemctl start mariadb;systemctl enable mariadb;systemctl status mariadb

mysql -uroot -p
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user zabbix@localhost identified by 'password';
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> quit;

'password' , 務必設置自己記得的資料庫密碼

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

*導入初始架構和數據,系統將提示您輸入新創建的密碼。

vi /etc/zabbix/zabbix_server.conf

DBPassword=password

vi /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

;php_value[date.timezone] = Europe/Riga
php_value[date.timezone] = Etc/GMT+4

*依照個別需要設定所需求的時區

systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

Firewall

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=10051/tcp --permanent
firewall-cmd --zone=public --add-port=10050/tcp --permanent
firewall-cmd --reload

*進入「 Web UI 」http://localhost/zabbix

*參考 官方網站 5.0 下載安裝Zabbix 

Zabbix-Server 參數優化修改

vi zabbix_server.conf

CacheSize=2G    #配置緩存大小
ValueCacheSize=2G  #歷史值緩存大小

systemctl restart zabbix-server


Zabbix MariaDB 備份:

mysql -uroot -p

select user,host,concat('SHOW GRANTS FOR ''',user,'''@''',host,''';') as GRANTS from mysql.user order by 1;

DROP USER ''@'localhost';
DROP USER 'root'@'localhost';
DROP USER ''@'zabbix';
DROP USER 'root'@'zabbix';
DROP USER 'root'@'127.0.0.1';
DROP USER 'root'@'::1';
create user mariabackup@localhost identified by 'password';
GRANT RELOAD, PROCESS, SUPER, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'mariabackup'@'localhost' IDENTIFIED BY PASSWORD '*D0135230C742059196C5B6325B4244C64593787E';
SHOW GRANTS FOR 'mariabackup'@'localhost';
FLUSH PRIVILEGES;

'password' , 務必設置自己記得的資料庫密碼

vi maintain_sh/Backup-Zabbix.sh

#!/bin/bash
## Backup the Zabbix Database with Mariabackup

MyUser="mariabackup"
MyPwsd="password"
MyHost="localhost"
MyBase="/mariabackup"
MyDest="${MyBase}/$(date +'%F')_$(hostname)"
MyTemp="${MyDest}/temp"
MyCSV="/root/maintain_sh/backup.csv"
MySQL="mysql -u ${MyUser} -p${MyPwsd} -h${MyHost}"

OldDay=$(date --date '+3 day ago' +'%F')
find ${MyBase} -type d -name "${OldDay}_$(hostname)" -exec rm -r {} +
find ${MyBase} -name "${OldDay}_$(hostname).tar" -exec rm -r {} +

## 1. Full Backup
which mariabackup
[ $? -eq 0 ] || yum -y install MariaDB-backup

FullBak="${MyDest}/FullBackup"
[ -d ${FullBak} ] || mkdir -p ${FullBak}

sync;sync;sync

START_TIME=$(date +"%s")
 END_TIME=""
DIFF_TIME=""

mariabackup --backup \
--user=${MyUser}     \
--password=${MyPwsd} \
--slave-info         \
--safe-slave-backup  \
--parallel=4         \
--compress           \
--compress-threads=4 \
--target-dir=${FullBak}

END_TIME=$(date +"%s")
DIFF_TIME=$(($END_TIME - $START_TIME))
TASK_TIME="$(($DIFF_TIME / 60)) m $(($DIFF_TIME % 60)) s"
MYDB_SIZE=$(du -h /var/lib/mysql | tail -n 1 | awk '{print $1}')
BAK_SIZE=$(du -h ${FullBak} | tail -n 1 | awk '{print $1}')
 TW_TIME=$(date --date="-12 hours ago" +"%T")

echo "$(date +'%F %T'),TW:${TW_TIME},${MYDB_SIZE},${TASK_TIME},${BAK_SIZE}" >> ${MyCSV}

sleep 60

## 2. Table and Procedure Backup
MyDmp="mysqldump -u ${MyUser} -p${MyPwsd} -h${MyHost}"
sync;sync;sync
for DB in zabbix
do
${MyDmp} --no-data              \
--routines --triggers           \
--default-character-set=utf8mb4 \
${DB} > ${MyDest}/${DB}.routines
${MyDmp} --no-data              \
--default-character-set=utf8mb4 \
${DB} > ${MyDest}/${DB}.table
done

sleep 10

## 3. MySQL.user backup
MyGrnt="${MyDest}/${MyHost}.grants"
MySQL="mysql -u ${MyUser} -p${MyPwsd} -h${MyHost}"
${MySQL} -Bse "
SELECT concat('SHOW GRANTS FOR ''',user,'''@''',host,''';') AS GRANTS
FROM mysql.user;" > ${MyTemp}
N=$(wc -l ${MyTemp} | awk '{print $1}')
for (( i=1; i<=$N; i=i+1 ))
do
 SQL=$(sed -n "${i}"p ${MyTemp})
 echo -e "$N of $i : $SQL" >> ${MyGrnt}
 ${MySQL} -Bse "${SQL}"    >> ${MyGrnt}
 echo -e "======\n"        >> ${MyGrnt}
done

sleep 10

## 4. My.cnf backup
cp -a /etc/my.cnf                 ${MyDest}
cp -a /etc/my.cnf.d               ${MyDest}

## 5. system config backup
cp -a /etc/zabbix/    ${MyDest}
cp -a /etc/logrotate.d/zabbix     ${MyDest}
cp -a /etc/logrotate.d/mysql      ${MyDest}
cp -a /etc/logrotate.d/maintain   ${MyDest}
cp -a /etc/logrotate.d/history    ${MyDest}
cp -a /etc/sysctl.conf            ${MyDest}
cp -a /etc/ssh/sshd_config        ${MyDest}
cp -a /root/maintain_sh/*.sh      ${MyDest}
tail -n 4 /etc/bashrc           > ${MyDest}/bashrc
crontab -l                      > ${MyDest}/crontab

sync;sync;sync

##tar
cd ${MyBase}
tar --remove-files -cf $(date +'%F')_$(hostname).tar ${MyDest}
chown -R mysql:mysql /var/log/mysql/

##--decompress
#mariabackup       \
#--decompress      \
#--remove-original \
#--target-dir=${FullBak}

#mariabackup --prepare \
#--target-dir=${FullBak}

find ${MyTemp} -print0 | xargs -0 rm -v > /dev/null

## End of script

chmod a+x Backup-Zabbix.sh
chown -R mysql:mysql /etc/my.cnf.d
mkdir -p /var/log/mysql/binlog
mkdir -p /var/log/mysql/redo
mkdir -p /var/log/mysql/undo
mkdir -p /var/log/mysql/tmpdir
chown -R mysql:mysql /var/log/mysql
chown -R mysql:mysql /var/lib/mysql

vi /etc/yum.repos.d/MariaDB.repo

# MariaDB 10.2 CentOS repository list - created 2016-02-28 13:16 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

yum -y install MariaDB-backup
systemctl restart zabbix-server;systemctl restart mariadb

vi /root/Script/zabbix-backup.sh

#!/bin/bash

deldate="$(date -d '1 days ago' +"%Y-%m-%d")"

rm -rf /mariabackup/${deldate}_zabbix.tar

chmod a+x zabbix-backup.sh 

crontab -e

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
 0  0  *  *  5 /bin/cat  /dev/null > /root/.mysql_history
 0  23 *  *  * /bin/bash /root/maintain_sh/Backup-Zabbix.sh
 5  5  *  *  * root      /root/Script/zabbix-backup.sh

Other:

vi .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

JAVA_HOME=/usr/local/java
LD_LIBRARY_PATH=/usr/local/apr/lib
PATH=$PATH:$HOME/bin:/usr/local/java/bin
export PATH JAVA_HOME LD_LIBRARY_PATH

## The different colours as variables
X="\e[00;37m"
R="\e[01;31m"
G="\e[01;32m"
Y="\e[01;33m"
B="\e[01;34m"
P="\e[01;35m"
W="\e[01;37m"
echo -e "---------------------------------------------------------------------"
java -version
echo -e "---------------------------------------------------------------------"
netstat -tunlp | egrep java | uniq
echo -e "---------------------------------------------------------------------"
for S in mariadb zabbix-server zabbix-agent httpd rh-php72-php-fpm
do
ACT=$(systemctl is-active ${S})
if [ $? -eq 0 ];then
 ACT="${B}${ACT}${X}"
else
 ACT="${R}${ACT}${X}\tsystemctl start ${S}"
fi
echo -e "SERVICE: ${P}${S}${X}     = ${ACT}"
done
echo -e "---------------------------------------------------------------------"
netstat -tunlp | egrep 'mysqld|php-fpm|httpd|zabbix'
echo -e "---------------------------------------------------------------------"
df -Th | egrep -v "boot|tmpfs"
echo -e "---------------------------------------------------------------------"
ll -h /usr/lib/zabbix/alertscripts
echo -e "---------------------------------------------------------------------"


Troubleshooting:

執行備份腳本時,發生錯誤訊息:

mysqldump: Got error: 1142: "SELECT command denied to user 'mariabackup'@'localhost' for table 'acknowledges'" when using LOCK TABLES
mysqldump: Got error: 1142: "SELECT command denied to user 'mariabackup'@'localhost' for table 'acknowledges'" when using LOCK TABLES
ERROR 1142 (42000) at line 2: SELECT command denied to user 'mariabackup'@'localhost' for table 'user'

依照錯誤訊息,其原因是mariabackup 沒有SELECT權限,因此我們需要依序關閉Zabbix Server、MariaDB

然後啟動Mysql安全模式,root使用者登入,來開啟mariabackup的Select權限

mysqld_safe --skip-grant-tables &

mysql -uroot

mysql> use mysql;

mysql> show tables;

 mysql> select * from user where user='mariabackup' and host='localhost'\G;

update user set `Select_priv` ='Y' where user='mariabackup' and host='localhost';

FLUSH PRIVILEGES;

-END-


天下風雲出我輩,一入江湖歲月催,

塵事如潮人如水,只嘆江湖幾人回。