What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

How can I monitor and automatically restart a service on a Linux host?

Votes:

0

I know it is possible to monitor certain services with SNMP and the SNMP Library sensor, but I want to restart the service in case of an error, too. Additionally, I do not want to use the SSH Script sensor for performance reasons.

Is there a solution for that particular scenario?

cron crontab custom-sensor http-push linux prtg service

Created on Aug 11, 2016 9:30:10 AM by  Dariusz Gorka [Paessler Support]

Last change on Jan 4, 2023 2:40:24 PM by  Brandy Greger [Paessler Support]



5 Replies

Accepted Answer

Votes:

1

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.

Created on Aug 11, 2016 1:10:31 PM by  Dariusz Gorka [Paessler Support]

Last change on Jan 4, 2023 2:41:27 PM by  Brandy Greger [Paessler Support]



Votes:

0

Hey PRTG Team,

I'm currently try to use the script with my values but I have only "No Data". If I use the part for the cronjob I become:

/home/scripts/prtg.sh sinusbot_fg http://xyz:8443 EAF84951-0F8E-4A69-8F75-0AA47E48B6B9
--2017-09-14 22:21:49--  http://xyz:8443/EAF84951-0F8E-4A69-8F75-0AA47E48B6B9?content=%3Cprtg%3E%3Cresult%3E%3Cchannel%3Esinusbot_fg%20status%3C/channel%3E%3Cvalue%3E1%3C/value%3E%3C/result%3E%3Ctext%3EService:%20sinusbot_fg%20is%20running!%3C/text%3E%3C/prtg%3E
Resolving xyz (xyz)... 62.138.206.x
Connecting to xyz (XYZ)|62.138.206.x|:8443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily

Location: /public/login.htm?loginurl=%2FEAF84951-0F8E-4A69-8F75-0AA47E48B6B9%3Fcontent%3D%253Cprtg%253E%253Cresult%253E%253Cchannel%253Esinusbot_fg%2520status%253C%2Fchannel%253E%253Cvalue%253E1%253C%2Fvalue%253E%253C%2Fresult%253E%253Ctext%253EService%3A%2520sinusbot_fg%2520is%2520running%21%253C%2Ftext%253E%253C%2Fprtg%253E&errormsg= [following]

--2017-09-14 22:21:49--  http://xyz:8443/public/login.htm?loginurl=%2FEAF84951-0F8E-4A69-8F75-0AA47E48B6B9%3Fcontent%3D%253Cprtg%253E%253Cresult%253E%253Cchannel%253Esinusbot_fg%2520status%253C%2Fchannel%253E%253Cvalue%253E1%253C%2Fvalue%253E%253C%2Fresult%253E%253Ctext%253EService%3A%2520sinusbot_fg%2520is%2520running%21%253C%2Ftext%253E%253C%2Fprtg%253E&errormsg=
Connecting to xyz (XYZ)|62.138.206.x|:8443... connected.
HTTP request sent, awaiting response... 200 OK

How can I fix it?

Thanks for the help :)

Created on Sep 14, 2017 8:31:21 PM

Last change on Sep 15, 2017 7:57:18 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Hi there,

Please note to use the configured port within the Push Data Advanced Sensor. You seem to use the HTTP Port of your webinterface. So if the configured port within the sensor is "5050", then use "http://xyz:5050" as the PRTG-Host.

Best regards.

Created on Sep 15, 2017 7:59:13 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Hi PRTG team, can I monitor more than 1 service using this script? if is possible whitch is the best way?

Created on Apr 29, 2022 11:53:44 AM



Votes:

0

Hi Hian,

Thank you for your message.

Regarding what you would like to achieve, this requires a few modifications in the script and more specifically about how it would handle services.

For example, you would have to pass a list of services as argument, which will be stored in the variable service. Then, you need to use a for loop to :

  • check if the service X is up and running
  • add the following content for each service in a variable (with SERVICE NAME and STATE replaced accordingly)
<result><channel>SERVICE NAME</channel><value>STATE</value></result>

At the end, the variable should contain the data that will be sent to PRTG. The format must be the following: https://www.paessler.com/manuals/prtg/custom_sensors#advanced_sensors.

Finally, when all services are processed simply execute a HTTP request to push the data. Please, note that you have to send a value for each channel within each request to avoid getting the value 0 for those missing.

Regards.

Created on Apr 29, 2022 1:23:07 PM by  Florian Lesage [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.