Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelZ committed Jun 7, 2022
1 parent 27aa9ec commit 4c9314c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 61 deletions.
39 changes: 25 additions & 14 deletions Custom Sensors/EXEXML/Get-SSLReport.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You can easily parse this using `Get-SSLReport -servername example.com | Convert
.NOTES
2019-11-22 Version 0.1 Initial Version
2021-03-19 Version 0.1.1 Updated input from "host" to match parameter name of function "ServerName"
2022-06-07 Version 0.1.2 Updated error handling
.EXAMPLE
Expand Down Expand Up @@ -125,32 +126,42 @@ function Get-SSLReport {
# Construct the URI part for Caching
if ($MaxCacheAgeHours -eq 0)
{
$cacheString = "&fromCache=off"
$cacheString = "&startNew=on"
} else {
$cacheString = "&fromCache=on&maxAge=$MaxCacheAgeHours"
}

# Enforce TLS 1.2 use for compatibility
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Send the WebRequest
$result = Invoke-WebRequest -UseBasicParsing -Uri "https://api.ssllabs.com/api/v3/analyze?host=$ServerName$cacheString" -TimeoutSec $TimeoutSeconds -Method Get

# Error out if we don't have a 200 OK status code
if ($result.StatusCode -ne 200)
{
throw "STATUS: $($result.StatusCode) ERROR: $($result.Content)"
}
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12

try {
$result = Invoke-WebRequest -UseBasicParsing -Uri "https://api.ssllabs.com/api/v3/analyze?host=$ServerName$cacheString" -TimeoutSec $TimeoutSeconds -Method Get
} catch [System.Net.WebException] {
$r = $_.Exception
switch ($r.Response.StatusCode)
{
200 { }
429 { throw "STATUS: $($r.Response.StatusCode) ERROR: throttled." }
500 { throw "STATUS: $($r.Response.StatusCode) ERROR: internal error. $($r.Response.Content)"}
503 { throw "STATUS: $($r.Response.StatusCode) ERROR: the service is not available."}
529 { throw "STATUS: $($r.Response.StatusCode) ERROR: the service is overloaded."}
default { throw "STATUS: $($r.Response.StatusCode) ERROR: $($r.Response.Content)" }
}
}

$parsed = $result.Content | ConvertFrom-Json

switch ($parsed.status)
{
"IN_PROGRESS" { start-sleep -Seconds 10; return Get-SSLReport -serverName $serverName -maxCacheAge $maxCacheAge -timeoutSec $timeoutSec }
"DNS" { start-sleep -Seconds 10; return Get-SSLReport -serverName $serverName -maxCacheAge $maxCacheAge -timeoutSec $timeoutSec }
"IN_PROGRESS" {
Write-Verbose "Scan is in progress. Waiting."
start-sleep -Seconds 10; return Get-SSLReport -serverName $serverName -maxCacheAge $maxCacheAge -timeoutSec $timeoutSec }
"DNS" {
Write-Verbose "Scan is in progress. Waiting."
start-sleep -Seconds 10; return Get-SSLReport -serverName $serverName -maxCacheAge $maxCacheAge -timeoutSec $timeoutSec }
"ERROR" {
throw $parsed.statusMessage
}
}
"READY" {
return $result.Content
}
Expand Down
80 changes: 33 additions & 47 deletions Custom Sensors/EXEXML/QualysSSLReport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,37 @@ System.String. A JSON string that PRTG can understand as a custom sensor
PS> .\QualysSSLReport.ps1 -ServerName microsoft.com
{
"prtg": [
{
"channel": "40.76.4.15",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "40.113.200.201",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "104.215.148.63",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "40.112.72.205",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "13.77.161.179",
"value": 3,
"warning": "0",
"notifychanged": "1"
}
]
{
"channel": "40.76.4.15",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "40.113.200.201",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "104.215.148.63",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "40.112.72.205",
"value": 3,
"warning": "0",
"notifychanged": "1"
},
{
"channel": "13.77.161.179",
"value": 3,
"warning": "0",
"notifychanged": "1"
}
]
}
.LINK
Expand All @@ -84,8 +84,8 @@ Import-Module $PSScriptRoot\Get-SSLReport.psm1

function Get-SSLGradeValue {
param(
[Parameter(Mandatory=$true)]
[string]$Grade
[Parameter(Mandatory=$true)]
[string]$Grade
)

switch ($Grade)
Expand All @@ -106,20 +106,6 @@ function Get-SSLGradeValue {

try {
$result = Get-SSLReport -ServerName $ServerName -TimeoutSeconds $TimeoutSeconds -MaxCacheAgeHours $MaxCacheAgeHours
} catch [System.Net.WebException] {
$r = $_.Exception

$errorobject = [pscustomobject]@{
'error'='1';
'text'="STATUS: $($r.Response.StatusCode) CONTENT: $($r.Response.Content)";
}

$prtg = [pscustomobject]@{
prtg = $errorobject
}

Write-Host ($prtg | ConvertTo-Json);
return
} catch {
$r = $_.Exception

Expand Down

0 comments on commit 4c9314c

Please sign in to comment.