My appliances do support the SNMP location, but only in a very limited way. I only have one line available for the string, but i'd like to use the GeoMaps feature that shows the icon in the correct location, but only it's name, similar to the following screenshot:
When entering the following string in the location:
Paessler Headquarters,49.4874996,11.1278013
...the location is correct, but instead of only "Paessler Headquarters", it shows the entire string, since I can't enter a <br /> or a different line break character within the device location.
While it would be easy to change, it's read from the device with every auto discovery, so changing them manually would be in vain. Is there any way to modify the location automatically with a sensor?
Article Comments
This article applies to PRTG Network Monitor 16 or later
Introduction
PRTG does indeed always read the SNMP location string of the device, since it could've been possibly relocated since the last auto discovery. The following script will iterate through all devices under a given Object ID and modify their location
according to the current location string. Here's what it does:
- Retrieve all devices under that Object ID, e.g. a group in PRTG
- Check if the device currently has a location similar to the following:
<location name>,<latitude>,<longitude>
- If the string matches, PRTG will update the location accordingly and it will look like in your screenshot.
Installation
Remarks
PowerShell based scripts must be allowed to run on the probe you're running the script on.
Please check out our Guide For PowerShell based Custom Sensors.
Setup
- Save the script as PRTG-UpdateSNMPLocations.ps1 to
C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE
- Modify the script's parameters according to the table in the Parameters section of this post
- Create a new EXE/Script Sensor on the Local Probe device
- Enter the following parameter:
-parentId 0
- Select PRTG-UpdateSNMPLocations.ps1 from the EXE/Script dropdown menu
- Hit Continue
You can give the script a small test run, make sure that you have devices that have a matching location string. Simply open up a PowerShell and execute the script. You will see something like this:
Console output of the script
As you can see, it also outputs a PRTG compliant result at the end.
Parameters
Parameter Name | Description | Example |
$prtgProtocol | The protocol used by your PRTG. | Use either http or https. |
$prtgPort | The port your webinterface uses | 80 |
$prtgHost | The FQDN of your PRTG webinterface | prtg.acme.com |
$prtgUser | The user accessing the API | prtgadmin |
$prtgPasshash | The passhash of the above user | 12345678 |
$parentId | The the ID of the Auto Discovery group | 0 |
Script
#requires -version 4.0
# ___ ___ _____ ___
#| _ \ _ \_ _/ __|
#| _/ / | || (_ |
#|_| |_|_\ |_| \___|
# Active PRTG Users
# ================================
# The following script will iterate through all devices under a given
# Object ID and modify their location according to the current location string.
#
# Version History
# ----------------------------
# 1.0 initial release
# # # # # # # # # # # # # # # # # # # # # # # # # #
param(
[string]$prtgProtocol = "http",
[int]$prtgPort = 80,
[string]$prtgHost = "prtg.acme.com",
[string]$prtgUser = "prtgadmin",
[string]$prtgPasshash = "123456789",
[int]$parentId = 0
);
function Console-ShowMessage([string]$type,[string]$message){
Write-Host ("[{0}] " -f (Get-Date)) -NoNewline;
switch ($type){
"success" { Write-Host " success " -BackgroundColor Green -ForegroundColor White -NoNewline; }
"information" { Write-Host " information " -BackgroundColor DarkCyan -ForegroundColor White -NoNewline; }
"warning" { Write-Host " warning " -BackgroundColor DarkYellow -ForegroundColor White -NoNewline; }
"error" { Write-Host " error " -BackgroundColor DarkRed -ForegroundColor White -NoNewline; }
default { Write-Host " notes " -BackgroundColor DarkGray -ForegroundColor White -NoNewline; }
}
Write-Host (" {0}{1}" -f $message,$Global:blank)
}
#region configuration
$progressPreference = 'silentlyContinue'
$counter = 0;
#endregion
# This will retrieve all active users from the configuration file and compare them to the webserver log
function This-UpdateLocations(){
begin
{ Console-ShowMessage -type information -message "Starting location update." }
process {
# regex for matching <string>,<latitude>,<longitude>
$regex = '^.+,([-+]?\d{1,2}([.]\d+)?),\s*([-+]?\d{1,3}([.]\d+)?)$'
# Get all devices beneath the configured ID
$Devices = (((Invoke-Webrequest -Uri ([string]::Format("{0}://{1}:{2}/api/table.json?content=devices&output=json&id={3}&columns=group,objid,device,location&username={4}&passhash={5}", $prtgProtocol,$prtgHost, $prtgPort,$parentId,$prtgUser,$prtgPasshash))).Content | ConvertFrom-Json).devices)
Console-ShowMessage -type information -message "$($Devices.Count) devices found in this group."
Foreach($Device in $Devices){
Console-ShowMessage -type information -message "Checking $($Device.device). Location: $($Device.location_raw)"
if($Device.location_raw -match $regex)
{
Console-ShowMessage -type warning -message "$($Device.device) still has SNMP location from the device. Updating...";
# put the location into an array so we can put it in the corresponding URL
$location = $Device.location_raw.Split(",")
if((Invoke-Webrequest -Uri ([string]::Format("{0}://{1}:{2}/api/setlonlat.htm?id={3}&location={4}&lonlat={5},{6}&username={7}&passhash={8}",$prtgProtocol,$prtgHost,$prtgPort,$device.objid, $location[0],$location[2],$location[1],$prtgUser,$prtgPasshash))))
{ Console-ShowMessage -type success -message "$($Device.device) location updated successfuly!"; $counter++; }
else
{ Console-ShowMessage -type error -message "Error while updating location of $($Device.device)"; Write-Host ("{0}:Device location could not be updated for $($Device.device)"); exit 1; }
}
else
{ Console-ShowMessage -type notes -message "$($Device.device) already has the correct location format"; }
}
}
End
{ Write-Host ("{0}:{0} device(s) had their location updated in the last run" -f $counter) }
}
This-UpdateLocations
Mar, 2017 - Permalink
This article applies to PRTG Network Monitor 16 or later
Introduction
PRTG does indeed always read the SNMP location string of the device, since it could've been possibly relocated since the last auto discovery. The following script will iterate through all devices under a given Object ID and modify their location according to the current location string. Here's what it does:
<location name>,<latitude>,<longitude>
Installation
Remarks
PowerShell based scripts must be allowed to run on the probe you're running the script on.
Please check out our Guide For PowerShell based Custom Sensors.
Setup
C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE
-parentId 0
You can give the script a small test run, make sure that you have devices that have a matching location string. Simply open up a PowerShell and execute the script. You will see something like this:
As you can see, it also outputs a PRTG compliant result at the end.
Parameters
Script
Mar, 2017 - Permalink