New Question
 
 
PRTG Network Monitor

Intuitive to Use.
Easy to manage.

200.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free PRTG
Download >>

 

What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

 

Top Tags


View all Tags


Is there a shell script example for the PRTG SSH Script Sensor?

Votes:

1

Your Vote:

Up

Down

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 [Paessler Support]

Last change on Aug 24, 2017 3:18:56 PM by  Gerald Schoch [Paessler Support]



13 Replies

Accepted Answer

Votes:

2

Your Vote:

Up

Down

This article applies to PRTG Network Monitor 12.3 or later

SSH Script Sensor

The expected return format for the scripts used with PRTG's SSH Script sensor are documented in the API documentation.


Snippet from the PRTG API Documentation

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

Value has to be a 64 bit integer or float and will be used as the resulting value for this sensor (e.g. 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 (e.g. a network/socket error)
3Protocol Error (e.g. web server returns a 404)
4Content Error (e.g. a web page does not contain a required word)


For more details, please see:


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 [Paessler Support]

Last change on Aug 10, 2018 12:08:56 PM by  Brandy Mauff [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Is there a way to make use of channels? I'd like to monitor several related daemons and having one sensor with a channel per daemon seems like a good way to keep the noise down and bring some continuity.

Created on Feb 5, 2013 2:37:10 PM by  Josiah Ritchie (110) 1 1



Votes:

0

Your Vote:

Up

Down

Sorry, only one channel per sensor is possible on this sensor type.

Created on Feb 5, 2013 3:40:54 PM by  Torsten Lindner [Paessler Support]



Votes:

0

Your Vote:

Up

Down

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 by  Josiah Ritchie (110) 1 1

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



Votes:

1

Your Vote:

Up

Down

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 by  Andreas Brauckmann (10) 1



Votes:

0

Your Vote:

Up

Down

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 by  Andrew Jones (0) 1

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



Votes:

0

Your Vote:

Up

Down

@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

Your Vote:

Up

Down

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 by  Josiah Ritchie (110) 1 1



Votes:

0

Your Vote:

Up

Down

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 by  Kyle Tucker (70) 1 1



Votes:

0

Your Vote:

Up

Down

@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

Your Vote:

Up

Down

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 by  Rodrigofl (0) 1

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



Votes:

0

Your Vote:

Up

Down

@ 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:

0

Your Vote:

Up

Down

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>

Created on May 17, 2017 12:38:39 PM by  pik256 (0)

Last change on May 18, 2017 10:08:44 AM by  Luciano Lingnau [Paessler Support]



Please log in or register to enter your reply.


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.