-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheck_ms_iis_application_pool.ps1
476 lines (464 loc) · 22.1 KB
/
check_ms_iis_application_pool.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# Script name: check_ms_iis_application_pool.ps1
# Version: v1.06.180411
# Created on: 10/03/2016
# Author: Willem D'Haese
# Purpose: Checks Microsoft Windows IIS application pool cpu and memory usage
# On Github: https://github.com/willemdh/check_ms_iis_application_pool
# On OutsideIT: https://outsideit.net/monitoring-iis-application-pools
# Recent History:
# 27/01/17 => AppCmd method as workaround for hanging gci
# 28/01/16 => appcount to the back
# 18/02/17 => Cleanup and PSSharpening
# 15/09/17 => Fixed perfdata not working in some cases (Yannick Charton)
# 22/09/17 => Fixed bug with multiple w3wp processes
# 11/04/18 => Fixed bug with multiple w3wp processes, cpu and memory parts and added warn/crit to perfdata (Yannick Charton)
# Copyright:
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#Requires -Version 2.0
$DebugPreference = 'SilentlyContinue'
$VerbosePreference = 'SilentlyContinue'
#$DebugPreference = 'Continue'
#$VerbosePreference = 'Continue'
$IISStruct = New-Object -TypeName PSObject -Property @{
StopWatch = [Diagnostics.Stopwatch]::StartNew()
ApplicationPool = ''
ProcessId = ''
Process = ''
PoolCount = ''
PoolState = ''
SitesCount = ''
WarningMemory = ''
CriticalMemory = ''
WarningCpu = ''
CriticalCpu = ''
CurrentMemory = ''
CurrentCpu = ''
Duration = ''
Exitcode = 3
AppPoolOnDemand = 0
AppCmd = 0
AppCmdList = ''
ReturnString = 'UNKNOWN: Please debug the script...'
}
#region Functions
Function Write-Log {
Param (
[parameter(Mandatory=$true)][string]$Log,
[parameter(Mandatory=$true)][ValidateSet('Debug', 'Info', 'Warning', 'Error', 'Unknown')][string]$Severity,
[parameter(Mandatory=$true)][string]$Message
)
$Now = Get-Date -Format 'yyyy-MM-dd HH:mm:ss,fff'
$LocalScriptName = Split-Path -Path $myInvocation.ScriptName -Leaf
If ( $Log -eq 'Undefined' ) {
Write-Debug -Message "${Now}: ${LocalScriptName}: Info: LogServer is undefined."
}
ElseIf ( $Log -eq 'Verbose' ) {
Write-Verbose -Message "${Now}: ${LocalScriptName}: ${Severity}: $Message"
}
ElseIf ( $Log -eq 'Debug' ) {
Write-Debug -Message "${Now}: ${LocalScriptName}: ${Severity}: $Message"
}
ElseIf ( $Log -eq 'Output' ) {
Write-Host "${Now}: ${LocalScriptName}: ${Severity}: $Message"
}
ElseIf ( $Log -match '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])(?::(?<port>\d+))$' -or $Log -match '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ) {
$IpOrHost = $log.Split(':')[0]
$Port = $log.Split(':')[1]
If ( $IpOrHost -match '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ) {
$Ip = $IpOrHost
}
Else {
$Ip = [Net.Dns]::GetHostAddresses($IpOrHost)[0].IPAddressToString
}
Try {
$LocalHostname = ([Net.Dns]::GetHostByName((hostname.exe)).HostName).tolower()
$JsonObject = (New-Object -TypeName PSObject |
Add-Member -PassThru NoteProperty logsource $LocalHostname |
Add-Member -PassThru NoteProperty hostname $LocalHostname |
Add-Member -PassThru NoteProperty scriptname $LocalScriptName |
Add-Member -PassThru NoteProperty logtime $Now |
Add-Member -PassThru NoteProperty severity_label $Severity |
Add-Member -PassThru NoteProperty message $Message )
If ( $psversiontable.psversion.major -ge 3 ) {
$JsonString = $JsonObject | ConvertTo-Json
$JsonString = $JsonString -replace "`n",' ' -replace "`r",' '
}
Else {
$JsonString = $JsonObject | ConvertTo-Json2
}
$Socket = New-Object -TypeName System.Net.Sockets.TCPClient -ArgumentList ($Ip,$Port)
$Stream = $Socket.GetStream()
$Writer = New-Object -TypeName System.IO.StreamWriter -ArgumentList ($Stream)
$Writer.WriteLine($JsonString)
$Writer.Flush()
$Stream.Close()
$Socket.Close()
}
Catch {
Write-Host "${Now}: ${LocalScriptName}: Error: Something went wrong while trying to send message to logserver `"$Log`"."
}
Write-Verbose -Message "${Now}: ${LocalScriptName}: ${Severity}: Ip: $Ip Port: $Port JsonString: $JsonString"
}
ElseIf ($Log -match '^((([a-zA-Z]:)|(\\{2}\w+)|(\\{2}(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}))(\\(\w[\w ]*))*)') {
If (Test-Path -Path $Log -pathType container){
Write-Host "${Now}: ${LocalScriptName}: Error: Passed Path is a directory. Please provide a file."
Exit 1
}
ElseIf (!(Test-Path -Path $Log)) {
Try {
$null = New-Item -Path $Log -Type file -Force
}
Catch {
$Now = Get-Date -Format 'yyyy-MM-dd HH:mm:ss,fff'
Write-Host "${Now}: ${LocalScriptName}: Error: Write-Log was unable to find or create the path `"$Log`". Please debug.."
exit 1
}
}
Try {
"${Now}: ${LocalScriptName}: ${Severity}: $Message" | Out-File -filepath $Log -Append
}
Catch {
Write-Host "${Now}: ${LocalScriptName}: Error: Something went wrong while writing to file `"$Log`". It might be locked."
}
}
}
Function ConvertTo-JSON2 {
param (
$MaxDepth = 4,
$ForceArray = $false
)
Begin {
$Data = @()
}
Process{
$Data += $_
}
End{
If ($Data.length -eq 1 -and $ForceArray -eq $false) {
$Value = $Data[0]
}
Else {
$Value = $Data
}
If ($Value -eq $null) {
Return 'null'
}
$DataType = $Value.GetType().Name
Switch -Regex ($DataType) {
'String' { Return "`"{0}`"" -f (Format-JSONString $Value ) }
'(System\.)?DateTime' { Return "`"{0:yyyy-MM-dd}T{0:HH:mm:ss}`"" -f $Value }
'Int32|Double' { Return "$Value" }
'Boolean' { Return "$Value".ToLower() }
'(System\.)?Object\[\]' {
If ($MaxDepth -le 0) {
Return "`"$value`""
}
$JsonResult = ''
ForEach ($Elem in $Value) {
If ($JsonResult.Length -gt 0) {
$JsonResult +=', '
}
$JsonResult += ($Elem | ConvertTo-JSON -MaxDepth ($MaxDepth -1))
}
Return '[' + $jsonResult + ']'
}
'(System\.)?Hashtable' {
$JsonResult = ''
ForEach ($Key in $Value.Keys) {
If ($JsonResult.Length -gt 0) {
$JsonResult +=', '
}
$jsonResult +=
@'
"{0}": {1}
'@ -f $Key , ($Value[$Key] | ConvertTo-JSON2 -MaxDepth ($MaxDepth -1) )
}
Return '{' + $jsonResult + '}'
}
default {
If ($MaxDepth -le 0) {
Return "`"{0}`"" -f (Format-JSONString $Value)
}
Return '{' +(($value | Get-Member -MemberType *property | ForEach-Object {
@'
"{0}": {1}
'@ -f $_.Name , ($value.($_.Name) | ConvertTo-JSON2 -maxDepth ($maxDepth -1))
}) -join ', ') + '}'
}
}
}
}
Function Initialize-Args {
Param (
[Parameter(Mandatory=$true)]$Args
)
try {
For ( $i = 0; $i -lt $Args.count; $i++ ) {
$CurrentArg = $Args[$i].ToString()
if ($i -lt $Args.Count-1) {
$Value = $Args[$i+1];
If ($Value.Count -ge 2) {
foreach ($Item in $Value) {
$null = Test-Strings -String $Item
}
}
else {
$Value = $Args[$i+1];
$null = Test-Strings -String $Value
}
}
else {
$Value = ''
}
switch -regex -casesensitive ($CurrentArg) {
'^(-A|--ApplicationPool)$' {
if ($value -match '^[a-zA-Z0-9. _-]+$') {
$IISStruct.ApplicationPool = $Value
}
else {
throw "Application Pool `"$value`" does not meet regex requirements."
}
$i++
}
'^(-WM|--WarningMemory)$' {
if ($value -match '^[0-9]{1,10}$') {
$IISStruct.WarningMemory = $Value
}
else {
throw "Method `"$value`" does not meet regex requirements."
}
$i++
}
'^(-CM|--CriticalMemory)$' {
if ($value -match '^[0-9]{1,10}$') {
$IISStruct.CriticalMemory = $Value
}
else {
throw "Method `"$value`" does not meet regex requirements."
}
$i++
}
'^(-WC|--WarningCpu)$' {
if ($value -match '^[0-9]{1,10}$') {
$IISStruct.WarningCpu = $Value
}
else {
throw "Method `"$value`" does not meet regex requirements."
}
$i++
}
'^(-CC|--CriticalCpu)$' {
if ($value -match '^[0-9]{1,10}$') {
$IISStruct.CriticalCpu = $Value
}
else {
throw "Method `"$value`" does not meet regex requirements."
}
$i++
}
'^(-APOD|--AppPoolOnDemand)$' {
if ($value -match '^[0-1]{1,2}$') {
$IISStruct.AppPoolOnDemand = $Value
}
else {
throw "Method `"$value`" does not meet regex requirements."
}
$i++
}
'^(-Appcmd|-AppCmd|-appcmd|-APPCMD)$' {
if ($value -match '^[0-1]{1,2}$') {
$IISStruct.Appcmd = $Value
}
else {
throw "Method `"$value`" does not meet regex requirements."
}
$i++
}
'^(-h|--Help)$' {
Write-Help
}
default {
throw "Illegal arguments detected: $_"
}
}
}
}
catch {
Write-Host "CRITICAL: Argument: $CurrentArg Value: $Value Error: $_"
Exit 2
}
}
Function Test-Strings {
Param ( [Parameter(Mandatory=$true)][string]$String )
$BadChars=@("``", '|', ';', "`n")
$BadChars | ForEach-Object {
If ( $String.Contains("$_") ) {
Write-Host "Error: String `"$String`" contains illegal characters."
Exit $IISStruct.ExitCode
}
}
Return $true
}
Function Invoke-CheckIISApplicationPool {
Try {
Import-Module -Name WebAdministration
If (Get-ChildItem -Path IIS:\AppPools | Where-Object {$_.Name -eq "$($IISStruct.ApplicationPool)"}) {
$IISStruct.PoolState = Get-ChildItem -Path IIS:\AppPools | Where-Object {$_.Name -eq "$($IISStruct.ApplicationPool)"} | Select-Object -Property State -ExpandProperty State
If ( $IISStruct.PoolState -eq 'Started') {
$IISStruct.ProcessId = Get-WmiObject -NameSpace 'root\WebAdministration' -class 'WorkerProcess' | Where-Object {$_.AppPoolName -match $IISStruct.ApplicationPool} | Select-Object -Expand ProcessId
If ( $IISStruct.ProcessId ) {
If ( @($($IISStruct.ProcessId)) -gt 1 ) {
$IISStruct.CurrentCpu = 0
$IISStruct.CurrentMemory = 0
Get-WmiObject -NameSpace 'root\WebAdministration' -class 'WorkerProcess' | Where-Object {$_.AppPoolName -match $IISStruct.ApplicationPool} | Select-Object -Expand ProcessId | ForEach-Object {
$MyProcessId=$_
$IISStruct.Process = Get-Wmiobject -Class Win32_PerfFormattedData_PerfProc_Process | Where-Object { $_.IdProcess -eq $MyProcessId }
$MyCurrentCpu = $IISStruct.Process.PercentProcessorTime
$MyCurrentMemory = [Math]::Round(($IISStruct.Process.workingSetPrivate / 1MB),2)
$IISStruct.CurrentCpu += $MyCurrentCpu
$IISStruct.CurrentMemory += $MyCurrentMemory
Write-Log Verbose Info "Application pool $($IISStruct.ApplicationPool) process id: $_ Percent CPU: $MyCurrentCpu Private Memory: $MyCurrentMemory"
}
}
Else {
$IISStruct.Process = Get-Wmiobject -Class Win32_PerfFormattedData_PerfProc_Process | Where-Object { $_.IdProcess -eq $IISStruct.ProcessId }
$IISStruct.CurrentCpu = $IISStruct.Process.PercentProcessorTime
$IISStruct.CurrentMemory = [Math]::Round(($IISStruct.Process.workingSetPrivate / 1MB),2)
}
Write-Log Verbose Info "Application pool $($IISStruct.ApplicationPool) process id(s): $($IISStruct.ProcessId) Percent CPU: $($IISStruct.CurrentCpu) Private Memory: $($IISStruct.CurrentMemory)"
$Sites = Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[@applicationPool='$($IISStruct.ApplicationPool)' and @path='/']/parent::*" machine/webroot/apphost -name name
$Apps = Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[@applicationPool='$($IISStruct.ApplicationPool)' and @path!='/']" machine/webroot/apphost -name path
$IISStruct.SitesCount = ($Sites,$Apps | ForEach-Object {$_.value}).count
$IISStruct.ExitCode = 0
$IISStruct.ReturnString = "OK: Application Pool `"$($IISStruct.ApplicationPool)`" with $($IISStruct.SitesCount) Applications. {CPU: $($IISStruct.CurrentCpu) %}{Memory: $($IISStruct.CurrentMemory) MB}"
$IISStruct.ReturnString += " | 'pool_cpu'=$($IISStruct.CurrentCpu)%;$($IISStruct.WarningCpu);$($IISStruct.CriticalCpu);0;100 'pool_memory'=$($IISStruct.CurrentMemory)MB;$($IISStruct.WarningMemory);$($IISStruct.CriticalMemory);0; 'app_count'=$($IISStruct.SitesCount)"
}
Else {
If ( $IISStruct.AppPoolOnDemand = 1 ) {
$IISStruct.Process = 0
$IISStruct.CurrentCpu = 0
$IISStruct.CurrentMemory = 0
$Sites = 0
$Apps = 0
$IISStruct.SitesCount = 0
$IISStruct.ExitCode = 0
$IISStruct.ReturnString = "OK: Application Pool Started but no process is assigned yet `"$($IISStruct.ApplicationPool)`" with 0 Applications. {CPU: 0%}{Memory: 0MB}"
$IISStruct.ReturnString += " | 'pool_cpu'=0%;$($IISStruct.WarningCpu);$($IISStruct.CriticalCpu);0;100 'pool_memory'=0MB;$($IISStruct.WarningMemory);$($IISStruct.CriticalMemory);0; 'app_count'=0"
}
Else {
Throw "Application Pool `"$($IISStruct.ApplicationPool)`" not found in WMI."
}
}
}
Else {
Throw "Application Pool `"$($IISStruct.ApplicationPool)`" is $($IISStruct.PoolState)."
}
}
Else {
Throw "Application Pool `"$($IISStruct.ApplicationPool)`" does not exist."
}
}
Catch {
$IISStruct.ExitCode = 2
$IISStruct.ReturnString = "CRITICAL: $_"
}
}
Function Invoke-CheckIISWithAppCmd {
Try {
[xml]$AppCmdXml = & "$env:windir\system32\inetsrv\appcmd.exe" list apppools /xml
$IISStruct.PoolCount = $AppCmdXml.appcmd.APPPOOL.Count
If ( ! $IISStruct.PoolCount ) {
$IISStruct.PoolState = $AppCmdXml.appcmd.APPPOOL.'state'
if ( $AppCmdXml.appcmd.APPPOOL.'APPPOOL.NAME' -eq $IISStruct.ApplicationPool ) {
$Found = $True
}
Else {
$IISStruct.ReturnString = "CRITICAL: NO IIS application pool with name $($IISStruct.ApplicationPool) found. "
$IISStruct.ExitCode = 2
}
}
Else {
$Found = $False
$i = 0
while ( ! $found -and $i -lt $IISStruct.PoolCount ) {
If ( $AppCmdXml.appcmd.APPPOOL[$i].'APPPOOL.NAME' -eq $IISStruct.ApplicationPool ) {
Write-Log Verbose Info "Application pool found: $($IISStruct.ApplicationPool)"
$IISStruct.PoolState = $AppCmdXml.appcmd.APPPOOL[$i].'state'
$Found = $True
}
Write-Log Verbose Info "Pool: $($AppCmdXml.appcmd.APPPOOL[$i].'APPPOOL.NAME')"
$i++
}
}
If ( $Found ) {
If ( $IISStruct.PoolState -eq 'Started') {
$IISStruct.ProcessId = Get-WmiObject -NameSpace 'root\WebAdministration' -class 'WorkerProcess' | Where-Object {$_.AppPoolName -match "^$($IISStruct.ApplicationPool)$"} | Select-Object -Expand ProcessId
If ( $IISStruct.ProcessId ) {
$IISStruct.Process = get-wmiobject Win32_PerfFormattedData_PerfProc_Process | Where-Object { $_.IdProcess -eq $IISStruct.ProcessId }
$IISStruct.CurrentCpu = $IISStruct.Process.PercentProcessorTime
Write-Log Verbose Info "Application pool $($IISStruct.ApplicationPool) process id: $($IISStruct.ProcessId) Percent CPU: $($IISStruct.CurrentCpu)"
$IISStruct.CurrentMemory = [Math]::Round(($IISStruct.Process.workingSetPrivate / 1MB),2)
Write-Log Verbose Info "Application pool $($IISStruct.ApplicationPool) process id: $($IISStruct.ProcessId) Private Memory: $($IISStruct.CurrentMemory)"
$Sites = Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[@applicationPool='$($IISStruct.ApplicationPool)' and @path='/']/parent::*" machine/webroot/apphost -name name
$Apps = Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[@applicationPool='$($IISStruct.ApplicationPool)' and @path!='/']" machine/webroot/apphost -name path
$IISStruct.SitesCount = ($Sites,$Apps | ForEach-Object {$_.value}).count
$IISStruct.ExitCode = 0
$IISStruct.ReturnString = "OK: Application Pool `"$($IISStruct.ApplicationPool)`" with $($IISStruct.SitesCount) Applications. {CPU: $($IISStruct.CurrentCpu) %}{Memory: $($IISStruct.CurrentMemory) MB}"
$IISStruct.ReturnString += " | 'pool_cpu'=$($IISStruct.CurrentCpu)%;$($IISStruct.WarningCpu);$($IISStruct.CriticalCpu);0;100 'pool_memory'=$($IISStruct.CurrentMemory)MB;$($IISStruct.WarningMemory);$($IISStruct.CriticalMemory);0; 'app_count'=$($IISStruct.SitesCount)"
}
Else {
If ( $IISStruct.AppPoolOnDemand = 1 ) {
$IISStruct.Process = 0
$IISStruct.CurrentCpu = 0
$IISStruct.CurrentMemory = 0
$Sites = 0
$Apps = 0
$IISStruct.SitesCount = 0
$IISStruct.ExitCode = 0
$IISStruct.ReturnString = "OK: Application Pool Started but no process is assigned yet `"$($IISStruct.ApplicationPool)`" with 0 Applications. {CPU: 0%}{Memory: 0MB}"
$IISStruct.ReturnString += " | 'pool_cpu'=0%;$($IISStruct.WarningCpu);$($IISStruct.CriticalCpu);0;100 'pool_memory'=0MB;$($IISStruct.WarningMemory);$($IISStruct.CriticalMemory);0; 'app_count'=0"
}
Else {
Throw "Application Pool `"$($IISStruct.ApplicationPool)`" not found in WMI."
}
}
}
Else {
Throw "Application Pool `"$($IISStruct.ApplicationPool)`" is $($IISStruct.PoolState)."
}
}
Else {
Throw "NO IIS application pool with name $($IISStruct.ApplicationPool) found. "
}
}
Catch {
$IISStruct.ExitCode = 2
$IISStruct.ReturnString = "CRITICAL: $_"
}
}
#endregion Functions
#region Main
If ( $Args ) {
If ( ! ( $Args[0].ToString()).StartsWith('$') ) {
If ( $Args.count -ge 1 ) {
Initialize-Args $Args
}
}
Else {
$IISStruct.ReturnString = 'CRITICAL: Script needs mandatory parameters to work.'
$IISStruct.ExitCode = 2
}
}
If ( $IISStruct.AppCmd -eq 0 ) {
Invoke-CheckIISApplicationPool
}
else {
Invoke-CheckIISWithAppCmd
}
Write-Host $IISStruct.ReturnString
Exit $IISStruct.ExitCode
#endregion Main