zoukankan      html  css  js  c++  java
  • Zabbix应用三:Zabbix监控MySQL

    利用Zabbix自带模版监控(Template App MySQL)MySQL服务

    一、添加模版:

    进入zabbix页面,找到'配置'->'主机'->'模版',点击'链接指示器'后面的'选择',如下图:

    在弹出的模版列表中选择'Template App MySQL',点击'添加'后,点击'更新'。

    现在监控MySQL的模版已经有了,但是还没没有数据,我们需要将从agentd端搜集到的数据渲染到模版中,才能显示。

    二、采集agentd端数据:

    在agentd端编写采集MySQL数据的脚本,内容如下:

    #!/bin/bash
    
    MYSQL_HOST='127.0.0.1'
    MYSQL_PORT='3306'
    MYSQL_CONN="mysqladmin -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

    该好脚本中,没有配置连接MySQL的帐号信息,是因为如果在该脚本中设置的话,在使用myslqadmin收集mysql信息时,会提示:

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

    这样会导致采集不到数据。

    为解决这个问题,我们需要将连接mysql的帐号信息写到mysql的配置文件中:

    vim /etc/my.cnf,添加如下一段:

    [mysqladmin]
    
    user=username
    password="password"

    然后重启mysql即可。

    三、在agentd端配置中添加UserParameter:

    万事俱备后,我们还需要在agentd中配置我们自定义的参数:

    vim zabbix_agentd.conf,添加如下信息:

    UserParameter=mysql.status[*],/shell/zabbix_agent/mysql_status.sh $1
    UserParameter=mysql.ping,mysqladmin -P 3306 ping | grep -c alive

    第一行是通过执行我们上面自己编写的脚本来收集mysql数据信息,第二行是使用mysql的ping工具来检测mysql是否运行

    好,到这里就配置完毕了,重启agentd端zabbix服务即可。

    zabbix效果图:

    在zabbix主页面,找到'图形':

    选择监控的主机名称,会发现多了MySQL的两项监控:

     以上就是配置MySQL监控的全部

  • 相关阅读:
    Spring总结(一)
    Java中NumberUtil工具类(持续更新中)
    Java中DateUtil工具类(持续更新中)
    解决poi导出报4000最大样式错误:java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook错误
    MySQL 5.7 zip 安装
    Java中String类方法
    Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [applicationContext-common.xml]: Cannot resolve reference to bean 'sqlSessionFactory'
    Ajax中async与cache参数
    打印类对象自动调用toString方法
    JSP基本语法小结
  • 原文地址:https://www.cnblogs.com/ahaii/p/6942357.html
Copyright © 2011-2022 走看看