老谢博客

  • 首页
  • WordPress
  • 网络技术
  • 乱七八糟
  • 运维技术
  • 给我留言
  • 关于老谢

Zabbix 3.2 agentd使用自带模板监控MySQL性能

分类:运维技术日期:2018-06-11 - 20:58:49作者:老谢

MySQL基础监控

  通过SHOW STATUS 可以提供服务器状态信息,也可以使用mysqladmin extendedstatus 命令获得。

以下几个参数对Myisam 和Innodb 存储引擎都计数:

  • Com_select 执行select 操作的次数,一次查询只累加1
  • Com_insert 执行insert 操作的次数,对于批量插入的insert 操作,只累加一次
  • Com_update 执行update 操作的次数
  • Com_delete 执行delete 操作的次数

以下几个参数是针对Innodb 存储引擎计数的:

  • Innodb_rows_read select 查询返回的行数
  • Innodb_rows_inserted 执行Insert 操作插入的行数
  • Innodb_rows_updated 执行update 操作更新的行数
  • Innodb_rows_deleted 执行delete 操作删除的行数

  通过以上几个参数,可以很容易的了解当前数据库的应用是以插入更新为主还是以查询操作为主,以及各种类型的SQL 大致的执行比例是多少。对于更新操作的计数,是对执行次数的计数,不论提交还是回滚都会累加。对于事务型的应用,通过Com_commit 和Com_rollback 可以了解事务提交和回滚的情况,对于回滚操作非常频繁的数据库,可能应用编写存在问题。

另外还有几个参数可以了解数据库的基本信息:

  • Connections 试图连接Mysql 服务器的次数
  • Uptime 服务器工作时间
  • Slow_queries 慢查询的次数

创建监控脚本
cd /usr/local/zabbix/share/scripts
vim chk_mysql.sh
 
