Revert Linux network and Freebsd hardware changes

pull/81297/head
Abhijeet Kasurde 4 weeks ago
parent ca31dd1e90
commit 682532fb0d

@ -22,7 +22,6 @@ import struct
import time
from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector
from ansible.module_utils.facts.sysctl import get_sysctl
from ansible.module_utils.facts.timeout import TimeoutError, timeout
from ansible.module_utils.facts.utils import get_file_content, get_mount_size
@ -46,7 +45,6 @@ class FreeBSDHardware(Hardware):
def populate(self, collected_facts=None):
hardware_facts = {}
self.sysctl = get_sysctl(self.module, ['hw'])
cpu_facts = self.get_cpu_facts()
memory_facts = self.get_memory_facts()
uptime_facts = self.get_uptime_facts()
@ -71,18 +69,15 @@ class FreeBSDHardware(Hardware):
def get_cpu_facts(self):
cpu_facts = {}
cpu_facts['processor'] = []
cpu_facts['processor_count'] = self.sysctl.get('hw.ncpu') or ''
sysctl = self.module.get_bin_path('sysctl')
if sysctl:
rc, out, err = self.module.run_command("%s -n hw.ncpu" % sysctl, check_rc=False)
cpu_facts['processor_count'] = out.strip()
dmesg_boot = get_file_content(FreeBSDHardware.DMESG_BOOT)
if not dmesg_boot:
dmesg_cmd = self.module.get_bin_path(
"dmesg",
warning="falling back to sysctl for processor_count, default to [] for processor"
)
if dmesg_cmd is None:
return cpu_facts
try:
rc, dmesg_boot, err = self.module.run_command(dmesg_cmd, check_rc=False)
rc, dmesg_boot, err = self.module.run_command(self.module.get_bin_path("dmesg"), check_rc=False)
except Exception:
dmesg_boot = ''
@ -98,45 +93,41 @@ class FreeBSDHardware(Hardware):
def get_memory_facts(self):
memory_facts = {}
pagesize = pagecount = freecount = None
if 'vm.stats.vm.v_page_size' in self.sysctl:
pagesize = int(self.sysctl['vm.stats.vm.v_page_size'])
if 'vm.stats.vm.v_page_count' in self.sysctl:
pagecount = int(self.sysctl['vm.stats.vm.v_page_count'])
if 'vm.stats.vm.v_free_count' in self.sysctl:
freecount = int(self.sysctl['vm.stats.vm.v_free_count'])
if pagesize is not None:
if pagecount is not None:
memory_facts['memtotal_mb'] = pagesize * pagecount // 1024 // 1024
if freecount is not None:
memory_facts['memfree_mb'] = pagesize * freecount // 1024 // 1024
swapinfo_cmd = self.module.get_bin_path('swapinfo', warning="skipping swap facts")
if swapinfo_cmd is None:
return memory_facts
# Get swapinfo. swapinfo output looks like:
# Device 1M-blocks Used Avail Capacity
# /dev/ada0p3 314368 0 314368 0%
#
rc, out, err = self.module.run_command("%s -k" % swapinfo_cmd)
lines = out.splitlines()
if len(lines[-1]) == 0:
lines.pop()
data = lines[-1].split()
if data[0] != 'Device':
memory_facts['swaptotal_mb'] = int(data[1]) // 1024
memory_facts['swapfree_mb'] = int(data[3]) // 1024
sysctl = self.module.get_bin_path('sysctl')
if sysctl:
rc, out, err = self.module.run_command("%s vm.stats" % sysctl, check_rc=False)
for line in out.splitlines():
data = line.split()
if 'vm.stats.vm.v_page_size' in line:
pagesize = int(data[1])
if 'vm.stats.vm.v_page_count' in line:
pagecount = int(data[1])
if 'vm.stats.vm.v_free_count' in line:
freecount = int(data[1])
memory_facts['memtotal_mb'] = pagesize * pagecount // 1024 // 1024
memory_facts['memfree_mb'] = pagesize * freecount // 1024 // 1024
swapinfo = self.module.get_bin_path('swapinfo')
if swapinfo:
# Get swapinfo. swapinfo output looks like:
# Device 1M-blocks Used Avail Capacity
# /dev/ada0p3 314368 0 314368 0%
#
rc, out, err = self.module.run_command("%s -k" % swapinfo)
lines = out.splitlines()
if len(lines[-1]) == 0:
lines.pop()
data = lines[-1].split()
if data[0] != 'Device':
memory_facts['swaptotal_mb'] = int(data[1]) // 1024
memory_facts['swapfree_mb'] = int(data[3]) // 1024
return memory_facts
def get_uptime_facts(self):
# On FreeBSD, the default format is annoying to parse.
# Use -b to get the raw value and decode it.
sysctl_cmd = self.module.get_bin_path('sysctl', warning="skipping uptime fact")
if not sysctl_cmd:
return {}
sysctl_cmd = self.module.get_bin_path('sysctl')
cmd = [sysctl_cmd, '-b', 'kern.boottime']
# We need to get raw bytes, not UTF-8.
@ -240,10 +231,7 @@ class FreeBSDHardware(Hardware):
dmi_facts = {}
# Fall back to using dmidecode, if available
dmi_bin = self.module.get_bin_path('dmidecode', warning="skipping dmi facts")
if not dmi_bin:
return dmi_facts
dmi_bin = self.module.get_bin_path('dmidecode')
DMI_DICT = {
'bios_date': 'bios-release-date',
'bios_vendor': 'bios-vendor',
@ -264,22 +252,18 @@ class FreeBSDHardware(Hardware):
'product_version': 'system-version',
'system_vendor': 'system-manufacturer',
}
if dmi_bin is None:
dmi_facts = dict.fromkeys(
DMI_DICT.keys(),
'NA'
)
return dmi_facts
for (k, v) in DMI_DICT.items():
(rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v))
if rc == 0:
# Strip out commented lines (specific dmidecode output)
# FIXME: why add the fact and then test if it is json?
dmi_facts[k] = ''.join([line for line in out.splitlines() if not line.startswith('#')])
try:
json.dumps(dmi_facts[k])
except UnicodeDecodeError:
if dmi_bin is not None:
(rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v))
if rc == 0:
# Strip out commented lines (specific dmidecode output)
# FIXME: why add the fact and then test if it is json?
dmi_facts[k] = ''.join([line for line in out.splitlines() if not line.startswith('#')])
try:
json.dumps(dmi_facts[k])
except UnicodeDecodeError:
dmi_facts[k] = 'NA'
else:
dmi_facts[k] = 'NA'
else:
dmi_facts[k] = 'NA'

