lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
305 stars 38 forks source link

Get-Channel : Object reference not set to an instance of an object. #381

Closed droorda closed 9 months ago

droorda commented 9 months ago

Describe the bug

when running Get-Channel on specific sensors I receive the error "Object reference not set to an instance of an object." I have verified the sensor is valid, verified that the request provided by verbose returns valid data.

Steps to reproduce

Get-Channel -SensorId 62263 -Verbose
VERBOSE: Get-Channel: Synchronously executing request https://prtg/api/table.xml?content=channels&columns=objid,name,lastvalue&count=*&showhide=1&id=62263&username=Name&passhash=*********
Get-Channel : Object reference not set to an instance of an object.
At line:1 char:1
+ Get-Channel -SensorId 62263 -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Channel], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,PrtgAPI.PowerShell.Cmdlets.GetChannel

Here is data returned by PRTG.
((Invoke-WebRequest -Uri 'https://prtg/api/table.xml?content=channels&columns=objid,name,lastvalue&count=*&showhide=1&id=62263&username=Name&passhash=*********').Content)
<?xml version="1.0" encoding="UTF-8"?>
  <channels totalcount="0" listend="1">
   <prtg-version>24.1.90.1306</prtg-version>
   <item>
    <objid>0</objid>
    <objid_raw>0000000000</objid_raw>
    <lastvalue>1 </lastvalue>
    <lastvalue_raw>1.0000</lastvalue_raw>
   </item>
   <item>
    <objid>-4</objid>
    <objid_raw>*000000004</objid_raw>
    <name>Downtime</name>
   </item>
   <item>
    <objid>1</objid>
    <objid_raw>0000000001</objid_raw>
    <name>Processor 1</name>
    <lastvalue/>
   </item>
   <item>
    <objid>2</objid>
    <objid_raw>0000000002</objid_raw>
    <name>Processor 2</name>
    <lastvalue/>
   </item>
  </channels>

What is the output of 'Get-PrtgClient -Diagnostic'?

PSVersion      : 5.1.19041.3930
PSEdition      : Desktop
OS             : Microsoft Windows 10 Pro
PrtgAPIVersion : 0.9.19
Culture        : en-US
CLRVersion     : 533325
PrtgVersion    : 24.1.90.1306
PrtgLanguage   : english.lng

Additional context

I have multiple Sensors that do this.

I can provide more samples if helpful.

lordmilko commented 9 months ago

Can you generate the exception and then provide the output of $Errors[0].Exception.StackTrace

