forked from louwrentius/showtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow
More file actions
executable file
·750 lines (577 loc) · 22.4 KB
/
show
File metadata and controls
executable file
·750 lines (577 loc) · 22.4 KB
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
#!/usr/bin/env python
#
# Author: Louwrentius
#
# Requirement: hdparm, ethtool, smartmontools
#
# Version: 1.03
#
import re
import subprocess
import sys
import os
import argparse
def set_arguments():
#
# Parse command line options
#
parser = argparse.ArgumentParser(description='Show detailed disk|net \
device information in ASCII table format')
parser.add_argument('devicetype', choices=['disk', 'net'],
help="Show disk information")
sa = parser.add_argument_group(title="Generic settings", description="Options that apply for all device types.")
sa.add_argument("-E", "--transparent",
action="store_true", help="Disable table formatting (no lines)")
sa.add_argument("-g", "--noheader",
action="store_true", help="Disable table header (in transparent mode)")
sg = parser.add_argument_group(title="Storage (generic)",
description="Generic\
options for storage devices")
sg.add_argument("-a", "--all-opts",
action="store_true", help="show all information")
sg.add_argument("-m", "--model",
action="store_true", help="device model")
sg.add_argument("-S", "--serial",
action="store_true", help="device serial number")
sg.add_argument("-D", "--state", action="store_true",
help="drive power status (active/standby)")
sg.add_argument("-e", "--apm", action="store_true",
help="Advanced Power Mode")
sg.add_argument("-s", "--size",
action="store_true", help="device size in Gigabytes")
sg.add_argument("-f", "--firmware", action="store_true",
help="device firmware version")
sg.add_argument("-c", "--controller", action="store_true",
help="controller to which device is connected")
sg.add_argument("-p", "--pcipath", action="store_true",
help="/dev/disk/by-path/ ID of the device")
sg.add_argument("-w", "--wwn", action="store_true",
help="device World Wide Name")
sg.add_argument("-o", "--scsi", action="store_true",
help="/dev/by-id/scsi")
ss = parser.add_argument_group(title="Storage (SMART)", description=
"Options based on SMART values of storage\
devices")
ss.add_argument("-t", "--temp", action="store_true", help="temperature\
in Celcius")
ss.add_argument("-H", "--hours", action="store_true", help="power on\
hours")
ss.add_argument("-P", "--pending", action="store_true", help="pendinag\
sector count")
ss.add_argument("-r", "--reallocated", action="store_true", help=
"reallocated sector count")
ss.add_argument("-R", "--reallocatedevent", action="store_true", help=
"reallocated sector event count")
ss.add_argument("-C", "--crc", action="store_true", help="CRC error")
ss.add_argument("-u", "--startstop", action="store_true", help="spin up/down")
ss.add_argument("-n", "--park", action="store_true", help="head parking")
nw = parser.add_argument_group(title="Network",
description="Available options for\
`network devices")
nw.add_argument("-l", "--link", action="store_true",
help="network card link status")
nw.add_argument("-4", "--ipv4", action="store_true",
help="IPv4 address")
nw.add_argument("-6", "--ipv6", action="store_true",
help="IPv6 address")
nw.add_argument("-M", "--mac", action="store_true",
help="hardware / MAC address")
nw.add_argument("-T", "--show-type", action="store_true",
help="network card type")
nw.add_argument("-d", "--driver", action="store_true",
help="driver module")
nw.add_argument("-F", "--firmware-version", action="store_true",
help="firmware version")
return parser
def main():
parser = set_arguments()
try:
args = parser.parse_args()
except OSError:
parser.print_help()
sys.exit(1)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
devicetype = args.devicetype
def get_table_header(args):
header_options = (
('model', 'Model', 'disk'),
('serial', 'Serial Number', 'disk'),
('state', 'State', 'disk'),
('apm', 'APM', 'disk'),
('size', 'GB', 'disk'),
('firmware', 'Firmware', 'disk'),
('controller', 'Controller', 'disk'),
('pcipath', '/dev/disk/by-path', 'disk'),
('wwn', '/dev/disk/by-id/wwn*', 'disk'),
('scsi', '/dev/disk/by-id/scsi*', 'disk'),
('temp', 'Temp', 'disk'),
('hours', 'Hours', 'disk'),
('pending', 'PS', 'disk'),
('reallocated', 'RS', 'disk'),
('reallocatedevent', 'RSE', 'disk'),
('crc', 'CRC', 'disk'),
('startstop', 'Spin', 'disk'),
('park', 'Park', 'disk'),
('link', 'Link', 'net'),
('ipv4', 'IPv4', 'net'),
('ipv6', 'IPv6', 'net'),
('mac', 'MAC', 'net'),
('show_type', 'Type', 'net'),
('driver', 'Driver', 'net'),
('firmware', 'Firmware', 'net'))
header = ['Dev']
for option in header_options:
if option[2] == args.devicetype:
if args.all_opts or getattr(args, option[0]):
if option[2] == args.devicetype:
header.append(option[1])
#print "Header " + str(header)
return header
table_data = []
table_header = get_table_header(args)
for device in get_devices(devicetype):
if not device.startswith("md") and not device.startswith("ram"):
devicedata = process_device(device, args)
if devicedata:
table_data.append(devicedata)
display_table(table_header, table_data, args)
def get_devices(devicetype):
if devicetype == "disk":
devices = get_block_devices()
elif devicetype == "net":
devices = get_interfaces()
for device in devices:
yield device
return
#
# Get all network devices
#
def get_net_devices():
netdevices = open('/proc/net/dev', 'r').readlines()
return netdevices
def get_interfaces():
interfaces = []
devices = get_net_devices()
for line in devices[2:]:
data = line.split(':')
interfaces.append(data[0].strip())
interfaces.sort()
return interfaces
#
# Get all disk devices
#
def get_block_devices():
devicepath = "/sys/block"
diskdevices = os.listdir(devicepath)
diskdevices.sort()
return diskdevices
def get_pci_devices():
try:
pci_devices = subprocess.Popen(['lspci'], stdout=subprocess.PIPE,
stderr=
subprocess.PIPE).communicate()[0]
return pci_devices
except:
print "Error when executing lspci, you might need 'pciutils'\
if you run a Red Hat flavour."
sys.exit(1)
def get_all_disk_paths():
disk_by_path_data = subprocess.Popen(['ls', '-alh', '/dev/disk/by-path'],
stdout=subprocess.PIPE,
stderr=
subprocess.PIPE).communicate()[0]
return disk_by_path_data
def get_all_disk_wwns():
disk_by_id_data = subprocess.Popen(['ls', '-alh', '/dev/disk/by-id'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()[0]
return disk_by_id_data
def generic_match_finder(device, data, pattern, matchgroup, split, itemnumber):
for item in data.splitlines():
if device in item:
regex = re.compile(pattern + '(.*)')
match = regex.search(item)
if match:
model = match.group(matchgroup).split(split)[itemnumber]
return model
return ""
def get_disk_deviceid(diskdevice, diskbypathdata):
deviceid = generic_match_finder(diskdevice, diskbypathdata, 'pci-0000:', 1,
".", 0)
return deviceid
def get_disk_path(diskdevice, diskbypathdata):
diskpath = generic_match_finder(diskdevice, diskbypathdata, 'pci-', 0, " ",
0)
return diskpath
def get_disk_wwn(diskdevice, diskbyiddata):
diskwwn = generic_match_finder(diskdevice, diskbyiddata, 'wwn', 0, " ", 0)
return diskwwn
def get_disk_scsi(diskdevice, diskbyiddata):
diskscsi = generic_match_finder(diskdevice, diskbyiddata, 'scsi', 0, " ",
0)
return diskscsi
def get_pci_device_name(diskdevice, pci_devices, diskbypathdata):
deviceid = get_disk_deviceid(diskdevice, diskbypathdata)
devicename = generic_match_finder(deviceid, pci_devices, deviceid, 1, ":",
1)
return devicename
def get_disk_model(hdparmdata):
model = generic_match_finder("", hdparmdata, 'Model Number:', 1, ", ",
0)
return model.strip()
def get_disk_serial(hdparmdata):
serial = generic_match_finder("", hdparmdata, 'Serial Number:', 1, ", ", 0)
return serial.strip()
def get_disk_drivestate(hdparmdata):
drivestate = generic_match_finder("", hdparmdata, 'drive state is:', 1, ", ", 0)
return drivestate.strip()
def get_disk_apmstate(hdparmdata):
apmstate = generic_match_finder("", hdparmdata, 'Advanced power management level:', 1, ", ", 0)
return apmstate.strip()
def get_fdisk_size(device):
command = "fdisk -l /dev/" + device
p = subprocess.Popen([command], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
rawdata = p.communicate()
return rawdata[0]
def get_disk_size(dev):
data = get_fdisk_size(dev)
if data:
raw = generic_match_finder("", data, ", ", 1, ", ", 0)
bytes = raw.split(" ")[0]
#Patch for " "octects of last entry
bytes = bytes.split('\xc2')[0]
gb = int(bytes) / 1000000000
return str(gb)
else:
return ""
def get_disk_firmware(hdparmdata):
match = generic_match_finder("", hdparmdata, 'Firmware Revision:', 1, ":",
0)
return match.strip()
def get_hdparm_data(device):
command = "hdparm -IC " + device
p = subprocess.Popen([command], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
rawdata = p.communicate()
returncode = p.returncode
if returncode == 13:
raise IOError
if returncode == 127:
raise OSError
if returncode > 0:
pass
return rawdata
def get_disk_data(device):
try:
data = get_hdparm_data(device)
return data[0]
except IOError:
print "Error: Did you use sudo or are you root?"
sys.exit(1)
except OSError:
print "Error: is hdparm installed?"
sys.exit(1)
#
# Processing of SMART DATA
#
def get_parameter_from_smart(data, parameter, distance):
regex = re.compile(parameter + '(.*)')
match = regex.search(data)
if match:
tmp = match.group(1)
length = len(tmp.split(" "))
if length <= distance:
distance = length-1
#
# SMART data is often a bit of a mess, so this
# hack is used to cope with this.
#
try:
model = match.group(1).split(" ")[distance].split(" ")[1]
except:
model = match.group(1).split(" ")[distance+1].split(" ")[1]
return str(model)
return "?"
def get_smart_data(device):
#
# For debugging purposes
#
try:
file = os.environ['smartdata']
if os.path.isfile(file):
f = open(file, 'r')
data = f.read()
return data
except:
pass
try:
child = subprocess.Popen(['smartctl', '-a', '-d', 'ata', device],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except OSError:
print "Executing smartctl gave an error, is smartmontools installed?"
sys.exit(1)
rawdata = child.communicate()
if child.returncode:
child = subprocess.Popen(['smartctl', '-a', device],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
rawdata = child.communicate()
if child.returncode == 1:
return ""
smartdata = rawdata[0]
return smartdata
#
# Network functions
#
def search_data_for_match(regex, data):
match = re.search(regex, data)
if match:
return match.group(1)
return ''
def get_interface_data(interface):
raw_data = subprocess.Popen(['ifconfig', interface],
stdout=subprocess.PIPE).communicate()[0]
return raw_data
def get_driver_data(interface, parameter):
if parameter:
command = ["ethtool", parameter, interface]
else:
command = ["ethtool", interface]
try:
raw_data = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()[0]
except OSError:
print "It seems that ethtool is not installed..."
sys.exit(1)
return raw_data
def get_interface_type(data):
match = re.search('encap:(\S+)', data)
if match:
return match.group(1)
return ''
def is_smart_used(args):
if args.temp or args.all_opts:
return True
if args.hours or args.all_opts:
return True
if args.pending or args.all_opts:
return True
if args.reallocated or args.all_opts:
return True
if args.reallocatedevent or args.all_opts:
return True
if args.crc or args.all_opts:
return True
if args.startstop or args.all_opts:
return True
if args.park or args.all_opts:
return True
return False
def process_device(dev, args):
"""
This function gathers all data from a device and puts this data
into a list. This list will become a single row in the output table.
"""
devicedata = []
devicedata.append(dev)
if args.devicetype == "disk":
fullpath = "/dev/" + dev
diskdata = get_disk_data(fullpath)
disksize = get_disk_size(dev)
if not disksize:
return None
pci_devices = get_pci_devices()
disk_paths = get_all_disk_paths()
disk_wwns = get_all_disk_wwns()
if is_smart_used(args):
disksmart = get_smart_data(fullpath)
if args.model or args.all_opts:
diskmodel = get_disk_model(diskdata)
devicedata.append(diskmodel)
if args.serial or args.all_opts:
diskserial = get_disk_serial(diskdata)
devicedata.append(diskserial)
if args.state or args.all_opts:
drivestate = get_disk_drivestate(diskdata)
devicedata.append(drivestate)
if args.apm or args.all_opts:
apmstate = get_disk_apmstate(diskdata)
devicedata.append(apmstate)
if args.size or args.all_opts:
devicedata.append(disksize)
if args.firmware or args.all_opts:
diskfw = get_disk_firmware(diskdata)
devicedata.append(diskfw)
if args.controller or args.all_opts:
pcidevice = get_pci_device_name(dev, pci_devices, disk_paths)
devicedata.append(pcidevice)
if args.pcipath or args.all_opts:
devicepath = get_disk_path(dev, disk_paths)
devicedata.append(devicepath)
if args.wwn or args.all_opts:
devicewwn = get_disk_wwn(dev, disk_wwns)
devicedata.append(devicewwn)
if args.scsi or args.all_opts:
devicescsi = get_disk_scsi(dev, disk_wwns)
devicedata.append(devicescsi)
#
# SMART DATA
#
if args.temp or args.all_opts:
disktemp = get_parameter_from_smart(disksmart,
'Temperature_Celsius', 10)
devicedata.append(disktemp)
if args.hours or args.all_opts:
diskpoweronhours = get_parameter_from_smart(disksmart,
'Power_On_Hours', 12)
devicedata.append(diskpoweronhours)
if args.pending or args.all_opts:
match1 = get_parameter_from_smart(disksmart,
'Total_Pending_Sectors', 10)
match2 = get_parameter_from_smart(disksmart,
'Current_Pending_Sector', 10)
if match1:
diskcurrentpending = match1
if match2:
diskcurrentpending = match2
if not match1 and not match2:
diskcurrentpending = "?"
devicedata.append(diskcurrentpending)
if args.reallocated or args.all_opts:
diskreallocatedsector = \
get_parameter_from_smart(disksmart, 'Reallocated_Sector_Ct', 9)
devicedata.append(diskreallocatedsector)
if args.reallocatedevent or args.all_opts:
diskreallocatedevent = \
get_parameter_from_smart(disksmart,
'Reallocated_Event_Count', 9)
devicedata.append(diskreallocatedevent)
if args.crc or args.all_opts:
diskudmacrcerror =\
get_parameter_from_smart(disksmart, 'UDMA_CRC_Error_Count',
10)
devicedata.append(diskudmacrcerror)
if args.startstop or args.all_opts:
diskstartstop =\
get_parameter_from_smart(disksmart, 'Start_Stop_Count',
10)
devicedata.append(diskstartstop)
if args.park or args.all_opts:
diskloadcycle =\
get_parameter_from_smart(disksmart, 'Load_Cycle_Count',
10)
devicedata.append(diskloadcycle)
#
# Network data
#
elif args.devicetype == "net":
raw_ifconfig = get_interface_data(dev)
raw_ethtool = get_driver_data(dev, "-i")
raw_ethtool_extended = get_driver_data(dev, "")
if args.link or args.all_opts:
link = search_data_for_match('Link\ detected:\ (\S+)',
raw_ethtool_extended)
devicedata.append(link)
if args.ipv4 or args.all_opts:
ipv4 = search_data_for_match('inet addr:(\S+)', raw_ifconfig)
devicedata.append(ipv4)
if args.ipv6 or args.all_opts:
ipv6 = search_data_for_match('inet6 addr: (\S+)', raw_ifconfig)
devicedata.append(ipv6)
if args.mac or args.all_opts:
mac = search_data_for_match('HWaddr (\S+)', raw_ifconfig)
devicedata.append(mac)
if args.show_type or args.all_opts:
itype = search_data_for_match('encap:(\S+)', raw_ifconfig)
devicedata.append(itype)
if args.driver or args.all_opts:
driver = search_data_for_match('driver:\ (\S+)', raw_ethtool)
devicedata.append(driver)
if args.firmware_version or args.all_opts:
firmware = search_data_for_match('firmware-version:\ (\S+)',
raw_ethtool)
devicedata.append(firmware)
else:
print "This is a bug, please inform the developer."
sys.exit(1)
#print "Devicedata " + str(devicedata)
return devicedata
#
# Get collumn size for proper table formatting
# Find the biggest string in a collumn
#
def get_collumn_size(table_header, table_data):
col_count = len(table_data[0])
col_widths = []
for i in xrange(col_count):
collumn = []
for row in table_data:
header_length = len(table_header[i])
data_length = len(row[i])
#
# The width of the header can be bigger than the data
#
if header_length > data_length:
collumn.append(header_length)
else:
collumn.append(data_length)
length = max(collumn)
col_widths.append(length)
#print "All coll widths: " + str(col_widths)
return col_widths
def display_table(table_header, table_data, args):
header_length = len(table_header)
data_length = len(table_data[0])
assert header_length == data_length, "Column count mismatch! %r/%r" % (header_length, data_length)
col_widths = get_collumn_size(table_header, table_data)
# Dirty hack to get a closing pipe character at the end of the row
col_widths.append(1)
# Some values to calculate the actual table with, including spacing
spacing = 1
delimiter = 3
table_width = (sum(col_widths) + len(col_widths) * spacing * delimiter) - \
delimiter
format = ""
for col in col_widths:
if args.transparent:
form = " %-" + str(col) + "s "
else:
form = "| %-" + str(col) + "s "
format += form
#
# Print header
#
table_header.append("")
if args.transparent:
if not args.noheader:
print '%s' % ' '*table_width
print format % tuple(table_header)
print '%s' % ' '*table_width
else:
print '%s' % '-'*table_width
#print table_header
print format % tuple(table_header)
print '%s' % '-'*table_width
#
# Print actual table contents
#
for row in table_data:
row.append("")
print format % tuple(row)
if not args.transparent:
print '%s' % '-'*table_width
#if hdparm_error:
# print "ERROR: hdparm not installed or not working!"
#
# Define table and add header as first row
# The header also defines the table / collumn width
#
#
# Main: get all devices and their data and display it in a table
#
if __name__ == "__main__":
main()