Skip to content

Instantly share code, notes, and snippets.

@jlogsdon
Created January 27, 2016 08:15
Show Gist options
  • Save jlogsdon/c7adcd1e0a021702841d to your computer and use it in GitHub Desktop.
Save jlogsdon/c7adcd1e0a021702841d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Usage: aws-tail [options] <tag-value> <file>
#
# Tail the same file on servers tagged with the given tag value. Each line is prefixed with the string "[$HOST_IP]"
# where $HOST_IP is the IP address the line came from. This file can be optionally filtered on the remote end to reduce
# transit data.
#
# Note that this is not perfect: output is not synchronized across connections which can lead to one line bleeding into
# another. For a general idea of what's going on, however, it works fantastic.
#
# Options:
# -c number of servers to listen in on. This defaults to 200 which is kind of insane if you aren't filtering.
# -f accepts a scalar filter value. Tags in relevant logs are great (such as RUBICON) for this.
# -u is for specifying the username to connect with (defaults to `whoami`).
#
# Example:
#
# Follow production logs for the Offers facet and grep for RUBICON on the server side.
#
# aws-tail -f RUBICON -u james.logsdon tapjoyserver-web_offers /mnt/log/tapjoyserver/production.log
username=`whoami`
count=200 # A crazy maximum really
while getopts 'c:f:i:u:' opt; do
case $opt in
c) count=$OPTARG ;;
f) filter=$OPTARG ;;
i) identity=$OPTARG ;;
u) username=$OPTARG ;;
esac
done
shift $(($OPTIND - 1)) # Remove the parsed arguments
search=$1
file=$2
hosts=$(aws ec2 describe-instances --filters "Name=tag-value,Values=${search}" | \
jq -c -M -r '.Reservations[].Instances[].PublicIpAddress' | \
grep -v '^null' | \
head -n${count})
command="tail -F ${file}"
if [ ! -z $filter ]; then
command+=" | grep '${filter}'"
fi
trap handle_int INT
function handle_int() {
for pid in $(jobs -p)
do
kill $pid
done
}
for host in $hosts
do
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${username}@${host} "${command}" &
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment