Daniel D.
Daniel D.

Reputation: 15

Powershell script showing UNKN state in Check_MK

I have a simple powershell script that checks the logged in users on a Windows Server (qwinsta).
It works perfectly with Nagios/NRPE, I can also execute the script locally on the Windows Server, but I can't get it to work with Check_MK.
I copied the script to the %ProgramData%\agent\local folder, a rescan on the Check_MK server finds the script, but the service status is UNKN (check failed - please submit a crash report!).

The script looks like this:

(qwinsta.exe) | findstr /v Disc | findstr /v Conn | findstr /v Listen | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv | findstr.exe USERNAME > C:\temp\users.txt

And then the customization for the Nagios output:

(Get-Content C:\temp\users.txt) -replace 'USERNAME', '' -replace ':','' | Set-Content C:\temp\users.txt 
$lines = Get-Content C:\Temp\users.txt 
$var=($lines|group|?{$_.count -ge 1}|Select -ExpandProperty Name) -join ","

I suspect the problem is in the way I wrote the Nagios output in Powershell:

if ($LoggedOnUsers -lt 1){<br>
    $NagiosStatus = "0"<br>
    Write-Host "No users are logged on"<br>
    }<br>
else <br>
    {<br>
     if (($Today -like "Saturday" -OR $Today -like "Sunday") -AND ($LoggedOnUsers -eq 1)) <br>
     {<br>
                $NagiosStatus = "2"<br>
                if ($LoggedOnUsers -eq 1) {<br>
                    Write-Host "$LoggedOnUsers user logged on: $var"<br>
                }<br>
                else {<br>
                    Write-Host "$LoggedOnUsers users logged on: $var"<br>
                }<br>
            }<br>
    else <br>
    {<br>
                if (($now -lt $max) -AND ($now -gt $min)) {<br>
                $NagiosStatus = "1"<br>
                if ($LoggedOnUsers -eq 1) {<br>
                    Write-Host "$LoggedOnUsers user logged on: $var "<br>
                }<br>
                else {<br>
                    Write-Host "$LoggedOnUsers users logged on: $var"<br>
                }<br>
            }<br>
            else {<br>
                $NagiosStatus = "2" <br>
                if ($LoggedOnUsers -eq 1) {<br>
                    Write-Host "$LoggedOnUsers user logged on: $var"<br>
                    }<br>
                else {<br>
                    Write-Host "$LoggedOnUsers users logged on: $var"<br>
                }<br>
            }<br>
    }<br>
    }<br>
exit $NagiosStatus<br>

Not sure how to write the output for Check_mk.
Any ideas welcome.

This is how the output looks in Nagios: enter image description here

Upvotes: 0

Views: 156

Answers (1)

Luuk
Luuk

Reputation: 14978

(qwinsta) -replace ' SESSIONNAME','_SESSIONNAME' -replace ' +',',' |
   ConvertFrom-csv | 
   Where-Object { $_.STATE -Match 'Active' } | 
   Select-Object USERNAME | 
   Get-Unique |
   Join-String -Property {$_.USERNAME} -Separator ','
  • First replace ' SESSIONNAME' by '_SESSIONNAME', this is done to make the columns split nicely.

  • Replace all 1 or more spaced (' +') by a ','.

  • Select the 'Active' ones (you can change this when you need other states too

  • Select the USERNAME

  • Each USERNAME just ones, so Get-Unique

  • Join the result with a ',' as separator

OK, I am not sure about this last parts, was not able to test because I only have one USERNAME , and my Powershell scripting levels are LOW

Upvotes: 1

Related Questions