def os_uptime(self): """Lazy evaluation of the server uptime.""" # Get the OS uptime. Because this is highly operating system dependent, several # different strategies may have to be tried: os_uptime_secs = None try: # For Linux: os_uptime_secs = float(open("/proc/uptime").read().split()[0]) except (IOError, KeyError): try: # For MacOs: from Quartz.QuartzCore import CACurrentMediaTime os_uptime_secs = CACurrentMediaTime() except ImportError: try: # for FreeBSD import ctypes from ctypes.util import find_library libc = ctypes.CDLL(find_library('c')) size = ctypes.c_size_t() buf = ctypes.c_int() size.value = ctypes.sizeof(buf) libc.sysctlbyname("kern.boottime", ctypes.byref(buf), ctypes.byref(size), None, 0) os_uptime_secs = time.time() - float(buf.value) except (AttributeError, IOError, NameError): pass return weewx.units.ValueHelper(value_t=(os_uptime_secs, "second", "group_deltatime"), formatter=self.formatter, converter=self.converter)
def os_uptime(self): """Lazy evaluation of the server uptime.""" # Get the OS uptime. Because this is highly operating system dependent, several # different strategies may have to be tried: os_uptime_secs = None try: # For Linux: os_uptime_secs = float(open("/proc/uptime").read().split()[0]) except (IOError, KeyError): try: # For MacOs: os_uptime_secs = CACurrentMediaTime() except NameError: pass return weewx.units.ValueHelper(value_t=(os_uptime_secs, "second", "group_deltatime"), formatter=self.formatter, converter=self.converter)
def os_uptime(self): """Lazy evaluation of the server uptime.""" # Get the OS uptime. Because this is highly operating system dependent, several # different strategies may have to be tried: os_uptime_secs = None try: # For Linux: os_uptime_secs = float(open("/proc/uptime").read().split()[0]) except (IOError, KeyError): try: # For MacOs: from Quartz.QuartzCore import CACurrentMediaTime os_uptime_secs = CACurrentMediaTime() except ImportError: try: # for FreeBSD import ctypes from ctypes.util import find_library libc = ctypes.CDLL(find_library('c')) size = ctypes.c_size_t() buf = ctypes.c_int() size.value = ctypes.sizeof(buf) libc.sysctlbyname("kern.boottime", ctypes.byref(buf), ctypes.byref(size), None, 0) os_uptime_secs = time.time() - float(buf.value) except (AttributeError, IOError, NameError): try: # For OpenBSD. See issue #428. import subprocess from datetime import datetime cmd = ['sysctl', 'kern.boottime'] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) o, e = proc.communicate() time_t = o.decode('ascii').split() time_as_string = time_t[1] + " " + time_t[2] + " " + time_t[4][:4] + " " + time_t[3] os_time = datetime.strptime(time_as_string, "%b %d %Y %H:%M:%S") epoch_time = (os_time - datetime(1970, 1, 1)).total_seconds() os_uptime_secs = time.time() - epoch_time except: pass return weewx.units.ValueHelper(value_t=(os_uptime_secs, "second", "group_deltatime"), formatter=self.formatter, converter=self.converter)
def _os_uptime(): """ Get the OS uptime. Because this is highly operating system dependent, several different strategies may have to be tried:""" try: # For Python 3.7 and later, most systems return time.clock_gettime(time.CLOCK_UPTIME) except AttributeError: pass try: # For Python 3.3 and later, most systems return time.clock_gettime(time.CLOCK_MONOTONIC) except AttributeError: pass try: # For Linux, Python 2 and 3: return float(open("/proc/uptime").read().split()[0]) except (IOError, KeyError, OSError): pass try: # For MacOS, Python 2: from Quartz.QuartzCore import CACurrentMediaTime return CACurrentMediaTime() except ImportError: pass try: # for FreeBSD, Python 2 import ctypes from ctypes.util import find_library libc = ctypes.CDLL(find_library('c')) size = ctypes.c_size_t() buf = ctypes.c_int() size.value = ctypes.sizeof(buf) libc.sysctlbyname("kern.boottime", ctypes.byref(buf), ctypes.byref(size), None, 0) os_uptime_secs = time.time() - float(buf.value) return os_uptime_secs except (ImportError, AttributeError, IOError, NameError): pass try: # For OpenBSD, Python 2. See issue #428. import subprocess from datetime import datetime cmd = ['sysctl', 'kern.boottime'] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) o, e = proc.communicate() # Check for errors if e: raise IOError time_t = o.decode('ascii').split() time_as_string = time_t[1] + " " + time_t[2] + " " + time_t[4][:4] + " " + time_t[3] os_time = datetime.strptime(time_as_string, "%b %d %Y %H:%M:%S") epoch_time = (os_time - datetime(1970, 1, 1)).total_seconds() os_uptime_secs = time.time() - epoch_time return os_uptime_secs except (IOError, IndexError, ValueError): pass # Nothing seems to be working. Return None return None