#!/usr/pythoncontroller/python 

import sys
import devhealth

def usage(cmd):
    print "Usage: %s [device] [test type] [device type (smartctl)]\n" % cmd
    print "If no argument is provided, it runs the default test (health) on all devices seen by smart tools."
    print "Valid device arguments are: /dev/ entries or 'all' for all devices. Default is 'all'."
    print "Valid test types are: 'health' (default short test), 'xerrors' (extended errors), and 'attrib' (shows all attributes)"
    print "Valid devtypes are input of the -d parameter of smartctl, e.g. 'cciss,0', '3ware,0', 'areca,0'"
    print "\nExtended Info mode: %s <-x|--extended> <device> : provides info on error and prefailure attributes.\n" % cmd
    sys.exit()

def extended_smartinfo(device):

    hdicts = devhealth.getSmartDevHealthDicts('health',device,'')
    d = hdicts[0]

    xdicts = devhealth.getSmartDevHealthDicts('attrib',device,'')

    assert( len(hdicts) == 1 and len(xdicts) == 1)
    x = xdicts[0]

    print ""
    print "================================================================================================="

    print "\nDEVICE: %s" % d['device']
    print "\nHEALTH CHECK: Status: %d, Health: %s, Result: %s, RetValue: %s" % \
		(d['status'], d['health_state'], d['result'], d['smcode'])
    if d['status'] != 1:
	print "\n%s" % d['error']
    if int(d['smcode']) != 0:
	print "SMART ERROR BITS: %08s" % bin(int(d['smcode'])).zfill(8)

    print "\nATTRIB CHECK: Status: %d, Health: %s, Result: %s, RetValue: %s" % \
	    (x['status'], x['health_state'], x['result'], x['smcode'])

    if int(x['smcode']) != 0:
	print "SMART ERROR BITS: %08s\n" % bin(int(x['smcode'])).zfill(8)

	xa = x['attribs'] if x.has_key('attribs') else {}
	for k in xa.iterkeys():
	    # locate "prefailure warning" attributes
	    if xa[k].has_key('flags') and xa[k]['flags'].lower().find('p') > 0:
		print "PREFAIL WARN: [ID: %3s] %24s: value: %3s, thresh: %3s, worst: %3s, raw_value: %6s" % \
			(xa[k]['id'], k, xa[k]['value'], xa[k]['thresh'], xa[k]['worst'], xa[k]['raw_value'])
	    # locate "error rate" attributes
	    elif xa[k].has_key('flags') and xa[k]['flags'].lower().find('r') > 0:
		print "ERROR RATE  : [ID: %3s] %24s: value: %3s, thresh: %3s, worst: %3s, raw_value: %6s" % \
			(xa[k]['id'], k, xa[k]['value'], xa[k]['thresh'], xa[k]['worst'], xa[k]['raw_value'])

	print "================================================================================================="



device=None
test='health'
devtype=''

for a in sys.argv:
    if a in ['-h', '--help']:
	usage(sys.argv[0])

if len(sys.argv) > 4:
    usage(sys.argv[0])

elif len(sys.argv) == 2 and len(sys.argv[1]) > 2:

    device = sys.argv[1] if sys.argv[1].lower() != 'all' else None

elif len(sys.argv) == 3 and len(sys.argv[1]) > 1 and len(sys.argv[2]) > 2:

    if sys.argv[1] in ['-x', '--extended']:
	device = sys.argv[2]
	extended_smartinfo(device) # special extended mode
	sys.exit()

    else:
	device = sys.argv[1] if sys.argv[1].lower() != 'all' else None
	test = sys.argv[2]

elif len(sys.argv) == 4 and len(sys.argv[1]) > 2 and len(sys.argv[2]) > 2:

    device = sys.argv[1] if sys.argv[1].lower() != 'all' else None
    test = sys.argv[2]
    devtype = sys.argv[3]

devhealth.getSmartDeviceHealth(test,device,devtype)

