Repository navigation
Expand file tree
/
Copy pathRegister-IcingaPerformanceCounterFromComponents.ps1
More file actions
83 lines (66 loc) · 2.91 KB
/
Copy pathRegister-IcingaPerformanceCounterFromComponents.ps1
File metadata and controls
83 lines (66 loc) · 2.91 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
<#
.SYNOPSIS
Discover component-declared performance counters by scanning the compiled kernel
.DESCRIPTION
`Register-IcingaPerformanceCounterFromComponents` parses the compiled module kernel
(`lib\IcingaForWindows.Kernel.ps1`, which contains every source file concatenated) and
finds all calls to `New-IcingaPerformanceCounter`. For each call it extracts the literal
counter paths passed to the `-Counter` parameter and registers them in the global store.
No component code is executed -- the declaration is read statically from the syntax tree.
This runs once during performance-counter environment initialization so that every
counter any component declares is warmed as soon as the daemon starts.
.OUTPUTS
None -- Populates `(Get-GlobalRuntimeVariable).Public.PerformanceCounter.Registered`.
.EXAMPLE
PS> Register-IcingaPerformanceCounterFromComponents
.NOTES
Only literal array/string arguments are discoverable. The kernel file is produced by
`Build-ModuleProject`; if it is missing the discovery step is skipped.
SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
SPDX-License-Identifier: GPL-3.0-or-later
#>
function Register-IcingaPerformanceCounterFromComponents()
{
$Store = (Get-GlobalRuntimeVariable).Public.PerformanceCounter;
if ($null -eq $Store) {
return;
}
[string]$KernelPath = Join-Path -Path (Get-ModuleRootDirectory) -ChildPath (Join-Path -Path 'lib' -ChildPath 'IcingaForWindows.Kernel.ps1');
if (-not (Test-Path -Path $KernelPath)) {
return;
}
$Tokens = $null;
$Errors = $null;
$Ast = [System.Management.Automation.Language.Parser]::ParseFile($KernelPath, [ref]$Tokens, [ref]$Errors);
$Calls = $Ast.FindAll(
{
param ($Node)
$Node -is [System.Management.Automation.Language.CommandAst] -and $Node.GetCommandName() -eq 'New-IcingaPerformanceCounter'
},
$true
);
[datetime]$Now = (Get-Date);
foreach ($Call in $Calls) {
$Elements = $Call.CommandElements;
for ($Index = 0; $Index -lt $Elements.Count; $Index++) {
$Element = $Elements[$Index];
if ($Element -isnot [System.Management.Automation.Language.CommandParameterAst]) {
continue;
}
if ($Element.ParameterName -ine 'Counter') {
continue;
}
# The argument can be attached to the parameter (-Counter:@(...)) or be the
# following command element (-Counter @(...)).
$Argument = $Element.Argument;
if ($null -eq $Argument -and ($Index + 1) -lt $Elements.Count) {
$Argument = $Elements[$Index + 1];
}
foreach ($Path in (Get-IcingaPerformanceCounterLiteral -Argument $Argument)) {
if (-not [string]::IsNullOrEmpty($Path)) {
$Store.Registered[$Path] = $Now;
}
}
}
}
}