zabbix添加监控Mysql

起因:zabbix自带的mysql监控模板直接使用会显示“不支持的”因为key的值是通过Mysql用户查看"show global status"信息或者用mysqladmin命令查看status或extended-status的信息而取的值。

 

如:

mysql -uroot -p -e "show global status" |grep  "Uptime" |head -1 |awk '{print $2}'
mysqladmin -uroot -p -h status | cut -f2 -d":"|cut -f1 -d"T"

 

方式:通过脚本结合zabbix模板实现监控

vim /usr/local/zabbix/scripts/mysql_check.sh    #编写脚本

#!/bin/bash

MYSQL_CONN="/usr/bin/mysqladmin"

注:原本服务器用mysqladmin命令格式为 mysqladmin -uxxx -pxxx -hxxx ,但如果将mysql用户密码写入脚本系统会
报一个Warning: Using a password on the command line interface can be insecure 的警告影响到zabbix服务端的接收返回值。
所以这里用在my.cfg文件中添加用户密码的方式避免系统警告
 vim /etc/my.cfg
 [mysqladmin]
 user=zabbix
 password=zabbix


# 参数是否正确
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_agentd.conf

打开UnsafeUserParameters=1(默认为0)  #开启自定义监控项

UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/usr/local/zabbix/scripts/mysql_check.sh $1
UserParameter=mysql.ping,mysqladmin ping | grep -c alive

重启zabbix_agentd

 

在服务端测试zabbix_get -s IP -k mysql.ping,mysql.status[Bytes_sent]等

[root@xx ~]# zabbix_get -s 192.168.1.1 -k mysql.ping
1
[root@xx ~]# zabbix_get -s 192.168.1.1 -k mysql.status[Bytes_sent]
356

 

在页面主机连接模板Template App MySQL

 

 

posted @ 2018-03-08 17:34  叮伱格斐呃  阅读(178)  评论(0编辑  收藏  举报
Live2D