For the annotation of the script and the whole idea, I'll put it in the script for you.
#!/bin/bash
Mon_file= "$" # Specify the script path to monitor
SEC=60 # Specifies the frequency to monitor, that is, how often to view the
Mon_point_file=/tmp/mon_mysql_slow.point # Specify the path to the MySQL slow log monitoring point
Diff_mon_file=/tmp/mon_mysql_slow.log # Specifies the MySQL slow log information storage path added to the monitoring frequency
Admin_mail=274546888@qq.com # Specifies which administrator to send to
function USAGE {
Echo-e "33[31m script name: 33[37m"
echo "$"
ECHO-E "33[31M Grammatical structure: 33[37m"
echo "$ {start|stop|restart} mysql slow log file path"
ECHO-E "33[31m Use example: 33[37m"
echo "$ start/usr/local/mysql/log/mysql_slow.log"
echo "$ stop"
echo "$ restart/usr/local/mysql/log/mysql_slow.log"
Echo-e "33[31m Note: 33[37m"
echo "1. The \$2 parameter cannot be null except for the stop operation, start and restart operations
echo "2. The file specified by the \$2 parameter must exist
Exit 2
}
function start {
echo "MySQL slow log monitoring process has started, the monitoring file for the $MON _file, monitoring frequency of ${sec}s once."
While:
Todo
[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#-}
Sed-n "$OLD _point,${new_point}p" $MON _file > $DIFF _mon_file
if [[s $DIFF _mon_file]];then
Sed-i ' 1i This new slow log ' $SUM _point ' bar ' $DIFF _mon_file
Mail-s "[WARNING] server $ (hostname) produces MySQL slow log $SUM _point $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 $} ']];
Then
For PID in ' Ps-ef|awk ' $0~ ' mon_mysql_slow_log.sh ' {print $} ';
Todo
[[$PID!= $$]] && kill-9 $PID >&/dev/null
Done
Else
Echo ' currently has no MySQL slow log monitoring process '
Exit 0
Fi
echo ' MySQL slow log monitoring process has stopped running '
}
function restart {
Stop
Start &
}
if [[$ = = Stop]]
Then
:
Else
[[$ < 3]] && USAGE
[[!-F $]] && USAGE
Fi
Case is in
Start
Start &
;;
Stop
Stop
;;
Restart)
Mon_file=$2
Restart
;;
*)
USAGE
;;
Esac
Ok!
That's the whole story of the script. The entire script is composed of four main functions. For the role of each function, I do here to introduce:
USAGE # This function is responsible for prompting the user how 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 is an illustration of the use of the script:
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. At the specified cycle interval, the log is changed by taking the length of the MySQL slow log as a record point, and then arriving at the next loop by comparison to the previous record point. If there is a change, the growth value is obtained by calculating the difference of the record point. The growth value is used to get the growing portion of the log, which is then sent through mail to the specified administrator mailbox.
Here to show the message sent out: