#!/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
#
##############################################################################

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

check_single_instance "/var/lock/subsys/snmp_5minutely" || exit 1

MCSETUP=/doremi/sbin/mcsetup.out
SBCSETUP=/doremi/sbin/sbcsetup.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

#
# SE7221 motherboard sensors
#
sensors_motherboard_model_SE7221()
{
	ipmitool -I open sensor | tr -s [:space:] > /tmp/snmp_ipmi.$$
	mv /tmp/snmp_ipmi.$$ /tmp/snmp_ipmi

	#
	# Temperatures
	#
	TEMR=$(cat /tmp/snmp_ipmi | grep 'Serverboard temp' | cut -d ' ' -f 4 | cut -d. -f1 )
	if [ "${TEMR}" != "na" ] ; then
		save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
		save_integer_value	TEMP_MOTHERBOARD_value	${TEMR}
	else
		if [ ! -f "$SNMP_PATH/TEMP_MOTHERBOARD_title" ] ; then
			save_string_value	TEMP_MOTHERBOARD_title	"Motherboard"
			save_integer_value	TEMP_MOTHERBOARD_value	0
		fi
	fi
	TEMR=$( cat /tmp/snmp_ipmi | grep 'Proc1 Core temp'  | cut -d ' ' -f 5 | cut -d. -f1 )
	if [ "${TEMR}" != "na" ] ; then
		save_string_value	TEMP_CPU_title		"CPU"
		save_integer_value	TEMP_CPU_value	$TEMR
	else
		if [ ! -f "$SNMP_PATH/TEMP_CPU_title" ] ; then
			save_string_value	TEMP_CPU_title		"CPU"
			save_integer_value	TEMP_CPU_value	0
                fi
	fi
	save_string_value	TEMP_CHIPSET_title		"Chipset"
	save_integer_value	TEMP_CHIPSET_value	0

	#
	# Voltages
	#
	save_string_value	VOLT_1_title			"Voltage 1"
	save_to_mV_string_value	VOLT_1_value			"0"
	save_string_value	VOLT_2_title			"Voltage 2"
	save_to_mV_string_value	VOLT_2_value			"0"
	save_string_value	VOLT_3_title			"+1.8V"
	VOLT=$( cat /tmp/snmp_ipmi | grep 'Baseboard 1.8V' | cut -d ' ' -f 4 )
	if [ "${VOLT}" != "na" ] ; then
		save_to_mV_string_value	VOLT_3_value			"${VOLT}"
	else
		save_to_mV_string_value	VOLT_3_value			"0"
	fi
	save_string_value	VOLT_4_title			"+5V"
	VOLT=$( cat /tmp/snmp_ipmi | grep 'Baseboard 5.0V' | cut -d ' ' -f 4 )
	if [ "${VOLT}" != "na" ] ; then
		save_to_mV_string_value	VOLT_4_value			"${VOLT}"
	else
		save_to_mV_string_value	VOLT_4_value			"0"
	fi
	save_string_value	VOLT_5_title			"+12V"
	VOLT=$( cat /tmp/snmp_ipmi | grep 'Baseboard 12V'  | cut -d ' ' -f 4 )
	if [ "${VOLT}" != "na" ] ; then
		save_to_mV_string_value	VOLT_5_value			"${VOLT}"
	else
		save_to_mV_string_value	VOLT_5_value			"0"
	fi
	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"

	#
	# Fan speeds 
	#
	FAN=$( cat /tmp/snmp_ipmi | grep 'Processor 1 Fan'  | cut -d ' ' -f 5 | cut -d. -f1 )
	if [ "$FAN" != "na" ] ; then
		save_string_value	FAN_CPU_title		"CPU Fan"
		save_integer_value	FAN_CPU_value		"$FAN"
	else
		if [ ! -f ${SNMP_PATH}/FAN_CPU_title ]; then
			save_string_value	FAN_CPU_title		"CPU Fan"
			save_integer_value	FAN_CPU_value		"-1"
		fi
	fi
	FAN=$( cat /tmp/snmp_ipmi | grep 'Serverboard Fan3'  | cut -d ' ' -f 4 | cut -d. -f1 )
	if [ "$FAN" != "na" ] ; then
		save_string_value	FAN_4_title		"Serverboard Fan3"
		save_integer_value	FAN_4_value		"$FAN"
	else
		if [ ! -f ${SNMP_PATH}/FAN_4_title ]; then
			save_string_value	FAN_4_title		"Serverboard Fan3"
			save_integer_value	FAN_4_value		"-1"
		fi
	fi
	FAN=$( cat /tmp/snmp_ipmi | grep 'Serverboard Fan4'  | cut -d ' ' -f 4 | cut -d. -f1 )
	if [ "$FAN" != "na" ] ; then
		save_string_value	FAN_5_title		"Serverboard Fan4"
		save_integer_value	FAN_5_value		"$FAN"
	else
		if [ ! -f ${SNMP_PATH}/FAN_5_title ]; then
			save_string_value	FAN_5_title		"Serverboard Fan4"
			save_integer_value	FAN_5_value		"-1"
		fi
	fi
	FAN=$( cat /tmp/snmp_ipmi | grep 'Serverboard Fan5'  | cut -d ' ' -f 4 | cut -d. -f1 )
	if [ "$FAN" != "na" ] ; then
		save_string_value	FAN_6_title		"Serverboard Fan5"
		save_integer_value	FAN_6_value		"$FAN"
	else
		if [ ! -f ${SNMP_PATH}/FAN_6_title ]; then
			save_string_value	FAN_6_title		"Serverboard Fan5"
			save_integer_value	FAN_6_value		"-1"
		fi
	fi
}

