Zabbix3.4使用Template监控mysql

一、Zabbix web页面操作

1.1 为主机增加mysql template

1.2 查看主机监控项状态

点击主机 - 应用集 - 监控项

查看信息,如果有报错需要解决

 把该模板Template App MySQL Link到相关的主机上面,发现Item的Status是不可用的,

因为key的值是通过Mysql用户查看"showglobal status"信息或者用mysqladmin命令查看status或extended-status的信息而取的值。

注:这里我是已经设置好了的

1.3 查看是否获取到数据
检测中 - 最新数据

二、Zabbix agent配置

2.1 在被监控的MySQL里面建立一个帐户,用于Zabbix Agent登录获取MySQL状态

GRANT USAGE ON *.* TO 'zabbix'@'123456' IDENTIFIED BY '密码';  
FLUSH PRIVILEGES; 

2.2 创建检测脚本
打开zabbix agent配置目录 /etc/zabbix
chk_mysql.sh

[root@test161 ~]# cat /etc/zabbix/chk_mysql.sh
#!/bin/bash
# -------------------------------------------------------------------------------
# FileName:    check_mysql.sh
# Revision:    1.0
# Date:        2018/02/28
# Author:      jiangzuxing

# 用户名
MYSQL_USER='zabbix'
 
# 密码
MYSQL_PWD='123456'
 
# 主机地址/IP
MYSQL_HOST='127.0.0.1'
 
# 端口
MYSQL_PORT='3306'
 
# 数据连接
#MYSQL_CONN="mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
MYSQL_CONN="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

注意:脚本的zabbix用户运行权限

[root@test161 ~]# chmod +x /etc/zabbix/chk_mysql.sh
[root@test161 ~]# ll /etc/zabbix/chk_mysql.sh
-rwxrwxr-x 1 zabbix zabbix 2693 3月   5 15:10 /etc/zabbix/chk_mysql.sh

2.2 修改监控项检测配置

将原先的注释了,添加新的配置 

[root@test161 etc]# vim zabbix_agentd.conf
......
# UserParameter
=mysql.ping,mysqladmin -uzabbix ping|grep -c alive UserParameter=mysql.status[*],/etc/zabbix/chk_mysql.sh $1 #UserParameter=mysql.ping,mysqladmin -uroot -p123456 -P3306 -h127.0.0.1 ping | grep -c alive UserParameter=mysql.ping,mysqladmin ping | grep -c alive UserParameter=mysql.version,mysql -V
.....

2.3 关闭SELinux和firewall
2.4 重启agent

[root@test161 etc]# pkill zabbix
[root@test161 etc]# ps -ef|grep zabbix
root     13625 12393  0 15:10 pts/1    00:00:00 vim /etc/zabbix/chk_mysql.sh
root     13713 15623  0 15:11 pts/3    00:00:00 grep zabbix
[root@test161 etc]# /usr/local/zabbix/sbin/zabbix_agentd

2.5 如果还有问题,创建mysql连接文件

vim /etc/zabbix/zabbix_agentd.d/.my.cnf

[client]
user = zabbix
password = 123456

三、zabbix测试mysql

在服务端连上客户端设置的键值,测试是否生效

[zabbix@test160 bin]$ /usr/local/zabbix/bin/zabbix_get -s 192.168.1.161 -k mysql.status[Uptime]
1516

 

 

配置过程出现的问题

1、在WEB监控项状态报错:
Value "mysqladmin: [Warning] Using a password on the command line interface can be insecure.
1" of type "string" is not suitable for value type "Numeric (unsigned)"

因为在zabbix_agentd.conf使用mysql密码会提示明文密码

解决:

# vi /etc/my.cnf

[client]

user = zabbix

host = localhost

password = 123456

或者 [mysqladmin]

host=localhost

user=zabbix

password=123456

socket=/data/mysql_data/mysql.sock

 

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

 2、zabbix 监控mysql down的时候 不报警

使用zabbix模板Template App MySQL 的监控项mysql.ping监控MySQL是否挂了

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

实际上呢?能返回1但是返回不了0,因为mysqladmin命令会先报错信息出来,这样zabbix-server调用的时候返回的不是纯粹数字,而是字符串+数字,zabbix-server的触发器里面识别不。

解决:

UserParameter=mysql.ping,mysqladmin  ping 2> /dev/null | grep -c alive 

或者sudo netstat -tunlp|grep 3306|wc -l

多种方式实现,总之把mysqladmin 这玩意儿报错信息忽略掉就可以了

参考:http://www.bubuko.com/infodetail-1870346.html

posted @ 2018-03-05 16:20  Gringer  阅读(339)  评论(0)    收藏  举报