droorda commented 9 months ago
TargetSite     : Boolean <GetChannelsInternal>b__3(System.Xml.Linq.XElement)
HResult        : -2147467261
StackTrace     :    at PrtgAPI.PrtgClient.<>c__DisplayClass115_0.<GetChannelsInternal>b__3(XElement e)
                    at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
                    at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
                    at System.Xml.Linq.Extensions.Remove[T](IEnumerable`1 source)
                    at PrtgAPI.PrtgClient.GetChannelsInternal(Either`2 sensorOrId, Func`2 nameFilter, Func`2 idFilter, CancellationToken token)
                    at PrtgAPI.PowerShell.Cmdlets.GetChannel.GetRecords()
                    at PrtgAPI.PowerShell.Base.PrtgObjectCmdlet`1.ProcessRecordEx()
                    at PrtgAPI.PowerShell.Base.PrtgCmdlet.ExecuteWithCoreState(Action action)
                    at PrtgAPI.PowerShell.Base.PrtgCmdlet.ProcessRecord()
                    at System.Management.Automation.CommandProcessor.ProcessRecord()
lordmilko commented 9 months ago

Thanks, my bad - it should be $Error not $Errors. I will investigate and see if I can see what would be causing this

lordmilko commented 9 months ago

Are you able to do

Set-PrtgClient -LogLevel All
Get-Channel -SensorId 62263 -Verbose

I understand you posted the response from when you did Invoke-WebRequest but I can't see an issue with the data that that returned.

It seems to me the issue is most likely this line of code:

https://github.com/lordmilko/PrtgAPI/blob/31fefb19655ffe6ce1d1c146c0491e42b269784e/src/PrtgAPI/Request/PrtgClient.Generated.cs#L52

It is implicitly assumed that all items will have an objid. All of the items in the response of Invoke-WebRequest did all have an objid, so my only thought is that maybe PrtgAPI is getting a different response where one item doesn't have an objid

droorda commented 9 months ago

It took me a few min to realize the $error plural issue. I have been looking at the working and non working responses and do not see anything that looks like a pattern. If I was better at C# I would try to trace the issue with a debugger.

PS C:\> Set-PrtgClient -LogLevel All
PS C:\> Get-Channel -SensorId 62263 -Verbose
VERBOSE: Get-Channel: Synchronously executing request https://prtg/api/table.xml?content=channels&columns=objid,name,lastvalue&count=*&showhide=1&id=62263&username=username&passhash=00000000
VERBOSE: Get-Channel: <?xml version="1.0" encoding="UTF-8"?>
  <channels totalcount="0" listend="1">
   <prtg-version>24.1.90.1306</prtg-version>
   <item>
    <objid>0</objid>
    <objid_raw>0000000000</objid_raw>
    <lastvalue>2 </lastvalue>
    <lastvalue_raw>2.0000</lastvalue_raw>
   </item>
   <item>
    <objid>-4</objid>
    <objid_raw>*000000004</objid_raw>
    <name>Downtime</name>
   </item>
   <item>
    <objid>1</objid>
    <objid_raw>0000000001</objid_raw>
    <name>Processor 1</name>
    <lastvalue/>
   </item>
   <item>
    <objid>2</objid>
    <objid_raw>0000000002</objid_raw>
    <name>Processor 2</name>
    <lastvalue/>
   </item>
  </channels>
Get-Channel : Object reference not set to an instance of an object.
At line:1 char:1
+ Get-Channel -SensorId 62263 -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Channel], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,PrtgAPI.PowerShell.Cmdlets.GetChannel

Here is a second Error sample if it helps

VERBOSE: Get-Channel: <?xml version="1.0" encoding="UTF-8"?>
  <channels totalcount="0" listend="1">
   <prtg-version>24.1.90.1306</prtg-version>
   <item>
    <objid>-4</objid>
    <objid_raw>*000000004</objid_raw>
    <name>Downtime</name>
   </item>
   <item>
    <objid>0</objid>
    <objid_raw>0000000000</objid_raw>
    <lastvalue>1.08 </lastvalue>
    <lastvalue_raw>1.0847</lastvalue_raw>
   </item>
   <item>
    <objid>1</objid>
    <objid_raw>0000000001</objid_raw>
    <name>Processor 1</name>
    <lastvalue>1 %</lastvalue>
    <lastvalue_raw>1.4233</lastvalue_raw>
   </item>
   <item>
    <objid>2</objid>
    <objid_raw>0000000002</objid_raw>
    <name>Processor 2</name>
    <lastvalue>&lt;1 %</lastvalue>
    <lastvalue_raw>0.7461</lastvalue_raw>
   </item>
   <item>
    <objid>3</objid>
    <objid_raw>0000000003</objid_raw>
    <name>Processor 3</name>
    <lastvalue/>
   </item>
  </channels>
Get-channel : Object reference not set to an instance of an object.
At line:1 char:1
+ Get-channel -SensorId 62276 -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Channel], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,PrtgAPI.PowerShell.Cmdlets.GetChannel
droorda commented 9 months ago

I think I see the Pattern. All the ones that error <objid>0</objid> does not have a <name></name>

lordmilko commented 9 months ago

When you look at sensor 62263 in the PRTG UI, what is the name of channel 0?

droorda commented 9 months ago

I do not show a name in the UI

droorda commented 9 months ago

I opened the Channel Properties which show the Name in mandatory, Added a name, and now the get-channel is working correctly. So not really a but on your side since PRTG claims that field is mandatory. More of a PRTG but that allows a channel to be created without a Mandatory Field.

lordmilko commented 9 months ago

Ah, I think I understand

https://github.com/lordmilko/PrtgAPI/blob/31fefb19655ffe6ce1d1c146c0491e42b269784e/src/PrtgAPI/Request/PrtgClient.Generated.cs#L51

This line assumes that you will have a name tag, but it could be empty

i.e. it allows for the following

<name></name>

but doesn't allow for this tag being missing completely

lordmilko commented 9 months ago

Hi @droorda,

Can you please 0.9.20-preview.2 via the manual installation instructions and advise whether this issue is resolved for channels that don't have names?

droorda commented 9 months ago

Once you closed the ticket, I manually added names to the sensors to clear the error.