Merge pull request #411 from valkheim/http_status
Add http status module
This commit is contained in:
commit
7636470537
3 changed files with 118 additions and 1 deletions
|
@ -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, 42 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)
|
||||
|
||||
|
|
68
bumblebee/modules/http_status.py
Normal file
68
bumblebee/modules/http_status.py
Normal file
|
@ -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
|
49
tests/modules/test_http_status.py
Normal file
49
tests/modules/test_http_status.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue