checkutils.sh 23.1 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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
[[ -z "${todo_exit-}" ]] && declare -a todo_exit
[[ -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?

# 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
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
    if [ -z "${BINDIR-""}" ] ; then 
	if [ -f "/etc/emulab/paths.sh" ]; then
	    source /etc/emulab/paths.sh
	else
	    export BINDIR=/usr/local/etc/emulab
	    export LOGDIR=/var/tmp
	fi
    fi
    
    if [ -f /etc/emulab/ismfs ] ; then
	mfsmode=1
    else
	mfsmode=0
    fi
Dan Reading's avatar
Dan Reading committed
81

82
83
84
    inithostname
    initlogs $@
    inittestinfo
Dan Reading's avatar
Dan Reading committed
85

86
87
    #trap 'err_report $FUNCNAME:$LINENO' ERR
    trap 'err_report $LINENO' ERR
Dan Reading's avatar
Dan Reading committed
88

89
90
91
92
    initdone="done"
    export initdone
    return 0
}
Dan Reading's avatar
Dan Reading committed
93
94

# any command causes exit if -e option set
Dan Reading's avatar
Dan Reading committed
95
96
# 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
97
98
99
100
101
102
103
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
104

Dan Reading's avatar
Dan Reading committed
105
106
# give some indication of exit on ERR trap
err_report() {
107
    echo "TRAP ERR at Caller Line $(caller)"
Dan Reading's avatar
Dan Reading committed
108
109
110
}


111
112
113
114
# read info from tmcc or a file. Copy into one of the three global arrays
# hwinv, hwinvcopy, tmccinfo
# $1 is the source $2 is the output array
# error if not both set
Dan Reading's avatar
Dan Reading committed
115
readtmcinfo() {
116
    local -A ref_hwinv
Dan Reading's avatar
Dan Reading committed
117
118
119
    local keyword
    local -i dcnt=0
    local -i ncnt=0
120
    local source output
121
    local rmtmp
Dan Reading's avatar
Dan Reading committed
122

123
    # what file to read from, if not set then make tmcc call
124
125
126
127
128
129
130
131
132
133
134
135
    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
	rmtmp="y" # remove tmp file
	source=/tmp/.$$tmcchwinv
	$($BINDIR/tmcc hwinfo > $source)
Dan Reading's avatar
Dan Reading committed
136
    else
137
	rmtmp=""
Dan Reading's avatar
Dan Reading committed
138
139
    fi

140
141
142
143
144
145
146
    # 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
147
	done
148
149
	# hwinvidx is always expected to be set
	ref_hwinv["hwinvidx"]="" #restart the array
Dan Reading's avatar
Dan Reading committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    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))
		;;
164
	    \#* ) continue ;; 
Dan Reading's avatar
Dan Reading committed
165
	esac
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	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
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
}

# 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

250
251
252
    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
253

254
    # just tested, take NETUNIT out
