복수서버들의 서비스포트 응답여부를 확인하여 메일로 보내주는 스크립트
2012.01.17 00:09
원문 : http://www.ischo.net -- 조인상 // 시스템 엔지니어
Writer : http://www.ischo.net -- ischo // System Engineer in Replubic Of Korea
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
원문 : http://www.ischo.net -- 조인상 //시스템 엔지니어
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
스크립트명 : chk_port.sh
스크립트목적 : 복수 서버들의 서비스 리스트를 작성 (server.list)
server.list 의 목록에 있는 서비스포트들의 응답여부를 점검하여 죽거나 복구된 서버들의 리스트를
지정된 메일로 보내준다.
환경파일 작성방법(server.list)
형식) <서버명칭> <호스트이름> <서버IP> <서비스포트> : 필드구분은 스페이스바로 구분
WEBSERVER test_web 192.168.10.1 80
DBSERVER test_db 192.168.10.2 1521
WEBSERVER2 test_web2 192.168.10.3 8080
DBSERVER2 test_db2 192.168.10.4 1522
#!/bin/ksh
######################################################################################
# script by ischo.
# http://www.ischo.net
# mail:chosim1@naver.com
# script description :
# Send mail to ${MAILADDR} if ${TARGET_SERVER}'s ${SERVER_PORT} port disabled
######################################################################################
# for time check
STIME=`date +%Y%m%d%H%M%S`
MTIME=`date +%H:%M`
DDATE=`date +%Y%m%d`
HTIME=`date +%H`
# Set Environment
MAILADDR=chosim1@naver.com
PROGHOME=/home/admin/chk_port
LISTFILE=${PROGHOME}/server.list
LOGFILE=${PROGHOME}/server_${DDATE}.log
MAILFILE=${PROGHOME}/mail.txt
STATDIR=${PROGHOME}/stat
# initial set
if [ ! -d ${STATDIR} ];
then
mkdir ${STATDIR}
fi
cat /dev/null > ${MAILFILE}
MAX_ROW=`cat ${LISTFILE} | wc -l`
CURRENT_ROW=1
#############################################################################################
## START MAIN PROCEDURE
#############################################################################################
while [ ${CURRENT_ROW} -le ${MAX_ROW} ];
do
ROW_CONTENT=`cat ${LISTFILE} | head -${CURRENT_ROW} | tail -1`
SERVER_NAME=`echo ${ROW_CONTENT} | awk {'print $1'}`
HOST_NAME=`echo ${ROW_CONTENT} | awk {'print $2'}`
SERVER_IP=`echo ${ROW_CONTENT} | awk {'print $3'}`
SERVER_PORT=`echo ${ROW_CONTENT} | awk {'print $4'}`
STATFILE=${STATDIR}/${SERVER_NAME}.stat
if [ ! -f ${STATFILE} ];
then
echo "0" > ${STATFILE}
fi
# Check server port
telnet ${SERVER_IP} ${SERVER_PORT} << !
quit
!
##################################################################################################################
# Check port and compare before status -> Logging
if [ $? -eq 0 ];
then
if [ `cat ${STATFILE}` -eq $? ];
then
echo "A L I V E : ${DDATE} ${MTIME} ${SERVER_NAME} (${HOST_NAME})-${SERVER_IP}:${SERVER_PORT}" >> ${LOGFILE}
else
echo "!RECOVERED: ${DDATE} ${MTIME} ${SERVER_NAME} (${HOST_NAME})-${SERVER_IP}:${SERVER_PORT}" >> ${LOGFILE}
echo "!RECOVERED: ${DDATE} ${MTIME} ${SERVER_NAME} (${HOST_NAME})-${SERVER_IP}:${SERVER_PORT}" >> ${MAILFILE}
echo 0 > ${STATFILE}
TRIGER_CHANGED=1
fi
else
if [ `cat ${STATFILE}` -eq $? ];
then
echo "! D O W N : ${DDATE} ${MTIME} ${SERVER_NAME} (${HOST_NAME})-${SERVER_IP}:${SERVER_PORT}" >> ${LOGFILE}
echo "! D O W N : ${DDATE} ${MTIME} ${SERVER_NAME} (${HOST_NAME})-${SERVER_IP}:${SERVER_PORT}" >> ${MAILFILE}
echo 1 > ${STATFILE}
TRIGER_CHANGED=1
else
echo "STIL DOWN : ${DDATE} ${MTIME} ${SERVER_NAME} (${HOST_NAME})-${SERVER_IP}:${SERVER_PORT}" >> ${LOGFILE}
fi
fi
##################################################################################################################
CURRENT_ROW=`expr ${CURRENT_ROW} + 1`
done
echo >> ${LOGFILE}
# Sendmail if port status changed ################################################################################
if [ ${TRIGER_CHANGED} -eq 1 ];
then
mail -s "KGC server Event !!!" ${MAILADDR} < ${MAILFILE}
fi
##################################################################################################################
find ${PROGHOME} -name "server*.log" -type f -mtime +15 -exec rm {} \;