當(dāng)前位置:首頁(yè) > IT技術(shù) > 數(shù)據(jù)庫(kù) > 正文

實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL
2022-05-31 17:10:54

1. Zabbix監(jiān)控MySQL的規(guī)劃

? ? ? ?在用Zabbix6.0.4做監(jiān)控,創(chuàng)建監(jiān)控項(xiàng)之前要盡量規(guī)劃好要監(jiān)控什么、如何監(jiān)控、監(jiān)控?cái)?shù)據(jù)如何存放、監(jiān)控?cái)?shù)據(jù)如何展現(xiàn)、異常情況報(bào)警、報(bào)警處置等。 本次實(shí)踐采用自行編寫的MySQL監(jiān)控腳本( /etc/zabbix/zabbix_agentd.d/check_mysql.sh )來(lái)實(shí)現(xiàn)Zabbix對(duì)MySQL的監(jiān)控。


2. 準(zhǔn)備MySQL主機(jī)

在IP:192.168.250.48 服務(wù)器上通過(guò)自定義yum源方式,安裝MySQL8.0。

#################################################################################
#### MySQL主機(jī)上也要先安裝 zabbix-agent
[root@CentOS84-IP48 ]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
[root@CentOS84-IP48 ]#dnf clean all
[root@CentOS84-IP48 ]#dnf -y install zabbix-agent
[root@CentOS84-IP48 ]#systemctl enable --now zabbix-agent

#################################################################################
#### 安裝mysql80的yum源,并安裝啟動(dòng)MYSQL
[root@CentOS84-IP48 ]#wget https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm
[root@CentOS84-IP48 ]#rpm -ivh mysql80-community-release-el8-1.noarch.rpm
[root@CentOS84-IP48 ]#dnf install -y mysql-server

[root@CentOS84-IP48 ]#systemctl enable --now mysqld.service
[root@CentOS84-IP48 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
[root@CentOS84-IP48 ]#

## 數(shù)據(jù)庫(kù)安全初始化
[root@CentOS84-IP48 ]#find / -name mysql_secure_installation
/usr/bin/mysql_secure_installation
[root@CentOS84-IP48 ]#mysql_secure_installation

#################################################################################
#### 授權(quán)mysql用戶zabbix可以登錄并查詢 (在zabbix的agent客戶端將要查詢需要的監(jiān)控?cái)?shù)據(jù))
## 利用前面設(shè)定的root密碼登錄到mysql
[root@CentOS84-IP48 ]#mysql -uroot -pshone2022
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 11
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)

mysql>

mysql> show master logs;
+---------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 | 446 | No |
+---------------+-----------+-----------+
1 row in set (0.00 sec)

mysql>


mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.01 sec)

## 不支持一次性完成創(chuàng)建和授權(quán)操作
mysql> grant all on *.* to zabbix@localhost identified by 'shone2022';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'shone2022'' at line 1

##
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

mysql> create user zabbix@"%" identified by 'shone2022';
Query OK, 0 rows affected (0.02 sec)

mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| zabbix | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

## 授權(quán)zabbix用戶可以獲取數(shù)據(jù)
mysql> grant all privileges on *.* to zabbix@"%";
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

3. 自定義監(jiān)控命令和監(jiān)控腳本

? ? ? 本部分將詳細(xì)介紹如何編寫和測(cè)試監(jiān)控命令、腳本,并驗(yàn)證運(yùn)行效果。 自定義腳本監(jiān)控?cái)U(kuò)展Agent的思路:Zabbix Server與Agent之間監(jiān)控?cái)?shù)據(jù)的采集主要是通過(guò)Zabbix Server主動(dòng)向Agent詢問(wèn)某個(gè)Key的值,Agent會(huì)根據(jù)Key去調(diào)用相應(yīng)的函數(shù)(或者命令等)去獲取這個(gè)值并返回給Server端。Zabbix的Agent本并沒(méi)有內(nèi)置MySQL的監(jiān)控功能(但是Server端提供了相應(yīng)的Template配置),所以我們需要使用Zabbix的User Parameters功能,為MySQL添加監(jiān)控腳本。


3.1 編寫監(jiān)控MySQL的版本和存活狀態(tài)的命令行

#### zabbix agent端
#################################################################################
#### mysql存活檢測(cè)
## 利用UserParameter參數(shù)自定義Agent Key來(lái)完成。采用mysqladmin這個(gè)工具來(lái)實(shí)現(xiàn),下面是整個(gè)編寫、校驗(yàn)的全流程記錄,對(duì)于要自己動(dòng)手編寫監(jiān)控項(xiàng)腳本和命令具有指導(dǎo)意義。
[root@CentOS84-IP48 ]#mysqladmin -h 127.0.0.1 -u zabbix -pshone2022 ping
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqld is alive

