zabbix监控mysql数据库状态

编写check_mysql.sh脚本
用于获取mysql性能指标数据,你需要修改相应的数据库信息
vim /usr/local/zabbix/share/zabbix/alertscripts/check_mysql.sh

#!/bin/bash

-------------------------------------------------------------------------------

FileName: check_mysql.sh

Revision: 1.0

Date: 2017/04/24

-------------------------------------------------------------------------------

用户名

MYSQL_USER='root'

密码

MYSQL_PWD='BiJX!Rrcr2G1yAO8'

主机地址/IP

MYSQL_HOST='localhost'

端口

MYSQL_PORT='3306'

数据连接

MYSQL_CONN="/usr/local/mariadb/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"

参数是否正确

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命令的路径 /usr/local/mariadb/bin/mysqladmin
whereis mysqladmin 可以搜一下命令在哪里

● Com_update:mysql执行的更新个数
● Com_select:mysql执行的查询个数
● Com_insert:mysql执行插入的个数
● Com_delete:执行删除的个数
● Com_rollback:执行回滚的操作个数
● Bytes_received:接受的字节数
● Bytes_sent:发送的字节数
● Slow_queries:慢查询语句的个数

chmod a+x !$ 添加脚本可执行权限

修改zabbix_agentd.conf 增加自定义key,在最后一行增加如下:
vim /usr/local/zabbix/etc/zabbix_agentd.conf

获取mysql版本

UserParameter=mysql.version,mysql -V

获取mysql性能指标,这个是上面定义好的脚本,注意填写对脚本路径

UserParameter=mysql.status[*],/usr/local/zabbix/share/zabbix/alertscripts/check_mysql.sh $1

获取mysql运行状态

UserParameter=mysql.ping,mysqladmin -uroot -pBiJX!Rrcr2G1yAO8 -P3306 -h127.0.0.1 ping | grep -c alive

修改mysql配置文件/etc/my.cnf(写入账号密码,否则取值需要交互)

[client]
host = "localhost"
user = "root"
password = "BiJX!Rrcr2G1yAO8"
port = 3306

重启客户端
service zabbix_agentd restart

在zabbix的web界面添加监控项并更新,等待出图

Link MySQL模板
模板是zabbix系统提供的,进入zabbix web后台,configuration-->hosts-->点击你的主机name-->选择template选项卡,选择模板“Template App MySQL”,最后点击update即可
常见错误解决思路
如果发现监控没有数据,请排查如下问题

  1. zabbix客户端是否重启
  2. 脚本是否有执行权限
  3. 数据库是否有权限
  4. 环境变量是否有问题
  5. 请看zabbix item列,鼠标移至红色叉上,有错误提示。
  6. 如果数据库密码保存在脚本中,会导致监控没有数据会不断的报错为Warning: Using a password on the command line interface can be insecure.需要将帐号密码等配置添加到my.cnf中。

由于上面没有加入mysql并发数的监控,现在添加并发数监控
编辑脚本文件 vim /usr/local/zabbix/share/zabbix/alertscripts/check_mysql.sh
增加取值项
Threads_connected)
result=${MYSQL_CONN} extended-status |grep -w "Threads_connected"|cut -d"|" -f3
echo $result
;;

并修改最后
*)
eche "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Threads_connected)"
;;

重启客户端,在web界面添加监控项

name随便填
key值要跟之前格式相同,可以对照脚本辨别参数

添加完以后:

此时可以在服务端做如下验证,如果出值正常等待一段时间即可
/usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -p10050 -k mysql.status[Threads_connected]

http://note.youdao.com/noteshare?id=09e656008bc7ef5b05dd5fd0cd7a00bc&sub=D59A4074D5F040D296B59219153CDF80 我的有道笔记

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章