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:

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:

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:

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.

Restart host after certain days of uptime

3. December 2015

blog.feldmann.io

PowerShell Version: >1
Modules: none

Nobody wants Windows hosts with too much uptime so this script combined with a planned task during the weekly maintenance downtime might help:

You might want to use the -force parameter on Restart-Computer for an immediate restart.