Last active
November 12, 2021 13:09
-
-
Save felixlindemann/5f52ee9dcc42290352f6a63c1204e491 to your computer and use it in GitHub Desktop.
PRTG-Custom Sensor for Sophos UTM Licenses (SG-SERIES)
This file contains 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
### 2019-08-13 ### | |
<# .SYNOPSIS Reset of VPN-Tunnel per Powershel. | |
.DESCRIPTION SOPHOS-API is used to read the status of licences | |
.NOTES Change the APi-Token from time to time in sophos. | |
API Token can be added at https:// + $IP + :4444/ --> Verwaltung --> Webdamin --> Restfull API --> Token | |
.COMPONENT to avoid certificate issues, certificate warnings are ignroed. #> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$IP, | |
[Parameter(Mandatory=$true)] | |
[string]$Port, | |
[Parameter(Mandatory=$true)] | |
[string]$firewall, | |
[Parameter(Mandatory=$true)] | |
[string]$token | |
) | |
# Ignoriere certificate errors on http-api-Call | |
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 | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12 | |
Write-Host "<prtg>" | |
try | |
{ | |
# encode token for http call | |
$tokenBase64 = [Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes("token:" + $token.Trim())) | |
# Write-Host" ... erfolgreich" | |
# prepare http Header | |
$headers = @{} | |
$headers.add("Authorization",'Basic ' + $tokenBase64) | |
$headers.add("Content-Type", "application/json") | |
$headers.add("Accept", "application/json") | |
# prepare http url | |
$uri="https://" + $IP.Trim() +":" + $Port.Trim() + "/api/nodes/licensing.license" | |
#call web api | |
$license = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers | |
# get required infos | |
$license= $license.Substring($license.IndexOf("[LicenseInfo]")).Substring(0, $license.IndexOf("----BEGIN")-1).Trim() | |
# split to array for licences | |
$content = $licence -split "\n" | |
# required to calc remaing runtime of licence | |
$Today=(GET-DATE) | |
$resulttable= New-Object System.Collections.ArrayList | |
# convert http-result to Array | |
foreach ($line in $content) { | |
if ($line[0] -eq ";") { | |
# Skip comment line | |
} | |
elseif ($line[0] -eq "[") { | |
# Found new segment: $segment | |
$segment = $line.replace("[","").replace("]","").Trim() | |
} | |
elseif ($line -like "*=*") { | |
# Found Keyline | |
$resulttable += New-Object PSObject -Property @{ | |
segment = $segment | |
Key = $line.split("=")[0].Trim() | |
value = $line.split("=")[1].Trim() | |
DaysRemaining=0 | |
date=$Today | |
} | |
} | |
else { | |
# Skip line | |
} | |
} | |
# Filter only enddates of licences | |
$resulttable = $resulttable | where Key -EQ "Stop" | |
# prepare PRTG-Sensor Information | |
foreach ($item in $resulttable) { | |
$item.date = [datetime]::ParseExact($item.value, "MM/dd/yyyy" ,$null) | |
# this is the number of ramaining days the licence is valid for | |
$item.DaysRemaining = ($ts = New-TimeSpan -Start $Today -End $item.date ).Days | |
write-host "<result>" | |
write-host "<channel>Lizenz (Tage verbleibend): " $item.segment "</channel>" | |
write-host "<value>" | |
write-host $item.DaysRemaining | |
write-host "</value>" | |
write-host "<LimitMaxWarning>90</LimitMaxWarning>" # start to warn by 90 Days | |
write-host "<LimitMaxError>10</LimitMaxError>" # start to set to error at 10 remaining days | |
write-host "<LimitMode>1</LimitMode>" | |
write-host "</result>" | |
} | |
} | |
catch | |
{ | |
# prepare PRTG-Error Message | |
Write-Host "<error>" | |
Write-Host "1" | |
Write-Host "</error>" | |
Write-Host "<text>" | |
Write-Host $PSItem.ToString() | |
Write-Host "</text>" | |
} | |
Write-Host "</prtg>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment