Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

# 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 <http://www.gnu.org/licenses/>. 

 

from __future__ import (absolute_import, division, print_function) 

__metaclass__ = type 

 

import os 

import json 

import re 

 

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 

""" 

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() 

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(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'] = [] 

rc, out, err = self.module.run_command("/sbin/sysctl -n hw.ncpu") 

cpu_facts['processor_count'] = out.strip() 

 

dmesg_boot = get_file_content(FreeBSDHardware.DMESG_BOOT) 

if not dmesg_boot: 

rc, dmesg_boot, err = self.module.run_command("/sbin/dmesg") 

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 = {} 

 

rc, out, err = self.module.run_command("/sbin/sysctl vm.stats") 

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 

# Get swapinfo. swapinfo output looks like: 

# Device 1M-blocks Used Avail Capacity 

# /dev/ada0p3 314368 0 314368 0% 

# 

rc, out, err = self.module.run_command("/usr/sbin/swapinfo -k") 

lines = out.splitlines() 

if len(lines[-1]) == 0: 

lines.pop() 

data = lines[-1].split() 

if data[0] != 'Device': 

memory_facts['swaptotal_mb'] = int(data[1]) // 1024 

memory_facts['swapfree_mb'] = int(data[3]) // 1024 

 

return memory_facts 

 

@timeout() 

def get_mount_facts(self): 

mount_facts = {} 

 

mount_facts['mounts'] = [] 

fstab = get_file_content('/etc/fstab') 

if fstab: 

for line in fstab.splitlines(): 

if line.startswith('#') or line.strip() == '': 

continue 

fields = re.sub(r'\s+', ' ', line).split() 

mount_statvfs_info = get_mount_size(fields[1]) 

mount_info = {'mount': fields[1], 

'device': fields[0], 

'fstype': fields[2], 

'options': fields[3]} 

mount_info.update(mount_statvfs_info) 

mount_facts['mounts'].append(mount_info) 

 

return mount_facts 

 

def get_device_facts(self): 

device_facts = {} 

 

sysdir = '/dev' 

device_facts['devices'] = {} 

drives = re.compile(r'(ada?\d+|da\d+|a?cd\d+)') # TODO: rc, disks, err = self.module.run_command("/sbin/sysctl kern.disks") 

slices = re.compile(r'(ada?\d+s\d+\w*|da\d+s\d+\w*)') 

if os.path.isdir(sysdir): 

dirlist = sorted(os.listdir(sysdir)) 

for device in dirlist: 

d = drives.match(device) 

if d: 

device_facts['devices'][d.group(1)] = [] 

s = slices.match(device) 

if s: 

device_facts['devices'][d.group(1)].append(s.group(1)) 

 

return device_facts 

 

def get_dmi_facts(self): 

''' learn dmi facts from system 

 

Use dmidecode executable if available''' 

 

dmi_facts = {} 

 

# Fall back to using dmidecode, if available 

dmi_bin = self.module.get_bin_path('dmidecode') 

DMI_DICT = dict( 

bios_date='bios-release-date', 

bios_version='bios-version', 

form_factor='chassis-type', 

product_name='system-product-name', 

product_serial='system-serial-number', 

product_uuid='system-uuid', 

product_version='system-version', 

system_vendor='system-manufacturer' 

) 

for (k, v) in DMI_DICT.items(): 

if dmi_bin is not None: 

(rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v)) 

if rc == 0: 

# Strip out commented lines (specific dmidecode output) 

# FIXME: why add the fact and then test if it is json? 

dmi_facts[k] = ''.join([line for line in out.splitlines() if not line.startswith('#')]) 

try: 

json.dumps(dmi_facts[k]) 

except UnicodeDecodeError: 

dmi_facts[k] = 'NA' 

else: 

dmi_facts[k] = 'NA' 

else: 

dmi_facts[k] = 'NA' 

 

return dmi_facts 

 

 

class FreeBSDHardwareCollector(HardwareCollector): 

_fact_class = FreeBSDHardware 

_platform = 'FreeBSD'