| 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 |
|
| 20 |
from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector |
| 21 |
from ansible.module_utils.facts.utils import get_mount_size |
| 22 |
|
| 23 |
|
| 24 |
class AIXHardware(Hardware): |
| 25 |
""" |
| 26 |
AIX-specific subclass of Hardware. Defines memory and CPU facts: |
| 27 |
- memfree_mb |
| 28 |
- memtotal_mb |
| 29 |
- swapfree_mb |
| 30 |
- swaptotal_mb |
| 31 |
- processor (a list) |
| 32 |
- processor_count |
| 33 |
- processor_cores |
| 34 |
- processor_threads_per_core |
| 35 |
- processor_vcpus |
| 36 |
""" |
| 37 |
platform = 'AIX' |
| 38 |
|
| 39 |
def populate(self, collected_facts=None): |
| 40 |
hardware_facts = {} |
| 41 |
|
| 42 |
cpu_facts = self.get_cpu_facts() |
| 43 |
memory_facts = self.get_memory_facts() |
| 44 |
dmi_facts = self.get_dmi_facts() |
| 45 |
vgs_facts = self.get_vgs_facts() |
| 46 |
mount_facts = self.get_mount_facts() |
| 47 |
devices_facts = self.get_device_facts() |
| 48 |
uptime_facts = self.get_uptime_facts() |
| 49 |
|
| 50 |
hardware_facts.update(cpu_facts) |
| 51 |
hardware_facts.update(memory_facts) |
| 52 |
hardware_facts.update(dmi_facts) |
| 53 |
hardware_facts.update(vgs_facts) |
| 54 |
hardware_facts.update(mount_facts) |
| 55 |
hardware_facts.update(devices_facts) |
| 56 |
hardware_facts.update(uptime_facts) |
| 57 |
|
| 58 |
return hardware_facts |
| 59 |
|
| 60 |
def get_cpu_facts(self): |
| 61 |
cpu_facts = {} |
| 62 |
cpu_facts['processor'] = [] |
| 63 |
|
| 64 |
# FIXME: not clear how to detect multi-sockets |
| 65 |
cpu_facts['processor_count'] = 1 |
| 66 |
rc, out, err = self.module.run_command( |
| 67 |
"/usr/sbin/lsdev -Cc processor" |
| 68 |
) |
| 69 |
if out: |
| 70 |
i = 0 |
| 71 |
for line in out.splitlines(): |
| 72 |
|
| 73 |
if 'Available' in line: |
| 74 |
if i == 0: |
| 75 |
data = line.split(' ') |
| 76 |
cpudev = data[0] |
| 77 |
|
| 78 |
i += 1 |
| 79 |
cpu_facts['processor_cores'] = int(i) |
| 80 |
|
| 81 |
rc, out, err = self.module.run_command( |
| 82 |
"/usr/sbin/lsattr -El " + cpudev + " -a type" |
| 83 |
) |
| 84 |
|
| 85 |
data = out.split(' ') |
| 86 |
cpu_facts['processor'] = [data[1]] |
| 87 |
|
| 88 |
cpu_facts['processor_threads_per_core'] = 1 |
| 89 |
rc, out, err = self.module.run_command( |
| 90 |
"/usr/sbin/lsattr -El " + cpudev + " -a smt_threads" |
| 91 |
) |
| 92 |
if out: |
| 93 |
data = out.split(' ') |
| 94 |
cpu_facts['processor_threads_per_core'] = int(data[1]) |
| 95 |
cpu_facts['processor_vcpus'] = ( |
| 96 |
cpu_facts['processor_cores'] * cpu_facts['processor_threads_per_core'] |
| 97 |
) |
| 98 |
|
| 99 |
return cpu_facts |
| 100 |
|
| 101 |
def get_memory_facts(self): |
| 102 |
memory_facts = {} |
| 103 |
pagesize = 4096 |
| 104 |
rc, out, err = self.module.run_command("/usr/bin/vmstat -v") |
| 105 |
for line in out.splitlines(): |
| 106 |
data = line.split() |
| 107 |
if 'memory pages' in line: |
| 108 |
pagecount = int(data[0]) |
| 109 |
if 'free pages' in line: |
| 110 |
freecount = int(data[0]) |
| 111 |
memory_facts['memtotal_mb'] = pagesize * pagecount // 1024 // 1024 |
| 112 |
memory_facts['memfree_mb'] = pagesize * freecount // 1024 // 1024 |
| 113 |
# Get swapinfo. swapinfo output looks like: |
| 114 |
# Device 1M-blocks Used Avail Capacity |
| 115 |
# /dev/ada0p3 314368 0 314368 0% |
| 116 |
# |
| 117 |
rc, out, err = self.module.run_command("/usr/sbin/lsps -s") |
| 118 |
if out: |
| 119 |
lines = out.splitlines() |
| 120 |
data = lines[1].split() |
| 121 |
swaptotal_mb = int(data[0].rstrip('MB')) |
| 122 |
percused = int(data[1].rstrip('%')) |
| 123 |
memory_facts['swaptotal_mb'] = swaptotal_mb |
| 124 |
memory_facts['swapfree_mb'] = int(swaptotal_mb * (100 - percused) / 100) |
| 125 |
|
| 126 |
return memory_facts |
| 127 |
|
| 128 |
def get_uptime_facts(self): |
| 129 |
uptime_facts = {} |
| 130 |
# On AIX, there are no options to get the uptime directly in seconds. |
| 131 |
# Your options are to parse the output of "who", "uptime", or "ps". |
| 132 |
# Only "ps" always provides a field with seconds. |
| 133 |
ps_bin = self.module.get_bin_path("ps") |
| 134 |
if ps_bin is None: |
| 135 |
return uptime_facts |
| 136 |
|
| 137 |
ps_cmd = [ps_bin, "-p", "1", "-o", "etime="] |
| 138 |
|
| 139 |
rc, out, err = self.module.run_command(ps_cmd) |
| 140 |
if rc != 0: |
| 141 |
return uptime_facts |
| 142 |
|
| 143 |
# Parse out |
| 144 |
if out: |
| 145 |
lines = out.splitlines() |
| 146 |
data = lines[0].replace(':', '-').split('-') |
| 147 |
try: |
| 148 |
days = int(data[0]) |
| 149 |
hours = int(data[1]) |
| 150 |
minutes = int(data[2]) |
| 151 |
seconds = int(data[3]) |
| 152 |
except (IndexError, ValueError): |
| 153 |
return uptime_facts |
| 154 |
# Calculate uptime in seconds |
| 155 |
uptime_seconds = (days * 86400) + (hours * 3600) + (minutes * 60) + seconds |
| 156 |
uptime_facts['uptime_seconds'] = int(uptime_seconds) |
| 157 |
|
| 158 |
return uptime_facts |
| 159 |
|
| 160 |
def get_dmi_facts(self): |
| 161 |
dmi_facts = {} |
| 162 |
|
| 163 |
rc, out, err = self.module.run_command("/usr/sbin/lsattr -El sys0 -a fwversion") |
| 164 |
data = out.split() |
| 165 |
dmi_facts['firmware_version'] = data[1].strip('IBM,') |
| 166 |
lsconf_path = self.module.get_bin_path("lsconf") |
| 167 |
if lsconf_path: |
| 168 |
rc, out, err = self.module.run_command(lsconf_path) |
| 169 |
if rc == 0 and out: |
| 170 |
for line in out.splitlines(): |
| 171 |
data = line.split(':') |
| 172 |
if 'Machine Serial Number' in line: |
| 173 |
dmi_facts['product_serial'] = data[1].strip() |
| 174 |
if 'LPAR Info' in line: |
| 175 |
dmi_facts['lpar_info'] = data[1].strip() |
| 176 |
if 'System Model' in line: |
| 177 |
dmi_facts['product_name'] = data[1].strip() |
| 178 |
return dmi_facts |
| 179 |
|
| 180 |
def get_vgs_facts(self): |
| 181 |
""" |
| 182 |
Get vg and pv Facts |
| 183 |
rootvg: |
| 184 |
PV_NAME PV STATE TOTAL PPs FREE PPs FREE DISTRIBUTION |
| 185 |
hdisk0 active 546 0 00..00..00..00..00 |
| 186 |
hdisk1 active 546 113 00..00..00..21..92 |
| 187 |
realsyncvg: |
| 188 |
PV_NAME PV STATE TOTAL PPs FREE PPs FREE DISTRIBUTION |
| 189 |
hdisk74 active 1999 6 00..00..00..00..06 |
| 190 |
testvg: |
| 191 |
PV_NAME PV STATE TOTAL PPs FREE PPs FREE DISTRIBUTION |
| 192 |
hdisk105 active 999 838 200..39..199..200..200 |
| 193 |
hdisk106 active 999 599 200..00..00..199..200 |
| 194 |
""" |
| 195 |
|
| 196 |
vgs_facts = {} |
| 197 |
lsvg_path = self.module.get_bin_path("lsvg") |
| 198 |
xargs_path = self.module.get_bin_path("xargs") |
| 199 |
cmd = "%s -o | %s %s -p" % (lsvg_path, xargs_path, lsvg_path) |
| 200 |
if lsvg_path and xargs_path: |
| 201 |
rc, out, err = self.module.run_command(cmd, use_unsafe_shell=True) |
| 202 |
if rc == 0 and out: |
| 203 |
vgs_facts['vgs'] = {} |
| 204 |
for m in re.finditer(r'(\S+):\n.*FREE DISTRIBUTION(\n(\S+)\s+(\w+)\s+(\d+)\s+(\d+).*)+', out): |
| 205 |
vgs_facts['vgs'][m.group(1)] = [] |
| 206 |
pp_size = 0 |
| 207 |
cmd = "%s %s" % (lsvg_path, m.group(1)) |
| 208 |
rc, out, err = self.module.run_command(cmd) |
| 209 |
if rc == 0 and out: |
| 210 |
pp_size = re.search(r'PP SIZE:\s+(\d+\s+\S+)', out).group(1) |
| 211 |
for n in re.finditer(r'(\S+)\s+(\w+)\s+(\d+)\s+(\d+).*', m.group(0)): |
| 212 |
pv_info = {'pv_name': n.group(1), |
| 213 |
'pv_state': n.group(2), |
| 214 |
'total_pps': n.group(3), |
| 215 |
'free_pps': n.group(4), |
| 216 |
'pp_size': pp_size |
| 217 |
} |
| 218 |
vgs_facts['vgs'][m.group(1)].append(pv_info) |
| 219 |
|
| 220 |
return vgs_facts |
| 221 |
|
| 222 |
def get_mount_facts(self): |
| 223 |
mount_facts = {} |
| 224 |
|
| 225 |
mount_facts['mounts'] = [] |
| 226 |
|
| 227 |
mounts = [] |
| 228 |
|
| 229 |
# AIX does not have mtab but mount command is only source of info (or to use |
| 230 |
# api calls to get same info) |
| 231 |
mount_path = self.module.get_bin_path('mount') |
| 232 |
if mount_path: |
| 233 |
rc, mount_out, err = self.module.run_command(mount_path) |
| 234 |
if mount_out: |
| 235 |
for line in mount_out.split('\n'): |
| 236 |
fields = line.split() |
| 237 |
if len(fields) != 0 and fields[0] != 'node' and fields[0][0] != '-' and re.match('^/.*|^[a-zA-Z].*|^[0-9].*', fields[0]): |
| 238 |
if re.match('^/', fields[0]): |
| 239 |
# normal mount |
| 240 |
mount = fields[1] |
| 241 |
mount_info = {'mount': mount, |
| 242 |
'device': fields[0], |
| 243 |
'fstype': fields[2], |
| 244 |
'options': fields[6], |
| 245 |
'time': '%s %s %s' % (fields[3], fields[4], fields[5])} |
| 246 |
mount_info.update(get_mount_size(mount)) |
| 247 |
else: |
| 248 |
# nfs or cifs based mount |
| 249 |
# in case of nfs if no mount options are provided on command line |
| 250 |
# add into fields empty string... |
| 251 |
if len(fields) < 8: |
| 252 |
fields.append("") |
| 253 |
|
| 254 |
mount_info = {'mount': fields[2], |
| 255 |
'device': '%s:%s' % (fields[0], fields[1]), |
| 256 |
'fstype': fields[3], |
| 257 |
'options': fields[7], |
| 258 |
'time': '%s %s %s' % (fields[4], fields[5], fields[6])} |
| 259 |
|
| 260 |
mounts.append(mount_info) |
| 261 |
|
| 262 |
mount_facts['mounts'] = mounts |
| 263 |
|
| 264 |
return mount_facts |
| 265 |
|
| 266 |
def get_device_facts(self): |
| 267 |
device_facts = {} |
| 268 |
device_facts['devices'] = {} |
| 269 |
|
| 270 |
lsdev_cmd = self.module.get_bin_path('lsdev') |
| 271 |
lsattr_cmd = self.module.get_bin_path('lsattr') |
| 272 |
if lsdev_cmd and lsattr_cmd: |
| 273 |
rc, out_lsdev, err = self.module.run_command(lsdev_cmd) |
| 274 |
|
| 275 |
for line in out_lsdev.splitlines(): |
| 276 |
field = line.split() |
| 277 |
|
| 278 |
device_attrs = {} |
| 279 |
device_name = field[0] |
| 280 |
device_state = field[1] |
| 281 |
device_type = field[2:] |
| 282 |
lsattr_cmd_args = [lsattr_cmd, '-E', '-l', device_name] |
| 283 |
rc, out_lsattr, err = self.module.run_command(lsattr_cmd_args) |
| 284 |
for attr in out_lsattr.splitlines(): |
| 285 |
attr_fields = attr.split() |
| 286 |
attr_name = attr_fields[0] |
| 287 |
attr_parameter = attr_fields[1] |
| 288 |
device_attrs[attr_name] = attr_parameter |
| 289 |
|
| 290 |
device_facts['devices'][device_name] = { |
| 291 |
'state': device_state, |
| 292 |
'type': ' '.join(device_type), |
| 293 |
'attributes': device_attrs |
| 294 |
} |
| 295 |
|
| 296 |
return device_facts |
| 297 |
|
| 298 |
|
| 299 |
class AIXHardwareCollector(HardwareCollector): |
| 300 |
_platform = 'AIX' |
| 301 |
_fact_class = AIXHardware |