標籤:zabbix mysql
1.linux-node2節點安裝資料庫
[[email protected] ~]# yum install -y mariadb-server[[email protected] ~]# systemctl start mariadb[[email protected] ~]# netstat -tulnp |grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 41299/mysqld
2.Zabbix添加資料庫主機監控
被動模式對Hostname沒要求,但主動模式必須與主機Hostname一致。"Configuration"-->"Host"(填入主機資訊)-->"Templates"(連結MySQL模板)
建立成功後,可以查看到MySQL相應的監控資訊,資料庫的增改刪查,
命令列進行查看擷取的資訊:[[email protected] ~]# zabbix_get -s linux-node2 -k mysql.status[Com_begin]0[[email protected] ~]# zabbix_get -s linux-node2 -k mysql.status[Slow_queries]0資料庫模板監控配置主要來自linux-node2節點:/etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 的配置[[email protected] ~]# 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 tableUserParameter=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使用者進行擷取資料,此處的授權由於實驗,就授權了全部許可權,正式生產時不能這樣設定。[[email protected] ~]# mysqlWelcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 711Server version: 5.5.56-MariaDB MariaDB ServerCopyright (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 [email protected] 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)修改監控配置,添加使用者名稱密碼[[email protected] ~]# 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 aliveUserParameter=mysql.version,mysql -V[[email protected] ~]# systemctl restart zabbix-agent
修改完畢後,我們可以看到在Item項都顯示Not supported,
此時修改一下zabbix對無效監控項的重新整理時間,預設是600s,我們改為30s。修改完成後就會變成enabled
"Administration"-->"General"-->右上方選擇"other"-->"Refresh unsupported items (in sec)"改為30
4.靈活使用宏變數(Macrros)進行傳參配置使用者名稱密碼監控
在主機中組態變數
(1)修改設定檔:[[email protected] ~]# 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[[email protected] ~]# systemctl restart zabbix-agent此時,如果未傳入使用者名稱密碼訪問是被拒絕的:[[email protected] ~]# 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)[[email protected] ~]# zabbix_get -s linux-node2 -k mysql.status[zabbix,zabbix,Slow_queries]0
(2)修改模板,模板中的變數值可以不設定,因為在串連模板時進行修改變數,此時會自動覆蓋在模板設定的變數值:
(3)修改模板中的Item:增加傳參的變數{$USER},{PASSWD}
Zabbix之MySQL監控