[modules/http_status] Update to latest API
This commit is contained in:
parent
967ec5892b
commit
39f2e6bfea
1 changed files with 30 additions and 32 deletions
|
@ -3,66 +3,64 @@
|
|||
"""Display HTTP status code
|
||||
|
||||
Parameters:
|
||||
* http_status.label: Prefix label (optional)
|
||||
* http_status.target: Target to retrieve the HTTP status from
|
||||
* http_status.expect: Expected HTTP status
|
||||
* http__status.label: Prefix label (optional)
|
||||
* http__status.target: Target to retrieve the HTTP status from
|
||||
* http__status.expect: Expected HTTP status
|
||||
"""
|
||||
|
||||
from requests import head
|
||||
|
||||
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'
|
||||
|
||||
def __init__(self, engine, config):
|
||||
widget = bumblebee.output.Widget(full_text=self.output)
|
||||
super(Module, self).__init__(engine, config, widget)
|
||||
@core.decorators.every(seconds=30)
|
||||
def __init__(self, config):
|
||||
super().__init__(config, core.widget.Widget(self.output))
|
||||
|
||||
self._label = self.parameter('label')
|
||||
self._target = self.parameter('target')
|
||||
self._expect = self.parameter('expect', '200')
|
||||
self._status = self.getStatus()
|
||||
self._output = self.getOutput()
|
||||
self.__label = self.parameter('label')
|
||||
self.__target = self.parameter('target')
|
||||
self.__expect = self.parameter('expect', '200')
|
||||
|
||||
def labelize(self, s):
|
||||
if self._label is None:
|
||||
if self.__label is None:
|
||||
return s
|
||||
return '{}: {}'.format(self._label, s)
|
||||
return '{}: {}'.format(self.__label, s)
|
||||
|
||||
def getStatus(self):
|
||||
try:
|
||||
res = head(self._target)
|
||||
except Exception:
|
||||
res = head(self.__target)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return self.UNK
|
||||
else:
|
||||
status = str(res.status_code)
|
||||
self._status = status
|
||||
return status
|
||||
|
||||
def getOutput(self):
|
||||
if self._status == self._expect:
|
||||
return self.labelize(self._status)
|
||||
if self.__status == self.__expect:
|
||||
return self.labelize(self.__status)
|
||||
else:
|
||||
reason = ' != {}'.format(self._expect)
|
||||
return self.labelize('{}{}'.format(self._status, reason))
|
||||
reason = ' != {}'.format(self.__expect)
|
||||
return self.labelize('{}{}'.format(self.__status, reason))
|
||||
|
||||
def output(self, widget):
|
||||
return self._output
|
||||
return self.__output
|
||||
|
||||
def update(self, widgets):
|
||||
self.getStatus()
|
||||
self._output = self.getOutput()
|
||||
def update(self):
|
||||
self.__status = self.getStatus()
|
||||
self.__output = self.getOutput()
|
||||
|
||||
def state(self, widget):
|
||||
if self._status == self.UNK:
|
||||
if self.__status == self.UNK:
|
||||
return 'warning'
|
||||
if self._status != self._expect:
|
||||
if self.__status != self.__expect:
|
||||
return 'critical'
|
||||
return self._output
|
||||
|
||||
return self.__output
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
Loading…
Reference in a new issue