diff --git a/README.md b/README.md index 65750ac..b2f8d5e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Test Coverage](https://codeclimate.com/github/tobi-wan-kenobi/bumblebee-status/badges/coverage.svg)](https://codeclimate.com/github/tobi-wan-kenobi/bumblebee-status/coverage) [![Issue Count](https://codeclimate.com/github/tobi-wan-kenobi/bumblebee-status/badges/issue_count.svg)](https://codeclimate.com/github/tobi-wan-kenobi/bumblebee-status) -**Many, many thanks to all contributors! As of now, 41 of the modules are from various contributors (!), and only 19 from myself.** +**Many, many thanks to all contributors! As of now, 43 of the modules are from various contributors (!), and only 19 from myself.** ![Solarized Powerline](https://github.com/tobi-wan-kenobi/bumblebee-status/blob/master/screenshots/themes/powerline-solarized.png) diff --git a/bumblebee/modules/http_status.py b/bumblebee/modules/http_status.py new file mode 100644 index 0000000..ae84655 --- /dev/null +++ b/bumblebee/modules/http_status.py @@ -0,0 +1,68 @@ +# pylint: disable=C0111,R0903 + +"""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 +""" + +from requests import head + +import psutil +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +class Module(bumblebee.engine.Module): + UNK = "UNK" + + def __init__(self, engine, config): + widget = bumblebee.output.Widget(full_text=self.output) + super(Module, self).__init__(engine, config, widget) + + self._label = self.parameter("label") + self._target = self.parameter("target") + self._expect = self.parameter("expect", "200") + self._status = self.getStatus() + self._output = self.getOutput() + + def labelize(self, s): + if self._label is None: + return s + return "{}: {}".format(self._label, s) + + def getStatus(self): + try: + res = head(self._target) + except Exception: + 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) + else: + reason = " != {}".format(self._expect) + return self.labelize("{}{}".format(self._status, reason)) + + def output(self, widget): + return self._output + + def update(self, widgets): + self.getStatus() + self._output = self.getOutput() + + def state(self, widget): + if self._status == self.UNK: + return "warning" + if self._status != self._expect: + return "critical" + return self._output + + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/screenshots/network_traffic.gif b/screenshots/network_traffic.gif new file mode 100644 index 0000000..2cc0144 Binary files /dev/null and b/screenshots/network_traffic.gif differ diff --git a/tests/modules/test_http_status.py b/tests/modules/test_http_status.py new file mode 100644 index 0000000..463884d --- /dev/null +++ b/tests/modules/test_http_status.py @@ -0,0 +1,49 @@ +# pylint: disable=C0103,C0111 + +import mock +import unittest + +from bumblebee.modules.http_status import Module +from bumblebee.config import Config + +class TestHttpStatusModule(unittest.TestCase): + def test_status_success(self): + config = Config() + config.set("http_status.target", "http://example.org") + self.module = Module(engine=mock.Mock(), config={"config":config}) + + self.assertTrue(not "warning" in self.module.state(self.module.widgets()[0])) + self.assertTrue(not "critical" in self.module.state(self.module.widgets()[0])) + self.assertEqual(self.module.getStatus(), "200") + self.assertEqual(self.module.getOutput(), "200") + + def test_status_error(self): + config = Config() + config.set("http_status.expect", "not a 200") + config.set("http_status.target", "http://example.org") + self.module = Module(engine=mock.Mock(), config={"config":config}) + + self.assertTrue(not "warning" in self.module.state(self.module.widgets()[0])) + self.assertTrue("critical" in self.module.state(self.module.widgets()[0])) + self.assertEqual(self.module.getStatus(), "200") + self.assertEqual(self.module.getOutput(), "200 != not a 200") + + def test_label(self): + config = Config() + config.set("http_status.label", "example") + config.set("http_status.target", "http://example.org") + self.module = Module(engine=mock.Mock(), config={"config":config}) + + self.assertEqual(self.module.getOutput(), "example: 200") + + def test_unknow(self): + config = Config() + config.set("http_status.target", "invalid target") + self.module = Module(engine=mock.Mock(), config={"config":config}) + + self.assertTrue("warning" in self.module.state(self.module.widgets()[0])) + self.assertEqual(self.module.getStatus(), "UNK") + self.assertEqual(self.module.getOutput(), "UNK != 200") + + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4