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

Is there a shell script example for the SSH Script sensor?

Votes:

1

I want to use the SSH Script sensor. Is there an example? What is the return format?

help linux prtg script sh shell ssh unix

Created on Aug 2, 2012 9:49:14 AM by  Daniel Zobel [Product Manager]

Last change on Dec 10, 2020 3:16:16 PM by  Brandy Greger [Paessler Support]



16 Replies

Accepted Answer

Votes:

2

This article applies as of PRTG 22

SSH Script sensor

The expected return format for the scripts used with the SSH Script sensor is documented in the PRTG Manual: Application Programming Interface (API) Definition.


Snippet from the PRTG API documentation

The returned data for standard SSH Script sensors must be in the following format: returncode:value:message

Value must be a 64-bit integer or float and will be used as the resulting value for this sensor (for example bytes, milliseconds, etc.), message can be any string and will be stored in the database.

The SSH script's "returncode" has to be one of the following values:

ValueDescription
0OK
1WARNING
2System Error (for example a network/socket error)
3Protocol Error (for example a web server returns a 404)
4Content Error (for example a web page does not contain a required word)



Example shell script

This example script will run on the target host and check if a service on a specific port is running. Change it to your liking.

#!/bin/bash

port="80"
service="WEB"

NETSTAT=`which netstat`
ID=`which id`

die(){
	exit 999
}

is_root(){
	local id=$($ID -u)
	if [ $id -ne 0 ] 
	then
		echo "4:500:You have to be root to run $0."    # returncode 4 = put sensor in DOWN status
		die
	fi
}

preparation(){
	if [ ! -x $NETSTAT ]
	then 
		echo "2:500:netstat not found."
		die
	fi
	if [ ! -x $ID ]
	then
		echo "2:500:id not found."    # returncode = 2 = put sensor in DOWN status
		die
	fi
	is_root
}

check_service(){
	serviceIsRunning=false
	openPorts=$($NETSTAT -tulpn | grep -vE '^Active|Proto' | grep 'LISTEN' | awk '{ print $4}' | awk -F: '{print $NF}' | sed '/^$/d' | sort -u)
	for openPort in $openPorts
	do
		if [ "$port" == "$openPort" ]
		then
			serviceIsRunning=true
			echo "0:200:$service is running."    # returncode 0 = put sensor in OK status
			break
		fi
	done
	if [ $serviceIsRunning == false ]
	then
		echo "1:404:$service is not running."    # returncode 1 = put sensor in WARNING status
	fi
}

main(){
	preparation
	check_service
}

main

Created on Aug 2, 2012 10:03:12 AM by  Daniel Zobel [Product Manager]

Last change on Jan 3, 2023 8:03:14 AM by  Brandy Greger [Paessler Support]



Votes:

1

These few lines cover a lot of simple service check needs.

Here's a super simple service check script I'm using on Ubuntu systems. When you set it up in PRTG, just put the service name you want to check in the parameter box. That becomes $1 in the script. $? is the status of the service as reported by the service command. This is all assembled in the echo to be formatted to make PRTG show pretty and informative results.

#!/bin/sh

service $1 status 2>&1 1>/dev/null

if [ $? -ne 0 ]; then
  echo "1:$?:$1 down"
else
  echo "0:$?:OK"
fi

Created on Apr 2, 2014 7:48:37 PM

Last change on Apr 3, 2014 7:07:26 AM by  Konstantin Wolff [Paessler Support]



Votes:

2

You can do it with SSH-Skript (advaned) in chanels

#!/bin/bash
#
# Array der Service Dienste
array=( apache2 c-icap clamav-daemon clamav-freshclam mysql qlproxy ssh )

function service_status {
  service $1 status 2>&1 1>/dev/null
	if [ $? -ne 0 ]; then
		echo -n "1:$?:$1 down"
	else
		echo -n "0:$?:OK"
	fi
}

echo "<prtg>"

# <-- Start
for i in "${array[@]}"
do

echo -n "   <result>
       <channel>Service: $i</channel>
         <value>"
service_status $i
echo "</value>
         <ValueLookup>linux.ssh</ValueLookup>
   </result>"
done
# End -->
echo "</prtg>"
exit

Output looks like:

