-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_server_health.ps1
163 lines (136 loc) · 3.66 KB
/
check_server_health.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
param (
[int]
$port = 8085,
[string]
$smtpUser,
[securestring]
$smtpPassword,
[string]
$smtpServer,
[string]
$smtpAlertTarget,
[string]
$hostsListFile,
[string]
$slackURI,
[string]
$slackChannel,
[string]
$alertType, #slack or email
[string]
$LBrg,
[string]
$LBname,
[string]
$tenantID
)
#Module Imports
if ((Get-Module -Name Pode | Measure-Object).Count -ne 0)
{
Remove-Module -Name Pode
}
Import-Module ..\pode\src\Pode.psm1 -ErrorAction Stop
#Pull in hosts to check
If (!([string]::IsNullOrEmpty($hostsListFile)))
{
$hostsListdata = Get-Content $hostsListFile
}
else
{
$hostsListdata = @("127.0.0.1", "127.0.0.1")
}
if ([string]::IsNullOrEmpty($hostsListdata))
{
ThrowError -ExceptionMessage "Your Host List Text file is empty!" | Out-Default
$hostsListdata = "ERROR"
}
#find script Dir and pass it to modules
$scriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
# create a server, and start listening on port 8085
Server -Threads 2 {
#get Present Working Dir
#pull in PS modules
script './alerts/alerts.psm1'
script './checks/checks.psm1'
# listen on localhost:8085
listen *:$Port http
#limit ip @('127.0.0.1', '[::1]') 5 10
# allow the local ip and some other ips
access allow ip @('127.0.0.1', '[::1]')
access allow ip @('192.169.0.1', '192.168.0.2')
access allow ip @('10.14.0.0/24' )
# deny an ip
#access deny ip 10.10.10.10
#access deny ip '10.10.0.0/24'
#access deny ip all
# log requests to the terminal
logger terminal
# set view engine to pode renderer
engine pode
schedule 'test' '@minutely' {
'hello' | Out-Default
}
# add to state
state set 'data-params' @{
'hostsListdata' = $hostsListdata;
'scriptDir' = $scriptDir;
'smtpUser' = $smtpUser;
'smtpPassword' = $smtpPassword;
'smtpServer' = $smtpServer;
'smtpAlertTarget' = $smtpAlertTarget;
'slackChannel' = $slackChannel;
'slackURI' = $slackURI;
'alertType' = $alertType;
'LBrg' = $LBrg;
'LBname' = $LBname;
'tenantID' = $tenantID
} | Out-Null
#$d | Out-Default
# GET request for web page on "localhost:8085/"
route 'get' '/' {
param($session)
$d = (state get 'data-params')
$d | Out-Default
view 'simple' -Data $d
}
# GET request throws fake "500" server error status code
route 'get' '/error' {
param($session)
status 500
}
# GET request to page that merely redirects to google
route 'get' '/redirect' {
redirect 'https://google.com'
}
# GET request that redirects to same host, just different port
route 'get' '/redirect-port' {
param($session)
if ($session.Request.Url.Port -ne 8086) {
redirect -port 8086
}
else {
json @{ 'value' = 'you got redirected!'; }
}
}
# GET request to download a file
route 'get' '/download' {
param($session)
attach 'Anger.jpg'
}
# GET request with parameters
route 'get' '/:userId/details' {
param($session)
json @{ 'userId' = $session.Parameters['userId'] }
}
# ALL request, that supports every method and it a default drop route
route * '/all' {
json @{ 'value' = 'works for every http method' }
}
route get '/api/*/hello' {
json @{ 'value' = 'works for every hello route' }
}
# ALL request, supports every method and route (good for mass https redirect)
route * * {
redirect -protocol https
}
} -FileMonitor