Skip to content

Instantly share code, notes, and snippets.

@shokoe
Created March 21, 2018 10:38
Show Gist options
  • Save shokoe/0662a7404502f3ae03174faaa533f0a0 to your computer and use it in GitHub Desktop.
Save shokoe/0662a7404502f3ae03174faaa533f0a0 to your computer and use it in GitHub Desktop.
Tail for server cluster shell script. Tails same file on multiple servers using EC2ulz.
#!/bin/bash
ssh_ops='-fn -o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR'
print_help(){
echo "Syntax: $0 <instance grep string> <log file> [grep regex]
example: mtail.sh 'proc-app-fronts?|proc-app-back001' /var/log/apache2/access.log '404|500'
exit mtail with Ctrl-C"
}
[ $# -lt 2 ] && print_help && exit
f="$2"
term(){
kill $pids
echo
#ps awux | grep ssh | grep '.log' | grep -v grep | awk '{print $2}' | xargs kill
}
slist=`Eins | grep running | egrep "$1" | awk '{print $1}' | sort | xargs`
unset C
for c in $slist; do
C="$C GREP_COLOR='00;$((i++%5+32))' egrep --line-buffered --color=always '$c|' |"
done
echo "Tailing $f on: $slist" | eval $C cat
for i in $slist; do
ip="`Eins | egrep "^$i\b" | awk '{print $5}'`"
#ssh -i $EC2_CONN_KEY $ssh_ops $ip "tail -f -n 5 $f | sed -u \"s#^#$i -- #\""
ssh $ec2_sshopt $ip "[ -f $f ] && tail -f -n 5 $f | sed -u \"s#^#$i -- #\" || echo \"$i -- File not found $f\" >&2" &
done | eval $C cat | egrep --line-buffered --color=always "${3}"
#done | GREP_COLOR='00;35' egrep --color=always "${slist// /|}|" | egrep --color=always "${3}"
pids="`ps auxw | grep -v grep | grep \"ssh -i.*-fn.*$f\" | awk '{print $2}' | xargs`"
trap term INT
while ps awux | grep -v grep | egrep "ssh -i.*-fn.*$f" &>/dev/null; do
sleep 0.1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment