checkutils.sh 28.2 KB
Newer Older
1
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/>.
# 
# }}}
#

24
25
26
if [ -z ${BASH_VERSINFO[0]} -o ${BASH_VERSINFO[0]} -lt 4 ] ; then
    echo "Need at least BASH version 4 to run nodecheck tests or to Collect Inventory, Not running checks"
    exit 0
Dan Reading's avatar
Dan Reading committed
27
28
fi

29
30
31
32
33
34
# Global Vars
       # the bash syntax ${var-1} means: use var if set else use nothing  
[[ -z "${NOSM-}" ]] && declare NOSM="echo" #do nothing command
[[ -z "${host-}" ]] && declare host       #emulab hostname
[[ -z "${failed-}" ]] && declare failed=""  #major falure to be commicated to user
[[ -z "${os-}" ]] && declare os=""      #[Linux|FreeBSD] for now
35
[[ -z "${osrel-}" ]] && declare osrel="" #release major number
36
[[ -z "${todo_exit-}" ]] && declare -a todo_exit=('echo "Exit trap"')
37
38
39
40
41
42
43
44
[[ -z "${hwinv[hwinvidx]-}" ]] && declare -A hwinv["hwinvidx"]=""  # hwinv from tmcc
[[ -z "${hwinvcopy[hwinvidx]-}" ]] && declare -A hwinvcopy["hwinvidx"]=""  # a copy of hwinv from tmcc
[[ -z "${tmccinfo[hwinvidx]-}" ]] && declare -A tmccinfo["hwinvidx"]="" # info from tmcc hwinfo
[[ -z "${collect_flag-}" ]] && declare -i collect_flag # from tmcc hwinfo
[[ -z "${check_flag-}" ]] && declare -i check_flag # from tmcc hwinfo
[[ -z "${projdir-}" ]] && declare projdir # from tmcc hwinfo
[[ -z "${errexit_val-}" ]] && declare errexit_val # holding var for set values, ie -e
[[ -z "${mfsmode-}" ]] && declare -i mfsmode=0 #are we running in a MFS?
45
46
[[ -z "${bitsize-}" ]] && declare bitsize=""
[[ -z "${native_bitsize-}" ]] && declare -i native_bitsize=0 # what is our native binary bit size
Dan Reading's avatar
Dan Reading committed
47
48
[[ -z "${USE_DD-}" ]] && declare USE_DD="tdd" # if set which dd for the tdd program to use
[[ -z "${TDD_DD-}" ]] && declare TDD_DD="dd" # if set which dd for the tdd program to use
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

# PathNames
[[ -z "${logfile-}" ]] && declare logfile # output log
[[ -z "${logfile4tb-}" ]] && declare -r logfile4tb="/tmp/nodecheck.log.tb" # for data to saved in perm storage
[[ -z "${tmplog-}" ]] && declare -r tmplog="/tmp/.$$tmp.log"
[[ -z "${logout-}" ]] && declare -r logout="/tmp/.$$logout.log" # temperary logging while building inventory"
[[ -z "${tmpout-}" ]] && declare -r tmpout="/tmp/.$$tempout.log" # ditto

# DEBUG
[[ -z "${DEBUG-}" ]] && declare -ir DEBUG=0 # Some debugging if set

initialize () {
    #exit on unbound var
    set -u
    #exit on any error
    set -e

    #call only once
    if [ "${initdone-uninit}" != "uninit" ] ; then
    	(( $DEBUG )) && printf "Attempt to call twice %s:%s called from %s\n" $FUNCNAME $LINENO "$(caller)"
	return 0
    fi
71

72
73
74
    if [ -z "${BINDIR-""}" ] ; then 
	if [ -f "/etc/emulab/paths.sh" ]; then
	    source /etc/emulab/paths.sh
75
76
77
78
79
80
81
	    # XXX paths.sh resets PATH so we need to re-add any special bindir
	    ldir=`$BINDIR/tmcc hwinfo | grep LOGDIR= | \
                  sed -e 's/.*LOGDIR="\(.*\)".*/\1/'`
	    bdir=$ldir/`uname -s`/bin-`uname -m`
	    if [ -d $bdir ] ; then
		export PATH="$PATH:$bdir"
	    fi
82
83
84
85
86
87
	else
	    export BINDIR=/usr/local/etc/emulab
	    export LOGDIR=/var/tmp
	fi
    fi
    
88
89
    bitsize=$(uname -m)
    case $bitsize in
Dan Reading's avatar
Dan Reading committed
90
	i386 | x86 | i686 ) native_bitsize=32 ;;
91
92
93
	amd64 | x86_64 ) native_bitsize=64 ;;
	* ) native_bitsize=0 ;;
    esac
94

Dan Reading's avatar
Dan Reading committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    if [ -f /etc/emulab/ismfs ] ; then
	mfsmode=1
	dd=$(whichdd2use)
	if [ "$dd" != "${dd/tdd}" ] ; then
	    USE_DD=$dd
	    TDD_DD=$(which dd)
	elif [ "$dd" != "${dd/bad}" ] ; then
	    USE_DD=$dd
	    TDD_DD=""
	else
	    USE_DD=$(which dd)
	    TDD_DD=""
	fi
    else
	mfsmode=0
	# no speed test don't have to check tdd, etc.
    fi

113
114
115
    inithostname
    initlogs $@
    inittestinfo
Dan Reading's avatar
Dan Reading committed
116

117
118
    #trap 'err_report $FUNCNAME:$LINENO' ERR
    trap 'err_report $LINENO' ERR
