2020-04-19 14:39:00 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Display HTTP status code
|
|
|
|
|
|
|
|
Parameters:
|
2020-04-19 14:42:40 +02:00
|
|
|
* http__status.label: Prefix label (optional)
|
|
|
|
* http__status.target: Target to retrieve the HTTP status from
|
|
|
|
* http__status.expect: Expected HTTP status
|
2020-04-19 14:39:00 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
from requests import head
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
2020-04-19 14:42:40 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.decorators
|
|
|
|
|
|
|
|
class Module(core.module.Module):
|
2020-04-19 14:39:21 +02:00
|
|
|
UNK = 'UNK'
|
2020-04-19 14:39:00 +02:00
|
|
|
|
2020-04-19 14:42:40 +02:00
|
|
|
@core.decorators.every(seconds=30)
|
|
|
|
def __init__(self, config):
|
|
|
|
super().__init__(config, core.widget.Widget(self.output))
|
2020-04-19 14:39:00 +02:00
|
|
|
|
2020-04-19 14:42:40 +02:00
|
|
|
self.__label = self.parameter('label')
|
|
|
|
self.__target = self.parameter('target')
|
|
|
|
self.__expect = self.parameter('expect', '200')
|
2020-04-19 14:39:00 +02:00
|
|
|
|
|
|
|
def labelize(self, s):
|
2020-04-19 14:42:40 +02:00
|
|
|
if self.__label is None:
|
2020-04-19 14:39:00 +02:00
|
|
|
return s
|
2020-04-19 14:42:40 +02:00
|
|
|
return '{}: {}'.format(self.__label, s)
|
2020-04-19 14:39:00 +02:00
|
|
|
|
|
|
|
def getStatus(self):
|
|
|
|
try:
|
2020-04-19 14:42:40 +02:00
|
|
|
res = head(self.__target)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2020-04-19 14:39:00 +02:00
|
|
|
return self.UNK
|
|
|
|
else:
|
|
|
|
status = str(res.status_code)
|
|
|
|
return status
|
|
|
|
|
|
|
|
def getOutput(self):
|
2020-04-19 14:42:40 +02:00
|
|
|
if self.__status == self.__expect:
|
|
|
|
return self.labelize(self.__status)
|
2020-04-19 14:39:00 +02:00
|
|
|
else:
|
2020-04-19 14:42:40 +02:00
|
|
|
reason = ' != {}'.format(self.__expect)
|
|
|
|
return self.labelize('{}{}'.format(self.__status, reason))
|
2020-04-19 14:39:00 +02:00
|
|
|
|
|
|
|
def output(self, widget):
|
2020-04-19 14:42:40 +02:00
|
|
|
return self.__output
|
2020-04-19 14:39:00 +02:00
|
|
|
|
2020-04-19 14:42:40 +02:00
|
|
|
def update(self):
|
|
|
|
self.__status = self.getStatus()
|
|
|
|
self.__output = self.getOutput()
|
2020-04-19 14:39:00 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
2020-04-19 14:42:40 +02:00
|
|
|
if self.__status == self.UNK:
|
2020-04-19 14:39:21 +02:00
|
|
|
return 'warning'
|
2020-04-19 14:42:40 +02:00
|
|
|
if self.__status != self.__expect:
|
2020-04-19 14:39:21 +02:00
|
|
|
return 'critical'
|
2020-04-19 14:42:40 +02:00
|
|
|
return self.__output
|
2020-04-19 14:39:00 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|