Python - How to parse smartctl program output?

I am writing a wrapper for smartctl in python 2.7.3 ...

I have time trying to wrap my head around how to analyze output from a program smartctlon Linux (specific Ubuntu x64)

I run smartctl -l selftest /dev/sdxthrough a subprocess and grab the output to a variable

This variable is split into a list, then I remove the useless header data and empty lines from the output.

Now I am left with a list of strings, which is great!

The data is sorted by tables, and I want to analyze them in a dict () file full of lists (I think this is the right way to represent tabular data in Python from reading documents)

Here is an example of data:

Num  Test_Description    Status                  Remaining  LifeTime(hours)  LBA_of_first_error
# 1  Short offline       Completed without error       00%     44796         -
# 2  Short offline       Completed without error       00%     44796         -
# 3  Short offline       Completed without error       00%     44796         -
# 4  Short offline       Completed without error       00%     44796         -
# 5  Short offline       Completed without error       00%     44796         -
# 6  Extended offline    Completed without error       00%     44771         -
# 7  Short offline       Completed without error       00%     44771         -
# 8  Short offline       Completed without error       00%     44741         -
# 9  Short offline       Completed without error       00%         1         -
#10  Short offline       Self-test routine in progress 70%     44813         -

I see some problems trying to parse this, and I'm open to solutions, but I can also just do it all wrong :-):

  • Self-test routine in progress Remaining
  • Num 9 #

, , - .

, !

, - :

#testStatus.py

#This module provides an interface for retrieving
#test status and results for ongoing and completed
#drive tests

import subprocess

#this function takes a list of strings and removes
#strings which do not have pertinent information
def cleanOutput(data):

  cleanedOutput = []

  del data[0:3] #This deletes records 0-3 (lines 1-4) from the list

  for item in data:
    if item == '': #This removes blank items from remaining list
      pass
    else:
      cleanedOutput.append(item)

  return cleanedOutput


def resultsOutput(data):

  headerLines = []
  resultsLines = []
  resultsTable = {}

  for line in data:
    if "START OF READ" in line or "log structure revision" in line:
      headerLines.append(line)
    else:
      resultsLines.append(line)

  nameLine = resultsLines[0].split()
  print nameLine


def getStatus(sdxPath):
  try:
    output = subprocess.check_output(["smartctl", "-l", "selftest", sdxPath])

  except subprocess.CalledProcessError:
    print ("smartctl command failed...")

  except Exception as e:
    print (e)

  splitOutput = output.split('\n')

  cleanedOutput = cleanOutput(splitOutput)

  resultsOutput(cleanedOutput)


#For Testing
getStatus("/dev/sdb")
+3
1

- ; . , ( , ), , :

num = line[1:2]
desc = line[5:25]
status = line[25:54]
remain = line[54:58]
lifetime = line[60:68]
lba = line[77:99]

-. , , . "num" , "num". . ( ) , , , , , . , .

+1

All Articles