-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_slot
executable file
·281 lines (200 loc) · 8.11 KB
/
power_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
#!/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
"""
# 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
# New depots
if re.search(r"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
###########################################################
# 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()
if SAS_Controller == "LSI_Invader":
# We have the backplane/slot, so use lsslot to get the dev entry
for line in SysExec("lsslot").splitlines():
if re.search(sys.argv[1], line, re.IGNORECASE):
This_Slot = line.split("|")[2].strip()
if This_Slot == sys.argv[2]:
Dev = line.split("|")[4].strip()
break
if not Dev:
sys.exit()
# Now use lsblock to get the serial number of the drive
for line in SysExec("lsblock").splitlines():
if re.search(Dev + " ", line, re.IGNORECASE):
Serial = line.split()[6]
if all(c in string.hexdigits for c in Serial):
Debug("Serial " + Serial + " appears to be a WWN hex")
Serial = "0x" + Serial
Non_WWN_Serial = "UNKNOWN_NON_WWN_SERIAL"
else:
# Preserve this for the cms-depot18 case and any similar...
Non_WWN_Serial = Serial
Debug("Serial " + Serial + " appears to be a serial number. Attempting to find matching WWN...")
# Change /dev/sda to sda for matching...
short_Dev = Dev.split("/")[-1]
output = SysExec("ls -alh /dev/disk/by-id")
for l in output.splitlines():
if not re.search(short_Dev + "$", l):
continue
Serial = l.split(" ")[-3].split("-")[-1]
Debug("Serial = " + Serial)
if not Serial:
sys.exit()
# Now, the actual Serial may be Serial, or Serial +/- 1 or 2...
Serial_m1 = hex(int(Serial, 16) - 1)
Serial_p1 = hex(int(Serial, 16) + 1)
Serial_m2 = hex(int(Serial, 16) - 2)
Serial_p2 = hex(int(Serial, 16) + 2)
Serial_list = [ Serial, Serial_m1, Serial_p1, Serial_m2, Serial_p2 ]
Debug("Serial_list = " + str(Serial_list))
# Now use MegaCli64 to find the Enclosure and Slot number. PITA....
Found = 0
for line in SysExec("MegaCli64 -PDList -aALL").splitlines():
if not re.search("Enclosure Device ID:", line) and not re.search("Slot Number:", line) and not re.search("Inquiry Data:", line) and not re.search("SAS Address", line):
continue
if re.search("Enclosure Device ID:", line):
Enclosure = line.split(":")[1].strip()
if re.search("Slot Number:", line):
Slot = line.split(":")[1].strip()
if re.search("Inquiry Data:", line):
if re.search(Non_WWN_Serial, line):
Debug("Found match to Non_WWN_Serial:" + line)
Found = 1
if re.search("SAS Address", line):
Test_Serial = line.split(":")[1].strip()
if Test_Serial in Serial_list:
Found = 1
if Found == 1:
break
if Found:
Debug("Matched with Enclosure " + Enclosure + " and Slot " + Slot)
# Ok, we should have both an Enclosure and Slot number
print("Setting the locate LED for slot " + sys.argv[1] + " " + sys.argv[2] + " to " + sys.argv[3].lower())
if re.search("on", sys.argv[3], re.IGNORECASE):
Verb = "-start"
if re.search("off", sys.argv[3], re.IGNORECASE):
Verb = "-stop"
SysExec("MegaCli64 -PdLocate " + Verb + "-physdrv[" + Enclosure + ":" + Slot + "] -aAll")
else:
print("ERROR: Couldn't map /dev to backplane/slot. Quitting...")
sys.exit()
if SAS_Controller == "LSI_Thunderbolt" or SAS_Controller == "LSI_Falcon" or SAS_Controller == "LSI_FusionMPT":
# It's a good card. Add any other card which responds normally to sg_ses commands
Backplane = {}
SG_Dev = ""
Slots = ""
for line in SysExec("lsbackplane").splitlines():
if not re.search("/dev/", line):
continue
Alias = line.split("|")[5].strip()
if re.search(sys.argv[1], Alias, re.IGNORECASE):
SG_Dev = line.split("|")[1].strip()
Slots = int(line.split("|")[2].strip())
if int(sys.argv[2]) >= Slots:
# A Error: Backplane Front has 24 slots and you specified slot 4
print("A Error: Backplane '" + Alias + "' has " + str(Slots) + " slots and you specified slot " + sys.argv[2] + ".")
sys.exit()
if int(sys.argv[2]) < 0:
print("B Error: Backplane '" + Alias + "' has " + str(Slots) + " slots and you specified slot " + sys.argv[2] + ".")
sys.exit()
if not SG_Dev:
print("Error: The backplane '" + sys.argv[1] + "' could not be found.")
sys.exit()
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()
State = sys.argv[3]
if re.search("on", State, re.IGNORECASE):
Verb = "--clear=devoff"
if re.search("off", State, re.IGNORECASE):
Verb = "--set=devoff"
print("Setting the power state for slot " + sys.argv[1] + " " + sys.argv[2] + " to " + sys.argv[3].lower())
SysExec("sg_ses -I " + sys.argv[2] + " " + Verb + " " + SG_Dev)
sys.exit()