#!/bin/bash

HOSTNAME=`hostname`
DOMAIN=`hostname -d`
EMAIL_ADDR="alert@webhosting-franken.de"

# schlafe für 10 sekunden wegen false nginx Test
sleep 10

# debug hostname
#echo $HOSTNAME.$DOMAIN
#exit

if [[ -f "/tmp/php_reload_lock" ]]; then
echo "EXIT!"
    exit
fi

touch /tmp/php_reload_lock


## Pruefung PHP Socket Problem
nginx_log=`tail -n50 /var/log/nginx/error.log | grep 'Connection refused' | grep socket | wc -l`;

if [ "$nginx_log" -gt 0  ]; then

  MSG="NGINX Socket Problem!!! PHP Error.....";

pickups=`tail -n50 /var/log/nginx/error.log | grep 'Connection refused'`

  echo "$MSG
$pickups
" | mailx -s "$HOSTNAME Alert!" $EMAIL_ADDR

fi

nginx -t 2> /tmp/nginx_test.txt
NGINX_CONF_TEST=`cat /tmp/nginx_test.txt | grep "syntax is ok"`
if [[ "$NGINX_CONF_TEST" != "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok" ]]; then
#if [[ "${NGINX_CONF_TEST//$'\r'/}" != "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok" ]]; then
        echo "$HOSTNAME NGINX!! CONFIG FEHLER!!!"
        MSG=`cat /tmp/nginx_test.txt`
        echo $MSG
        echo $MSG | mailx -s "$HOSTNAME NGINX!! CONFIG FEHLER!!!" alert@webhosting-franken.de
fi
rm /tmp/nginx_test.txt

rm /tmp/php_reload_lock
exit
## Abbruch.

TEST_APACHE=`curl -I -m5 -sS 127.0.0.1:8082/server-status | head -1 | grep -o '.\{3\}$'`
if [[ "${TEST_APACHE//$'\r'/}" != "OK" ]]; then

        _PID=`ps aux | grep apache2 | awk '{ print $2 }'`

        if [[ "$_PID" != "" ]]; then
                kill -9 $_PID &> /dev/null
                rm -f /var/run/apache2.pid
        fi
        logger System Apache restart from monitoring
        /etc/init.d/apache2 start
fi
sleep 1

TEST_NGINX=`curl -I -m5 -sS https://$HOSTNAME.$DOMAIN/test.html | head -1 | awk '{print $2}'`
if [[ "${TEST_NGINX//$'\r'/}" != "200" ]]; then

        echo "Restart NGINX!! "

        NGINX_PID=`ps aux | grep nginx | awk '{ print $2 }'`

        if [[ "$NGINX_PID" != "" ]]; then
                kill -9 $NGINX_PID &> /dev/null
                rm -f /var/run/nginx.pid
        fi
        logger System Nginx restart from monitoring
        /etc/init.d/nginx start
fi


nginx -t 2> /tmp/nginx_test.txt
NGINX_CONF_TEST=`cat /tmp/nginx_test.txt | grep "syntax is ok"`
if [[ "$NGINX_CONF_TEST" != "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok" ]]; then
#if [[ "${NGINX_CONF_TEST//$'\r'/}" != "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok" ]]; then
        echo "$HOSTNAME NGINX!! CONFIG FEHLER!!!"
        MSG=`cat /tmp/nginx_test.txt`
        echo $MSG
        echo $MSG | mailx -s "$HOSTNAME NGINX!! CONFIG FEHLER!!!" alert@webhosting-franken.de
fi
rm /tmp/nginx_test.txt

TEST56=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_56.php`
if [ "$TEST56" != "1" ]; then
    echo "Restart PHP!! Version: 5.6"
    kill_pid=`ps aux | grep "php/5.6" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php5.6-fpm restart
    logger System Restart PHP!! Version: 5.6 from monitoring
fi
sleep 1

TEST70=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_70.php`
if [ "$TEST70" != "1" ]; then
    echo "Restart PHP!! Version: 7.0"
    kill_pid=`ps aux | grep "php/7.0/" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php7.0-fpm restart
    logger System Restart PHP!! Version: 7.0 from monitoring
fi
sleep 1

TEST71=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_71.php`
if [ "$TEST71" != "1" ]; then
    echo "Restart PHP!! Version: 7.1"
    kill_pid=`ps aux | grep "php/7.1" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php7.1-fpm restart
    logger System Restart PHP!! Version: 7.1 from monitoring

fi
sleep 1

TEST72=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_72.php`
if [ "$TEST72" != "1" ]; then
    echo "Restart PHP!! Version: 7.2"
    kill_pid=`ps aux | grep "php/7.2" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php7.2-fpm restart
    logger System Restart PHP!! Version: 7.2 from monitoring
fi
sleep 1

TEST73=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_73.php`
if [ "$TEST73" != "1" ]; then
    echo "Restart PHP!! Version: 7.3"
    kill_pid=`ps aux | grep "php/7.3" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php7.3-fpm restart
    logger System Restart PHP!! Version: 7.3 from monitoring
fi

TEST74=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_74.php`
if [ "$TEST74" != "1" ]; then
    echo "Restart PHP!! Version: 7.4"
    kill_pid=`ps aux | grep "php/7.4" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php7.4-fpm restart
    logger System Restart PHP!! Version: 7.4 from monitoring
fi

TEST80=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_80.php`
if [ "$TEST80" != "1" ]; then
    echo "Restart PHP!! Version: 8.0"
    kill_pid=`ps aux | grep "php/8.0" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php8.0-fpm restart
    logger System Restart PHP!! Version: 8.0 from monitoring
fi

TEST81=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_81.php`
if [ "$TEST81" != "1" ]; then
    echo "Restart PHP!! Version: 8.1"
    kill_pid=`ps aux | grep "php/8.1" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php8.1-fpm restart
    logger System Restart PHP!! Version: 8.1 from monitoring
fi

TEST82=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_82.php`
if [ "$TEST82" != "1" ]; then
    echo "Restart PHP!! Version: 8.2"
    kill_pid=`ps aux | grep "php/8.2" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php8.2-fpm restart
    logger System Restart PHP!! Version: 8.2 from monitoring
fi

TEST83=`curl -m10 -sS https://$HOSTNAME.$DOMAIN/test_83.php`
if [ "$TEST83" != "1" ]; then
    echo "Restart PHP!! Version: 8.3"
    kill_pid=`ps aux | grep "php/8.3" | grep master | awk '{ print $2 }'`
    echo "Kill PID: "$kill_pid
    kill -9 $kill_pid
    /etc/init.d/php8.3-fpm restart
    logger System Restart PHP!! Version: 8.3 from monitoring
fi

rm /tmp/php_reload_lock
