Debian Codegraph @

lib/ansible/module_utils/facts/hardware/hpux.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
21 from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector
22
23
24 class HPUXHardware(Hardware):
25 """
26 HP-UX-specific subclass of Hardware. Defines memory and CPU facts:
27 - memfree_mb
28 - memtotal_mb
29 - swapfree_mb
30 - swaptotal_mb
31 - processor
32 - processor_cores
33 - processor_count
34 - model
35 - firmware
36 """
37
38 platform = 'HP-UX'
39
40 def populate(self, collected_facts=None):
41 hardware_facts = {}
42
43 # TODO: very inefficient calls to machinfo,
44 # should just make one and then deal with finding the data (see facts/sysctl)
45 # but not going to change unless there is hp/ux for testing
46 cpu_facts = self.get_cpu_facts(collected_facts=collected_facts)
47 memory_facts = self.get_memory_facts()
48 hw_facts = self.get_hw_facts()
49
50 hardware_facts.update(cpu_facts)
51 hardware_facts.update(memory_facts)
52 hardware_facts.update(hw_facts)
53
54 return hardware_facts
55
56 def get_cpu_facts(self, collected_facts=None):
57 cpu_facts = {}
58 collected_facts = collected_facts or {}
59
60 if collected_facts.get('ansible_architecture') in ['9000/800', '9000/785']:
61 rc, out, err = self.module.run_command("ioscan -FkCprocessor | wc -l", use_unsafe_shell=True)
62 cpu_facts['processor_count'] = int(out.strip())
63 # Working with machinfo mess
64 elif collected_facts.get('ansible_architecture') == 'ia64':
65 if collected_facts.get('ansible_distribution_version') == "B.11.23":
66 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep 'Number of CPUs'", use_unsafe_shell=True)
67 if out:
68 cpu_facts['processor_count'] = int(out.strip().split('=')[1])
69 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep 'processor family'", use_unsafe_shell=True)
70 if out:
71 cpu_facts['processor'] = re.search('.*(Intel.*)', out).groups()[0].strip()
72 rc, out, err = self.module.run_command("ioscan -FkCprocessor | wc -l", use_unsafe_shell=True)
73 cpu_facts['processor_cores'] = int(out.strip())
74 if collected_facts.get('ansible_distribution_version') == "B.11.31":
75 # if machinfo return cores strings release B.11.31 > 1204
76 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep core | wc -l", use_unsafe_shell=True)
77 if out.strip() == '0':
78 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep Intel", use_unsafe_shell=True)
79 cpu_facts['processor_count'] = int(out.strip().split(" ")[0])
80 # If hyperthreading is active divide cores by 2
81 rc, out, err = self.module.run_command("/usr/sbin/psrset | grep LCPU", use_unsafe_shell=True)
82 data = re.sub(' +', ' ', out).strip().split(' ')
83 if len(data) == 1:
84 hyperthreading = 'OFF'
85 else:
86 hyperthreading = data[1]
87 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep logical", use_unsafe_shell=True)
88 data = out.strip().split(" ")
89 if hyperthreading == 'ON':
90 cpu_facts['processor_cores'] = int(data[0]) / 2
91 else:
92 if len(data) == 1:
93 cpu_facts['processor_cores'] = cpu_facts['processor_count']
94 else:
95 cpu_facts['processor_cores'] = int(data[0])
96 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep Intel |cut -d' ' -f4-", use_unsafe_shell=True)
97 cpu_facts['processor'] = out.strip()
98 else:
99 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | egrep 'socket[s]?$' | tail -1", use_unsafe_shell=True)
100 cpu_facts['processor_count'] = int(out.strip().split(" ")[0])
101 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep -e '[0-9] core' | tail -1", use_unsafe_shell=True)
102 cpu_facts['processor_cores'] = int(out.strip().split(" ")[0])
103 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep Intel", use_unsafe_shell=True)
104 cpu_facts['processor'] = out.strip()
105
106 return cpu_facts
107
108 def get_memory_facts(self, collected_facts=None):
109 memory_facts = {}
110 collected_facts = collected_facts or {}
111
112 pagesize = 4096
113 rc, out, err = self.module.run_command("/usr/bin/vmstat | tail -1", use_unsafe_shell=True)
114 data = int(re.sub(' +', ' ', out).split(' ')[5].strip())
115 memory_facts['memfree_mb'] = pagesize * data // 1024 // 1024
116 if collected_facts.get('ansible_architecture') in ['9000/800', '9000/785']:
117 try:
118 rc, out, err = self.module.run_command("grep Physical /var/adm/syslog/syslog.log")
119 data = re.search('.*Physical: ([0-9]*) Kbytes.*', out).groups()[0].strip()
120 memory_facts['memtotal_mb'] = int(data) // 1024
121 except AttributeError:
122 # For systems where memory details aren't sent to syslog or the log has rotated, use parsed
123 # adb output. Unfortunately /dev/kmem doesn't have world-read, so this only works as root.
124 if os.access("/dev/kmem", os.R_OK):
125 rc, out, err = self.module.run_command("echo 'phys_mem_pages/D' | adb -k /stand/vmunix /dev/kmem | tail -1 | awk '{print $2}'",
126 use_unsafe_shell=True)
127 if not err:
128 data = out
129 memory_facts['memtotal_mb'] = int(data) / 256
130 else:
131 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo | grep Memory", use_unsafe_shell=True)
132 data = re.search(r'Memory[\ :=]*([0-9]*).*MB.*', out).groups()[0].strip()
133 memory_facts['memtotal_mb'] = int(data)
134 rc, out, err = self.module.run_command("/usr/sbin/swapinfo -m -d -f -q")
135 memory_facts['swaptotal_mb'] = int(out.strip())
136 rc, out, err = self.module.run_command("/usr/sbin/swapinfo -m -d -f | egrep '^dev|^fs'", use_unsafe_shell=True)
137 swap = 0
138 for line in out.strip().splitlines():
139 swap += int(re.sub(' +', ' ', line).split(' ')[3].strip())
140 memory_facts['swapfree_mb'] = swap
141
142 return memory_facts
143
144 def get_hw_facts(self, collected_facts=None):
145 hw_facts = {}
146 collected_facts = collected_facts or {}
147
148 rc, out, err = self.module.run_command("model")
149 hw_facts['model'] = out.strip()
150 if collected_facts.get('ansible_architecture') == 'ia64':
151 separator = ':'
152 if collected_facts.get('ansible_distribution_version') == "B.11.23":
153 separator = '='
154 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo |grep -i 'Firmware revision' | grep -v BMC", use_unsafe_shell=True)
155 hw_facts['firmware_version'] = out.split(separator)[1].strip()
156 rc, out, err = self.module.run_command("/usr/contrib/bin/machinfo |grep -i 'Machine serial number' ", use_unsafe_shell=True)
157 if rc == 0 and out:
158 hw_facts['product_serial'] = out.split(separator)[1].strip()
159
160 return hw_facts
161
162
163 class HPUXHardwareCollector(HardwareCollector):
164 _fact_class = HPUXHardware
165 _platform = 'HP-UX'
166
167 required_facts = set(['platform', 'distribution'])