New Question
 
 
PRTG Network Monitor

Intuitive to Use.
Easy to manage.

300.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


Unable to modify Primary Channel using API

Votes:

0

Your Vote:

Up

Down

We are able to change Sensor Names and Priorities fine using the API (e.g. setobjectproperty.htm?id=2423&name=name&value=NewName) and these changes are reflected in the GUI. We are also able to retrieve the Primary Channel ID (ie getobjectproperty.htm?id=2423&name=primarychannel returns 0 or 1 depending which I set in the GUI).

However when I try to change the primary channel (setobjectproperty.htm?id=2423&name=primarychannel&value=1) the value is returned by getobjectpropertychanges (even if set to an invalid value e.g. 5) but is not changed in the GUI/Web interface. As soon as I navigate off the web page, the value is reset.

Any help/advice would be greatly appreciated.

On a side note, if there is a repository of pre-written API script templates available that would be very useful.

System: PRTG v8 with Support Contract running on Windows 2003. API calls being made from powershell on Windows 7.

api channel prtg

Created on Oct 8, 2010 2:51:27 PM by  jhsubscribe (50) 1 1



Best Answer

Accepted Answer

Votes:

0

Your Vote:

Up

Down

Thanks for the update Torsten.

For anyone else who comes across this post in the same problem, the best workaround I've found is to delete the sensors and recreate from template. You lose all your historic data.

Here's my hacked together powershell code for reference / save someone else the time:

$devices = PRTG_Get_Devices | ?{$_.device -like "*windows*"}
$template_sensor_id = 3832 # Memory sensor
PRTG_Duplicate_Sensors -devices $devices -template_sensor_id $template_sensor_id

##########  DUPLICATE TEMPLATE SENSOR TO ARRAY OF DEVICES
Function PRTG_Duplicate_Sensors
	{
	param ($devices,$template_sensor_id)
	#$template_sensor_id = 3744 
	$template_sensor = PRTG_Get_Sensors | ?{$_.objID -eq $template_sensor_id}
	foreach ($device in $devices)
		{	
		$existing_sensor = $null
		$existing_sensor = PRTG_Get_Sensors | ?{$_.device -eq $device.device} | ?{$_.sensor -eq $template_sensor.sensor}
		if (($existing_sensor -ne $null) -and ($device.device -ne $null))
			{
			"Sensor already exists on "+($device.device)+" - removing"
			$url = $prtg_api+"deleteobject.htm?id="+$existing_sensor.objid+"&approve=1"+$userstring	
			$text = (new-object system.net.webclient).downloadstring($url)
			}
		if ($device.device -ne $null)
			{
			"Sensor doesn't exist on "+$device.device+", creating sensor"
			$url = $prtg_api+"duplicateobject.htm?id="+$template_sensor_id+"&name="+$template_sensor.sensor+"&targetid="+$device.objid+$userstring
			$text = (new-object system.net.webclient).downloadstring($url)
			$text = $text.Substring(($text.IndexOf('/sensor.htm?id=')),19)
			$new_sensor_id = $text.Substring(($text.IndexOf('=')+1),4)
			# Start the senor
			$url = $prtg_api+"pause.htm?id="+$new_sensor_id+"+&action=1"+$userstring
			$text = PRTG_Get($url)
			# Run a scan
			$url = $prtg_api+"scannow.htm?id="+$new_sensor_id+$userstring
			$out = PRTG_Get($url)
			}
		else 
			{"device can't be found"}
		}
}


Function PRTG_Get
	{
	param ([String]$url)
	#$url = $prtg_api+"table.xml?content=sensors&columns=objid,group,device,sensor,status,message,lastvalue,priority,favorite"+$userstring
	$text = (new-object system.net.webclient).downloadstring($url)
	$text2 = $text.Replace("", "")
	$output=[xml]$text2
	$output
}

Function PRTG_Get_Sensors
	{
	$url = $prtg_api+$url_sensor+$userstring
	(PRTG_Get($url)).sensors.item
}


Created on Oct 11, 2010 2:04:27 PM by  jhsubscribe (50) 1 1

Last change on Oct 11, 2010 2:59:54 PM by  Torsten Lindner [Paessler Support]



4 Replies

Votes:

0

Your Vote:

Up

Down

Hello,

I'm afraid changing the Primary Channel with the API is not possible, due to internal regulations in PRTG.

Best Regards.

Created on Oct 11, 2010 12:29:36 PM by  Torsten Lindner [Paessler Support]



Accepted Answer

Votes:

0

Your Vote:

Up

Down

Thanks for the update Torsten.

For anyone else who comes across this post in the same problem, the best workaround I've found is to delete the sensors and recreate from template. You lose all your historic data.

Here's my hacked together powershell code for reference / save someone else the time:

$devices = PRTG_Get_Devices | ?{$_.device -like "*windows*"}
$template_sensor_id = 3832 # Memory sensor
PRTG_Duplicate_Sensors -devices $devices -template_sensor_id $template_sensor_id

##########  DUPLICATE TEMPLATE SENSOR TO ARRAY OF DEVICES
Function PRTG_Duplicate_Sensors
	{
	param ($devices,$template_sensor_id)
	#$template_sensor_id = 3744 
	$template_sensor = PRTG_Get_Sensors | ?{$_.objID -eq $template_sensor_id}
	foreach ($device in $devices)
		{	
		$existing_sensor = $null
		$existing_sensor = PRTG_Get_Sensors | ?{$_.device -eq $device.device} | ?{$_.sensor -eq $template_sensor.sensor}
		if (($existing_sensor -ne $null) -and ($device.device -ne $null))
			{
			"Sensor already exists on "+($device.device)+" - removing"
			$url = $prtg_api+"deleteobject.htm?id="+$existing_sensor.objid+"&approve=1"+$userstring	
			$text = (new-object system.net.webclient).downloadstring($url)
			}
		if ($device.device -ne $null)
			{
			"Sensor doesn't exist on "+$device.device+", creating sensor"
			$url = $prtg_api+"duplicateobject.htm?id="+$template_sensor_id+"&name="+$template_sensor.sensor+"&targetid="+$device.objid+$userstring
			$text = (new-object system.net.webclient).downloadstring($url)
			$text = $text.Substring(($text.IndexOf('/sensor.htm?id=')),19)
			$new_sensor_id = $text.Substring(($text.IndexOf('=')+1),4)
			# Start the senor
			$url = $prtg_api+"pause.htm?id="+$new_sensor_id+"+&action=1"+$userstring
			$text = PRTG_Get($url)
			# Run a scan
			$url = $prtg_api+"scannow.htm?id="+$new_sensor_id+$userstring
			$out = PRTG_Get($url)
			}
		else 
			{"device can't be found"}
		}
}


Function PRTG_Get
	{
	param ([String]$url)
	#$url = $prtg_api+"table.xml?content=sensors&columns=objid,group,device,sensor,status,message,lastvalue,priority,favorite"+$userstring
	$text = (new-object system.net.webclient).downloadstring($url)
	$text2 = $text.Replace("", "")
	$output=[xml]$text2
	$output
}

Function PRTG_Get_Sensors
	{
	$url = $prtg_api+$url_sensor+$userstring
	(PRTG_Get($url)).sensors.item
}


Created on Oct 11, 2010 2:04:27 PM by  jhsubscribe (50) 1 1

Last change on Oct 11, 2010 2:59:54 PM by  Torsten Lindner [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Thank you very much for sharing your code and work!

Created on Oct 11, 2010 3:00:22 PM by  Torsten Lindner [Paessler Support]



Votes:

0

Your Vote:

Up

Down

I just used Chrome Developer tools to see what endpoint the UI was hitting. By doing that, I found the following which works for changing a sensor's primary channel. In this example, I'm setting all the sensors tagged with wmiphysicaldisksensor to use channel 15 as their primary channel. Channel ID's can be determined easily enough using the UI and your browser's developers tools on the drop-down.

$username = "username"
$password = "password"
$prtgServer = "prtg.local"
$response = Invoke-RestMethod "https://$prtgServer/api/table.json?content=sensors&columns=objid,device,sensor&[email protected](wmiphysicaldisksensor)&username=$username&password=$password"
foreach($sensor in $response.sensors) {
    $result = Invoke-RestMethod "https://$prtgServer/editsettings" -Method "POST" -Headers @{ "Content-Type" = "application/x-www-form-urlencoded" } -Body "primarychannel_=15&id=$($sensor.objid)&username=$username&password=$password"
    Write-Host "Modified sensor $($sensor.objid) ($($sensor.device))"
}

Created on Mar 17, 2017 3:52:23 AM by  wardrop (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.