Create check_mk host checks with PowerShell

4. December 2015

blog.feldmann.io

PowerShell Version: >2 
Modules: none

If you use check_mk to monitor your environment you are able to create simple local host checks using PowerShell.

Your check_mk agent should be installed on the target host in a specific location, such as C:\Program Files (x86)\check_mk

After the agent installation you should have a local folder (C:\Program Files (x86)\check_mk\local) in which you can put a simple PowerShell Get-Content script to return whatever you have scripted in a check_mk readable format:

Get-Content -path "C:\Program Files (x86)\check_mk\tmp\xxxxx.log"

To achieve the check_mk readable format make sure to print your scripted output into something like this:

<<<local>>>
0 CHECK_NAME – OK – CHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)

I created a short example with a folder count:

$outfile = "C:\Program Files (x86)\check_mk\tmp\xxxxx.log"
#check folder count on c
$c_folder_count = (Get-Childitem c:\ | measure).count
#check_mk output
echo `<`<`<local`>`>`> | Out-File $outfile
echo "0 FOLDER-COUNT-C - OK - We have $c_folder_count folders on drive C|folders_on_c=$c_folder_count" | Out-File $outfile -Append

The output to file C:\Program Files (x86)\check_mk\tmp\xxxx.log is:

<<<local>>>
0 FOLDER-COUNT-C – OK – We have 40 folders on drive C|folders_on_c=40

You are now able to do a short cmk -II hostname && cmk -R on your check_mk server and the new check should appear on your host.

The next step would be different states for different results:

<<<local>>>
0 CHECK_NAME – OKCHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)

<<<local>>>
1 CHECK_NAME – WARNING CHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)

<<<local>>>
2 CHECK_NAME – CRITICAL CHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)

With a simple if we could generate different outputs for different cases:

$outfile = "C:\Program Files (x86)\check_mk\tmp\xxxxx.log"

#check folder count on c
$c_folder_count = (Get-Childitem c:\ | measure).count

#check_mk output
echo `<`<`<local`>`>`> | Out-File $outfile
if ($c_folder_count -lt 30)
    {
    #if count is below 100 we are fine
    echo "0 FOLDER-COUNT-C - OK - We have $c_folder_count folders on drive C|folders_on_c=$c_folder_count" | Out-File $outfile -Append
    }
else
    {
    #if the count is over 100 we get create a warning
    echo "1 FOLDER-COUNT-C - WARNING - We have $c_folder_count folders on drive C|folders_on_c=$c_folder_count" | Out-File $outfile -Append
    }

It is important to give only the second Out-File an Append, so that the output will be overwritten every time the script is running.

You could now create a local task, running the script once a week/day/hour to generate the output for check_mk.

#check_mk

Leave a Reply

Your email address will not be published. Required fields are marked *


*