#!/bin/sh
##############################################################################
#
#  Copyright (c) 2011, Doremi Labs, Inc.
#    All rights reserved.
#
#  Redistribution in source or binary forms, with or without
#  modification, are NOT permitted.
#
#    Doremi Labs, Inc.
#    1020 Chestnut St.
#    Burbank, CA 91506
#    Tel : (818) 562 1101
#    Fax : (818) 562 1109
#
# BVI: RAID status is now handled by harddiskstatus.sh
# HMO: fix to report correct status for linear and stripping RAID
# HMO: add .$$ identification in temporrary file to prevent race conflict
# HMO: change ipmi temporary filename to prevent race conflict
# HMO: add number of drives in RAID table
# HMO: process board information only once
# HMO: booting device properties added
#
##############################################################################

# for definition of motherboard_model()
. /etc/init.d/drmfunctions
. /doremi/etc/init.d/snmp.sh

DOREMI_PATH=/var/doremi
SBCSETUP=/doremi/sbin/sbcsetup.out
MBCTL=/doremi/sbin/mb_ctl.out
LDBCTL=/doremi/sbin/ldbctl.out

if [ "${MOTHERBOARD_MODEL_D945GTP}" != "1" ]; then
	MOTHERBOARD_MODEL_D945GTP=1
fi
if [ "${MOTHERBOARD_MODEL_SE7221x}" != "2" ]; then
	MOTHERBOARD_MODEL_SE7221x=2
fi
if [ "${MOTHERBOARD_MODEL_S3000AH}" != "3" ]; then
	MOTHERBOARD_MODEL_S3000AH=3
fi

adjust_fan_value()
{
	if test "$1" -gt 12000 2> /dev/null; then
		expr "$1" / 2
	else
		echo "$1"
	fi
}

sm_nand_alarm_set()
{
	local TYPE="$1"
	local SEVERITY="$2"
	local STATUS="$3"
	
	touch "${ALARM_PATH}/MD_SM_NAND_${TYPE}_not_mounted"
	log_alarm "MD_SM_NAND_${TYPE}_not_mounted alarm raised"
	# send a log
	${LDBCTL} -s SecurityModuleNANDstatus "Severity=${SEVERITY}" "Status=${STATUS}" | \
	${LDBCTL} -l
	return 0
}

sm_nand_alarm_clear()
{
	local TYPE="$1"
	
	log_alarm "MD_SM_NAND_${TYPE}_not_mounted alarm cleared"
	rm -f "${ALARM_PATH}/MD_SM_NAND_${TYPE}_not_mounted"
	return 0
}

check_channel_security_sm_blackout()
{
	local DOOR=$1
	local MARRIAGE=$2
	local PARTIAL_BLACKOUT=$3
	local ALARM
	
	# PARTIAL_BLACKOUT state is 0="no partial blackout" 1="partial blackout"
	if [ "${DOOR}" == "unknown" ] || [ "${MARRIAGE}" == "unknown" ] || [ "${PARTIAL_BLACKOUT}" == "unknown" ]; then
		ALARM="unknown"
	else
		if [ "${PARTIAL_BLACKOUT}" == "1" ]; then
			if [ "${DOOR}" == "0" ] || [ "${MARRIAGE}" == "0" ]; then
				ALARM="warning"
			else
				ALARM="normal"
			fi
		else
			if [ "${DOOR}" == "0" ] || [ "${MARRIAGE}" == "0" ]; then
				ALARM="error"
			else
				ALARM="normal"
			fi
		fi
	fi
	
	update_alarm_state SM_MARRIAGE $ALARM
}

get_md_status()
{
	local NAME=$1
	local VAL=$(grep ${NAME} ${SNMP_PATH}/md_status.txt 2>/dev/null | awk '{print $2}')
	
	if [ ! -n "${VAL}" ]; then
		VAL="unknown"
	fi
	
	echo "${VAL}"
}

check_sm_status_alarms()
{
	if test -r ${SNMP_PATH}/md_status.txt; then
		local NAND_SMD_MOUNTED=$(grep NANDSMDMounted ${SNMP_PATH}/md_status.txt | awk '{print $2}')    
		if [ "$NAND_SMD_MOUNTED" == "0" ]; then
			if ! test -r "${ALARM_PATH}/MD_SM_NAND_SMD_not_mounted"; then
				sm_nand_alarm_set "SMD" "Warning" "smd-not-mounted"
			fi
		else
			if test -r "${ALARM_PATH}/MD_SM_NAND_SMD_not_mounted"; then
				sm_nand_alarm_clear "SMD"
			fi
		fi
		
		local NAND_VAR_MOUNTED=$(grep NANDVARMounted ${SNMP_PATH}/md_status.txt | awk '{print $2}')
		if [ "$NAND_VAR_MOUNTED" == "0" ]; then
			if ! test -r "${ALARM_PATH}/MD_SM_NAND_VAR_not_mounted"; then
				local board_type=$(cat ${SNMP_PATH}/md_product.txt)
				case "$board_type" in 
					imb)
						sm_nand_alarm_set "VAR" "Error" "var-not-mounted"
						;;
					
					*)
						sm_nand_alarm_set "VAR" "Warning" "var-not-mounted"
						;;
				esac
			fi
		else
			if test -r "${ALARM_PATH}/MD_SM_NAND_VAR_not_mounted"; then
				sm_nand_alarm_clear "VAR"
			fi
		fi
		
		local DOOR_STATE
		local MARRIAGE_STATE
		local BLACKOUT_STATE
		if DOOR_STATE=$(get_md_status ServiceDoorArmed) && MARRIAGE_STATE=$(get_md_status ActiveMarriage) && BLACKOUT_STATE=$(get_md_status PartialBlackout); then
			check_channel_security_sm_blackout ${DOOR_STATE} ${MARRIAGE_STATE} ${BLACKOUT_STATE}
		fi
	fi
}

#
# 945GTP motherboard sensors
#
sensors_motherboard_model_D945()
{
	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	#
	# Temperatures
	#
	save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
	save_integer_value	TEMP_MOTHERBOARD_value	$(cat /tmp/snmp_sensors | grep Board: | cut -d ' ' -f 2 | tr -d [+C])
	save_string_value	TEMP_CPU_title				"CPU"
	save_integer_value	TEMP_CPU_value			$(cat /tmp/snmp_sensors | grep CPU: | cut -d ' ' -f 2 | tr -d [+C])
	save_string_value	TEMP_CHIPSET_title			"Chipset"
	save_integer_value	TEMP_CHIPSET_value		$(cat /tmp/snmp_sensors | grep BoaRemote: | cut -d ' ' -f 2 | tr -d [+C])

	#
	# Fan speeds
	#	
	save_string_value	FAN_MOTHERBOARD_title		"Motherboard Fan"
	save_integer_value	FAN_MOTHERBOARD_value		"`cat /tmp/snmp_sensors | grep fan4: | cut -d ' ' -f 2`"
	save_string_value	FAN_CPU_title			"CPU Fan"
	save_integer_value	FAN_CPU_value			"`cat /tmp/snmp_sensors | grep CPU_Fan: | cut -d ' ' -f 2`"
	save_string_value	FAN_CHIPSET_title		"Chipset Fan"
	save_integer_value	FAN_CHIPSET_value		"`cat /tmp/snmp_sensors | grep fan3: | cut -d ' ' -f 2`"

	#
	# Voltages
	#
	save_string_value	VOLT_1_title			"Voltage 1"
	save_to_mV_string_value	VOLT_1_value			"`cat /tmp/snmp_sensors | grep VCore: | cut -d ' ' -f 2`"
	save_string_value	VOLT_2_title			"Voltage 2"
	save_to_mV_string_value	VOLT_2_value			"0"
	save_string_value	VOLT_3_title			"+3.3V"
	save_to_mV_string_value	VOLT_3_value			"`cat /tmp/snmp_sensors | grep V3.3: | cut -d ' ' -f 2`"
	save_string_value	VOLT_4_title			"+5V"
	save_to_mV_string_value	VOLT_4_value			"`cat /tmp/snmp_sensors | grep V5: | cut -d ' ' -f 2`"
	save_string_value	VOLT_5_title			"+12V"
	save_to_mV_string_value	VOLT_5_value			"`cat /tmp/snmp_sensors | grep V12: | cut -d ' ' -f 2`"
	save_string_value	VOLT_6_title			"-5V"
	save_to_mV_string_value	VOLT_6_value			"-5"
	save_string_value	VOLT_7_title			"-12V"
	save_to_mV_string_value	VOLT_7_value			"-12"
}

#
# SE7221 motherboard sensors
#
sensors_motherboard_model_SE7221()
{
	# Get System Fan #1 and #2 from PC87427 driver (more reliable than IPMI)
	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	save_string_value	FAN_MOTHERBOARD_title		"Serverboard Fan1"
	save_integer_value	FAN_MOTHERBOARD_value		"`cat /tmp/snmp_sensors | grep fan1 | cut -d ' ' -f 2 | cut -d. -f1`"
	save_string_value	FAN_CHIPSET_title		"Serverboard Fan2"
	save_integer_value	FAN_CHIPSET_value		"`cat /tmp/snmp_sensors | grep fan2 | cut -d ' ' -f 2 | cut -d. -f1`"
}

#
# S3000 motherboard sensors
#
sensors_motherboard_model_S3000()
{
	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	#
	# Temperatures
	# 
	save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
	save_integer_value	TEMP_MOTHERBOARD_value	$(cat /tmp/snmp_sensors | grep temp2: | cut -d ' ' -f 2 | tr -d [+C])
	save_string_value	TEMP_CPU_title				"CPU"
	save_integer_value	TEMP_CPU_value			$(cat /tmp/snmp_sensors | grep temp1: | cut -d ' ' -f 2 | tr -d [+C])
	save_string_value	TEMP_CHIPSET_title			"Chipset"
	save_integer_value	TEMP_CHIPSET_value		$(cat /tmp/snmp_sensors | grep temp3: | cut -d ' ' -f 2 | tr -d [+C])
	
	#
	# Fan speeds
	# 
	save_string_value	FAN_MOTHERBOARD_title		"Motherboard Fan"
	save_integer_value	FAN_MOTHERBOARD_value		"-1"
	save_string_value	FAN_CPU_title			"CPU Fan"
	save_integer_value	FAN_CPU_value			"-1"
	save_string_value	FAN_CHIPSET_title		"Chipset Fan"
	save_integer_value	FAN_CHIPSET_value		"-1"
	
	#
	# Voltages
	# 
	save_string_value	VOLT_1_title			"Voltage 1"
	save_to_mV_string_value	VOLT_1_value			"`cat /tmp/snmp_sensors | grep in0: | cut -d ' ' -f 2`"
	save_string_value	VOLT_2_title			"Voltage 2"
	save_to_mV_string_value	VOLT_2_value			"`cat /tmp/snmp_sensors | grep in1: | cut -d ' ' -f 2`"
	save_string_value	VOLT_3_title			"+3.3V"
	save_to_mV_string_value	VOLT_3_value			"`cat /tmp/snmp_sensors | grep in2: | cut -d ' ' -f 2`"
	save_string_value	VOLT_4_title			"+5V"
	save_to_mV_string_value	VOLT_4_value			"`cat /tmp/snmp_sensors | grep in3: | cut -d ' ' -f 2`"
	save_string_value	VOLT_5_title			"+12V"
	save_to_mV_string_value	VOLT_5_value			"`cat /tmp/snmp_sensors | grep in4: | cut -d ' ' -f 2`"
	save_string_value	VOLT_6_title			"-5V"
	save_to_mV_string_value	VOLT_6_value			"-5"
	save_string_value	VOLT_7_title			"-12V"
	save_to_mV_string_value	VOLT_7_value			"-12"
}

#
# 83627hf sensors
#
sensors_83627hf()
{
	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	#
	# Temperatures
	#
	save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
	save_integer_value	TEMP_MOTHERBOARD_value	$(cat /tmp/snmp_sensors | grep temp2: | cut -d ' ' -f 2 | tr -d [+\uffffC])
	save_string_value	TEMP_CPU_title				"CPU"
	save_integer_value	TEMP_CPU_value			$(cat /tmp/snmp_sensors | grep temp1: | cut -d ' ' -f 2 | tr -d [+\uffffC])
	save_string_value	TEMP_CHIPSET_title			"Chipset"
	save_integer_value	TEMP_CHIPSET_value		$(cat /tmp/snmp_sensors | grep temp3: | cut -d ' ' -f 2 | tr -d [+\uffffC])
	
	#
	# Fan speeds
	#
	save_string_value	FAN_MOTHERBOARD_title		"Motherboard Fan"
	save_integer_value	FAN_MOTHERBOARD_value		"-1"
	save_string_value	FAN_CPU_title			"CPU Fan"
	save_integer_value	FAN_CPU_value			"-1"
	save_string_value	FAN_CHIPSET_title		"Chipset Fan"
	save_integer_value	FAN_CHIPSET_value		"-1"
	
	#
	# Voltages
	#
	save_string_value	VOLT_1_title			"Voltage 1"
	save_to_mV_string_value	VOLT_1_value			"`cat /tmp/snmp_sensors | grep 'VCore 1:' | cut -d ' ' -f 2`"
	save_string_value	VOLT_2_title			"Voltage 2"
	save_to_mV_string_value	VOLT_2_value			"`cat /tmp/snmp_sensors | grep 'VCore 2:' | cut -d ' ' -f 2`"
	save_string_value	VOLT_3_title			"+3.3V"
	save_to_mV_string_value	VOLT_3_value			"`cat /tmp/snmp_sensors | grep V3.3: | cut -d ' ' -f 2`"
	save_string_value	VOLT_4_title			"+5V"
	save_to_mV_string_value	VOLT_4_value			"`cat /tmp/snmp_sensors | grep V5: | cut -d ' ' -f 2`"
	save_string_value	VOLT_5_title			"+12V"
	save_to_mV_string_value	VOLT_5_value			"`cat /tmp/snmp_sensors | grep V12: | cut -d ' ' -f 2`"
	save_string_value	VOLT_6_title			"-5V"
	save_to_mV_string_value	VOLT_6_value			"-5"
	save_string_value	VOLT_7_title			"-12V"
	save_to_mV_string_value	VOLT_7_value			"-12"
}

