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:
it should normally go critical but check_mk can not read the content of the $folderscount variable.
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
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
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.
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.