#!/bin/bash
#check mysql_status
MYSQL_USER='root'
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='3306'
MYSQL_PWD='root'
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
if [ $# -ne "1" ];then
    echo "error error!"
fi
case $1 in
    Uptime)
        result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`              #mysql启动时间累加,mysql工作时间
        echo $result
        ;;
    Com_update)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`   #执行update 操作的次数
        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`  #执行select 操作的次数,一次查询只累加1
        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`  #执行insert 操作的次数,对于批量插入的insert 操作,只累加一次;
                echo $result
                ;;
    Com_delete)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` #执行delete 操作的次数
                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
                ;;
    Innodb_rows_read)
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_read"|cut -d"|" -f3`    #Innodb中select 查询返回的行数
                echo $result
             ;;
    Innodb_rows_inserted)
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_inserted"|cut -d"|" -f3` #执行Insert 操作插入的行数
                echo $result
        ;;
    Innodb_rows_updated)
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_updated"|cut -d"|" -f3` #执行update 操作更新的行数
                echo $result
        ;;
    Innodb_rows_deleted)
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_deleted"|cut -d"|" -f3` #执行update 操作更新的行数
                echo $result
        ;;
    Qcache_free_memory)
        result=`${MYSQL_CONN} extended-status |grep -w "Qcache_free_memory"|cut -d"|" -f3`  #缓存中的空闲内存
                echo $result
                ;;
    Qcache_hits)
        result=`${MYSQL_CONN} extended-status |grep -w "Qcache_hits"|cut -d"|" -f3`  #每次查询在缓存中命中时就增大
                echo $result
                ;;
    Qcache_inserts)
        result=`${MYSQL_CONN} extended-status |grep -w "Qcache_inserts"|cut -d"|" -f3`  #每次插入一个查询时就增大。命中次数除以插入次数就是不中比率
                echo $result
                ;;
    Threads_connected)
        result=`${MYSQL_CONN} extended-status |grep -w "Threads_connected"|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||Inn                                                                            odb_rows_read|Innodb_rows_inserted|Innodb_rows_updated|Innodb_rows_deleted|Qcache_free_memory|Qcache_hits|Qcache_inserts|Threads_connected)"
        ;;
esac

cd /usr/local/zabbix/share/scripts vim chk_mysql.sh #!/bin/bash #check mysql_status MYSQL_USER='root' MYSQL_HOST='127.0.0.1' MYSQL_PORT='3306' MYSQL_PWD='root' MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}" if [ $# -ne "1" ];then echo "error error!" fi case $1 in Uptime) result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"` #mysql启动时间累加,mysql工作时间 echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3` #执行update 操作的次数 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` #执行select 操作的次数,一次查询只累加1 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` #执行insert 操作的次数,对于批量插入的insert 操作,只累加一次; echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` #执行delete 操作的次数 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 ;; Innodb_rows_read) result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_read"|cut -d"|" -f3` #Innodb中select 查询返回的行数 echo $result ;; Innodb_rows_inserted) result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_inserted"|cut -d"|" -f3` #执行Insert 操作插入的行数 echo $result ;; Innodb_rows_updated) result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_updated"|cut -d"|" -f3` #执行update 操作更新的行数 echo $result ;; Innodb_rows_deleted) result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_deleted"|cut -d"|" -f3` #执行update 操作更新的行数 echo $result ;; Qcache_free_memory) result=`${MYSQL_CONN} extended-status |grep -w "Qcache_free_memory"|cut -d"|" -f3` #缓存中的空闲内存 echo $result ;; Qcache_hits) result=`${MYSQL_CONN} extended-status |grep -w "Qcache_hits"|cut -d"|" -f3` #每次查询在缓存中命中时就增大 echo $result ;; Qcache_inserts) result=`${MYSQL_CONN} extended-status |grep -w "Qcache_inserts"|cut -d"|" -f3` #每次插入一个查询时就增大。命中次数除以插入次数就是不中比率 echo $result ;; Threads_connected) result=`${MYSQL_CONN} extended-status |grep -w "Threads_connected"|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||Inn odb_rows_read|Innodb_rows_inserted|Innodb_rows_updated|Innodb_rows_deleted|Qcache_free_memory|Qcache_hits|Qcache_inserts|Threads_connected)" ;; esac

修改zabbix-agentd配置文件,在UserParameter字段位置,添加:
UserParameter=mysql.status[*],/usr/local/zabbix/share/scripts/chk_mysql.sh $1
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -proot -P3306 -h127.0.0.1  ping | grep -c alive
UserParameter=mysql.version,mysql -V

UserParameter=mysql.status[*],/usr/local/zabbix/share/scripts/chk_mysql.sh $1 UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -proot -P3306 -h127.0.0.1 ping | grep -c alive UserParameter=mysql.version,mysql -V

原文地址 : https://www.xj123.info/7163.html

本站遵循 : 署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5)

版权声明 : 原创文章转载时,请务必以超链接形式标明文章原始出处

Tags: mysql , zabbix
  • 上一篇:Zabbix 3.2 agentd监控Nginx性能
  • 下一篇:Zabbix 3.2 agentd自动发现监控资源占用TOP 10进程
0条评论

暂时没有评论!

发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2025 年 6 月
一 二 三 四 五 六 日
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« 5 月    

最新文章

  • 认知,是否是一座大山?当架构决策变成配置清单比价
  • 重装博客服务器环境
  • 特斯拉24款标续 Model Y 2万公里使用体验
  • 接盘的傻子
  • 小牛us电瓶指示灯闪三次不上电
  • 一次还不错的小米售后体验
  • 装台1600元办公主机
  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记