@ -45,10 +45,7 @@ class LinuxNetwork(Network):
def populate(self, collected_facts=None):
network_facts = {}
ip_path = self.module.get_bin_path(
'ip',
warning="skipping network facts"
)
ip_path = self.module.get_bin_path('ip')
if ip_path is None:
return network_facts
default_ipv4, default_ipv6 = self.get_default_interfaces(ip_path,
@ -298,12 +295,8 @@ class LinuxNetwork(Network):
if not address == '::1':
ips['all_ipv6_addresses'].append(address)
ip_path = self.module.get_bin_path(
"ip",
warning="skipping interface info facts"
)
if ip_path is None:
continue
ip_path = self.module.get_bin_path("ip")
args = [ip_path, 'addr', 'show', 'primary', 'dev', device]
rc, primary_data, stderr = self.module.run_command(args, errors='surrogate_then_replace')
if rc == 0:
@ -336,35 +329,31 @@ class LinuxNetwork(Network):
def get_ethtool_data(self, device):
data = {}
ethtool_path = self.module.get_bin_path(
"ethtool",
warning="skipping ethtool facts"
)
if ethtool_path is None:
return data
args = [ethtool_path, '-k', device]
rc, stdout, stderr = self.module.run_command(args, errors='surrogate_then_replace')
# FIXME: exit early on falsey if we can
if rc == 0:
features = {}
for line in stdout.strip().splitlines():
if not line or line.endswith(":"):
continue
key, value = line.split(": ")
if not value:
continue
features[key.strip().replace('-', '_')] = value.strip()
data['features'] = features
ethtool_path = self.module.get_bin_path("ethtool")
# FIXME: exit early on falsey ethtool_path and un-indent
if ethtool_path:
args = [ethtool_path, '-k', device]
rc, stdout, stderr = self.module.run_command(args, errors='surrogate_then_replace')
# FIXME: exit early on falsey if we can
if rc == 0:
features = {}
for line in stdout.strip().splitlines():
if not line or line.endswith(":"):
continue
key, value = line.split(": ")
if not value:
continue
features[key.strip().replace('-', '_')] = value.strip()
data['features'] = features
args = [ethtool_path, '-T', device]
rc, stdout, stderr = self.module.run_command(args, errors='surrogate_then_replace')
if rc == 0:
data['timestamping'] = [m.lower() for m in re.findall(r'SOF_TIMESTAMPING_(\w+)', stdout)]
data['hw_timestamp_filters'] = [m.lower() for m in re.findall(r'HWTSTAMP_FILTER_(\w+)', stdout)]
m = re.search(r'PTP Hardware Clock: (\d+)', stdout)
if m:
data['phc_index'] = int(m.groups()[0])
args = [ethtool_path, '-T', device]
rc, stdout, stderr = self.module.run_command(args, errors='surrogate_then_replace')
if rc == 0:
data['timestamping'] = [m.lower() for m in re.findall(r'SOF_TIMESTAMPING_(\w+)', stdout)]
data['hw_timestamp_filters'] = [m.lower() for m in re.findall(r'HWTSTAMP_FILTER_(\w+)', stdout)]
m = re.search(r'PTP Hardware Clock: (\d+)', stdout)
if m:
data['phc_index'] = int(m.groups()[0])
return data

Loading…
Cancel
Save