zabbix3.4监控mysql5.7

zabbix3.4监控mysql5.7

zabbix_agent

尝试使用zabbix3.4监控mysql5.7,首先需要在mysql所在服务器,安装agent,安装方法很简单:

1
yum -y install zabbix-agent

安装完成之后在本地服务器下会有zabbix-agent的配置相关生成(本地测试用的centos7,生成目录在/etc/zabbix下)

配置MySQL

首先,为了避免接下来可能出现的把数据库账号密码写死在命令行中的情况,可以先在MySQL配置文件中配置一下,/etc/my.cnf文件中新增配置:

1
2
3
[mysqladmin] 
user=zabbix
password=11111

这样在执行/usr/bin/mysqladmin -uuser -ppwd -Pport -hhost ping时候就不用加上用户名密码了,由于zabbix-agent也是安装在本机,所以该命令在配置完mysql账密之后可以简化为mysqladmin ping

配置zabbix-agent

zabbix_agentd.conf

配置zabbix-agent的文件是/etc/zabbix/zabbix_agentd.conf,所有需要的修改都在这里面执行,列一下可能用到的修改项:

1
2
3
4
5
Server=192.168.85.132(大约97行)
ServerActive=192.168.85.132(大约138行)
Hostname=Zabbix server(大约149行)
Include=/etc/zabbix/zabbix_agentd.d/*.conf(大约269行(如果是注释状态,释放即可))
UnsafeUserParameters=1(大约288行,默认是0,修改为1)

userparameter_mysql.conf

在上步Include=/etc/zabbix/zabbix_agentd.d/*.conf释放之后,zabbix-agent可以从指定的文件夹(zabbix_agentd.d)中读取配置,该文件夹中默认就有userparameter_mysql.conf文件,里面就是zabbix用到的监控的item(监控项),每个监控项写一行,创建监控项格式:

1
2
3
UserParameter=<key>,<shell command>
或者
UserParameter=<key>,<script dir>

示例:

1
2
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/etc/zabbix/script/mysql/chk_mysql.sh $1

第一条item,key是mysql.version,意思就是这是监控MySQL版本的一个item,后面的mysql -V是具体执行的shell命令,通过执行该命令获取MySQL的版本;
第二个item,key是mysql.status[*],意思是该监控项监控的是,mysql的状态,通过执行/etc/zabbix/script/mysql/chk_mysql.shshell 脚本来获取MySQL的各状态。

附上我的userparameter_mysql.conf配置(网上一堆这样的配置,我也是参考的)

1
2
3
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/etc/zabbix/script/mysql/chk_mysql.sh $1
UserParameter=mysql.ping,mysqladmin ping | grep -c alive

chk_mysql.sh

上步,配置项中配置的是脚本文件的,就是将获取监控项的各个脚本写在一个文件中,避免userparameter_mysql.conf文件太杂太乱,如上步配置的,获取MySQL状态的脚本是/etc/zabbix/script/mysql/chk_mysql.sh默认安装的zabbix是没有该文件的,需要手动创建
mkdir -p /etc/zabbix/script/mysql,
cd /etc/zabbix/script/mysql,
vim chk_mysql.sh
完成创建脚本,编辑,附上我的脚本(来自互联网):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
# -------------------------------------------------------------------------------
# FileName: check_mysql.sh
# Revision: 1.0
# Date: 2018/06/20
# Author: kyle
# Email:
# Website:
# Description:
# Notes: ~
# -------------------------------------------------------------------------------
# Copyright:
# License: GPL


# 主机地址/IP
MYSQL_HOST='192.168.85.132'

# 端口
MYSQL_PORT='3306'

# 数据连接
MYSQL_CONN="/usr/bin/mysqladmin"

# 参数是否正确
if [ $# -ne "1" ];then
echo "arg error!"
fi

# 获取数据
case $1 in
Uptime)
result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |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

脚本其实很简单,通过mysqladmin获取MySQL自带的计数器数值即可~

zabbix-server

进入zabbix web页面,Configuration-Hosts,选择主机,打开主机的配置页面,选择Templates(模板),在Link new templates栏输入MySQL,在跳转出的模板中选择Template DB MySQL,点击add,完成模板配置,然后回到服务器,重启agent端:

1
systemctl restart zabbix-agent

测试数据获取:

1
zabbix_get -s yourHostIp -k mysql.status[Com_insert]

可以成功获取到数据。

图形查看

返回zabbix web端,在Monitoring-Graphs下可以看见配置完成的监控项的实时图形。

可能遇到的问题

以下是可能会遇见的问题:

Using a password on the command line interface can be insecure.

首先是这个[warning],这是因为在命令行中写死了数据库的用户名和密码,所以有这个不安全的提示;解决办法,只需要将数据库的账密添加到/etc/my.cnf中,并且在命令行中去掉账密即可。

2539” of type “string”: cannot convert value to numeric type

这个问题,很明显,是字符格式的问题,shell脚本获取的监控项数据,设置的为string类型,但是选择的zabbix绘图要求的item的数据类型是numeric类型,只要转换下格式即可;解决办法,Configuration-Templtes,点击items,跳转到item页面,选择item,点击开,编辑类型Type of information;具体的类型可以先通过Configuration-Templtes-Graphs查看。

文章目录
  1. zabbix_agent
  2. 配置MySQL
  3. 配置zabbix-agent
    1. zabbix_agentd.conf
    2. userparameter_mysql.conf
    3. chk_mysql.sh
  4. zabbix-server
    1. 图形查看
  5. 可能遇到的问题
    1. Using a password on the command line interface can be insecure.
    2. 2539” of type “string”: cannot convert value to numeric type
|