最新评论

  • zwwooooo:类似以前做网站开发时,一开始有自...
  • 老陳网志:有点高端,像我们整点nas玩玩就够...
  • springwood:自从 CentOS 不维护之后,我换 U...
  • 大D:难都搞下来了,那就更得YM了
  • 大D:只能是YM了,谢总牛啊
  • 灰常记忆:经济不好 今年我也换了机器 一...
  • 大峰:这是海外服务器嘛?速度挺快的。
  • 大D:只能单走一个6了哈哈哈
  • zwwooooo:买特斯拉和买iPhone的人群其实相似...
  • 平安家属子痕:一直坚持油车,看你写的心里有...

日志存档

  • 2025 年 5 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2024 年 9 月
  • 2024 年 5 月
  • 2024 年 1 月
  • 2023 年 4 月
  • 2021 年 10 月
  • 2021 年 4 月
  • 2021 年 3 月
  • 2021 年 2 月
  • 2020 年 11 月
  • 2020 年 9 月
  • 2020 年 5 月
  • 2020 年 4 月
  • 2020 年 3 月
  • 2020 年 1 月
  • 2019 年 12 月
  • 2019 年 10 月
  • 2019 年 7 月
  • 2019 年 6 月
  • 2019 年 5 月
  • 2019 年 3 月
  • 2019 年 1 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2018 年 10 月
  • 2018 年 7 月
  • 2018 年 6 月
  • 2018 年 5 月
  • 2018 年 4 月
  • 2018 年 3 月
  • 2018 年 1 月
  • 2017 年 10 月
  • 2017 年 9 月
  • 2017 年 8 月
  • 2017 年 7 月
  • 2017 年 2 月
  • 2017 年 1 月
  • 2016 年 12 月
  • 2016 年 11 月
  • 2016 年 10 月
  • 2016 年 7 月
  • 2016 年 6 月
  • 2016 年 4 月
  • 2016 年 2 月
  • 2016 年 1 月
  • 2015 年 12 月
  • 2015 年 10 月
  • 2015 年 9 月
  • 2015 年 7 月
  • 2015 年 5 月
  • 2015 年 4 月
  • 2015 年 3 月
  • 2015 年 2 月
  • 2015 年 1 月
  • 2014 年 12 月
  • 2014 年 10 月
  • 2014 年 9 月
  • 2014 年 8 月
  • 2014 年 7 月
  • 2014 年 6 月
  • 2014 年 5 月
  • 2014 年 4 月
  • 2014 年 3 月
  • 2014 年 2 月
  • 2014 年 1 月
  • 2013 年 12 月
  • 2013 年 11 月
  • 2013 年 10 月
  • 2013 年 9 月
  • 2013 年 8 月
  • 2013 年 7 月
  • 2013 年 6 月
  • 2013 年 5 月
  • 2013 年 4 月
  • 2013 年 3 月
  • 2013 年 2 月
  • 2013 年 1 月
  • 2012 年 12 月
  • 2012 年 11 月
  • 2012 年 9 月
  • 2012 年 8 月
  • 2012 年 7 月
  • 2012 年 6 月
  • 2012 年 5 月
  • 2012 年 4 月
  • 2012 年 3 月
  • 2012 年 2 月
  • 2012 年 1 月
  • 2011 年 12 月
  • 2011 年 11 月
  • 2011 年 10 月
  • 2011 年 9 月
  • 2011 年 8 月
  • 2011 年 7 月
  • 2011 年 6 月
  • 2011 年 5 月
  • 2011 年 4 月
  • 2011 年 3 月
  • 2011 年 2 月
  • 2011 年 1 月
  • 2010 年 12 月
  • 2010 年 11 月
  • 2010 年 10 月
  • 2010 年 9 月
  • 2010 年 8 月
  • 2010 年 7 月

W3C

  • XHTML 1.0 Transitional
  • CSS level 3
  • Google+
Copyright © 2010-2025 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 43 queries in 1.046 seconds | Memory 38.92 MB | 尼玛的备案
Powered by WordPress. | Hosted By LAOXUEHOST | Theme by WordPress主题巴士 | 站点地图 | SiteMap | uptime查询