Advertisement
RamSet

Pi-hole state probe.

Jan 21st, 2020
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.37 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # target IP of monitored Pi-hole
  4. target=192.168.1.3
  5.  
  6. # count the pings
  7. count=$( ping -c 3 -w 3 $target | grep icmp* | wc -l )
  8.  
  9. # folder where we store files
  10. dir=/home/pi/
  11.  
  12. #check for active state
  13. probe2=$(curl -s http://${target}/admin/ | grep Active)
  14. check2=$(echo $probe2)
  15.  
  16. #check for offline state
  17. probe=$(curl -s http://${target}/admin/ | grep offline)
  18. check=$(echo $probe)
  19.  
  20. #run selftest (this is on the Pi-hole instance that will take over in case the main one is offline/in error state)
  21. selftest=$(curl -s http://127.0.0.1/admin/ | grep "Active")
  22. selfecho=$(echo $selftest)
  23.  
  24. function selftest {
  25. self=$(curl -s http://127.0.0.1/admin/ | grep "Active")
  26. selfecho=$(echo $self)
  27. }
  28.  
  29. function pass {
  30. sudo cp /home/pi/stuff/*.* /etc/dnsmasq.d/
  31. }
  32.  
  33. #notifications via pushover (will send process state via pushover and attempt restart)
  34. function ernotify {
  35. echo "ER1 - Moving resolv.conf file"
  36. sudo mv /etc/resolv.conf /etc/resolv.conf.old
  37. echo "ER1 - Using 1.1.1.1 as DNS for notification"
  38. sudo echo "nameserver 1.1.1.1" > /etc/resolv.conf
  39. echo "ER1 - Fail Notification"
  40. pushover pihole "<b><font color="red"><b>Pi-Hole FTLS DNS is in error state</font> since $(date). <br>Attempting restart</b>"
  41. echo "ER1 - Restoring resolv.conf"
  42. sudo mv /etc/resolv.conf.old /etc/resolv.conf
  43. echo "ER1 - Grabbing FTL status"
  44. selfstatus1=$(sudo systemctl status pihole-FTL.service > ${dir}FTLstatusER1)
  45. selffail31=$(cat ${dir}FTLstatusER1 | tail -3 | awk 'NR==3')
  46. selffail21=$(cat ${dir}FTLstatusER1 | tail -3 | awk 'NR==2')
  47. selffail11=$(cat ${dir}FTLstatusER1 | tail -3 | awk 'NR==1')
  48. echo "ER1 - Restarting FTL"
  49. sudo systemctl restart pihole-FTL.service
  50. echo "ER1 - Successful notification"
  51. pushover pihole "<b>Pi-Hole FTLS DNS is <font color="lime">fixed</b></font> (restart successful)<br> FTL-Service status prior to restart: <br><br>$selffail31 <br><br>$selffail21 <br><br>$selffail11 "
  52. }
  53.  
  54. #aditional health check. if first attempt didn't fix the service, send a notification about it and request manual intervention
  55. function ernotify2 {
  56. echo "ER2 - Moving resolv.conf file"
  57. sudo mv /etc/resolv.conf /etc/resolv.conf.old
  58. echo "ER2 - Using 1.1.1.1 as DNS for notification"
  59. sudo echo "nameserver 1.1.1.1" > /etc/resolv.conf
  60. echo "ER2 - Grabbing FTL status"
  61. selfstatus=$(sudo systemctl status pihole-FTL.service > ${dir}FTLstatusER2)
  62. selffail3=$(cat ${dir}FTLstatusER2 | tail -3 | awk 'NR==3')
  63. selffail2=$(cat ${dir}FTLstatusER2 | tail -3 | awk 'NR==2')
  64. selffail1=$(cat ${dir}FTLstatusER2 | tail -3 | awk 'NR==1')
  65. echo "ER2 - FTL Debug Notification sent"
  66. pushover pihole "<b><font color="red"><b>Pi-hole FTL restart failed.</font></b> <br>Manual intervention is needed. FTL-Service output: <br><br>$selffail3 <br><br>$selffail2 <br><br>$selffail1 "
  67. sleep 5
  68. echo "ER2 - Restoring resolv.conf"
  69. sudo mv /etc/resolv.conf.old /etc/resolv.conf
  70. }
  71.  
  72. selftest
  73.  
  74. #compare/rpobe for error status
  75. if [ -z "$selfecho" ]
  76. then
  77.     if [ -e ${dir}FTL.err ]
  78.     then
  79.     echo "FTL error file present. Exiting..."
  80.     exit 0
  81.     else
  82.     echo ""
  83.     echo "Local FTL in error state."
  84.     echo ""
  85.     ernotify
  86.     selftest=$(curl -s http://127.0.0.1/admin/ | grep "Active")
  87.     selfecho=$(echo $selftest)
  88.     echo ""
  89.     echo "Running additional check ..."
  90.     if [ -z "$selfecho" ]
  91.     then
  92.     echo ""
  93.     echo "FTL still in error mode. Running diagnostic check..."
  94.     echo ""
  95.     ernotify2
  96.     echo ""
  97.     echo "Create lock file..."
  98.     touch ${dir}FTL.err
  99.     echo ""
  100.     else
  101.     echo ""
  102.     echo "FTL restart - Second check. FTL up and running."
  103.     fi
  104.     fi
  105. else
  106.     echo ""
  107.     echo "Local FTL up and running."
  108.     echo ""
  109.     sudo rm -f ${dir}FTL.err
  110. fi
  111.  
  112.  
  113. #check for reply
  114. if [ -z "$check" ] && [ -z "$check2" ]
  115. then
  116.         echo "MezelPi is not responding!"
  117.         if [ -e ${dir}dhcp.on ]
  118.         then
  119.         echo "DHCP server already enabled. No changes or notifications performed."
  120.         exit 0
  121. else
  122.         echo "Generating lock file"
  123.         touch ${dir}dhcp.on
  124.         echo "Done."
  125. #        sudo pihole -a enabledhcp "192.168.1.2" "192.168.1.110" "192.168.1.1" "1h" "local"
  126.         sudo pihole -a enabledhcp "192.168.1.2" "192.168.1.110" "192.168.1.1" "1" "local"
  127.         sudo rm -f /etc/dnsmasq.d/02-pihole-dhcp.conf
  128.         sudo rm -f /etc/dnsmasq.d/03-pihole-wildcard.conf
  129.         sudo rm -f /etc/dnsmasq.d/04-pihole-static-dhcp.conf
  130.         sudo rm -f /etc/dnsmasq.d/05-3-dns.conf
  131.         fi
  132.  
  133. #check for 04-pihole-static-dhcp.conf
  134. if [ -e /etc/dnsmasq.d/04-pihole-static-dhcp.conf ]
  135. then
  136.     echo "04-pihole-static-dhcp.conf present. No changes made."
  137. else
  138.     echo "04-pihole-static-dhcp.conf missing. Generating file."
  139.  
  140. #pass strings in order for existing dhcp clients to keep IPs
  141. pass
  142. echo "Done."
  143.  
  144. #restart dns and activate DHCP server
  145. echo "Restarting DNS."
  146. #pihole restartdns
  147. sleep 2
  148. sudo pkill pihole-FTL
  149. sleep 2
  150. sudo systemctl restart pihole-FTL.service
  151. echo "Done."
  152.  
  153. #notify
  154. #sleep 10
  155. (pushover pihole "MezelPi is not responding,  backup DHCP is<font color="red"><b> ON</font></b>." &)
  156. echo "Notification sent."
  157. fi
  158.  
  159. else
  160. if [ -z "$check2" ] && [[ $check == *"offline"* ]]
  161.  
  162.     then
  163.         echo "MezerlPi is up with errors!"
  164.         if [ -e ${dir}dhcp.on ]
  165.         then
  166.         echo "DHCP server already enabled. No changes or notifications performed."
  167.         exit 0
  168.   else
  169.         echo "Generating lock file"
  170.         touch ${dir}dhcp.on
  171.         echo "Done."
  172. #        sudo pihole -a enabledhcp "192.168.1.200" "192.168.1.251" "192.168.1.1" "1h" "local"
  173. #        sudo pihole -a enabledhcp "192.168.1.200" "192.168.1.251" "192.168.1.1" "1" "local"
  174.         sudo pihole -a enabledhcp "192.168.1.2" "192.168.1.110" "192.168.1.1" "1" "local"
  175.         sudo rm -f /etc/dnsmasq.d/02-pihole-dhcp.conf
  176.         sudo rm -f /etc/dnsmasq.d/03-pihole-wildcard.conf
  177.         sudo rm -f /etc/dnsmasq.d/04-pihole-static-dhcp.conf
  178.         sudo rm -f /etc/dnsmasq.d/05-3-dns.conf
  179.         fi
  180.  
  181. #check for 04-pihole-static-dhcp.conf
  182.  if [ -e /etc/dnsmasq.d/04-pihole-static-dhcp.conf ]
  183. then
  184.     echo "04-pihole-static-dhcp.conf present. No changes made."
  185. else
  186.     echo "04-pihole-static-dhcp.conf missing. Generating file."
  187.  
  188. #copy config files in order for existing dhcp clients to keep IPs
  189. pass
  190. echo "Done."
  191.  
  192. #restart dns and activate DHCP server
  193. echo "Restarting DNS."
  194. sudo pkill pihole-FTL
  195. sleep 2
  196. sudo systemctl start pihole-FTL.service
  197. echo "Done."
  198. #send a pushover notification  
  199. (pushover pihole "MezelPi is in in error state(FTL offline), backup DHCP is<font color="red"><b> ON</font></b>." &)
  200. echo "Notification sent."
  201. exit 0
  202.  fi
  203. else
  204.     echo "MezelPi is Alive!"
  205.  
  206. #remove DHCP settings.
  207.  
  208.  if [ -e /etc/dnsmasq.d/02-pihole-dhcp.conf ]
  209.  then
  210. echo "Removing settings."
  211. sudo rm -f /etc/dnsmasq.d/02-pihole-dhcp.conf
  212. sudo rm -f /etc/dnsmasq.d/03-pihole-wildcard.conf
  213. sudo rm -f /etc/dnsmasq.d/04-pihole-static-dhcp.conf
  214. sudo rm -f /etc/dnsmasq.d/dhcp-join.conf
  215. sudo rm -f /etc/dnsmasq.d/05-3-dns.conf
  216.  
  217. #restart pihole / disable DNS seerver
  218. echo "Restarting DNS."
  219. sudo pihole -a disabledhcp
  220. sleep 2
  221. sudo pkill pihole-FTL
  222. sleep 2
  223. sudo systemctl start pihole-FTL.service
  224. sudo rm -f ${dir}dhcp.on
  225. echo "Done."
  226.  
  227. #send pushover notification
  228. (pushover pihole "MezelPiHole backup DHCP is<font color="lime"><b> OFF</font></b>." &)
  229. echo "Notification sent."
  230. else
  231.     echo "Lock file already deleted. No action performed."
  232.  fi
  233.  fi
  234. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement