The check plugin check_systemd_unit monitors a systemd unit.
The plug-and-play Linux binaries take the following CLI arguments and no environment variables:
$ ./check_systemd_unit -unit UNIT \ [-warn PROPERTY(THRESHOLD) ...] [-crit PROPERTY(THRESHOLD) ...] \ [-js-warn JS_EXPR ...] [-js-crit JS_EXPR ...]
UNIT is the systemd unit to monitor, e.g.
PROPERTY(THRESHOLD) specifies a numeric unit property and
an alert threshold range conforming to the Nagio$ check plugin API,
-warn NRestarts(@~:42) warns if the unit's NRestarts are <= 42.
-js-crit ActiveState==="active" returns status critical
if the unit's ActiveState is not "active".
To print the legal info, execute the plugin in a terminal:
In this case the program will always terminate with exit status 3 ("unknown") without actually checking anything.
If you want to actually execute a check inside a terminal, you have to connect the standard output of the plugin to anything other than a terminal – e.g. the standard input of another process:
$ ./check_systemd_unit -unit icinga2.service |cat
In this case the exit code is likely to be the cat's one. This can be worked around like this:
bash $ set -o pipefail bash $ ./check_systemd_unit -unit icinga2.service |cat
Just integrate the plugin into the monitoring tool of your choice like any other check plugin. (Consult that tool's manual on how to do that.) It should work with any monitoring tool supporting the Nagio$ check plugin API.
The only limitation: check_systemd_unit must be run on the host to be checked – either with an agent of your monitoring tool or by SSH. Otherwise it will check the host your monitoring tool runs on.
This repository ships the check command definition as well as a service template and host example for Icinga 2.
The service definition will work in both correctly set up Icinga 2 clusters and Icinga 2 instances not being part of any cluster as long as the hosts are named after the endpoints.