Skip to content

Instantly share code, notes, and snippets.

@cjmatta
Created November 19, 2014 02:54
Show Gist options
  • Save cjmatta/facd717b57a4ec4e7db1 to your computer and use it in GitHub Desktop.
Save cjmatta/facd717b57a4ec4e7db1 to your computer and use it in GitHub Desktop.
A bash script for running a query against a drill cluster and then collecting the updated logs from the whole cluster.
#!/bin/bash
set -o nounset
set -o errexit
if [[ ! -f $1 ]];
then
echo "Query file ${1} not found, exiting.";
exit 1;
fi
if [[ ! -d /opt/mapr/drill ]];
then
echo "Drill not installed on this host.";
exit 1;
fi
sQueryFile=$1;
sDrillVersion=$(ls /opt/mapr/drill);
aDrillHosts=($(maprcli node list -filter csvc=="drill-bits" -columns ip | awk '{print $1}' | tail -n +2));
sZKConnect=$(cat /opt/mapr/drill/${sDrillVersion}/conf/drill-override.conf | grep zk.connect | awk '{print $2}' | sed 's/"//g');
SQLLINE="/opt/mapr/drill/${sDrillVersion}/bin/sqlline -u \"jdbc:drill:zk=${sZKConnect}\"";
function join { local IFS="$1"; shift; echo "$*"; }
function set_current_drillbit_logs {
# will copy the current drillbit.log to /tmp and return the location
# to be used with get_drillbit_logs to get only the updated log data.
sFilename="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)_drillbit.log"
for host in ${aDrillHosts[@]}; do
ssh -q $host "cp /opt/mapr/drill/${sDrillVersion}/logs/drillbit.log ${sFilename}" 2> /dev/null;
done
echo $sFilename;
}
function delete_temp_file {
for host in ${aDrillHosts[@]}; do
ssh -q $host rm -f $1 2> /dev/null
done;
}
function get_drillbit_logs {
# This function will go around to each host in aDrillHosts and get the difference
# in the drillbit.log since `set_current_drillbit_logs` was run.
sFilename=$1;
sHostString=$(join , ${aDrillHosts[@]});
echo "Collecting log files from: ${sHostString}";
for host in ${aDrillHosts[@]}; do
ssh $host "diff ${sFilename} /opt/mapr/drill/${sDrillVersion}/logs/drillbit.log" > ./${host}_drillbit.log && echo "Received log file from ${host}"
done;
}
function run_query {
echo "Running query from ${1}"
$SQLLINE -f $1 &> $(basename $1)_output.log
}
FILE=$(set_current_drillbit_logs)
run_query $sQueryFile
get_drillbit_logs $FILE
delete_temp_file $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment