Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Last active July 6, 2018 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidpixo/a65e59017f866d7557139a0a21ac4a80 to your computer and use it in GitHub Desktop.
Save kidpixo/a65e59017f866d7557139a0a21ac4a80 to your computer and use it in GitHub Desktop.
Small/basic python3.6 script to read,import in pandas and export in multiple format your smartctl status.
*.db
*.html
*.xls
*.xslx
import pandas as pd # << you need to instal this!!!
import subprocess
import io
disks = ['DISK ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE']
# here I am querying for /dev/diskXXX for Mac, change to /dev/devXXX for *NIX.
for d in list(range(1,3)):
key = f'/dev/disk{d}'
#TODO: this stops the loop the first time we hit an error in smartclt. To improve: user input? search in /dev/?
try:
disks_key = subprocess.check_output(['smartctl','--attributes',key]).decode().split('\n')[7:-2]
print(f"smartctl retriving from disk '{key}'")
disks+=[key+' '+r for r in disks_key]
except subprocess.CalledProcessError as e:
print(f"smartctl cannot retrive from disk '{key}' stopping.\n returned error :\n error (code {e.returncode}): {e.output}")
break
df = pd.read_fwf( io.StringIO('\n'.join(disks)), widths=[10,4,24,9,7,6,7,10,9,12,20])
df['datetime'] = pd.datetime.now()
# EXAMPLES :
# save to sqlite3 db
import sqlite3
df.to_sql('smartctl_history',
sqlite3.connect("smartctl_history.db"),
if_exists='append',
index=False)
# search for rows containig fail
# print(df[df['TYPE'].str.contains('fail')])
# write output to html and highlight the world file
#
# def color_fail_red(val):
# color = 'red' if 'fail' in val else 'black'
# return 'color: %s' % color
#
# with open('smartctl.html','w') as f:
# f.write(df.style.applymap(color_fail_red, subset=['TYPE']).render())
#
# # write output to xlsx
# df.to_excel('smartctl.xlsx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment