Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report for the last 2 months using the PRTGapi #338

Open
2 tasks done
MPrica12 opened this issue Jun 20, 2023 · 9 comments
Open
2 tasks done

Report for the last 2 months using the PRTGapi #338

MPrica12 opened this issue Jun 20, 2023 · 9 comments
Labels
question Questions raised by people who don't know how to program or read the wiki :P

Comments

@MPrica12
Copy link

MPrica12 commented Jun 20, 2023

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

  • It wasn't covered by the wiki, I swear!
  • I have tried doing some basic research on this issue but haven't been able to come up with anything. Please help!
@MPrica12 MPrica12 added the question Questions raised by people who don't know how to program or read the wiki :P label Jun 20, 2023
@lordmilko
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

Hi @MPrica12,

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

@MPrica12
Copy link
Author

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.b__39_0()
at PrtgAPI.PowerShell.Progress.ProgressTask1.<>c__DisplayClass4_01.b__0()
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
Copy link
Author

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.b__39_0()
at PrtgAPI.PowerShell.Progress.ProgressTask1.<>c__DisplayClass4_01.b__0()
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
Copy link
Owner

lordmilko commented Jun 21, 2023

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions raised by people who don't know how to program or read the wiki :P
Projects
None yet
Development

No branches or pull requests

2 participants