[modules/http_status] Update to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-19 14:42:40 +02:00
parent 967ec5892b
commit 39f2e6bfea

View file

@ -3,66 +3,64 @@
"""Display HTTP status code """Display HTTP status code
Parameters: Parameters:
* http_status.label: Prefix label (optional) * http__status.label: Prefix label (optional)
* http_status.target: Target to retrieve the HTTP status from * http__status.target: Target to retrieve the HTTP status from
* http_status.expect: Expected HTTP status * http__status.expect: Expected HTTP status
""" """
from requests import head from requests import head
import psutil import psutil
import bumblebee.input
import bumblebee.output
import bumblebee.engine
class Module(bumblebee.engine.Module): import core.module
import core.widget
import core.decorators
class Module(core.module.Module):
UNK = 'UNK' UNK = 'UNK'
def __init__(self, engine, config): @core.decorators.every(seconds=30)
widget = bumblebee.output.Widget(full_text=self.output) def __init__(self, config):
super(Module, self).__init__(engine, config, widget) super().__init__(config, core.widget.Widget(self.output))
self._label = self.parameter('label') self.__label = self.parameter('label')
self._target = self.parameter('target') self.__target = self.parameter('target')
self._expect = self.parameter('expect', '200') self.__expect = self.parameter('expect', '200')
self._status = self.getStatus()
self._output = self.getOutput()
def labelize(self, s): def labelize(self, s):
if self._label is None: if self.__label is None:
return s return s
return '{}: {}'.format(self._label, s) return '{}: {}'.format(self.__label, s)
def getStatus(self): def getStatus(self):
try: try:
res = head(self._target) res = head(self.__target)
except Exception: except Exception as e:
print(e)
return self.UNK return self.UNK
else: else:
status = str(res.status_code) status = str(res.status_code)
self._status = status
return status return status
def getOutput(self): def getOutput(self):
if self._status == self._expect: if self.__status == self.__expect:
return self.labelize(self._status) return self.labelize(self.__status)
else: else:
reason = ' != {}'.format(self._expect) reason = ' != {}'.format(self.__expect)
return self.labelize('{}{}'.format(self._status, reason)) return self.labelize('{}{}'.format(self.__status, reason))
def output(self, widget): def output(self, widget):
return self._output return self.__output
def update(self, widgets): def update(self):
self.getStatus() self.__status = self.getStatus()
self._output = self.getOutput() self.__output = self.getOutput()
def state(self, widget): def state(self, widget):
if self._status == self.UNK: if self.__status == self.UNK:
return 'warning' return 'warning'
if self._status != self._expect: if self.__status != self.__expect:
return 'critical' return 'critical'
return self._output return self.__output
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4