#
# refresh_fullcap_primary_projector_status
#
refresh_fullcap_primary_projector_status ()
{
	local type
	
	if board_full_cap_support; then
		$SBCSETUP -a --query-spb > $SNMP_PATH/md_queryspb
		res=$?
		rm -f $SNMP_PATH/PROJ_*_md_connection $SNMP_PATH/PROJ_*_thumbprint $SNMP_PATH/PROJ_*_security_error
		rm -f $SNMP_PATH/AUDIO_*_md_connection $SNMP_PATH/AUDIO_*_thumbprint $SNMP_PATH/AUDIO_*_security_error
		if [ $res -eq 0 ]; then
			device_count=$(grep configured $SNMP_PATH/md_queryspb | awk '{print $1}')
			for (( i=0 ; $i < $device_count ; i++ )); do
				tail_lines=$((12*($i+1)))
				tail -$tail_lines $SNMP_PATH/md_queryspb | head -12 > $SNMP_PATH/md_queryspb_$i
				name=$(grep Name $SNMP_PATH/md_queryspb_$i | awk -F : '{print $2}' | cut -c 2-)
				if [ -e $SNMP_PATH/AUDIO_${name}_ip_address ]; then
					type=AUDIO
				else
					type=PROJ
				fi
				connected=$(grep Connected $SNMP_PATH/md_queryspb_$i | awk '{print $3}')
				if [ "$connected" == "Yes" ]; then
					echo -n 1 > $SNMP_PATH/${type}_${name}_md_connection
					update_alarm_state ${type}_${name}_md_connection "normal"
					grep Thumbprint $SNMP_PATH/md_queryspb_$i | awk '{print $3}' > $SNMP_PATH/${type}_${name}_thumbprint
					#
					security_alert=$(grep SecurityAlert $SNMP_PATH/md_queryspb_$i | awk '{print $3}')
					if [ "$security_alert" == "Yes" ]; then
						echo -n 1 > $SNMP_PATH/${type}_${name}_security_error
					else
						echo -n 0 > $SNMP_PATH/${type}_${name}_security_error
					fi
				else
					echo -n 0 > $SNMP_PATH/${type}_${name}_md_connection
					update_alarm_state ${type}_${name}_md_connection "error"
				fi
 				rm -f $SNMP_PATH/md_queryspb_$i
			done
		fi
	fi
}

