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


How can I get host Device IP in powershell without hard-coding-in Device ID or Sensor ID?

Votes:

0

Your Vote:

Up

Down

Hi there, I would appreciate any assistance as I've spent two days trying to find solutions to my problem online.

The purpose of my powershell sensor (for PRTG) is to collect data from a corporate API and display the relevant stats in PRTG. Here's the problem(s): - I need the device IP (not the server IP) as it forms part of the API URL. - The device on which the sensor will be placed is very likely to be cloned in the future (several times), so I cannot hard-code the device ID. New clones will have their own API and a different IP address (API and device IPs are the same). - Similarly, I can't hard-code in the sensor ID - Since there are alot of other sensors, my manage does not want me to enter the IP/device-id as a param because the person doing the cloning could easily forget to update it. So no params.

From what I've researched online, I can get to the device IP if only I could just fetch the Sensor ID or device ID, but I don't know how to do that in powershell (and I didn't find any examples online that did either).

Thanks

device ip powershell prtg

Created on Apr 20, 2017 11:34:40 AM by  MichaelMcQuirk (0) 1



Best Answer

Accepted Answer

Votes:

0

Your Vote:

Up

Down

Dear MichaelMcQuirk

This looks like a rather complex issue. Managing device tree objects with scripts in in principle possible, but PRTG is optimized to be used manually via the interface. Especially if you clone objects, it can get complicated.

Created on Apr 21, 2017 12:46:11 PM by  Arne Seifert [Paessler Support]



9 Replies

Votes:

0

Your Vote:

Up

Down

Dear MichaelMcQuirk

There is no easy way to do that. You could use the API to generate a list which contains object ids and sensor names and then look the ip up by its name, or by its comments.

/api/table.csv?id=0&content=sensors&columns=objid,sensor=htmllong,status,message,lastvalue,comments&count=*

You can use the API to get the parent object (the device id of a sensor)

/api/getobjectstatus.htm?id=2123&name=parentid

(Of course, the id value has to be replaced with the actual sensor ID.)

You can use the API to extract the host name or IP (whatever is entered) for a device

/api/getobjectproperty.htm?id=2001&name=host

(Again, the id value has of course to be the correct ID.)

Created on Apr 20, 2017 12:17:11 PM by  Arne Seifert [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Thanks Arne,

Unfortunately, as mentioned in the original post, I cannot hard code in a sensor ID as I'm sure the ID will be different for new clones. If there were some way to get the ID using PowerShell, that would pretty much solve everything (indirectly).

Created on Apr 20, 2017 12:35:56 PM by  MichaelMcQuirk (0) 1



Votes:

0

Your Vote:

Up

Down

Dear MichaelMcQuirk

See the first API call I mentioned. You could generate a list of all sensor IDs in the script and identify the sensor by the name, or comments. (Somehow you have to have a unique property.) That you can use the ID that search yielded. Do you have anything to identify the sensor(s) in question?

Created on Apr 20, 2017 2:48:19 PM by  Arne Seifert [Paessler Support]



Votes:

0

Your Vote:

Up

Down

@Arne, the problem I have is that almost nothing will be unique as the sensor is expected to be cloned multiple times. From what I've heard, the name and ID will change (incrementally for each new clone) but I'll still have no way of telling which one of the sensors the current PowerShell script is running for/under, which is necessary for the sensor to do its thing.

The closest solution I have found to my problem is a Python sensor example which manages to get its device id. Unfortunately I'm not experienced in python and recoding my PowerShell sensor (as simple as it is) could take up too much time. Here's the code for that Python Sensor:

# -*- coding: utf-8 -*-

import sys
import json
# 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])

    # create sensor result
    result = CustomSensorResult("This sensor is added to " + data['host'])

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

Created on Apr 21, 2017 6:28:05 AM by  MichaelMcQuirk (0) 1



Accepted Answer

Votes:

0

Your Vote:

Up

Down

Dear MichaelMcQuirk

This looks like a rather complex issue. Managing device tree objects with scripts in in principle possible, but PRTG is optimized to be used manually via the interface. Especially if you clone objects, it can get complicated.

Created on Apr 21, 2017 12:46:11 PM by  Arne Seifert [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Can you explain why you cannot use a placeholder as a parameter? for example use %host, so when you clone to sensor (you are forced to enter a new hostname / IP) the custom exe script will continue to point to the correct host.

https://kb.paessler.com/en/topic/373-what-placeholders-can-i-use-with-prtg

Created on Apr 26, 2017 5:16:43 AM by  AndrewG (1,781) 2 2



Votes:

0

Your Vote:

Up

Down

@AndrewG, thanks for the suggestion, I did not consider placeholders before-hand.

I've done some looking into using placeholders and unfortunately, I don't think it'll help. We clone from working/existing sensors and not templates, so (if my understanding that placeholders are only really used in templates, is correct) it won't be applicable.

Busy learning python at the moment to convert our current powershell code to python.

Created on Apr 26, 2017 12:45:36 PM by  MichaelMcQuirk (0) 1



Votes:

0

Your Vote:

Up

Down

@MichaelMcQuirk, that is not the case with PRTG, placeholders do not "hardcode themselves" once you clone. They will stay as a placeholder variable for life.

Placeholders is certainly the way to go.

I use this method to pass the DeviceID through to a powershell script, this script does a few things, then updates the PRTG devices NOTES tab (based on the DeviceID I pass to the script) with the detailed results of the script. The script also returns the results normally to the PRTG sensor so I can see if its "up" or "down".

Created on Apr 28, 2017 7:43:17 AM by  AndrewG (1,781) 2 2



Votes:

0

Your Vote:

Up

Down

I highly recommend using the PRTG API for Powershell written by lordmilko. i've been using it for a few months & it makes it possible to use PRTG in an automated fashion that Paessler have no included.

PS Install Command: Install-Module -Name PrtgAPI

https://www.powershellgallery.com/packages/PrtgAPI/0.9.5

Created on Mar 22, 2019 1:15:11 AM by  claed (0)



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.