#
# 83627ehf sensors
#
sensors_83627ehf()
{
	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	#
	# Temperatures
	#
	save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
	save_integer_value	TEMP_MOTHERBOARD_value	$(cat /tmp/snmp_sensors | grep \"Sys Temp:\" | cut -d ' ' -f 3 | cut -d '.' -f 1 | tr -d [+\uffffC])
	save_string_value	TEMP_CPU_title				"CPU"
	save_integer_value	TEMP_CPU_value			$(cat /tmp/snmp_sensors | grep \"CPU Temp:\" | cut -d ' ' -f 3 | cut -d '.' -f 1 | tr -d [+\uffffC])
	save_string_value	TEMP_CHIPSET_title			"Chipset"
	save_integer_value	TEMP_CHIPSET_value		$(cat /tmp/snmp_sensors | grep \"AUX Temp:\" | cut -d ' ' -f 3 | cut -d '.' -f 1 | tr -d [+\uffffC])
	
	#
	# Fan speeds
	#
	save_string_value	FAN_MOTHERBOARD_title		"Motherboard Fan"
	save_integer_value	FAN_MOTHERBOARD_value		"-1"
	save_string_value	FAN_CPU_title			"CPU Fan"
	save_integer_value	FAN_CPU_value			"`cat /tmp/snmp_sensors | grep \"fan4:\" | cut -d ' ' -f 2`"
	save_string_value	FAN_CHIPSET_title		"Chipset Fan"
	save_integer_value	FAN_CHIPSET_value		"-1"

	save_string_value	FAN_4_title		"Fan1"
	save_integer_value	FAN_4_value		"`cat /tmp/snmp_sensors | grep \"Case Fan:\" | cut -d ' ' -f 3`"
	save_string_value	FAN_5_title		"Fan2"
	save_integer_value	FAN_5_value		"`cat /tmp/snmp_sensors | grep \"CPU Fan:\" | cut -d ' ' -f 3`"
	save_string_value	FAN_6_title		"Fan3"
	save_integer_value	FAN_6_value		"`cat /tmp/snmp_sensors | grep \"fan5:\" | cut -d ' ' -f 2`"
	
	#
	# Voltages
	#
	save_string_value	VOLT_1_title			"Voltage 1"
	save_to_mV_string_value	VOLT_1_value			"`cat /tmp/snmp_sensors | grep 'VCore:' | cut -d ' ' -f 2`"
	save_string_value	VOLT_2_title			"Voltage 2"
	save_to_mV_string_value	VOLT_2_value			"0"
	save_string_value	VOLT_3_title			"+3.3V"
	save_to_mV_string_value	VOLT_3_value			"`cat /tmp/snmp_sensors | grep 3VCC: | cut -d ' ' -f 2`"
	save_string_value	VOLT_4_title			"+5V"
	save_to_mV_string_value	VOLT_4_value			"`cat /tmp/snmp_sensors | grep in6: | cut -d ' ' -f 2`"
	save_string_value	VOLT_5_title			"+12V"
	save_to_mV_string_value	VOLT_5_value			"`cat /tmp/snmp_sensors | grep in1: | cut -d ' ' -f 2`"
	save_string_value	VOLT_6_title			"-5V"
	save_to_mV_string_value	VOLT_6_value			"-5"
	save_string_value	VOLT_7_title			"-12V"
	save_to_mV_string_value	VOLT_7_value			"-12"
}

#
# 83627ehf_x7spa sensors
#
sensors_83627ehf_x7spa()
{
	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	#
	# Temperatures
	#
	save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
	save_integer_value	TEMP_MOTHERBOARD_value	$(cat /tmp/snmp_sensors | grep \"Sys Temp:\" | cut -d ' ' -f 3 | cut -d '.' -f 1 | tr -d [+\uffffC])
	save_string_value	TEMP_CPU_title				"CPU"
	save_integer_value	TEMP_CPU_value			$(cat /tmp/snmp_sensors | grep \"CPU Temp:\" | cut -d ' ' -f 3 | cut -d '.' -f 1 | tr -d [+\uffffC])
	save_string_value	TEMP_CHIPSET_title			"Chipset"
	save_integer_value	TEMP_CHIPSET_value		$(cat /tmp/snmp_sensors | grep \"AUX Temp:\" | cut -d ' ' -f 3 | cut -d '.' -f 1 | tr -d [+\uffffC])
	
	#
	# Fan speeds
	#
	save_string_value	FAN_MOTHERBOARD_title		"Motherboard Fan"
	save_integer_value	FAN_MOTHERBOARD_value		"-1"
	save_string_value	FAN_CPU_title			"CPU Fan"
	save_integer_value	FAN_CPU_value			"-1"
	save_string_value	FAN_CHIPSET_title		"Chipset Fan"
	save_integer_value	FAN_CHIPSET_value		"-1"
	
	#
	# Voltages
	#
	save_string_value	VOLT_1_title			"Voltage 1"
	save_to_mV_string_value	VOLT_1_value			"`cat /tmp/snmp_sensors | grep 'VCore:' | cut -d ' ' -f 2`"
	save_string_value	VOLT_2_title			"Voltage 2"
	save_to_mV_string_value	VOLT_2_value			"0"
	save_string_value	VOLT_3_title			"+3.3V"
	save_to_mV_string_value	VOLT_3_value			"`cat /tmp/snmp_sensors | grep 3VCC: | cut -d ' ' -f 2`"
	save_string_value	VOLT_4_title			"+5V"
	VOLTAGE5=`cat /tmp/snmp_sensors | grep in6: | cut -d ' ' -f 2 | tr -d '+' | tr -d '-'`
	STR_VOLTAGE5=`echo "scale=4; $VOLTAGE5*2" | bc`
	save_to_mV_string_value	VOLT_4_value			"`echo +$STR_VOLTAGE5`"
	save_string_value	VOLT_5_title			"+12V"
	VOLTAGE12=`cat /tmp/snmp_sensors | grep in1: | cut -d ' ' -f 2 | tr -d '+' | tr -d '-'`
	STR_VOLTAGE12=`echo "scale=4; $VOLTAGE12*2" | bc`
	save_to_mV_string_value	VOLT_5_value			"`echo +$STR_VOLTAGE12`"
	save_string_value	VOLT_6_title			"-5V"
	save_to_mV_string_value	VOLT_6_value			"-5"
	save_string_value	VOLT_7_title			"-12V"
	save_to_mV_string_value	VOLT_7_value			"-12"
}

#
# sensors_fusion
#
sensors_fusion()
{
	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	#
	# Temperatures
	#
	save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
	save_integer_value	TEMP_MOTHERBOARD_value	$(cat /tmp/snmp_sensors | grep -A3 'max1619' | grep 'temp1:' | cut -d ' ' -f 2 | cut -d. -f1 | tr -d [+\uffffC])
	save_string_value	TEMP_CPU_title				"CPU"
	save_integer_value	TEMP_CPU_value			$(cat /tmp/snmp_sensors | grep -A3 'max1619' | grep 'temp2:' | cut -d ' ' -f 2 | cut -d. -f1 | tr -d [+\uffffC])
	save_string_value	TEMP_CHIPSET_title			"Chipset"
	save_integer_value	TEMP_CHIPSET_value		0
}

sensors_83793()
{
	local CPUTEMP
	local MTBTEMP
	local NCPUT
	local FANVAL

	sensors | tr -s [:space:] > /tmp/snmp_sensors.$$
	mv /tmp/snmp_sensors.$$ /tmp/snmp_sensors

	#
	# Temperatures
	#
	CPUTEMP="`cat /tmp/snmp_sensors | grep 'CPU1 Temp:' | cut -d ' ' -f 3 | cut -d. -f1 | tr -d [+\uffffC]`"
	MTBTEMP="`cat /tmp/snmp_sensors | grep temp5: | cut -d ' ' -f 2 | cut -d. -f1 | tr -d [+\uffffC]`"
	NCPUT="`echo ${CPUTEMP} ${MTBTEMP} | awk '{ if ($1>0 && $2>0 && $1<($2+2)) { print $2+2 } else { print $1} }'`"
	save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
	save_integer_value	TEMP_MOTHERBOARD_value	${MTBTEMP}
	save_string_value	TEMP_CPU_title				"CPU"
	save_integer_value	TEMP_CPU_value			${NCPUT}
	save_string_value	TEMP_CHIPSET_title			"Chipset"
	save_integer_value	TEMP_CHIPSET_value		0
	
	#
	# Fan speeds
	#
	# Motherboard fan5 -> fan7
	# Motherboard fan1 -> fan2
	# Motherboard fan2 -> fan1
	# Motherboard fan3 -> fan3
	# Motherboard fan4 -> 
	FANVAL="`cat /tmp/snmp_sensors | grep fan2: | cut -d ' ' -f 2`"
	save_string_value	FAN_MOTHERBOARD_title		"Fan1"
	save_integer_value	FAN_MOTHERBOARD_value		"`adjust_fan_value ${FANVAL}`" 
	FANVAL="`cat /tmp/snmp_sensors | grep fan7: | cut -d ' ' -f 2`"
	save_string_value	FAN_CPU_title			"CPU Fan"
	save_integer_value	FAN_CPU_value			"`adjust_fan_value ${FANVAL}`" 
	FANVAL="`cat /tmp/snmp_sensors | grep fan1: | cut -d ' ' -f 2`"
	save_string_value	FAN_CHIPSET_title		"Fan2"
	save_integer_value	FAN_CHIPSET_value		"`adjust_fan_value ${FANVAL}`" 

	FANVAL="`cat /tmp/snmp_sensors | grep fan3: | cut -d ' ' -f 2`"
	save_string_value	FAN_4_title			"Fan3"
	save_integer_value	FAN_4_value			"`adjust_fan_value ${FANVAL}`" 
	FANVAL="`cat /tmp/snmp_sensors | grep fan4: | cut -d ' ' -f 2`"
	save_string_value	FAN_5_title			"Fan4"
	save_integer_value	FAN_5_value			"`adjust_fan_value ${FANVAL}`" 
	FANVAL="`cat /tmp/snmp_sensors | grep fan5: | cut -d ' ' -f 2`"
	save_string_value	FAN_6_title			"Fan5"
	save_integer_value	FAN_6_value			"`adjust_fan_value ${FANVAL}`" 
	
	#
	# Voltages
	#
	save_string_value	VOLT_1_title			"Voltage 1"
	save_to_mV_string_value	VOLT_1_value			"`cat /tmp/snmp_sensors | grep 'VCoreA:' | cut -d ' ' -f 2`"
	save_string_value	VOLT_2_title			"Voltage 2"
	save_to_mV_string_value	VOLT_2_value			"`cat /tmp/snmp_sensors | grep 'VCoreB:' | cut -d ' ' -f 2`"
	save_string_value	VOLT_3_title			"+3.3V"
	save_to_mV_string_value	VOLT_3_value			"`cat /tmp/snmp_sensors | grep 3.3V: | cut -d ' ' -f 2`"
	save_string_value	VOLT_4_title			"+5V"
	save_to_mV_string_value	VOLT_4_value			"`cat /tmp/snmp_sensors | grep 5V: | cut -d ' ' -f 2`"
	save_string_value	VOLT_5_title			"+12V"
	save_to_mV_string_value	VOLT_5_value			"`cat /tmp/snmp_sensors | grep 12V: | cut -d ' ' -f 2`"
	save_string_value	VOLT_6_title			"-5V"
	save_to_mV_string_value	VOLT_6_value			"-5"
	save_string_value	VOLT_7_title			"-12V"
	save_to_mV_string_value	VOLT_7_value			"-12"
}

sense_md_raid()
{
	local devname="$1"
	local devfile="/dev/$1"
	local tmpfile="/tmp/mdadm_detail_$1.$$.tmp"
	local -i active_devices=0
	local -i raid_size=0
	
	#
	# state offline
	#
	if ! test -b "${devfile}"; then
		# inactive
		rm -f "/tmp/snmp/RAID_${devname}_*"
		save_integer_value "RAID_${devname}_state" 2
		return 0
	fi
	if ! test -r "/proc/mdstat"; then 
		# inactive
		rm -f "/tmp/snmp/RAID_${devname}_*"
		save_integer_value "RAID_${devname}_state" 2
		return 0
	fi
	if ! mdadm --detail ${devfile} > "${tmpfile}"; then
		# inactive
		rm -f "${tmpfile}"
		rm -f "/tmp/snmp/RAID_${devname}_*"
		save_integer_value "RAID_${devname}_state" 2
		return 0
	fi
	#
	# state fail online
	#
	active_devices=$(grep 'Active Devices :' "${tmpfile}" | cut -f2 -d':' | sed -e 's/^[ \t]*//')
	raid_level=$(grep 'Raid Level :' "${tmpfile}" | cut -f2 -d':' | sed -e 's/^[ \t]*//')
	raid_size=$(grep 'Raid Devices :' "${tmpfile}" | cut -f2 -d':' | sed -e 's/^[ \t]*//')
	
	grep 'Spare Devices :' "${tmpfile}" | cut -f2 -d':' | save_integer_value "RAID_${devname}_spares" -
	grep 'UUID :' "${tmpfile}" | cut -f2- -d':' | save_string_value "RAID_${devname}_uuid" -
	save_string_value "RAID_${devname}_level" ${raid_level}
	save_integer_value "RAID_${devname}_size" ${raid_size}
	if ! grep -q "^${devname} : active " "/proc/mdstat"; then
		rm -f "${tmpfile}"
		rm -f "/tmp/snmp/RAID_${devname}_active"
		save_integer_value "RAID_${devname}_state" 3
		return 0
	fi
	#
	# working state
	#
	#
	# raid5 special trick to detect un-working online RAID
	#
	if test "${raid_level}" = raid5; then
		if ! grep 'State :' "${tmpfile}" | cut -f2 -d':' | grep -q -e active -e clean; then
			rm -f "${tmpfile}"
			rm -f "/tmp/snmp/RAID_${devname}_active"
			save_integer_value "RAID_${devname}_state" 3
			return 0
		fi
		# check for nr of failed devices
		# if Active_Devices < Raid_Devices -1; then fault
		if test $((raid_size - active_devices)) -gt 1 ; then
			# inactive
			rm -f "${tmpfile}"
			rm -f "/tmp/snmp/RAID_${devname}_active"
			save_integer_value "RAID_${devname}_state" 3
			return 0
		fi
	elif test "${raid_level}" = linear; then
		if ! grep 'State :' "${tmpfile}" | cut -f2 -d':' | grep -q -e clean -e active; then
			rm -f "${tmpfile}"
			rm -f "/tmp/snmp/RAID_${devname}_active"
			save_integer_value "RAID_${devname}_state" 3
			return 0
		fi  
	fi
	# we're in active state
	save_integer_value "RAID_${devname}_active" 1
	# recovery
	if grep 'State :' "${tmpfile}" | cut -f2 -d':' | grep -q -e recovering -e resyncing; then
		if grep -q 'Rebuild Status :' "${tmpfile}"; then
			grep 'Rebuild Status :' "${tmpfile}" | cut -f2 -d':' | cut -f1 -d'%' | save_integer_value "RAID_${devname}_recovery"
		else
			save_integer_value "RAID_${devname}_recovery" 0
		fi
	fi
	# final state
	if grep 'State :' "${tmpfile}" | cut -f2 -d':' | grep -q recovering; then
		save_integer_value "RAID_${devname}_state" 5
		#  elif grep 'State :' "${tmpfile}" | cut -f2 -d':' | grep -q resyncing; then
		#    save_integer_value "RAID_${devname}_state" 7
	elif grep 'State :' "${tmpfile}" | cut -f2 -d':' | grep -q degraded; then
		save_integer_value "RAID_${devname}_state" 4
	else
		save_integer_value "RAID_${devname}_state" 6
	fi
	# end
	rm -f "${tmpfile}"
}

mkdir -p "$SNMP_PATH"


set_string_value "$SNMP_PATH/$DOREMI_OID.1.3.8.0" "`date`"

#
# Display KDM in use and its expiration time
# (Disabled: List all assets)
#
/doremi/bin/drmsnmp_cpl.out -c

#
# Hardware Sensors
# 
motherboard_model
case "$?" in
	$MOTHERBOARD_MODEL_D945GTP)
		sensors_motherboard_model_D945
		;;
	$MOTHERBOARD_MODEL_SE7221x)
		sensors_motherboard_model_SE7221
		;;
	$MOTHERBOARD_MODEL_S3000AH)
		sensors_motherboard_model_S3000
		;;
	$MOTHERBOARD_MODEL_S3210)
		sensors_motherboard_model_SE7221
		;;
	$MOTHERBOARD_MODEL_PDSM4)
		sensors_83793
		;;
	$MOTHERBOARD_MODEL_X7SB4)
		sensors_83793
		;;
	$MOTHERBOARD_MODEL_X7SLA)
		sensors_83627ehf
		;;
	$MOTHERBOARD_MODEL_X7SPA)
		sensors_83627ehf_x7spa
		;;
	$MOTHERBOARD_MODEL_FUSION)
		sensors_fusion
		;;
	*)
		# try to use IPMI for unknown boards
		sensors_motherboard_model_SE7221
		;;
