日志搜集脚本根据虚机id 快速定位错误日志

controller_ip=(xxxx) #括号中修改为控制节点ip
read -p "Please input the vm_id: " vm_id
function req_search(){
openrc=`ls /root/ |grep openrc`
if [ -z $openrc ];then
echo "openrc is not exits ,please bash at controller!"
else source /root/$openrc
req=`nova instance-action-list $vm_id |tail -2 |grep req |awk '{print $4}'`
if [ -z $req ];then
echo "there is not have req, please check api.log"
else
cd /var/log/nova/
api=`zgrep $req * |grep nova-api |grep ERROR`
scheduler=`zgrep $req * |grep nova-scheduler |grep ERROR`
conductor=`zgrep $req * |grep nova-conductor |grep ERROR`
# api=`zgrep $req * |grep nova-api `
# scheduler=`zgrep $req * |grep nova-scheduler `
# conductor=`zgrep $req * |grep nova-conductor `
fi
fi
}

function compute_log(){
req=`nova instance-action-list $vm_id |tail -2 |grep req |awk '{print $4}'`
if [ -z $req ];then
echo "there is not have req, please check api.log"
else
host=` nova show $vm_id |grep "OS-EXT-SRV-ATTR:host" |head -1 |awk '{print $4}'`
cd /var/log/nova/
compute=`zgrep $req * |grep nova-compute |grep ERROR`
# compute=`zgrep $req * |grep nova-compute `
fi
ssh $host cd /var/log/nova/
d=`echo $compute |wc -w `
if [ $d -gt 1 ];then
echo "##########$host:compute-log have ERROR"
echo " $compute" |tee /root/$host-compute-log
fi

function log_out(){
req_search
for i in ${controller_ip[*]}
do
ssh $i cd /var/log/nova/
a=`echo $api |wc -w`
b=`echo $scheduler |wc -w`
c=`echo $conductor |wc -w`
if [ $a -gt 1 ];then
echo "##########$i:api-log have ERROR"
echo " $api" |tee /root/$i-api-log
fi
if [ $b -gt 1 ];then
echo "##########$i:scheduler have ERROR"
echo " $scheduler" |tee /root/$i-scheduler-log
fi
if [ $c -gt 1 ];then
echo "##########$i:conductor have ERROR"
echo " $conductor" |tee /root/$i-conductor-log
fi
done
}


if [ -n $vm_id ];then
log_out
compute_log
else
echo "input error please redo"
fi

posted @ 2019-12-05 11:10  jimmy阳  阅读(163)  评论(0编辑  收藏  举报