Zabbix學習之路(五)之MySQL監控


  • 1.linux-node2節點安裝數據庫

[root@linux-node2 ~]# yum install -y mariadb-server
[root@linux-node2 ~]# systemctl start mariadb
[root@linux-node2 ~]# netstat -tulnp |grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      41299/mysqld
  • 2.Zabbix添加數據庫主機監控

被動模式對Hostname沒要求,但主動模式必須與主機Hostname一致。
"Configuration"-->"Host"(填入主機信息)-->"Templates"(鏈接MySQL模板)
如圖:

 

創建成功后,可以查看到MySQL相應的監控信息,數據庫的增改刪查,如圖:

 

命令行進行查看獲取的信息:
[root@linux-node1 ~]# zabbix_get -s linux-node2 -k mysql.status[Com_begin]
0
[root@linux-node1 ~]# zabbix_get -s linux-node2 -k mysql.status[Slow_queries]
0 數據庫模板監控配置主要來自linux-node2節點:/etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 的配置
[root@linux-node2 ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 
 
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.
 
# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -N | awk '{print $$2}'    #mysql的狀態獲取
 
# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/var/lib/zabbix mysql -N'
 
UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive    #mysql的存活獲取
UserParameter=mysql.version,mysql -V    #mysql的版本獲取
  • 3.帶密碼對MySQL監控

以上對數據庫的監控,都是沒有密碼直接獲取值,這是不合理的,那么需要如何添加密碼進行獲取監控數據呢?

(1)先對數據庫進行授權和密碼,通過zabbix用戶進行獲取數據,此處的授權由於試驗,就授權了全部權限,正式生產時不能這樣設置。
 
[root@linux-node2 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 711
Server version: 5.5.56-MariaDB MariaDB Server
 
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> grant all on *.* to zabbix@localhost identified by "zabbix";
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> quit;
Bye
 
2)修改監控配置,添加用戶名密碼
[root@linux-node2 ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 
 
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -uzabbix -pzabbix -N | awk '{print $$2}'
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/var/lib/zabbix mysql -N'
UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping -uzabbix -pzabbix | grep -c alive
UserParameter=mysql.version,mysql -V
 
[root@linux-node2 ~]# systemctl restart zabbix-agent

修改完畢后,我們可以看到在Item項都顯示Not supported,如圖:

此時修改一下zabbix對無效監控項的刷新時間,默認是600s,我們改為30s。修改完成后就會變成enabled

"Administration"-->"General"-->右上角選擇"other"-->"Refresh unsupported items (in sec)"改為30

  • 4.靈活使用宏變量(Macrros)進行傳參配置用戶名密碼監控

如圖:在主機中配置變量

 

(1)修改配置文件:
[root@linux-node2 ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 
UserParameter=mysql.status[*],echo "show global status where Variable_name='$3';" | HOME=/var/lib/zabbix mysql -u$1 -p$2 -N | awk '{print $$2}'
UserParameter=mysql.ping[*],HOME=/var/lib/zabbix mysqladmin -u$1 -p$2 ping| grep -c alive
[root@linux-node2 ~]# systemctl restart zabbix-agent
 
此時,如果未傳入用戶名密碼訪問是被拒絕的:
[root@linux-node1 ~]# zabbix_get -s linux-node2 -k mysql.status[Slow_queries]
Enter password: ERROR 1045 (28000): Access denied for user 'Slow_queries'@'localhost' (using password: YES)
[root@linux-node1 ~]# zabbix_get -s linux-node2 -k mysql.status[zabbix,zabbix,Slow_queries]
0 (2)修改模板,模板中的變量值可以不設置,因為在連接模板時進行修改變量,此時會自動覆蓋在模板設置的變量值:

(3)修改模板中的Item:增加傳參的變量{$USER},{PASSWD}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM