#!/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
记录容器下pod日志,如果遇到溢出,则重启对应服务
于 2022-10-09 14:36:47 首次发布