Skip to content

Instantly share code, notes, and snippets.

@onechiporenko
Created January 24, 2017 12:21
Show Gist options
  • Save onechiporenko/f560794e05ddea55d1674f4cfda96ca6 to your computer and use it in GitHub Desktop.
Save onechiporenko/f560794e05ddea55d1674f4cfda96ca6 to your computer and use it in GitHub Desktop.
#!/bin/bash
me=$(basename $0)
USAGE="Usage:\t$me ssh-key-file ip1 ip2 .. ipN\n\n\t$me ssh-key-file file_with_hosts.txt\n\n\t\tfile_with_hosts:\n\t\t\thostname1 ip1\n\t\t\thostname2 ip2\n\t\t\t...\n\t\t\thostnameN ipN\n\n\t\tfile_with_hosts:\n\t\t\tip1\n\t\t\tip2\n\t\t\t...\n\t\t\tipN";
hash jq 2>/dev/null || { echo >&2 "jq should be installed ('apt-get install jq')"; exit 1; }
# no parameters are provided
if [ -z "$1" ]
then
echo -e $USAGE
exit 1
fi
sshkey="$1"
if [ ! -f $sshkey ]
then
echo -e "SSH key file not exists"
exit 1
fi
# parameters: 1 - host's index in the array, 2 - host, 3 - ssh key
get_logs () {
type="node"
if [ $1 = 1 ]
then
credentials="admin:admin"
protocol="http"
port="8080"
cluster=$(curl "$protocol://$2:$port/api/v1/clusters/" -u "$credentials" | jq -r ".items[0].Clusters.cluster_name")
curl "$protocol://$2:$port/api/v1/clusters/$cluster/hosts?fields=host_components/component/ServiceComponentInfo/component_name&minimal_response=true" -u "$credentials" > "components.json"
type="server"
ssh -oStrictHostKeyChecking=no -i "$3" root@"$2" "tar -zcf - /var/run/ambari-server/stack-recommendations" > "$type"-"$2"-stack-recommendations.tar.gz
fi
ssh -oStrictHostKeyChecking=no -i "$3" root@"$2" "tar -zcf - /var/log" > "$type"-"$2".tar.gz
ssh -oStrictHostKeyChecking=no -i "$3" root@"$2" "cd /var/lib/ambari-agent/data/; tar -zcf - *.json *.txt" > "$type"-"$2"-ambari-agent.tar.gz
}
index=1
if [ -f "$2" ];
# file_with_hosts is provided
then
hosts=$(cat "$2" | sed 's/[^ ]* //g;s/\n/ /g;')
tmp=( $hosts )
count=${#tmp[@]}
for host in $hosts
do
echo "$host [$index of $count]"
get_logs $index $host $sshkey
index=$(($index + 1))
done
else
# list of hosts is provided as arguments
count=$(($#-1))
for host in "$@"
do
if [ "$host" != "$1" ];
then
echo "$host [$index of $count]"
get_logs $index $host $sshkey
index=$(($index + 1))
fi
done
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment