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

Python Custom Sensor - SSH to a device

Votes:

0

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())

python python-script-advanced-sensor ssh

Created on Dec 18, 2017 2:11:45 PM

Last change on Dec 18, 2017 10:27:36 PM by  Stephan Linke [Paessler Support]



1 Reply

Votes:

0

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 [email protected].

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

Created on Dec 20, 2017 10:40:32 AM by  Sebastian Kniege [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.