Thread Monitor

  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
#*=============================================================================
#* Script Name:  Get-ThreadCPUpct.ps1
#*=============================================================================
#* Created:     01/01/2025
#* Author:      First Last
#*=============================================================================

###############################################################################
#
# DO NOT EDIT THE CODE DIRECTLY - Check it out of source control and use deploy.ps1
#
###############################################################################

<#
    .SYNOPSIS
    This script provides an automated way to monitor the stuck thread of a 
    Tomcat instance. If a thread is found to be stuck longer than 6 hours
    an email is sent, but only 1 email. Once the instance is restarted and
    the thread goes away, the script will adjust.    
#>

# Setup the log file name
$todayDate = Get-Date -Format "yyyyMMdd"
$Transcript = $PSScriptRoot + "\Log_${todayDate}.txt"
$fromEmail = 'monserver <DoNotReply.Monserver@domain.com>'
$toEmail = @(
    "alert.email.1@domain.com", 
    "alert.email.2@domain.com", 
    "alert.email.3@domain.com", 
    "alert.email.4@domain.com"
    )

# Start recording output to file
Start-Transcript -Path $Transcript -Append

# Define an array of remote server to query
$remoteServerNames = @(
    "SERVER-01", 
    "SERVER-02", 
    "SERVER-03",
    "SERVER-31", 
    "SERVER-32", 
    "SERVER-33",
    "SERVER-91", 
    "SERVER-92", 
    "SERVER-93"
    )

$ThreadAge = @{
    L="ThreadAge";
    E={[Timespan]::FromMilliseconds(
            $_.UserModeTime + $_.KernelModeTime
        ).ToString("dd\:hh\:mm\:ss")}
    }

$ThreadAgeD = @{
    L="ThreadAgeD";
    E={[Timespan]::FromMilliseconds(
            $_.UserModeTime + $_.KernelModeTime
        ).ToString("dd")}
    }

$ThreadAgeH = @{
    L="ThreadAgeH";
    E={[Timespan]::FromMilliseconds(
            $_.UserModeTime + $_.KernelModeTime
        ).ToString("hh")}
    }

$ThreadWaitReason = @{
    L = 'ThreadWaitReason'
    E = {
        # property is an array, so process all values
        $value = $_.ThreadWaitReason

        switch([int]$value)
        {
            0          {'Executive'}
            1          {'FreePage'}
            2          {'PageIn'}
            3          {'PoolAllocation'}
            4          {'ExecutionDelay'}
            5          {'FreePage'}
            6          {'PageIn'}
            7          {'Executive'}
            8          {'FreePage'}
            9          {'PageIn'}
            10         {'PoolAllocation'}
            11         {'ExecutionDelay'}
            12         {'FreePage'}
            13         {'PageIn'}
            14         {'EventPairHigh'}
            15         {'EventPairLow'}
            16         {'LPCReceive'}
            17         {'LPCReply'}
            18         {'VirtualMemory'}
            19         {'PageOut'}
            20         {'Rendezvous'}
            21         {'KeyedEvent'}
            22         {'Terminated'}
            23         {'ProcessInSwap'}
            24         {'CpuRateControl'}
            25         {'CalloutStack'}
            26         {'Kernel'}
            27         {'Resource'}
            28         {'PushLock'}
            29         {'Mutex'}
            30         {'QuantumEnd'}
            31         {'DispatchInt'}
            32         {'Preempted'}
            33         {'YieldExecution'}
            34         {'FastMutex'}
            35         {'GuardedMutex'}
            36         {'Rundown'}
            37         {'MaximumWaitReason'}
            default    {"$value"}
        }
    }  
}

$currSidFilePaths = @()
$serverWaitSecs = 5

# Connect to the remote server using WMI and retrieve thread information for Tomcat
$qryServer = @(
    "SELECT * "
    "FROM Win32_PerfFormattedData_PerfProc_Thread "
    "WHERE PercentProcessorTime > 95 AND Name LIKE '%Tomcat%'"
) -Join ""

