MySQL Slow log monitor script instance anatomy

Source: Internet
Author: User
Tags diff

MySQL Slow Log on the company line, has not been well monitored. While I was idle last week, I wrote the monitoring script, and today I specially sent out the code to share with 51 Bo friends.

        for script annotations and the overall idea, I'll put it behind the script and explain it to you.

#!/bin/bash##  This script is used to monitor  MySQL  slow log changes within a specified frequency and to alert you when growth occurs # written by sunsky#  Mail : [email protected]# date : 2014-11-17 10:00:00#mon_file= "$"     #  Specify the script path that you want to monitor sec=60          #  Specify the frequency to be monitored, that is, how often to view mon_point_file=/tmp/mon_mysql_slow.point  #  specifies the path to the monitoring point where the MySQL slow log is stored Diff_mon_ file=/tmp/mon_mysql_slow.log     #  Specifies the storage path of the MySQL slow log information added within the monitoring frequency [email  protected]            #  specifies which administrator function to send to  USAGE {    echo -e  "\033[31m script name:  \033[37m"      echo  "    $0"     echo -e  "\033[31M syntax structure:  \033 [37m     echo      $0 {start|stop|restart} mysql slow log file path "&NBSP;&NBSP;&NBSP;&NBSP;ECHO&NBSP;-E&NBSP; " \033[31m Use Example:  \033[37m "    echo "     $0 start /usr /local/mysql/log/mysql_slow.log "    echo "     $0 stop "     echo  "    $0 restart /usr/local/mysql/log/mysql_slow.log"     echo -e  "\033[31m precautions:  \033[37m"     echo  "     1.  In addition to the stop operation, the,\$2  parameter cannot be null when start and restart operations "    echo " The file specified by the     2. \$2  parameter must exist "    exit 2}function start  {    echo  "MySQL slow log monitoring process has been started, the monitoring file for   $MON _file , monitoring frequency is  ${sec}s once."     while :    do         [ -f  $MON _point_file ] | |  echo 0 >  $MON _point_file        new_point=$ (awk  ' End{print nr} '   $MON _file)          old_point=$ (< $MON _point_file)         [[ -z $ old_point ]]&&old_point=0        sum_point0=$ ($NEW _ point-$OLD _point))         SUM_POINT=${SUM_POINT0#-}         tail -$SUM _point  $MON _file >  $DIFF _mon_file         if [[ -s  $DIFF _mon_file ]];then             sed -i  ' 1i  This new slow log   ' $SUM _point '   '    $DIFF _mon_file            mail  -s  "[Warning]  server  $ (hostname)   Generate  MySQL  slow log   $SUM _point  Bar"   $ADMIN _ mail <  $DIFF _mon_file            >  $DIFF _mon_file             echo  $NEW _point >  $MON _ point_file        fi         sleep ${sec}s    done}function stop {         if [[ -n  ' ps -ef|awk  ' $0~ "mon_mysql_slow_log.sh" {print $2} '  ] ;            then                 for PID in  ' Ps -ef|awk   ' $0~ ' mon_mysql_slow_log.sh ' {print $2} ';                        do                            [[   $PID  != $$ ]] && kill -9  $PID  >& /dev/null                         done            else                 echo  ' Currently no MySQL slow log monitoring process '                  exit 0        fi         echo  ' MySQL slow log monitoring process has stopped running '}function restart {         stop        start &}if [[ $1 ==  Stop ]]    then        :    else         [[ $2 < 3 ]] && USAGE         [[ ! -f $2 ]] && USAGEficase $1  In    start)         start &         ;;     stop)         stop         ;;     restart)         MON_FILE=$2         restart        ;;     *)         USAGE         ;; Esac

Ok!

The above is the entire contents of the script. The entire script is comprised of four main functions. For the purpose of each function, here's what I do:

Usage # The function is responsible for prompting the user to use the script correctly start # This function is responsible for starting the script stop # This function is responsible for stopping the monitoring script Restart # This function is responsible for restarting the monitoring script

The following script is used:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/53/80/wKiom1RpXvbQT11mAANgpoABdRc407.jpg "title=" Qq20141117103349.png "alt=" Wkiom1rpxvbqt11maangpoabdrc407.jpg "/>

The whole idea of the script is to start a dead loop through the while:;d o statement;done, and then control the cycle interval of the dead loop through sleep in the dead loop. In the specified cycle interval, the log is changed by taking the length of the MySQL slow log as a record point, and then when the next loop arrives, by comparison to the previous record point. If there is a change, the growth value is calculated by calculating the difference of the record point. This portion of the log that grows is obtained by increasing the value, and then sent to the specified administrator mailbox via mail.

Here are the messages sent out:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/53/7F/wKioL1RpaCmDwmdoAADNd7VSm3c747.jpg "title=" Qq20141117111043.png "alt=" Wkiol1rpacmdwmdoaadnd7vsm3c747.jpg "/>

Ok! The other one can understand, here is no nonsense.

This article concludes, hope to be able to help 51 Bo friends!

This article is from the "not only Linux" blog, so be sure to keep this source http://nolinux.blog.51cto.com/4824967/1577326

MySQL Slow log monitor script instance anatomy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.