Create a gist now

Instantly share code, notes, and snippets.

Yo when a disk fails smartctl asscessment
#!/usr/bin/python
import collections
from datetime import datetime
import json
import sys
try:
from pySMART import Device
except ImportError:
print """You will need to install pySMART from
https://pypi.python.org/pypi/pySMART to access
smartctl stats."""
sys.exit(1)
import requests
from traceback import print_exc
# map devices to mount points
DISKS = {
'sda': 'root',
'sdb': 'films',
'sdc': 'backup',
'sdd': 'tv'
}
DATA_FILE_LOCATION = '/tmp/disk_assessment.json'
# if you want to use justyo.co
YO = False
YO_API_TOKEN = 'APIKEY'
YO_USERNAME = 'NAME_TO_YO_AT'
YO_DATA = {
'api_token': YO_API_TOKEN,
'username': YO_USERNAME
}
def test_disks():
try:
data = {}
failed_disks = []
for device, name in DISKS.iteritems():
disk = Device('/dev/{}'.format(device))
data[name] = disk.assessment
# log failed ones so we can alert
if disk.assessment != 'PASS':
failed_disks.append(device)
sorted_data = collections.OrderedDict(sorted(data.items()))
write_data_file(sorted_data)
for name, reading in sorted_data.iteritems():
print "{0}: {1}".format(name, reading)
for device in failed_disks:
if YO:
requests.post("http://api.justyo.co/yo/", data=YO_DATA)
print "WARNING! Disk: {0} - {1} did not pass smartcl assessment"\
.format(device, DISKS[device])
except Exception as exception:
print_exc()
print "Failed to get sensor data: {0}".format(exception.message)
def write_data_file(data):
try:
data['time'] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
with open(DATA_FILE_LOCATION, 'w') as data_file:
data_file.write(json.dumps(data))
data_file.close()
return True
except Exception as exception:
print_exc()
print "Failed to write to data file. Error {}".format(
exception.message)
if __name__ == "__main__":
test_disks()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment