-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_ib_switch.py
executable file
·284 lines (247 loc) · 9.37 KB
/
check_ib_switch.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import re
import sys
import argparse
import logging
import subprocess
import itertools
def parse_table_hex(lines):
info = {}
for line in lines:
m = re.match(r'(.*?)\s*\| (.*)', line)
info[str(m.group(1))] = int(m.group(2), 16)
return info
def parse_table_ascii(lines):
info = {}
for line in lines:
m = re.match(r'(.*?)(\[\d+\])?\s*\| (.*)', line)
field = str(m.group(1))
hex_v = bytearray.fromhex(m.group(3)[2:]).decode()
value = str(hex_v.replace(u'\x00', '').strip())
if m.group(2):
if field in info.keys():
info[field] = info[field] + value
else:
info[field] = value
else:
info[field] = value
return info
def mlxreg_ext_fans(lid, fan_id):
cmdargs = ['mlxreg_ext', '-d', 'lid-{0}'.format(lid), '--reg_name',
'MFSM', '--get', '--indexes', 'tacho={}'.format(fan_id)]
stdout, stderr = subprocess.Popen(cmdargs,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
return parse_table_hex(stdout.decode("utf-8").splitlines()[4:-1])
def mlxreg_ext_temp(lid, sensor_id, slot_index=None):
if slot_index is not None:
s = 'sensor_index={},slot_index={}'.format(sensor_id, slot_index)
else:
s = 'sensor_index={}'.format(sensor_id)
cmdargs = ['mlxreg_ext', '-d', 'lid-{0}'.format(lid), '--reg_name',
'MTMP', '--get', '--indexes', s]
stdout, stderr = subprocess.Popen(cmdargs,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
return parse_table_hex(stdout.decode("utf-8").splitlines()[4:-1])
def mlxreg_ext_psu(lid):
cmdargs = ['mlxreg_ext', '-d', 'lid-{0}'.format(lid), '--reg_name',
'MSPS', '--get']
stdout, stderr = subprocess.Popen(cmdargs,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
psus = {}
lines = stdout.decode("utf-8").splitlines()[4:-1]
for line in lines:
# PSU Watt with 0x8 prependded
m_watt = re.match(r'psu(\d)\[2\]\s+\| 0x8(.*)', line)
if m_watt:
psus['watt_' + m_watt.group(1)] = int(m_watt.group(2), 16)
return psus
def mlxreg_ext(lid, register):
cmdargs = ['mlxreg_ext', '-d', 'lid-{0}'.format(lid), '--reg_name',
register, '--get']
stdout, stderr = subprocess.Popen(cmdargs,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
return parse_table_ascii(stdout.decode("utf-8").splitlines()[4:-1])
def ascii_field(name):
for field in ['vendor_name', 'vendor_sn', 'vendor_pn', 'vendor_rev']:
if field in name:
return True
return False
def mlxreg_ext_ports(lid, port_id):
index = 'local_port={},pnat=0x0,page_select=0x3,group_opcode=0x0'
cmdargs = ['mlxreg_ext', '-d', 'lid-{0}'.format(lid), '--reg_name',
'PDDR', '--get', '--indexes', index.format(port_id)]
stdout, stderr = subprocess.Popen(cmdargs,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
lines = stdout.decode("utf-8").splitlines()[4:-1]
info = parse_table_ascii(filter(ascii_field, lines))
info.update(parse_table_hex(itertools.filterfalse(ascii_field, lines)))
return info
def guid_to_lid():
stdout, stderr = subprocess.Popen(['ibswitches'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
lids = {} # store GUID to LID mapping
for line in stdout.decode("utf-8").splitlines():
logging.debug('guid_to_lid: %s', line)
m = re.match(r'.*(0x.*) ports.* lid (\d+)', line)
lids[m.group(1)] = int(m.group(2))
return lids
def print_info(info):
for item in info:
print(item)
parser = argparse.ArgumentParser(description='')
parser.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
parser.add_argument("--guid", help="Switch GUID to check",
action="store")
parser.add_argument("--node_name_map", help="Node name map file path",
action="store")
parser.add_argument("--name", help="Switch name used in node-name-map",
action="store")
parser.add_argument("--fan", help="Check fans", action="store_true")
parser.add_argument("--cable", help="Check cables", action="store_true")
parser.add_argument("--psu", help="Check PSUs", action="store_true")
parser.add_argument("--temp", help="Check temperatures", action="store_true")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
if args.name is False and args.guid is False:
print('Need to use the GUID or the switch name')
sys.exit(3)
if args.name:
if args.node_name_map is None:
print('node_name_map need to be defined')
sys.exit(3)
guid_name = {}
name_guid = {}
if args.node_name_map:
with open(args.node_name_map) as f:
for line in f:
m = re.match(r'(0x.*) "(.*)"', line)
if m:
guid_name[m.group(1)] = m.group(2)
name_guid[m.group(2)] = m.group(1)
guids = guid_to_lid()
if args.guid:
# received a GUID, check in node name map
guid = args.guid
if args.guid in guid_name:
name = guid_name[args.guid]
else:
name = args.guid
else:
# Received the name, need to get the GUID from the file
guid = name_guid[args.name]
name = args.name
lid = guids[guid]
perfdata = []
criticals = []
warnings = []
info = []
sw = mlxreg_ext(lid, 'MSGI')
info.append('GUID={} LID={} Name={}'.format(guid, lid, name))
info.append('{} PN={} Rev={} SN={}'.format(sw['product_name'],
sw['part_number'], sw['revision'], sw['serial_number']))
if args.psu:
if sw['product_name'] == 'Jaguar UnmngIB200':
normal_max_watt = 220 # HDR
else:
normal_max_watt = 100 # EDR
try:
psus = mlxreg_ext_psu(lid)
for i in range(2):
psu_watt = 'watt_{}'.format(i)
if psus[psu_watt] < 30:
criticals.append('PSU{} is down with {}W'.format(
i, psus[psu_watt]))
if psus[psu_watt] > normal_max_watt:
warnings.append('PSU{} might be alone with {}W'.format(
i, psus[psu_watt]))
perfdata.append('PSU{psu}_W={watt};;30:{normal_max_watt};;'.format(
psu=i,
watt=psus[psu_watt],
normal_max_watt=normal_max_watt,
))
except KeyError:
# Error while reading the PDUs
criticals.append('Unable to read the PSU')
if args.fan:
for i in range(1, 9):
try:
fan_info = mlxreg_ext_fans(lid, i)
rpm = fan_info['rpm']
if rpm < 4500:
criticals.append('Fan #{} is too slow, {} RPM'.format(i, rpm))
elif rpm > 13000:
criticals.append('Fan #{} is too fast, {} RPM'.format(i, rpm))
perfdata.append('Fan{fan}_RPM={speed};;{MIN_FAN}:{MAX_FAN};;'.format(
fan=i,
speed=rpm,
MIN_FAN=4500,
MAX_FAN=13000,
))
except KeyError:
criticals.append('Could not read fan #{}'.format(i))
if args.temp:
if sw['product_name'] == 'Jaguar UnmngIB200':
sensor_range = range(0,2)
slot_index = 0
max_temp = 50
else:
sensor_range = range(1,7)
slot_index = None
max_temp = 45
for i in sensor_range:
try:
temp_info = mlxreg_ext_temp(lid, i, slot_index)
temperature = temp_info['temperature']/10
if temperature > max_temp:
criticals.append('Temperature of #{} is too high, {}C'.format(
i, temperature))
perfdata.append('Temperature{sensor}_C={temp};;5:{MAX_TEMP};;'.format(
sensor=i,
temp=temperature,
MAX_TEMP=max_temp,
))
except KeyError:
criticals.append('Could not read temp #{}'.format(i))
if args.cable:
for i in range(1, 37):
cable = mlxreg_ext_ports(lid, i)
temperature = cable['temperature']/256
if temperature > 70:
criticals.append('Cable {} is overtemp at {}C > 70C'.format(
i, temperature))
info.append('Cable #{}, {} PN={} SN={} Rev={} FW={}, {}M'.format(
i,
cable['vendor_name'],
cable['vendor_pn'],
cable['vendor_sn'],
cable['vendor_rev'],
cable['fw_version'],
cable['cable_length'])
)
if len(criticals) > 0:
print('{criticals} | {perfdata}'.format(
criticals=', '.join(criticals) + ', '.join(warnings),
perfdata=' '.join(perfdata),
))
print_info(info)
sys.exit(2)
elif len(warnings) > 0:
print('{warnings} | {perfdata}'.format(
warnings=', '.join(warnings),
perfdata=' '.join(perfdata),
))
print_info(info)
sys.exit(1)
else:
print('Switch OK | {perfdata}'.format(
perfdata=' '.join(perfdata),
))
print_info(info)
sys.exit(0)