Dan Reading's avatar
Dan Reading committed
119

120
121
122
123
    initdone="done"
    export initdone
    return 0
}
Dan Reading's avatar
Dan Reading committed
124
125

# any command causes exit if -e option set
Dan Reading's avatar
Dan Reading committed
126
127
# including a grep just used so see if some string is in a file
# have a way to save current state and restore
Dan Reading's avatar
Dan Reading committed
128
129
130
131
132
133
134
save_e() {
    x=$-
    [[ "${x/e}" == "${x}" ]] &&	errexit_val=off || errexit_val=on
}
restore_e() {
    [[ $errexit_val == "on" ]] && set -e || set +e
}
Dan Reading's avatar
Dan Reading committed
135

Dan Reading's avatar
Dan Reading committed
136
137
# give some indication of exit on ERR trap
err_report() {
138
    echo "TRAP ERR at Caller Line $(caller)"
Dan Reading's avatar
Dan Reading committed
139
140
141
}


142
# read info from tmcc or a file. Copy into one of the three global arrays
143
# hwinv, hwinvcopy or tmccinfo
144
145
# $1 is the source $2 is the output array
# error if not both set
Dan Reading's avatar
Dan Reading committed
146
readtmcinfo() {
147
    local -A ref_hwinv
Dan Reading's avatar
Dan Reading committed
148
149
150
    local keyword
    local -i dcnt=0
    local -i ncnt=0
151
    local source output
152
    local rmtmp
Dan Reading's avatar
Dan Reading committed
153

154
    # what file to read from, if not set then make tmcc call
155
156
157
158
159
160
161
162
163
    source=${1+$1} # use $1 if set otherwise use nothing
    output=${2+$2} 
    if [ $# -ne 2 ] ; then
	printf "\n%s\n" "Script error missing arg source:|$source| output:|$output|"
	printf "Where %s:%s called from %s\n" $FUNCNAME $LINENO "$(caller)"
	exit 1
    fi

    if [ "$source" = "tmcc" ] ; then
164
	# need temp file to hold tmcc hwinv output
165
166
167
	rmtmp="y" # remove tmp file
	source=/tmp/.$$tmcchwinv
	$($BINDIR/tmcc hwinfo > $source)
Dan Reading's avatar
Dan Reading committed
168
    else
169
	rmtmp=""
Dan Reading's avatar
Dan Reading committed
170
171
    fi

172
173
174
175
176
177
178
    # initalize output array
    if [ -z "${ref_hwinv["hwinvidx"]+${ref_hwinv["hwinvidx"]}}" ] ; then
	ref_hwinv["hwinvidx"]="" #start the array
    else
	# reset the array
	for i in ${ref_hwinv["hwinvidx"]} ; do
	    unset ref_hwinv[$i]
Dan Reading's avatar
Dan Reading committed
179
	done
180
181
	# hwinvidx is always expected to be set
	ref_hwinv["hwinvidx"]="" #restart the array
Dan Reading's avatar
Dan Reading committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    fi

    # handle mult-line  input for disks and nets
    while read -r in ; do
	keyword=${in%% *}
	case $keyword in
	    DISKUNIT ) 
		keyword+="$dcnt"
		((++dcnt))
		;;
	    NETUNIT ) 
		keyword+="$ncnt"
		((++ncnt))
		;;
196
	    \#* ) continue ;; 
Dan Reading's avatar
Dan Reading committed
197
	esac
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	ref_hwinv["hwinvidx"]+="$keyword " # keeping the keyword list preserves order
	ref_hwinv[$keyword]=$in
    done < $source
    [ -n "$rmtmp" ] && rm $source || : # the colon just stops a error being caught by -e

    case $output in 
	tmccinfo )
	    if [ -z "${tmccinfo["hwinvidx"]+${tmccinfo["hwinvidx"]}}" ] ; then
		tmccinfo["hwinvidx"]="" #start the array
	    else	
		for i in ${tmccinfo["hwinvidx"]} ; do
		    unset tmccinfo[$i]
		done
		tmccinfo["hwinvidx"]="" #restart the array
	    fi
	    tmccinfo["hwinvidx"]=${ref_hwinv["hwinvidx"]} 
	    for i in ${ref_hwinv["hwinvidx"]} ; do
		tmccinfo[$i]=${ref_hwinv[$i]}
	    done
	    ;;
	hwinv )
	    if [ -z "${hwinv["hwinvidx"]+${hwinv["hwinvidx"]}}" ] ; then
		hwinv["hwinvidx"]="" #start the array
	    else	
		for i in ${hwinv["hwinvidx"]} ; do
		    unset hwinv[$i]
		done
		hwinv["hwinvidx"]="" #restart the array
	    fi
	    hwinv["hwinvidx"]=${ref_hwinv["hwinvidx"]} 
	    for i in ${ref_hwinv["hwinvidx"]} ; do
		hwinv[$i]=${ref_hwinv[$i]}
	    done
	    ;;
	hwinvcopy )
	    if [ -z "${hwinvcopy["hwinvidx"]+${hwinvcopy["hwinvidx"]}}" ] ; then
		hwinvcopy["hwinvidx"]="" #start the array
	    else	
		for i in ${hwinvcopy["hwinvidx"]} ; do
		    unset hwinvcopy[$i]
		done
		hwinvcopy["hwinvidx"]="" #restart the array
	    fi
	    hwinvcopy["hwinvidx"]=${ref_hwinv["hwinvidx"]} 
	    for i in ${ref_hwinv["hwinvidx"]} ; do
		hwinvcopy[$i]=${ref_hwinv[$i]}
	    done
	    ;;
	* ) 
	    printf "\n%s\n" "Script error illegal output array |$output|"
	    printf "Where %s:%s called from %s\n" $FUNCNAME $LINENO "$(caller)"
	    exit 1
    esac
Dan Reading's avatar
Dan Reading committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
}

# copy assoctive array hwinv into hwinvcopy
# this is a little stupid but since I can't pass array I use globals
copytmcinfo () {
    # initalize array
    if [ -z "${hwinvcopy["hwinvidx"]+${hwinvcopy["hwinvidx"]}}" ] ; then
	hwinvcopy["hwinvidx"]="" #start the array
    else	
	for i in ${hwinvcopy["hwinvidx"]} ; do
	    unset hwinvcopy[$i]
	done
	hwinvcopy["hwinvidx"]="" #restart the array
    fi
    # copy index from old array
    hwinvcopy["hwinvidx"]=${hwinv["hwinvidx"]} 
    for i in ${hwinv["hwinvidx"]} ; do
	hwinvcopy[$i]=${hwinv[$i]}
    done
}

# compare arrays hwinv and copyhwinv arg1=outputfile
comparetmcinfo() {
    local fileout=$1
    # need to handle differing order with disks and nic addresses
    local localidx="${hwinv["hwinvidx"]}"
    local tbdbidx="${hwinvcopy["hwinvidx"]}"
    local localnics="" tbdbnics="" netunit=""
    local -i a b
    local x addr

282
283
284
    rm -f ${fileout} ${fileout}_local ${fileout}_tbdb ${fileout}_local_pre ${fileout}_tbdb_pre
    compareunits NET ${fileout}_local ${fileout}_tbdb
    compareunits DISK ${fileout}_local ${fileout}_tbdb
Dan Reading's avatar
Dan Reading committed
285

286
    # just tested, take NETUNIT out
Dan Reading's avatar
Dan Reading committed
287
288
289
290
291
    localidx=${localidx//NETUNIT[[:digit:]][[:digit:]]/}
    localidx=${localidx//NETUNIT[[:digit:]]/}
    tbdbidx=${tbdbidx//NETUNIT[[:digit:]][[:digit:]]/}
    tbdbidx=${tbdbidx//NETUNIT[[:digit:]]/}

292
    # just tested, take DISKUNIT out
Dan Reading's avatar
Dan Reading committed
293
294
295
296
297
    localidx=${localidx//DISKUNIT[[:digit:]][[:digit:]]/}
    localidx=${localidx//DISKUNIT[[:digit:]]/}
    tbdbidx=${tbdbidx//DISKUNIT[[:digit:]][[:digit:]]/}
    tbdbidx=${tbdbidx//DISKUNIT[[:digit:]]/}

298
299
300
301
    # take the TESTINFO line out 
    localidx=${localidx//TESTINFO/}
    tbdbidx=${localidx//TESTINFO/}

302
    # contact the two indexs then find get the uniq union
Dan Reading's avatar
Dan Reading committed
303
304
305
306
307
308
309
310
    arrayidx="$localidx $tbdbidx"
    arrayidx=$(uniqstr $arrayidx)

    # step through the local index, looking only for one copy
    for i in ${localidx} ; do
	# following bash syntax: "${a+$a}" says use $a if exists else use nothing
	if [ -z "${hwinvcopy[$i]+${hwinvcopy[$i]}}" ] ; then
	    # localidx has it - hwinvcopy does not
311
	    printf "%s\n" "${hwinv[$i]}" >> ${fileout}_local_pre
Dan Reading's avatar
Dan Reading committed
312
313
314
315
316
	    arrayidx=${arrayidx/$i} # nothing to compare with
	fi
    done
    # step through the testbed index, looking only for one copy
    for i in ${tbdbidx} ; do
317
	# following bash syntax: "${a+$a}" says use $a if exists else use nothing
Dan Reading's avatar
Dan Reading committed
318
	if [ -z "${hwinv[$i]+${hwinv[$i]}}" ] ; then
319
	    printf "%s\n" "${hwinvcopy[$i]}"  >> ${fileout}_tbdb_pre
Dan Reading's avatar
Dan Reading committed
320
321
322
	    arrayidx=${arrayidx/$i} 
	fi
    done
323
324

    #compare whats left
Dan Reading's avatar
Dan Reading committed
325
326
    for i in $arrayidx ; do
	if [ "${hwinv[$i]}" != "${hwinvcopy[$i]}" ] ; then
327
	    if [ ! -f $fileout ] ; then
328
		echo "Differences found locally compared with testbed database" >> $fileout
329
	    fi
Dan Reading's avatar
Dan Reading committed
330
331
332
333
334
335
	    echo "$i does not match" >> $fileout
	    echo "local ${hwinv[$i]}" >> $fileout
	    echo "tbdb ${hwinvcopy[$i]}" >> $fileout
	fi
    done

336
    if [ -f ${fileout}_local -o -f ${fileout}_local_pre ] ; then
Dan Reading's avatar
Dan Reading committed
337
	printf "Only found in local search and not in testbed database\n" >> $fileout
338
339
340
341
	[[ -f ${fileout}_local_pre ]] && cat ${fileout}_local_pre >> ${fileout}
	[[ -f ${fileout}_local ]] && cat ${fileout}_local >> ${fileout}	
    fi
    if [ -f ${fileout}_tbdb -o -f ${fileout}_tbdb_pre ] ; then
Dan Reading's avatar
Dan Reading committed
342
	printf "In testbed database but not found in local search\n" >> $fileout
343
344
345
346
347
	[[ -f ${fileout}_tbdb_pre ]] && cat ${fileout}_tbdb_pre >> ${fileout}
	[[ -f ${fileout}_tbdb ]] && cat ${fileout}_tbdb >> ${fileout}
    fi

    rm -f ${fileout}_local ${fileout}_tbdb ${fileout}_local_pre ${fileout}_tbdb_pre
Dan Reading's avatar
Dan Reading committed
348
349
350
351

    return 0
}

352
# Compare multi-line units arg1=unittype arg2=localonlyfile arg3=tbdbonlyfile
Dan Reading's avatar
Dan Reading committed
353
354
355
356
357
358
359
compareunits() {
    local unittype=$1
    local localonly=$2
    local tbdbonly=$3
    local localidx="${hwinv["hwinvidx"]}"
    local tbdbidx="${hwinvcopy["hwinvidx"]}"
    local localunits="" tbdbunits="" devunit=""
360
    local -i a b disregard_order
361
    local x addr ckaddr
Dan Reading's avatar
Dan Reading committed
362

363
    # How are things different between unit types, only NET and DISK right now
Dan Reading's avatar
Dan Reading committed
364
365
366
367
368
369
370
371
    case $unittype in
	NET )
	    unitinfoidx_str="NETINFO"
	    unitinfo_strip="NETINFO UNITS="
	    unit_str="NETUNIT"
	    unit_pre_strip="*ID=\""
	    unit_post_strip="\"*"
	    unit_human_output="NIC"
372
	    unit_human_case="lower"
373
	    disregard_order=1
Dan Reading's avatar
Dan Reading committed
374
375
376
377
378
379
380
381
	    ;;
	DISK )
	    unitinfoidx_str="DISKINFO"
	    unitinfo_strip="DISKINFO UNITS="
	    unit_str="DISKUNIT"
	    unit_pre_strip="*SN=\""
	    unit_post_strip="\"*"
	    unit_human_output="DISK"
382
	    unit_human_case="upper"
383
	    disregard_order=0
Dan Reading's avatar
Dan Reading committed
384
385
386
387
388
389
390
	    ;;
	* )
	    echo "Error in compareunits don't now type $unittype. Giving up."
	    exit 1
	    ;;
    esac

391
    # Find the number of units in each list use biggest number for compare test
Dan Reading's avatar
Dan Reading committed
392
393
    if [ -n "${hwinv["${unitinfoidx_str}"]+${hwinv["${unitinfoidx_str}"]}}" ] ; then
	    x=${hwinv["${unitinfoidx_str}"]}
394
	    localcnt=${x/${unitinfo_strip}}
Dan Reading's avatar
Dan Reading committed
395
    else
396
	localcnt=0
Dan Reading's avatar
Dan Reading committed
397
398
399
    fi
    if [ -n "${hwinvcopy["${unitinfoidx_str}"]+${hwinvcopy["${unitinfoidx_str}"]}}" ] ; then
	x=${hwinvcopy["${unitinfoidx_str}"]}
400
	tbdbcnt=${x/${unitinfo_strip}}
Dan Reading's avatar
Dan Reading committed
401
    else
402
	tbdbcnt=0
Dan Reading's avatar
Dan Reading committed
403
    fi
404
    [[ $localcnt > $tbdbcnt ]] && maxunits=$localcnt || maxunits=$tbdbcnt 
405
406

    # here we are pulling out just the address/serialnumber from each array and saving it in a list
Dan Reading's avatar
Dan Reading committed
407
408
409
410
411
    for ((i=0; i<$maxunits; i++)) ; do
	# gather just the units addresses 
	devunit="${unit_str}${i}"
        # following bash syntax: "${a+$a}" says use $a if exists else use nothing
	if [ -n "${hwinv[$devunit]+${hwinv[$devunit]}}" ] ; then
412
413
414
415
416
417
418
419
420
	    ckaddr=${hwinv[$devunit]}
	    # add just the address/serialnumber
	    addr=${ckaddr#${unit_pre_strip}}
	    if [ "$ckaddr" != "$addr" ] ; then
		# make sure that we have removed the pre_strip
		# -- for example if a disk serial number is not removed cause we don't have it then we don't have anything to compare
		addr=${addr%%${unit_post_strip}}
		localunits+="$addr "
	    fi
Dan Reading's avatar
Dan Reading committed
421
422
423
	    localidx=${localidx/$devunit}
	fi
	if [ -n "${hwinvcopy[$devunit]+${hwinvcopy[$devunit]}}" ] ; then
424
425
426
427
428
429
	    ckaddr=${hwinvcopy[$devunit]}
	    addr=${ckaddr#${unit_pre_strip}}
	    if [ "$ckaddr" != "$addr" ] ; then
		addr=${addr%%${unit_post_strip}}
		tbdbunits+="$addr "
	    fi
Dan Reading's avatar
Dan Reading committed
430
431
432
433
	    tbdbidx=${tbdbidx/$devunit}
	fi
    done

434
435
436
437
438
439
440
441
442
    # Adjust the case in both strings to the case we want
    if [ "$unit_human_case" == "upper" ] ; then
	localunits=${localunits^^}
	tbdbunits=${tbdbunits^^}
    else
	localunits=${localunits,,}
	tbdbunits=${tbdbunits,,}
    fi

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    if (( $disregard_order )) ; then
        # remove from both lists all words matched in the other list
        # any thing left-over is non-matching
	x=$localunits
	for i in $x ; do
	    if [ "${tbdbunits/$i}" != "${tbdbunits}" ]; then
	    # same, take it out of both lists
		tbdbunits=${tbdbunits/$i}
		localunits=${localunits/$i}
	    fi
	done
        # same but swap arrays
	x=$tbdbunits
	for i in $x ; do
	    if [ "${localunits/$i}" != "${localunits}" ]; then
		localunits=${localunits/$i}
		tbdbunits=${tbdbunits/$i}
	    fi
	done
462
463
464
465
466
467
468
#    elif [ $localcnt -eq $tbdbcnt ] ; then
#	# care about order, just remove in order matching entries
#	for i in $localcnt ; do  # does not matter which cnt used the are the same
#	    
#	done
    # else 
	# not the same number of devices. Don't match, do nothing.
469
    fi
Dan Reading's avatar
Dan Reading committed
470

Dan Reading's avatar
Dan Reading committed
471
    #remove extra spaces
472
473
    save_e
    set +e
Dan Reading's avatar
Dan Reading committed
474
    read -rd '' tbdbunits <<< "$tbdbunits"
475
476
    read -rd '' localunits <<< "$localunits"
    restore_e
Dan Reading's avatar
Dan Reading committed
477

478
    # if the two strings are the same then zero strings
479

480
    if (( $disregard_order )) ; then
481
482
	# early code removed all matching strings from the two lists
        # any leftover strings would be mismatches in ether localunits or tbdbunits
483
484
485
486
487
488
489
	if [ -n "${localunits}" ]; then
	    printf "%s%s %s\n" "${unit_human_output}" "s:" "$localunits"  >> $localonly
	fi
	if [ -n "${tbdbunits}" ]; then
	    printf "%s%s %s\n" "${unit_human_output}" "s:" "$tbdbunits" >> $tbdbonly
	fi
    else
490
	# care about order, first see if the two lists are the same.
491
	if [ "$tbdbunits" != "$localunits" ] ; then
Dan Reading's avatar
Dan Reading committed
492
	    # and we care about order report it
493
	    # NOTE: if !local or !tb then not out of order, extra local or in tb
494
	    [[ -z "${offline-}" ]] && declare -i offline=0 # if set from gen_sql
495
	    if [ "${localunits}" -a "${tbdbunits}" ] ; then
496
497
		(( ! $offline )) && printf "ERROR %s: OUT OF ORDER found %s from tbdb %s\n" "${unit_human_output}" "$localunits" "$tbdbunits" 
		(( ! $offline )) && ( printf "ERROR %s: OUT OF ORDER found local[%s] info from tbdb[%s]\n" "${unit_human_output}" "$localunits" "$tbdbunits" >> $fileout ) || ( printf "WARNING %s: ORDER ['%s'] compared to ['%s']\n" "${unit_human_output}" "$localunits" "$tbdbunits" >> $fileout )
Dan Reading's avatar
Dan Reading committed
498
	    else
Dan Reading's avatar
Dan Reading committed
499
500
501
502
503
504
		if [ -n "${localunits}" ]; then
		    printf "%s%s %s\n" "${unit_human_output}" "s:" "$localunits"  >> $localonly
		fi
		if [ -n "${tbdbunits}" ]; then
		    printf "%s%s %s\n" "${unit_human_output}" "s:" "$tbdbunits" >> $tbdbonly
		fi
Dan Reading's avatar
Dan Reading committed
505
506
		(( ! $offline )) && printf "ERROR %s MISSING found %s from tbdb %s\n" "${unit_human_output}" "s:" "$localunits" "$tbdbunits" 
		(( ! $offline )) && ( printf "ERROR %s MISSING found %s from tbdb %s\n" "${unit_human_output}" "$localunits" "$tbdbunits" >> $fileout ) || ( printf "WARNING MISSING %s%s ORDER '%s' compared to '%s'\n" "${unit_human_output}" "s:" "$localunits" "$tbdbunits" >> $fileout )
Dan Reading's avatar
Dan Reading committed
507
	    fi
508
	fi
Dan Reading's avatar
Dan Reading committed
509
510
    fi

511
    return 0
Dan Reading's avatar
Dan Reading committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
}


# take a string make the words in it uniq
uniqstr() {
    local instr="$@"
    local outstr=""
    for i in $instr ; do
	if [ "${outstr/$i}" ==  "$outstr" ] ; then
	    # $i not in outstr, add it
	    outstr+="$i "
	fi
    done
    echo $outstr
}

# print only the testbed data table
printtmcinfo() {
    local -i hdunits=0 nicunits=0
    for i in ${hwinv["hwinvidx"]} ; do
	case $i in 
	    CPUINFO ) printf "%s\n" "${hwinv[$i]}" ;;
	    MEMINFO ) printf "%s\n" "${hwinv[$i]}" ;;
	    DISKINFO ) 
		printf "%s\n" "${hwinv[$i]}" 
		x=${hwinv[$i]}
		hdunits=${x/#DISKINFO UNITS=/}

		# for HD need also check that we have a valid value
		# we collect more info then the testbed data base wants
		for ((n=0; n<$hdunits; n++)) ; do
		    # grab diskunitline
                    s=${hwinv[DISKUNIT$n]}
		    # turn space seperated string into array
		    unset -v d ; declare -a d=(${s// / })
		    numelm=${#d[*]}
		    echo -n "${d[0]} " #that is the word DISKUNIT
		    
		    for ((elm=1; elm<$numelm; elm++)) ; do
		        # must have form obj=value (where val can be blank) to work
			objval=${d[$elm]}
			[[ -z $objval ]] && continue  # that's bad no tupil
			obj=${objval%%=*}
			val=${objval##*=}
			[[ -z $val ]] && continue # bad also no value (or empty string)
557
558
559
560
561
			u=$val # orignal value
			[[ $u == ${u/UNKNOWN} ]] || continue # the value has the UNKNOWN value
			[[ $u == ${u/NoInfo} ]] || continue # the value has the NA
			[[ $u == ${u/LINUXNOT} ]] || continue # the value has the LinuxNot
			[[ $u == ${u/bad_} ]] || continue # has on of the bad_* strings
Dan Reading's avatar
Dan Reading committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
		        # out put the stuff the database wants
		        # skip the stuff the database does not want
			case $obj in
			    SN | TYPE | SECSIZE | SECTORS | WSPEED | RSPEED )
				echo -n "$objval " ;;
			esac
		    done
		    echo "" # end the line
		done
		;;
            NETINFO ) printf "%s\n" "${hwinv[$i]}" 
		x=${hwinv[$i]}
		nicunits=${x/#NETINFO UNITS=/}
		for ((i=0; i<$nicunits; i++)); do printf "%s\n" "${hwinv[NETUNIT$i]}"; done ;;
	esac
    done
}

# print all hwinv
printhwinv() {
    for i in ${hwinv["hwinvidx"]} ; do
	printf "%s\n" "${hwinv[$i]}"
    done
}

# which is not in busybox and not a bash builtin
which() {
589
590
591
592
593
594
595
596
597
598
599
600
601
602
    if [ -x /usr/bin/which ] ; then
	# have real which, use it
	/usr/bin/which $@
	return 0
    else
	mypath=$PATH
	mypath=${mypath//:/ }
	for i in $mypath ; do
	    if [ -e $i/$1 ] ; then
		echo $i/$1
		return 0
	    fi
	done
    fi
Dan Reading's avatar
Dan Reading committed
603
604
605
606
607
608
609
610
}

inithostname() {
    os=$(uname -s)
    if [ -z $os ] ; then
	echo "ERROR uname messed up"
	exit 1
    fi
611
    osrel=`uname -r | sed 's/^\([0-9][0-9]*\)\..*/\1/'`
612
613
    if [ -e "$BINDIR/tmcc" ] ; then
	host=$($BINDIR/tmcc nodeid)
Dan Reading's avatar
Dan Reading committed
614
    else
615
	echo "WARN no $BINDIR/tmcc command for nodeid"
Dan Reading's avatar
Dan Reading committed
616
	# maybe its just time to give up
617
618
619
620
621
622
	if [ -e "${BOOTDIR}/realname" ] ; then
	    host=$(cat $BOOTDIR/realname)
	elif [ -e "$BOOTDIR/nodeid" ] ; then
	    host=$(cat $BOOTDIR/nodeid)
	else
	    host=$(hostname)
Dan Reading's avatar
Dan Reading committed
623
624
625
626
627
628
629
	fi
    fi
    return 0
}

findSmartctl() {
    local findit=$(which smartctl)
630
631
632
633
634
635
636
    if [ "$os" == "FreeBSD" ] ; then
	if [ "$osrel" == "9" ] ; then
	    findit=$(which smartctl9)
	elif [ "$osrel" == "10" ] ; then
	    findit=$(which smartctl10)
	fi
    fi
Dan Reading's avatar
Dan Reading committed
637
638
639
640
641
642
643
    if [ -z "${findit}" ] ; then
	if [ -x "/usr/sbin/smartctl" ]; then
	    findit="/usr/sbin/smartctl"
	else
	    findit=$NOSM
	fi
    fi
Dan Reading's avatar
Dan Reading committed
644

Dan Reading's avatar
Dan Reading committed
645
646
647
    echo $findit
    return 0
}
648
findSmartctl_getopt() {
649
650
651
652
653
    local smrtctl=$(findSmartctl)
    if [ "${smrtctl/smartctl}" != "$smrtctl" ] ; then
        # check functionally
	x=$($smrtctl --get | grep REQUIRED)
	[[ -z "$x" ]] && smrtctl=$NOSM
654
    fi
655
656
    
    echo $smrtctl
657
658
    return 0
}
Dan Reading's avatar
Dan Reading committed
659

Dan Reading's avatar
Dan Reading committed
660
661
# Array of command to be run at exit time
on_exit() {
662
#  (( $DEBUG )) && echo "EXIT on_exit $(caller)"
Dan Reading's avatar
Dan Reading committed
663
664
665
666
667
    for i in "${todo_exit[@]}" ; do
        $($i)
    done
    return 0
}
668

Dan Reading's avatar
Dan Reading committed
669
add_on_exit() {
670
    local -i nex=${#todo_exit[*]}
Dan Reading's avatar
Dan Reading committed
671
672
673
674
675
676
677
    todo_exit[$nex]="$@"
    if [[ $nex -eq 0 ]]; then
        trap on_exit EXIT
    fi
    return 0
}

678
679
# setup logging $1 is local log file if not set default to /tmp file
# $2 is the collect file, if not set then no collection is done
Dan Reading's avatar
Dan Reading committed
680
initlogs () {
681
682
683
684
685
686
    #call only once
    if [ "${initlogdone-notdone}" != "notdone" ] ; then
    	(( $DEBUG )) && printf "Attempt to call twice %s:%s called from %s\n" $FUNCNAME $LINENO "$(caller)"
	return 0
    fi

Dan Reading's avatar
Dan Reading committed
687
    # the following bash syntax lets us test ifa  positional arg is set
688
689
    # before we try and use it
    # needed if running with -u set. 
Dan Reading's avatar
Dan Reading committed
690
    # It means use $1 if set else use a default path
Dan Reading's avatar
Dan Reading committed
691
    logfile=${1-"/tmp/nodecheck.log"}
Dan Reading's avatar
Dan Reading committed
692

693
694
695
    # need to have inittestinfo run, help programmer out
    [[ "${collect_flag-undef}" = "undef" ]] && inittestinfo

Dan Reading's avatar
Dan Reading committed
696
    # this file is only used in gather mode
697
    # set the name so it can be tested for
698
    (( $collect_flag )) && { cat /dev/null > $logfile4tb ; add_on_exit "rm -f $logfile4tb" ; }
699

700
    cat /dev/null > ${tmplog} # create and truncate
Dan Reading's avatar
Dan Reading committed
701
702
    add_on_exit "rm -f $tmplog"

703
    cp /dev/null ${logout} # make it exist
Dan Reading's avatar
Dan Reading committed
704
    add_on_exit "rm -f $logout"
705
706
    
    cp /dev/null ${tmpout}
Dan Reading's avatar
Dan Reading committed
707
708
    add_on_exit "rm -f $tmpout"

709
710
    initlogdone="done"
    export initlogdone
Dan Reading's avatar
Dan Reading committed
711
712
713
    return 0
}

714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
inittestinfo () {
    local testinfo
    #call only once
    if [ "${inittestdone-notdone}" != "notdone" ] ; then
    	(( $DEBUG )) && printf "Attempt to call twice %s:%s called from %s\n" $FUNCNAME $LINENO "$(caller)"
	return 0
    fi

    # if tmccinfo array not set then read it in
    [[ -z "${tmccinfo["hwinvidx"]+${tmccinfo["hwinvidx"]}}" ]] && readtmcinfo tmcc tmccinfo
    testinfo=${tmccinfo["TESTINFO"]}
    collect_flag=$(echo $testinfo | awk -F = '{print $3}' | awk '{print $1}')
    check_flag=$(echo $testinfo | awk -F = '{print $4}')
    (( $collect_flag )) && projdir=$(echo $testinfo | awk -F \" '{print $2}') || projdir=""
    [[ "${projdir:0:1}" != '/' ]] && ( printf "%s():collect is set but invaild path given |%s|" $FUNCNAME $projdir ;  exit 1 )

    inittestdone="done"
    export inittestdone
    return 0
}
    

Dan Reading's avatar
Dan Reading committed
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
getdrivenames() {
    # use smartctl if exits
    # use scan of disk devices
    # use / 
    # truncate all together and then make uniq

    local os=$(uname -s)
    local sm=$(findSmartctl)
    local drivelist=""
    local drives="" 
    local x elm

#    if [ "$sm" != "${sm/smartctl}" ] ; then
#	x=$($sm --scan-open | awk '{print $1}')
#	if [ "$x" != "${x/dev}" ] ; then
#	    for elm in $x ; do
#		x=${x/"/dev/pass2"/} # FreeBSD not a HD, Tape?
#		drivelist+="$x "
#	    done
#	fi
#    fi


    case $os in
	Linux )
	    list="a b c d e f g h i j k l m n o p"
	    for i in $list
	    do
		if [ -b /dev/sd${i} ] ; then
		    drivelist+="/dev/sd${i} "
		fi
	    done
	    ;;
	FreeBSD )
	    list="0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	    for i in $list
	    do
Dan Reading's avatar
Dan Reading committed
773
774
		[[ ! -L /dev/ad${i} ]] &&  [[ -c /dev/ad${i} ]] && drivelist+="/dev/ad${i} "
		[[ -c /dev/ada${i} ]] && drivelist+="/dev/ada${i} "
Dan Reading's avatar
Dan Reading committed
775
		[[ -c /dev/ad${i} ]] && drivelist+="/dev/ad${i} " 
776
777
778
		[[ -c /dev/da${i} ]] && drivelist+="/dev/da${i} " 
		[[ -c /dev/ar${i} ]] && drivelist+="/dev/ar${i} " 
		[[ -c /dev/aacd${i} ]] && drivelist+="/dev/aacd${i} " 
Dan Reading's avatar
Dan Reading committed
779
780
		[[ -c /dev/amrd${i} ]] && drivelist+="/dev/amrd${i} " 
		[[ -c /dev/mfid${i} ]] && drivelist+="/dev/mfid${i} " 
781
		[[ -c /dev/mfisyspd${i} ]] && drivelist+="/dev/mfisyspd${i} " 
Dan Reading's avatar
Dan Reading committed
782
783
784
785
786
787
788
	    done
	    ;;
	* )
	    echo "${FUNCNAME[0]}:${LINENO} Internal error"
	    exit
	    ;;
    esac
Dan Reading's avatar
Dan Reading committed
789

Dan Reading's avatar
Dan Reading committed
790
791
792
793
    echo $drivelist
    return 0
}

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# return the requested hwinfo 
# $1 is the type
getfromtb() {
    local info=$1
    local -i units=0
    
    case $info in
	TESTINFO | CPUINFO )
            # make sure that tmccinfo does have the info requested.
	    [[ -z "${tmccinfo[$info]+${tmccinfo[$info]}}" ]] && return 0
	    # take off the info
	    s=${tmccinfo[$info]}
	    s=${s/$info }
	    printf "%s" "$s"
	    ;;
	NETINFO | DISKINFO )
	    [[ -z "${tmccinfo[$info]+${tmccinfo[$info]}}" ]] && return 0
	    s=${tmccinfo[$info]}
	    s=${s//=/}
	    s=${s/UNITS}
	    s=${s/$info }
	    printf "%s" "$s"
	    ;;
	MEMINFO )
	    [[ -z "${tmccinfo[$info]+${tmccinfo[$info]}}" ]] && return 0
	    s=${tmccinfo[$info]}
	    s=${s/$info SIZE=}
	     printf "%s" "$s"
	     ;;
	DISKUNIT )
	    [[ -z "${tmccinfo[${info}0]+${tmccinfo[${info}0]}}" ]] && return 0
	    # only returning serial numbers
	    x=${tmccinfo["DISKINFO"]}
	    units=${x/#DISKINFO UNITS=/}
	    for ((n=0; n<$units; n++)) ; do
		s=${tmccinfo[DISKUNIT$n]}
		    # turn space seperated string into array
		unset -v d ; declare -a d=(${s// / })
		numelm=${#d[*]}
		for ((elm=1; elm<$numelm; elm++)) ; do
		    objval=${d[$elm]}
		    [[ -z $objval ]] && continue  # that's bad no tupil
		    obj=${objval%%=*}
		    val=${objval##*=}
		    [[ -z $val ]] && continue # bad also no value (or empty s
		    if [ "$obj" = "SN" ] ;  then
			val=${val//=/}
			val=${val//\"/}
			printf "%s " "$val"
		    fi
		done
	    done
	    ;;
	NETUNIT )
	    [[ -z "${tmccinfo[${info}0]+${tmccinfo[${info}0]}}" ]] && return 0
	    # only return ID
	    x=${tmccinfo["NETINFO"]}
	    units=${x/#NETINFO UNITS=/}
	    for ((i=0; i<$units; i++)) ; do
		s=${tmccinfo[NETUNIT$i]}
		unset -v d ; declare -a d=(${s// / })
		numelm=${#d[*]}
		for ((elm=1; elm<$numelm; elm++)) ; do
		    objval=${d[$elm]}
		    [[ -z $objval ]] && continue  # that's bad no tupil
		    obj=${objval%%=*}
		    val=${objval##*=}
		    [[ -z $val ]] && continue # bad also no value (or empty s
		    if [ "$obj" = "ID" ] ;  then
			val=${val//=/}
			val=${val//\"/}
			printf "%s " "$val"
		    fi
		done
	    done
	    ;;
	* ) printf "ibinfo what is this in my case statment |%s|\n" "$info" ; exit 1
	    ;;
    esac
    return 0
}

Dan Reading's avatar
Dan Reading committed
876
877
878
879
880
whichdd2use() {
    local usetdd
    local canwe
    local -i bad64=0
    
881

Dan Reading's avatar
Dan Reading committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
    workhorsedd=$(which dd)
    canwe=$(ls -l $workhorsedd | grep busybox)
    [[ $canwe ]] && { echo "bad_busybox_dd"; return 0; }

    # if we have a timed dd, use a timeout rather than a count
    usetdd=$(which tdd)
    [[ -x $usetdd ]] && { USE_DD=$usetdd; TDD_DD=$workhorsedd; usedd=$usetdd; }
    # check compatabily, ok to have 32bit on 64bit machine
    if [ $native_bitsize -ne 64 ] ; then
	# check directory name of where the exectuable is installed
	# if not 32 or not 64 then assume native binary and is ok
	if [ "$workhorsedd" != "${workhorsedd/64}" ] ; then # it has 64 in path
	    bad64=1
	fi
	# and check tdd if we are using it
	if [ "${USE_DD}" != "${USE_DD/tdd}" ] ; then
	    if [ "$USE_DD" != "${USE_DD/64}" ] ; then
		bad64=1
	    fi
	fi
	if [ $bad64 -eq 1 ] ; then
	    echo "bad_64bit_dd_on_32bit_machine"
	    return 0
	fi
    fi
    echo "$usedd"

    return 0
}

# call whichdd2use() before this to use correctly
ddargs() {
    local args=""
    if [ "$os" == "Linux" ] ; then 
	#args="bs=64k iflag=direct count=8000"
	#note iflag direct can't be used with /dev/zero as infile 
	args="bs=64k"
    elif [ "$os" == "FreeBSD" ] ; then 
	args="bs=64k"
    fi
922

Dan Reading's avatar
Dan Reading committed
923
924
925
926
927
928
929
930
931
932
    if [ "$USE_DD" != "${USE_DD/tdd}" ] ; then
	# if we have a timed dd, use a timeout rather than a count
	args="$args timeout=5 count=20000" # XXX paranoid: leave a count just in case
    else
	args="$args count=8000"
    fi
	
    echo "$args"
    return 0
}
933

Dan Reading's avatar
Dan Reading committed
934
935
# The timesys function terminates its script unless it terminates earlier on its own
# args: max_time output_file command command_args
936
# does not work....
Dan Reading's avatar
Dan Reading committed
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
timesys() {
    maxtime="$1"; shift;
    out="$1" ; shift;
    command="$1"; shift;
    args="$*"
    me_pid=$$;
    sleep $maxtime &
    timer_pid=$!
{
    $command $args &
    command_pid=$!
    wait ${timer_pid}
    siglist="2 15 9"
    for i in $siglist ; do
	running=$(ps -a | grep $command_pid | grep dd)
	[[ "$running" ]] || break
	kill -${i} ${command_pid}
    done
} > $out 2>&1
}
957

958
return 0