lordmilko / PrtgAPI

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

Performance Difference in Get-Sensor #329

Closed xasz closed 1 year ago

xasz commented 1 year ago

What are you trying to do, and to what extent does PrtgAPI provide existing functionality around this?

I am manipulate a bunch of sensors and try to optimize my scripts and found this:

PS > $prtgGroup = Get-Group -ID $PRTGGroupID   
PS > Measure-Command { $prtgGroup | Get-Sensor }                

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 17
Milliseconds      : 118
Ticks             : 171187673
TotalDays         : 0,000198133880787037
TotalHours        : 0,00475521313888889
TotalMinutes      : 0,285312788333333
TotalSeconds      : 17,1187673
TotalMilliseconds : 17118,7673

PS > Measure-Command { Get-Sensor -Group $prtgGroup.Id }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 7
Milliseconds      : 181
Ticks             : 71811049
TotalDays         : 8,31146400462963E-05
TotalHours        : 0,00199475136111111
TotalMinutes      : 0,119685081666667
TotalSeconds      : 7,1811049
TotalMilliseconds : 7181,1049

There is a 10 seconds difference just by how you use Get-Sensor

I tried to figure it out in the code, but could not figure it out. I never wrapped C# in Powershell and did not quite understand from just looking at your code, otherwise i would have tried to pull.

I just do not understand who Get-Sensor -Group <> is differently handelt then Piping a Object into it. There has to be some Object-Type resolving which seems so take about 10 seconds.

I am changing my script from piping the $Group object into Get-Sensor to use the -Group Parameter, so far i can improve my script runtime by alot and i mean really, a lot. Call this 6 times and you have a minute and i am not sure if i am saving perfomance on my prtg core server.

Perhaps someone can point me to the Object resolving in the code and perhaps i can work on a optimization.

Is this something that has some sort of parallel in the PRTG UI? If so where do you go/how do you normally do it?

No

Due Dilligance

lordmilko commented 1 year ago

If you specify -Verbose you can see the exact API requests a given PrtgAPI command executes

Get-Group -ID $PRTGGroupID -Verbose
Get-Sensor -Group $prtgGroup.Id -Verbose

The PRTG HTTP API provides the following filters for querying based on a parent object

Based on this, it is not valid to do -Group $prtgGroup.Id. The -Group parameter allows you to specify either the name of the group to query, or a Group object (from which the Name is extracted)

Get-Group Servers | Get-Sensor

Get-Sensor -Group Servers

The issue with filtering by name however is that it is possible that you could have multiple groups with a given name all across your hierarchy. As such, filtering for sensors under the group "Servers" may be completely ambiguous. PrtgAPI is able to resolve this ambiguity automatically for you, at the cost of needing to execute additional API requests. You can disable this behavior by specifying -Recurse:$false

Get-Group Servers | Get-Sensor -Recurse:$false

However, I would note that you have a bigger issue here: it should not take 10 seconds for the Get-Sensor command to run. I would recommend focusing on improving the performance of your PRTG server. Specifying -Recurse:$false may make your scripts run faster, however as noted above if you have multiple groups with the same name you may introduce some unexpected side effects.

xasz commented 1 year ago

Well, after further testing. it does not seem to do the same thng. Sorry, you was faster thatn me. wanted to close right now.

lordmilko commented 1 year ago

Correction: theres two reasons piping from a group is slower than not.

I believe Get-Sensor will always attempt to resolve ambiguous groups when a Group object is specified. If you don't want to do that, you have to specify the name of the group instead.