序言:
        业务报警订单提交异常,页面一直没有反应,排查后是事务没有提交或者回滚导致,想到如果及时监控事务的运行状态报警出来,那么就可以及时排查出问题所在,方便运营处理,所以自己就弄了一个shell脚本放在nagios来处理事务报警情况。

1,编写事务监控脚本 
#!/bin/bash
# author: tim.man
# version: 1.0
# desc: check the RUNNING TRANSACTION over

ST_OK=0  
ST_WR=1  
ST_CR=2  
ST_UK=3

TIME_TRX=10

# 提示信息
print_help() {  
    echo "$PROGNAME -w INT -c INT"  
    echo "Options:"  
    echo "  -w/--warning)"  
    echo "     Sets a warning number"  
    echo "  -c/--critical)"  
    echo "     Sets a critical level for io"  
    exit $ST_UK  
}

while test -n "$1"; do  
    case "$1" in  
        -help|-h)  
            print_help  
            exit $ST_UK  
            ;;  
        --warning|-w)  
            warning=$2  
        shift  
            ;;  
        --critical|-c)  
            critical=$2  
        shift  
            ;;  
        *)  
            echo "Unknown argument: $1"  
            print_help  
            exit $ST_UK  
            ;;  
        esac  
    shift  
done

get_wcdiff() {  
    if [ ! -z "$warning" -a ! -z "$critical" ]  
    then  
        wclvls=1  
  
        if [ ${warning} -gt ${critical} ]  
        then  
            wcdiff=1  
        fi  
    elif [ ! -z "$warning" -a -z "$critical" ]  
    then  
        wcdiff=2  
    elif [ -z "$warning" -a ! -z "$critical" ]  
    then  
        wcdiff=3  
    fi  
}

# 脚本判断
val_wcdiff() {  
    if [ "$wcdiff" = 1 ]  
    then  
        echo "Please adjust your warning/critical thresholds. The warning must be lower than the critical level!"  
        exit $ST_UK  
    elif [ "$wcdiff" = 2 ]  
    then  
        echo "Please also set a critical value when you want to use warning/critical thresholds!"  
        exit $ST_UK  
    elif [ "$wcdiff" = 3 ]  
    then  
        echo "Please also set a warning value when you want to use warning/critical thresholds!"  
        exit $ST_UK  
    fi  
}

get_wcdiff  
val_wcdiff

# 统计mysql的事务中最大运行时间
max_over_time=`/usr/local/mysql/bin/mysql
--user=nagios --password="nagiosq@xxx"  -NS /usr/local/mysql/mysql.sock
-e "SELECT TIME_TO_SEC(TIMEDIFF(NOW(),t.trx_started)) FROM
information_schem
a.INNODB_TRX
t WHERE TIME_TO_SEC(TIMEDIFF(NOW(),t.trx_started))>$TIME_TRX ORDER
BY TIME_TO_SEC(TIMEDIFF(NOW(),t.trx_started)) DESC LIMIT 1;" |awk
'{print $1}'`

# 如果当前没有RUNNING的事务,则直接赋值为0,以免下面if判断出错
if [ ! -n "$max_over_time" ];then max_over_time=0 
fi

# 取得当前所以阻塞的事务数量
num_trx=`/usr/local/mysql/bin/mysql
--user=nagios --password="nagiosq@xxx"  -NS /usr/local/mysql/mysql.sock
-e "SELECT COUNT(1) FROM information_schema.INNODB_TRX t WHERE
TIME_TO_SEC(TIMEDIF
F(NOW(),t.trx_started))>$TIME_TRX;" |awk '{print $1}'`

if [ -n "$warning" -a -n "$critical" ]  
then  
    if [ `expr $max_over_time \> $warning` -eq 1 -a `expr  $max_over_time \< $critical` -eq 1 ]  
    then  
        echo "WARNING - $num_trx TRANSACTIONS RUNNING,go over for $max_over_time seconds"  
        exit $ST_WR  
    elif [ `expr $max_over_time \> $critical` -eq 1 ]  
    then  
        echo "CRITICAL- $num_trx TRANSACTIONS RUNNNING,go over for $max_over_time seconds"  
        exit $ST_CR  
    else  
        echo "OK- TRANSACTIONS RAN successfully."  
        exit $ST_OK  
    fi  
fi

2,在nagios客户端添加脚本监控
先测试下脚本
[root@wgq_idc_dbm_3_61 binlog]# /usr/local/nagios/libexec/check_trx -w 30 -c 60
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
OK- TRANSACTIONS RAN successfully.

[root@wgq_idc_dbm_3_61 binlog]#

在nrpe.cfg里面添加监控命令
[root@wgq_idc_dbm_3_61 binlog]# vim /usr/local/nagios/etc/nrpe.cfg
command[check_mysql_trx]=/usr/local/nagios/libexec/check_trx -w 30 -c 60

之后重启nagios客户端监控, service nrpe restart

4,在nagios主监控服务器上面添加配置选项
先去nagios服务器上面check一下
[root@localhost etc]# /usr/local/nagios/libexec/check_nrpe -H10.2xx.3.xx -c check_mysql_trx
OK- TRANSACTIONS RAN successfully.
[root@localhost etc]#

