利用zabbix自定义脚本监控MySQL基础状态

news/2024/11/29 15:02:02/

mysql_monitorsh_0">1、 mysql_monitor.sh

主要关注mysql的这些状态:
uptime| version| questions| slowQueries| select| bytesSent| bytesReceived| ping| activeThread| connectThread| maxConnect| bigSql| bigTransaction | innodbLock| primaryKey| dbRole| readonly| superReadonly| ioRunning| sqlRunning| slaveBehind

#!/bin/bash
MYSQL_USER='zabbix_mysql_monitor' 
MYSQL_PWD='zabbix_mysql_monitor_passwd'
MYSQL_PATH='/vdb/mysql/bin'
MYSQL_CONN="${MYSQL_PATH}/mysql -u${MYSQL_USER} -p${MYSQL_PWD}"
MYSQL_ADMIN="${MYSQL_PATH}/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD}"main(){case $1 inuptime) result=`${MYSQL_CONN} -e "\s" 2>/dev/null|grep Uptime|awk -F ':' '{print $2}'`echo $result;;version) result=`${MYSQL_CONN} -e "\s" 2>/dev/null|grep 'Server version'|awk -F ':' '{print $2}'`echo $result;;questions)# qps 两次差集result=`${MYSQL_ADMIN} status 2>/dev/null |cut -f4 -d":"|cut -f1 -d"S"`echo $result;;slowQueries)# 两次差集result=`${MYSQL_ADMIN} status 2>/dev/null |cut -f5 -d":"|cut -f1 -d"O"`echo $result;;select)# 两次差集 selectresult=`${MYSQL_ADMIN} extended-status 2>/dev/null |grep -w "Com_select"|cut -d"|" -f3`echo $result;;bytesSent)# 两次差集result=`${MYSQL_ADMIN} extended-status 2>/dev/null |grep -w "Bytes_sent" |cut -d"|" -f3`echo $result;;bytesReceived)# 两次差集result=`${MYSQL_ADMIN} extended-status 2>/dev/null |grep -w "Bytes_received" |cut -d"|" -f3`echo $result;;ping)result=`${MYSQL_ADMIN} ping 2>/dev/null| grep -c alive` if [ ${result} -eq 1 ] ; thenecho 0elseecho 1fi;;activeThread) result=`${MYSQL_CONN} -e "show global status like 'threads_running'\G" 2>/dev/null|grep -i Value|awk -F ':' '{print $2}'`echo $result ;; connectThread) result=`${MYSQL_CONN} -e "show global status like 'threads_connected'\G" 2>/dev/null|grep -i Value|awk -F ':' '{print $2}'`echo $result ;;maxConnect) result=`${MYSQL_CONN} -e "select @@max_connections value\G" 2>/dev/null|grep -i Value|awk -F ':' '{print $2}'`echo $result ;; bigSql)# 超过30秒的sqlresult=`${MYSQL_CONN} -e "select count(*) value from information_schema.processlist where user not in ('event_scheduler','system user') and user not like 'mysql_router\%' and command<>'sleep' and command not like 'Binlog Dump%' and time>=30\G" 2>/dev/null|grep -i Value|awk -F ':' '{print $2}'`echo $result if [ ${result} -gt 0 ] ; thenfilename=/tmp/bigsql`date +"%Y%m%d%H%M%S"`res=`${MYSQL_CONN} -e "tee $filename;select * from information_schema.processlist where user not in ('event_scheduler','system user') and user not like 'mysql_router\%' and command<>'sleep' and command not like 'Binlog Dump%' and time>=30\G" 2>/dev/null`fi;;bigTransaction)# 超过5分钟的事务result=`${MYSQL_CONN} -e "select count(*) value from information_schema.innodb_trx trx join information_schema.processlist pcl on trx.trx_mysql_thread_id=pcl.id where trx.trx_mysql_thread_id !=connection_id() and to_seconds(now())-to_seconds(trx_started)>300\G" 2>/dev/null|grep -i Value|awk -F ':' '{print $2}'`echo $result if [ ${result} -gt 0 ] ; thenfilename=/tmp/bigtrx`date +"%Y%m%d%H%M%S"`res=`${MYSQL_CONN} -e "tee $filename;SELECT trx_mysql_thread_id AS PROCESSLIST_ID, NOW(), TRX_STARTED, TO_SECONDS(now())-TO_SECONDS(trx_started) AS TRX_LAST_TIME , USER, HOST, DB, TRX_QUERY FROM INFORMATION_SCHEMA.INNODB_TRX trx JOIN INFORMATION_SCHEMA.processlist pcl ON trx.trx_mysql_thread_id=pcl.id WHERE trx.trx_mysql_thread_id !=connection_id() and to_seconds(now())-to_seconds(trx_started)>300\G" 2>/dev/null`fi;;innodbLock)result=`${MYSQL_CONN} -e "select ifnull(sum(waiting_age),0) value from (SELECT distinct a.OBJECT_SCHEMA AS locked_schema, a.OBJECT_NAME AS locked_table, \"Metadata Lock\" AS locked_type, c.PROCESSLIST_ID AS waiting_processlist_id, c.PROCESSLIST_TIME AS waiting_age, c.PROCESSLIST_INFO AS waiting_query, c.PROCESSLIST_STATE AS waiting_state, d.PROCESSLIST_ID AS blocking_processlist_id, d.PROCESSLIST_TIME AS blocking_age, d.PROCESSLIST_INFO AS blocking_query, concat('KILL ', d.PROCESSLIST_ID) AS sql_kill_blocking_connection FROM performance_schema.metadata_locks a JOIN performance_schema.metadata_locks b ON a.OBJECT_SCHEMA = b.OBJECT_SCHEMA AND a.OBJECT_NAME = b.OBJECT_NAME AND a.lock_status = 'PENDING' AND b.lock_status = 'GRANTED' AND a.OWNER_THREAD_ID <> b.OWNER_THREAD_ID JOIN performance_schema.threads c ON a.OWNER_THREAD_ID = c.THREAD_ID JOIN performance_schema.threads d ON b.OWNER_THREAD_ID = d.THREAD_ID) e\G" 2>/dev/null|grep -i Value|awk -F ':' '{print $2}'`echo $resultif [ ${result} -gt 60 ] ; thenfilename=/tmp/innodbLock`date +"%Y%m%d%H%M%S"`res=`${MYSQL_CONN} -e "tee $filename;SELECT distinct a.OBJECT_SCHEMA AS locked_schema, a.OBJECT_NAME AS locked_table, \"Metadata Lock\" AS locked_type, c.PROCESSLIST_ID AS waiting_processlist_id, c.PROCESSLIST_TIME AS waiting_age, c.PROCESSLIST_INFO AS waiting_query, c.PROCESSLIST_STATE AS waiting_state, d.PROCESSLIST_ID AS blocking_processlist_id, d.PROCESSLIST_TIME AS blocking_age, d.PROCESSLIST_INFO AS blocking_query, concat('KILL ', d.PROCESSLIST_ID) AS sql_kill_blocking_connection FROM performance_schema.metadata_locks a JOIN performance_schema.metadata_locks b ON a.OBJECT_SCHEMA = b.OBJECT_SCHEMA AND a.OBJECT_NAME = b.OBJECT_NAME AND a.lock_status = 'PENDING' AND b.lock_status = 'GRANTED' AND a.OWNER_THREAD_ID <> b.OWNER_THREAD_ID JOIN performance_schema.threads c ON a.OWNER_THREAD_ID = c.THREAD_ID JOIN performance_schema.threads d ON b.OWNER_THREAD_ID = d.THREAD_ID\G" 2>/dev/null`fi;;primaryKey)result=`${MYSQL_CONN} -e "select ifnull(count(distinct table_schema,table_name),0) value from information_schema.columns where table_schema not in ('sys','information_schema','performance_schema','mysql','sysaux','mysql_innodb_cluster_metadata') and (table_name not in (select distinct table_name from information_schema.columns where column_key='PRI') and (table_schema,table_name) not in (select table_schema,table_name from information_schema.views where table_schema not in ('sys','information_schema','performance_schema','mysql','sysaux','mysql_innodb_cluster_metadata')))\G" 2>/dev/null|grep -i Value|awk -F ':' '{print $2}'`echo $result;;dbRole) # 0是主库result=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|wc -l`if [ ${result} -eq 0 ] ; thenecho 0elseecho 1fi;;readonly)# 0表示只读状态正常(主库不检查readonly,从库readonly=1)result=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|wc -l`readonly=`${MYSQL_CONN} -e "select @@read_only value\G" 2>/dev/null|awk -F ':' '{print $2}'`readonly=`eval echo $readonly`if [ ${result} -eq 0 ] ; thenecho 0elif [ ${result} -gt 0 ]&[ ${readonly} -eq 1 ] ; thenecho 0elseecho 1fi;;superReadonly)# 0表示只读状态正常(主库不检查readonly,从库readonly=1)result=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|wc -l`readonly=`${MYSQL_CONN} -e "select @@super_read_only value\G" 2>/dev/null|awk -F ':' '{print $2}'`readonly=`eval echo $readonly`if [ ${result} -eq 0 ] ; thenecho 0elif [ ${result} -gt 0 ]&[ ${readonly} -eq 1 ] ; thenecho 0elseecho 1fi;;ioRunning)# 0表示IO进程状态正常result=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|wc -l`ioRunning=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|grep -i -w Slave_IO_Running|awk -F ':' '{print $2}'`ioRunning=`eval echo $ioRunning| tr [a-z] [A-Z]`if [ ${result} -eq 0 ] ; thenecho 0elif [ ${result} -gt 0 ]&[ ${ioRunning} == 'YES' ] ; thenecho 0elseecho 1fi;;sqlRunning)result=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|wc -l`sqlRunning=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|grep -i -w Slave_SQL_Running|awk -F ':' '{print $2}'`sqlRunning=`eval echo $sqlRunning| tr [a-z] [A-Z]`if [ ${result} -eq 0 ] ; thenecho 0elif [ ${result} -gt 0 ]&[ ${sqlRunning} == 'YES' ] ; thenecho 0elseecho 1fi;;slaveBehind)result=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|wc -l`slaveBehind=`${MYSQL_CONN} -e "show slave status\G" 2>/dev/null|grep Seconds_Behind_Master|awk -F ':' '{print $2}'`if [ ${result} -eq 0 ] ; thenecho 0slaveBehind=0elseecho ${slaveBehind}fiif [ ${slaveBehind} -gt 60 ] ; thenfilename=/tmp/slave`date +"%Y%m%d%H%M%S"`res=`${MYSQL_CONN} -e "tee $filename;show slave status\G" 2>/dev/null`fi;;*)echo "Usage:$0 (uptime| version| questions| slowQueries| select| bytesSent| bytesReceived| ping| activeThread| connectThread| maxConnect| bigSql| bigTransaction | innodbLock| primaryKey| dbRole| readonly| superReadonly| ioRunning| sqlRunning| slaveBehind)" ;; esac
}main $1

