| 1 |
# This file is part of Ansible |
| 2 |
# |
| 3 |
# Ansible is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# Ansible is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
import re |
| 19 |
import time |
| 20 |
|
| 21 |
from ansible.module_utils.common.text.converters import to_text |
| 22 |
|
| 23 |
from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector |
| 24 |
from ansible.module_utils.facts import timeout |
| 25 |
|
| 26 |
from ansible.module_utils.facts.utils import get_file_content, get_mount_size |
| 27 |
from ansible.module_utils.facts.sysctl import get_sysctl |
| 28 |
|
| 29 |
|
| 30 |
class OpenBSDHardware(Hardware): |
| 31 |
""" |
| 32 |
OpenBSD-specific subclass of Hardware. Defines memory, CPU and device facts: |
| 33 |
- memfree_mb |
| 34 |
- memtotal_mb |
| 35 |
- swapfree_mb |
| 36 |
- swaptotal_mb |
| 37 |
- processor (a list) |
| 38 |
- processor_cores |
| 39 |
- processor_count |
| 40 |
- processor_speed |
| 41 |
- uptime_seconds |
| 42 |
|
| 43 |
In addition, it also defines number of DMI facts and device facts. |
| 44 |
""" |
| 45 |
platform = 'OpenBSD' |
| 46 |
|
| 47 |
def populate(self, collected_facts=None): |
| 48 |
hardware_facts = {} |
| 49 |
self.sysctl = get_sysctl(self.module, ['hw']) |
| 50 |
|
| 51 |
hardware_facts.update(self.get_processor_facts()) |
| 52 |
hardware_facts.update(self.get_memory_facts()) |
| 53 |
hardware_facts.update(self.get_device_facts()) |
| 54 |
hardware_facts.update(self.get_dmi_facts()) |
| 55 |
hardware_facts.update(self.get_uptime_facts()) |
| 56 |
|
| 57 |
# storage devices notoriously prone to hang/block so they are under a timeout |
| 58 |
try: |
| 59 |
hardware_facts.update(self.get_mount_facts()) |
| 60 |
except timeout.TimeoutError: |
| 61 |
pass |
| 62 |
|
| 63 |
return hardware_facts |
| 64 |
|
| 65 |
@timeout.timeout() |
| 66 |
def get_mount_facts(self): |
| 67 |
mount_facts = {} |
| 68 |
|
| 69 |
mount_facts['mounts'] = [] |
| 70 |
fstab = get_file_content('/etc/fstab') |
| 71 |
if fstab: |
| 72 |
for line in fstab.splitlines(): |
| 73 |
if line.startswith('#') or line.strip() == '': |
| 74 |
continue |
| 75 |
fields = re.sub(r'\s+', ' ', line).split() |
| 76 |
if fields[1] == 'none' or fields[3] == 'xx': |
| 77 |
continue |
| 78 |
mount_statvfs_info = get_mount_size(fields[1]) |
| 79 |
mount_info = {'mount': fields[1], |
| 80 |
'device': fields[0], |
| 81 |
'fstype': fields[2], |
| 82 |
'options': fields[3]} |
| 83 |
mount_info.update(mount_statvfs_info) |
| 84 |
mount_facts['mounts'].append(mount_info) |
| 85 |
return mount_facts |
| 86 |
|
| 87 |
def get_memory_facts(self): |
| 88 |
memory_facts = {} |
| 89 |
# Get free memory. vmstat output looks like: |
| 90 |
# procs memory page disks traps cpu |
| 91 |
# r b w avm fre flt re pi po fr sr wd0 fd0 int sys cs us sy id |
| 92 |
# 0 0 0 47512 28160 51 0 0 0 0 0 1 0 116 89 17 0 1 99 |
| 93 |
rc, out, err = self.module.run_command("/usr/bin/vmstat") |
| 94 |
if rc == 0: |
| 95 |
memory_facts['memfree_mb'] = int(out.splitlines()[-1].split()[4]) // 1024 |
| 96 |
memory_facts['memtotal_mb'] = int(self.sysctl['hw.physmem']) // 1024 // 1024 |
| 97 |
|
| 98 |
# Get swapctl info. swapctl output looks like: |
| 99 |
# total: 69268 1K-blocks allocated, 0 used, 69268 available |
| 100 |
# And for older OpenBSD: |
| 101 |
# total: 69268k bytes allocated = 0k used, 69268k available |
| 102 |
rc, out, err = self.module.run_command("/sbin/swapctl -sk") |
| 103 |
if rc == 0: |
| 104 |
swaptrans = {ord(u'k'): None, |
| 105 |
ord(u'm'): None, |
| 106 |
ord(u'g'): None} |
| 107 |
data = to_text(out, errors='surrogate_or_strict').split() |
| 108 |
memory_facts['swapfree_mb'] = int(data[-2].translate(swaptrans)) // 1024 |
| 109 |
memory_facts['swaptotal_mb'] = int(data[1].translate(swaptrans)) // 1024 |
| 110 |
|
| 111 |
return memory_facts |
| 112 |
|
| 113 |
def get_uptime_facts(self): |
| 114 |
# On openbsd, we need to call it with -n to get this value as an int. |
| 115 |
sysctl_cmd = self.module.get_bin_path('sysctl') |
| 116 |
if sysctl_cmd is None: |
| 117 |
return {} |
| 118 |
|
| 119 |
cmd = [sysctl_cmd, '-n', 'kern.boottime'] |
| 120 |
|
| 121 |
rc, out, err = self.module.run_command(cmd) |
| 122 |
|
| 123 |
if rc != 0: |
| 124 |
return {} |
| 125 |
|
| 126 |
kern_boottime = out.strip() |
| 127 |
if not kern_boottime.isdigit(): |
| 128 |
return {} |
| 129 |
|
| 130 |
return { |
| 131 |
'uptime_seconds': int(time.time() - int(kern_boottime)), |
| 132 |
} |
| 133 |
|
| 134 |
def get_processor_facts(self): |
| 135 |
cpu_facts = {} |
| 136 |
processor = [] |
| 137 |
for i in range(int(self.sysctl['hw.ncpuonline'])): |
| 138 |
processor.append(self.sysctl['hw.model']) |
| 139 |
|
| 140 |
cpu_facts['processor'] = processor |
| 141 |
# The following is partly a lie because there is no reliable way to |
| 142 |
# determine the number of physical CPUs in the system. We can only |
| 143 |
# query the number of logical CPUs, which hides the number of cores. |
| 144 |
# On amd64/i386 we could try to inspect the smt/core/package lines in |
| 145 |
# dmesg, however even those have proven to be unreliable. |
| 146 |
# So take a shortcut and report the logical number of processors in |
| 147 |
# 'processor_count' and 'processor_cores' and leave it at that. |
| 148 |
cpu_facts['processor_count'] = self.sysctl['hw.ncpuonline'] |
| 149 |
cpu_facts['processor_cores'] = self.sysctl['hw.ncpuonline'] |
| 150 |
|
| 151 |
return cpu_facts |
| 152 |
|
| 153 |
def get_device_facts(self): |
| 154 |
device_facts = {} |
| 155 |
devices = [] |
| 156 |
devices.extend(self.sysctl['hw.disknames'].split(',')) |
| 157 |
device_facts['devices'] = devices |
| 158 |
|
| 159 |
return device_facts |
| 160 |
|
| 161 |
def get_dmi_facts(self): |
| 162 |
dmi_facts = {} |
| 163 |
# We don't use dmidecode(8) here because: |
| 164 |
# - it would add dependency on an external package |
| 165 |
# - dmidecode(8) can only be ran as root |
| 166 |
# So instead we rely on sysctl(8) to provide us the information on a |
| 167 |
# best-effort basis. As a bonus we also get facts on non-amd64/i386 |
| 168 |
# platforms this way. |
| 169 |
sysctl_to_dmi = { |
| 170 |
'hw.product': 'product_name', |
| 171 |
'hw.version': 'product_version', |
| 172 |
'hw.uuid': 'product_uuid', |
| 173 |
'hw.serialno': 'product_serial', |
| 174 |
'hw.vendor': 'system_vendor', |
| 175 |
} |
| 176 |
|
| 177 |
for mib in sysctl_to_dmi: |
| 178 |
if mib in self.sysctl: |
| 179 |
dmi_facts[sysctl_to_dmi[mib]] = self.sysctl[mib] |
| 180 |
|
| 181 |
return dmi_facts |
| 182 |
|
| 183 |
|
| 184 |
class OpenBSDHardwareCollector(HardwareCollector): |
| 185 |
_fact_class = OpenBSDHardware |
| 186 |
_platform = 'OpenBSD' |