- 环境 CentOS 7.6 zabbix-agent 4.0.14 MySQL 5.7
- 创建监控MySQL用户 用root用户登录MySQL,创建授权用户信息。
<span>#grant</span> <span>usage</span> <span>on</span> . <span>to</span> ‘<span>jiankong</span>’@’<span>mysql</span>服务器<span>ip</span>’ identified by ‘xxxxxx’;
<span>#flush</span> <span>privileges</span>;
- <span>#flush</span> <span>privileges</span>; 这里直接使用root用户测试。
- agent端配置 zabbix-agent没有安装,使用yum install -y zabbix-agent命令安装。 修改zabbix配置默认的userparameter_mysql.conf文件 目录:/etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 注释掉默认的mysql status配置项,增加监控脚本文件。 grep -Ev '^$|^#' /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
<span>UserParameter</span>=mysql.status[*],/etc/zabbix/scripts/chk_mysql.sh <span>$1</span>
<span>UserParameter</span>=mysql.ping,mysqladmin -uroot -pxxxxxx -h <span>'127.0.0.1'</span> ping <span>2</span>> /dev/null | grep -c alive
<span>UserParameter</span>=mysql.version,mysql -V
/etc/zabbix/scripts/chk_mysql.sh数据库监控脚本。
代码语言:javascript复制<span>#!/bin/bash</span>
<span># -------------------------------------------------------------------------------</span>
<span># FileName: check_mysql.sh</span>
<span># Revision: 1.0</span>
<span># Date: 2020/04/12</span>
<span># Author: Joey King</span>
<span># Email:</span>
<span># Website:</span>
<span># Description: Zabbix Mysql</span>
<span># Notes: None</span>
<span># -------------------------------------------------------------------------------</span>
<span># User</span>
MYSQL_USER=<span>'root'</span>
<span># PASSWD</span>
MYSQL_PWD=<span>'xxxxxx'</span>
<span># HOST IP</span>
MYSQL_HOST=<span>'127.0.0.1'</span>
<span>#MYSQL_HOST='10.10.10.10'</span>
<span># PORT</span>
MYSQL_PORT=<span>'3306'</span>
<span># CONN</span>
MYSQL_CONN=<span>"/usr/bin/mysqladmin -u<span>${MYSQL_USER}</span> -p<span>${MYSQL_PWD}</span> -h<span>${MYSQL_HOST}</span> -P<span>${MYSQL_PORT}</span>"</span>
<span># CHK PARAMETERS</span>
<span>if</span> [ <span>$#</span> <span>-ne</span> <span>"1"</span> ];<span>then</span>
<span>echo</span> <span>"arg error!"</span>
<span>fi</span>
<span># COLLECTION DATA</span>
<span>case</span> <span>$1</span> <span>in</span>
Uptime)
result=`<span>${MYSQL_CONN}</span> status 2> /dev/null|cut <span>-f</span>2 <span>-d</span><span>":"</span>|cut <span>-f</span>1 <span>-d</span><span>"T"</span>`
<span>echo</span> <span>$result</span>
;;
Com_update)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Com_update"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Slow_queries)
result=`<span>${MYSQL_CONN}</span> status 2> /dev/null|cut <span>-f</span>5 <span>-d</span><span>":"</span>|cut <span>-f</span>1 <span>-d</span><span>"O"</span>`
<span>echo</span> <span>$result</span>
;;
Com_select)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Com_select"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Com_rollback)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Com_rollback"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Questions)
result=`<span>${MYSQL_CONN}</span> status 2> /dev/null|cut <span>-f</span>4 <span>-d</span><span>":"</span>|cut <span>-f</span>1 <span>-d</span><span>"S"</span>`
<span>echo</span> <span>$result</span>
;;
Com_insert)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Com_insert"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Com_delete)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Com_delete"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Com_commit)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Com_commit"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Bytes_sent)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Bytes_sent"</span> |cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Bytes_received)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Bytes_received"</span> |cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Com_begin)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Com_begin"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Threads_connected)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Threads_connected"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
Threads_running)
result=`<span>${MYSQL_CONN}</span> extended-status 2> /dev/null|grep -w <span>"Threads_running"</span>|cut <span>-d</span><span>"|"</span> <span>-f</span>3`
<span>echo</span> <span>$result</span>
;;
*)
<span>echo</span> <span>"Usage:<span>$0</span>(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"</span>
;;
<span>esac</span>
这里,遇到个坑,花了近2天时间。监控脚本配置后,在zabbix-server测试能否从agent端获取到数据,用zabbix_get测试有返回数据。
代码语言:javascript复制<span>zabbix_get</span> <span>-s10</span><span>.10</span><span>.10</span><span>.10</span> <span>-p</span> 10050 <span>-k</span> "<span>mysql</span><span>.status</span><span>[Threads_connected]</span>"
- 但返回结果是:
上图中将Warning信息也显示出来,是由于监控脚本文件中有配置密码信息,所以给显示出来,但其实是有问题的,zabbix-server端用zabbix-get获取信息后,读取的返回结果在zabbix Web页面显示,是读取的第一行返回信息,并非读取返回第二行的真实信息。当时认为是个Warning信息,没在意(在这认栽了)。 在zabbix-Web监控项中显示type "string" is not suitable for value type "Numeric (unsigned)"。
接下来就是解决问题, 比较简单,就是将Warning信息不显示出来,扔到垃圾桶。 修改监控脚本,脚本中加入 " 2> /dev/null "
zabbix-agent 服务重启解决,service zabbix-agent restart 再次 zabbix-server 端用 zabbix-get 测试获取返回结果:
- zabbix自定义Mysql监控项 以上监控脚本中关于Mysql的连接数和并发数情况,即监控脚本中Threads_connected 和 Threads_running 的信息。在 zabbix Mysql 监控模板中是没有这两块的监控信息。 脚本中增加 Threads_connected 和 Threads_running 的信息,详见上面监控脚本。接下就是在 zabbix 数据库监控默认模板 Template DB MySQL 上创建配置监控项、创建图形、创建触发器。 4.1 创建监控项
4.2 创建图形
4.3 创建触发器给Threads_connected连接数创建触发器。