mysql_monitorshetczabbixsharezabbixexternalscripts_183">2、把mysql_monitor.sh放在/etc/zabbix/share/zabbix/externalscripts/,并赋予它执行权限

mysqlMYSQL_USERzabbix_mysql_monitor_sqlmysql_184">3、另外你需要在mysql数据库中创建一个用来监控用的账户MYSQL_USER=‘zabbix_mysql_monitor’ ,以下这些事sql语句,是需要进去mysql交互界面输入的。

create user 'zabbix_mysql_monitor'@'localhost' identified with mysql_native_password by 'zabbix_mysql_monitor_passwd';grant select,process,replication client on *.* to 'zabbix_mysql_monitor'@'localhost' ;flush privileges;

zabbixzabbix_agent2dmysql_confmysqlconf_193">4、在/etc/zabbix/zabbix_agent2.d/mysql_conf写入一个文件mysql.conf

UserParameter=mysql.status[*],/etc/zabbix/share/zabbix/externalscripts/mysql_monitor.sh $1

zabbixserver_198">5、OK了。然后你就可以去zabbix-server网页端创建自动发现规则和监控项除法项了


http://www.ppmy.cn/news/1550925.html

相关文章

深入解析分布式遗传算法及其Python实现

目录 深入解析分布式遗传算法及其Python实现目录第一部分:分布式遗传算法的背景与原理1.1 遗传算法概述1.2 分布式遗传算法的引入1.3 分布式遗传算法的优点与挑战优点:挑战:第二部分:分布式遗传算法的通用Python实现2.1 基本组件的实现第三部分:案例1 - 基于多种交叉与变异…

