Using powershell variables for Metrics in Local Checks

hello everyone,

im still testing the enterprise edition amd trying to understand how scripting in bash, batch, and powershell works with metrics…

i have this problem in powershell :
the original source code was

$folderscount=(ls $HOME\Desktop| ? {$_.Mode -eq "d-----"}).Count
Write-Host "P FoldersCount MYVALUE=$folderscount;2;3;; there is $folderscount Folder on Desktop of $env:COMPUTERNAME"

Output in powershell:
image

it should normally go critical but check_mk can not read the content of the $folderscount variable.

image

than i tried this code :

$files = get-childitem -Path $HOME\Desktop 
$folders = $files | where-object { $_.PSIsContainer }
$folderscount = $folders.Count

Write-Host "P FoldersCount MYVALUE=$folderscount;2;3;; there is $folderscount Folder on Desktop of $env:COMPUTERNAME"

correct output in powershell but not in checkmk (as above)

But with this code works :

$folderscount = 3
Write-Host "P FoldersCount MYVALUE=$folderscount;2;3;; there is $folderscount Folder on Desktop of $env:COMPUTERNAME"

i thought maybe the type of $foldercount is STRING in the first two code examples
and i added [int]$folderscount but still did not made a diffrence.
i also tried with parentheses and brackets like :

Write-Host "P FoldersCount MYVALUE=${folderscount};2;3;; there is ${folderscount} Folder on Desktop of $env:COMPUTERNAME

and

Write-Host "P FoldersCount MYVALUE=$($folderscount);2;3;; there is $($folderscount) Folder on Desktop of $env:COMPUTERNAME

but still does not work.

Hi,

in the default configuration the checkmk agent on Windows is running as the local system account.

When you test the Powershell script with another account you might get other results since you count the number of files in $HOME\Desktop.

I’m not an expert in Windows: I’m not sure if the local system account has a Desktop directory or not, maybe this is the problem…

Best Regards
Thomas

1 Like

you are right! and thanks for the tipp.

after changing $home\desktop to full path like C:\Users\Administrator\Desktop… it showed the right output. so i think i need to reconfigure the agent to not be execute like local system account

Hello ymez,

It should not be necessary to change the account used by the Checkmk agent to monitor files like you are trying to do.

The issue, as Thomas pointed out, is that the PowerShell script is using a profile-specific variable when building the check output. This makes the output dependent on the user that runs the script.

Consider this statement:

Write-Host "P FoldersCount MYVALUE=${folderscount};2;3;; there is ${folderscount} Folder on Desktop of $env:COMPUTERNAME"

Remember that Windows is a multi-user Operating System, and that each user has their own Desktop folder. With that in mind, it means that it doesn’t make sense to count the folders on the Desktop by computer name alone; we need to include the account’s name as well:

Write-Host "P FoldersCount MYVALUE=${folderscount};2;3;; Folders on ${env:USERNAME}'s Desktop on ${env:COMPUTERNAME}: ${folderscount}"

This code would also make it clear that when you run the script locally it is running as Administrator and that when the Checkmk agent runs the script it is running as a different user.

I don’t know what you are trying to monitor, but using a profile-specific variable like $HOME is almost certainly not what you want. The “right” or “best” way to write the PowerShell code is going to depend on what exactly you want to monitor.

Hope this helps,
Jason

1 Like

i got it thank you so much for the explination.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.