esac

#
# ECC errors counts
#
if [ -f /proc/ecc ] ; then
	ecc=`cat /proc/ecc`
	echo $ecc | cut -d ' ' -f 1 > $SNMP_PATH/DIMM_status
	echo $ecc | cut -d ' ' -f 2 > $SNMP_PATH/DIMM_corrected_errors
	echo $ecc | cut -d ' ' -f 3 > $SNMP_PATH/DIMM_uncorrected_errors
	if [ "$(cat $SNMP_PATH/DIMM_status)" != "ok" ]; then
		if [ ! -e $ALARM_PATH/DIMM_error ]; then
			log_alarm "DIMM_error alarm raised"
		fi
		touch $ALARM_PATH/DIMM_error
	else
		if [ -e $ALARM_PATH/DIMM_error ]; then
			log_alarm "DIMM_error alarm cleared"
			rm -f $ALARM_PATH/DIMM_error;
		fi
	fi
fi

if test -x /doremi/sbin/harddiskstatus.sh; then
  /doremi/sbin/harddiskstatus.sh sense-su
fi

#
# RAID devices
#
# BV: this is now done in harddiskinfo.sh
sense_raid() {
	devices=`cat /etc/mdadm/mdadm.conf | grep ARRAY | tr -s [:space:] | cut -d ' ' -f 2`
	for device in $devices ; do
		devname=`echo -n $device | cut -c 6-`
		devnum=`echo -n $device  | tr -d /dev/md`
		devnum=`expr $devnum + 1`
		# Type
		raidsec=0
		if grep -qs 'active linear' /proc/mdstat; then
			set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.2.$devnum" "0"
		elif grep -qs 'active raid0' /proc/mdstat; then
			set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.2.$devnum" "0"
		else
			set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.2.$devnum" "5"
			raidsec=1
		fi
		# Active
		if [ -b "$device" ] ; then
			#
			# Double-Check: the device $device may exist but it is not an 
			#  active RAID device
			#
			if [ "${raidsec}" = "1" ]; then
				value=`mdadm --monitor -1 "$device" | grep -i DeviceDisappeared`
				if [ "$value" = "" ] ; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.3.$devnum" "1"
					#echo "1"      > $SNMP_PATH/$DOREMI_OID.1.3.9.1.2.$devnum
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.3.$devnum" "0"
					#echo "0"      > $SNMP_PATH/$DOREMI_OID.1.3.9.1.2.$devnum
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.3.$devnum" "1"
			fi
		else
			set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.3.$devnum" "0"
			#echo "0"      > $SNMP_PATH/$DOREMI_OID.1.3.9.1.2.$devnum
		fi
	# HMO FIX: start
	# 
	# this seems likely overkill; but it is to limit the risk of wrong detection
	# using validated code as much as possible. This stuff is time consuming to test.
		sense_md_raid ${devname}
		if ! test -e /tmp/snmp/RAID_${devname}_active; then
			set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.3.$devnum" "0"        
		fi
	# HMO FIX: end
		# Degraded
		if [ "${raidsec}" = "1" ]; then
			value=`mdadm --monitor -1 "$device" | grep -i DegradedArray`
			if [ "$value" = "" ] ; then
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.4.$devnum" "0"
				#echo "0"      > $SNMP_PATH/$DOREMI_OID.1.3.9.1.3.$devnum
			else
				#echo "1"      > $SNMP_PATH/$DOREMI_OID.1.3.9.1.3.$devnum
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.4.$devnum" "1"
			fi
			# Recovery
			value=`mdadm --detail "$device" | grep -i Rebuild | cut -d ' ' -f 5 | tr -d %`
			if [ "$value" = "" ] ; then
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.5.$devnum" "100"
				#echo "100"   > $SNMP_PATH/$DOREMI_OID.1.3.9.1.4.$devnum
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.5.$devnum" "$value"
				#echo "$value" > $SNMP_PATH/$DOREMI_OID.1.3.9.1.4.$devnum
			fi
		else
			set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.4.$devnum" "0"
			set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.5.$devnum" "100"        
		fi
		# Size (number of drives)
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.9.1.6.$devnum" "`grep ${devname} /proc/mdstat | tr -c -d '[' | wc -c`"
		# Resync aborted
		if [ -e /sys/block/${devname}/md/sync_aborted ]; then
			_snmp="/tmp/snmp_${devname}_sync_aborted"		
			if [ ! -e ${_snmp}  ]; then
				echo 0 > ${_snmp}
			fi
			value=`cat ${_snmp}`
			_curr=`cat /sys/block/${devname}/md/sync_aborted`
			if [ "${_curr}" != "${value}" ] && [ "${_curr}" != "0" ]; then
				_mount=`cat /proc/mounts | grep ${device} | awk '{print $2}'`
				if [ "${_mount}" != "" ]; then
					_mount="LogicalName=${_mount}"
				fi
				${LDBCTL} -s RAIDMaintenance AuthId=root Operation=rebuild OSDeviceName=${device} ${_mount} Exception=RebuildAborted |
				${LDBCTL} -l
				echo "Rebuild aborted!"
			fi
			echo ${_curr} > ${_snmp}
		fi
	done
}

