| 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 |
|
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import struct |
| 20 |
import time |
| 21 |
|
| 22 |
from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector |
| 23 |
from ansible.module_utils.facts.sysctl import get_sysctl |
| 24 |
|
| 25 |
|
| 26 |
class DarwinHardware(Hardware): |
| 27 |
""" |
| 28 |
Darwin-specific subclass of Hardware. Defines memory and CPU facts: |
| 29 |
- processor |
| 30 |
- processor_cores |
| 31 |
- memtotal_mb |
| 32 |
- memfree_mb |
| 33 |
- model |
| 34 |
- osversion |
| 35 |
- osrevision |
| 36 |
- uptime_seconds |
| 37 |
""" |
| 38 |
platform = 'Darwin' |
| 39 |
|
| 40 |
def populate(self, collected_facts=None): |
| 41 |
hardware_facts = {} |
| 42 |
|
| 43 |
self.sysctl = get_sysctl(self.module, ['hw', 'machdep', 'kern', 'hw.model']) |
| 44 |
mac_facts = self.get_mac_facts() |
| 45 |
cpu_facts = self.get_cpu_facts() |
| 46 |
memory_facts = self.get_memory_facts() |
| 47 |
uptime_facts = self.get_uptime_facts() |
| 48 |
|
| 49 |
hardware_facts.update(mac_facts) |
| 50 |
hardware_facts.update(cpu_facts) |
| 51 |
hardware_facts.update(memory_facts) |
| 52 |
hardware_facts.update(uptime_facts) |
| 53 |
|
| 54 |
return hardware_facts |
| 55 |
|
| 56 |
def get_system_profile(self): |
| 57 |
rc, out, err = self.module.run_command(["/usr/sbin/system_profiler", "SPHardwareDataType"]) |
| 58 |
if rc != 0: |
| 59 |
return dict() |
| 60 |
system_profile = dict() |
| 61 |
for line in out.splitlines(): |
| 62 |
if ': ' in line: |
| 63 |
(key, value) = line.split(': ', 1) |
| 64 |
system_profile[key.strip()] = ' '.join(value.strip().split()) |
| 65 |
return system_profile |
| 66 |
|
| 67 |
def get_mac_facts(self): |
| 68 |
mac_facts = {} |
| 69 |
if 'hw.model' in self.sysctl: |
| 70 |
mac_facts['model'] = mac_facts['product_name'] = self.sysctl['hw.model'] |
| 71 |
mac_facts['osversion'] = self.sysctl['kern.osversion'] |
| 72 |
mac_facts['osrevision'] = self.sysctl['kern.osrevision'] |
| 73 |
|
| 74 |
return mac_facts |
| 75 |
|
| 76 |
def get_cpu_facts(self): |
| 77 |
cpu_facts = {} |
| 78 |
if 'machdep.cpu.brand_string' in self.sysctl: # Intel |
| 79 |
cpu_facts['processor'] = self.sysctl['machdep.cpu.brand_string'] |
| 80 |
cpu_facts['processor_cores'] = self.sysctl['machdep.cpu.core_count'] |
| 81 |
else: # PowerPC |
| 82 |
system_profile = self.get_system_profile() |
| 83 |
cpu_facts['processor'] = '%s @ %s' % (system_profile['Processor Name'], system_profile['Processor Speed']) |
| 84 |
cpu_facts['processor_cores'] = self.sysctl['hw.physicalcpu'] |
| 85 |
cpu_facts['processor_vcpus'] = self.sysctl.get('hw.logicalcpu') or self.sysctl.get('hw.ncpu') or '' |
| 86 |
|
| 87 |
return cpu_facts |
| 88 |
|
| 89 |
def get_memory_facts(self): |
| 90 |
memory_facts = { |
| 91 |
'memtotal_mb': int(self.sysctl['hw.memsize']) // 1024 // 1024, |
| 92 |
'memfree_mb': 0, |
| 93 |
} |
| 94 |
|
| 95 |
total_used = 0 |
| 96 |
page_size = int(self.sysctl.get('hw.pagesize', 4096)) |
| 97 |
|
| 98 |
vm_stat_command = self.module.get_bin_path('vm_stat') |
| 99 |
if vm_stat_command is None: |
| 100 |
return memory_facts |
| 101 |
|
| 102 |
if vm_stat_command: |
| 103 |
rc, out, err = self.module.run_command(vm_stat_command) |
| 104 |
if rc == 0: |
| 105 |
# Free = Total - (Wired + active + inactive) |
| 106 |
# Get a generator of tuples from the command output so we can later |
| 107 |
# turn it into a dictionary |
| 108 |
memory_stats = (line.rstrip('.').split(':', 1) for line in out.splitlines()) |
| 109 |
|
| 110 |
# Strip extra left spaces from the value |
| 111 |
memory_stats = dict((k, v.lstrip()) for k, v in memory_stats) |
| 112 |
|
| 113 |
for k, v in memory_stats.items(): |
| 114 |
try: |
| 115 |
memory_stats[k] = int(v) |
| 116 |
except ValueError: |
| 117 |
# Most values convert cleanly to integer values but if the field does |
| 118 |
# not convert to an integer, just leave it alone. |
| 119 |
pass |
| 120 |
|
| 121 |
if memory_stats.get('Pages wired down'): |
| 122 |
total_used += memory_stats['Pages wired down'] * page_size |
| 123 |
if memory_stats.get('Pages active'): |
| 124 |
total_used += memory_stats['Pages active'] * page_size |
| 125 |
if memory_stats.get('Pages inactive'): |
| 126 |
total_used += memory_stats['Pages inactive'] * page_size |
| 127 |
|
| 128 |
memory_facts['memfree_mb'] = memory_facts['memtotal_mb'] - (total_used // 1024 // 1024) |
| 129 |
|
| 130 |
return memory_facts |
| 131 |
|
| 132 |
def get_uptime_facts(self): |
| 133 |
|
| 134 |
# On Darwin, the default format is annoying to parse. |
| 135 |
# Use -b to get the raw value and decode it. |
| 136 |
sysctl_cmd = self.module.get_bin_path('sysctl') |
| 137 |
if not sysctl_cmd: |
| 138 |
return {} |
| 139 |
|
| 140 |
cmd = [sysctl_cmd, '-b', 'kern.boottime'] |
| 141 |
|
| 142 |
# We need to get raw bytes, not UTF-8. |
| 143 |
rc, out, err = self.module.run_command(cmd, encoding=None) |
| 144 |
|
| 145 |
# kern.boottime returns seconds and microseconds as two 64-bits |
| 146 |
# fields, but we are only interested in the first field. |
| 147 |
struct_format = '@L' |
| 148 |
struct_size = struct.calcsize(struct_format) |
| 149 |
if rc != 0 or len(out) < struct_size: |
| 150 |
return {} |
| 151 |
|
| 152 |
(kern_boottime, ) = struct.unpack(struct_format, out[:struct_size]) |
| 153 |
|
| 154 |
return { |
| 155 |
'uptime_seconds': int(time.time() - kern_boottime), |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
class DarwinHardwareCollector(HardwareCollector): |
| 160 |
_fact_class = DarwinHardware |
| 161 |
_platform = 'Darwin' |