lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

Get-Sensor mit Tags "+" and "-" #358

Closed schoenm1 closed 11 months ago

schoenm1 commented 11 months ago

What's going on?

Hi @lordmilko Your function Get-Sensor -Tags $mytags is calling (tested with -verbose) an url with'&filter_tags=@sub(...)' I'm working with Tags using Tag,+Tag,-Tag example: https://myserver/sensors.htm?filter_tags=@tag(%2BDT:Probe-Device,%2BSLA-Platin,Ping,cpu,memory,%2DNo-Display) which is https://myserver/sensors.htm?filter_tags=@tag(+DT:Probe-Device,+SLA-Platin,Ping,cpu,memory,-No-Display) => this will show all Sensors MUST Include "SLA-Platin" and either "DT:Probe-Device" or "Ping" but must not include 'No-Display'. In other words: "NOT No-Display AND SLA-Platin AND (Ping or DT;Probe-Device)" In your function Get-Sensor -Tags $mytags the +,- is not allowed When testing with verbose, your function is using '&filter_tags=@sub(...)'and not the '&filter_tags=@tag(...)'. Is there a possibility to use PRTGAPi for using + and -? Or can I only use:

$allPlusTags =  @("SLA-Platin","DT:Probe-Device")
$OrTags = @("Ping","cpu","memory")
$sensors = Get-Sensor -Tags $allPlusTags
$allSensors = $null
foreach ($orTagentry in $OrTags){
  Write-Host "this tag: $orTagentry"
  $allSensors += $sensors | Where-Object {$_.Tags -match $orTagentry}
}
# missing filtering out "-" Tags

Due Dilligance

lordmilko commented 11 months ago

Get-Sensor provides two parameters for filtering based on tags

# Get all sensors that have either tag "foo" or "bar"
Get-Sensor -Tag foo,bar

# Get all sensors that have both of the tags "foo" and "bar"
Get-Sensor -Tags foo,bar

Due PRTG not doing case insensitive comparisons when filtering for "equals", and tags being stored within PRTG as a space delimited string, PrtgAPI does a "contains" query (@sub) and then applies your wildcard expression to the returned results client side, reducing the response to just the items you were really after.

I was not aware of the @tag syntax, however I have tested and confirmed that you should be able to apply this filter yourself using the New-SearchFilter cmdlet (flt for short)

flt tags eq "@tag(TestGroup,-pingsensor)" | get-sensor

In my test server, TestGroup returns 24 sensors. 4 of which have the pingsensor tag, and excluding it correctly returns the other 20 sensors