Tuesday, August 23, 2016

Running "MRPE" check_mk scripts asynchronously on Windows

I have a corner case on Windows where I need to execute classic Nagios NRPE scripts within check_mk, but in asynchronous mode. These scripts can, in certain circumstances such as a network timeout, take a significant time to execute and they cannot be run from the check_mk agent.

It's possible to have honest-to-goodness check_mk scripts execute asynchronously, using the async directive in check_mk.ini. I tried it, it works. However, this is not supported by the agent with classic nagios plugins.

So, I wrote a wrapper named mrpe_async_wrapper that does just that. It's not rocket science; the wrapper is simply a Windows batch file that:

  1. Creates a scheduled task (on its first run) that executes the check script at 5 minutes intervals;
  2. The scheduled taks instructs mrpe_async_wrapper to run the check script and save its output in a status file;
  3. When run directly, mrpe_async_wrapper reports the contents of the status file instead of executing the script. It does it quickly. So, you can run it each minute if you want, but it will only report the status within up to the last 5 minutes. 

This lets you run slow or unpredictable NRPE scripts from check_mk without fear. I've been running this for a few days and it seems to do the job for me.

To configure it, simply add a directive to the [mrpe] section of check_mk.ini like this (on the same line)

check = check_gizmo C:\tools\mrpe_async_wrapper.bat check_gizmo C:\tools\check_gizmo.bat


This defines an MRPE check named "check_gizmo", which instructs the wrapper to create a scheduled task named "check_gizmo" that runs c:\tools\check_gizmo.bat asynchrnously.

Here is the code for the wrapper:
mrpe_async_wrapper.bat

Have fun.