If you have critical services running on Linux machine, it is important to monitor their status and ensure they are running. That will also help in minimizing troubleshooting time, as you will know immediately the issue’s cause.
In this blog we will monitor Linux service using bash script and integrate it with PRTG network monitor, you can integrate the script in any monitoring tool if you know how the tool parse the data, so you can adjust the script accordingly.
1- Create a new path in the Linux machine /var/prtg/scripts as this is the path PRTG reads from
2- Create inside above path a file with .sh extension using vi editor, I will call mine servicemon.sh
vi servicemon.sh
3- Start writing the script: we will take the script in chunks to elaborate each part.
#!/bin/bash
name=swap.target
In the variable called “name”, we place the service name to be monitored. In this example, I am monitoring the service “swap.target”
if (systemctl is-active --quiet $name); then
if statement is to evaluate the service status. It will generate exit code 0 if service is running (indicate success), and non-zero exit code if the service is not running.
is-active is to check if the service is active, and –quiet is to suppress the output and not to generate any text.
echo "0:"$?":Service "$name" is running"
For PRTG to interpret the result we need to show it in formate “returncode:value:message” as described in this link.
If the previous if statement returns 0 (which means service is running) this “echo” command will print output in the format “returncode:value:message”.
So here:
- returncode we are setting it as 0 which evaluates to OK in PRTG as this statement will be executed in healthy state (when service is running).
- value is $? which is a special variable in shell scripting which holds the exit code of last command (in our case systemctl is-active –quiet $name).
- Remaining part is the message which will show the service name and that it is running.
The conations marks (“”) here are excluding out the variables $? and $name and include literal strings that are going to be printed as is.
else
echo "4:"$?":Service "$name" is not running"
fi
Similarly, In case service is not running, exit code of previous if statement will be non zero and it will go to else statement where:
- returncode will be 4 which will show the error state in PRTG.
- value is $? which is a special variable in shell scripting which holds the exit code of last command (in our case systemctl is-active –quiet $name).
- message will say that service is not running.
Full script will be at end of page.
4- After you save the file you need to make it executable:
chmod +x servicemon.sh
IMPORTANT Note: this article is made using Lab environment only shows basic steps needed on Linux to achieve the monitoring, in real environment, make sure that you assign specific service account to be used ONLY by monitoring tool and that you are giving it the most restricted permissions to run the script, that’s to ensure security precautions are in place.
5- Testing the script by running it manually:
Below is the output in case service is running.

Below is when service is stopped

Let’s now integrate this with PRTG
1- On PRTG make sure to give the SSH details on the credentials area of the needed device
2- Click on add sensor, and select SSH Script Sensor

3- Add the service name as sensor name, and select the script added on the device, then click create

4- Below is the sensor is up state when service is running

Below is the sensor in down state when service is not running

Full script: replace SERVICENAME with the service name you want to monitor
#!/bin/bash
name=SERVICENAME
if (systemctl is-active --quiet $name); then
echo "0:"$?":Service "$name" is running"
else
echo "4:"$?":Service "$name" is not running"
fi

Leave a comment