Advertisement
DGorka

Autorestart service and send status to PRTG

Aug 11th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.85 KB | None | 0 0
  1. #!/bin/bash
  2. #___ ___ _____ ___
  3. #| _ \ _ \_   _/ __|
  4. #|  _/   / | || (_ |
  5. #|_| |_|_\ |_| \___|
  6. #    NETWORK MONITOR
  7. #-------------------
  8. #(c) 2016 Dariusz Gorka, Paessler AG
  9. #
  10. #This script checks if a certain service us running.
  11. #The script also tries to restart the service if it is not started.
  12. #
  13.  
  14. #Enter the correct process name. (ps -e)
  15. service=$1
  16. #Enter the server address of your PRTG, including HTTPS/HTTP and the sensor port.
  17. prtghost=$2
  18. #Enter the Identification Token of the HTTP Push Data Advanced sensor.
  19. identtoken=$3
  20.  
  21. #Check if process is running
  22. if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
  23.   then
  24.     #Send response to PRTG that the service is running.
  25.     wget -O/dev/null "$prtghost/$identtoken?content=<prtg><result><channel>$service status</channel><value>1</value></result><text>Service: $service is running!</text></prtg>"
  26.   else
  27.     #Send response to PRTG that the service is not started.
  28.     wget -O/dev/null "$prtghost/$identtoken?content=<prtg><result><channel>$service status</channel><value>0</value></result><text>Service: $service is down, but will restart!</text></prtg>"
  29.     #Try to restart the service
  30.     /etc/init.d/$service start
  31.  
  32.     #Check if restart was successfully
  33.     if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
  34.       then
  35.         #Send response to PRTG that the restart was successfully
  36.         wget -O/dev/null "$prtghost/$identtoken?content=<prtg><result><channel>$service status</channel><value>1</value></result><text>Service: $service restarted properly!</text></prtg>"
  37.       else
  38.         #Send response to PRTG that the restart was not succesfully
  39.         wget -O/dev/null "$prtghost/$identtoken?content=<prtg><result><channel>$service status</channel><value>0</value></result><text>Service: $service can't restart properly! Please take action!</text></prtg>"
  40.       fi
  41. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement