-
-
Save noahcampbell/739827 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requires: mktemp | |
PROG=`basename $0` | |
# defaults | |
SSH_USER=$(whoami) | |
WORKSPACE=$(mktemp -d /tmp/${PROG}-resources-$$) | |
VERBOSE=0 | |
usage() | |
{ | |
cat <<EOF | |
usage: $PROG options | |
OPTIONS: | |
-h Show this message | |
-u SSH username (default=$SSH_USER) | |
-f hosts file (or reads stdin) | |
EOF | |
} | |
die() { echo $* 1>&2 ; exit 1 ; } | |
while getopts "hvu:f:" OPTION | |
do | |
case $OPTION in | |
h) | |
usage | |
exit 1 | |
;; | |
v) | |
VERBOSE=1 | |
;; | |
u) | |
SSH_USER=$OPTARG | |
;; | |
f) | |
HOST_FILE=$OPTARG | |
;; | |
*) | |
usage | |
exit 1 | |
esac | |
done | |
verbose() { [ "$VERBOSE" == "1" ] && { echo "VERBOSE : $*" 1>&2 ; } } | |
verbose "{SSH_USER=\"$SSH_USER\", WORKSPACE=\"$WORKSPACE\", HOST_FILE=\"$HOST_FILE\"}" | |
[ -s "$HOST_FILE" -a ! -r "$HOST_FILE" ] && { | |
die "file not readable: $HOST_FILE" | |
} | |
mkdir -p $WORKSPACE || { die "Failed creating data directory: $WORKSPACE" ; } | |
collect_script=$WORKSPACE/${PROG}-collect.$$ || exit 1 | |
cat > $collect_script <<EOF | |
#!/bin/sh | |
USAGE="\$0 <outputfile>" | |
[ \$# = 1 ] || { echo "\$USAGE" ; exit 1 ; } | |
outputfile=\$1 | |
hostname=\$(hostname) | |
osArch=\$(uname -p) | |
osName=\$(uname -s) | |
osVers=\$(uname -r) | |
username=\$(whoami) | |
dstamp=\$(date "+%Y-%m-%d %H:%M:%S") | |
echo " <node name='\${hostname}' description='Last updated \${dstamp}' \ | |
osArch='\${osArch}' osName='\${osName}' osVers='\${osVers}' \ | |
username='\${username}'/>" > \$outputfile | |
EOF | |
# Reasign fd0 to the specified host file | |
[ -f "$HOST_FILE" ] && { exec 0<> $HOST_FILE ; } | |
i=0 | |
while read line | |
do | |
echo $line | egrep -q '^#' && continue | |
host=$line | |
verbose "collecting node info from host: $host ..." | |
scp $collect_script ${SSH_USER}@${host}:/tmp/$(basename $collect_script) || { | |
die "Failed copying collection script on host: ${host}" | |
} | |
ssh -n ${SSH_USER}@${host} sh /tmp/$(basename $collect_script) /tmp/node.xml.$$ || { | |
die "Failed executing collection script on host: ${host}" | |
} | |
scp ${SSH_USER}@${host}:/tmp/node.xml.$$ ${WORKSPACE}/${host}.xml || { | |
die "Failed copying resources data from host: ${host}" | |
} | |
i=$(expr $i + 1) | |
done | |
if [ $i -gt 0 ] | |
then | |
verbose "Generating resources.xml for $i hosts ..." | |
echo "<project>" | |
cat ${WORKSPACE}/*.xml || die "Failure due to internal script error" | |
echo "</project>" | |
verbose "Done." | |
fi | |
# | |
# clean up the temporary files | |
# | |
verbose "cleaning up temporary files" | |
rm -rf ${WORKSPACE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment