원문 : http://www.ischo.net -- 조인상 // 시스템 엔지니어

Writer : http://www.ischo.net -- ischo // System Engineer in Replubic Of Korea

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

원문 : http://www.ischo.net -- 조인상 //시스템 엔지니어

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

프로세스 생사여부 검사후 메일보내는 쉘스크립트

 

먼저 해당 디렉토리의  proclist.txt 파일에 정의된 프로세스 리스트들을 읽어서

생사여부를 검사하고

죽은 프로세스가 있으면 메일로 보내주는 스크립트

 

완전하진 않지만 그럭저럭 쓸만함.

 

 

 

 

#!/bin/ksh

######################################################################################
# script by ischo.
# http://www.ischo.net
# mail:chosim1@naver.com
# script description :
# This script read first $PROG_HOME/proclist.txt file, then check process.
# If exist died process, make Mail Contents and send mail to $MAIL_RECIEVER
######################################################################################


######################################################################################
## ENVIRONMENT SETTING - You can edit this.
######################################################################################

# Absolute Path of script
PROG_HOME=/root/chkproc

# E-mail reciever
MAIL_RECIEVER=chosim1@naver.com

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

 

 

######################################################################################
# Variables for host environment - You do not need to edit.
######################################################################################

# ServerName : This setting will be included in SMS
SERVERNAME=`hostname`

# Config File List
MAILFILE=${PROG_HOME}/chkproc.mail
LOGFILE=${PROG_HOME}/chkproc_`date +%Y%m%d`.log
PROCLIST=${PROG_HOME}/proclist.txt
ALIVEFILE=${PROG_HOME}/alive_proclist.tmp
LASTSTATFILE=${PROG_HOME}/laststat.tmp
CURSTATFILE=${PROG_HOME}/curstat.tmp
TEMPSTATFILE=${PROG_HOME}/tempstat.tmp


# TIME mark VARS
STIME=`date +%Y%m%d%H%M%S`
TTIME=`date +%H:%M`
DDATE=`date +%Y%m%d`

# First VAR's value for Operation
ERROR=0
NOPROC=`cat ${PROCLIST} | wc -l`
Recover_stat="no"
Down_stat="no"

# First creation of LASTSTATFILE
if [ -f ${LASTSTATFILE} ];
then
        touch ${LASTSTATFILE}
else
        for TEMP in `cat ${PROCLIST}`
        do
        echo "${TEMP} : DOWN" >> ${LASTSTATFILE}
        done
fi

cat /dev/null > ${CURSTATFILE}

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

 

 

######################################################################################
# DEFINE FUNCTIONS
######################################################################################
# Function SendMail - Sending Mail of Error
function Func_SendMail
{


for PROCNAME in `cat ${PROCLIST}`
do

        CURRENT_STAT=`cat ${CURSTATFILE} | grep ${PROCNAME} | awk '{print $3}'`
        LAST_STAT=`cat ${LASTSTATFILE} | grep ${PROCNAME} | awk '{print $3}'`

        if [ ${CURRENT_STAT} = UP ];
        then
                if [ ${LAST_STAT} = DOWN ];
                then
                Recover_stat="yes"
                fi
                cat ${LASTSTATFILE} | grep -v ${PROCNAME} > ${TEMPSTATFILE}
                cat ${TEMPSTATFILE} > ${LASTSTATFILE}
                echo "${PROCNAME} : UP" >> ${LASTSTATFILE}
        else
                if [ ${LAST_STAT} = UP ];
                then
                Down_stat="yes"
                fi
                cat ${LASTSTATFILE} | grep -v ${PROCNAME} > ${TEMPSTATFILE}
                cat ${TEMPSTATFILE} > ${LASTSTATFILE}
                echo "${PROCNAME} : DOWN" >> ${LASTSTATFILE}
        fi

done

if [ ${Down_stat} = yes ];
then
mail -s "Process Down Alert!" ${MAIL_RECIEVER} < ${MAILFILE}
fi

if [ ${Recover_stat} = yes ];
then
echo "mail -s 'Process Recovered!' ${MAIL_RECIEVER} < ${MAILFILE}"
mail -s "Process Recovered!" ${MAIL_RECIEVER} < ${MAILFILE}
fi

}

 

