Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 373 lines (318 sloc) 8.987 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 160 161 162 163 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 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 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 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 282 283 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
#!/bin/sh
#
# Copyright (c) 2007,2008,2009,2010,2011 Yahoo! Inc.
# All rights reserved.
#
# Originally written by Jan Schaumann <jschauma@yahoo-inc.com> in March 2007.
#
# Redistribution and use of this software in source and binary forms,
# with or without modification, are permitted provided that the following
# conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# * Neither the name of Yahoo! Inc. nor the names of its
# contributors may be used to endorse or promote products
# derived from this software without specific prior
# written permission of Yahoo! Inc.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# This script is part of the "scanmaster" suite of scripts. It is the
# primary invoking command and hence named the same. It represents the
# portion of the process that farms a scan job out to a number of scanning
# nodes.
#
# See scanmaster(1) for details.

#set -eu

###
### Globals
###

AGGREGATOR=""
AUTHMODE="pubkey"
STARTDATE=$(date +%s)
FULLDATE=$(date +%Y%m%d%H%M)
DATE=$(date +%Y%m%d)
DIR="/mnt/scanmaster"
INPUT=""
JOBS=""
NAME=""
NO_HEADLESS_FLAG=""
NUM_HOSTS=
PASSTHROUGH=""
POST=""
POSTFILE=""
PROGNAME="${0##*/}"
PRE=""
REMOTE_SCRIPT=""
WHOSTS="/usr/local/share/scanmaster/slavenodes"

# set in prescanTasks
PREFIX=""

###
### Subroutines
###

# function : checkWorkerHosts
# purpose : make sure that the entries in the WHOSTS file appear to be up
# and running
# inputs : none
# returns : nothing; creates a local worker hosts file and sets the
# WHOSTS variable to point to that

checkWorkerHosts() {
local whosts="${PREFIX}/.whosts"
rm -f ${whosts} >/dev/null 2>&1
for h in $(sed -e 's/#.*//' ${WHOSTS}); do
ping -c 1 -i 1 -t 3 -q ${h} >/dev/null 2>&1 && {
echo "${h}" >> ${whosts}
}
done
export WHOSTS="${whosts}"
}


# function : farmOutJobs
# purpose : set up a screenrc on each of our worker hosts, then invoke it
# note: user needs to detach from each host's screen session
# inputs : none

farmOutJobs() {
local count=1

local goners="${HOME}/.screenrc.scanslave"
local scanslave="scanslave -A ${AUTHMODE} ${NO_HEADLESS_FLAG} -d ${DIR} -r ${REMOTE_SCRIPT} ${POST} ${JOBS}"
local cmd detach host file chunkfile

if [ x"${NO_HEADLESS_FLAG}" = x"-H" ]; then
detach=""
else
detach="-dm"
fi

cd ${PREFIX}/input/${FULLDATE}

for file in *; do
if [ x"${file}"x = 'x*x' ]; then
# nothing to do, no files found in directory
break
fi
host=$( echo ${count}p | ed -s ${WHOSTS})
chunkfile="${PREFIX}/input/${FULLDATE}/${file}"
cmd="${scanslave} ${NAME} ${chunkfile} ${PASSTHROUGH} && rm -f ${goners}"
ssh ${host} "echo \"screen sh -c \\\"${cmd}\\\"\" > ~/.screenrc.scanslave"
ssh -t ${host} "env ${SCAN_ENV} screen ${detach} -c ~/.screenrc.scanslave -e^Ww && sleep 1"
count=$(( ${count} + 1 ))
done
}


# function : finishTasks
# purpose : clean up, invoke any optional aggregator scripts
# inputs : none

finishTasks() {

date > ${PREFIX}/ended

if [ -n "${AGGREGATOR}" ]; then
${AGGREGATOR} ${NAME} ${INPUT} ${PREFIX}/started
fi
echo "Done."
}

# function : prepChunks
# purpose : split the input into chunks as needed
# inputs : none

prepChunks() {

local num_in

local first last lines

local chunkdir="${PREFIX}/input/${FULLDATE}"

if [ ${NUM_HOSTS} -lt 2 ]; then
echo "Need more than ${num_in} hosts. Bailing out..." >&2
exit
fi

cd ${chunkdir}

num_in=$(wc -l <${INPUT})
lines=$(( ${num_in}/${NUM_HOSTS} ))

if [ ${lines} -gt 0 ]; then
split -l ${lines} ${INPUT}
last=$(ls | awk -v N="${NUM_HOSTS}" '{ if (FNR > N) { print }}')
first=$(ls | head -1)
if [ -n "${last}" ]; then
cat ${last} >> ${first} && rm ${last}
fi
else
# too little input to split, just copy the file into place
cp ${INPUT} .
fi
}


# function : prescanTasks
# purpose : run any pre- script, verify all given files exist,
# clean up old gunk, create directories as needed
# inputs : none

prescanTasks() {
local file var

if [ -n "${PRE}" ]; then
${PRE} ${NAME}
fi

for file in "${INPUT}" "${REMOTE_SCRIPT}" "${WHOSTS}"; do
if [ ! -f "${file}" ]; then
echo "${PROGNAME}: No such file: ${file}" >&2
exit 1;
# NOTREACHED
fi
done

# optional arguments
for var in "AGGREGATOR" "POSTFILE"; do
file=$(eval echo \$${var})
if [ -n "${file}" ] && [ ! -f "${file}" ]; then
echo "${PROGNAME}: No such file: ${file}" >&2
exit 1;
# NOTREACHED
fi
done

PREFIX="${DIR}/chunked/${NAME}"

rm -fr ${PREFIX}/*
mkdir -p ${PREFIX}/input/${FULLDATE} || exit 1

date > ${PREFIX}/started

checkWorkerHosts
NUM_HOSTS=$(wc -l <${WHOSTS})
}

# function : status
# purpose : print out the names of the slaves still working
# inputs : none; called when SIGINFO is received
# returns : nothing; prints to stdout the hostnames of all slaves still
# working

status() {
find ${PREFIX}/* -maxdepth 2 -name '.slave' -exec cat {} \; 2>/dev/null
}


# function : usage
# purpose : print usage
# inputs : none

usage() {
cat <<EOH
Usage: ${PROGNAME} [-Hh] [-A authmode] [-a aggregator] [-c post-chunk] [-d dir]
                   [-j jobs] [-p pre] [-w workers] -i input -n name -r remote
                   [-- scanslave-passthrough-args]
       -A authmode specify ssh authentication mode
       -H do not use the headless user for ssh connections
       -a aggregator specify a script to aggregate all results
       -d dir specify a shared directory
       -h print this message and exit
       -i input specify the file containing the hostlist
       -j jobs specify the number of jobs scanhosts(1) should run
                      (passed on to scanslave(1))
       -n name specify the name of this scan
       -w workers specify a file containing a list of worker hosts
       -p pre specify a script to run before doing anything else
       -r remote specify the script to execute on all target hosts
EOH
}


# function : waitForAll
# purpose : wait for all screen sessions on all hosts to complete by way
# of counting "done" files that each finished job drops into
# the well-known directory
# inputs : none

waitForAll() {
local count diff minutes num_in now seconds time

while [ 1 ]; do
count=$(find ${PREFIX}/* -maxdepth 2 -name '.done' -print 2>/dev/null | wc -l)
time=$(date +%T)
echo -n ${time}: ${count} /${NUM_HOSTS} done
if [ ${count} -eq ${NUM_HOSTS} ]; then
echo
break
fi
echo " - waiting..."
# sleep in intervals to give SIGINFO a chance to be caught
for n in 1 2 3 4 5 6; do
sleep 10
done
done
now=$(date +%s)
diff=$(( ${now} - ${STARTDATE}))
minutes=0
seconds=${diff}
if [ ${diff} -gt 60 ]; then
minutes=$(( ${seconds} / 60 ))
seconds=$(( ${seconds} % 60 ))
fi
num_in=$(wc -l <${INPUT})
echo "$(date +%T): Scanned ${num_in} hosts in ${minutes} minutes and ${seconds} seconds."
}


###
### Main
###

trap status INFO

while getopts 'A:Ha:c:d:hi:j:n:p:r:w:' opt; do
        case ${opt} in
A)
AUTHMODE="${OPTARG}"
;;
H)
NO_HEADLESS_FLAG="-H"
;;
a)
AGGREGATOR="${OPTARG}"
;;
c)
POSTFILE="${OPTARG}"
POST="-p ${POSTFILE}"
;;
d)
DIR="${OPTARG}"
;;
h|\?)
usage
exit 0
# NOTREACHED
;;
i)
INPUT="${OPTARG}"
;;
j)
JOBS="-n ${OPTARG}"
;;
n)
NAME="${OPTARG}"
;;
p)
if [ ! -f "${OPTARG}" ]; then
echo "${PROGNAME}: No such file: ${OPTARG}" >&2
exit 1;
# NOTREACHED
fi
PRE="${OPTARG}"
;;
r)
REMOTE_SCRIPT="${OPTARG}"
;;
w)
WHOSTS="${OPTARG}"
;;
*)
usage
exit 1
# NOTREACHED
;;
esac
done
shift $(($OPTIND - 1))

if [ -z "${INPUT}" -o -z "${REMOTE_SCRIPT}" -o -z "${NAME}" ]; then
usage
exit 1
# NOTREACHED
fi

if [ x"${1}" = x"--" ]; then
shift
fi

if [ $# -gt 0 ]; then
PASSTHROUGH="-- $@"
fi

prescanTasks
prepChunks
farmOutJobs
waitForAll
finishTasks
Something went wrong with that request. Please try again.