## 如果MySQL狀態(tài)正常,會(huì)顯示mysqld is alive,否則會(huì)提示連不上。對(duì)于服務(wù)器端,mysqld is alive表述,計(jì)算機(jī)程序語(yǔ)言不好處理的,服務(wù)器端最好接收1和0,1表示服務(wù)可用,0表示服務(wù)不可用。改進(jìn)一下這個(gè)命令,如下:
[root@CentOS84-IP48 ]#mysqladmin -h 127.0.0.1 -u zabbix -pshone2022 ping | grep -c alive
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
1
[root@CentOS84-IP48 ]#

## 從上面的輸出結(jié)果看,有mysqladmin: [Warning]信息,需要處置掉,基本方法就是將密碼寫在文件中
## 用戶名和密碼放在命令中對(duì)于以后的維護(hù)不好,所以我們?cè)? /etc/zabbix/ 下創(chuàng)建一個(gè)包含MySQL用戶名和密碼的配置文件“ .my.cnf ”,如下:

[root@CentOS84-IP48 ]#find / -name zabbix
/run/zabbix
/etc/zabbix
/var/lib/selinux/targeted/active/modules/100/zabbix
/var/lib/selinux/targeted/tmp/modules/100/zabbix
/var/log/zabbix
/usr/share/selinux/targeted/default/active/modules/100/zabbix
[root@CentOS84-IP48 ]#
[root@CentOS84-IP48 ]#ll /usr/bin/mysqladmin
-rwxr-xr-x 1 root root 7147544 Sep 2 2021 /usr/bin/mysqladmin
[root@CentOS84-IP48 ]#ll /etc/zabbix
total 20
-rw-r--r-- 1 root root 16451 May 3 15:30 zabbix_agentd.conf
drwxr-xr-x 2 root root 6 May 3 15:30 zabbix_agentd.d


## .my.cnf 文件格式內(nèi)容
[root@CentOS84-IP48 ]#vim /etc/zabbix/.my.cnf
[client]
user=zabbix
host=127.0.0.1
password=shone2022

## 這個(gè)文件是隱藏文件,需要家 -a 才能看到,同時(shí)注意必須注意寫法 [client] 字段必須有,否則執(zhí)行命令時(shí)候報(bào)錯(cuò)。

[root@CentOS84-IP48 ]#ll -a /etc/zabbix/
total 36
drwxr-xr-x 3 root root 70 May 25 20:04 .
drwxr-xr-x. 144 root root 8192 May 25 19:19 ..
-rw-r--r-- 1 root root 55 May 25 20:01 .my.cnf
-rw-r--r-- 1 root root 16451 May 3 15:30 zabbix_agentd.conf
drwxr-xr-x 2 root root 6 May 3 15:30 zabbix_agentd.d
[root@CentOS84-IP48 ]#cat /etc/zabbix/.my.cnf
[client]
user=zabbix
host=127.0.0.1
password=shone2022
[root@CentOS84-IP48 ]#

## 有了密碼文件后的命令進(jìn)化為下面的格式
[root@CentOS84-IP48 ]#HOME=/etc/zabbix/ mysqladmin ping | grep -c alive
1

## 完成這步后需要做的就是將這個(gè)監(jiān)控命令添加到Zabbix Agent中,并與一個(gè)Key對(duì)應(yīng),這樣Zabbox Server就能通過(guò)這個(gè)Key獲取MySQL的狀態(tài)了。用mysql.ping作為MySQL狀態(tài)的Key。將下面的命令行可以直接寫在agent的配置文件中。

UserParameter=mysql.ping,HOME=/etc/zabbix/ mysqladmin ping | grep -c alive

3.2 編寫監(jiān)控MySQL的性能腳本

? ? ? ?自行編寫的腳本去監(jiān)控MySQL的靈活性更強(qiáng),可以獲取自己想要的數(shù)據(jù)和狀態(tài)。更適合去監(jiān)控MySQL主從、主主、讀寫分離模式下的生產(chǎn)環(huán)境下的MySQL服務(wù)器。

#### 性能腳本
[root@CentOS84-IP48 ]#vim /etc/zabbix/zabbix_agentd.d/check_mysql.sh
[root@CentOS84-IP48 ]#cat /etc/zabbix/zabbix_agentd.d/check_mysql.sh
#!/bin/bash
# -------------------------------------------------------------------------------
# FileName: check_mysql.sh
# -------------------------------------------------------------------------------

# 用戶名
MYSQL_USER='zabbix'

# 密碼
MYSQL_PWD='shone2022'

# 主機(jī)地址/IP 下面這個(gè)寫法是agent 和 數(shù)據(jù)庫(kù)在同臺(tái)機(jī)器上
MYSQL_HOST='127.0.0.1'

# 端口
MYSQL_PORT='3306'

# 數(shù)據(jù)連接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"

# 參數(shù)是否正確
if [ $# -ne "1" ];then
echo "arg error!"
fi

# 獲取數(shù)據(jù)
case $1 in
Uptime)
result=`${MYSQL_CONN} status 2>/dev/null |cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status 2>/dev/null |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status 2>/dev/null |cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;

*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac

## 腳本運(yùn)行授權(quán)和權(quán)屬修改
[root@CentOS84-IP48 ]#chmod a+x /etc/zabbix/zabbix_agentd.d/check_mysql.sh
[root@CentOS84-IP48 ]#chown zabbix.zabbix /etc/zabbix/zabbix_agentd.d/check_mysql.sh
[root@CentOS84-IP48 ]#

4. Zabbix Agent添加自定義監(jiān)控項(xiàng)

#### 前面已經(jīng)安裝好zabbix_agent,修改并配置好zabbix_agent
[root@CentOS84-IP48 ]#vim /etc/zabbix/zabbix_agentd.conf
...................
## mysql版本
UserParameter=mysql.version,mysql -V
## 監(jiān)控MySQL的存活狀態(tài),當(dāng)狀態(tài)發(fā)生異常,發(fā)出報(bào)警;
UserParameter=mysql.ping,HOME=/etc/zabbix/ mysqladmin ping | grep -c alive
# #監(jiān)控mysql性能的腳本
UserParameter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/check_mysql.sh $1
....................

[root@CentOS84-IP48 ]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.250.18
ListenPort=10050
ServerActive=127.0.0.1
Hostname=192.168.250.48
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UserParameter=mysql.version,mysql -V
UserParameter=mysql.ping,HOME=/etc/zabbix/ mysqladmin ping | grep -c alive
UserParameter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/check_mysql.sh $1
[root@CentOS84-IP48 ]#
[root@CentOS84-IP48 ]#systemctl restart zabbix-agent

#### zabbix_agent上測(cè)試 本機(jī)命令及腳本編寫的監(jiān)控項(xiàng)的實(shí)際效果
[root@CentOS84-IP48 ]#/usr/sbin/zabbix_agentd -t mysql.ping
mysql.ping [t|1]
[root@CentOS84-IP48 ]#/usr/sbin/zabbix_agentd -t mysql.version
mysql.version [t|mysql Ver 8.0.26 for Linux on x86_64 (Source distribution)]
[root@CentOS84-IP48 ]#/usr/sbin/zabbix_agentd -t mysql.status[Uptime]
mysql.status[Uptime] [t|9921]
[root@CentOS84-IP48 ]#/usr/sbin/zabbix_agentd -t mysql.status[Com_update]
mysql.status[Com_update] [t|0]
[root@CentOS84-IP48 ]#

5. Zabbix Server命令行測(cè)試監(jiān)控項(xiàng)數(shù)據(jù)

#### Zabbix_server測(cè)試聯(lián)通情況等
[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.48 -p 10050 -k mysql.status[Uptime]
10190
[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.48 -p 10050 -k mysql.status[Com_update]
0
[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.48 -p 10050 -k mysql.ping
1
[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.48 -p 10050 -k mysql.version
mysql Ver 8.0.26 for Linux on x86_64 (Source distribution)
[root@CentOS84-IP18 ]#

6. 創(chuàng)建監(jiān)控模板

創(chuàng)建模板,并自定義監(jiān)控項(xiàng)、觸發(fā)器、圖形及儀表盤 配置 --- 模板 --- 創(chuàng)建模板

6.1 自定義模板

實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL_centos


實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL_MySQL監(jiān)控腳本_02


6.2 自定義監(jiān)控項(xiàng)

實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL_MySQL監(jiān)控腳本_03


6.3 自定義監(jiān)控圖形

實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL_Zaabix監(jiān)控MYSQL_04


7. 創(chuàng)建主機(jī)并關(guān)聯(lián)自定義MySQL監(jiān)控模板

創(chuàng)建主機(jī)并關(guān)聯(lián)自定義模板?MySQL-shone

配置 --- 主機(jī) --- 創(chuàng)建主機(jī)


實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL_Zaabix監(jiān)控MYSQL_05


8. 驗(yàn)證監(jiān)控項(xiàng)數(shù)據(jù)


實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL_mysql_06


實(shí)踐案例:Zabbix利用自定義腳本監(jiān)控MySQL_Zaabix監(jiān)控MYSQL_07


本文摘自 :https://blog.51cto.com/s

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >