monit のアラートメールに top の結果を入れる方法

monit のアラートメールが来ても、cpu usage が 70% 超えた、というのがわかっても、どのプロセスがcpuを使っているのかわからねー。ってなる。

なので、アラートメールに top コマンドの結果を入れる方法

参考サイトは以下だが、スクリプトが少し間違っている&Linuxのtopコマンドとオプションが違う。

Re: Alert format question/feature request
http://lists.gnu.org/archive/html/monit-general/2011-09/msg00006.html

/usr/local/bin/send_top.sh として以下を保存

#!/bin/bash                                                                     
# copied from                                                                   
# Re: Alert format question/feature request                                     
# http://lists.gnu.org/archive/html/monit-general/2011-09/msg00006.html         
#                                                                               

OUTPUT="/tmp/monit-top.out"

exec 1>$OUTPUT;
exec 2>&1

# monit event description                                                       
echo "Host: $MONIT_HOST"
echo "Service: $MONIT_SERVICE"
echo "Event: $MONIT_EVENT"
echo "Description: $MONIT_DESCRIPTION"
echo "Date: $MONIT_DATE "
echo

# append the top output                                                         
/usr/bin/top -b -n 1 -d 10 | head -30
#send the alert to users in the script arguments ($1, $2, \205)  
cat $OUTPUT | /bin/mail -s "$MONIT_SERVICE: $MONIT_EVENT" $@


/etc/monit.conf で以下のように設定する

    if cpu usage (user) > 70% for 3 cycles then exec "/usr/local/bin/send_top.sh mail@example.com"