-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.ps1
148 lines (116 loc) · 4.71 KB
/
App.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env pwsh
#
# File Name: App.ps1
# Author: Darian Benam <[email protected]>
# Date Created: Saturday, September 30, 2023
# Date Updated: Saturday, September 30, 2023
#Requires -Version 7.0
class WebService
{
[string] $Endpoint
[int] $ExpectedHttpResponseCode
[int] $ReceivedHttpResponseCode
[string] $ExceptionMessage
WebService([string] $Endpoint, [int] $ExpectedHttpResponseCode, [int] $ReceivedHttpResponseCode)
{
$this.Endpoint = $Endpoint
$this.ExpectedHttpResponseCode = $ExpectedHttpResponseCode
$this.ReceivedHttpResponseCode = $ReceivedHttpResponseCode
}
WebService([string] $Endpoint, [int] $ExpectedHttpResponseCode, [int] $ReceivedHttpResponseCode, [string] $ExceptionMessage)
{
$this.Endpoint = $Endpoint
$this.ExpectedHttpResponseCode = $ExpectedHttpResponseCode
$this.ReceivedHttpResponseCode = $ReceivedHttpResponseCode
$this.ExceptionMessage = $ExceptionMessage
}
}
function Get-WebServiceProNotificationEmailBody($AppConfig, $WebServiceProblemList)
{
$NotificationEmailBody = Get-Content -Path $AppConfig.NotificationEmailTemplatePath -Raw
$WebServiceListHtml = '<ul>'
foreach ($WebService in $WebServiceProblemList)
{
$WebServiceListHtml += "<li><a href=`"$($WebService.Endpoint)`" target=`"_blank`" rel=`"noopener`">$($WebService.Endpoint)</a> returned HTTP status code <strong>$($WebService.ReceivedHttpResponseCode)</strong> but <strong>$($WebService.ExpectedHttpResponseCode)</strong> was expected.</li>"
}
$WebServiceListHtml += '</ul>'
$NotificationEmailBody = $NotificationEmailBody -Replace '{{ WEB_SERVICE_LIST }}', $WebServiceListHtml
return $NotificationEmailBody
}
function Send-WebServiceProNotification($AppConfig, $SmtpConfig, $WebServiceProblemList)
{
try
{
$NotificationEmailBody = Get-WebServiceProNotificationEmailBody -AppConfig $AppConfig -WebServiceProblemList $WebServiceProblemList
$SmtpPassword = ConvertTo-SecureString $SmtpConfig.Password -AsPlainText -Force
$SmtpCredential = New-Object System.Management.Automation.PSCredential ($SmtpConfig.Username, $SmtpPassword)
Send-MailMessage -ErrorAction Stop -WarningAction SilentlyContinue -SmtpServer $SmtpConfig.Server -Port $SmtpConfig.Port -Credential $SmtpCredential -UseSsl -From $SmtpConfig.NoReplyEmailAddress -To $SmtpConfig.AlertReceiverEmailAddress -Subject '[WebMonitor Pro] Problem(s) Detected' -Body $NotificationEmailBody -BodyAsHtml
return $null
}
catch
{
return $_
}
}
Push-Location $PSScriptRoot
Write-Output "================"
Write-Output " WebMonitor Pro "
Write-Output "================`n"
Write-Output "Current Date: $(Get-Date)`n"
$AppConfigFilePath = './AppConfig.json'
$SmtpConfigFilePath = './SmtpConfig.json'
if (-not (Test-Path -Path $AppConfigFilePath))
{
Write-Output "ERROR: `"$AppConfigFilePath`" is missing! Aborting...`n"
exit 1
}
if (-not (Test-Path -Path $SmtpConfigFilePath))
{
Write-Output "ERROR: `"$SmtpConfigFilePath`" is missing! Aborting...`n"
exit 1
}
$AppConfig = Get-Content -Path './AppConfig.json' -Raw | ConvertFrom-Json
$SmtpConfig = Get-Content -Path './SmtpConfig.json' -Raw | ConvertFrom-Json
$WebServiceAuditList = New-Object System.Collections.Generic.List[WebService]
foreach ($Service in $AppConfig.Services)
{
$Endpoint = $Service.Endpoint
$ExpectedHttpResponseCode = $Service.ExpectedHttpResponseCode
try
{
Write-Output "Checking: $Endpoint"
$WebRequestResponse = Invoke-WebRequest -SkipHttpErrorCheck -Uri $Endpoint -Method Head -Headers `
@{
'User-Agent' = "WebMonitor Pro/v$($AppConfig.WebMonitorProVersion) (+https://github.com/BeardedFish/WebMonitor-Pro)"
}
$WebRequestResponseStatusCode = $WebRequestResponse.StatusCode
$WebServiceAuditList.Add([WebService]::new($Endpoint, $ExpectedHttpResponseCode, $WebRequestResponseStatusCode)) | Out-Null
}
catch
{
$WebServiceAuditList.Add([WebService]::new($Endpoint, $ExpectedHttpResponseCode, $_.Exception.Response.StatusCode.Value__, $_.Exception.Message)) | Out-Null
}
}
$WebServiceAuditList | Format-Table -Property Endpoint, ExpectedHttpResponseCode, ReceivedHttpResponseCode, ExceptionMessage
$WebServiceProblemList = $WebServiceAuditList | Where-Object `
{
$_.ExpectedHttpResponseCode -ne $_.ReceivedHttpResponseCode
}
if ($WebServiceProblemList.Count -gt 0)
{
Write-Output "WARNING: $($WebServiceProblemList.Count) issue(s) detected"
$ExceptionMessage = Send-WebServiceProNotification -AppConfig $AppConfig -SmtpConfig $SmtpConfig -WebServiceProblemList $WebServiceProblemList
if ($null -eq $ExceptionMessage)
{
Write-Output "Notification has been sent to administrator successfully!`n"
}
else
{
Write-Output "Failed to send notification to administrator! Reason: $ExceptionMessage`n"
}
}
else
{
Write-Output "All Systems Operational`n"
}
Pop-Location