#!/bin/bash
set -x 
tmplog="/tmp/as_log_`date +%s`.txt"
if [ -f /usr/bin/asmonitor ]
then 
asmonitor_collect=/usr/bin/asmonitor
asinfo_collect=asinfo
conf_file='/etc/aerospike/aerospike.conf'
asdlog_file='/var/log/aerospike/aerospike.log'
else
asmonitor_collect=/usr/bin/clmonitor
asinfo_collect=clinfo
conf_file='/etc/citrusleaf/citrusleaf.conf'
asdlog_file='/var/log/citrusleaf.log'
fi

function sep
{
set +x
cmd=$1
c=0
while [ $c -le 50 ]
do echo -n '=' >> ${tmplog}
let c=c+1
done
echo -e "\n CALLING BAKERY" >> ${tmplog}
echo $cmd >> ${tmplog}
set -x
}

echo "Generating tmp log at ${tmplog}"
sep "collecting ifconfig"
/sbin/ifconfig >> ${tmplog}

for param in Node Namespace XDR
do sep "$asmonitor_collect -e info $param"
$asmonitor_collect -e "info ${param}" >> ${tmplog}
done

for param in compareconfig latency printconfig stat
do 
sep "$asmonitor_collect -e \"${param}\"" 
$asmonitor_collect -e "${param}" >> ${tmplog}
done

for param in objsz ttl evict
do 
sep "$asmonitor_collect -e histdump -v ${param}"
$asmonitor_collect -e "histdump -v ${param}" >> ${tmplog}
done

sep "$asmonitor_collect -e $asinfo_collect -v statistics"
$asmonitor_collect -e "$asinfo_collect -v statistics" >> ${tmplog}
sep "$asmonitor_collect -e $asinfo_collect -v statistics -p 3004"
$asmonitor_collect -e "$asinfo_collect -v statistics -p 3004" >> ${tmplog}

sep "Collecting log"
cl_log=`$asinfo_collect -v logs|cut -f 2 -d ':'|grep -v requested`
if [ -f $cl_log ]
then
tail -n 1000 $cl_log >> ${tmplog}
else
sudo tail -n 1000 $asdlog_file >> ${tmplog}
fi

sep "uname -a"
uname -a >> ${tmplog}

sep "OS Version"
os_ver='/etc/redhat-release'
if [ -f $os_ver ]
then head $os_ver  >> ${tmplog}
rpm -qa|grep -E 'citrus|aero' >> ${tmplog}
else
lsb-release -a  >> ${tmplog}
#debian versions
fi

smd_home='/opt/aerospike/smd/'
if [ -d $smd_home ]
then
for smd in `ls ${smd_home}`
do 
sep $smd
cat  ${smd_home}/$smd >> ${tmplog} 
done 
fi

sep $conf_file
if [ -f $conf_file ]
then cat $conf_file >> ${tmplog} 
else
echo "$conf_file does not exist"
fi

declare -a asinfo_collect_ignore
asinfo_collect_ignore=('value','is')
for ns in `$asinfo_collect -v namespaces -l|grep -v -E 'requested'`;
do 
if test "${asinfo_collect_ignore[$ns]+isset}"
then echo ''
else
sep "pmap for $ns" 
$asmonitor_collect -e "pmap ${ns}" >> ${tmplog}
fi
done

for param in sindex-qnodemap: udf-list
do
sep $param
$asmonitor_collect -e "${asinfo_collect} -v ${param}" >> ${tmplog}
done

sep 'End of ascollectinfo'

tar -czvf ${tmplog}.tgz ${tmplog}
set +x
clear
echo -e "\n\n\n\n\n\n ${tmplog}.tgz created. Please mail it to support@aerospike.com ."
echo -e "You can delete ${tmplog} and ${tmplog}.tgz after sending. Thank you! \n\n\n\n\n\n"