#
# Dolphin
#
if [ -r /proc/dolphin0 ]; then
	cat /proc/dolphin0 > /tmp/snmp_dolphin
elif [ -r /proc/dolphin ]; then
	cat /proc/dolphin > /tmp/snmp_dolphin
fi 

if test -r /tmp/snmp_dolphin; then
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.4.1.1.2.1" "`cat /tmp/snmp_dolphin | grep -i 'decoder errors'  | tr -s [:space:] | cut -d ' ' -f 3`"
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.4.1.1.3.1" "`cat /tmp/snmp_dolphin | grep -i 'frame underflow' | tr -s [:space:] | cut -d ' ' -f 3`"
else
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.4.1.1.2.1" "0"
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.4.1.1.3.1" "0"
fi

#
# SecurityManager
#
if [ -x $SBCSETUP ] && board_tls_support; then
	if ! $SBCSETUP -a - 2>/dev/null; then
		update_alarm_state "SM_NETWORK" "error"
	else
		update_alarm_state "SM_NETWORK" "normal"
		$SBCSETUP -a --get-temperature > /tmp/temperature.$$
		mv /tmp/temperature.$$ /tmp/temperature
		cat /tmp/temperature | grep Clock | cut -d ' ' -f 3 | cut -d '.' -f 1 > $SNMP_PATH/TEMP_MD_RTC_value
		#check Tamper chipset temperature OID before uncommenting
		cat /tmp/temperature | grep "Chipset 1" > /dev/null 2>&1
		if [ $? == 0 ]; then
			cat /tmp/temperature | grep 'Chipset 1' | cut -d ' ' -f 4 > $SNMP_PATH/TEMP_MD_FPGA1_value
		fi
		cat /tmp/temperature | grep "Chipset 2" > /dev/null 2>&1
		if [ $? == 0 ]; then
			cat /tmp/temperature | grep 'Chipset 2' | cut -d ' ' -f 4 > $SNMP_PATH/TEMP_MD_FPGA2_value
		fi
		set_string_value "$SNMP_PATH/$DOREMI_OID.1.6.10.1.2.0" "`$SBCSETUP -a --get-version | grep ProductName | cut -d ' ' -f 4`"
	fi
