#!/bin/bash
# checking if http is really down 
if systemctl is-active --quiet httpd.service ; then 
  echo "Http seems to be in active state, kindly check server side if alert is not closed"; 
  exit
fi

#Getting apache down time stamp from  systemctl
check_apache_down_time_stamp=$(systemctl show httpd --property=ActiveExitTimestamp | awk -F"="  '{print substr($2, 1, length($2)-3)}'  | cut -d: -f1,2)
#echo "printing last apache down time ""$check_apache_down_time_stamp"""

#Converting timestamp to epoch time
con_time_epoch=$(date -d "$check_apache_down_time_stamp" +"%s")
#echo "Successfully converted to epoch time "$con_time_epoch"""

#Again coverting epoch to regular time stamp as per to match apache logs time stamp
con_epoch_to_regular=$(date +"%a %b %d %H:%M:%S %Y" -d@"$con_time_epoch" |cut -d: -f1,2)
#echo "coverted time stamp as per apache logs ""$con_epoch_to_regular"""

#Getting current system time for case condition
current_system_time=$(date  '+%a %b %_d %H:%M')

# Regex to search logs between 10 mins of intervals start from apache down time stamps

if sed -n "\#\[""$con_epoch_to_regular""#, \#\[""$current_system_time""#p" /var/log/apache2/error_log | grep "httpd: bad" ; then
  /root/bin/montool clearsemaphores 
  /scripts/restartsrv_httpd
fi
