#!/bin/bash

if [ $# == 0 ]; then
    echo "WARNING: No arguments given. usage is $0 bond0=eth0,eth1 bond1=eth2,eth3"
    exit 1
fi

errors=0
err_msg="CRITICAL:"
msg="OK:"
argc=$#
argv=("$@")

for (( j=0; j<argc; j++ )); do
    bond=`echo "${argv[j]}"|awk -F'=' '{print $1}'`
    ifaces=`echo "${argv[j]}"|awk -F'=' '{print $2}'|sed 's/,/ /g'`
    bond_status=`ovs-appctl bond/show ${bond} 2>$1`
    if [ $? -ne 0 ]; then
            ((errors++))
            err_msg+=" Error getting status of ${bond}, bond might not exist or incorrect configured."
    else
        for iface in $ifaces; do
            iface_state=`ovs-appctl bond/show ${bond} 2>&1|grep ^member\ $iface`
            if [ -z "${iface_state}" ]; then
                ((errors++))
                err_msg+=" Interface $iface not configured for $bond." 
            else
                state=`echo $iface_state|awk -F': ' '{print $2}'`
                if [ "$state" == "enabled" ]; then
                    msg+=" Iface ${iface} in ${bond} enabled."
                else
                    ((errors++))
                     err_msg+=" Interface ${iface} not enabled in ${bond}."
                fi
            fi
        done
    fi
done

if [ ${errors} -ne 0 ]; then
    echo "${err_msg}"
    exit ${errors}
fi

echo "${msg}"
exit 0
