lordmilko / PrtgAPI

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

Report for the last 2 months using the PRTGapi #338

Open MPrica12 opened 1 year ago

MPrica12 commented 1 year ago

What's going on?

Hello,

I'm trying to create an export of all of the instances each sensors went down from an .xlsx file in the last 2 months

I'm not sure what the issue is but when I try to make an export from PRTG using the API and following the documentation in the Get-ObjectLog section the data being pulled in the export is only from the last 3 days, as those are the only instances when the sensors seems to have went down, which is impossible as we are sure some of those sensors went down in more times in the last 2 months. Am I configuring something wrong in my script?

$EndDate = Get-Date -Day 1 -Month 4 -Year 2023 $StartDate = Get-Date -Day 31 -Month 5 -Year 2023

$Xls = Import-Excel "C:\Monitoring_documents\Files_work\test.xlsx"

$export = foreach($line in $Xls.Id){ Write-Host "Processing sensor " $line $Logs = Get-Sensor -id $line | Get-ObjectLog -StartDate $StartDate -EndDate $EndDate -Status Down $Logs | Select-Object DateTime, Id, Name, Status, Message Write-Host "Found sensor with down status in the period with ID " $Logs.Id start-sleep 2 }

$export | Export-Csv C:\Monitoring_documents\Files_work\Sensors_down_updated.csv

Due Dilligance

lordmilko commented 1 year ago

Hi @MPrica12,

When you look on the logs tab of the sensor in PRTG, how many instances of the sensor being in status "Down" does it show between April 1st and May 31st?

I would also note when you want to analyze sensor outages, the best way to do this is to generate a historical report, not look at the object event logs. You can obtain the same information you get by doing a historical report in the PRTG UI using the -Report parameter of Get-SensorHistory as documented here

MPrica12 commented 1 year ago

Hello @lordmilko,

Thanks a lot for your response, I am working on making it working by using Get-SensorHistory, but I seem to be facing issues with the DateTire format of Powershell, I keep getting this error when running the below: $EndDate = Get-Date -Day 1 -Month 4 -Year 2023 $StartDate = Get-Date -Day 31 -Month 5 -Year 2023

$history = Get-Sensor -Id 74710 | Get-SensorHistory -Report -StartDate $StartDate -EndDate $EndDate | where Status -eq Down

Get-SensorHistory: String '4/13/2023 10:06:17 AM'(example of time) was not recognized as a valid DateTime.

I tried using ParseExact before running the command but I still get the same error:

$EndDate1 = [DateTime]::ParseExact('01/04/2023 00:00:00', 'dd/MM/yyyy HH:mm:ss', $null) $StartDate1 = [DateTime]::ParseExact('31/05/2023 23:59:59', 'dd/MM/yyyy HH:mm:ss', $null)

$history = Get-Sensor -Id 74710 | Get-SensorHistory -Report -StartDate $StartDate1 -EndDate $EndDate1 | where Status -eq Down

Get-SensorHistory: String '4/13/2023 10:06:17 AM' was not recognized as a valid DateTime.

lordmilko commented 1 year ago

Hi @MPrica12,

Can you reproduce the exception with the datetime and then provide the output of $error[0].Exception.StackTrace

MPrica12 commented 1 year ago

Hi @lordmilko,

Here is the output:

PS C:\Scripts\Scripts Powershell> $error[0].Exception.StackTrace at System.DateTime.Parse(String s) at PrtgAPI.PrtgClient.GetSensorHistoryReportInternal(Either2 sensorOrId, PrtgResponse response) at PrtgAPI.PrtgClient.GetSensorHistoryReport(Either2 sensorOrId, Nullable1 startDate, Nullable1 endDate) at PrtgAPI.PowerShell.Cmdlets.GetSensorHistory.b39_0() at PrtgAPI.PowerShell.Progress.ProgressTask1.<>c__DisplayClass4_01.b0() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() at PrtgAPI.PowerShell.Progress.ProgressTask1.ValueEmpty() at PrtgAPI.PowerShell.Progress.ProgressTask`1.Write() at PrtgAPI.PowerShell.Cmdlets.GetSensorHistory.ProcessSensorHistoryReport() at PrtgAPI.PowerShell.Cmdlets.GetSensorHistory.ProcessRecordEx() at PrtgAPI.PowerShell.Base.PrtgCmdlet.ExecuteWithCoreState(Action action) at System.Management.Automation.CommandProcessor.ProcessRecord()

MPrica12 commented 1 year ago

I just tried running it without any kind of startdate or enddate parameters and I get the same error:

Get-SensorHistory: String '6/20/2023 2:01:11 PM' was not recognized as a valid DateTime.

Here is the StackTrace:

at System.DateTime.Parse(String s) at PrtgAPI.PrtgClient.GetSensorHistoryReportInternal(Either2 sensorOrId, PrtgResponse response) at PrtgAPI.PrtgClient.GetSensorHistoryReport(Either2 sensorOrId, Nullable1 startDate, Nullable1 endDate) at PrtgAPI.PowerShell.Cmdlets.GetSensorHistory.b39_0() at PrtgAPI.PowerShell.Progress.ProgressTask1.<>c__DisplayClass4_01.b0() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() at PrtgAPI.PowerShell.Progress.ProgressTask1.ValueEmpty() at PrtgAPI.PowerShell.Progress.ProgressTask`1.Write() at PrtgAPI.PowerShell.Cmdlets.GetSensorHistory.ProcessSensorHistoryReport() at PrtgAPI.PowerShell.Cmdlets.GetSensorHistory.ProcessRecordEx() at PrtgAPI.PowerShell.Base.PrtgCmdlet.ExecuteWithCoreState(Action action) at System.Management.Automation.CommandProcessor.ProcessRecord()

lordmilko commented 1 year ago

This is a bug in PrtgAPI which is occurring because the datetime format of your system is MM/dd/yyyy. You can temporarily work around this by changing your date format in Windows to be dd/MM/yyyy. I will investigate what may be required to resolve this

MPrica12 commented 1 year ago

Hello @lordmilko

I just did a few changes to the DateTime, now I get a different error:

Get-SensorHistory: Failed to find the table 'table_statereporttable' in the specified HTML response.

What do you think I should do about this?

lordmilko commented 1 year ago

Can you please provide the output of Get-PrtgClient -Diagnostic

In addition, if you execute Get-SensorHistory -Report with the -Verbose parameter it will show you the API request that was executed

C:\> get-sensor -id 2056|Get-SensorHistory -Report -verbose
http://prtg.example.com/historicdata_html.htm?edate...

If you copy and paste this entire URL in your web browser it should bring up the report, with the Sensor Status History section down the bottom. If you're using Chrome, and then do Ctrl+Shift+J, you can use the Selector button to select the Sensor Status History header

image

image

If you then look at the adjacent HTML inside the table-wrapper div and form, you should see a table

image

The expected name of this table is table_statereporttable as you can see in this screenshot. If the table doesn't exist, or has a different name, this will help explain why we're getting an error it couldn't find the table. Are you able to advise whether or not the table exists or not, and if so what its id is

MPrica12 commented 1 year ago

Here is the output:

PSVersion : 7.3.4 PSEdition : Core OS : Microsoft Windows 10.0.19041 PrtgAPIVersion : 0.9.18 Culture : ro-RO CLRVersion : .NETCoreApp,Version=v7.0 PrtgVersion : 23.2.83.1760 PrtgLanguage : english.lng

I will look into the table_statereporttable and will provide it soon, thank you!