trevellyan

FreeNAS drive identification

Jan 28th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # FreeNAS drive identification
  4. # based on a shell script by @Bidule0hm
  5.  
  6. import subprocess
  7.  
  8. print '''\
  9. +========+============================================+====================+
  10. | Device | GPTID                                      |   Serial Number    |
  11. +========+============================================+====================+'''
  12.  
  13. drives = subprocess.check_output(['sysctl', '-n', 'kern.disks']).split()
  14. drives.sort()
  15.  
  16. for drive in drives:
  17.     try:
  18.         gptid = subprocess.check_output(['glabel', 'status', '-s', drive+'p2']).split()[0]
  19.     # not partitioned?
  20.     except subprocess.CalledProcessError:
  21.         gptid = 'N/A'
  22.  
  23.     try:
  24.         serial = filter(
  25.             lambda line: 'umber' in line,
  26.             subprocess.check_output(['smartctl', '-i', '/dev/'+drive]).splitlines()
  27.             )[0]
  28.         serial = serial.split()[2]
  29.     # SMART not supported?
  30.     except subprocess.CalledProcessError:
  31.         serial = 'N/A'
  32.  
  33.     print '|{0:^8}|{1:^44}|{2:^20}|'.format(drive, gptid, serial)
  34.     print '+--------+--------------------------------------------+--------------------+'
Add Comment
Please, Sign In to add comment