So I write the following script.
The script write in a log file the various steps.
The mail dir is a directory where other daemon check for new file and send it to mail..
The lock files avoid to write too many file on mail dir in case of heart down. It rewrite the error at most each hour.
The script include two url heartbeat, you can copy & paste other ones.
If wildfly process is down the script try to restart.
I'm not a bash script expert feel free to comment in order to correct or improve the scripts!
#!/bin/bash
LOG_FILE=/home/you/url_heartbeat.log #FILE FOR LOGGING
MAIL_DIR_FILE=/home/mailDir/heartbeat.log #FILE PATH FOR COPING THE LOG IF TEST IS NOT PASSED (IN THIS DIR A DAEMON WILL SEND A MAIL)
WILDFLY_SERVER_LOG=/opt/wildfly/standalone/log/server.log #WILDFLY SERVER.LOG TO SEE IF PARTICULAR ERROR OCCURS
CURRENT_HOUR_LOCK_FILE="$(date +%H).fail"
PREVIOUS_HOUR_LOCK_FILE="$(expr $(date +%H) - 1).fail"
rm -f $PREVIOUS_HOUR_LOCK_FILE #REMOVE PREVIOUS HOUR FILE
#DO TEST IF CURRENT_HOUR_LOCK_FILE DOESN'T EXIST
if [ -f $CURRENT_HOUR_LOCK_FILE ];
then
echo "$(date) Skip Test file $CURRENT_HOUR_LOCK_FILE exist" >> $LOG_FILE
else
echo -e "$(date) start url_heartbeat" > $LOG_FILE
TEST_PASSED=true
RESPONSE_CODE=$(curl -sL -w "%{http_code}" "http://www.example1.com/index.jsp" -o /dev/null) #GET URL RESPONSE CODE
if [ "$RESPONSE_CODE" = 301 ] || [ "$RESPONSE_CODE" = 200 ] #IF 301 REDIRECT OR 200 OK
then
echo -e "OK Example1: $RESPONSE_CODE" >> $LOG_FILE
else
echo -e "FAIL Example1: $RESPONSE_CODE" >> $LOG_FILE
TEST_PASSED=false
fi
RESPONSE_CODE=$(curl -sL -w "%{http_code}" "http://example2/index.jsp" -o /dev/null)
if [ "$RESPONSE_CODE" = 301 ] || [ "$RESPONSE_CODE" = 200 ]
then
echo -e "OK Example2: $RESPONSE_CODE" >> $LOG_FILE
else
echo -e "FAIL Example2: $RESPONSE_CODE" >> $LOG_FILE
TEST_PASSED=false
fi
WILDFLY_STATUS=$(/etc/init.d/wildfly status) #CHECK WILDFLY PROCESS STATUS
echo -e $WILDFLY_STATUS
if [[ "$WILDFLY_STATUS" == *running* ]]
then
echo -e "\nOK Wildfly Status: $WILDFLY_STATUS\n" >> $LOG_FILE
else
echo -e "\nFAIL Wildfly Status: $WILDFLY_STATUS\n" >> $LOG_FILE
TEST_PASSED=false #IF WILDFLY DOWN TRY TO RESTART
/etc/init.d/wildfly restart #RESTART COMMAND
sleep 10m #WAIT RESTARTING
WILDFLY_STATUS=$(/etc/init.d/wildfly status) #CHECK WILDFLY PROCESS STATUS AGAIN
echo -e "\nAfter Restart Wildfly Status: $WILDFLY_STATUS\n" >> $LOG_FILE
if [[ "$WILDFLY_STATUS" == *running* ]]
then
echo -e "Restart Successfully\n" >> $LOG_FILE
rm -f $CURRENT_HOUR_LOCK_FILE #TEST FAILED BUT RESTART SUCCESSFULLY SO DELETE LOCK FILE IN ORDER TO DOESN'T SKIP THE TESTS AT NEXT CRON HIT
fi
fi
echo -e "test passed: $TEST_PASSED" >> $LOG_FILE
echo -e "test passed: $TEST_PASSED"
if [ "$TEST_PASSED" = false ]
then
echo -e "NOT PASSED\n\n\n $(tail -100 $WILDFLY_SERVER_LOG)" >> $LOG_FILE
touch $CURRENT_HOUR_LOCK_FILE #MAKE A FILE IF TEST IS NOT PASSED WITH CURRENT HOUR AS NAME
cp $LOG_FILE $MAIL_DIR_FILE
fi
fi
#TEST CASE
#CURRENT_HOUR_LOCK_FILE EXIST -> SKIP TEST
#CURRENT_HOUR_LOCK_FILE NOT EXIST -> DO TEST
#ALL TEST OK -> CHECK LOG FILE FOR ALL OK ECHOES
#AT LEAST ONE RESPONSE_CODE URL TEST NOT OK ->
#1) SEE THE CURRENT_HOUR_LOCK
#2) THE LOG_FILE IS COPIED IN MAIL_DIR PATH
#3) THE NEXT CRON HIT SKIP
#WILDFLY IS NOT RUNNING -> AS THE PREVIOUS CASE AND TRY TO RESTART
#IF RESTART IS OK THE CURRENT_HOUR_LOCK IS DELETED AND NEXT CRON HIT DO THE TEST
#IF RESTART GOES WRONG THE NEXT TEST IS SKIPPED
No comments:
Post a Comment