프로세스 생사여부 검사후 메일보내는 쉘스크립트
2011.11.10 19:46
원문 : 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 {} \;