From b28c73af622a81f83e26b81f5752e47e5d8efc4e Mon Sep 17 00:00:00 2001 From: Anatoly Pugachev Date: Tue, 16 Apr 2019 01:01:24 +0300 Subject: [PATCH] Fixes solaris (sunos) uptime in ansible facts module (#54626) * correct uptime on solaris by using system boot_time instead of snaptime * add unit test --- .../fragments/solaris_uptime_boot_time.yaml | 2 ++ lib/ansible/module_utils/facts/hardware/sunos.py | 14 ++++++-------- .../hardware/test_sunos_get_uptime_facts.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/solaris_uptime_boot_time.yaml create mode 100644 test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py diff --git a/changelogs/fragments/solaris_uptime_boot_time.yaml b/changelogs/fragments/solaris_uptime_boot_time.yaml new file mode 100644 index 0000000000..0a55f964af --- /dev/null +++ b/changelogs/fragments/solaris_uptime_boot_time.yaml @@ -0,0 +1,2 @@ +bugfixes: + - facts - change to use boot_time on a solaris OS to report correct uptime (https://github.com/ansible/ansible/issues/53635) diff --git a/lib/ansible/module_utils/facts/hardware/sunos.py b/lib/ansible/module_utils/facts/hardware/sunos.py index 89a7e720ff..90696beee6 100644 --- a/lib/ansible/module_utils/facts/hardware/sunos.py +++ b/lib/ansible/module_utils/facts/hardware/sunos.py @@ -17,6 +17,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import re +import time from ansible.module_utils.six.moves import reduce @@ -266,18 +267,15 @@ class SunOSHardware(Hardware): def get_uptime_facts(self): uptime_facts = {} - # On Solaris, unix:0:system_misc:snaptime is created shortly after machine boots up - # and displays tiem in seconds. This is much easier than using uptime as we would - # need to have a parsing procedure for translating from human-readable to machine-readable - # format. - # Example output: - # unix:0:system_misc:snaptime 1175.410463590 - rc, out, err = self.module.run_command('/usr/bin/kstat -p unix:0:system_misc:snaptime') + # sample kstat output: + # unix:0:system_misc:boot_time 1548249689 + rc, out, err = self.module.run_command('/usr/bin/kstat -p unix:0:system_misc:boot_time') if rc != 0: return - uptime_facts['uptime_seconds'] = int(float(out.split('\t')[1])) + # uptime = $current_time - $boot_time + uptime_facts['uptime_seconds'] = int(time.time() - int(out.split('\t')[1])) return uptime_facts diff --git a/test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py b/test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py new file mode 100644 index 0000000000..3777e72500 --- /dev/null +++ b/test/units/module_utils/facts/hardware/test_sunos_get_uptime_facts.py @@ -0,0 +1,16 @@ +import time +from ansible.module_utils.facts.hardware import sunos + + +def test_sunos_get_uptime_facts(mocker): + kstat_output = '\nunix:0:system_misc:boot_time\t1548249689\n' + + module_mock = mocker.patch('ansible.module_utils.basic.AnsibleModule') + module = module_mock() + module.run_command.return_value = (0, kstat_output, '') + + inst = sunos.SunOSHardware(module) + + expected = int(time.time()) - 1548249689 + result = inst.get_uptime_facts() + assert expected == result['uptime_seconds']