zoukankan      html  css  js  c++  java
  • perl 监控mysql 事务和锁

    use DBI;
    use Net::SMTP;
       use HTTP::Date qw(time2iso str2time time2iso time2isoz);  
    # mail_user should be your_mail@163.com
    sub send_mail{
       if (@_ != 2){print "please input message and mailto";exit 1};
        my $message= shift;
    my $CurrTime = time2iso(time());
        my $to_address  = shift;
         my $mail_user   = 'yjzhao@podinns.com';
        my $mail_pwd    = 'xx';
        my $mail_server = 'smtp.exmail.qq.com';
        my $from    = "From: $mail_user
    ";
        my $subject = "Subject: zjcap info
    ";
        my $info = "$message";
         my $message = <<CONTENT; 
         $info
    CONTENT
        my $smtp = Net::SMTP->new($mail_server);
    
        $smtp->auth($mail_user, $mail_pwd) || die "Auth Error! $!";
        $smtp->mail($mail_user);
        $smtp->to($to_address);
    
        $smtp->data();             # begin the data
        $smtp->datasend($from);    # set user
        $smtp->datasend($subject); # set subject
        $smtp->datasend("
    
    ");
        $smtp->datasend("$message
    "); # set content
        $smtp->dataend();
        $smtp->quit();
    };
    if ( $#ARGV != 1 ){
        print "input your root password and ip address"."
    ";
    	 exit(-1);  
    	};
    my $message='information_schema';
    my $ip="$ARGV[1]";
    my $user="root";
    my $passwd="$ARGV[0]";
    my $dbh = DBI->connect("dbi:mysql:database=$message;host=$ip;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
    @arr2=();
    ##防止utf-8中文乱码
    $dbh->do("SET NAMES utf8");
    my $hostSql = qq{SELECT 
        NOW(),  (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(a.trx_started)) diff_sec,
        b.id,
        b.user,
        b.host,
        b.db
    FROM
        information_schema.innodb_trx a
            INNER JOIN
        information_schema.PROCESSLIST b ON a.TRX_MYSQL_THREAD_ID = b.id};
    my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);
    my $selStmt = $dbh->prepare($hostSql);
      $selStmt->execute();
      $selStmt->bind_columns(undef, $a1, $a2, $a3,$a4,$a5,$a6);
      print "$a1,$a2,$a3,$a4,$a5,$a6
    ";
       while( $selStmt->fetch() )  
          {  
          if ($a2 >= 20){
          print "$a1,$a2,$a3,$a4,$a5,$a6
    ";    
          print " mysq[$ip]  processid[$a3] $a4@$a5 in db[$a6] hold  transaction time $a2 "."
    "; 
          send_mail(" mysq[$ip]  processid[$a3] $a4@$a5 in db[$a6] hold  transaction time $a2 ",'yjzhao@podinns.com');
          }; 
         };
    my $hostSql = qq{SELECT 
        r.trx_id waiting_trx_id,
        r.trx_mysql_thread_id waiting_thread,
        r.trx_query waiting_query,
        b.trx_id blocking_trx_id,
        b.trx_mysql_thread_id blocking_thread,
        b.trx_query blocking_query
    FROM
        information_schema.innodb_lock_waits w
            INNER JOIN
        information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
            INNER JOIN
        information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id};
    my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);
    my $selStmt = $dbh->prepare($hostSql);
      $selStmt->execute();
      $selStmt->bind_columns(undef, $a1, $a2, $a3,$a4,$a5,$a6);
      while( $selStmt->fetch() )  
          {  
          if ($a1&&$a2&&$a3&&$a4&&$a5){
          print "$a1,$a2,$a3,$a4,$a5,$a6
    ";    
          print "  blocking_thread[$5] blocking waiting_thread[$a2]'s $a3"."
    "; 
          send_mail("  blocking_thread[$a5] blocking waiting_thread[$a2]'s $a3" ,'yjzhao@podinns.com');
          }; 
         };

  • 相关阅读:
    electron 安装失败解决办法
    面向 B 端的产品经理
    如何学习一门新语言或框架
    斑马为什么有条纹?
    沃伦·巴菲特 | 成功的 10/10/10 法则
    如果想进入一家大公司面试,你会怎么做?
    人工智能缺陷与误觉:让机器产生幻觉的「怪异事件」
    20 世纪 70 年代的太空殖民艺术
    如何实现SSH断开后 进程仍然在后台运行
    让Linux关闭终端(关闭SSH等)后,程序继续运行
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350016.html
Copyright © 2011-2022 走看看