# Function LogTop - Logging Number of Alive Processes
function Func_LogTop
{
NOP=0
cat /dev/null > ${ALIVEFILE}

for TEMP_PROC in `cat ${PROCLIST}`
do

        for REAL_PROC in `ps -ef | grep ${TEMP_PROC} | grep -v grep | awk {'print $8'} | sort | uniq`
        do
                if [ ${TEMP_PROC} = ${REAL_PROC} ];
                then
                ps -ef | grep ${TEMP_PROC} | grep -v grep | awk {'print $8'} | sort | uniq >> ${ALIVEFILE}
                NOP=`expr ${NOP} + 1`
                fi
        done

done

echo "${DDATE} ${TTIME}  Number of alive Processes : ${NOP}/${NOPROC}" >> ${LOGFILE}
}

 

# Function Logging - Log Process Check Result
function Func_Logging
{
echo "${DDATE} ${TTIME}  --Process ${PROC} Down" >> ${LOGFILE}
}


# Function LogBottom - Log Bottom Message
function Func_LogBottom
{
cat /dev/null > ${ALIVEFILE}

if [ ${ERROR} -eq 1 ];
then
echo "ps -ef result :" >> ${LOGFILE}
cat ${ALIVEFILE}  >> ${LOGFILE}
fi

}

 

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

 


#############################################################################################
# Main Procedure
# - Start Program
#############################################################################################

echo "-- ${DDATE} ${TTIME}  ${SERVERNAME} Process Check List --" > $MAILFILE


# Logging Number of Alive Processes
Func_LogTop


# Loop Process check and Log
for PROC in `cat $PROCLIST`
do

PROC_CHECK=`ps -ef | grep $PROC | grep -v grep | wc -l`
SEND_MSG=`echo "[critical] $SERVERNAME Process $PROC Down. $TTIME"`

        if [ $PROC_CHECK -eq 0 ];
        then

        echo "-- Process $PROC Down. ${TTIME}" >> $MAILFILE
        echo "${PROC} : DOWN" >> ${CURSTATFILE}
        Func_Logging
        ERROR=1

        else
        {
        echo "-- Process $PROC alive. ${TTIME}" >> $MAILFILE
        echo "${PROC} : UP" >> ${CURSTATFILE}
        }

        fi

done

# Send Mail
Func_SendMail


# Maintain Number of LOG files
find ${PROG_HOME} -name "*.log" -type f -mtime +5 -exec rm {} \;

 

 

번호 제목 글쓴이 날짜 조회 수
공지 [공지] 게시자료 열람자유. 불펌금지입니다. 조인상 2010.12.07 32453
64 kill 명령어로 줄수 있는 interrupt list ischo 2012.08.17 12808
63 apache log rotate 시키기 ischo 2012.08.14 17452
62 DNS 기초에서 보안까지 조인상 2012.07.26 11663
61 아파치. 톰캣 연동 (apache+tomcat by mod_jk) ischo 2012.04.18 17543
60 SELINUX 비활성화하기 ischo 2012.04.06 16157
59 CentOS 6 에서 XDMCP 활성화 방법 ischo 2012.02.25 15579
58 리눅스 커널이 64bit 인지 확인방법 ischo 2012.02.24 35289
57 복수서버들의 서비스포트 응답여부를 확인하여 메일로 보내주는 스크립트 file ischo 2012.01.17 14968
56 리눅스에서 디스크 덤프하기 ischo 2011.12.22 22268
55 telnet 으로 메일 보내기 ischo 2011.12.07 14654
54 웹서버 포트 무응답시 메일 보내주는 스크립트 file ischo 2011.11.30 11220
53 Shell 스크립트의 내장변수 + exit code ischo 2011.11.16 17123
» 프로세스 생사여부 검사후 메일보내는 쉘스크립트 file ischo 2011.11.10 13797
51 TCP/IP Flow file ischo 2011.10.19 11830
50 ssh 접속시 로그인 속도가 느릴경우 속도 향상법 ischo 2011.08.30 23285
49 백그라운드로 프로세스 실행시키기 ischo 2011.08.25 15053
48 TCP TIME_WAIT이 많이 발생하여 문제가 되는 경우 ischo 2011.08.18 28659
47 리눅스 mdadm 을 이용한 S/W RAID 구성 [1] ischo 2011.07.20 25716
46 리눅스 Services 설명 ischo 2011.07.08 24260
45 아파치 SSL 적용된 경우 startup 시에 SSL 패스워드 자동입력시키기 ischo 2011.06.02 24569
서버에 요청 중입니다. 잠시만 기다려 주십시오...