nagiosplugin’s auxiliary classes are not strictly required to write checks, but
simplify common tasks and provide convenient access to functionality that is
regularly needed by plugin authors.
Note
All classes that plugin authors typically need are imported directly into the
nagiosplugin name space. For example, use
Persistent dict to remember state between invocations.
Cookies are used to remember file positions, counters and the like
between plugin invocations. It is not intended for substantial amounts
of data. Cookies are serialized into JSON and saved to a state file. We
prefer a plain text format to allow administrators to inspect and edit
its content. See LogTail for an
application of cookies to get only new lines of a continuously growing
file.
Cookies are locked exclusively so that at most one process at a time has
access to it. Changes to the dict are not reflected in the file until
Cookie.commit() is called. It is recommended to use Cookie as
context manager to get it opened and committed automatically.
After creation, a cookie behaves like a normal dict.
Parameters:
statefile – file name to save the dict’s contents
Note
If statefile is empty or None, the Cookie will be
oblivous, i.e., it will forget its contents on garbage
collection. This makes it possible to explicitely throw away
state between plugin runs (for example by a command line
argument).
Opens the file and passes a dict-like object into the
subordinate context. See open() for details about opening
semantics. When the context is left in the regular way (no
exception raised), the cookie is committed to disk.
LogTail builds on Cookie to access new lines of a
continuosly growing log file. It should be used as context manager that
provides an iterator over new lines to the subordinate context. LogTail
saves the last file position into the provided cookie object.
As the path to the log file is saved in the cookie, several LogTail
instances may share the same cookie.
Seeks to the last seen position and reads new lines.
The last file position is read from the cookie. If the log file
has not been changed since the last invocation, LogTail seeks to
that position and reads new lines. Otherwise, the position saved
in the cookie is reset and LogTail reads from the beginning.
After leaving the subordinate context, the new position is saved
in the cookie and the cookie is closed.