foreach ($remoteServer in $remoteServerNames) {
    # Initialize retry count
    $retryCount = 1
    $errRetryCount = 1
    $maxRetries = 3
    $tCount = 0

    Write-Host "`nChecking threads on server: $remoteServer"
    Write-Host "=============================================================================="

    while (($errRetryCount -le $maxRetries) -and ($retryCount -le $maxRetries) -and ($tCount -lt 1)) {
        try {
            Write-Host "Try for threads: ${retryCount}"

            $threads = Get-WmiObject -Query $qryServer -ComputerName $remoteServer -ErrorAction Stop
            $errRetryCount = 1 # maxRetries errors per try

            $tCount = $threads | Measure-Object | Select-Object -ExpandProperty Count
            Write-Host "Thread count: ${tCount}"

            if ($tCount -lt 1) {             
                if ($retryCount -lt $maxRetries) {
                    Write-Host "Waiting ${serverWaitSecs}s and then retrying to double-check..."
                    Start-Sleep -Seconds $serverWaitSecs
                    Write-Host "Retrying WMI query..."
                } else {
                    Write-Host "Moving on..."
                }
                $retryCount++
            } else {
                 Write-Host "Found threads: $tCount"
            }

        } catch {
            $errRetryCount++
            Write-Host "Error try #${errRetryCount} failed: $_"
            Start-Sleep -Seconds $serverWaitSecs
        }
    } 

    if ($tCount -gt 0){
        # Iterate through each thread and get the owner process command line so we know which instance.
        foreach ($thread in $threads) {
            $processId = $thread.IDProcess
            $threadId = $thread.IDThread
            $pctCPU = $thread.PercentProcessorTime

            $qryProc = @(
                "SELECT CommandLine "
                "FROM Win32_Process "
                "WHERE ProcessId = $processId"
            ) -Join ""

            $qryThread = @(
                "SELECT * "
                "FROM Win32_Thread "
                "WHERE Handle = $threadId"
            ) -Join ""

            # Initialize retry count
            $retryCount = 1
            $maxRetries = 3

            :triesTime while ($retryCount -le $maxRetries) {
                try {
                    Write-Host "Try for cpu time: ${retryCount}"
                    $cpuTime = Get-WmiObject `
                        -Query $qryThread `
                        -ComputerName $remoteServer `
                        -ErrorAction Stop | `
                        Select-Object $ThreadAge, $ThreadAgeD, $ThreadAgeH, $ThreadWaitReason 
                    break triesTime
                } catch {
                    $retryCount++
                    Write-Error "Attempt $retryCount failed: $_"
                    Start-Sleep -Seconds $serverWaitSecs
                }
            }

            # Initialize retry count
            $retryCount = 1
            $maxRetries = 3

            :triesCmdL while ($retryCount -le $maxRetries) {
                try {
                    Write-Host "Try for command line: ${retryCount}"
                    $commandLine = (Get-WmiObject -Query $qryProc `
                                        -ComputerName $remoteServer `
                                        -ErrorAction Stop).CommandLine
                    break triesCmdL
                } catch {
                    $retryCount++
                    Write-Error "Attempt $retryCount failed: $_"
                    Start-Sleep -Seconds $serverWaitSecs
                }
            }

            $commandLine = $commandLine.Substring($commandLine.LastIndexOf("/") +1)

            $iFound = @(
                "Instance: ${commandLine} "
                "p-ID: ${processId} "
                "t-ID: ${threadId} "
                "t-CPU %: ${pctCPU} "
                "t-CPU time: $($cpuTime.ThreadAge) "
                "t-Reason: $($cpuTime.ThreadWaitReason) "
            ) -Join ""

            Write-Host $iFound

            # if the instance cputime is greater than 6 hours check if it's foundid file is present
            # we have a persistent, stuck thread)
            if (([int]$cpuTime.ThreadAgeD -gt 0) -or ([int]$cpuTime.ThreadAgeH -ge 6)) 
            {
                $sidserver = $remoteServer.Substring($remoteServer.LastIndexOf("-") +1) #03
                $sidinst = $commandLine.Substring($commandLine.Length - 2) #07
                $sid = $sidserver + $sidinst + $processId + $threadId #sid03078285964 
                $sidFilePath = $PSScriptRoot + "\sid${sid}"

                $iMessage = @(
                    "If an opportunity arises you can restart the intance in off hours."
                    "Make sure to Log this occurance."
                    "Take a look at today's log to determine the start time."
                    "Log is located here:"
                    "  MONSERVER-02 - ${Transcript}"
                ) -Join "`n"

                if (-Not (Test-Path -Path $sidFilePath)) {
                    # Send the message using the internal relay
                    Send-MailMessage `
                        -From $fromEmail `
                        -To $toEmail `
                        -Subject "_Alert - APP - Thread Monitor" `
                        -Body "${remoteServer} - ${iFound}`n`n${iMessage}" `
                        -Priority High `
                        -SmtpServer 'relay.domain.com'

                    New-Item -Path $sidFilePath -ItemType File | Out-Null
                }

                $currSidFilePaths += $sidFilePath
            }
        }
    }
}

# At the end of the script, get all sid files in the script directory.
$allSidFiles = Get-ChildItem -Path $PSScriptRoot -File -Filter "sid*.*"

# Clear out sid files that are no longer needed.
foreach ($sidFile in $allSidFiles) {

    if (-Not ($currSidFilePaths -contains $sidFile.FullName)) {
        # File is not in the array, delete it
        Remove-Item -Path $sidFile.FullName -Force
        Write-Output "Deleted file: $($sidFile.FullName)"
    }
}

Stop-Transcript

# Get the files in the directory
$files = Get-ChildItem -Path $PSScriptRoot -File -Filter "*.txt"

# Count the number of files in the directory
$fileCount = $files.Count

# Check if the number of files is greater than 90
if ($fileCount -gt 90) {
    # Get the oldest file in the directory
    $oldestFile = $files | Sort-Object LastWriteTime | Select-Object -First 1

    # Delete the oldest file
    Remove-Item $oldestFile.FullName
}