在services.cfg里面添加事务监控选项:
define service{
        host_name               mysqlserver
        service_description     Check mysql transctions
        check_command           check_nrpe!check_mysql_trx
        max_check_attempts      5
        check_command           check_nrpe!check_mysql_trx
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          opsweb
        }

在commands.cnf里面添加事务监控命令:
# add by tim.man on 20141201
define command{
        command_name    check_mysql_trx
        command_line    $USER1$/check_mysql_trx -w $ARG1$ -c $ARG2$
        }

邮件短信报警电话报警已经添加,所以无需重新配置。

然后重新加载nagios
[root@localhost objects]# service nagios reload
Running configuration check...
Reloading nagios configuration...
done
[root@localhost objects]#

Nagios 里面监控MySQL事务一直RUNNING没有结束的报警Shell脚本 (转)的更多相关文章

  1. MySql创建函数与过程,触发器, shell脚本与sql的相互调用。

    一:函数 1:创建数据库和表deptartment, mysql> use DBSC; Database changed mysql), ), )); Query OK, rows affect ...

  2. 分布式监控系统Zabbix-3.0.3-完整安装记录 - 添加shell脚本监控

    对公司的jira访问状态进行监控,当访问状态返回值是200的时候,脚本执行结果为1:其他访问状态返回值,脚本执行结果是0.然后将该脚本放在zabbix进行监控,当非200状态时发出报警.jira访问状 ...

  3. 实现对MySQL数据库进行分库/分表备份(shell脚本)

    工作中,往往数据库备份是件非常重要的事情,毕竟数据就是金钱,就是生命!废话不多,下面介绍一下:如何实现对MySQL数据库进行分库备份(shell脚本) Mysq数据库dump备份/还原语法: mysq ...

  4. perl 监控mysql 事务和锁

    use DBI; use Net::SMTP; use HTTP::Date qw(time2iso str2time time2iso time2isoz); # mail_user should ...

  5. 监控之--Nagios如何监控本地主机及本地服务

    上一节内容介绍了Nagios监控服务在linux环境下的安装过程,本节内容将详细介绍如何使用已经安装的Nagios服务的一些配置文件的使用以及如何监控本地相关服务,如要完成对一台主机的监控Nagios ...

  6. zabbix 邮件报警 监控mysql主从

    1)设置邮件模板及邮件服务器 邮箱密码记得写授权密码 2)配置接受报警的邮箱 3)添加报警触发器 配置邮箱服务器 yum -y install mailx yum -y install sendmai ...

  7. shell脚本实例-系统监控

    shell脚本监控网站并实现邮件.短信报警shell进程监控脚本(发送邮件报警)Shell脚本监控服务器在线状态和邮件报警的方法 http://www.jbxue.com/jb/shell/ 11. ...

  8. MySQL 使用XtraBackup的shell脚本介绍

    mysql_backup.sh是关于MySQL的一个使用XtraBackup做备份的shell脚本,实现了简单的完整备份和增量备份.以及邮件发送备份信息等功能.功能目前还比较简单,后续将继续完善和增加 ...

  9. nagios监控mysql主机,nginx,磁盘IO,网卡流量

    http://blog.chinaunix.net/uid-28685162-id-3506260.html nagios安装完成,打开/usr/local/nagios/etc/nagios.cfg ...

随机推荐

  1. IOS中大文件拷贝算法

    + (void)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath { //每次读取数据大小 #define READ_SI ...

  2. dedecms 织梦列表页标题增加显示页数

    判断是否为第一页,为第一页则不显示页数的代码: {dede:pagelist listsize='0' listitem='pageno' function='html2text(@me)' runp ...

  3. 浅谈CDN技术的性能与优势

    从淘宝架构中的CDN入手分析 使用CDN和反向代理提高网站性能.由于淘宝的服务器不能分布在国内的每个地方,所以不同地区的用户访问需要通过互联路由器经过不同长度的路径来访问服务器,返回路径也一样,所以数 ...

  4. Spark-运行时架构

    Spark运行时架构 在分布式环境下,Spark集群采用的时主/从结构.在一个Spark集群中,有一个节点负责中央协调,调度各个分布式工作节点.这个中央协调节点被称为驱动器(Driver),与之对应的 ...

  5. Python去除字符串的空格

    Python能够找出字符串开头和末尾多余的空白. 要确保字符串末尾没有空白,可使用方法rstrip(). 还可以剔除字符串开头的空白,或同时剔除字符串两端的空白. 为此,可分别使用方法lstrip() ...

  6. webdriver 选择

    You only need   from selenium import webdriver . Execute   html= browser.find_element_by_xpath(" ...

  7. dr01_SetColor

    1. TGraphicUnit.SetColor 2. 3.

  8. Springboot- Caused by: org.hibernate.AnnotationException: No identifier specified for entity:

    错误与异常: Caused by: org.hibernate.AnnotationException: No identifier specified for entity: 原因:引用了不对的包, ...

  9. JavaScript 对时间日期格式化

    JavaScript 对时间日期格式化 // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位 ...

  10. JavaScript 获取输入时的光标位置及场景问题

    前言 在输入编辑的业务场景中,可能会需要在光标当前的位置或附近显示提示选项.比如社交评论中的@user功能,要确保提示的用户列表总是出现在@字符右下方,又或者是在自定义编辑器中 autocomplet ...