s4ros

adselect_unimplemented_watchdog.sh

Sep 20th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.60 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. logline='event reporting instance is not ready to be reused'
  4. logfile=$(ls -1tr /opt/ang/log/adselect/adselect.log)
  5.  
  6. if [[ $(id -u) -ne 0 ]]; then
  7.     echo "${0} has to be executed as root!"
  8.     exit 1
  9. fi
  10.  
  11. # returns adselect running time in epoch
  12. function get_adselect_run_time() {
  13.     echo $(date "+%s" -d "$(ps -eo time,cmd | grep adselect.fcgi | grep -v grep | sort -k1 -n -r | head -n 1 | awk {'print $1'})")
  14. }
  15.  
  16. # returns timestamp from the last line in $logfile in epoch
  17. function get_time_from_log() {
  18.     echo $(date "+%s" -d "$(tail -n 1 $logfile | awk '{print $1,$2}')")
  19. }
  20.  
  21. # returns current date in epoch
  22. function get_current_time() {
  23.     echo $(date "%s")
  24. }
  25.  
  26. # /////////////////////////////////////////////////////////////////////
  27. # // MAIN
  28. function main() {
  29.     adselect_restart=0
  30.     adselectrun=$(get_adselect_run_time)
  31.     timefromlog=$(get_time_from_log)
  32.     if [[ $timefromlog -gt $adselectrun ]]; then
  33.         tail -n 20 $logfile | grep "$logline" > /dev/null
  34.         if [[ $? -eq 0 ]]; then
  35.             adselect_restart=1
  36.         fi
  37.         cat $(ls -1t /opt/ang/log/adselect-req9/*.log | tail -n 1) | tail -n 20 | grep 'unimplemented' > /dev/null
  38.         if [[ $? -eq 0 ]]; then
  39.             echo "unimplemented log found"
  40.             adselect_restart=1
  41.         fi
  42.         if [[ $adselect_restart -eq 1 ]]; then
  43.             echo "adselect have to be restarted"
  44.             echo $(hostname)
  45.             echo $(curl -s showip.net;echo)
  46.             service adselect stop
  47.             sleep 3
  48.             service adselect start
  49.         fi
  50.     fi
  51.  
  52.  
  53. }
  54.  
  55. main
Add Comment
Please, Sign In to add comment