bp(二)利用java安装破解bp

声明&#xff01; 学习视频来自B站up主 **泷羽sec** 有兴趣的师傅可以关注一下&#xff0c;如涉及侵权马上删除文章&#xff0c;笔记只是方便各位师傅的学习和探讨&#xff0c;文章所提到的网站以及内容&#xff0c;只做学习交流&#xff0c;其他均与本人以及泷羽sec团队无关&a…

systemverilog约束中:=和:/的区别

“x dist { [100:102] : 1, 200 : 2, 300 : 5}” 意味着其值等于100或101或102或200或300其中之一&#xff0c; 其权重比例为1:1:1:2:5 “x dist { [100:102] :/ 1, 200 : 2, 300 : 5}” 意味着等于100&#xff0c;101&#xff0c;102或200&#xff0c;或300其…

YunSDR通信小课堂-5

第2讲 单音信号自收发测试 2.1 实验目的 1. 掌握YunSDR的基本使用和配置方法&#xff1b; 2. 掌握MATLAB网络通信基本原理&#xff1b; 3. 理解数据的收发过程。 2.2 实验器材 YunSDR一台&#xff0c;PC机一台&#xff0c;Matlab2013a&#xff08;或更高版本&#xff09;&a…

使用开源GCC编译微软WMI相关函数的示例代码

