두 개의 서비스를 확인하고 서비스 상태가 다운된 것으로 확인되면 이메일이 전송되는 스크립트를 5분마다 실행하도록 예약했습니다. 잘 작동합니다.
하지만 문제는 서비스가 온라인 상태가 될 때 UP 경고도 보내길 원한다는 것입니다. 이메일을 보내기 위해 행을 추가할 수 있지만 계속 반복해서 경고가 표시됩니다. 경고 기능을 어떻게 추가할 수 있지만 경고는 한 번만 보내야 합니다(다운 경고와 마찬가지로).
#!/bin/bash
if pgrep "mysql" > /dev/null
then
echo "MYSQL Running"
rm -f /var/run/.mysql_mail_sent
else
echo "ALERT: mysqld Stopped."
if [ ! -f /var/run/.mysql_mail_sent ]; then
echo "Sending MYSQL DOWN Email..."
echo "DOWN ALERT! sending email" > /var/run/.mysql_mail_sent
fi
fi
if pgrep "radiusd" > /dev/null
then
echo "radiusd Running"
rm -f /var/run/.radiusd_mail_sent
else
echo "ALERT: RADIUSD Stopped"
if [ ! -f /var/run/.radiusd_mail_sent ]; then
echo "RADIUSD DOWN: Sending Email."
echo "DOWN ALERT! sending email" > /var/run/.radiusd_mail_sent
fi
fi
답변1
자, 드디어 성공했습니다. BASH는 더 빠르며 타사 도구가 필요하지 않습니다. [오후 4시 43분 업데이트]
#!/bin/bash
# Scheduled Script to check linux service status after every 5 minutes.
# If found stopped, send sms or email Alerts, but donot repeat it untill next status change.
# Syed Jahanzaib
# Run script with service name like
# ./status.sh mysqld
# Check if no service name is given
if [ "$1" == "" ]; then
echo No service name have been provided.
echo Usage exmaple:
echo
echo -e "./status.sh mysqld"
echo
fi
DATE=`date`
COMPANY="MYISP"
SERVICE1="$1"
SUBJECT="ALERT: $SERVICE1 is Down..."
STATUS_HOLDER="/tmp/$SERVICE1_STATUS_HOLDER.txt"
# KANNEL Gateway Info
KANNELURL="127.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="password"
CELL1="0333xxxxxx"
# SMS Msgs test
MSG_UP="$COMPANY Info: $SERVICE1 is now UP @ $DATE"
MSG_DOWN="$COMPANY Alert: $SERVICE1 is now DOWN @ $DATE"
touch $STATUS_HOLDER
for SRVCHK in $SERVICE1
do
PID=$(pgrep $SERVICE1)
if [ "$PID" == "" ]; then
echo "$SRVCHK is down"
if [ $(grep -c "$SRVCHK" "$STATUS_HOLDER") -eq 0 ]; then
echo "ALERT: $SERVICE1 is down at $(date) / SENDING SMS ...."
echo "$MSG_DOWN" > /tmp/$SERVICE1_down.sms
# Sending DOWN SMS via KANNEL
cat /tmp/$SERVICE1_up.sms | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
echo "$SRVCHK" >> $STATUS_HOLDER
fi
else
echo -e "$SRVCHK is alive and its PID are as follows...\n$PID"
if [ $(grep -c "$SRVCHK" "$STATUS_HOLDER") -eq 1 ]; then
echo "INFO ALERT : $SERVICE1 is UP at $(date) / SENDING SMS ...."
echo "$MSG_UP" > /tmp/$SERVICE1_up.sms
# Sending UP SMS via KANNEL
cat /tmp/$SERVICE1_up.sms | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
sed -i "/$SRVCHK/d" "$STATUS_HOLDER"
fi
fi
done