Repository navigation
Expand file tree
/
Copy pathWrite-ThreadTree.ps1
More file actions
110 lines (94 loc) · 4.07 KB
/
Copy pathWrite-ThreadTree.ps1
File metadata and controls
110 lines (94 loc) · 4.07 KB
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
<#
.SYNOPSIS
Append a formatted tree entry for a thread node to a StringBuilder.
.DESCRIPTION
`Write-ThreadTree` renders a single thread line (with name, id, state, age,
and parameters) into a supplied `System.Text.StringBuilder` and recursively
renders child nodes from the pre-built tree to produce the full hierarchical
view. The node is a PSObject as returned by `Get-ThreadTree`.
.PARAMETER Node
| Name | Type | Description | Default |
|------|----------|-------------------------------------------|---------|
| Node | PSObject | Tree node as returned by `Get-ThreadTree` | $null |
.PARAMETER StringBuilder
| Name | Type | Description | Default |
|---------------|---------------------------|--------------------------------|---------|
| StringBuilder | System.Text.StringBuilder | The builder to append lines to | $null |
.PARAMETER Level
| Name | Type | Description | Default |
|-------|--------------|-------------------------------|---------|
| Level | System.Int32 | Nesting level for indentation | 0 |
.OUTPUTS
None. Appends formatted lines to the provided `StringBuilder` instance.
.EXAMPLE
$sb = [System.Text.StringBuilder]::new()
Get-ThreadTree | ForEach-Object { Write-ThreadTree -Node $_ -StringBuilder $sb }
.NOTES
This function does not return a value; it operates by side-effects on the
supplied builder. Child nodes are read directly from `$Node.Children` -- no
additional registry lookups are performed.
SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
SPDX-License-Identifier: GPL-3.0-or-later
#>
function Write-ThreadTree()
{
param (
[PSObject]$Node = $null,
[System.Text.StringBuilder]$StringBuilder = $null,
[int]$Level = 0
);
$indent = ' ' * ($Level * 4);
$prefix = '';
if ($Level -ne 0) {
$prefix = [string]::Concat([char]0x2514, [char]0x2500, [char]0x2500, ' ');
}
# Show thread parameters if any present
$params = '';
if ($Node.Parameters -and $Node.Parameters.Count -gt 0) {
$paramList = @();
foreach ($param in $Node.Parameters.GetEnumerator()) {
[string]$paramKey = $param.Key;
[string]$ArgumentColor = Get-LoggingConsoleColorForObjectType -Object $param.Value.GetType().Name;
if ($paramKey.StartsWith('-')) {
$paramKey = $paramKey.Substring(1);
}
$paramList += [string]::Format('[DarkGray]-{0}[/DarkGray] [{2}]{1}[/{2}]', $paramKey, $param.Value, $ArgumentColor);
}
$params = " | Params: @{ $($paramList -join '; ') }";
}
$ThreadStateColor = 'Gray';
switch ($Node.State) {
'RUNNING' { $ThreadStateColor = 'Green'; break; }
'ALIVE' { $ThreadStateColor = 'Green'; break; }
'PROCESSING' { $ThreadStateColor = 'Green'; break; }
'WAITING' { $ThreadStateColor = 'Green'; break; }
'IDLE' { $ThreadStateColor = 'Yellow'; break; }
'BLOCKED' { $ThreadStateColor = 'Magenta'; break; }
'FROZEN' { $ThreadStateColor = 'Blue'; break; }
'ZOMBIE' { $ThreadStateColor = 'DarkYellow'; break; }
'DEAD' { $ThreadStateColor = 'Red'; break; }
'TERMINATED' { $ThreadStateColor = 'Red'; break; }
}
$keepAlive = '';
if ($Node.KeepAlive) {
$keepAlive = '[DarkGreen][KEEPALIVE][/DarkGreen]';
}
$StringBuilder.AppendLine(
[string]::Format(
'{0}{1} [Yellow]{2}[/Yellow] [{3}] [{5}]{4}[/{5}] [{6}s] {7}{8}',
$indent,
$prefix,
$Node.Name,
$Node.Id,
$Node.State,
$ThreadStateColor,
$Node.LastSeen,
$keepAlive,
$params
)
) | Out-Null;
# Recursively render each child node
foreach ($child in ($Node.Children | Sort-Object -Property Name)) {
Write-ThreadTree -Node $child -Level ($Level + 1) -StringBuilder $StringBuilder;
}
}