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-04-07 21:23:42 +02:00
|
|
|
def load(module_name, config=core.config.Config([])):
|
2020-03-06 14:14:34 +01:00
|
|
|
error = None
|
2020-04-07 21:23:42 +02:00
|
|
|
module_short, alias = (module_name.split(':') + [module_name])[0:2]
|
|
|
|
config.set('__alias__', alias)
|
2020-03-06 14:14:34 +01:00
|
|
|
for namespace in [ 'core', 'contrib' ]:
|
|
|
|
try:
|
2020-04-07 21:23:42 +02:00
|
|
|
mod = importlib.import_module('modules.{}.{}'.format(namespace, module_short))
|
2020-03-06 14:14:34 +01:00
|
|
|
return getattr(mod, 'Module')(config)
|
|
|
|
except ModuleNotFoundError as e:
|
2020-04-07 21:23:42 +02:00
|
|
|
log.fatal('failed to import {}: {}'.format(module_short, e))
|
2020-03-06 14:14:34 +01:00
|
|
|
except ImportError as e:
|
2020-04-07 21:23:42 +02:00
|
|
|
log.fatal('failed to import {}: {}'.format(module_short, 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-04-07 21:23:42 +02:00
|
|
|
def __init__(self, config=core.config.Config([]), widgets=[]):
|
2020-02-07 21:28:29 +01:00
|
|
|
super().__init__()
|
2020-04-07 21:23:42 +02:00
|
|
|
self.__config = config
|
2020-04-04 13:55:54 +02:00
|
|
|
self.__widgets = widgets if isinstance(widgets, list) else [ widgets ]
|
|
|
|
for widget in self.__widgets:
|
2020-02-23 21:13:49 +01:00
|
|
|
widget.module(self)
|
2020-04-04 13:55:54 +02:00
|
|
|
self.__name = None
|
2020-04-07 21:23:42 +02:00
|
|
|
self.alias = self.__config.get('__alias__', 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
|
|
|
|
|
2020-04-07 21:23:42 +02:00
|
|
|
for prefix in [ self.name(), self.module_name(), self.alias ]:
|
|
|
|
value = self.__config.get('{}.{}'.format(prefix, key), value)
|
2020-02-04 21:09:11 +01:00
|
|
|
# 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):
|
2020-04-07 21:23:42 +02:00
|
|
|
self.__config.set('{}.{}'.format(self.name(), key), value)
|
2020-02-23 14:52:58 +01:00
|
|
|
|
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-04-07 21:23:42 +02:00
|
|
|
module = Error(config=self.__config, module='error', error=str(e))
|
2020-04-04 13:55:54 +02: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-04-04 13:55:54 +02:00
|
|
|
return self.__name if self.__name else self.module_name()
|
2020-02-04 21:09:11 +01:00
|
|
|
|
|
|
|
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:
|
2020-04-04 13:55:54 +02:00
|
|
|
self.__widgets = widgets
|
|
|
|
return self.__widgets
|
2020-01-26 14:06:09 +01:00
|
|
|
|
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))
|
2020-04-04 13:55:54 +02:00
|
|
|
self.__module = module
|
|
|
|
self.__error = error
|
2020-02-15 14:04:53 +01:00
|
|
|
|
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-04-04 13:55:54 +02: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
|