From 7813ffd719e73670e715e09f0e4256facf453002 Mon Sep 17 00:00:00 2001 From: Chris Blumentritt Date: Fri, 13 Mar 2015 15:35:31 -0500 Subject: [PATCH] Adding uptime_seconds fact for linux and darwin platforms Adds ansible_uptime_seconds facts for linux and darwin platforms. BSD platforms may also work. --- lib/ansible/module_utils/facts.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py index 424c388fb6e..c1951925e4b 100644 --- a/lib/ansible/module_utils/facts.py +++ b/lib/ansible/module_utils/facts.py @@ -652,6 +652,7 @@ class LinuxHardware(Hardware): self.get_memory_facts() self.get_dmi_facts() self.get_device_facts() + self.get_uptime_facts() try: self.get_mount_facts() except TimeoutError: @@ -990,6 +991,9 @@ class LinuxHardware(Hardware): self.facts['devices'][diskname] = d + def get_uptime_facts(self): + uptime_seconds_string = get_file_content('/proc/uptime').split(' ')[0] + self.facts['uptime_seconds'] = int(float(uptime_seconds_string)) class SunOSHardware(Hardware): """ @@ -1588,6 +1592,7 @@ class Darwin(Hardware): self.get_mac_facts() self.get_cpu_facts() self.get_memory_facts() + self.get_uptime_facts() return self.facts def get_sysctl(self): @@ -1635,6 +1640,12 @@ class Darwin(Hardware): if rc == 0: self.facts['memfree_mb'] = long(out.splitlines()[-1].split()[1]) / 1024 / 1024 + def get_uptime_facts(self): + kern_boottime = self.sysctl['kern.boottime'] + boottime = datetime.datetime.strptime(kern_boottime, "%a %b %d %H:%M:%S %Y") + delta = datetime.datetime.now() - boottime + self.facts['uptime_seconds'] = int(delta.total_seconds()) + class Network(Facts): """ This is a generic Network subclass of Facts. This should be further