2020-01-19 16:06:21 +01:00
|
|
|
import importlib
|
|
|
|
import logging
|
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
import core.input
|
2020-02-15 14:04:53 +01:00
|
|
|
import core.widget
|
2020-02-23 14:47:47 +01:00
|
|
|
import core.decorators
|
2020-02-07 21:28:29 +01:00
|
|
|
|
2020-03-06 14:31:29 +01:00
|
|
|
try:
|
|
|
|
error = ModuleNotFoundError('')
|
|
|
|
except Exception as e:
|
|
|
|
ModuleNotFoundError = Exception
|
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
log = logging.getLogger(__name__)
|
2020-01-19 15:36:52 +01:00
|
|
|
|
2020-03-29 14:30:59 +02:00
|
|
|
def every(minutes=0, seconds=0):
|
|
|
|
def decorator_init(init):
|
|
|
|
def call_init(obj, *args, **kwargs):
|
|
|
|
init(obj, *args, **kwargs)
|
|
|
|
if obj.parameter('interval') is None:
|
|
|
|
obj.set('interval', minutes*60 + seconds)
|
|
|
|
return call_init
|
|
|
|
return decorator_init
|
|
|
|
|
2020-02-03 21:30:06 +01:00
|
|
|
def load(module_name, config=None):
|
2020-03-06 14:14:34 +01:00
|
|
|
error = None
|
|
|
|
for namespace in [ 'core', 'contrib' ]:
|
|
|
|
try:
|
|
|
|
mod = importlib.import_module('modules.{}.{}'.format(namespace, module_name))
|
|
|
|
return getattr(mod, 'Module')(config)
|
|
|
|
except ModuleNotFoundError as e:
|
2020-03-06 20:57:32 +01:00
|
|
|
log.fatal('failed to import {}: {}'.format(module_name, e))
|
2020-03-06 14:14:34 +01:00
|
|
|
except ImportError as e:
|
2020-03-06 20:57:32 +01:00
|
|
|
log.fatal('failed to import {}: {}'.format(module_name, e))
|
2020-03-06 14:14:34 +01:00
|
|
|
error = str(e)
|
|
|
|
if not error:
|
|
|
|
error = 'No such module'
|
|
|
|
log.fatal('failed to import {}: {}'.format(module_name, error))
|
2020-03-28 13:44:45 +01:00
|
|
|
return Error(config=config, module=module_name, error=error)
|
2020-01-19 15:36:52 +01:00
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
class Module(core.input.Object):
|
2020-02-03 21:30:06 +01:00
|
|
|
def __init__(self, config=None, widgets=[]):
|
2020-02-07 21:28:29 +01:00
|
|
|
super().__init__()
|
2020-02-03 21:30:06 +01:00
|
|
|
self._config = config
|
2020-01-26 14:06:09 +01:00
|
|
|
self._widgets = widgets if isinstance(widgets, list) else [ widgets ]
|
2020-02-23 21:13:49 +01:00
|
|
|
for widget in self._widgets:
|
|
|
|
widget.module(self)
|
2020-02-04 21:09:11 +01:00
|
|
|
self._name = None
|
2020-03-23 15:32:06 +01:00
|
|
|
self.next_update = None
|
2020-02-04 21:09:11 +01:00
|
|
|
|
|
|
|
def parameter(self, key, default=None):
|
|
|
|
value = default
|
|
|
|
|
|
|
|
for prefix in [ self.name(), self.module_name() ]:
|
|
|
|
value = self._config.get('{}.{}'.format(prefix, key), value)
|
|
|
|
# TODO retrieve from config file
|
|
|
|
return value
|
2020-01-26 14:06:09 +01:00
|
|
|
|
2020-02-23 14:52:58 +01:00
|
|
|
def set(self, key, value):
|
|
|
|
self._config.set('{}.{}'.format(self.name(), key), value)
|
|
|
|
|
2020-01-19 15:36:52 +01:00
|
|
|
def update(self):
|
|
|
|
pass
|
|
|
|
|
2020-03-01 14:08:16 +01:00
|
|
|
def update_wrapper(self):
|
|
|
|
try:
|
|
|
|
self.update()
|
|
|
|
except Exception as e:
|
2020-03-28 13:44:45 +01:00
|
|
|
module = Error(config=self._config, module='error', error=str(e))
|
2020-03-01 14:36:12 +01:00
|
|
|
self._widgets = [module.widget()]
|
2020-03-01 14:08:16 +01:00
|
|
|
self.update = module.update
|
|
|
|
|
2020-02-03 21:30:06 +01:00
|
|
|
def name(self):
|
2020-02-04 21:09:11 +01:00
|
|
|
return self._name if self._name else self.module_name()
|
|
|
|
|
|
|
|
def module_name(self):
|
2020-02-03 21:30:06 +01:00
|
|
|
return self.__module__.split('.')[-1]
|
|
|
|
|
2020-03-13 13:56:08 +01:00
|
|
|
def widgets(self, widgets=None):
|
|
|
|
if widgets:
|
|
|
|
self._widgets = widgets
|
2020-01-26 14:06:09 +01:00
|
|
|
return self._widgets
|
|
|
|
|
2020-03-01 14:36:12 +01:00
|
|
|
def widget(self, name=None):
|
|
|
|
if not name: return self.widgets()[0]
|
|
|
|
|
|
|
|
for w in self.widgets():
|
|
|
|
if w.name() == name: return w
|
|
|
|
return None
|
|
|
|
|
2020-02-23 21:13:49 +01:00
|
|
|
def state(self, widget):
|
|
|
|
return []
|
|
|
|
|
2020-03-05 21:13:15 +01:00
|
|
|
def threshold_state(self, value, warn, crit):
|
|
|
|
if value > float(self.parameter('critical', crit)):
|
|
|
|
return 'critical'
|
|
|
|
if value > float(self.parameter('warning', warn)):
|
|
|
|
return 'warning'
|
|
|
|
return None
|
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
class Error(Module):
|
2020-03-28 13:44:45 +01:00
|
|
|
def __init__(self, module, error, config=core.config.Config([])):
|
2020-02-15 14:04:53 +01:00
|
|
|
super().__init__(config, core.widget.Widget(self.full_text))
|
|
|
|
self._module = module
|
|
|
|
self._error = error
|
|
|
|
|
2020-02-23 14:52:58 +01:00
|
|
|
self.set('scrolling.bounce', False)
|
|
|
|
self.set('scrolling.speed', 2)
|
|
|
|
self.set('width', 15)
|
2020-02-23 14:47:47 +01:00
|
|
|
|
|
|
|
@core.decorators.scrollable
|
|
|
|
def full_text(self, widget):
|
2020-02-15 14:04:53 +01:00
|
|
|
return '{}: {}'.format(self._module, self._error)
|
2020-01-19 16:06:21 +01:00
|
|
|
|
2020-02-24 15:07:34 +01:00
|
|
|
def state(self, widget):
|
|
|
|
return ['critical']
|
|
|
|
|
2020-01-19 15:36:52 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|