gen_sql 11.3 KB
Newer Older
1 2 3 4 5
#! /usr/local/bin/bash

source checkutils.sh


6 7 8 9 10 11 12
checkdrift_main() {
    set -u
    projdir=/proj/emulab-ops/nodecheck
    cd $projdir
    if (( $# )) ; then
	allnodes="$@"
    else
13
	allnodes=$(ls -d pc* pg* dbox* gpu* 2> /dev/null)
14
    fi
Dan Reading's avatar
Dan Reading committed
15

16 17 18 19 20 21 22 23 24 25 26 27
    for host in $allnodes ; do
	if [ -d $projdir/$host/.tbdb ] ; then
	    cd $projdir/$host/.tbdb
	    newest=$(ls -t | head -1)
	    allchecks=$(ls -t)
	    readtmcinfo $newest hwinvcopy
	    declare -i countsame=-1
	    for tocheck in $allchecks ; do
		readtmcinfo $tocheck hwinv
		# note: will check against self for sanity
		comparetmcinfo /tmp/.$$checkdiff
		if [ -s /tmp/.$$checkdiff ] ; then
28
		    echo ""
29
		    echo "$host $newest $tocheck are not the same"
30 31 32 33 34 35
		    echo "====================================================="
		    # reset compar file
		    echo "diff $tocheck $newest"
		    diff $tocheck $newest
		    newest=$tocheck
		    readtmcinfo $newest hwinvcopy
36 37 38 39 40 41 42
		    # XXX - start
		    # XXX Do not enable this code unless you want
		    # the files moved out of the way
#		    [[ ! -d $projdir/$host/.tbdb/.notsame ]] && sudo mkdir -p $projdir/$host/.tbdb/.notsame 
#		    sudo mv $tocheck $projdir/$host/.tbdb/.notsame
		    # XXX - end
		else
43
# 		    echo "$host $newest $tocheck SAME"
44 45 46 47
		    ((countsame++))
		fi
		rm -f /tmp/.$$checkdiff
	    done
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
: #	    echo "$host $countsame inventory files are the same"
	else
	    echo "No record of node id \"$host\"."
	fi
    done
}
checkwce_main() {
    set -u
    projdir=/proj/emulab-ops/nodecheck
    cd $projdir
    if (( $# )) ; then
	allnodes="$@"
    else
	allnodes=$(ls -d pc* pg* dbox* gpu* 2> /dev/null)
    fi

    for host in $allnodes ; do
	if [ -f $projdir/$host/full ] ; then

	#    wcestate=$(grep $projdir/$host/full$)
	    readtmcinfo $projdir/$host/full hwinv
	    numberofdrives=${hwinv["DISKINFO"]}
	    numberofdrives=${numberofdrives/DISKINFO UNITS=/}
	    
            for ((idx=0; idx<${numberofdrives}; idx++)) ; do
		unset -v d ; declare -a d=(${hwinv["DISKUNIT$idx"]})
                wce=${d[2]}
		if [ "$wce" != "${wce/enabled}" ] ; then
		    :
		elif [ "$wce" != "${wce/disabled}" ] ; then
		    x=${d[1]}
		    x=${x##*SN=}; x=${x%% *}; x=${x//\"/}
		    echo "$host ${d[8]} Write Cache Disabled bsidx=$(get_bsidx_from_sn $x)"
		else
		    :
		fi
            done

86 87 88 89 90 91
	else
	    echo "No record of node id \"$host\"."
	fi
    done
}

Dan Reading's avatar
Dan Reading committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#   README
#   to get HD info from serial number
# select b.node_id,b.bs_id,b.bsidx,a.attrvalue from blockstores as b,blockstore_attributes as a where b.bsidx=a.bsidx and b.role='element' and a.attrkey='serialnum' and a.attrvalue='$sn';
#   to get HD info from node_id
# select b.node_id,b.bs_id,b.bsidx,a.attrvalue from blockstores as b,blockstore_attributes as a where b.bsidx=a.bsidx and b.role='element' and a.attrkey='serialnum' and b.node_id='pc510';
#
#   to get bsidx from serial number
# db_bsidx=$(mysql -B -N -e "select b.bsidx from blockstores as b,blockstore_attributes as a where b.bsidx=a.bsidx and b.role='element' and a.attrkey='serialnum' and a.attrvalue='$sn';" tbdb)
#   to get serial number from bsidx
# db_sn=$(mysql -B -N -e "select a.attrvalue from blockstores as b,blockstore_attributes as a where b.bsidx=a.bsidx and b.role='element' and a.attrkey='serialnum' and b.bsidx=$bsidx;" tbdb);

get_bsidx_from_sn() {
    echo $(mysql -B -N -e "select b.bsidx from blockstores as b,blockstore_attributes as a where b.bsidx=a.bsidx and b.role='element' and a.attrkey='serialnum' and a.attrvalue='$1';" tbdb)
}
get_sn_from_bsidx() {
    echo $(mysql -B -N -e "select a.attrvalue from blockstores as b,blockstore_attributes as a where b.bsidx=a.bsidx and b.role='element' and a.attrkey='serialnum' and b.bsidx=$1;" tbdb);
}
get_host_from_bsidx() {
    echo $(mysql -B -N -e "select node_id from blockstores where bsidx=$1;" tbdb);
}
get_drive_from_bsidx() {
    echo $(mysql -B -N -e "select bs_id from blockstores where bsidx=$1;" tbdb);
}
115 116 117
get_bsidx_from_hostAnddrive() {
    echo $(mysql -B -N -e "select bsidx from blockstores where node_id=\"$1\" and bs_id=\"disk${2}\";" tbdb)
}
Dan Reading's avatar
Dan Reading committed
118

119 120
gentbsql_main() {
    set -u
Dan Reading's avatar
Dan Reading committed
121
    local sn="UNK" hdtype="UNK" secsize="UNK" sectors="UNK" wspeed="UNK" rspeed="UNK"
122 123
    projdir=/proj/emulab-ops/nodecheck
    cd $projdir
124 125 126
    if (( $# )) ; then
	allnodes="$@"
    else
127
	allnodes=$(ls -d pc* pg* dbox* gpu* 2> /dev/null )
128
    fi
129

Dan Reading's avatar
Dan Reading committed
130 131 132 133
    # unique BlockStore number based on first mac address
    bsidx_base=$(mysql -B -N -e "select idx from emulab_indicies where name='next_bsidx';" tbdb)
    keep_bsidx_base=$bsidx_base
    
134
    for host in $allnodes ; do
135 136
	if [ -d $projdir/$host ] ; then
	    cd $projdir/$host
Dan Reading's avatar
Dan Reading committed
137
	    sn="UNK" hdtype="UNK" secsize="UNK" sectors="UNK" wspeed="UNK" rspeed="UNK"
138 139
#	    [[ -f "diff" ]] || { echo "# $host: No changes needed"; continue; }
	    [[ -f "diff" ]] || continue;
Dan Reading's avatar
Dan Reading committed
140
	    [[ -f "node" ]] || { echo "# $host: Missing node file"; continue; }
141
	    if [ -z "$(grep "Diff Report" diff)" ] ; then
Dan Reading's avatar
Dan Reading committed
142
		echo "# $host Inventory Report to old to use"
143 144
		continue
	    fi
145

146
	    listofdisks=$(grep 'DISKs:' diff)
147
	    listofdisks=${listofdisks//DISKs:}
Dan Reading's avatar
Dan Reading committed
148
	    hdnum=0
149 150
	    for i in $listofdisks ; do
		toadd=$(grep $i node)
151 152
		if [ -n "$toadd" ] ; then
		    toadd=${toadd/DISKUNIT}
Dan Reading's avatar
Dan Reading committed
153 154 155 156 157 158
		    if [ "${toadd/SN=}" != "$toadd" ]; then
			sn=${toadd##*SN=}
			sn=${sn%% *}
			sn=${sn//\"/}
			# check and see if serial number is already in database
			bsidx=$(get_bsidx_from_sn $sn)
159
			[[ "$bsidx" ]] && continue
Dan Reading's avatar
Dan Reading committed
160 161
		    fi

162 163
#  just do it don't check for TYPE from file
#		    if [ "${toadd/TYPE=}" != "$toadd" ]; then
Dan Reading's avatar
Dan Reading committed
164 165 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
# /* by Mike's orders
#  type		"class"	"protocol"	"rot-speed"	nodes with this type
# 
#  pata-generic	local	PATA		7200		pc600,pc850,pc2000
#  scsi-generic	local	SCSI		10000		pc3000,pc6000
#  sata-generic	local	SATA		7200		pc2400w,d710,d820,pcivy
#  sas-generic	local	SAS		10000		d820
#  ssd-generic	local	SATA		0		gpuhost,d820(pc601)
#  c2100-sas	local	SAS		15000		c2100
# */
			nodetype=$(mysql -B -N -e "select type from nodes where node_id='$host';" tbdb)
			case $nodetype in
			    pc600 | pc850 | pc2000 ) hdtype="pata-generic" ;;
			    pc3000 | pc6000 ) hdtype="scsi-generic" ;;
			    pc2400w | d710 | d820 | pcivy ) hdtype="sata-generic" ;;
			    d820 ) hdtype="sas-generic" ;;
			    gpuhost ) hdtype="ssd-generic" ;;
			    c2100 ) hdtype="c2100-sas" ;;
			    * ) 
				hdtype=${toadd##*TYPE=}
				hdtype=${hdtype%% *}
				;;
			esac
			# XXX XXX XXX special case
			if [ "$host" == "pc601" ] ; then
			    hdtype="ssd-generic"
			fi
191 192
# just do it don't check for TYPE from file
#		    fi
Dan Reading's avatar
Dan Reading committed
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 219 220 221 222 223 224
		    if [ "${toadd/SECSIZE=}" != "$toadd" ]; then
			secsize=${toadd##*SECSIZE=}
			secsize=${secsize%% *}
		    fi
		    if [ "${toadd/SECTORS=}" != "$toadd" ]; then
			sectors=${toadd##*SECTORS=}
			sectors=${sectors%% *}
		    fi
		    if [ "${toadd/RSPEED=}" != "$toadd" ]; then
			rspeed=${toadd##*RSPEED=}
			rspeed=${rspeed%% *}
		    fi
		    if [ "${toadd/WSPEED=}" != "$toadd" ]; then
			wspeed=${toadd##*WSPEED=}
			wspeed=${wspeed%% *}
		    fi
		    size=$(($sectors / 2048))
		    # check and see if bsidx already used
		    while [ "$(mysql -B -N -e "select bsidx from blockstores where bsidx=$bsidx_base;" tbdb)" ] ; do
			echo "# bsidx=$bsidx_base already used, bumping bsidx_base again"
			((++bsidx_base))
		    done
                    # check and see if drive already used
		    while [ "$(mysql -B -N -e "select bs_id from blockstores where node_id=\"$host\" and bs_id=\"disk${hdnum}\";" tbdb)" ] ; do
			echo "# drive${hdnum} already used, bumping hdnum again"
			((++hdnum))
		    done
		    printf "mysql -e \"insert into blockstores values (%d, '%s', 'disk%d', '%s', 'element', %d, 1, now());\" tbdb\n" "$bsidx_base" "$host" "$hdnum" "$hdtype" "$size" 
		    # now the second table
		    printf "mysql -e \"insert into blockstore_attributes values (%d, '%s', '%s', '%s');\" tbdb\n" "$bsidx_base" "serialnum" "$sn" "string" 
		    ((++bsidx_base))
		    ((++hdnum))
225
		else
Dan Reading's avatar
Dan Reading committed
226
		    if [ "$i" == "UNKNOWN" ] ; then
227 228 229 230 231 232 233
			echo -n "# Missing Serial Number for disk${hdnum} on $host"
			x=$(get_bsidx_from_hostAnddrive $host ${hdnum})
			if [ -n "$x" ] ; then
			    echo " but found $(get_drive_from_bsidx $x) $(get_host_from_bsidx $x) @ bsidx=$x SN=$(get_sn_from_bsidx $x)" 
			else
			    echo ""
			fi
Dan Reading's avatar
Dan Reading committed
234 235
			((++hdnum))
		    else
236
			toadd="SN='$i'"
237
			if [ "$sn" != "UNK" ] ; then
238 239 240 241 242 243 244 245 246 247 248
			    x_bsidx=$(get_bsidx_from_sn $i)
			    [[ $x_bsidx ]] && y_hostid=$(get_host_from_bsidx $x_bsidx) || y_hostid=""
			    if [ -n "$x_bsidx" -a -n "$y_hostid" ] ; then
				printf "mysql -e \"delete from blockstores where node_id='%s' and bsidx='%s';\" tbdb\n" "$host" "$x_bsidx"
				printf "mysql -e \"delete from blockstore_attributes where attrkey='%s';\" tbdb\n" "$i"
			    else
				echo "#Discovery error node_id $host"
				      # Discovery error
				      # Have only seen this when the "full" file for host lists a hard drive, including SN
				      # but the "node" file does not have serial number for for the HD
			    fi
249
			fi
Dan Reading's avatar
Dan Reading committed
250
		    fi
251
		fi
252 253
	    done
	    listofnics=$(grep 'NICs:' diff)
254
	    listofnics=${listofnics//NICs:/}
255 256
	    for i in $listofnics ; do
		toadd=$(grep $i node)
257
		if [ -n "$toadd" ] ; then
Dan Reading's avatar
Dan Reading committed
258 259 260 261
		    toadd=${toadd/*ID=\"}
		    toadd=${toadd/\"}
		    uuid=$(uuidgen)
		    printf "#BYHAND mysql -e \"insert into interfaces set node_id='%s',mac='%s',card=X,port=X,interface_type='?',iface='ethX',role='?',uuid='$uuid';\" tbdb\n" "$host" "$toadd"
262 263
		else
		    toadd="ID=\"$i\""
Dan Reading's avatar
Dan Reading committed
264
		    printf "#BYHAND mysql -e \"delete from interfaces where node_id='%s' and %s;\" tbdb\n" "$host" "$toadd"
265
		fi
266
	    done
267
	else
268
	    echo "# No record of node id \"$host\"."
269
	fi
270
    done
Dan Reading's avatar
Dan Reading committed
271 272 273
    if [ $keep_bsidx_base -ne $bsidx_base ] ; then
	printf "mysql -e \"update emulab_indicies set idx=%d where name='next_bsidx';\" tbdb\n" "$bsidx_base"
    fi
274 275 276 277 278 279 280 281 282
}

setdirstructure() {
    # start XXX
    # decided to change names again, rename if old names -- XXX remove this after all node have run the new code
    # take into consideration if old directory structure was added to to a new directory structure 
    set -u
    projdir=/proj/emulab-ops/nodecheck
    cd $projdir
283
    allnodes=$(ls -d pc* pg* dbox* gpu* 2> /dev/null)
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    dirs2workon=""
    for i in $allnodes; do [[ -L $i/$i.diff ]] && dirs2workon+="$i "; done
    owd=$PWD

    for host in $dirs2workon ; do
    	if [ -d ${projdir}/$host ] ; then
	    cd ${projdir}/$host
	    [[ -L $host.full ]] && rm $host.full
	    [[ -L $host.diff ]] && rm $host.diff
	    [[ -L $host ]] && rm $host
	    for i in full diff tbdb ; do
		if [ -d $i ] ; then
		    [[ ! -d .$i ]] && (mkdir .$i ; chmod g+x .$i)
		    list=$(ls -A $i)
		    for j in $list ; do
			mv $i/$j .$i
		    done
		    rmdir $i
		fi
	    done
	    cd $owd
	fi
    done
    
    # put links in place
    for i in $dirs2workon ; do
	cd $projdir/$i
	links="full diff node"
	for link in $links ; do
	    case $link in
		full | diff )
		    # make the symlinks
		    if [ ! -e ${link} -a -d .${link} ] ; then
			linkto=$(ls -t .${link} | head -1)
			if [ -n "$linkto" ] ; then
 			    ln -s .${link}/${linkto} $link
			fi
		    fi
		    ;;
		node )
		    if [ ! -e ${link} -a -d .tbdb ] ; then
			linkto=$(ls -t .tbdb | head -1)
			if [ -n "$linkto" ] ; then
 			    ln -s .tbdb/${linkto} $link
			    ln -s .tbdb/${linkto} $i
			fi
		    fi
		    ;;
	    esac
	done
    done
}


338
if [ "$0" != "${0/setdir}" ] ; then
339
    setdirstructure
340
elif [ "$0" != "${0/sql}" ] ; then
341
    gentbsql_main $@
342 343
elif [ "$0" != "${0/drift}" ] ; then
    checkdrift_main $@
344 345
elif [ "$0" != "wce" ] ; then
    checkwce_main $@
346 347
else
    "Print useage help"
348 349 350
fi