fi
# write bootup default value
test -e $SNMP_PATH/TEMP_MD_RTC_title || echo "MD RTC" > $SNMP_PATH/TEMP_MD_RTC_title
test -e $SNMP_PATH/TEMP_MD_FPGA1_title || echo "MD FPGA #1" > $SNMP_PATH/TEMP_MD_FPGA1_title
test -e $SNMP_PATH/TEMP_MD_FPGA2_title || echo "MD FPGA #2" > $SNMP_PATH/TEMP_MD_FPGA2_title
test -r "$SNMP_PATH/$DOREMI_OID.1.6.10.1.2.0" || set_string_value "$SNMP_PATH/$DOREMI_OID.1.6.10.1.2.0" ""

if board_tls_support; then
	res=$($SBCSETUP -a --get-single-version) && set_string_value "$SNMP_PATH/$DOREMI_OID.1.6.10.1.1.0" "$res"
elif [ -e "/proc/dolphin0" ]; then
	res=$($SBCSETUP --get-pci-version) && set_string_value "$SNMP_PATH/$DOREMI_OID.1.6.10.1.1.0" "$res"
fi
test -r "$SNMP_PATH/$DOREMI_OID.1.6.10.1.1.0" || set_string_value "$SNMP_PATH/$DOREMI_OID.1.6.10.1.1.0" ""

### service door / marriage related OIDs ###
refreshed=0
# defaults
test -e "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" || set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" 1
test -e "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" || set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" 1
test -e "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" || set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" 1
test -e "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" || set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" 1
test -e "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" || set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" 1
test -e "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" || set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" 1
test -e "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" || set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" 1
if [ -e /proc/dolphin0 ]; then
	if board_full_cap_support; then
		$SBCSETUP -a --get-status > /tmp/snmp/md_status.txt
	fi
	if board_type "clarity" || board_type "certainty" || board_type "fusion"; then
		if [ -e /tmp/snmp/md_status.txt ]; then
			partial_blackout="`cat /tmp/snmp/md_status.txt | grep 'PartialBlackout ' | awk '{print $2}'`"
			if [ $? -eq 0 ]; then
				if [ "$partial_blackout" == "1" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" 3
				elif [ "$partial_blackout" == "0" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" 4
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" 2
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" 2
			fi
	
			physical_marriage="`cat /tmp/snmp/md_status.txt | grep 'PhysicalMarriage ' | awk '{print $2}'`"
			if [ $? -eq 0 ]; then
				if [ "$physical_marriage" == "1" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" 3
				elif [ "$physical_marriage" == "0" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" 4
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" 2
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" 2
			fi
				
			logical_marriage="`cat /tmp/snmp/md_status.txt | grep 'LogicalMarriage ' | awk '{print $2}'`"
			if [ $? -eq 0 ]; then
				if [ "$logical_marriage" == "1" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" 3
				elif [ "$logical_marriage" == "0" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" 4
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" 2
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" 2
			fi
	
			active_marriage="`cat /tmp/snmp/md_status.txt | grep 'ActiveMarriage ' | awk '{print $2}'`"
			if [ $? -eq 0 ]; then
				if [ "$active_marriage" == "1" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" 3
				elif [ "$active_marriage" == "0" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" 4
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" 2
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" 2
			fi
	
			val="`cat /tmp/snmp/md_status.txt | grep 'ServiceDoorOpened ' | awk '{print $2}'`"
			if [ $? -eq 0 ]; then
				if [ "$val" == "1" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" 3
				elif [ "$val" == "0" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" 4
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" 2
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" 2
			fi
	
			service_door="`cat /tmp/snmp/md_status.txt | grep 'ServiceDoorArmed ' | awk '{print $2}'`"
			if [ $? -eq 0 ]; then
				if [ "$service_door" == "1" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" 3
				elif [ "$service_door" == "0" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" 4
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" 2
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" 2
			fi
			
			security_lock="`cat /tmp/snmp/md_status.txt | grep 'SecurityLock ' | awk '{print $2}'`"
			if [ $? -eq 0 ]; then
				if [ "$security_lock" == "1" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" 3
				elif [ "$security_lock" == "0" ]; then
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" 4
				else
					set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" 2
				fi
			else
				set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" 2
			fi
			
			if [ "$service_door" == "1" ]; then
				service=3 # normal
			elif [ "$service_door" == "0" ]; then
				if [ "$partial_blackout" == "1" ]; then
					service=4 # warning
				else
					service=5 # error
				fi
			else
				service=1 # undefined
			fi
			echo -n $service > "$SNMP_PATH/md_security_service_door.txt"
			
			if [ "$active_marriage" == "1" ]; then
				marriage=3 # normal
			elif [ "$physical_marriage" == "1" ] && [ "$logical_marriage" == "1" ]; then
				marriage=4 # warning
			elif [ "$physical_marriage" == "0" ] || [ "$logical_marriage" == "0" ]; then
				if [ "$partial_blackout" == "1" ]; then
					marriage=4 # warning
				else
					marriage=5 # error
				fi
			else
				marriage=1 # undefined
			fi
			echo -n $marriage > "$SNMP_PATH/md_security_marriage.txt"
			
			if [ "$service" == "5" ] || [ "$marriage" == "5" ]; then
				clearance=5 # error
			else
				if [ "$security_lock" == "1" ]; then
					clearance=4 # warning
				elif [ "$service" == "4" ] || [ "$marriage" == "4" ]; then
					clearance=4 # warning
				elif [ "$security_lock" == "0" ]; then
					clearance=3 # normal
				else
					clearance=1 # undefined
				fi
			fi
			echo -n $clearance > "$SNMP_PATH/md_security_clearance.txt"
			
			grep BoardRevision /tmp/snmp/md_status.txt | awk '{print $2}' > /tmp/snmp/md_revision.txt
                        
			refreshed=1
		fi
		$MBCTL status > /tmp/snmp/md_ctl_status.txt
		if [ -e /tmp/snmp/md_ctl_status.txt ]; then
			echo -n "MD Tamper Vcci" > /tmp/snmp/VOLT_MD_VCCI_title
			if val=$(grep "TamperVcci" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/VOLT_MD_VCCI_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_VCCI_value
			fi
			#
			echo -n "MD Tamper Vbat" > /tmp/snmp/VOLT_MD_VBAT_title
			if val=$(grep "TamperVbat" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/VOLT_MD_VBAT_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_VBAT_value
			fi
			#
			echo -n "MD Tamper Vref" > /tmp/snmp/VOLT_MD_VREF_title
			if val=$(grep "TamperVref" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/VOLT_MD_VREF_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_VREF_value
			fi
			#
			echo -n "MD Tamper AD1" > /tmp/snmp/VOLT_MD_AD1_title
			if val=$(grep "TamperAD1Voltage" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/VOLT_MD_AD1_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_AD1_value
			fi
			#
			echo -n "MD Tamper AD2" > /tmp/snmp/VOLT_MD_AD2_title
			if val=$(grep "TamperAD2Voltage" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/VOLT_MD_AD2_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_AD2_value
			fi
			#
			echo -n "MD Tamper AD3" > /tmp/snmp/VOLT_MD_AD3_title
			if val=$(grep "TamperAD3Voltage" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/VOLT_MD_AD3_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_AD3_value
			fi
			#
			echo -n "MD Tamper AD4" > /tmp/snmp/VOLT_MD_AD4_title
			if val=$(grep "TamperAD4Voltage" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/VOLT_MD_AD4_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_AD4_value
			fi
			#
			echo -n "MD Tamper Temperature" > /tmp/snmp/TEMP_MD_TAMPER_title
			if val=$(grep "TamperTemperature" /tmp/snmp/md_ctl_status.txt); then
				val=$(echo -n $val | cut -d : -f 2 | cut -d ' ' -f 2)
				echo -n $val > /tmp/snmp/TEMP_MD_TAMPER_value
			else
				echo -n "" > /tmp/snmp/VOLT_MD_TAMPER_value
			fi
		fi
	else
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" 2
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" 2
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" 2
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" 2
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" 2
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" 2
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" 2
		echo -n 2 > "$SNMP_PATH/md_security_clearance.txt"
		echo -n 2 > "$SNMP_PATH/md_security_service_door.txt"
		echo -n 2 > "$SNMP_PATH/md_security_marriage.txt"
		refreshed=1
	fi
	if board_type "certainty"; then
		$MBCTL "g4kcrash" 2>/dev/null | awk '{print $2}' > /tmp/snmp/FPGA4K_decoder_crash.txt
	fi  
fi
if [ $refreshed -ne 1 ]; then
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.4.1.0" 1
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.2.0" 1
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.3.0" 1
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.1.0" 1
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.2.1.0" 1
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.1.4.0" 1
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.15.3.2.0" 1
	echo -n 1 > "$SNMP_PATH/md_security_clearance.txt"
	echo -n 1 > "$SNMP_PATH/md_security_service_door.txt"
	echo -n 1 > "$SNMP_PATH/md_security_marriage.txt"
fi

check_sm_status_alarms