#
# refresh_Barco_table_alarms
#
refresh_Barco_table_alarms ()
{
	ip=$1
	snmpfile_base=$2
	oid=$3

	res=$(snmpwalk -v 1 -Oe -Ov -c public $ip $oid | awk '{print $2}')
	if [ ! -n "$res" ]; then
		# Unknown
		rm -f "${snmpfile_base}_error" "${snmpfile_base}_warning"
	elif echo "$res" | grep -q -e 1 -e 2 -e 3 -e 6; then
		# Error
		echo -n 0 > "${snmpfile_base}_warning"
		echo -n 1 > "${snmpfile_base}_error"
	elif echo "$res" | grep -q -e 4 -e 5; then
		# Warning
		echo -n 1 > "${snmpfile_base}_warning"
		echo -n 0 > "${snmpfile_base}_error"
	elif echo "$res" | grep -q -v -e 0 -e 1 -e 2 -e 3 -e 4 -e 5 -e 6; then
		# Unknown value
		rm -f "${snmpfile_base}_error" "${snmpfile_base}_warning"
	elif echo "$res" | grep -q -e 0; then
		# Normal
		echo -n 0 > "${snmpfile_base}_warning"
		echo -n 0 > "${snmpfile_base}_error"
	fi
}

#
# refresh_Barco_projector_alarms
#
refresh_Barco_projector_alarms ()
{
	title=$1
	model=$2
	ip=$3
	
	# temperature
	refresh_Barco_table_alarms $ip "/tmp/snmp/PROJ_${title}_temperature" .1.3.6.1.4.1.12612.220.11.4.2.1.1.1.7

	# voltage
	refresh_Barco_table_alarms $ip "/tmp/snmp/PROJ_${title}_battery" .1.3.6.1.4.1.12612.220.11.4.2.1.2.1.7

	# fan
	refresh_Barco_table_alarms $ip "/tmp/snmp/PROJ_${title}_fan" .1.3.6.1.4.1.12612.220.11.4.2.1.3.1.7
	
	# no filter timer
	rm -f "/tmp/snmp/PROJ_${title}_filter_timer_error" "/tmp/snmp/PROJ_${title}_filter_timer_warning"
	
	# no lamp timer
	rm -f "/tmp/snmp/PROJ_${title}_lamp_timer_error" "/tmp/snmp/PROJ_${title}_lamp_timer_warning"
}

#
# refresh_Christie_temperature_sensor
#
refresh_Christie_temperature_sensor ()
{
	ip=$1
	snmpfile_base=$2
	base_oid=$3
	state=1

	# check temperature sensors diode error
	res=$(snmpwalk -v 1 -Oe -Ov -c public $ip ${base_oid}.2.2.1.1.3 | awk '{print $2}')
	if [ ! -n "$res" ]; then
		# Unknown
		rm -f "${snmpfile_base}_error" "${snmpfile_base}_warning"
	elif echo "$res" | grep -q -e 1; then
		# Error
		echo -n 0 > "${snmpfile_base}_warning"
		echo -n 1 > "${snmpfile_base}_error"
	elif echo "$res" | grep -q -v -e 1 -e 2; then
		# Unknown value
		rm -f "${snmpfile_base}_error" "${snmpfile_base}_warning"
	elif echo "$res" | grep -q -e 2; then
		# Normal
		echo -n 0 > "${snmpfile_base}_warning"
		echo -n 0 > "${snmpfile_base}_error"
	fi
}

#
# refresh_Christie_state_to_snmp
#
refresh_Christie_state_to_snmp ()
{
	snmpfile_base=$1
	state=$2
	
	case $state in
		1)
			# unknown
			rm -f "${snmpfile_base}_error" "${snmpfile_base}_warning"
			;;
			
		2)
			# not application
			rm -f "${snmpfile_base}_error" "${snmpfile_base}_warning"
			;;
			
		3)
			# normal
			echo -n 0 > "${snmpfile_base}_warning"
			echo -n 0 > "${snmpfile_base}_error"
			;;
			
		4)
			# warning
			echo -n 1 > "${snmpfile_base}_warning"
			echo -n 0 > "${snmpfile_base}_error"
			;;
			
		5)
			# error
			echo -n 0 > "${snmpfile_base}_warning"
			echo -n 1 > "${snmpfile_base}_error"
			;;
			
		*)
			# unknown
			rm -f "${snmpfile_base}_error" "${snmpfile_base}_warning"
			;;
	esac	
}

