Using Parse Functions


This article is obsolete and may be not valid anymore!

1. Parse Functions

Some agent based checks have to deal with more complex agent output then simply a few well defined rows and columns. One such example is the check multipath, which needs to deal with a great variety of bizarre output formats of the command multipath -l on Linux.

The typical implementation of such a check is to shift the task of parsing the stuff that arrives the inventory- or check function in the info parameter to a helper function. That function parses the raw agent output into some convenient data structure - often, but now always, a dictionary. While this works pretty well, it has one drawback: If the check deals with more than one item then the same parsing has to be done over and over again.

Beginning with version 1.2.5i7 Check_MK's check API now allows to declare a "parse_function" in the check_info declaration. Here is an example:

multipath
check_info["multipath"] = {
    'check_function':          check_multipath,
    'inventory_function':      inventory_multipath,
    'parse_function':          parse_multipath,
    'service_description':     'Multipath %s',
    'group':                   'multipath',
}

You need to define this function in the same file. It gets the one argument info and must return some parsed representation of that data:

multipath
def parse_multipath(info):
    parsed = {}
    # Do some bizarre parsing from info into the new variable parsed
    ...
    return parsed

Check_MK will now automatically process all agent output with the parse function before it calls the inventory or check function. In order to make this clear, you should not anymore use the name info as argument, but rather parsed:

multipath
def inventory_multipath(parsed):
    # parsed is already in convenient parsed form

... and ...

multipath
def check_multipath(item, params, parsed):
    # parsed is already in convenient parsed form

Note: Parse functions cannot be defined for sub checks like foo.bar. Define it on the main check foo. All subchecks will the automatically also use the parse function. If there is no man check then you have bad luck and cannot use parse functions.