I am trying to use a Python Custom Sensor to ssh to a device, run a single command, capture the output and return that to the channel sensor.

My SSH code works great on its own :-)

Ideally I like to pass the username and password to the script also but for now its static in the initiateSSH function

When I set up the Sensor and pass the %host parameter to this script PRTG throws out an error:

XML: Structural error in xml file, 8 open items. -- JSON: The returned json does not match the expected structure (Invalid JSON.). (code: PE231)

This is my code, any help would be greatly appreciated.

#Imports
import logging
import time
import datetime
import sys
import subprocess
import os
import sys
import json


def initiateSSH (host):

	import paramiko
	import cmd
	import time
	import sys
	import socket
	import os

	usrpassword = 'password'
	cmd = 'show clock’
	delay = int(5)

	ssh = paramiko.SSHClient()
	ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

	ssh.connect(host, username='admin', password=usrpassword)
	chan = ssh.invoke_shell()
	time.sleep(delay)

	#Send command:

	chan.send(cmd)
	time.sleep(delay)
	chan.send('\n')
	resp = chan.recv(99999)
	logging.info (resp)
	time.sleep(delay)
	chan.send('\n')
	resp = chan.recv(99999)

	return resp
	ssh.close


#get CustomSensorResult from paepy package

from paepy.ChannelDefinition import CustomSensorResult

if __name__ == "__main__":
    # interpret first command line parameter as json object
    data = json.loads(sys.argv[1])

    #SSH to Host

    sshGet = initiateSSH(data)

    #Return Result to PRTG

    result = CustomSensorResult("Result from SSH " + sshGet)

    # add primary channel

    result.add_channel(channel_name="Percentage", unit="Percent", value=87, is_float=False, 
    primary_channel=True,
                       is_limit_mode=True, limit_min_error=10, limit_max_error=90,
                       limit_error_msg="Percentage too high")

    # add additional channel
    result.add_channel(channel_name="Response Time", unit="TimeResponse", value="4711")

    # print sensor result to std
    print(result.get_json_result())

Article Comments

Hello fishfingerbandit,

Thank you very much for your contact.

We'll need additional information from the sensors, that's why I'd like to ask to contact use directly via email to support@paessler.com.

Please enable the "Write sensor result to disk" option from the sensor's "Settings" tab. After the next sensor scan, PRTG will create and store additional log files for debugging purposes in the "/Logs (Sensor)" sub directory in the PRTG data folder on the Probe system the sensor is running on. The file name will contain the sensor ID such as "Result of Sensor [ID].x". The sensor ID can be found in the sensor's "Overview" tab.

Please attach this file when contacting us.

Thank you in advance.
Sebastian


Dec, 2017 - Permalink