在mysql上执行的时候show engine innodb status
,出现这样的行---TRANSACTION 17610C9A, ACTIVE 504 sec starting index read
:
我需要一个脚本来监控关键字后的秒数何时ACTIVE
超过 100,然后发送电子邮件警报。
答案1
value=$(show engine innodb status | grep TRANSACTION | grep ACTIVE | \
sed -e 's/.*ACTIVE //' -e 's/\([[:digit:]]\{1,\}\).*/\1/')
if [ "$value" -gt 100 ]
then
send email here
fi
我不确定要 grep 做什么,因为听起来输出中可能还有其他行——greps 的目标是只包含我们应该在其中查找“ACTIVE NNN”部分的行。
sed 表达式只是删除前导部分(.* 到“ACTIVE”),然后匹配 1 个或多个数字,删除数字后面的任何内容。如果结果值严格大于 100,则发送您的电子邮件。
答案2
我会做类似的事情:
detected=(
mysql --defaults-extra-file=/etc/mysql/debian.cnf --raw -Be '
show engine innodb status' | perl -ln -0777 -e '
while (/(\*\*\* \(\d+\) TRANSACTION:\s+TRANSACTION \S+ ACTIVE (\d+).*?)(?=\*\*\*)/sg) {
print $1 if $2 > 100;
}'
)
if [ -n "$detected" ]; then
mailx -s "$subject" "$addresses" << EOF
Some transactions took more than 100 seconds:
$detected
EOF
fi
作为参考,上面的内容适应了我看到的格式,看起来更像是:
*** (1) TRANSACTION:
TRANSACTION AE523D, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 9 lock struct(s), heap size 1248, 4 row lock(s), undo log entries 2
MySQL thread id 40705, OS thread handle 0x7fa6d8197700, query id 5697977 localhost bugs updating
DELETE FROM tokens WHERE token = 'JpLgGK5Ygn'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 32823 n bits 280 index `PRIMARY` of table `bugs`.`tokens` trx id AE523D lock_mode X locks rec but not gap waiting
Record lock, heap no 212 PHYSICAL RECORD: n_fields 7; compact format; info bits 32
0: len 10; hex 4a704c67474b3559676e; asc JpLgGK5Ygn;;
1: len 6; hex 000000ae523b; asc R;;;
2: len 7; hex 6c0000c01f0572; asc l r;;
3: len 3; hex 80000f; asc ;;
4: len 8; hex 80001255ef969792; asc U ;;
5: len 7; hex 73657373696f6e; asc session;;
6: len 17; hex 6372656174655f6174746163686d656e74; asc create_attachment;;
*** (2) TRANSACTION: