程序監控

程序監控

下面script可以監控給定的程序(利用process的識別單字),亦可以將結果透過MAIL發送出來
#!/bin/bash
#################################################################################
#
# Author: simon su
# Create Date: 2008/6/6
# Object: To show the PS array processes and can also mail alert for losing processes.
# Usage:
#  1. "psMonitor-sync.sh": to show all process status
#  2. "psMonitor-sync.sh -m xxx@ooo.com": to mail the monitor result to xxx
#
#################################################################################
usage(){
echo Usage:
echo  1. psMonitor-sync.sh: to show all process status \(JobName...\)
echo  2. psMonitor-sync.sh -m xxx@ooo.com: to mail the monitor result to xxx.
echo  3. psMonitor-sync.sh -d \"JobName XXXX OOOO\" [-m xxx@ooo.com]: to using specific keyword as the monitor key.
}

if [ -n "$1" ] && [ "$1" == "--help" ]; then
usage
exit 0
fi

MAIL_TMP=/tmp/mail-psMonitor.txt

if [ -n "$1" ] && [ -n "$2" ] && [ "$1" = "-d" ] ; then
        PS="$2"
        #echo Set PS to $PS
else
        PS="`cat /home/weblogic/script/conf/services.conf`"
fi

i=0
err_lost=0
err_lost_list=""
err_more=0
err_more_list=""

init(){
        rm $MAIL_TMP
        touch $MAIL_TMP
}

domail(){
        if [ -n "$1" ]; then
                /usr/bin/finger >> $MAIL_TMP
                mail -s "Daemon abnormal alert: $name `date`" $1 < $MAIL_TMP
        fi
}

writemail(){
        echo "Daemon: $1 process abnormal [$2]..." >> $MAIL_TMP
        echo "Time: `date`" >> $MAIL_TMP
        echo "Current Users:" >> $MAIL_TMP
        echo `finger` >> $MAIL_TMP
        echo ------------------------------------------------------ >> $MAIL_TMP
}

#Renew the mail content
init

for name in ${PS[*]}
do
        ((i++))
        echo $i-[$name]
        if [ `ps -efwww | grep $name | grep java | grep -v $0 | wc -l` -eq 0 ]; then
                ((err_lost++))
                err_lost_list="$err_lost_list $name"
                writemail $name "lost"
                echo "  No daemon: $name ..."
        elif [ `ps -efwww | grep $name | grep java | grep -v $0 | wc -l` -gt 1 ]; then
                ((err_more++))
                err_more_list="$err_more_list $name"
                writemail $name "more"
                echo "  To more daemon: $name ..."
                echo "  "$name ......[`ps -AHwww --format pid --format command | grep $name | grep java | /bin/awk '{print $1}'`]
        else
                echo "  "$name ......[`ps -AHwww --format pid --format command | grep $name | grep java | /bin/awk '{print $1}'`]
                #echo `ps -efwww | grep $name | grep java`
        fi
        echo ""
done
echo -----------------------------------------------------------------------------
echo Total monitor services: $i,
echo Lost list \(not work service\): $err_lost - $err_lost_list
echo More list \(more then 1 process\): $err_more - $err_more_list
echo ""

if [ -n "$1" ] && [ "$1" == "-d" ] && [ -n $3 ] && [ "$3" == "-m" ] && [ -n $4 ]; then
        if [ $err_lost -gt 0 ] || [ $err_more -gt 0 ]; then
        echo will mail to $4
        domail $4
        fi
elif [ -n "$1" ] && [ "$1" == "-m" ] && [ -n $2 ]; then
        if [ $err_lost -gt 0 ] && [ $err_more -gt 0 ]; then
        echo will mail to $2
        domail $2
        fi
fi

這個網誌中的熱門文章

Bash判斷參數是否存在

Node.js package : forever