#!/bin/bash
# Allo-GG
# Routine de verification de l'etat des services

# LOG2RAM
if mount | grep -q 'log2ram on /var/log type tmpfs'
then
  echo -e "\e[32m\e[32m[OK]\e[39m\e[39m Log2Ram"
else
  echo -e "\e[31m\e[31m[ERROR]\e[39m\e[39m Log2Ram"
  gpio -g write 18 1
  exit
fi

# APACHE
if service apache2 status | grep -q 'running'
then
  echo -e "\e[32m[OK]\e[39m Apache"
else
  echo -e "\e[31m[ERROR]\e[39m Apache"
  gpio -g write 18 1
  exit
fi

# CRON
if service cron status | grep -q 'running'
then
  echo -e "\e[32m[OK]\e[39m Cron"
else
  echo -e "\e[31m[ERROR]\e[39m Cron"
  gpio -g write 18 1
  exit
fi


# WIRINGPI
if gpio -v | head -n 1 | grep -q 'gpio version: [0-9]'
then
  echo -e "\e[32m[OK]\e[39m WiringPi"
else
  echo -e "\e[31m[ERROR]\e[39m WiringPi"
  gpio -g write 18 1
  exit
fi


# INTERNET

# tester 5 ping
if ping -c5 8.8.8.8 | grep -q '0% packet loss'
# Si la reponse est 0 paquets perdu, alors OK
then
  echo -e "\e[32m[OK]\e[39m Internet"
# Mais sinon, il y a un probleme
else
  # Tentative de relancer le wifi ...
  sleep 2
  echo
  echo -e "\e[93m Probleme sur le Wifi ? Tentative de relancer la connexion en cours ...\e[39m"
  ifdown --force wlan0
  sleep 2
  ifup wlan0
  sleep 2

  # ... puis refaire un test, en testant 10 ping cette fois
  if ping -c10 8.8.8.8 | grep -q '0% packet loss'
  # Si la reponse est 0 paquets perdu, alors finalement tout va bien
  then
    echo -e "\e[32m[OK]\e[39m Internet"
  # Mais si le probleme est toujours present, alors on alerte :
  else
  echo -e "\e[31m[ERROR]\e[39m Internet"
  gpio -g write 18 1
  exit
  fi
fi

# Tout va bien ^_^ Allumage de la LED d'etat.
gpio -g write 18 0
echo
echo -e "\e[42m[Tout va bien ^_^]\e[49m"
echo
exit
