1219820Sjeff#!/bin/sh
2219820Sjeff
3219820SjeffIBPATH=${IBPATH:-@IBSCRIPTPATH@}
4219820Sjeff
5219820Sjefffunction usage() {
6219820Sjeff	echo Usage: `basename $0` "[-h] [-v] [-N | -nocolor]" \
7219820Sjeff	    "[<topology-file> | -C ca_name -P ca_port -t(imeout) timeout_ms]"
8219820Sjeff	exit -1
9219820Sjeff}
10219820Sjeff
11219820Sjefffunction user_abort() {
12219820Sjeff	echo "Aborted"
13219820Sjeff	exit 1
14219820Sjeff}
15219820Sjeff
16219820Sjefftrap user_abort SIGINT
17219820Sjeff
18219820Sjeffgflags=""
19219820Sjeffverbose=""
20219820Sjeffv=0
21219820Sjeffntype=""
22219820Sjeffnodeguid=""
23219820Sjeffoldlid=""
24219820Sjefftopofile=""
25219820Sjeffca_info=""
26219820Sjeff
27219820Sjeffwhile [ "$1" ]; do
28219820Sjeff	case $1 in
29219820Sjeff	-h)
30219820Sjeff		usage
31219820Sjeff		;;
32219820Sjeff	-N|-nocolor)
33219820Sjeff		gflags=-N
34219820Sjeff		;;
35219820Sjeff	-v)
36219820Sjeff		verbose=-v
37219820Sjeff		v=1
38219820Sjeff		;;
39219820Sjeff	-P | -C | -t | -timeout)
40219820Sjeff		case $2 in
41219820Sjeff		-*)
42219820Sjeff			usage
43219820Sjeff			;;
44219820Sjeff		esac
45219820Sjeff		if [ x$2 = x ] ; then
46219820Sjeff			usage
47219820Sjeff		fi
48219820Sjeff		ca_info="$ca_info $1 $2"
49219820Sjeff		shift
50219820Sjeff		;;
51219820Sjeff	-*)
52219820Sjeff		usage
53219820Sjeff		;;
54219820Sjeff	*)
55219820Sjeff		if [ "$topofile" ]; then
56219820Sjeff			usage
57219820Sjeff		fi
58219820Sjeff		topofile="$1"
59219820Sjeff		;;
60219820Sjeff	esac
61219820Sjeff	shift
62219820Sjeffdone
63219820Sjeff
64219820Sjeffif [ "$topofile" ]; then
65219820Sjeff	netcmd="cat $topofile"
66219820Sjeffelse
67219820Sjeff	netcmd="$IBPATH/ibnetdiscover $ca_info"
68219820Sjefffi
69219820Sjeff
70219820Sjefftext="`eval $netcmd`"
71219820Sjeffrv=$?
72219820Sjeffecho "$text" | awk '
73219820SjeffBEGIN {
74219820Sjeff	ne=0
75219820Sjeff	pe=0
76219820Sjeff}
77219820Sjefffunction check_node(lid)
78219820Sjeff{
79219820Sjeff	nodechecked=1
80219820Sjeff	if (system("'$IBPATH'/ibchecknode'"$ca_info"' '$gflags' '$verbose' " lid)) {
81219820Sjeff		ne++
82219820Sjeff		badnode=1
83219820Sjeff		return
84219820Sjeff	}
85219820Sjeff}
86219820Sjeff
87219820Sjeff/^Ca/ || /^Switch/ || /^Rt/ {
88219820Sjeff			nnodes++
89219820Sjeff			ntype=$1; nodeguid=substr($3, 4, 16); ports=$2
90219820Sjeff			if ('$v')
91219820Sjeff				print "\n# Checking " ntype ": nodeguid 0x" nodeguid
92219820Sjeff
93219820Sjeff			nodechecked=0
94219820Sjeff			badnode=0
95219820Sjeff			if (ntype != "Switch")
96219820Sjeff				next
97219820Sjeff
98219820Sjeff			lid = substr($0, index($0, "port 0 lid ") + 11)
99219820Sjeff			lid = substr(lid, 1, index(lid, " ") - 1)
100219820Sjeff			check_node(lid)
101219820Sjeff		}
102219820Sjeff/^\[/	{
103219820Sjeff		nports++
104219820Sjeff		port = $1
105219820Sjeff		if (!nodechecked) {
106219820Sjeff			lid = substr($0, index($0, " lid ") + 5)
107219820Sjeff			lid = substr(lid, 1, index(lid, " ") - 1)
108219820Sjeff			check_node(lid)
109219820Sjeff		}
110219820Sjeff		if (badnode) {
111219820Sjeff			print "\n# " ntype ": nodeguid 0x" nodeguid " failed"
112219820Sjeff			next
113219820Sjeff		}
114219820Sjeff		sub("\\(.*\\)", "", port)
115219820Sjeff		gsub("[\\[\\]]", "", port)
116219820Sjeff		if (system("'$IBPATH'/ibcheckportstate'"$ca_info"' '$gflags' '$verbose' " lid " " port)) {
117219820Sjeff			if (!'$v' && oldlid != lid) {
118219820Sjeff				print "# Checked " ntype ": nodeguid 0x" nodeguid " with failure"
119219820Sjeff				oldlid = lid
120219820Sjeff			}
121219820Sjeff			pe++;
122219820Sjeff		}
123219820Sjeff}
124219820Sjeff
125219820Sjeff/^ib/	{print $0; next}
126219820Sjeff/ibpanic:/	{print $0}
127219820Sjeff/ibwarn:/	{print $0}
128219820Sjeff/iberror:/	{print $0}
129219820Sjeff
130219820SjeffEND {
131219820Sjeff	printf "\n## Summary: %d nodes checked, %d bad nodes found\n", nnodes, ne
132219820Sjeff	printf "##          %d ports checked, %d ports with bad state found\n", nports, pe
133219820Sjeff}
134219820Sjeff'
135219820Sjeffexit $rv
136