niccheck 3.68 KB
Newer Older
Dan Reading's avatar
Dan Reading committed
1
#!/bin/bash
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# Copyright (c) 2013 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
Dan Reading's avatar
Dan Reading committed
24 25 26 27 28

echo -n ' Starting niccheck.. '

source checkutils.sh

29 30 31 32 33 34
x=$(caller)
[[ "${x/NULL}" = "$x" ]] && declare -ri niccheck_standalone=0 || declare -ri niccheck_standalone=1

initialize $@
cp /dev/null $tmplog

Dan Reading's avatar
Dan Reading committed
35
tmplog2=/tmp/.2tmp
36
cp /dev/null ${tmplog2}
Dan Reading's avatar
Dan Reading committed
37 38 39 40
add_on_exit "rm -f $tmplog2"

finished() {
cat ${tmplog2}
41
echo "Niccheck `date`:" >> ${logfile}
Dan Reading's avatar
Dan Reading committed
42 43 44 45
cat ${tmplog} >> ${logfile} 
cat ${tmplog2} >> ${logfile} 
rm -f ${tmplog2}

46
(( $niccheck_standalone )) && exit 0 || return 0
Dan Reading's avatar
Dan Reading committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
}

{
unset -v ifinfo ; declare -a ifinfo=()
case $os in
    Linux )
	delimiter="HWaddr" ;;
    FreeBSD )
	delimiter="ether" ;;
    * )
	failed="os $os unknown"
	finished
	;;
esac

62
# Ignore virtual devices that won't be in the DB
63
badboys="(ifb|xenbr|vlan|AMPR)"
64 65

unset -v d ; declare -a d=($(ifconfig -a | grep $delimiter | grep -Ev $badboys))
Dan Reading's avatar
Dan Reading committed
66
declare -i cnt=0;
67
ifinfo[0]=''
Dan Reading's avatar
Dan Reading committed
68 69 70 71 72
for ((idx=0; idx<${#d[*]};idx++)) ; do
    [[ "${d[idx]}" == "${delimiter}" ]] || continue
    ((++idx))
    y=${d[idx]}
    y=${y,,} #lowercase
73 74 75 76 77 78 79
    e=${y//:/}
    # don't place on list if dup. can happen with vlan interface
    list=${ifinfo[@]}
    for i in $list ; do
	[[ "$i" == "$e" ]] && e=''
    done
    [[ "$e" ]] && { ifinfo[$cnt]=${e} ; ((++cnt)) ; }
Dan Reading's avatar
Dan Reading committed
80 81 82 83 84 85 86
done

echo "${ifinfo[@]}"

} >> ${tmplog} 2>&1

ifcnt=${#ifinfo[*]}
87

Dan Reading's avatar
Dan Reading committed
88
# if saving data for testbed database
89
if (( $collect_flag ))  ; then
Dan Reading's avatar
Dan Reading committed
90 91 92 93 94 95 96 97
    printf "NETINFO UNITS=%s\n"  ${ifcnt} >> ${logfile4tb}
    for ((ifi=0; ifi<$ifcnt; ifi++)) ; do
	macl="${ifinfo[$ifi]}"
	macl=${macl,,}
	printf "NETUNIT TYPE=\""ETH\"" ID=\""%s\""\n" ${ifinfo[$ifi]} >> ${logfile4tb}
    done
fi

98 99 100 101 102 103
# we are done if in MFS mode
if (( $mfsmode )) ; then
    (( $niccheck_standalone )) && exit 0 || return 0
fi

tbreturn=$(getfromtb NETUNIT)
Dan Reading's avatar
Dan Reading committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
if [ -z "$tbreturn" ] ; then 
    failed="TBmiss no info"
    $(echo "TBmiss empty info returned. Have ${ifinfo[@]} FAILED" >> ${tmplog2})
    finished
fi

#lowercase
tbreturn=${tbreturn,,}

#turn space seperated string into array
unset -v tbinfo; declare -a tbinfo=(${tbreturn// / })

tbcnt=${#tbinfo[*]}
numnics=$ifcnt
maxcnt=${ifcnt}


if [[ ${ifcnt} -ne ${tbcnt} ]] ; then
    failed="TBmiss"
    [[ ${ifcnt} -gt ${tbcnt} ]] && maxcnt=${ifcnt} || maxcnt=${tbcnt}
fi

for ((idx=0; idx<$maxcnt; idx++)) ; do
    for ((ifi=0; ifi<$ifcnt; ifi++)) ; do
	for ((tbi=0; tbi<$tbcnt; tbi++)) ; do
	    if [ "${ifinfo[$ifi]}" = "${tbinfo[$tbi]}" ] ; then
		ifinfo[$ifi]=''
		tbinfo[$tbi]=''
	    fi
	done
    done
done
ifresult=${ifinfo[@]}
tbresult=${tbinfo[@]}

#	    failed="$failed TB missing nic "
#	    failed="$failed TB extra nic "

if [[ "$ifresult" || "$tbresult" ]] ; then
    if [[ $ifresult ]] ; then
	failed="${failed}: NotIn TB $ifresult"
    fi
    if [[ $tbresult ]] ; then
	failed="${failed}: TB Claims $tbresult"
    fi
fi


{
    [[ -z ${failed} ]] && echo "$numnics interfaces OK" || echo "$numnics interfaces $failed" FAILED

} >> ${tmplog2} 2>&1


finished

160
(( $niccheck_standalone )) && exit 0 || return 0
Dan Reading's avatar
Dan Reading committed
161