root@proxy:~# /var/prtg/scriptsxml/service.sh
<prtg>
   <result>
       <channel>Service: apache2</channel>
         <value>0:1:OK</value>
         <ValueLookup>icr.linux.ssh</ValueLookup>
   </result>
   <result>
       <channel>Service: qlproxy</channel>
         <value>0:1:OK</value>
         <ValueLookup>icr.linux.ssh</ValueLookup>
   </result>
   <result>
       <channel>Service: ssh</channel>
         <value>0:1:OK</value>
         <ValueLookup>icr.linux.ssh</ValueLookup>
   </result>
</prtg>

Lookup Looks:

<?xml version="1.0" encoding="UTF-8"?>
  <ValueLookup id="icr.linux.ssh" desiredValue="0" ............... >
    <Lookups>
      <SingleInt state="Ok" value="0">
        running
      </SingleInt>
      <SingleInt state="warning" value="1">
        stopped
      </SingleInt>
  </Lookups>
  </ValueLookup>

Created on Jul 15, 2014 1:49:06 PM



Votes:

0

Hi

I have created a script similar to the one Josiah posted above but it always seems to return a value of zero when using it against the ssh sensor in prtg even when the process is stopped, please see below script i have written:

#!/bin/bash

#check if clxauthintn is running
if pgrep clxauthintn >/dev/null 2>&1
  then
     echo "0:$?:OK"
  else
     echo "1:$?:$1 down"
fi

Anyone have any ideas at all why this would not be working?

Created on Oct 16, 2015 3:01:01 PM

Last change on Oct 19, 2015 8:24:31 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

@Andrew Jones: What are the results when you run the script from the shell as the user you are logged in? Also, please try logging in with the user provided in PRTG and run the script again. What are the results here?

Created on Oct 19, 2015 8:25:57 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

Does the user running the script not have pgrep in their path? I believe "if" would consistently throw a 0 if pgrep wasn't found. Otherwise, I'm guessing some other environment inconsistency.

Created on Oct 19, 2015 3:15:17 PM



Votes:

0

How do we force a DOWN state with this sensor? I don't see a way.

Created on Jan 29, 2016 6:42:39 PM



Votes:

0

@Kyle: What exact sensor are you referring to? The EXE/Script or the EXE/Script Advanced Sensor?

Created on Feb 1, 2016 3:15:18 PM by  Konstantin Wolff [Paessler Support]



Votes:

0

I wrote a script in Python, just test. And the sensor return: Received empty SSH response. Please check the sensor log.

Even with the print "0:1:OK".

# python new_alert_monit.py 
0:1:OK

EDIT BY MOD: formatting!

Created on Jul 21, 2016 1:44:15 PM

Last change on Jul 22, 2016 1:14:31 PM by  Konstantin Wolff [Paessler Support]



Votes:

0

@ Rodrigofl: Please try wrapping up your Python script in bash. Means adding something like this which calls your Python script:

#!/bin/bash
/usr/bin/python /var/prtg/scripts/new_alert_monit.py

This should work.

Created on Jul 22, 2016 1:16:57 PM by  Konstantin Wolff [Paessler Support]



Votes:

1

Here is a simple script for advanced SSH script bandwidth monitoring for Eth0. It is possible to add additional channels for other interfaces. The measure is based on ifconfig output and counter sensor mode:

#!/bin/bash
#
echo -n "<prtg>
  <result>
    <channel>Eth0 RX</channel>
    <unit>BytesBandwidth</unit>
    <VolumeSize>KiloBit</VolumeSize>
    <mode>Difference</mode>
    <value>"
ifconfig eth0 | gawk '
/RX bytes/ {\
  sub (/.*RX bytes:/,"");\
  sub(/ \(.*:/,"</value>\n  </result>\n  <result>\n    <channel>Eth0 TX</channel>\n    <unit>BytesBandwidth</unit>\n    <VolumeSize>KiloBit</VolumeSize>\n    <mode>Difference</mode>\n    <value>");\
  sub(/ \(.*/,"</value>\n  </result>\n</prtg>");print $0}
' 

The output looks like this:

<prtg> <result> <channel>Eth0 RX</channel> <unit>BytesBandwidth</unit> <VolumeSize>KiloBit</VolumeSize> <mode>Difference</mode> <value>6174822801</value> </result> <result> <channel>Eth0 TX</channel> <unit>BytesBandwidth</unit> <VolumeSize>KiloBit</VolumeSize> <mode>Difference</mode> <value>144638053073</value> </result> </prtg>

ifconfig in newer versions of Linux produces a different output. This is a new version of the SSH script for bandwidth monitoring:

#!/bin/bash
#
ifconfig eth0 | gawk '
/[RT]X packets/ {if (!tx) print "<prtg>";
print "  <result>\
\n    <channel>eth " $1 "</channel>\
\n    <unit>BytesBandwidth</unit>\
\n    <VolumeSize>KiloBit</VolumeSize>\
\n    <mode>Difference</mode>\
\n    <value>" $5 "</value>\
\n  </result>";
if (tx++) print "</prtg>"}'

Created on May 17, 2017 12:38:39 PM

Last change on Jul 15, 2022 4:37:56 AM by  Felix Wiesneth [Paessler Support]



Votes:

0

Josiah Ritchie: So it will be more correct?

#!/bin/sh

service $1 status 2>&1 1>/dev/null

if [ "$?" -ne 0 ]; then
  echo -n "0:$?:OK"
else
  echo -n "1:$?:$1 DOWN"
fi

Created on Feb 11, 2019 10:54:34 AM

Last change on Feb 11, 2019 10:56:56 AM by  Sven Roggenhofer [Paessler Technical Support]



Votes:

0

Script in post 195361 working, script post 192199 not work

Created on Feb 19, 2019 11:24:14 AM



Votes:

0

Here is the first script, modified to produce the correct return codes for you...

(Note that the exit 0 return code is produced automatically, if the script exits successfully. i.e the port is listening on 80)

#!/bin/bash

port="80"
service="WEB"

NETSTAT=`which netstat`
ID=`which id`

die(){
	exit 999
}

is_root(){
	local id=$($ID -u)
	if [ $id -ne 0 ] 
	then
		echo "4:500:You have to be root to run $0."    # returncode 4 = put sensor in DOWN status
		exit 4
                die
	fi
}

preparation(){
	if [ ! -x $NETSTAT ]
	then 
		echo "2:500:netstat not found."
		die
	fi
	if [ ! -x $ID ]
	then
		echo "2:500:id not found."    # returncode = 2 = put sensor in DOWN status
                exit 2
		die
	fi
	is_root
}

check_service(){
	serviceIsRunning=false
	openPorts=$($NETSTAT -tulpn | grep -vE '^Active|Proto' | grep 'LISTEN' | awk '{ print $4}' | awk -F: '{print $NF}' | sed '/^$/d' | sort -u)
	for openPort in $openPorts
	do
		if [ "$port" == "$openPort" ]
		then
			serviceIsRunning=true
			echo "0:200:$service is running."    # returncode 0 = put sensor in OK status
			break
		fi
	done
	if [ $serviceIsRunning == false ]
	then
		echo "1:404:$service is not running."    # returncode 1 = put sensor in WARNING status
                exit 1
	fi
}

main(){
	preparation
	check_service
}

main

Created on May 28, 2019 7:02:58 AM

Last change on May 28, 2019 7:02:58 AM



Votes:

0

So most of these solutions didnt work for me, so here is a more simple code.

systemctl is-active --quiet shhd && echo "1:$?:shhd down" || echo "0:$?:shhd up"

Save as *.sh, and make sure the file is in /var/prtg/scripts/. Also make sure the file has execution rights.

This only monitors the shhd service tho, if you want to have it for multiple services you can use:

systemctl is-active --quiet $1 && echo "1:$?:$1 down" || echo "0:$?:$1 up"

Make sure to fill in the Parameters field in PRTG.

Created on Jun 5, 2020 11:41:38 AM



Votes:

0

I am able to use this script to monitor Redhat Linux process. Below is the details for reference:

On target machines where you want to monitor the Linux script. Create a folder in /var/prtg/scripts. Copy the script mentioned in first response under below link

https://kb.paessler.com/en/topic/39513-is-there-a-shell-script-example-for-the-ssh-script-sensor

and paste in to a file with your preferred name. Suppose we made a file httpd.sh so its like /var/prtg/scripts/httpd.sh. Now modify "Port" as per your process requirement. In our case we select port 80 as we need to monitor web service on target Linux machine. Give executable permission and disable the firewall & SElinux temporary

Now come to PRTG console and add a sensor name "ssh script" under your Device, then here you click on "Script box" drop down menu and select httpd.sh script which you placed earlier. Under the drop down menu you will find all scripts which you placed on target Linux machine. You may also configure other options too depends on your need. Click on "Create". This will add a sensor under your device and become in "Green" box if the process is running otherwise it will be "Yellow" in color

Created on Jan 9, 2023 12:12:20 PM




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.