|
@@ -21,6 +21,7 @@ import re
|
|
|
import subprocess
|
|
|
import os.path
|
|
|
import platform
|
|
|
+import time
|
|
|
|
|
|
class SysInfo:
|
|
|
def __init__(self):
|
|
@@ -127,9 +128,9 @@ class SysInfo:
|
|
|
"""Returns network connection information (as a multi-line string)."""
|
|
|
return self._net_connections
|
|
|
|
|
|
-class SysInfoLinux(SysInfo):
|
|
|
- """Linux implementation of the SysInfo class.
|
|
|
- See the base class documentation for more information.
|
|
|
+class SysInfoPOSIX(SysInfo):
|
|
|
+ """Common POSIX implementation of the SysInfo class.
|
|
|
+ See the SysInfo class documentation for more information.
|
|
|
"""
|
|
|
def __init__(self):
|
|
|
super().__init__()
|
|
@@ -137,14 +138,21 @@ class SysInfoLinux(SysInfo):
|
|
|
self._num_processors = os.sysconf('SC_NPROCESSORS_CONF')
|
|
|
self._endianness = sys.byteorder
|
|
|
|
|
|
- with open('/proc/sys/kernel/hostname') as f:
|
|
|
- self._hostname = f.read().strip()
|
|
|
-
|
|
|
u = os.uname()
|
|
|
self._platform_name = u[0]
|
|
|
self._platform_version = u[2]
|
|
|
self._platform_machine = u[4]
|
|
|
|
|
|
+class SysInfoLinux(SysInfoPOSIX):
|
|
|
+ """Linux implementation of the SysInfo class.
|
|
|
+ See the SysInfo class documentation for more information.
|
|
|
+ """
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+ with open('/proc/sys/kernel/hostname') as f:
|
|
|
+ self._hostname = f.read().strip()
|
|
|
+
|
|
|
with open('/proc/version') as f:
|
|
|
self._platform_is_smp = ' SMP ' in f.read().strip()
|
|
|
|
|
@@ -269,6 +277,123 @@ class SysInfoLinux(SysInfo):
|
|
|
if self._net_connections is None:
|
|
|
self._net_connections = 'Unknown'
|
|
|
|
|
|
+class SysInfoBSD(SysInfoPOSIX):
|
|
|
+ """Common BSD implementation of the SysInfo class.
|
|
|
+ See the SysInfo class documentation for more information.
|
|
|
+ """
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+ self._hostname = None
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['hostname'])
|
|
|
+ self._hostname = s.decode('utf-8')
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ if self._hostname is None:
|
|
|
+ self._hostname = 'Unknown'
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['sysctl', '-n', 'kern.boottime'])
|
|
|
+ t = s.decode('utf-8').strip()
|
|
|
+ sec = time.time() - int(t)
|
|
|
+ self._uptime = int(round(sec))
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['sysctl', '-n', 'vm.loadavg'])
|
|
|
+ l = s.decode('utf-8').strip().split(' ')
|
|
|
+ if len(l) >= 3:
|
|
|
+ self._loadavg = [float(l[0]), float(l[1]), float(l[2])]
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['sysctl', '-n', 'hw.physmem'])
|
|
|
+ self._mem_total = int(s.decode('utf-8').strip())
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['vmstat'])
|
|
|
+ lines = s.decode('utf-8').split('\n')
|
|
|
+ v = re.split('\s+', lines[2])
|
|
|
+ used = int(v[4]) * 1024
|
|
|
+ self._mem_free = self._mem_total - used
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ self._platform_distro = self._platform_name + ' ' + self._platform_version
|
|
|
+
|
|
|
+class SysInfoOpenBSD(SysInfoBSD):
|
|
|
+ """OpenBSD implementation of the SysInfo class.
|
|
|
+ See the SysInfo class documentation for more information.
|
|
|
+ """
|
|
|
+ def __init__(self):
|
|
|
+ super().__init__()
|
|
|
+
|
|
|
+
|
|
|
+ self._platform_is_smp = False
|
|
|
+ self._mem_cached = -1
|
|
|
+ self._mem_buffers = -1
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['swapctl', '-s', '-k'])
|
|
|
+ l = s.decode('utf-8').strip()
|
|
|
+ r = re.match('^total: (\d+) 1K-blocks allocated, (\d+) used, (\d+) available', l)
|
|
|
+ if r:
|
|
|
+ self._swap_total = int(r.group(1).strip()) * 1024
|
|
|
+ self._swap_free = int(r.group(3).strip()) * 1024
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ self._net_interfaces = None
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['ifconfig'])
|
|
|
+ self._net_interfaces = s.decode('utf-8')
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ if self._net_interfaces is None:
|
|
|
+ self._net_interfaces = 'Unknown'
|
|
|
+
|
|
|
+ self._net_routing_table = None
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['route', '-n', 'show'])
|
|
|
+ self._net_routing_table = s.decode('utf-8')
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ if self._net_routing_table is None:
|
|
|
+ self._net_routing_table = 'Unknown'
|
|
|
+
|
|
|
+ self._net_stats = None
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['netstat', '-s'])
|
|
|
+ self._net_stats = s.decode('utf-8')
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ if self._net_stats is None:
|
|
|
+ self._net_stats = 'Unknown'
|
|
|
+
|
|
|
+ self._net_connections = None
|
|
|
+
|
|
|
+ try:
|
|
|
+ s = subprocess.check_output(['netstat', '-an'])
|
|
|
+ self._net_connections = s.decode('utf-8')
|
|
|
+ except (subprocess.CalledProcessError, OSError):
|
|
|
+ pass
|
|
|
+
|
|
|
+ if self._net_connections is None:
|
|
|
+ self._net_connections = 'Unknown'
|
|
|
+
|
|
|
class SysInfoTestcase(SysInfo):
|
|
|
def __init__(self):
|
|
|
super().__init__()
|
|
@@ -280,6 +405,8 @@ def SysInfoFromFactory():
|
|
|
osname = platform.system()
|
|
|
if osname == 'Linux':
|
|
|
return SysInfoLinux()
|
|
|
+ elif osname == 'OpenBSD':
|
|
|
+ return SysInfoOpenBSD()
|
|
|
elif osname == 'BIND10Testcase':
|
|
|
return SysInfoTestcase()
|
|
|
else:
|