如下代码是使用国产RedPanda-Cpp的编译工具编译的&#xff0c;该工具使用简单&#xff1b; 该方式是调用微软的WMI接口相关函数 但是使用GCC编译会出现编译不过的问题&#xff0c;很多代码库的函数都不存在&#xff1b; 在编译时&#xff0c;需要添加这些库文件&#xff1a;…

golang 实现比特币内核:如何接入 RPC 后端获得特定交易的二进制数据

我们非常关注解析比特币的二进制数据,这使得我们的工作看起来是可行的。比特币是一个分布式网络系统,这意味着它需要全球各地的节点协同工作,甚至比特币核心库也需要连接其他节点来帮助它,就像查询交易费一样。 世界上没有免费的午餐。当你使用比特币系统进行交易时,你需…

ssm189基于Java的在线教育平台设计与实现+jsp(论文+源码)_kaic

毕 业 设 计&#xff08;论 文&#xff09; 题目&#xff1a;在线教育平台的设计与实现 摘 要 互联网发展至今&#xff0c;无论是其理论还是技术都已经成熟&#xff0c;而且它广泛参与在社会中的方方面面。它让信息都可以通过网络传播&#xff0c;搭配信息管理工具可以很好地为…

如何解决 java.security.acl.NotOwnerException: 在 ACL 中尝试执行非所有者的操作问题?亲测有效的解决方法!

在 Java 中&#xff0c;java.security.acl.NotOwnerException 异常通常出现在访问控制列表&#xff08;ACL&#xff09;操作中。当你尝试在一个不属于拥有者的实体上执行特定的操作时&#xff0c;Java 安全管理器会抛出此异常。简单来说&#xff0c;它指的是你正在尝试执行一个…