Debian Codegraph @

lib/ansible/module_utils/facts/hardware/netbsd.py

raw
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 os
19 import re
20 import time
21
22 from functools import reduce
23
24 from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector
25 from ansible.module_utils.facts.timeout import TimeoutError, timeout
26
27 from ansible.module_utils.facts.utils import get_file_content, get_file_lines, get_mount_size
28 from ansible.module_utils.facts.sysctl import get_sysctl
29
30
31 class NetBSDHardware(Hardware):
32 """
33 NetBSD-specific subclass of Hardware. Defines memory and CPU facts:
34 - memfree_mb
35 - memtotal_mb
36 - swapfree_mb
37 - swaptotal_mb
38 - processor (a list)
39 - processor_cores
40 - processor_count
41 - devices
42 - uptime_seconds
43 """
44 platform = 'NetBSD'
45 MEMORY_FACTS = ['MemTotal', 'SwapTotal', 'MemFree', 'SwapFree']
46
47 def populate(self, collected_facts=None):
48 hardware_facts = {}
49 self.sysctl = get_sysctl(self.module, ['machdep'])
50 cpu_facts = self.get_cpu_facts()
51 memory_facts = self.get_memory_facts()
52
53 mount_facts = {}
54 try:
55 mount_facts = self.get_mount_facts()
56 except TimeoutError:
57 pass
58
59 dmi_facts = self.get_dmi_facts()
60 uptime_facts = self.get_uptime_facts()
61
62 hardware_facts.update(cpu_facts)
63 hardware_facts.update(memory_facts)
64 hardware_facts.update(mount_facts)
65 hardware_facts.update(dmi_facts)
66 hardware_facts.update(uptime_facts)
67
68 return hardware_facts
69
70 def get_cpu_facts(self):
71 cpu_facts = {}
72
73 i = 0
74 physid = 0
75 sockets = {}
76 if not os.access("/proc/cpuinfo", os.R_OK):
77 return cpu_facts
78 cpu_facts['processor'] = []
79 for line in get_file_lines("/proc/cpuinfo"):
80 data = line.split(":", 1)
81 key = data[0].strip()
82 # model name is for Intel arch, Processor (mind the uppercase P)
83 # works for some ARM devices, like the Sheevaplug.
84 if key == 'model name' or key == 'Processor':
85 if 'processor' not in cpu_facts:
86 cpu_facts['processor'] = []
87 cpu_facts['processor'].append(data[1].strip())
88 i += 1
89 elif key == 'physical id':
90 physid = data[1].strip()
91 if physid not in sockets:
92 sockets[physid] = 1
93 elif key == 'cpu cores':
94 sockets[physid] = int(data[1].strip())
95 if len(sockets) > 0:
96 cpu_facts['processor_count'] = len(sockets)
97 cpu_facts['processor_cores'] = reduce(lambda x, y: x + y, sockets.values())
98 else:
99 cpu_facts['processor_count'] = i
100 cpu_facts['processor_cores'] = 'NA'
101
102 return cpu_facts
103
104 def get_memory_facts(self):
105 memory_facts = {}
106 if not os.access("/proc/meminfo", os.R_OK):
107 return memory_facts
108 for line in get_file_lines("/proc/meminfo"):
109 data = line.split(":", 1)
110 key = data[0]
111 if key in NetBSDHardware.MEMORY_FACTS:
112 val = data[1].strip().split(' ')[0]
113 memory_facts["%s_mb" % key.lower()] = int(val) // 1024
114
115 return memory_facts
116
117 @timeout()
118 def get_mount_facts(self):
119 mount_facts = {}
120
121 mount_facts['mounts'] = []
122 fstab = get_file_content('/etc/fstab')
123
124 if not fstab:
125 return mount_facts
126
127 for line in fstab.splitlines():
128 if line.startswith('#') or line.strip() == '':
129 continue
130 fields = re.sub(r'\s+', ' ', line).split()
131 mount_statvfs_info = get_mount_size(fields[1])
132 mount_info = {'mount': fields[1],
133 'device': fields[0],
134 'fstype': fields[2],
135 'options': fields[3]}
136 mount_info.update(mount_statvfs_info)
137 mount_facts['mounts'].append(mount_info)
138 return mount_facts
139
140 def get_dmi_facts(self):
141 dmi_facts = {}
142 # We don't use dmidecode(8) here because:
143 # - it would add dependency on an external package
144 # - dmidecode(8) can only be ran as root
145 # So instead we rely on sysctl(8) to provide us the information on a
146 # best-effort basis. As a bonus we also get facts on non-amd64/i386
147 # platforms this way.
148 sysctl_to_dmi = {
149 'machdep.dmi.system-product': 'product_name',
150 'machdep.dmi.system-version': 'product_version',
151 'machdep.dmi.system-uuid': 'product_uuid',
152 'machdep.dmi.system-serial': 'product_serial',
153 'machdep.dmi.system-vendor': 'system_vendor',
154 }
155
156 for mib in sysctl_to_dmi:
157 if mib in self.sysctl:
158 dmi_facts[sysctl_to_dmi[mib]] = self.sysctl[mib]
159
160 return dmi_facts
161
162 def get_uptime_facts(self):
163 # On NetBSD, we need to call sysctl with -n to get this value as an int.
164 sysctl_cmd = self.module.get_bin_path('sysctl')
165 if sysctl_cmd is None:
166 return {}
167
168 cmd = [sysctl_cmd, '-n', 'kern.boottime']
169
170 rc, out, err = self.module.run_command(cmd)
171
172 if rc != 0:
173 return {}
174
175 kern_boottime = out.strip()
176 if not kern_boottime.isdigit():
177 return {}
178
179 return {
180 'uptime_seconds': int(time.time() - int(kern_boottime)),
181 }
182
183
184 class NetBSDHardwareCollector(HardwareCollector):
185 _fact_class = NetBSDHardware
186 _platform = 'NetBSD'