Tutorial #1: ‘Hello world’ check

In the first tutorial, we will develop check_world. This check will determine if the world exists. The algorithm is simple: if the world would not exist, the check would not execute.

This minimalistic check consists of a Resource World which models the part of the world that is interesting for the purposes of our check. Resource classes must define a Resource.probe() method which returns a list of metrics. We just return a single Metric object that states that the world exists.

#!python

"""Hello world Nagios check."""

import nagiosplugin


class World(nagiosplugin.Resource):

    def probe(self):
        return [nagiosplugin.Metric('world', True, context='null')]


def main():
    check = nagiosplugin.Check(World())
    check.main()

if __name__ == '__main__':
    main()

We don’t have a context to evaluate the returned metric yet, so we resort to the built-in “null” context. The “null” context does nothing with its associated metrics.

We now create a Check object that is fed only with the resource object. We could put context and summary objects into the Check() constructor as well. This will be demonstrated in the next tutorial. There is also no command line processing nor timeout handling nor output control. We call the Check.main() method to evaluate resources, construct text output and exit with the appropriate status code.

Running the plugin creates very simple output:

1
2
$ check_world.py
WORLD OK

The plugin’s exit status is 0, signalling success to the calling process.