老谢博客

  • 首页
  • 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条评论

暂时没有评论!

发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2023年 5月
一 二 三 四 五 六 日
1234567
891011121314
15161718192021
22232425262728
293031  
« 4月    

最新文章

  • 装台1600元办公主机
  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记
  • centos7.9部署oxidized自动备份交换机配置
  • Surface Pro 4更换屏幕与电池
  • VCSA中删除horizon view链接克隆生成的replica-受保护副本
  • Esxi6.7U3安装SanDisk Fusion-io 1.3T ioscale Pci SSD加速卡驱动
  • 搭建ELK日志系统分析处理fortigate的syslog日志
  • 华为USG防火墙配置NAT映射回流解决内网通过公网映射访问内部服务器

最新评论

  • 王光卫博客:以前也喜欢DIY现在玩不动了
  • 大D:大佬!
  • Sirit:够用就好,不过现在的电子产品是真的...
  • 美樂地:我也想买个mini PC,打算等下一代的...
  • Mr.Chou:老牌子又不用带独显够用的,如换是...
  • 小熊:评论区全是大佬,就我完全不太懂。
  • 老俍:真是巧了,我也想给苏菲4pro换电池呢,...
  • 空空裤兜:all in one的小主机都有两千的了。...
  • Pampo:1600组一个12100的不错了,价格压挺低...
  • 无敌风火轮:和索尼没啥关系,这是索尼电脑的...

日志存档

  • 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-2023 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 2 queries in 0.224 seconds | Memory 36.63 MB | 皖ICP备13010663号-1
Powered by WordPress. | Hosted By 腾讯云 | Theme by WordPress主题巴士 | 站点地图 | SiteMap | Uptime | 技术支持:苏州天剑计算机系统有限公司