-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsenclosure
executable file
·1072 lines (723 loc) · 27.9 KB
/
lsenclosure
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
lsbackplane - List block devices attached to a storage backplane
"""
import re
import os
import sys
import time
from subprocess import Popen, PIPE, STDOUT
from prettytable import PrettyTable
# Cache info
CACHE_DATA_ARRAY = {}
CACHE_TIME_ARRAY = {}
# Enable/disable debugging messages
PRINT_DEBUG = True
def Debug(input_txt):
"""
Print debugging info on a single line.
"""
this_type = str(type(input_txt))
if PRINT_DEBUG:
if this_type == "<class 'dict'>":
for k in input_txt.keys():
print("DEBUG: " + str(k) + " - " + str(input_txt[k]))
else:
print("DEBUG: " + str(input_txt))
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(CACHE_DATA_ARRAY.keys())
if cmd in Cache_Keys:
Cache_Age = time.time() - CACHE_TIME_ARRAY[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 = CACHE_DATA_ARRAY[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")
CACHE_DATA_ARRAY[cmd] = f.read()
CACHE_TIME_ARRAY[cmd] = time.time()
f.close()
return_val = CACHE_DATA_ARRAY[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:
CACHE_DATA_ARRAY[cmd] = Popen(cmd.split(), stdout=PIPE, stderr=STDOUT).communicate()[0]
CACHE_TIME_ARRAY[cmd] = time.time()
return_val = CACHE_DATA_ARRAY[cmd]
if str(type(return_val)) == "<class 'bytes'>":
return_val = return_val.decode("utf-8")
return(return_val)
def which(program):
"""
Functions similar to the 'which' program in Unix. Given
an executable filename, it will return the whole path to
that executable.
which("ls") should return "/bin/ls"
Will print an error message and terminate the program if
it can't locate the executable in the path.
"""
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
def ext_candidates(fpath):
yield fpath
for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
yield fpath + ext
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
for candidate in ext_candidates(exe_file):
if is_exe(candidate):
return candidate
#####################################################################################################
def Bin_Requires(bin):
# Because "bin" is required for this script to run, we do a little
# extra work trying to find it before giving up.
if os.path.isfile("/sbin/" + bin):
return "/sbin/" + bin
elif os.path.isfile("/usr/sbin/" + bin):
return "/usr/sbin/" + bin
elif os.path.isfile("/usr/local/sbin/" + bin):
return "/usr/local/sbin/" + bin
bin_path = which(bin)
if not bin_path:
print("ERROR: Could not locate " + bin + " in the PATH")
sys.exit()
return(bin_path)
### If a recommended binary isn't available, you can still run, but let
### the user know it would work better if the binary was available
def Bin_Recommends(bin):
bin_path = which(bin)
if not bin_path:
print("INFO: This program would run better with " + bin + " in the PATH")
return bin_path
### If a suggested binary isn't available, run anyway
def Bin_Suggests(bin):
return which(bin)
### What external binaries do we need to have available
for bin in ("sg_ses", "sg_vpd", "lsblk", "lsscsi"):
bin_found = which(bin)
if not bin_found:
Bin_Requires(bin)
#####################################################################################################
def parse_lsblk():
"""
Return info on all block devices and partitions.
Note: RHEL8 has an ancient version of "lsblk" compared to Ubuntu,
and is missing some useful fields. :-/
"""
Debug("def parse_lsblk() entry")
m = {}
for line in SysExec("lsblk -O -P").splitlines():
# Parse all the keyval pairs
keyval = dict(re.findall(r'(\w+)="([^"]+)"', re.sub("-", "_", line)))
# Fetch the sd_dev device and delete it from the keyval pair
name = keyval["NAME"]
del keyval['NAME']
if re.search("loop", name):
continue
m["/dev/" + name] = keyval
Debug("parse_lsblk():: final_map = " + str(m))
Debug("def parse_lsblk() exit")
return(m)
def parse_lsblk_disks():
"""
parse_lsblk() returns info on disks, partitions, and other things.
Use this to filter it down to just disks
"""
Debug("def parse_lsblk_disks() entry")
map = parse_lsblk()
# First, iterate over map, find the lowest (h)ctl
min_h = 9999
for k in map.keys():
# Skip non-SCSI drives that don't have a HCTL value
if "HCTL" not in map[k]:
continue
h = int(map[k]["HCTL"].split(":")[0])
min_h = min(h, min_h)
# Create a working copy that only contains "disk" info
map2 = {}
for k in map.keys():
if map[k]["TYPE"] != "disk":
continue
if "HCTL" not in map[k]:
continue
this_h = int(map[k]["HCTL"].split(":")[0])
if this_h != min_h:
continue
map2[k] = map[k]
# Iterate over the initial map again. If there is "mpath" data,
# pluck it out and add it to the new map
for k in map.keys():
if map[k]["TYPE"] != "mpath":
continue
# Keys to save are "NAME" (mpatha), "KNAME" (dm-0),
# "PATH" (/dev/mapper/mpatha), "PTUUID" (to match), "PKNAME" (sda)
for k2 in map2.keys():
if map2[k2]["PTUUID"] == map[k]["PTUUID"]:
map2[k2]["MP_NAME"] = k.split("/")[-1]
map2[k2]["MP_KNAME"] = map[k]["KNAME"]
map2[k2]["MP_PKNAME"] = map[k]["PKNAME"]
map2[k2]["MP_PATH"] = map[k]["PATH"]
break
Debug("parse_lsblk_disks():: final_map = " + str(map))
Debug("def parse_lsblk_disks() exit")
return(map2)
def parse_lsscsi():
"""
Parse the output of "lssci -gUN" into a dict
"""
Debug("def parse_lsscsi() entry")
map = {}
c = 0
for line in SysExec("lsscsi -gUN").splitlines():
line = ' '.join(line.split())
hctl = line.split()[0]
hctl = re.sub("\]", "", hctl)
hctl = re.sub("\[", "", hctl)
type = line.split()[1]
sas_wwn = line.split()[2]
sd_dev = line.split()[3]
sg_dev = line.split()[4]
# If the sd_dev doesn't point to an actual sd_dev, give it a fake one
if sd_dev == "-":
sd_dev = "/dev/null" + str(c)
c = c + 1
map[sd_dev] = {}
map[sd_dev]["sg_dev"] = sg_dev
map[sd_dev]["hctl"] = hctl
map[sd_dev]["sas_wwn"] = sas_wwn
map[sd_dev]["type"] = type
# Since we have two HBA's, each backplane is visible twice. Let's eliminate duplicate copies by
# iterating over the map and only leaving backplanes from the lowest (h)ctl
min_h = 9999
for sd_dev in map:
h = int(map[sd_dev]["hctl"].split(":")[0])
min_h = min(min_h, h)
map2 = {}
for sd_dev, dict in map.items():
h = int(map[sd_dev]["hctl"].split(":")[0])
if h == min_h:
map2[sd_dev] = dict
Debug("parse_lsscsi():: final_map = " + str(map2))
Debug("def parse_lsscsi() exit")
return(map2)
def parse_lsscsi_disks():
"""
Parse the output of parse_lsscsi() and only return info on disks
"""
Debug("def parse_lsscsi_disks() entry")
map = parse_lsscsi()
map2 = parse_lsscsi()
for key in map.keys():
if map[key]["type"] != "disk":
del map2[key]
Debug("parse_lsblk_disks():: final_map = " + str(map2))
Debug("def parse_lsscsi_disks() exit")
return(map2)
def parse_lsscsi_enclosures():
"""
Parse the output of parse_lsscsi() and only return info on enclosures
"""
Debug("def parse_lsscsi_enclosures() entry")
map = parse_lsscsi()
map2 = parse_lsscsi()
for k in map.keys():
if map[k]["type"] != "enclosu":
del map2[k]
Debug("parse_lsblk_enclosures():: final_map = " + str(map2))
Debug("def parse_lsscsi_enclosures() exit")
return(map2)
def map_sata_wwn_to_hba_wwn():
"""
With SATA drives the HBA or backplane sometimes lies about the WWN of the actual drive.
Get a list of all drives (so we can map sd_devices to their sg_devices), and then
query sg_vpd --page=di <sg_dev> to find the mapping between fake WWN and real WWN.
"""
Debug("def map_sata_wwn_to_hb_wwn() entry")
map_sata_wwn_to_hba_wwn = {}
for sd_dev in map_sd_to_sg:
sg_dev = map_sd_to_sg[sd_dev]["sg_dev"]
wwn_list = []
for line in SysExec("sg_vpd --page=di " + str(sg_dev)).splitlines():
if not re.search("0x5", line):
continue
wwn_list.append(line.strip())
if len(wwn_list) != 2:
continue
map_sata_wwn_to_hba_wwn[wwn_list[1]] = wwn_list[0]
Debug("map_sata_wwn_to_hba_wwn():: final_map = " + str(map_sata_wwn_to_hba_wwn))
Debug("def map_sata_wwn_to_hb_wwn() exit")
return(map_sata_wwn_to_hba_wwn)
def GetLocateLEDState(This_Backplane, This_Slot):
"""
This function returns the state of the Locate LED on the given backplane/slot
"""
This_LedState = SysExec("sg_ses -I " + str(This_Slot) + \
" --get=ident " + This_Backplane).strip()
LedState_Descr = "Unknown"
if This_LedState == "1":
LedState_Descr = "On"
if This_LedState == "0":
LedState_Descr = "Off"
return(LedState_Descr)
def is_multipath_enabled():
"""
Probe server and see if multipath is enabled. True if yes, False if no.
"""
# Verify if the multipath binary exists in the PATH. If not, then
# it's not running multipath
if not which("multipath"):
return(False)
multipath_ll = SysExec("multipath -ll")
if re.search("DM multipath kernel driver not loaded", multipath_ll):
return(False)
# Now query multipath and see if it's actually running in multipath
# mode or not
num_lines = sum(1 for line in multipath_ll.splitlines())
if num_lines == 0:
return(False)
else:
return(True)
def map_dm_to_mpath():
"""
Map the dm block device exposed by block mapper to the corresponding /dev/mapper entry
"""
Debug("def map_dm_to_mpath() entry")
map = {}
for line in SysExec("ls -alh /dev/mapper").splitlines():
if not re.search("mpath", line):
continue
line = " ".join(line.split())
dm_dev = line.split()[-1].split("/")[1]
mpath_dev = line.split()[-3]
mpath_dev = re.sub("-part1", "", mpath_dev)
mpath_dev = re.sub("-part2", "", mpath_dev)
map[dm_dev] = mpath_dev
Debug("map_dm_to_mpath():: final_map = " + str(map))
Debug("def map_dm_to_mpath() exit")
return(map)
def map_dm_to_sd_dev():
"""
Map the dm block device exposed by device mapper to the underlying /dev/sd* entries
"""
Debug("def map_dm_to_sd_dev() entry")
map = {}
for line in SysExec("ls -1 /sys/block").splitlines():
if not re.search("^dm", line):
continue
dm_dev = line
sd_dev = SysExec("ls -1 /sys/block/" + str(dm_dev) + "/slaves")
sd_dev = sd_dev.replace("\n", ",").strip().rstrip(",")
if re.search("dm-", sd_dev):
continue
map[dm_dev] = sd_dev
Debug("map_dm_to_sd_dev(): final_map = " + str(map))
Debug("def map_dm_to_sd_dev() exit")
return(map)
def map_mpath_to_sd_dev():
"""
Combine the two previous function to map /dev/sda -> /dev/mapper/mpatha
"""
Debug("def map_mpath_to_sd_dev() entry")
map = {}
map_1 = map_dm_to_sd_dev()
map_2 = map_dm_to_mpath()
dict_1 = dict(map_1)
dict_2 = dict(map_2)
for dm in sorted(dict_1):
sd_dev = map_1[dm]
mpath = map_2[dm]
map[mpath] = sd_dev
Debug("map_mpath_to_sd_dev:: final_map = " + str(map))
Debug("def map_mpath_to_sd_dev() exit")
return(map)
def map_dev_to_rid():
"""
Return a map of /dev/ entry -> RID
"""
Debug("def map_dev_to_rid() entry")
Map = {}
output_ridlist = SysExec("ls -alh /dev/disk/by-label")
map_dm_mpath = map_dm_to_mpath()
for line in output_ridlist.splitlines():
if not re.search("rid-data-", line):
continue
This_Dev = line.split(" ")[-1]
This_Dev = This_Dev.split("/")[2]
if re.search("^sd", This_Dev):
This_Dev = re.sub("[0-9]*$", "", This_Dev)
This_Dev = "/dev/" + This_Dev
This_Rid = line.split(" ")[-3]
This_Rid = This_Rid.split("-")[2]
Debug("map_dev_to_rid():: Dev " + This_Dev + " = Rid " + This_Rid)
if multipath_active:
This_Dev = This_Dev.split("/")[2]
This_Dev = map_dm_mpath[This_Dev]
This_Dev = "/dev/mapper/" + This_Dev
Map[This_Dev] = This_Rid
Debug("map_dev_to_rid:: final_map = " + str(Map))
Debug("def map_dev_to_rid() exit")
return(Map)
def map_enclosures():
"""
Loop over the enclosures and pull together some info on them.
"""
Debug("def map_enclosures() entry")
map_lsscsi = parse_lsscsi_enclosures()
map = {}
for sd_dev, dict in map_lsscsi.items():
hctl = dict["hctl"]
wwn = dict["sas_wwn"]
sg_dev = dict["sg_dev"]
# WWN maps to the backplane, while HCTL and SG_Dev map depend on
# number of HBA's. For a depot with N hba's, they will have
# N unique values.
if not sg_dev in map:
map[sg_dev] = {}
map[sg_dev]["wwn"] = wwn
map[sg_dev]["hctl"] = hctl
# Figure out if it's the "front" or "back" backplane
output = SysExec("sg_ses -p aes " + str(sg_dev)).splitlines()
num_slots = 0
for i in output:
if re.search("Element type: SAS expander", i):
break
if re.search("Element index:", i):
num_slots = num_slots + 1
map_lsscsi[sd_dev]["num_slots"] = num_slots
if num_slots == 12:
map_lsscsi[sd_dev]["alias"] = "Back"
if num_slots == 24:
map_lsscsi[sd_dev]["alias"] = "Front"
if num_slots == 31:
map_lsscsi[sd_dev]["alias"] = "Valiant"
for line in SysExec("sg_ses -p cf " + sg_dev).splitlines():
if re.search("enclosure vendor:", line):
enc_vendor = re.sub("vendor:", ":", line)
enc_vendor = enc_vendor.split(":")[1].strip()
map_lsscsi[sd_dev]["enc_vendor"] = enc_vendor
if re.search("product:", line):
enc_product = re.sub(" rev:", ":", line)
enc_product = enc_product.split(":")[2].strip()
map_lsscsi[sd_dev]["enc_product"] = enc_product
Debug("def map_enclosures() exit")
return(map_lsscsi)
def map_sg_ses_enclosure_slot_to_sas_wwn():
"""
Map backplane/slot to SAS WWN
"""
Debug("def map_sg_ses_enclosure_slot_to_sas_wwn() entry")
map = {}
map_enc = map_enclosures()
map_wwn = map_sata_wwn_to_hba_wwn()
for enc in map_enc.keys():
sg_dev = map_enc[enc]["sg_dev"]
map[sg_dev] = {}
slot = ""
sas_wwn = ""
Debug("map_sg_ses_enclosure_slot_to_sas_wwn:: sg_dev = " + str(sg_dev))
for line in SysExec("sg_ses -p aes " + str(sg_dev)).splitlines():
if re.search("Element type: SAS expander", line):
break
if not re.search("(Element index:|SAS address|target port for:)", line):
continue
if re.search("attached SAS address", line):
continue
if re.search("target port for:", line):
line = line.split(":")[1].strip()
if line == "SSP":
protocol = "SAS"
elif line == "SATA_device":
protocol = "SATA"
else:
protocol = "UNKNOWN_PROTO"
if re.search("Element index:", line):
slot = ' '.join(line.split())
slot = slot.split(":")[1]
slot = ' '.join(slot.split())
slot = slot.split()[0]
if re.search("SAS address", line):
sas_wwn = line.split(":")[1].strip()
if protocol == "SATA":
if sas_wwn in map_wwn:
sas_wwn = map_wwn[sas_wwn]
if sas_wwn == "0x0":
sas_wwn = "EMPTY"
if slot and not slot in map[sg_dev]:
map[sg_dev][slot] = {}
if sas_wwn:
map[sg_dev][slot]["wwn"] = sas_wwn
if slot and sas_wwn:
slot = ""
sas_wwn = ""
# Debug("map_sg_ses_enclosure_slot_to_sas_wwn:: map = " + str(map))
Debug("def map_sg_ses_enclosure_slot_to_sas_wwn() exit")
return(map)
def return_sd_dev(wwn_1, map):
"""
Match the given wwn and return the sd_dev that belongs to it
"""
if wwn_1 == "EMPTY":
return("EMPTY")
for sd_dev in map.keys():
wwn_2 = map[sd_dev]["WWN"]
# Now, the actual Serial may be Serial, or Serial +/- 3.
# I wish I understood the logic here better.
wwn_m1 = hex(int(wwn_2, 16) - 1)
wwn_p1 = hex(int(wwn_2, 16) + 1)
wwn_m2 = hex(int(wwn_2, 16) - 2)
wwn_p2 = hex(int(wwn_2, 16) + 2)
Serial_list = [wwn_m2, wwn_m1, wwn_2, wwn_p1, wwn_p2]
if wwn_1 in Serial_list:
return(sd_dev)
return("UNKNOWN")
def HumanFriendlyVendor(Vendor, Model):
"""
Return the Vendor string for the media (Seagate, Hitachi, etc.)
Note that this isn't as straightforward as you'd hope because
many vendors are quite loose with the standards. This is meant to be
human-friendly, and you should always use the raw query instead when
trying to match something.
"""
# udev likes to use underscores instead of spaces, so change that here.
Vendor = re.sub("_", " ", Vendor)
Model = re.sub("_", " ", Model)
# Ok, sometimes they like to put the vendor in the model field
# so try to fix some of the more egregious ones
if Vendor == "ATA" or Vendor == "ATAPI" or Vendor == "UNKNOWN":
if re.search("Samsung", Model, re.I):
Vendor = "Samsung"
if re.search("Hitachi", Model, re.I):
Vendor = "Hitachi"
if re.search("Fujitsu", Model, re.I):
Vendor = "Fujitsu"
if re.search("Maxtor", Model, re.I):
Vendor = "Maxtor"
if re.search("MATSHITA", Model, re.I):
Vendor = "Matshita"
if re.search("LITE-ON", Model, re.I):
Vendor = "Lite-On"
if Model.startswith("WDC"):
Vendor = "WDC"
if Model.startswith("INTEL"):
Vendor = "Intel"
if Model.startswith("OCZ"):
Vendor = "OCZ"
if Model.startswith("Optiarc"):
Vendor = "Optiarc"
if re.search("KINGSTON", Model):
Vendor = "Kingston"
# Seagate is all the fark over the place...
if Vendor.startswith("ST"):
Vendor = "Seagate"
if Model.startswith("ST"):
Vendor = "Seagate"
if re.search("Toshiba", Model, re.I):
Vendor = "Toshiba"
if re.search("Maxtor", Vendor, re.I):
Vendor = "Maxtor"
if re.search("LITE-ON", Vendor):
Vendor = "Lite-On"
# If it's still set to ATAPI or ATA, then it's a unknown
# vendor who's *really* lax on following standards.
if Vendor in ('ATAPI', 'ATA'):
Vendor = "unknown"
return Vendor.strip()
def HumanFriendlyModel(Vendor, Model):
"""
Return the Model string for the media
Note that this isn't as straightforward as you'd hope because
many vendors are quite loose with the standards This is meant to be
human-friendly, and you should always use the raw query instead when
trying to match something.
"""
# udev likes to use underscores instead of spaces, so change that here.
Vendor = re.sub("_", " ", Vendor)
Model = re.sub("_", " ", Model)
# Ok, sometimes they like to put the vendor in the model field
# so try to fix some of the more egregious ones
if re.search("Hitachi", Model, re.I):
Model = re.sub("Hitachi", "", Model).strip()
Model = re.sub("HITACHI", "", Model).strip()
if re.search("Fujitsu", Model, re.I):
Model = re.sub("Fujitsu", "", Model).strip()
Model = re.sub("FUJITSU", "", Model).strip()
if re.search("Maxtor", Model, re.I):
Model = re.sub("Maxtor", "", Model).strip()
Model = re.sub("MAXTOR", "", Model).strip()
if re.search("Matshita", Model, re.I):
Model = re.sub("Matshita", "", Model).strip()
Model = re.sub("MATSHITA", "", Model).strip()
if re.search("LITE-ON", Model, re.I):
Model = re.sub("Lite-On", "", Model).strip()
Model = re.sub("LITE-ON", "", Model).strip()
if re.search("KINGSTON", Model):
Model = re.sub("KINGSTON ", "", Model)
if Model.startswith("WDC"):
Model = re.sub("^WDC", "", Model).strip()
if Model.startswith("INTEL"):
Model = re.sub("^INTEL", "", Model).strip()
if Model.startswith("OCZ-"):
Model = re.sub("^OCZ-", "", Model).strip()
if Model.startswith("Optiarc"):
Model = re.sub("^Optiarc", "", Model).strip()
if Vendor.startswith("ST"):
Model = Vendor + Model
if Model.startswith("TOSHIBA"):
Model = re.sub("^TOSHIBA ", "", Model).strip()
if Vendor.startswith("MAXTOR"):
Model = re.sub("^MAXTOR ", "", Vendor + Model).strip()
if Model.startswith("ATAPI"):
Model = re.sub("^ATAPI", "", Model)
return Model.strip()
def HumanFriendlySerial(Serial, Vendor, Model):
"""
Try to de-crapify the Serial Number (again, polluted with other fields)
"""
NO_SERIAL = "NO_SERIAL"
if Serial == Model:
return NO_SERIAL
Serial = Serial.split("_")[-1]
Serial = re.sub("^SATA", "", Serial) # one drive starts with "SATA_"
Serial = re.sub(Model, "", Serial) # Filter out the model
Serial = re.sub(Model[:-1], "", Serial) # Filter out the model
Serial = re.sub(Vendor, "", Serial) # Filter out the Vendor
Serial = re.sub("^(_)*", "", Serial) # Filter out leading "_"
Serial = re.sub("(_)*$", "", Serial) # Filter out trailing "_"
Serial = re.sub("^-", "", Serial) # Another edge case... (WDC...)
Serial = re.sub("^WD-", "", Serial) # Yet another WDC edge case...
Serial = Serial.strip() # and strip
if not Serial:
Serial = "NO_SERIAL"
return Serial
### Main()
### Determine whether multipathing is enabled or not.
### Enabled requires more data and is a bit more complex
multipath_active = is_multipath_enabled()
Debug("main:: multipath_active = " + str(multipath_active))
### Use lsblk to report info on all detected block devices.
### map_lsblk will be the primary data structure of this script,
### and others will be used to augment it before printing
map_lsblk = parse_lsblk_disks()
Debug("main:: map_lsblk = " + str(map_lsblk))
### Parse "lsscsi" output and add useful fields to map_lsblk
map_sd_to_sg = parse_lsscsi_disks()
for sd_dev, d in map_sd_to_sg.items():
if not re.search("/dev/", sd_dev):
continue
map_lsblk[sd_dev]["sg_dev"] = d["sg_dev"]
map_lsblk[sd_dev]["hctl"] = d["hctl"]
map_lsblk[sd_dev]["lsscsi_wwn"] = d["sas_wwn"]
Debug("main:: map_lsblk = " + str(map_lsblk))
### Map of sd_dev -> RID
dev_to_rid_map = map_dev_to_rid()
#Debug("main:: dev_to_rid_map = " + str(dev_to_rid_map))
### Scan the backplanes and build a dict of useful info
map_bp_slot = map_enclosures()
Debug("main:: map_bp_slot = " + str(map_bp_slot))
### Parse "sg_ses" and get enclosure/slot info for each wwn
map_es_to_sas_wwn = map_sg_ses_enclosure_slot_to_sas_wwn()
Debug("main:: map_es_to_sas_wwn = " + str(map_es_to_sas_wwn))
### Loop over enclosures to find the alias/backplane and add it to map_lsblk
for sg_dev in map_es_to_sas_wwn:
Debug("main:: Looking for " + sg_dev + " in map_bp_slot()")
for key, d in map_bp_slot.items():
print("FOOBAR! key = " + str(key) + " and d = " + str(d))
actual_sg_dev = d["sg_dev"]
num_slots = d["num_slots"]
alias = d["alias"]
Debug("actual_sg_dev = " + str(actual_sg_dev) + " and num_slots = " + \
str(num_slots) + " and alias = " + str(alias))
if sg_dev == actual_sg_dev:
print("XYZZY map_es_to_sas_wwn = " + str(map_es_to_sas_wwn))
for slot in map_es_to_sas_wwn[sg_dev]:
wwn_1 = map_es_to_sas_wwn[sg_dev][slot]["wwn"]
sd_dev = return_sd_dev(wwn_1, map_lsblk)
Debug("main:: sg_dev " + str(actual_sg_dev) + " slot " + \
str(slot) + " maps to sd_dev " + str(sd_dev))
if sd_dev in map_lsblk:
map_lsblk[sd_dev]["backplane"] = actual_sg_dev
map_lsblk[sd_dev]["slot"] = slot
map_lsblk[sd_dev]["bp_alias"] = alias
### Add the RID to map_lsblk
for key, d in map_lsblk.items():
bd = key
if multipath_active:
bd = d["MP_PATH"]
if bd in dev_to_rid_map:
map_lsblk[key]["RID"] = dev_to_rid_map[bd]
else:
map_lsblk[key]["RID"] = "NORID"
### Add the locate LED status to map_lsblk
for sd_dev, d in map_lsblk.items():
backplane = d["backplane"]
slot = d["slot"]
map_lsblk[sd_dev]["locate_led"] = GetLocateLEDState(backplane, slot)