{"id":145766,"date":"2019-06-02T08:05:56","date_gmt":"2019-06-02T00:05:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=145766"},"modified":"2019-05-24T09:27:38","modified_gmt":"2019-05-24T01:27:38","slug":"zabbix-monitor-mysql","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/zabbix-monitor-mysql.html","title":{"rendered":"\u5982\u4f55\u7528zabbix\u76d1\u63a7mysql\u591a\u5b9e\u4f8b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nagent\u4e0a\u8d77\u4e86\u591a\u4e86 mysql\u5b9e\u4f8b\uff0c\u5360\u7528\u4e0d\u540c\u7684\u7aef\u53e3\uff0cagent \u4ec5\u5728\u521d\u59cb\u72b6\u51b5\u4e0b\uff0c\u585e\u5165\u811a\u672c\u548c \u952e\u914d\u7f6e\uff0c\u7136\u540e\u91cd\u542f\u3002 \u4ee5\u540e\u7ef4\u62a4\u7684\u65f6\u5019\uff08mysql\u7aef\u53e3\u53d8\u52a8\uff09\uff0c\u8981\u505a\u5230 \u4e0d\u80fd \u52a8agent\uff0c\u529b\u4e89 \u53ea\u5728 web\u7aef \u6216\u8005server\u7aef\u505a\u4fee\u6539 \u5c31\u80fd\u81ea\u52a8\u76d1\u63a7\u5230\u5bf9\u5e94\u7aef\u53e3\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u7528\u5230\u7684\u4e3b\u8981\u5de5\u5177\uff1a \u5b8f\uff08\u5206\u4e3a\u6a21\u677f\u5b8f\uff0c\u4e3b\u673a\u5b8f\uff0c\u81ea\u52a8\u53d1\u73b0\u5b8f\uff09<\/strong><\/div>\n
      \u4e3b\u673a\u5b8f\u683c\u5f0f\uff1a$MACRO   \uff08\u76f4\u63a5\u5c31\u53ef\u4ee5 \u586b  \u503c\uff09\r\n           \u81ea\u52a8\u53d1\u73b0\u5b8f\uff1a#MACRO    \uff08\u9700\u8981\u914d\u5408 \u521b\u5efa\u7684 key \u53d6\u503c\uff09\r\n<\/pre>\n
\u5927\u4f53\u67b6\u6784<\/strong><\/div>\n

1.agent\u4e3b\u673a -- agent \u4e3b\u673a\u521b\u5efa \u5b8f\u53d8\u91cf $MYSQLPORT \uff0c\u5b8f\u53d8\u91cf\u7684\u503c\u4e3a Discovery rules\u7684 \u81ea\u52a8\u53d1\u73b0\u811a\u672c\u53c2\u6570
\n2.\u6a21\u677f -- \u6a21\u677f\u91cc\u521b\u5efa\u81ea\u52a8\u53d1\u73b0\u89c4\u5219\uff0c\u9760\u7684\u662f\u81ea\u52a8\u53d1\u73b0\u811a\u672c\uff0c\u5f97\u5230\u81ea\u52a8\u53d1\u73b0\u5b8f\u53d8\u91cf\u3002
\n3.\u76d1\u63a7\u9879-- \u5728Discovery rules \u91cc\u521b\u5efa\u76d1\u63a7\u9879\uff0c\u76d1\u63a7\u9879\u540d\u79f0\u548ckey\u503c\u91cc\u90fd\u6709\u81ea\u52a8\u53d1\u73b0\u5b8f\u53d8\u91cf\uff0c\u4f1a\u81ea\u52a8\u751f\u6210\u76f8\u5173\u7aef\u53e3\u7684\u76d1\u63a7\u9879
\n4.\u5ba2\u6237\u7aef \u811a\u672c\u548c\u952e\u521b\u5efa\uff0c\u6765\u652f\u6301\u6574\u4e2a\u67b6\u6784\u8fd0\u884c<\/p>\n

\u642d\u5efa\u6b65\u9aa4<\/strong><\/div>\n
1.\u521b\u5efa\u6a21\u677f<\/strong><\/span><\/div>\n
\n
\n
<\/div>\n
\"\"<\/div>\n<\/div>\n
<\/div>\n<\/div>\n

\u56e0\u4e3a\u8fd9\u4e2a\u6a21\u677f\u662f \u514b\u9686 \u7684\u9ed8\u8ba4 mysql\u76d1\u63a7\u6a21\u677f\uff0c\u53ef\u4ee5\u5ffd\u7565 items\uff0ctriggers\u3002 \u76d1\u63a7\u9879\u90fd\u5728 Discovery rules \u91cc\u3002<\/p>\n

2.\u4e3a\u6a21\u677f\u6dfb\u52a0 \u81ea\u52a8\u53d1\u73b0\u5b8f\uff08\u81ea\u52a8\u53d1\u73b0\u5b8f\u58f0\u660e\u7684\u9014\u5f84\u5c31\u662f\u4e0b\u56fe\u7684\u811a\u672c\uff0c\u811a\u672c\u91cc\u53ea\u6709\u751f\u6210\u56fa\u5b9a\u7684\u683c\u5f0f\uff0czabbix\u5c31\u4f1a\u8ba4\u5b9a\u4ed6\u662f \u81ea\u52a8\u53d1\u73b0\u5b8f\uff09<\/strong><\/span><\/div>\n
\n
\n
<\/div>\n
\"\"<\/div>\n<\/div>\n
<\/div>\n<\/div>\n

\u4e0a\u56fe \u91cc\u7684 mysql.discovery \u5c31\u662f\u83b7\u53d6 \u81ea\u52a8\u53d1\u73b0\u5b8f\u7684 \u811a\u672c\uff0c\u811a\u672c\u662f\u653e\u5728 agent\u7aef\u3002<\/p>\n

\u540e\u9762\u7684 $MYSQLPORT \u4e3a\u6b64\u811a\u672c\u6267\u884c\u65f6\u5e26\u7684\u53c2\u6570\uff0c$MYSQLPORT \u662f\u4e3b\u673a\u5b8f\u3002<\/p>\n

discovery rule \u53f3\u8fb9\u7684 filters \u662f\u8fc7\u6ee4\u89c4\u5219\u7684\u610f\u601d\uff0c\u6bd4\u65b9\u4f60 \u53ea\u60f3\u53d6\u811a\u672c\u91cc\u7684 \u67d0\u67d0 \u81ea\u52a8\u53d1\u73b0\u5b8f\uff0c\u5c31\u53ef\u4ee5\u5728\u91cc\u9762\u8bbe\u7f6e\u3002 \u8fd9\u91cc\u5c31\u53ea\u6709\u4e00\u4e2a\u81ea\u52a8\u53d1\u73b0\u5b8f\uff0c\u6240\u6709\u4e0d\u505a \u914d\u7f6e\u3002<\/p>\n

3.\u521b\u5efa\u76d1\u63a7\u9879\uff08\u76d1\u63a7\u9879\u548c\u76d1\u63a7\u7684\u811a\u672c\u90fd\u662f\u8981\u6539\u7684\uff0c\u56e0\u4e3a\u81ea\u52a8\u53d1\u73b0\u529f\u80fd\uff0c\u81f3\u5c11\u811a\u672c\u4f1a\u591a\u4e86\u4e00\u4e2a\u53c2\u6570\uff09<\/strong><\/span><\/div>\n
\n
\n
<\/div>\n
\"\"<\/div>\n<\/div>\n<\/div>\n
4.\u521b\u5efa \u4e3b\u673a\u5b8f<\/strong><\/span><\/div>\n
\n
\n
<\/div>\n
\"\"<\/div>\n<\/div>\n<\/div>\n

\u70b9\u5f00\u4e3b\u673a\uff0c\u6dfb\u52a0\u4e3b\u673a\u5b8f\uff0c\u5b8f\u540d\u79f0\u662f \u81ea\u52a8\u53d1\u73b0\u811a\u672c\u91cc\u7684 \u53c2\u6570\uff0c \u5b8f\u7684\u503c\u5c31\u662f \u6b64 \u4e3b\u673a\u4e0a\u9700\u8981\u76d1\u63a7\u7684\u7aef\u53e3\u3002<\/p>\n

\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86 \u53ea\u8981\u6539\u52a8\u4e3b\u673a\u5b8f\u7684 \u503c\uff0c\u5c31\u53ef\u4ee5\u6539\u53d8\u4e3b\u673a\u5b8f\u7684\u76d1\u63a7\u9879\u3002<\/p>\n

\u811a\u672c\u5c55\u793a<\/strong><\/div>\n
1.discovery_mysql.sh \u81ea\u52a8\u53d1\u73b0\u7aef\u53e3\u811a\u672c (\u7f51\u4e0a\u6284\u7684\u811a\u672c\u505a\u4e86\u4fee\u6539)<\/strong><\/span><\/div>\n
 res=`echo<\/span> $1<\/span>| sed \"s\/_\/\\n\/g\"<\/span>`;\r\nport=($res<\/span>)\r\nprintf<\/span> '{\\n'<\/span>\r\nprintf<\/span> '\\t\"data\":[\\n'<\/span>\r\nfor<\/span> key in<\/span> ${!port[@]}<\/span>\r\ndo<\/span>\r\n    if<\/span> [[ \"${#port[@]}<\/span>\"<\/span> -gt 1 && \"${key}<\/span>\"<\/span> -ne \"$((${#port[@]}-1)<\/span>)\"<\/span> ]];\r\nthen<\/span>\r\n        printf<\/span> '\\t {\\n'<\/span>\r\n        printf<\/span> \"\\t\\t\\t\\\"{#MYSQLPORT}\\\":\\\"${port[${key}<\/span>]}\\\"},\\n\"<\/span>\r\n   \r\nelse<\/span> [[ \"${key}<\/span>\"<\/span> -eq \"((${#port[@]}<\/span>-1))\"<\/span> ]]\r\n        printf<\/span> '\\t {\\n'<\/span>\r\n        printf<\/span> \"\\t\\t\\t\\\"{#MYSQLPORT}\\\":\\\"${port[${key}<\/span>]}\\\"}\\n\"<\/span>\r\nfi<\/span>\r\ndone<\/span>\r\nprintf<\/span> '\\t ]\\n'<\/span>\r\nprintf<\/span> '}\\n'<\/span>\r\n<\/pre>\n

\u6267\u884c\u5b9e\u4f8b\uff1a \u4ee5_ \u4e3a\u5206\u9694\u7b26\uff0c\u683c\u5f0f\u5316\u8f93\u51fa \u81ea\u52a8\u53d1\u73b0\u5b8f
\n.\/discovery_mysql.sh 3306_3307_3308<\/p>\n

{\r\n              \"data\"<\/span>:[\r\n              {\r\n                                \"{#MYSQLPORT}\"<\/span>:\"3306\"<\/span>},\r\n              {\r\n                                \"{#MYSQLPORT}\"<\/span>:\"3307\"<\/span>},\r\n              {\r\n                                \"{#MYSQLPORT}\"<\/span>:\"3308\"<\/span>}\r\n              ]\r\n}\r\n<\/pre>\n
2.mysql_filestype.sh \u76d1\u63a7mysql\u7684\u4e00\u4e9b\u6302\u8f7d\u76d8<\/strong><\/span><\/div>\n
var=$1<\/span>\r\nMYSQL_PORT=$2<\/span>\r\nMYSQL_NAME=`ps -ef |grep '\/mysql\/app\/bin\/mysqld'<\/span>| grep \"$MYSQL_PORT<\/span>\"<\/span> |grep -v grep | awk -F\" \"<\/span> '{print $11}'<\/span> |awk -F\"\/\"<\/span> '{print $2}'<\/span>` ;\r\nMYSQL_SOCk_DIR=\"\/$MYSQL_NAME<\/span>\/\"<\/span> ;\r\ndf -h \"${MYSQL_SOCk_DIR}<\/span>${var}<\/span>\"<\/span> |grep -v Filesystem |awk -F\" \"<\/span> '{print $5}'<\/span> | awk -F\"%\"<\/span> '{print $1}'<\/span> ;\r\n<\/pre>\n
3.mysql_ping.sh \u76d1\u63a7mysql\u72b6\u6001<\/strong><\/span><\/div>\n
MYSQL_PORT=$1<\/span>;\r\n\r\n[ \"${MYSQL_USER}<\/span>\"<\/span>     = ''<\/span> ] &&  MYSQL_USER=xxx\r\n[ \"${MYSQL_PASSWORD}<\/span>\"<\/span> = ''<\/span> ] &&  MYSQL_PASSWORD=xxxxx\r\n\r\nmysqladmin=\/mysql\/app\/bin\/mysqladmin  ;\r\n\r\nMYSQL_SOCk_DIR=`ps -ef |grep 'mysql'<\/span>| grep \"${MYSQL_PORT}<\/span>\"<\/span> |grep -v 'grep'<\/span> | awk -F\" \"<\/span> '{print $16}'<\/span>`  ;\r\n\r\n${mysqladmin}<\/span>   -u${MYSQL_USER}<\/span> -p${MYSQL_PASSWORD}<\/span>  ${MYSQL_SOCk_DIR}<\/span>  ping   2> \/dev\/null |grep 'alive'<\/span>|wc -l  ;\r\n<\/pre>\n

\u6ce8\u610f: \u8fd9\u8fb9\u8fde\u63a5\u5b9e\u4f8b \u90fd\u662f mysql -u -p -S \/xxx\/mysql.sock<\/p>\n

\u4f46\u662f\u540e\u6765\u53d1\u73b0\u90e8\u5206\u5ba2\u6237\u7aef\u603b\u662f\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u503c\uff0c\u800c\u4e14\u5c31\u662f\u8fd9\u6761 \u8bed\u53e5\u62a5\u9519\uff0c \u611f\u89c9\u662f\u73af\u5883\u53d8\u91cf\u7684\u95ee\u9898<\/p>\n

\u6240\u4ee5\u62ff --socket=\/xxx\/mysql.sock \u6765\u4ee3\u66ff -S\/xxx\/mysql.sock \u3002 \u8fd9\u91cc\u7684\u53d8\u91cf${MYSQL_SOCk_DIR} \u5c31\u662f--socket=\/xxx\/mysql.sock \u3002<\/p>\n

4.mysql_repl.sh mysql\u4e3b\u4ece\u72b6\u6001\u76d1\u63a7<\/strong><\/span><\/div>\n
var=$1<\/span>\r\nMYSQL_PORT=$2<\/span>\r\nMYSQL_SOCk_DIR=`ps -ef |grep 'mysql'<\/span>| grep \"${MYSQL_PORT}<\/span>\"<\/span> |grep -v 'grep'<\/span> | awk -F\" \"<\/span> '{print $16}'<\/span>`  ;\r\nmysql=\/mysql\/app\/bin\/mysql\r\n[ \"${MYSQL_USER}<\/span>\"<\/span>     = ''<\/span> ] &&  MYSQL_USER=xxx\r\n[ \"${MYSQL_PASSWORD}<\/span>\"<\/span> = ''<\/span> ] &&  MYSQL_PASSWORD=xxxx\r\n${mysql}<\/span>  -u${MYSQL_USER}<\/span> -p${MYSQL_PASSWORD}<\/span> ${MYSQL_SOCk_DIR}<\/span> -e \"show slave status\\G;\"<\/span>  2> \/dev\/null |grep \"\\b${var}<\/span>\\b\"<\/span>|awk -F\" \"<\/span> '{print $2}'<\/span>   ;\r\n<\/pre>\n

\u4e3b\u4ece\u72b6\u6001 \u6709\u51e0\u4e2a\u7aef\u53e3\u7684\u503c\u662f \u7a7a\u7684\uff0c \u8fd9\u4e2a\u5f97 \u5177\u4f53\u95ee\u9898\u5177\u4f53\u8bbe\u7f6e<\/p>\n

5.mysql_status2 mysql\u6027\u80fd\u4e4b\u7c7b\u7684\u76d1\u63a7<\/strong><\/span><\/div>\n
mysql=\/mysql\/app\/bin\/mysql\r\nvar=$1<\/span>\r\nMYSQL_PORT=$2<\/span>\r\n[ \"${MYSQL_USER}<\/span>\"<\/span>     = ''<\/span> ] &&  MYSQL_USER=xxx\r\n[ \"${MYSQL_PASSWORD}<\/span>\"<\/span> = ''<\/span> ] &&  MYSQL_PASSWORD=xxxxx\r\n\r\nMYSQL_SOCk_DIR=`ps -ef |grep 'mysql'<\/span>| grep \"${MYSQL_PORT}<\/span>\"<\/span> |grep -v 'grep'<\/span> | awk -F\" \"<\/span> '{print $16}'<\/span>`  ;\r\n\r\n${mysql}<\/span>  -u${MYSQL_USER}<\/span> -p${MYSQL_PASSWORD}<\/span> ${MYSQL_SOCk_DIR}<\/span> -e \"show global status;\"<\/span> 2> \/dev\/null |grep -v Variable_name|grep \"\\b${var}<\/span>\\b\"<\/span>|awk '{print $2}'<\/span>   ;\r\n<\/pre>\n

\u53d1\u73b0\u6709\u4e9b\u76d1\u63a7\u9879 \u6216\u56e0\u4e3a\u6570\u5b57\u8fc7\u957f\u800c\u65e0\u6cd5\u663e\u793a\uff0c\u5f85\u89e3\u51b3<\/p>\n

6.mysql_version.sh mysql\u7248\u672c\u76d1\u63a7<\/strong><\/span><\/div>\n
MYSQL_PORT=$1<\/span>;\r\n\r\nMYSQL_SOCk_DIR=`ps -ef |grep 'mysql'<\/span>| grep \"${MYSQL_PORT}<\/span>\"<\/span> |grep -v 'grep'<\/span> | awk -F\" \"<\/span> '{print $16}'<\/span>`  ;\r\n\r\nmysql=\/mysql\/app\/bin\/mysql ;\r\n\r\n[ \"${MYSQL_USER}<\/span>\"<\/span>     = ''<\/span> ] &&  MYSQL_USER=xxx\r\n[ \"${MYSQL_PASSWORD}<\/span>\"<\/span> = ''<\/span> ] &&  MYSQL_PASSWORD=xxxx\r\n\r\n${mysql}<\/span>  -u${MYSQL_USER}<\/span> -p${MYSQL_PASSWORD}<\/span> ${MYSQL_SOCk_DIR}<\/span>  -e \"select version();\"<\/span> 2> \/dev\/null |awk 'END {print}'<\/span>\r\n<\/pre>\n
7.mysql_status_many.conf \u751f\u6210\u53ef\u7528\u952e \u914d\u7f6e\u6587\u4ef6<\/strong><\/span><\/div>\n
UserParameter=mysql.discovery[*],\/patrol\/zabbix<\/span>\/bin\/duoshili<\/span>_discovery\/discovery_mysql.sh $1  \r\nUserParameter=mysql.status_many[*],\/patrol\/zabbix<\/span>\/bin\/duoshili<\/span>_discovery\/mysql_status2 $1 $2\r\nUserParameter=mysql.ping_many[*],\/patrol\/zabbix<\/span>\/bin\/duoshili<\/span>_discovery\/mysql_ping.sh $1\r\nUserParameter=mysql.version_many[*],\/patrol\/zabbix<\/span>\/bin\/duoshili<\/span>_discovery\/mysql_version.sh $1\r\nUserParameter=mysqlcheck_repl[*],\/patrol\/zabbix<\/span>\/bin\/duoshili<\/span>_discovery\/mysql_repl.sh $1 $2\r\nUserParameter=mysql.filestyle_many[*],\/patrol\/zabbix<\/span>\/bin\/duoshili<\/span>_discovery\/mysql_filestype<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u4e3b\u673a\u5b8f\u683c\u5f0f\uff1a$MACRO \uff08\u76f4\u63a5\u5c31\u53ef\u4ee5 \u586b \u503c\uff09 \u81ea\u52a8\u53d1\u73b0\u5b8f\uff1a#MACRO \uff08\u9700\u8981\u914d\u5408 \u521b\u5efa\u7684 key \u53d6\u503c\uff09 […]<\/p>\n","protected":false},"author":63,"featured_media":145775,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-145766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/145766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=145766"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/145766\/revisions"}],"predecessor-version":[{"id":145901,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/145766\/revisions\/145901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/145775"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=145766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=145766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=145766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}