0x0 # ˆl–Ðz¾”Ji?u4 ^¸ËwÒ˜´oÀ ƒyç¿ÄÃ"� # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . from __future__ import (absolute_import, division, print_function) __metaclass__ = type import os import json import re import struct import time from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector from ansible.module_utils.facts.timeout import TimeoutError, timeout from ansible.module_utils.facts.utils import get_file_content, get_mount_size class FreeBSDHardware(Hardware): """ FreeBSD-specific subclass of Hardware. Defines memory and CPU facts: - memfree_mb - memtotal_mb - swapfree_mb - swaptotal_mb - processor (a list) - processor_cores - processor_count - devices - uptime_seconds """ platform = 'FreeBSD' DMESG_BOOT = '/var/run/dmesg.boot' def populate(self, collected_facts=None): hardware_facts = {} cpu_facts = self.get_cpu_facts() memory_facts = self.get_memory_facts() uptime_facts = self.get_uptime_facts() dmi_facts = self.get_dmi_facts() device_facts = self.get_device_facts() mount_facts = {} try: mount_facts = self.get_mount_facts() except TimeoutError: pass hardware_facts.update(cpu_facts) hardware_facts.update(memory_facts) hardware_facts.update(uptime_facts) hardware_facts.update(dmi_facts) hardware_facts.update(device_facts) hardware_facts.update(mount_facts) return hardware_facts def get_cpu_facts(self): cpu_facts = {} cpu_facts['processor'] = [] 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: try: rc, dmesg_boot, err = self.module.run_command(self.module.get_bin_path("dmesg"), check_rc=False) except Exception: dmesg_boot = '' for line in dmesg_boot.splitlines(): if 'CPU:' in line: cpu = re.sub(r'CPU:\s+', r"", line) cpu_facts['processor'].append(cpu.strip()) if 'Logical CPUs per core' in line: cpu_facts['processor_cores'] = line.split()[4] return cpu_facts def get_memory_facts(self): memory_facts = {} 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