diff --git a/AUTHORS b/AUTHORS index 2c34875..15ae75e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1,2 @@ -Kevin Carter +Kevin Carter Major Hayden diff --git a/ChangeLog b/ChangeLog index 94e909a..c244aa2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,7 @@ CHANGES ======= -* Added KVM Metric plugin -* Couple of updates: telegraf line protocol, dynamic imports, metadata +* Added KVM Metric plugin (#2) +* Couple of updates: telegraf line protocol, dynamic imports, metadata (#1) * Proof of concept * Initial commit diff --git a/monitorstack/plugins/uptime.py b/monitorstack/plugins/uptime.py index 1b8e19e..3686537 100644 --- a/monitorstack/plugins/uptime.py +++ b/monitorstack/plugins/uptime.py @@ -26,9 +26,7 @@ from monitorstack.cli import pass_context @pass_context def cli(ctx): """Get system uptime.""" - with open('/proc/uptime', 'r') as f: - output = f.read() - uptime = output.split()[0] + uptime = get_uptime() output = { 'exit_code': 0, 'message': 'uptime is ok', @@ -37,7 +35,19 @@ def cli(ctx): 'platform': platform.platform() }, 'variables': { - 'uptime': uptime + 'uptime': str(uptime) } } return output + + +def get_uptime(): + """Read the uptime from the proc filesystem.""" + with open('/proc/uptime', 'r') as f: + output = f.read() + + # /proc/uptime outputs two numbers: seconds since start (which we want) + # and seconds the machine has spent idle (we don't want that) + uptime = output.split()[0] + + return float(uptime) diff --git a/tests/test_uptime.py b/tests/test_uptime.py index ff764a9..fe04bff 100644 --- a/tests/test_uptime.py +++ b/tests/test_uptime.py @@ -15,10 +15,12 @@ """Tests for the base class.""" import json +import sys from click.testing import CliRunner from monitorstack.cli import cli +from monitorstack.plugins.uptime import get_uptime class TestUptime(object): @@ -31,3 +33,9 @@ class TestUptime(object): result_json = json.loads(result.output) assert 'uptime' in result_json['variables'] assert result.exit_code == 0 + + def test_get_uptime(self): + """Ensure the cli() method works.""" + uptime = get_uptime() + assert isinstance(uptime, float) + assert uptime > 0