#!/usr/bin/python

# Example for output from agent
# ---------------------------------------------------------
# lm85-i2c-0-2e
# in0         +1.56 V  min   +0.00 V max   +3.32 V   
# Vcore       +1.34 V  min   +0.00 V max   +2.99 V   
# +3.3V       +3.33 V  min   +0.00 V max   +4.38 V   
# +5V         +5.10 V  min   +0.00 V max   +6.64 V   
# +12V       +11.94 V  min   +0.00 V max  +15.94 V   
# fan1       1472 RPM  min     0 RPM
# fan2          0 RPM  min     0 RPM
# fan3        968 RPM  min     0 RPM
# fan4       1272 RPM  min     0 RPM
# temp1       +64.0 C  low   128.0 C high  +127.0 C  
# MB Temp    +50.0 C  low   127.0 C high  +127.0 C  
# temp3       +42.0 C  low   127.0 C high  +127.0 C  
# cpu0vid   +1.088 V
# ---------------------------------------------------------

def inventory_lmsensors(checkname, info):
	# [['lm85i2c02e'], ['in0', '+1.56', 'V', 'min', '+0.00', 'V', 'max', '+3.32', 'V'], ['Vcore', '+1.35', 'V', 'min', '+0.00', 'V', 'max', '+2.99', 'V'], ['+3.3V', '+3.33', 'V', 'min', '+0.00', 'V', 'max', '+4.38', 'V'], ['+5V', '+5.10', 'V', 'min', '+0.00', 'V', 'max', '+6.64', 'V'], ['+12V', '+12.00', 'V', 'min', '+0.00', 'V', 'max', '+15.94', 'V'], ['fan1', '1376', 'RPM', 'min', '0', 'RPM'], ['fan2', '0', 'RPM', 'min', '0', 'RPM'], ['fan3', '970', 'RPM', 'min', '0', 'RPM'], ['fan4', '1276', 'RPM', 'min', '0', 'RPM'], ['temp1', '+59.0', 'C', 'low', '128.0', 'C', 'high', '+127.0', 'C'], ['MB', 'Temp', '+50.0', 'C', 'low', '127.0', 'C', 'high', '+127.0', 'C'], ['temp3', '+43.0', 'C', 'low', '127.0', 'C', 'high', '+127.0', 'C'], ['cpu0vid', '+1.088', 'V']]
	inventory = []
	for line in info:
		if len(line) < 3:
			continue # We don't use the chipname (yet) 'lm85i2c02e'
		sname = line[0] # 'in0', 'fan1', etc
		# scur  = line[1] # current value, '+1.56'
		stype = line[2] # 'V', 'RPM', 'C'

		smin = None
		smax = None
		# See if sensor has a supplied min/max value
		if len(line) > 5:
			if line[3] == 'min' or line[3] == 'low':
				smin = line[4]
			elif line[3] == 'max' or line[3] == 'high':
				smax = line[4]
		if len(line) > 8:
			if line[6] == 'min' or line[6] == 'low':
				smin = line[7]
			elif line[6] == 'max' or line[6] == 'high':
				smax = line[7]

		if stype == 'V' and checkname == 'lmsensors.volt':
			inventory.append( (sname, "", (smin, smax)) )
		elif stype == 'C' and checkname == 'lmsensors.temp':
			inventory.append( (sname, "", (smin, smax)) )
		elif stype == 'RPM' and checkname == 'lmsensors.fan':
			inventory.append( (sname, "", (smin, smax)) )
	return inventory

def check_lmsensors(item, params, info):
	smin, smax = params
	values = None
	for line in info:
		if len(line) > 1 and line[0] == item:
			values = line
			break
	if values == None:
		return (3, "UNKNOWN - sensor status not found in agent output")
	value = float(values[1])
	stype = values[2]
	perfdata = [ ( item, value, "", savefloat(smax) ) ]
	if smax != None and value > float(smax):
		return (2, "CRITICAL - Sensor value %s %s, which is bigger than %s" % (value, stype, smax), perfdata)
	elif smin != None and value < float(smin):
		return (2, "CRITICAL - Sensor value %s %s, which is smaller than %s" % (value, stype, smin), perfdata)
	# TODO: warnings
	else:
		return (0, "OK - Sensor value is %s %s" % (value, stype), perfdata)

check_info['lmsensors.fan'] = (check_lmsensors, "Sensor %s", 1, inventory_lmsensors)
check_info['lmsensors.temp'] = (check_lmsensors, "Sensor %s", 1, inventory_lmsensors)
check_info['lmsensors.volt'] = (check_lmsensors, "Sensor %s", 1, inventory_lmsensors)
