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
Add comment