Created
October 9, 2018 13:08
-
-
Save stephanlinke/e38c9cee2826a24e580cb0998c684d54 to your computer and use it in GitHub Desktop.
Retrieve the response time for an incident (between sensor being down and acknowledged)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This requires prtgapi to be installed: | |
| # Install-Module prtgapi | |
| # | |
| # ignore unsigned TLS certificates | |
| add-type @" | |
| using System.Net; | |
| using System.Security.Cryptography.X509Certificates; | |
| public class TrustAllCertsPolicy : ICertificatePolicy { | |
| public bool CheckValidationResult( | |
| ServicePoint srvPoint, X509Certificate certificate, | |
| WebRequest request, int certificateProblem) { | |
| return true; | |
| } | |
| } | |
| "@ | |
| [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
| # configure your PRTG Server | |
| $PRTGServer = "<fqdn of your prtg server>" | |
| # modify this parameter to get longer reports - this is inverted on purpose due to how logs are read from PRTG | |
| $StartDate = (Get-Date).AddDays(-14) | |
| $EndDate = (Get-Date) | |
| # proper message output | |
| Function This-ShowMessage([string]$type,$message){ | |
| Write-Host ("[{0}] " -f (Get-Date)) -NoNewline; | |
| switch ($type){ | |
| "success" { Write-Host " success " -BackgroundColor Green -ForegroundColor White -NoNewline; } | |
| "information" { Write-Host " information " -BackgroundColor DarkCyan -ForegroundColor White -NoNewline; } | |
| "warning" { Write-Host " warning " -BackgroundColor DarkYellow -ForegroundColor White -NoNewline; } | |
| "error" { Write-Host " error " -BackgroundColor DarkRed -ForegroundColor White -NoNewline; } | |
| default { Write-Host " notes " -BackgroundColor DarkGray -ForegroundColor White -NoNewline; } | |
| } | |
| Write-Host (" {0}{1}" -f $message,$Global:blank) | |
| } | |
| # reset the incidents | |
| $incidents = $null; | |
| Connect-PrtgServer -Server $PRTGServer -Credential (Get-Credential -Message "Please enter your PRTG Credentials") -Force | |
| This-ShowMessage -type information "Retrieving Sensors" | |
| $Sensors = (Get-Sensor) | |
| This-ShowMessage -type success "Found $($Sensors.Count) Sensors." | |
| foreach($Sensor in $Sensors) { | |
| This-ShowMessage -type information "[Objid #$($Sensor.Id)] Retrieving Logs... " | |
| $Log = (Get-Sensor -Id $Sensor.Id | Get-ObjectLog -StartDate $EndDate -EndDate $StartDate) | |
| try{ | |
| $Filtered = ($Log | Where-Object { $_.Status -eq "Down" -or $_.Status -eq "DownAcknowledged" } ) | |
| Foreach($Logentry in (1..($Filtered.Count -1))) | |
| { | |
| if($Filtered[$Logentry - 1].Status -like "DownAcknowledged") | |
| { | |
| $ResponseTime = ($Filtered[$Logentry -1].DateTime - $Filtered[$Logentry].DateTime) | |
| $ResponseTime = [string]::Format("{0}d {1}h {2}m {3}s", $ResponseTime.Days, $ResponseTime.Hours, $ResponseTime.Minutes, $ResponseTime.Seconds) | |
| $incidents += [pscustomobject] @{ | |
| Date = $Filtered[$Logentry].DateTime | |
| Objid = $Filtered[$LogEntry].Id | |
| Device = $Filtered[$LogEntry].Device | |
| Sensor = $Filtered[$Logentry].Sensor | |
| Message = $Filtered[$Logentry].Message | |
| "Response Time" = $ResponseTime | |
| } | |
| } | |
| } | |
| } | |
| catch{ | |
| # do nothing. | |
| } | |
| } | |
| # use $incidents | ConvertTo-CSV to get a CSV output instead | |
| $incidents | Ft |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From PrtgAPI 0.9.0 you can specify
-IgnoreSSLtoConnect-PrtgServer; this allows you to ignore SSL validation errors without having to manually paste in a whole bunch of code to ignore invalid certificates yourself!In addition, you can even specify a -
StatustoGet-ObjectLogto filter against one or more statuses server side! This is really useful when trying to track down rare events that occur within a large timespanRegards,
lordmilko