#
# refresh_Christie_voltage_sensor
#
refresh_Christie_voltage_sensor ()
{
	ip=$1
	snmpfile_base=$2
	base_oid=$3
	state=1
	
	for volt_node in 1 2 3 4 5 6 7; do
		res=$(snmpget -v 1 -Oe -Ov -c public $ip ${base_oid}.2.5.2.${volt_node}.0 | awk '{print $2}')
		if [ ! -n "$res" ]; then
			# Unknown
			state=1
		else
			case "$res" in
				1)
					# power good
					state=3
					;;
				2)
					# power not good
					state=5
					;;
				3)
					# not available
					if [ $state -eq 1 ]; then
						state=2
					fi
					;;
				*)
					# unknown
					;;
			esac
			if [ $state -eq 5 ]; then
				# voltage error detected, no need to check other sensors
				break
			fi
		fi
	done
	
	refresh_Christie_state_to_snmp $snmpfile_base $state
}

#
# refresh_Christie_fan_sensor
#
refresh_Christie_fan_sensor ()
{
	ip=$1
	snmpfile_base=$2
	base_oid=$3
	state=1

	# check fan fault
	res=$(snmpwalk -v 1 -Oe -Ov -c public $ip ${base_oid}.2.2.2.1.3 | awk '{print $2}')
	if [ ! -n "$res" ]; then
		# Unknown
		state=1
	elif echo "$res" | grep -q -e 1; then
		# Error
		state=5
	elif echo "$res" | grep -q -e 2; then
		# Normal
		state=3
	fi
	
	if [ $state -ne 5 ]; then
		# check fan too slow (only if no fan error reported)
		res=$(snmpwalk -v 1 -Oe -Ov -c public $ip ${base_oid}.2.2.2.1.4 | awk '{print $2}')
		if [ ! -n "$res" ]; then
			# Unknown
			if [ $state -ne 3 ]; then
				state=1
			fi
		elif echo "$res" | grep -q -e 1; then
			# Warning
			state=4
		elif echo "$res" | grep -q -e 2; then
			# Normal
			state=3
		fi
	fi
	
	refresh_Christie_state_to_snmp $snmpfile_base $state
}

#
# refresh_Christie_lamp_timer
#
refresh_Christie_lamp_timer ()
{
	ip=$1
	snmpfile_base=$2
	oid=$3
	state=1
	
	res=$(snmpget -v 1 -Oe -Ov -c public $ip ${oid} | awk '{print $2}')
	if [ -n "$res" ]; then
		case "$res" in
			1)
				# lamp expired
				state=4
				;;
			2)
				# lamp is good
				state=3
				;;
			*)
				# unknown
				;;
		esac
	fi

	refresh_Christie_state_to_snmp $snmpfile_base $state
}

#
# refresh_Christie_projector_alarms
#
refresh_Christie_projector_alarms()
{
	title=$1
	model=$2
	ip=$3
	
	# BV: cannot validate without Christie projector.
	# from Christie's MIBs, the OID 11th node depends on product model
	# 1 = cdcpS2MIB
	# 2 = cp2220
	# 3 = cp2230
	# 4 = cp2210
	# 5 = cp4220
	# 6 = cp4230
	# i will assume 6 = cp4230
	# TODO: use correct model type above
	base_oid=.1.3.6.1.4.1.25766.1.12.1

	# temperature
	refresh_Christie_temperature_sensor $ip "/tmp/snmp/PROJ_${title}_temperature" ${base_oid}.6

	# voltage
	refresh_Christie_voltage_sensor $ip "/tmp/snmp/PROJ_${title}_battery" ${base_oid}.6

	# fan
	refresh_Christie_fan_sensor $ip "/tmp/snmp/PROJ_${title}_fan" ${base_oid}.6
	
	# no filter timer
	refresh_Christie_state_to_snmp "/tmp/snmp/PROJ_${title}_filter_timer" 2
	
	# lamp timer
	refresh_Christie_lamp_timer $ip "/tmp/snmp/PROJ_${title}_lamp_timer" ${base_oid}.1.3.4.7.0
}