Dan Reading's avatar
Dan Reading committed
255
256
257
258
259
    localidx=${localidx//NETUNIT[[:digit:]][[:digit:]]/}
    localidx=${localidx//NETUNIT[[:digit:]]/}
    tbdbidx=${tbdbidx//NETUNIT[[:digit:]][[:digit:]]/}
    tbdbidx=${tbdbidx//NETUNIT[[:digit:]]/}

260
    # just tested, take DISKUNIT out
Dan Reading's avatar
Dan Reading committed
261
262
263
264
265
    localidx=${localidx//DISKUNIT[[:digit:]][[:digit:]]/}
    localidx=${localidx//DISKUNIT[[:digit:]]/}
    tbdbidx=${tbdbidx//DISKUNIT[[:digit:]][[:digit:]]/}
    tbdbidx=${tbdbidx//DISKUNIT[[:digit:]]/}

266
267
268
269
    # take the TESTINFO line out 
    localidx=${localidx//TESTINFO/}
    tbdbidx=${localidx//TESTINFO/}

270
    # contact the two indexs then find get the uniq union
Dan Reading's avatar
Dan Reading committed
271
272
273
274
275
276
277
278
    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
279
	    printf "%s\n" "${hwinv[$i]}" >> ${fileout}_local_pre
Dan Reading's avatar
Dan Reading committed
280
281
282
283
284
	    arrayidx=${arrayidx/$i} # nothing to compare with
	fi
    done
    # step through the testbed index, looking only for one copy
    for i in ${tbdbidx} ; do
285
	# following bash syntax: "${a+$a}" says use $a if exists else use nothing
Dan Reading's avatar
Dan Reading committed
286
	if [ -z "${hwinv[$i]+${hwinv[$i]}}" ] ; then
287
	    printf "%s\n" "${hwinvcopy[$i]}"  >> ${fileout}_tbdb_pre
Dan Reading's avatar
Dan Reading committed
288
289
290
	    arrayidx=${arrayidx/$i} 
	fi
    done
291
292

    #compare whats left
Dan Reading's avatar
Dan Reading committed
293
294
    for i in $arrayidx ; do
	if [ "${hwinv[$i]}" != "${hwinvcopy[$i]}" ] ; then
295
296
297
	    if [ ! -f $fileout ] ; then
		echo "Differences found locally compared with testbed database" > $fileout
	    fi
Dan Reading's avatar
Dan Reading committed
298
299
300
301
302
303
	    echo "$i does not match" >> $fileout
	    echo "local ${hwinv[$i]}" >> $fileout
	    echo "tbdb ${hwinvcopy[$i]}" >> $fileout
	fi
    done

304
305
306
307
308
309
310
311
312
313
314
315
    if [ -f ${fileout}_local -o -f ${fileout}_local_pre ] ; then
	printf "\nOnly found in local search and not in testbed database\n" >> $fileout
	[[ -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
	printf "\nIn testbed database but not found in local search\n" >> $fileout
	[[ -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
316
317
318
319

    return 0
}

320
# Compare multi-line units arg1=unittype arg2=localonlyfile arg3=tbdbonlyfile
Dan Reading's avatar
Dan Reading committed
321
322
323
324
325
326
327
328
329
330
compareunits() {
    local unittype=$1
    local localonly=$2
    local tbdbonly=$3
    local localidx="${hwinv["hwinvidx"]}"
    local tbdbidx="${hwinvcopy["hwinvidx"]}"
    local localunits="" tbdbunits="" devunit=""
    local -i a b
    local x addr

331
    # How are things different between unit types, only NET and DISK right now
Dan Reading's avatar
Dan Reading committed
332
333
334
335
336
337
338
339
    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"
340
	    unit_human_case="lower"
Dan Reading's avatar
Dan Reading committed
341
342
343
344
345
346
347
348
	    ;;
	DISK )
	    unitinfoidx_str="DISKINFO"
	    unitinfo_strip="DISKINFO UNITS="
	    unit_str="DISKUNIT"
	    unit_pre_strip="*SN=\""
	    unit_post_strip="\"*"
	    unit_human_output="DISK"
349
	    unit_human_case="upper"
Dan Reading's avatar
Dan Reading committed
350
351
352
353
354
355
356
	    ;;
	* )
	    echo "Error in compareunits don't now type $unittype. Giving up."
	    exit 1
	    ;;
    esac

357
    # Find the number of units in each list use biggest number for compare test
Dan Reading's avatar
Dan Reading committed
358
359
360
361
362
363
364
365
366
367
368
369
370
    if [ -n "${hwinv["${unitinfoidx_str}"]+${hwinv["${unitinfoidx_str}"]}}" ] ; then
	    x=${hwinv["${unitinfoidx_str}"]}
	    a=${x/${unitinfo_strip}}
    else
	a=0
    fi
    if [ -n "${hwinvcopy["${unitinfoidx_str}"]+${hwinvcopy["${unitinfoidx_str}"]}}" ] ; then
	x=${hwinvcopy["${unitinfoidx_str}"]}
	b=${x/${unitinfo_strip}}
    else
	b=0
    fi
    [[ $a > $b ]] && maxunits=$a || maxunits=$b 
371
372

    # 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
373
374
375
376
377
378
379
380
    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
	    # add just the address
	    addr=${hwinv[$devunit]}
	    addr=${addr#${unit_pre_strip}}
381
	    addr=${addr%%${unit_post_strip}}
Dan Reading's avatar
Dan Reading committed
382
383
384
385
386
387
	    localunits+="$addr "
	    localidx=${localidx/$devunit}
	fi
	if [ -n "${hwinvcopy[$devunit]+${hwinvcopy[$devunit]}}" ] ; then
	    addr=${hwinvcopy[$devunit]}
	    addr=${addr#${unit_pre_strip}}
388
	    addr=${addr%%${unit_post_strip}}
Dan Reading's avatar
Dan Reading committed
389
390
391
392
393
	    tbdbunits+="$addr "
	    tbdbidx=${tbdbidx/$devunit}
	fi
    done

394
395
396
397
398
399
400
401
402
403
404
405
    # 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

    # remove from the lists all matching words
    x=$localunits
    for i in $x ; do
Dan Reading's avatar
Dan Reading committed
406
407
408
409
410
	if [ "${tbdbunits/$i}" != "${tbdbunits}" ]; then
	    tbdbunits=${tbdbunits/$i}
	    localunits=${localunits/$i}
	fi
    done
411
412
413
    # same but swap arrays
    x=$tbdbunits
    for i in $x ; do
Dan Reading's avatar
Dan Reading committed
414
415
416
417
418
419
	if [ "${localunits/$i}" != "${localunits}" ]; then
	    localunits=${localunits/$i}
	    tbdbunits=${tbdbunits/$i}
	fi
    done
    #remove extra spaces
420
421
    save_e
    set +e
Dan Reading's avatar
Dan Reading committed
422
    read -rd '' tbdbunits <<< "$tbdbunits"
423
424
    read -rd '' localunits <<< "$localunits"
    restore_e
Dan Reading's avatar
Dan Reading committed
425

426
427

    # any mismatches would be in ether localunits or tbdbunits
Dan Reading's avatar
Dan Reading committed
428
429
430
431
432
433
434
    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

435
    return 0
Dan Reading's avatar
Dan Reading committed
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
}


# 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
}

# no args uses the globel arrays hwinvv, tcm_in, tcm_out
#mergetmcinfo() {
#    for i in ${hwinv["hwinvidx"]} ; do
#	hwinv[$i]+=" ADD"
#    done
#}

# arg $1 is the file to write uses the globel tcm_out array
#writetmcinfo() {
#:
#}


# 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)
			u=${val,,} #lower case
			[[ $u == ${u/unk} ]] || continue # the value has the UNKNOWN value
			[[ $u == ${u/na} ]] || continue # the value has the NA
			[[ $u == ${u/not} ]] || continue # the value has the LinuxNot
			[[ $u == ${u/bad} ]] || continue # the value bad_dd
		        # 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() {
526
527
528
529
530
531
532
533
534
535
536
537
538
539
    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
540
541
542
543
544
545
546
547
}

inithostname() {
    os=$(uname -s)
    if [ -z $os ] ; then
	echo "ERROR uname messed up"
	exit 1
    fi
548
549
    if [ -e "$BINDIR/tmcc" ] ; then
	host=$($BINDIR/tmcc nodeid)
Dan Reading's avatar
Dan Reading committed
550
    else
551
	echo "WARN no $BINDIR/tmcc command for nodeid"
Dan Reading's avatar
Dan Reading committed
552
	# maybe its just time to give up
553
554
555
556
557
558
	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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
	fi
    fi
    return 0
}

findSmartctl() {
    local findit=$(which smartctl)
    if [ -z "${findit}" ] ; then
	if [ -x "/usr/sbin/smartctl" ]; then
	    findit="/usr/sbin/smartctl"
	else
	    findit=$NOSM
	fi
    fi
    echo $findit
    return 0
}
576
findSmartctl_getopt() {
577
578
579
580
581
    local smrtctl=$(findSmartctl)
    if [ "${smrtctl/smartctl}" != "$smrtctl" ] ; then
        # check functionally
	x=$($smrtctl --get | grep REQUIRED)
	[[ -z "$x" ]] && smrtctl=$NOSM
582
    fi
583
584
    
    echo $smrtctl
585
586
    return 0
}
Dan Reading's avatar
Dan Reading committed
587

Dan Reading's avatar
Dan Reading committed
588
589
# Array of command to be run at exit time
on_exit() {
590
#  (( $DEBUG )) && echo "EXIT on_exit $(caller)"
Dan Reading's avatar
Dan Reading committed
591
592
593
594
595
    for i in "${todo_exit[@]}" ; do
        $($i)
    done
    return 0
}
596

Dan Reading's avatar
Dan Reading committed
597
add_on_exit() {
598
    local -i nex=${#todo_exit[*]}
Dan Reading's avatar
Dan Reading committed
599
600
601
602
603
604
605
    todo_exit[$nex]="$@"
    if [[ $nex -eq 0 ]]; then
        trap on_exit EXIT
    fi
    return 0
}

606
607
# 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
608
initlogs () {
609
610
611
612
613
614
    #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
615
    # the following bash syntax lets us test ifa  positional arg is set
616
617
    # before we try and use it
    # needed if running with -u set. 
Dan Reading's avatar
Dan Reading committed
618
    # It means use $1 if set else use a default path
Dan Reading's avatar
Dan Reading committed
619
    logfile=${1-"/tmp/nodecheck.log"}
Dan Reading's avatar
Dan Reading committed
620

621
622
623
    # need to have inittestinfo run, help programmer out
    [[ "${collect_flag-undef}" = "undef" ]] && inittestinfo

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

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

631
    cp /dev/null ${logout} # make it exist
Dan Reading's avatar
Dan Reading committed
632
    add_on_exit "rm -f $logout"
633
634
    
    cp /dev/null ${tmpout}
Dan Reading's avatar
Dan Reading committed
635
636
    add_on_exit "rm -f $tmpout"

637
638
    initlogdone="done"
    export initlogdone
Dan Reading's avatar
Dan Reading committed
639
640
641
    return 0
}

642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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
		[[ -c /dev/ad${i} ]] && drivelist+="/dev/ad${i} " 
702
703
704
		[[ -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
705
706
		[[ -c /dev/amrd${i} ]] && drivelist+="/dev/amrd${i} " 
		[[ -c /dev/mfid${i} ]] && drivelist+="/dev/mfid${i} " 
707
		[[ -c /dev/mfisyspd${i} ]] && drivelist+="/dev/mfisyspd${i} " 
Dan Reading's avatar
Dan Reading committed
708
709
710
711
712
713
714
715
716
717
718
719
	    done
	    ;;
	* )
	    echo "${FUNCNAME[0]}:${LINENO} Internal error"
	    exit
	    ;;
    esac
    
    echo $drivelist
    return 0
}

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# 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
805
806
# The timesys function terminates its script unless it terminates earlier on its own
# args: max_time output_file command command_args
807
# does not work....
Dan Reading's avatar
Dan Reading committed
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
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
}
828

829
return 0