#!/bin/bash

# Collect statistics on Infiniband networking
# Note the -r flag resets the counters, so this should be ~1 minute of data
if [[ ( -n "`which ibstat`" ) && ( -n "`ibstat`" ) ]]; then
	# This node has Infiniband
	ib_stats=`perfquery -rx`
	if [ $? -eq 0 ]; then
		ib_bytes_out=$(grep -Po '^PortXmitData:\.+\K\d+$' <<< "$ib_stats")
		ib_bytes_in=$(grep -Po '^PortRcvData:\.+\K\d+$' <<< "$ib_stats")
		ib_pkts_out=$(grep -Po '^PortXmitPkts:\.+\K\d+$' <<< "$ib_stats")
		ib_pkts_in=$(grep -Po '^PortRcvPkts:\.+\K\d+$' <<< "$ib_stats")

		# use counter * 4 / 60 because running once a minute, and perfquery returns 'octets divided by 4' for some reason
		# we also use a float because gmetric only supports up to uint32, and IB can do more than 4GB/s = 16 Gbit
		gmetric --name="ib_bytes_out" --title="InfiniBand Bytes Sent" --value="$(($ib_bytes_out * 4 / 60))" --units="bytes/sec" --type="float" --group="network" 
		gmetric --name="ib_bytes_in" --title="InfiniBand Bytes Received" --value="$(($ib_bytes_in * 4 / 60))" --units="bytes/sec" --type="float" --group="network" 
		gmetric --name="ib_pkts_out" --title="InfiniBand Packets Sent" --value="$(($ib_pkts_out / 60))" --units="packets/sec" --type="float" --group="network" 
		gmetric --name="ib_pkts_in" --title="InfiniBand Packets Received" --value="$(($ib_pkts_out / 60))" --units="packets/sec" --type="float" --group="network" 
	fi
fi