#
# refresh_NEC_projector_alarms
#
refresh_NEC_projector_alarms ()
{
	title=$1
	model=$2
	ip=$3
}

#
# refresh_projectors_alarms
#
refresh_projectors_alarms ()
{
	title_files=$(ls /tmp/snmp/PROJ*_title 2>/dev/null)
	if [ -n "$title_files" ]; then
		for title_file in $title_files; do
			ip_address=""
			title=$(cat $title_file)
			test -e "/tmp/snmp/PROJ_${title}_vendor" && vendor=$(cat "/tmp/snmp/PROJ_${title}_vendor")
			test -e "/tmp/snmp/PROJ_${title}_model" && model=$(cat "/tmp/snmp/PROJ_${title}_model")
			test -e "/tmp/snmp/PROJ_${title}_ip_address" && ip_address=$(cat "/tmp/snmp/PROJ_${title}_ip_address")
			if [ ! -n "$ip_address" ]; then
				# can't fetch snmp if we don't have the ip address
				continue
			fi
			case "$vendor" in
				Barco|Christie|NEC)
					ping -c1 -W3 $ip_address >/dev/null 2>&1
					if [ $? -eq 0 ]; then
						refresh_${vendor}_projector_alarms "$title" $model $ip_address
					fi
					;;
				
				*)
					;;
			esac
		done
	fi
}

refresh_network_status() {
	local KEYWORD=$1
	local interface=$2
	
	local conf
	conf=$(ifconfig $interface 2>/dev/null)
	
	if [ "$?"  != "0" ]; then
		echo -n 3 > $SNMP_PATH/${KEYWORD}_status
		echo -n 3 > $SNMP_PATH/${KEYWORD}_config
		update_alarm_state ${KEYWORD} error
	else
		if echo $conf | grep -q "inet addr:"; then
			echo -n 4 > $SNMP_PATH/${KEYWORD}_config
		else
			echo -n 3 > $SNMP_PATH/${KEYWORD}_config
			update_alarm_state ${KEYWORD} warning
		fi
		if echo $conf | grep -q "RUNNING"; then
			echo -n 5 > $SNMP_PATH/${KEYWORD}_status
		else
			echo -n 4 > $SNMP_PATH/${KEYWORD}_status
			update_alarm_state ${KEYWORD} warning
		fi
		
		# should we clear the alarm
		if echo $conf | grep -q "inet addr:" && echo $conf | grep -q "RUNNING"; then
			update_alarm_state ${KEYWORD} normal
		fi
	fi
	
}

refresh_all_network_status() {
	refresh_network_status ETH0 eth0
	refresh_network_status ETH1 eth1

	if [ ! -e /proc/dolphin0 ] || board_type "certainty" || board_type "clarity" || board_type "dolphin"; then
		echo -n 2 > $SNMP_PATH/ASIX0_config
		echo -n 2 > $SNMP_PATH/ASIX0_status
	else
		refresh_network_status ASIX0 asix0
	fi
}

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

# refresh the RTC clock consumed
res=$($MCSETUP --get-rtc)
if [ $? -eq 0 ]; then
	valid=$(echo "$res" | grep "The dolphin RTC time")
	if [ $? -eq 1 ]; then
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.14.4.2.0" $(echo $res | awk -F , '{print $2}')
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.14.4.1.0" 1
	else
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.14.4.2.0" 0
		set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.14.4.1.0" 2
	fi
else
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.14.4.2.0" 0
	set_integer_value "$SNMP_PATH/$DOREMI_OID.1.3.14.4.1.0" 2
fi

refresh_fullcap_primary_projector_status

refresh_projectors_alarms

refresh_all_network_status


