libgs.monitoring

date:Mon Sep 18 09:22:40 2017
author:Kjetil Wormnes

Monitoring is a stand-alone module that allows monitoring of telemetry points while attempting to have a minimal impact on the execution of the rest of the code.

It implements a simple pythonic syntax for the creation of monitoring points. To use;

  1. Create a monitor
>>> from libgs.monitoring import Monitor
>>> mon = Monitor()

2. Define a function that returns the monitored value, and decorate it with mon.monitor().

For example, to monitor the current time:

>>> from datetime import datetime
>>> @mon.monitor()
>>> def current_time():
>>>    return datetime.utcnow()
  1. Start the monitor
>>> mon.start()

And that’s it. The current time is now monitored at the default update interval under a monitor point called “current_time”. You can view the state of the monitor by printing it:

>>> print(mon)
Monitor ()  -- running
Name                          Value               Alert     Last polled
----------------------------- ------------------- --------- ---------------
.current_time                 2018-06-21 00:39:07           0.7

More complicated monitoring functions can easily be created by specifying the point value in the decorator. See Monitor.monitor() for syntax. You can also change the update interval etc…

The constructor also takes some arguments to customise its default behaviour. See Monitor.

You are able to add callbacks whenever a monitored value is updated. This is useful if you are displaying the data on a dashboard or updating a database. See Monitor.add_callback().

Finally, you can set alert levels by returning an Alert object rather than a value from your monitoring function. This module defines GreenAlert, OrangeAlert, RedAlert, and CriticalAlert. You are welcome to customise or make others by deriving a new subclass from Alert.

The following example monitoring function gets the cpu usage every 2 seconds, and marks the alert as Red if it is above 50%:

>>> @mon.monitor(dt = 2)
>>> def cpu_usage():
>>>     usage = psutil.cpu_percent(interval=1)
>>>     if usage > 50:
>>>         return RedAlert(usage)
>>>     else:
>>>         return GreenAlert(usage)

Classes

Alert(val) Base class for Alerts.
CriticalAlert(val)
GreenAlert(val)
Monitor([workers, tick, tick_cb, default_dt]) Class to provide functionality for monitoring.
MonitorItem A monitored item.
NoAlert(val)
OrangeAlert(val)
RedAlert(val)