#!/bin/bash

# Environment statistics (UPS load and temperature)
# Load from older UPSs is in tenths of a percent
load_ups1=`snmpget -v 1 -c 'private' -m PowerNet-MIB ups1 PowerNet-MIB::upsHighPrecOutputLoad.0 | grep -Eo '[0-9]+$'`
power_ups1=$((6 * $load_ups1))
load_ups2=`snmpget -v 1 -c 'private' -m PowerNet-MIB ups2 PowerNet-MIB::upsHighPrecOutputLoad.0 | grep -Eo '[0-9]+$'`
power_ups2=$((6 * $load_ups2))
# New UPS lets us read power in watts directly
power_ups221=`snmpget -v 1 -c 'r3@dsnmp!' -m PowerNet-MIB ups221 PowerNet-MIB::upsAdvOutputActivePower.0 | grep -Eo '[0-9]+$'`
power_222=$(($power_ups1 + $power_ups2))
power_total=$(($power_ups1 + $power_ups2 + $power_ups221))
gmetric --name "power_222" --value "$power_222" --type "int32" --group "environment" --title "222 cluster power consumption" --units "W"
gmetric --name "power_221" --value "$power_ups221" --type "int32" --group "environment" --title "221 cluster power consumption" --units "W"

# Temperature from universal IO
temp_222=`snmpget -v 1 -c 'private' -m PowerNet-MIB ups1 PowerNet-MIB::uioSensorStatusTemperatureDegF.1.1 | grep -Eo '[0-9]+$'`
temp_221=`snmpget -v 1 -c 'r3@dsnmp!' -m PowerNet-MIB ups221 PowerNet-MIB::uioSensorStatusTemperatureDegF.1 | grep -Eo '[0-9]+$'`
gmetric --name "ambient_temp_222" --value "$temp_222" --type "int8" --group "environment" --title "222 ambient temperature" --units "F"
gmetric --name "ambient_temp_221" --value "$temp_221" --type "int8" --group "environment" --title "221 ambient temperature" --units "F"

# Bandwidth from the switch cross-cluster
# Note port 10201 is the first 10G SFP port
octet_out1=`snmpget -v 1 -c private switch-ipmi IF-MIB::ifOutOctets.10201|grep -Eo [0-9]+$`
sleep 1
octet_out2=`snmpget -v 1 -c private switch-ipmi IF-MIB::ifOutOctets.10201|grep -Eo [0-9]+$`
bytes_out=$(($octet_out2-$octet_out1))
if [ ! $bytes_out -lt 0 ]; then
	# Don't send gmetric if 32-bit counter has rolled over and result is negative
	gmetric --name "uplink_bytes_out" --value "$bytes_out" --type "uint32" --group "environment" --title "221 uplink bytes out" --units "Bytes/sec"
fi

octet_in1=`snmpget -v 1 -c private switch-ipmi IF-MIB::ifInOctets.10201|grep -Eo [0-9]+$`
sleep 1
octet_in2=`snmpget -v 1 -c private switch-ipmi IF-MIB::ifInOctets.10201|grep -Eo [0-9]+$`
bytes_in=$(($octet_in2-$octet_in1))
if [ ! $bytes_in -lt 0 ]; then
	# Don't send gmetric if 32-bit counter has rolled over and result is negative
	gmetric --name "uplink_bytes_in" --value "$bytes_in" --type "uint32" --group "environment" --title "221 uplink bytes in" --units "Bytes/sec"
fi
