zabbix 3.0.2监控mysql

内网有一台mysql服务器,版本是5.7.14mysql

关于这个版本安装,有兴趣能够参考sql

http://xiao987334176.blog.51cto.com/2202382/1783509shell


zabbix自带有一个模板Template App MySQL,用来监控mysql的vim

可是不能直接使用,不然会由于没有Key,致使获取不到数据。安全


下面介绍详细步骤。服务器

首先在mysql服务器安装zabbix-agent,请参考ide

http://xiao987334176.blog.51cto.com/2202382/1768281测试

最下面一部分。ui


###################################################################################spa

讲解一个比较重要的问题。

在shell里面,直接运行mysql相关命令,指定密码时,就会有一个提示

好比: mysql -u zabbix -p123456 运行以后

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


这个提示在mysql 5.5以后会出现。

并且,这个提示,会被zabbix-servre捕捉到,在zabbix-server.log日志中会出现

24123:20160826:101433.609 error reason for "110:mysql.status[Bytes_sent]" changed: Received value [mysqladmin: [Warning] Using a password on the command line interface can be insecure.8991074] is not suitable for value type [Numeric (float)]

提示参数不符合

在zabbix-server服务器上面,使用zabbix-get命令时

[root@localhost ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.110 -p10050 -k mysql.status[Uptime]

mysqladmin: [Warning] Using a password on the command line interface can be insecure.

57577


###################################################################################


那么为了解决这个问题,必须无密码登陆才能够,为了安全起见,限定只能在localhost登陆

先建立一个zabbix用户(建立用户,密码不能为空,不然报错)

grant all PRIVILEGES on *.* to zabbix@'localhost' identified by '123456';

设置密码为root

update mysql.user set authentication_string=password('root') where user='zabbix' and Host = 'localhost';

刷新权限

flush privileges;

退出

exit;


测试无密码登陆

[root@localhost opt]# mysql -u zabbix

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 930

Server version: 5.7.14 Source distribution


Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> exit

Bye


发现一个很奇怪的现象,把密码设置和root同样,就能够无密码登陆了


mysql> select Host,User,authentication_string from mysql.user;

+-----------+-----------+-------------------------------------------+

| Host      | User      | authentication_string                     |

+-----------+-----------+-------------------------------------------+

| localhost | root      | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

| localhost | zabbix    | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

+-----------+-----------+-------------------------------------------+

3 rows in set (0.00 sec)



进入Mysql服务器的zabbix-agent目录

cd /usr/local/zabbix-agent/

mkdir alertscripts

编辑脚本

vim checkmysql.sh

内容以下:


#!/bin/sh

#MYSQL_SOCK="/data/3306/mysqld.sock"

#MYSQL_PWD=`cat /usr/local/zabbix-agent/alertscripts/.mysqlpassword`

ARGS=1

if [ $# -ne "$ARGS" ];then

echo "Please input one arguement:"

fi

case $1 in

Uptime)

result=`mysqladmin -uzabbix status|cut -f2 -d":"|cut -f1 -d"T"`

echo $result

;;

Com_update)

result=`mysqladmin -uzabbix extended-status |grep -w "Com_update"|cut -d"|" -f3`

echo $result

;;

Slow_queries)

result=`mysqladmin -uzabbix status |cut -f5 -d":"|cut -f1 -d"O"`

echo $result

;;

Com_select)

result=`mysqladmin -uzabbix extended-status |grep -w "Com_select"|cut -d"|" -f3`

echo $result

;;

Com_rollback)

result=`mysqladmin -uzabbix extended-status |grep -w "Com_rollback"|cut -d"|" -f3`

echo $result

;;

Questions)

result=`mysqladmin -uzabbix status|cut -f4 -d":"|cut -f1 -d"S"`

echo $result

;;

Com_insert)

result=`mysqladmin -uzabbix extended-status |grep -w "Com_insert"|cut -d"|" -f3`

echo $result

;;

Com_delete)

result=`mysqladmin -uzabbix extended-status |grep -w "Com_delete"|cut -d"|" -f3`

echo $result

;;

Com_commit)

result=`mysqladmin -uzabbix extended-status |grep -w "Com_commit"|cut -d"|" -f3`

echo $result

;;

Bytes_sent)

result=`mysqladmin -uzabbix extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`

echo $result

;;

Bytes_received)

result=`mysqladmin -uzabbix extended-status |grep -w "Bytes_received" |cut -d"|" -f3`

echo $result

;;

Com_begin)

result=`mysqladmin -uzabbix extended-status |grep -w "Com_begin"|cut -d"|" -f3`

echo $result

;;

 

*)

echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"

;;

esac


设置权限

chmod 755 checkmysql.sh

chown zabbix:zabbix -R /usr/local/zabbix-agent/

编辑配置文件


vim /usr/local/zabbix-agent/etc/zabbix_agentd.conf

增长紫色部分,内容以下:


LogFile=/usr/local/zabbix-agent/logs/zabbix_agentd.log

###zabbix 服务端地址

Server=192.168.1.109

##agent服务监听地址,也就是本机地址

#ListenIP=192.168.1.105

###ServerActive=192.168.1.110

##zabbix-server端主机地址(zabbix server)

Hostname=zabbix server


UserParameter=mysql.version,mysql -V

UserParameter=mysql.status[*],/usr/local/zabbix-agent/alertscripts/checkmysql.sh $1

UserParameter=mysql.ping,mysqladmin -uzabbix ping | grep -c alive


重启zabbix_agentd

/etc/init.d/zabbix_agentd restart


在zabbix-server服务器上面检查mysql

[root@localhost alertscripts]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.110 -p10050 -k mysql.status[Uptime]

9479

[root@localhost alertscripts]#


若是能直接返回数字,没有多余的字符,就说明成功了。


在zabbix-server添加一台主机

wKiom1e_1PXyeowlAABaU5gX3nw180.png-wh_50

添加模板的时候,选择Template App MySQL和Template OS Linux 

wKioL1e_1QDA7LctAABF08HqVyk658.png-wh_50

等待10分钟,图像就会出来了。

wKiom1e_1Y3RRp_hAAEBwRFRqys035.png-wh_50

wKioL1e_1ZrAmlj_AAD3JVYNG0M361.png-wh_50