fhumayun

PHP5-FPM Watchdog

Apr 16th, 2013
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.97 KB | None | 0 0
  1. #!/bin/sh
  2. #-------------------------------------------------
  3. # Turn on debug mode
  4. set -x
  5. #-------------------------------------------------
  6. # General Variables
  7. URL="http://seekeraftertruth.com/"
  8. BASEPATH="/home/admin/scripts/"
  9. LOGSOURCE="satstatus.log"
  10. TIMESTAMP=`date +%r`
  11.  
  12.  
  13. #-------------------------------------------------
  14. # Test Site Availability
  15. #-------------------------------------------------
  16. if [ ! -f "$URL" ]
  17. then
  18.     touch $BASEPATH$SATSTATUS
  19.     HTTP_PRE_STATUS=`wget --spider -nv $URL -o $BASEPATH$LOGSOURCE`
  20.     HTTP_STATUS=`cat $BASEPATH$LOGSOURCE |  /usr/bin/awk '{print $5}'`
  21. else
  22.     truncate -s0 $BASEPATH$LOGSOURCE
  23.     HTTP_PRE_STATUS=`wget --spider -nv $URL -o $BASEPATH$LOGSOURCE`
  24.     HTTP_STATUS=`cat $BASEPATH$LOGSOURCE |  /usr/bin/awk '{print $5}'`
  25. fi
  26. #-------------------------------------------------
  27. # Check PHP5-FPM status
  28. #-------------------------------------------------
  29. if [ "$HTTP_STATUS" != "200" ]
  30. then
  31.     # Restart the crashed PHP5-fpm service
  32.       service php5-fpm restart
  33.  
  34.     #-------------------------------------------------
  35.     # Log Handling Variables During Failure Condition
  36.     #-------------------------------------------------
  37.     LOGPATH="/srv/www/seekeraftertruth.com/logs/"
  38.     LOGPREFIX="php5-fpm-"
  39.     LOGSUFFIX=".log"
  40.     LOGDATE="php5-fpm-`(date +%F)`.log"
  41.     LOGOUTPUT=`cat $BASEPATH$LOGSOURCE | grep -v ":" `
  42.     LOGMAKE=`echo "Wget reported" $LOGOUTPUT "at" $TIMESTAMP >> $LOGPATH$LOGDATE`
  43.     LOGDAY=`echo $LOGPATH$LOGDATE | awk -F\- '{print $5}' | cut -d"." -f1`
  44.     LOGMONTH=`echo $LOGPATH$LOGDATE | awk -F\- '{print $4}'`
  45.     LOGYEAR=`echo $LOGPATH$LOGDATE | awk -F\- '{print $3}'`
  46.     LOGFILEYESTERDAY="$LOGPATH$LOGPREFIX`date -d "-1 days" +"%Y-%m-%d"`$LOGSUFFIX"
  47.     LOGYESTERDAY=`date -d "-1 days" +"%Y%m%d"`
  48.     LOGTODAY=`(date +%F)`
  49.     LOGRECORD="$LOGYEAR$LOGMONTH$LOGDAY"
  50.     LOGCOUNT=`grep -c "" $LOGPATH$LOGDATE`
  51.     LOGFLAG="${BASEPATH}WGET_FLAG_DO_NOT_DELETE_`(date +%F)`"
  52.     FLAGDATE=`stat --format "%y" $LOGPATH$LOGDATE | awk -F" " '{print $1}' | sed 's/-//g'`
  53.     CHOWNUSER="www-data"
  54.     CHOWNGRP="adm"
  55.  
  56.     # Create a log occurrence at /srv/www/seekeraftertruth.com/logs/php5-fpm-(date +%F)
  57.     $LOGMAKE
  58.    
  59.     #-------------------------------------------------
  60.     # Email Variables Initialized in Failure Condition
  61.     #-------------------------------------------------
  62.     SUBJECT="[$LOGCOUNT times] PHP5-FPM restarted"
  63.     # Email from  ?
  64.     EMAIL_FROM="php5fpm-report@seekeraftertruth.com"
  65.     # Email to  ?
  66.     EMAIL_TO="satfeed@live.com"
  67.     # Email text/message
  68.     EMAILMESSAGE="PHP5-FPM restarted $LOGCOUNT times on $LOGDAY-$LOGMONTH-$LOGYEAR"
  69.  
  70.     if [ -f "$LOGFILEYESTERDAY" ]
  71.     then
  72.         if [ ! -f "$LOGFLAG" ]
  73.         then
  74.             # echo "Going to email log file for $LOGPATH$LOGDATE"
  75.             echo $EMAILMESSAGE | mail -s "$SUBJECT" "$EMAIL_TO" -a "Reply-To: $EMAIL_FROM" < $LOGPATH$LOGDATE
  76.             touch $LOGFLAG
  77.             chown $CHOWNUSER:$CHOWNGRP "$LOGPATH$LOGDATE"
  78.             chown $CHOWNUSER:$CHOWNGRP "$LOGFLAG"
  79.         fi
  80.     fi
  81.    
  82.     if [ ${FLAGDATE} -eq ${LOGYESTERDAY} ]
  83.     then
  84.         rm $LOGFLAG
  85.     fi
  86. fi
Add Comment
Please, Sign In to add comment