记录容器下pod日志,如果遇到溢出,则重启对应服务

#!/bin/sh


#. /home/software/.bashrc

workdir=/home/software/check_pod/`date +'%Y%m%d%H'`

LOG_PATH=${workdir}/logs

mkdir -p ${LOG_PATH}

nowtime="`date +'%Y%m%d%H%M%S'`"

nowdate=`date +'%Y%m%d'`

restarfile=$LOG_PATH/restar${nowtime}.log

logfile=$LOG_PATH/checkcode${nowdate}.log

errorlogfile=$LOG_PATH/checkcode_error${nowtime}.log

errorcodecfg=${workdir}/errorcode.cfg

log_strfile=${workdir}/logstr.tmp

tolfile=${workdir}/tolfile.tmp

echo >${tolfile}

poderrlogdir=${LOG_PATH}/${nowdate}

mkdir -p ${poderrlogdir}

count=0

logprint()

{

  nowtime="`date +'%Y%m%d%H%M%S'`"

  logstr=$2

        logtype=$1

        if [ $logtype = 1 ]

        then

            echo "$nowtime [INFO] $logstr"|tee -a $logfile

        elif [ $logtype = 2 ]

        then

            echo "$nowtime [ERR] $logstr"|tee -a $logfile

            echo "$nowtime [ERR] $logstr"|tee -a $errorlogfile

        else

            echo "logtype error"

        fi



}

DEPLOY_NAME="`kubectl get deploy -n iwos-namespace |awk '{if(NR!=1)print $1}'`"

#DEPLOY_NAME="`kubectl get deploy -n iwos-namespace |grep grid-war-map|awk '{print $1}'`"



for i_deploy in $DEPLOY_NAME

do

   logprint 1 "STRAR CHECK ${i_deploy}"

   POD_NAME="`kubectl get pod -n iwos-namespace| grep ${i_deploy}|awk '{print $1}'`"

   for i_pod in ${POD_NAME}

   do

       #errorcodenum=`kubectl logs --tail 1000 $i_pod -n iwos-namespace | grep -iE  "$errorcode"|wc -l`

       #echo ${log_strfile}

       kubectl logs --tail 1000 $i_pod -n iwos-namespace>${log_strfile}

       #查错误码

       grep -v "#" ${errorcodecfg}|while read line

       do

           isflag=0 #重置重启标志

           erroNO=`echo ${line}|awk -F\| '{print $1}'`  #错误码

           errorcode=`echo ${line}|awk -F\| '{print $2}'`  #错误码关键字

           ifrestarflag=`echo ${line}|awk -F\| '{print $3}'` #是否重启开关

           ifrestarnum=`echo ${line}|awk -F\| '{print $4}'`  #重启阈值

           errorcodenum=`grep "$errorcode" $log_strfile|wc -l`

           #echo "${i_pod}|${errorcode}|${errorcodenum}" |tee -a ${tolfile}

           #echo ${errorcodenum}

                 if [ $errorcodenum -ne 0 ];then

                    poderrlogfile=${poderrlogdir}/${i_pod}_${nowtime}_err${erroNO}.log

                    cp ${log_strfile} ${poderrlogfile}

                    logprint 2 "check ${i_pod} exists [errorcode:${errorcode}] nums:$errorcodenum,pless check ${poderrlogfile}"

                    if [ ${ifrestarflag} -eq 1 -a $errorcodenum -gt ${ifrestarnum}  ]

                    then

                        logprint 1 "restart ${i_pod}"

                        #grep -iE  "$errorcode" ${poderrlogfile}

                        echo "kubectl delete po $i_pod -n iwos-namespace">>${restarfile}

                        #sleep 10

                        isflag=1

                    fi

                    echo "${i_pod}|${erroNO}|${errorcode}|${errorcodenum}|${isflag}" |tee -a ${tolfile}

                 else

                    logprint 1 "check ${i_pod} [errorcode:${errorcode}] normal"

                 fi

       done



   done

   logprint 1 "END CHECK ${i_deploy}

   "

done



#find /home/software/tool/logs/ -mtime +30|xargs rm -rf

#find /home/software/tool/logs/ -name '*log' -mtime +15|xargs rm -rf

#find /home/software/tool/logs/ -type d -mtime +15|xargs rm -rf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三朝看客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值