-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight_slot
executable file
·289 lines (204 loc) · 8.66 KB
/
light_slot
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
285
286
287
288
289
#!/usr/bin/env python3
import re
import os
import sys
import math
import time
import string
from subprocess import Popen, PIPE, STDOUT
# Cache info
CacheDataArray = {}
CacheTimeArray = {}
# Enable/disable debugging messages
Print_Debug = True
def Debug(text):
"""
A wrapper to print debugging info on a single line.
"""
if Print_Debug:
print("DEBUG: " + text)
return
def SysExec(cmd):
"""
Run the given command and return the output
"""
Debug("SysExec:: cmd = " + cmd)
# Cache the output of the command for 20 seconds
Cache_Expires = 20
# Computed once, used twice
Cache_Keys = list(CacheDataArray.keys())
if cmd in Cache_Keys:
Cache_Age = time.time() - CacheTimeArray[cmd]
else:
Cache_Age = 0
Return_Val = "ERROR"
# If we have valid data cached, return it
if cmd in Cache_Keys and Cache_Age < Cache_Expires:
Return_Val = CacheDataArray[cmd]
# If the cmd is "cat", use fopen/fread/fclose to open it and
# cache it as we go
elif not cmd in Cache_Keys and cmd.split()[0] == "cat":
f = open(cmd.split()[1], "r")
CacheDataArray[cmd] = f.read()
CacheTimeArray[cmd] = time.time()
f.close()
Return_Val = CacheDataArray[cmd]
# If we don't have cached data, or it's too old, regenerate it
elif not cmd in Cache_Keys or Cache_Age > Cache_Expires:
CacheDataArray[cmd] = Popen(cmd.split(), stdout=PIPE, stderr=STDOUT).communicate()[0]
CacheTimeArray[cmd] = time.time()
Return_Val = CacheDataArray[cmd]
if str(type(Return_Val)) == "<class 'bytes'>":
Return_Val = Return_Val.decode("utf-8")
return Return_Val
def Get_SASController():
"""
Return the SAS controller model in this depot.
"""
Debug("def Get_SASController() entry")
output_lspci = SysExec("lspci")
# Determine the card type, and thus the path we need to take...
SAS_Controller = "Unknown"
for line in output_lspci.splitlines():
# Falcon is our 1st gen SAS controller
if re.search(r"SAS2008 PCI-Express Fusion-MPT SAS-2 \[Falcon\] \(rev 03\)", line):
SAS_Controller = "LSI_Falcon"
break
# Thunderbolt is our 2nd gen SAS controller
if re.search(r"MegaRAID SAS 2208 \[Thunderbolt\] \(rev 05\)", line):
SAS_Controller = "LSI_Thunderbolt"
break
# Invader is our 3rd gen SAS controller
if re.search(r"MegaRAID SAS-3 3108 \[Invader\] \(rev 02\)", line):
SAS_Controller = "LSI_Invader"
break
# TriMode is our 4th gen SAS controller
if re.search(r"SAS3408 Fusion-MPT Tri-Mode I/O Controller Chip", line):
SAS_Controller = "LSI_FusionMPT"
break
# Our 5th gen SAS controller
if re.search(r"LSI Fusion-MPT 12GSAS/PCIe Secure SAS38xx", line):
SAS_Controller = "LSI_FusionMPT"
break
Debug("Get_SASController():: SAS Controller type = " + SAS_Controller)
if SAS_Controller == "Unknown":
Debug("Get_SASController():: This is an unknown controller type which may not work properly without new code.")
Debug("def Get_SASController() exit")
return SAS_Controller
def FindBackplanes():
"""
Use the "lsbackplane" command to return info about backplanes in the server.
"""
Debug("def FindBackplanes() entry")
Backplanes = {}
# Iterate over a list of enclosures
output_lsbackplane = SysExec("lsbackplane")
for line in output_lsbackplane.splitlines():
if not re.search("/dev/", line):
continue
Enc_SG_Dev = line.split("|")[1].strip()
Backplanes[Enc_SG_Dev] = {}
Backplanes[Enc_SG_Dev]["NumSlots"] = line.split("|")[2].strip()
Backplanes[Enc_SG_Dev]["Bus"] = line.split("|")[3].strip()
Backplanes[Enc_SG_Dev]["SASAddr"] = line.split("|")[4].strip()
Backplanes[Enc_SG_Dev]["Alias"] = line.split("|")[5].strip()
Debug("FindBackplanes():: Backplane " + Enc_SG_Dev + " NumSlots " + Backplanes[Enc_SG_Dev]["NumSlots"] + " Bus " + Backplanes[Enc_SG_Dev]["Bus"] + " SASAddr " + Backplanes[Enc_SG_Dev]["SASAddr"] + " Alias " + Backplanes[Enc_SG_Dev]["Alias"])
Debug("def FindBackplanes() exit")
return(Backplanes)
###########################################################
# Main program
###########################################################
if len(sys.argv) != 4:
print("\n" + sys.argv[0] + " - Turn on/off a LED on a slot")
print("\nUsage: " + sys.argv[0] + " [backplane] [slot#] [on|off]\n")
sys.exit()
SAS_Controller = Get_SASController()
if SAS_Controller == "Unknown":
print("ERROR: Didn't recognize SAS/RAID card. Quitting...")
sys.exit()
#######################################################################################
### Redo logic:
###
### if SAS_Controller == "LSI_Thunderbolt" then use MegaCLI64 to turn locate on/off
### otherwise use sg_ses.
Debug("light_slot called with arguments: " + str(sys.argv))
Backplanes_List = FindBackplanes()
if not re.search(sys.argv[3], "on", re.IGNORECASE) and not re.search(sys.argv[3], "off", re.IGNORECASE):
print("Error: The location light can only be in state 'on' or 'off'. You specified state '" + sys.argv[3] + "', which is invalid.")
sys.exit()
if SAS_Controller == "LSI_Thunderbolt":
Debug("LSI_Thunderbolt path")
Debug("Using MegaCli64 to switch locate LED...")
for bp, keyvals in Backplanes_List.items():
for key, val in Backplanes_List[bp].items():
if re.search(sys.argv[1], val, re.IGNORECASE):
enclosure = Backplanes_List[bp]['Bus'].split(":")[2]
break
if not "enclosure" in globals():
print("ERROR: Could not detect enclosure " + sys.argv[1])
sys.exit()
if re.search("on", sys.argv[3], re.IGNORECASE):
Verb = "-start"
if re.search("off", sys.argv[3], re.IGNORECASE):
Verb = "-stop"
print("INFO: Setting the locate LED for slot " + sys.argv[1] + " " + sys.argv[2] + " to " + sys.argv[3].lower())
SysExec("MegaCli64 -PdLocate " + Verb + "-physdrv[" + enclosure + ":" + sys.argv[2] + "] -aAll")
sys.exit()
elif SAS_Controller == "LSI_Invader":
Debug("LSI_Invader path")
Debug("Using storcli64 to switch locate LED...")
Debug("Backplanes_List = " + str(Backplanes_List))
for bp, keyvals in Backplanes_List.items():
Debug("Iterating bp = " + str(bp) + " keyvals = " + str(keyvals))
for key, val in Backplanes_List[bp].items():
Debug("Iterating key = " + str(key) + " val " + str(val))
Debug("Searching for sys.argv[1] " + str(sys.argv[1]) + " in val " + val)
if re.search(sys.argv[1], val, re.IGNORECASE):
enclosure = Backplanes_List[bp]['Bus'].split(":")[2]
num_slots = int(Backplanes_List[bp]['NumSlots'])
break
if not "enclosure" in globals():
print("ERROR: Could not detect enclosure " + sys.argv[1])
sys.exit()
if not int(sys.argv[2]) in range(0, num_slots):
print("ERROR: Backplane only has " + str(num_slots) + " slots.")
sys.exit()
if re.search("on", sys.argv[3], re.IGNORECASE):
Verb = "start"
if re.search("off", sys.argv[3], re.IGNORECASE):
Verb = "stop"
print("INFO: Setting the locate LED for slot " + sys.argv[1] + " " + sys.argv[2] + " to " + sys.argv[3].lower())
SysExec("storcli64 /c0/e" + enclosure + "/s" + str(int(sys.argv[2]) + 1) + " " + Verb + " locate")
sys.exit()
else:
Debug("Generic path")
Debug("Using sg_ses to switch locate LED...")
Debug("Testing for " + sys.argv[1] + " " + sys.argv[2])
for bp, keyvals in Backplanes_List.items():
Debug("Iterating bp = " + str(bp) + " keyvals = " + str(keyvals))
for key, val in Backplanes_List[bp].items():
Debug("Iterating key = " + str(key) + " val " + str(val) + " across bp " + str(bp))
num_slots = int(Backplanes_List[bp]['NumSlots'])
Debug("num_slots = " + str(num_slots))
if sys.argv[1] == val:
Debug("sys.argv[1] " + str(sys.argv[1]) + " = val " + str(val))
enclosure = bp
Debug("Enclosure = " + str(enclosure))
break
if "enclosure" in globals():
break
if not enclosure:
print("Error: The backplane '" + sys.argv[1] + "' could not be found.")
sys.exit()
Debug("num_slots = " + str(num_slots))
if not int(sys.argv[2]) in range(0, num_slots):
print("ERROR: Backplane only has " + str(num_slots) + " slots.")
sys.exit()
State = sys.argv[3]
if re.search("on", State, re.IGNORECASE):
Verb = "--set=ident"
if re.search("off", State, re.IGNORECASE):
Verb = "--clear=ident"
print("INFO: Setting the locate LED for slot " + sys.argv[1] + " " + sys.argv[2] + " to " + sys.argv[3].lower())
SysExec("sg_ses -I " + sys.argv[2] + " " + Verb + " " + enclosure)
sys.exit()