#!/bin/bash

oldstat=/var/run/snmp_bond0_stat
exec < /proc/net/bonding/bond0

active="notfound"
main_status="notfound" interface=main
eth0_status="notfound" eth0_fcount=-1
eth1_status="notfound" eth1_fcount=-1

while read line; do
      case "$line" in
        "Currently Active Slave: "*)
            active="${line##Currently Active Slave: }" ;;
        "Slave Interface: "*)
            interface="${line##Slave Interface: }" ;;
        "MII Status: "*)
            eval "${interface}_status=${line##MII Status: }" ;;
        "Link Failure Count: "*)
            eval "${interface}_fcount=${line##Link Failure Count: }" ;;
      esac
done

if [ -f $oldstat ]; then
      read old_active old_eth0_fcount old_eth1_fcount < $oldstat
else
      old_active="$active"
      old_eth0_fcount=$eth0_fcount
      old_eth1_fcount=$eth1_fcount
fi

echo $active $eth0_fcount $eth1_fcount > $oldstat

ch=none

[ $eth0_fcount -ne $old_eth0_fcount ] && ch="$ch eth0_new_failure"
[ $eth1_fcount -ne $old_eth1_fcount ] && ch="$ch eth1_new_failure"
[ $old_active != $active ] && ch="$ch new_active=$active"

echo "${ch#none }"
echo "$active"
echo "bond0:$main_status eth0:$eth0_status eth1:$eth1_status"
