This article applies as of PRTG 22
How to monitor, automatically restart, and report the current status of a Linux service
To monitor a service, restart it automatically, and push the current status to PRTG with HTTP, use the script below.
- The script basically checks if the desired service is available or not.
- If the service is up, the script sends a report to the PRTG core server with the value "1".
- Value "1" means that the service is up and value "0" means that the service is down or could not properly restart.
- The script also sends sensor messages like $service is running, $service is down, $service restarted properly, and $service can't restart properly.
How to use the script
Save the following script on the target system, where it can be executed by CRON.
#!/bin/bash
# ____ ____ ____________
# / __ \/ __ \/_ __/ ____/
# / /_/ / /_/ / / / / / __
# / ____/ _, _/ / / / /_/ /
#/_/ /_/ |_| /_/ \____/
# NETWORK MONITOR
#-------------------
#(c) 2016 Dariusz Gorka, Paessler AG
#
#This script checks if a certain service is running.
#The script also tries to restart the service if it is not started.
#
#Enter the correct process name. (ps -e)
service=$1
#Enter the server address of your PRTG, including HTTPS/HTTP and the sensor port.
prtghost=$2
#Enter the Identification Token of the HTTP Push Data Advanced sensor.
identtoken=$3
#Check if process is running
if (( $(pgrep -x $service | wc -l) > 0 ))
then
#Send response to PRTG that the service is running.
wget -O/dev/null "$prtghost/$identtoken?content=<prtg><result><channel>$service status</channel><value>1</value></result><text>Service: $service is running!</text></prtg>"
else
#Send response to PRTG that the service is not started.
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>"
#Try to restart the service
/etc/init.d/$service start
#Check if restart was successfully
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
#Send response to PRTG that the restart was successfully
wget -O/dev/null "$prtghost/$identtoken?content=<prtg><result><channel>$service status</channel><value>1</value></result><text>Service: $service restarted properly!</text></prtg>"
else
#Send response to PRTG that the restart was not successful
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>"
fi
fi
Create the CRONTAB below on your Linux server to use the script. To open the CRONTAB file, enter the command crontab -e as root and paste the code into the last line.
*/5 * * * * /PATH/TO/THESCRIPT/script.sh <SERVICENAME> <PRTGHOST> <IDENT-TOKEN>
Note: Keep in mind that you need to adjust the path and the parameters of the crontab, like in the examples below:
- <SERVICENAME>: snmpd
- <PRTGHOST>: https://example.com:5050 (use the Port-Host combination of the configured sensor here)
- <IDENT-TOKEN>: prtg-is-great
The response will be sent to the HTTP Push Data Advanced sensor. Create this sensor with the following settings:
- Sensor Name: <NAME OF THE SERVICE>
- Port: 5050 (Use a different port or Identification Token for every sensor.)
- Identification Token: <YOUR-IDENT-TOKEN>
- You can leave the default settings for all other options.
Set a limit on the sensor's $service status channel. To set this limit, select Enable alerting based on limits in the channel's settings and enter "1" in the Lower Error Limit (#) field.
Add comment