shell監控伺服器埠加web飛信報警

科技小能手發表於2017-11-12

#!/bin/sh

#################################################

# check remote os service

#

#

# date:2010-02-26

##################################################

export LANG=en_US.UTF-8

###########################################

# check pid 

#

############################################

MY_PID=`/tmp/check.pid`

#媯€鏌id鏂囦歡鏄惁瀛樺湪

if [ -f “$MY_PID” ]

then

#pid鏂囦歡瀛樺湪

PID_NUM=$(cat /tmp/check.pid)

CHECK_PID=$(ps aux | grep $PID_NUM | grep -v grep)

#媯€鏌ヨ繖涓猵id榪涗▼鏄惁瀛樺湪

if [ “$CHECK_PID” != “” ]

then

exit 0

fi

fi

#鐢熸垚紼嬪簭鑷繁鐨㓨id,騫朵繚瀛?

#$$鎸囩殑鏄綋鍓嶇▼搴廝ID

echo $$ > $MY_PID

##################################################################################

ROOT_DIR=`/root/monitor`

#IPADDR_ARRAY=(“http://www.pk.com” “http://news.pk.com/shtml/20100401/864541.shtml” “http://down.pk.com/xiazai/6661.shtml” “http://xin.1.html” “http://act.2pk.com/spe/10/wow/5years/”)

IPADDR_ARRAY=(“www.51112pk.com” “p2p.inbel.com”)

IPADDR_ARRAY_LEN=${#IPADDR_ARRAY[@]}

#SERVICE_PORT=`80`

CHECK_LOG_DIR=”$ROOT_DIR/check_log”

COUNT_LOG_DIR=”$ROOT_DIR/count_log”

#####################################

#check directory and count file

#

#####################################

if [ ! -d “$CHECK_LOG_DIR” ]

then

mkdir -p $CHECK_LOG_DIR

fi

if [ ! -d “$COUNT_LOG_DIR” ]

then

mkdir -p $COUNT_LOG_DIR

fi

i=0

while [ $i -lt $IPADDR_ARRAY_LEN ]

do

IPADDR=${IPADDR_ARRAY[$i]}

CHECK_LOG_FILE=”$CHECK_LOG_DIR/$IPADDR.log”

COUNT_LOG_FILE=”$COUNT_LOG_DIR/$IPADDR.txt”

WEB_MESSAGE_FAILED=”$IPADDR–is-not-running”

WEB_MESSAGE_SUCCESS=”$IPADDR–is-up-and-runing-fine”

if [ ! -s “$COUNT_LOG_FILE” ]

then

echo 0 > $COUNT_LOG_FILE

fi



COUNT_FILE_NUMBER=$(cat $COUNT_LOG_FILE)

echo | curl -I $IPADDR > $CHECK_LOG_FILE

#grep “404” $CHECK_LOG_FILE > /dev/null

HTTP_STATUS=$(head -n 1 $CHECK_LOG_FILE | awk `{print $2}`)

if [ “$HTTP_STATUS” = “200” ]

then

if [ “$COUNT_FILE_NUMBER” = “3” ]

then

curl “http://sms.api.bz/fetion.php?username=159555555&password=254605sxz&sendto=1595333333&message=$WEB_MESSAGE_SUCCESS”

echo 0 > $COUNT_LOG_FILE

fi

else

case $COUNT_FILE_NUMBER in

0)

echo 1 > $COUNT_LOG_FILE

curl “http://sms.api.bz/fetion.php?username=159522222&password=254605sxz&sendto=1595555557&message=$WEB_MESSAGE_FAILED”

;;

1)

echo 2 > $COUNT_LOG_FILE

curl “http://sms.api.bz/fetion.php?username=1592222&password=254605sxz&sendto=15911111&message=$WEB_MESSAGE_FAILED”

;;

2)

echo 3 > $COUNT_LOG_FILE

curl “http://sms.api.bz/fetion.php?username=15952222&password=254605sxz&sendto=159111111&message=$WEB_MESSAGE_FAILED”

;;

*)

;;

esac

fi

let i++

done

##################################################################

rm -rf $MY_PID



根據自己的需要可以變化修改這個指令碼檢測伺服器埠存活報警

 `

本文轉自 liang3391 51CTO部落格,原文連結:http://blog.51cto.com/liang3391/456605


相關文章