本文是我的一个学生在运维工作中写的自动日志清理脚本程序,我这里不评价该shell脚本写的好与坏,只是发出来,和大家做一个分享,如果能给大家带来一点点思路上的参考就够了。

自动日志清理脚本程序

#!/bin/sh
##########################################################
#created by ydds      #
#date 2011-4-25       #
#function log clear default save 7 days logs   #
#when hard disk space achieve 80%, automatic save 3 days #
#when hard disk space achieve 90%,save only LOG today   #
#when only today log hard disk space achieve >95%  #
#   clear today log      #
##########################################################
set -x

oldboyWarnValue=80
oldboyCriticalValue=90
oldboyCC=95
nohupFlag=0

# $1 must be directory
if [ $# -ne 2 ]||[  -z $1 ]||[ ! -d $1 ];then
        echo "Uage:logClear /home/oldboy/apachelogs /dev/xvda1"
 exit 1
fi

#clear log path
logPath=$1

devstr=$(df|awk '/\//{print $1}'|sed -n '/\//p')
echo "${devstr}"|grep -q "$2"
[ $? -ne 0 ]&&exit 1

# hard disk name
oldboyName=$2

scriptLog=/server/script/clearLog.log

#clear nohuplog
clearNohupLog()
{
 if [ ${nohupFlag} -ne 1 ];then
   # check nulup.log >1G clear
         fileNohupLog="${logPath}/../nohup.log"
         NohupSize=$(du ${fileNohupLog}|awk '{print $1/1024/1024}'|awk -F "." '{print $1}')
  echo "check nohup.log size: $(du -h ${fileNohupLog}|awk '{print $1}')">>${scriptLog}
         if [ -f ${fileNohupLog} ];then
                 if [ ${NohupSize} -ge 1 ]||[ $1 -eq 1 ];then
                         echo "">${fileNohupLog}
    if [ $? -eq 0 ];then
     echo "clear nohup.log ok!">>${scriptLog}
    fi 
                 fi
         fi
 fi
}

#hard disk used
oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')

echo "[$(date +%Y-%m-%d\ %H:%M:%S)]check hard disk used start....">>${scriptLog}
echo "check current hard disk space used ${oldboyUsed}%">>${scriptLog}

#clear nohup.log >warning
if [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
 echo "hard disk used ${oldboyUsed}% > ${oldboyWarnValue}%,start clear nohup.log..">>${scriptLog}
 clearNohupLog 1
 nohupFlag=1
fi

if [ ${nohupFlag} -eq 1 ];then
 #hard disk used
 echo "after deal nohup.log ,check hard disk again...">>${scriptLog}
 oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
fi

#save 7,3 days logs
clearnDayLogs()
{
 # cd tmp directory
 cd /tmp

if [ $1 -ne 1 ];then
  if [ -d ${logPath} ];then
   cd ${logPath}
   find . -type f -mtime +$1 |xargs rm -f
   
   if [ $? -eq 0 ];then
                         echo " clear ${logPath} logs $1 ago days logs successfully!">>${scriptLog}
                 else
                         echo " clear ${logPath} logs $1 ago days logs fail!">>${scriptLog}
                 fi 
  fi
 else
  #clear nohup.log
         clearNohupLog 1

clearFileNameArr=($(ls ${logPath}|awk -F "." 'NF==3{print $0}'))
  # clear file name is not blank
  if [ -n "${clearFileNameArr}" ];then
   echo "|">>${scriptLog}
   for((i=0;i<${#clearFileNameArr[@]};i++))
          do
    if [ ${i} -eq 0 ];then
     echo "clear file------------------start--------------------------->">>${scriptLog}
    fi
    
    # cd  this directory
                  cd ${logPath}
    if [ -f "${logPath}/${clearFileNameArr[$i]}" ];then
                   find . -type f -name ${clearFileNameArr[$i]} |xargs rm -f
    fi

if [ $? -eq 0 ];then
     echo "${clearFileNameArr[$i]}">>${scriptLog}
    fi
          done
   echo "clear file------------------------- end -------------------->">>${scriptLog}
   echo "|">>${scriptLog}
  else
   echo "☆☆☆☆☆☆ Not files 1 day ago ☆☆☆☆☆☆">>${scriptLog}
  fi
  afterClearoldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
  echo "clear ${logPath} logs ${1} ago days logs successfully!">>${scriptLog}
 fi
}

 如果你把握不好该脚本带来的误删除的风险,请慎用。

#when today log save ,check hard disk used >80%,so  clear today logs
clearTodayLog()
{
 oldboyUsedTmp=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
 echo "clear today log start ...">>${scriptLog}
 #todayFileArr=($(ls ${logPath}))
 todayFileArr=($(find ${logPath} -type f|awk -F "/" '{print $NF}' ))
 echo "start save current logs tail 300 ..">>${scriptLog}
 echo "|">>${scriptLog}
 echo "dealing currnet logs name*********start*************>>>>">>${scriptLog}
 for((i=0;i<${#todayFileArr[@]};i++))
 do
   cd ${logPath}
    
   #get tail 300 --> tmp file
   tail -300 ${todayFileArr[$i]}>/tmp/tmpydds
  
   #check file exist again save tail 300 --- this file

if [ -f "${logPath}/${todayFileArr[$i]}"  ];then
           find . -type f -name ${todayFileArr[$i]} -exec cat /tmp/tmpydds >${logPath}/${todayFileArr[$i]} \;
    if [ $? -eq 0 ];then
     echo " ${todayFileArr[$i]}----- ok!">>${scriptLog}
    else
    echo " ${todayFileArr[$i]}----- fail!">>${scriptLog}
    fi
   fi
 done 
 echo "dealing currnet logs name********* end *************>>>>">>${scriptLog}
 echo "|">>${scriptLog}
 if [ $? -eq 0 ];then
   echo "clear today log successfully!">>${scriptLog}
  
 else 
  echo "clear today log fail!">>${scriptLog}
 fi
}

wflag=0
cflag=0
flag=0
count=0

#clear nohup.log
#clearNohupLog 0

while true
do
        oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')
 
 #not first and hard disk < warning value  break
        [ ${count} -ne 0 ]&&[ ${oldboyUsed} -lt ${oldboyWarnValue} ]&&break

count=1
 
################next
        if [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
  case ${cflag} in
                        1)
                                clearTodayLog
                                cflag=2
                        ;;
                        2)
                                echo "baojing---------->">>${scriptLog}
                                break
                        ;;
                esac

case ${wflag} in
                        1)
                                clearnDayLogs 1
                                wflag=2
                        ;;
                        2)
                                clearTodayLog
                                wflag=3
                        ;;
                        3)
    echo "baojing---------->">>${scriptLog}
                                break
                        ;;
                esac 
        fi

######################first
        if [ ${wflag} -eq 0 ]&&[ ${cflag} -eq 0 ];then

#when >95% clear today
  if [ ${oldboyUsed} -ge ${oldboyCC} ];then

#clear 1 ago log
   clearnDayLogs 1

#clear today log
   clearTodayLog  
   break

#when hard disk is used 90% is Critical  today log only save
                elif [ ${oldboyUsed} -ge ${oldboyCriticalValue} ];then
                        echo "${oldboyName} current used ${oldboyUsed}%>= ${oldboyCriticalValue}% is Critical!">>${scriptLog}
                        cflag=1
                        clearnDayLogs 1

#when hard disk is used ${oldboyWarnValue}% is Warning automatic save 3 days
                elif [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
                        echo "${oldboyName} current used ${oldboyUsed}%>= ${oldboyWarnValue}% is Warnning!">>${scriptLog}
                        wflag=1
                        clearnDayLogs 3

#when hard disk is used lt 80% the logs oldboy is normal.
                else
                        echo "${oldboyName} current used ${oldboyUsed}% is Normal!">>${scriptLog}
                        clearnDayLogs 7
                        break
                fi
        fi

flag=1
done

#when clear all log oldboy used
oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')

#after deal log check disk used

if [ ${oldboyUsed} -ge ${oldboyCC} ];then
 echo "oldboy used ${oldboyUsed}% is Critical Critical!">>${scriptLog}

elif [ ${oldboyUsed} -ge ${oldboyCriticalValue} ];then
  echo "oldboy used ${oldboyUsed}% is Critical!">>${scriptLog}

elif [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
  echo "oldboy used ${oldboyUsed}% is Warning!">>${scriptLog}

else
 echo "oldboy used ${oldboyUsed}% is Normal!"
fi

echo "check hard disk used end!">>${scriptLog}

我的一个学生在运维工作中写的自动日志清理脚本程序相关推荐

  1. Linux 运维工作中的经典应用ansible(批量管理)Docker容器技术(环境的快速搭建)...

    一 Ansible自动化运维工具 Python 在运维工作中的经典应用ansible(批量管理操作)1.安装ansible(需要bese epel 2种源) wget -O /etc/yum.repo ...

  2. 实用技术干货!教你用机器学习提高日常安全运维工作中的效率

    作者介绍:黄龙,网易易盾资深安全工程师,专注于互联网安全,擅长安全攻防对抗和甲方安全建设,拥有CISSP认证,同时也是网易云课堂<Web安全工程师>微专业核心制作人. ​一.安全运维工作 ...

  3. 运维工作中,你都有哪些技巧

    很多人都说运维工作是苦逼的,不可否认,有时候我也这样觉的,但回头想想,又有那份工作不辛苦呢,看看那些在叙利亚前线的记者,在马路上的清洁工,在饭店不停颠勺的厨师,在理发店里两只胳膊永远呆在空中的理发师, ...

  4. 浅谈机场综合布线运维工作中的难点问题

    随着系统规模越来越大.体系越来越复杂.信息系统的作用越来越明显.运维保障的要求越来越高,综合布线维护工作面临的问题也愈加凸显.以下以国内某大型机场在综合布线运维工作中面临的实际问题为例,给大家分享综合 ...

  5. 运维工作中常见的一些定律

    以下是我在工作中积累的,和运维工作相关的一些定律,接下来,我会对各个定律分别展开进行阐述,从而加深大家的理解 一万小时定律,要在任何领域成为大师,一般需要约10年的艰苦努力 墨菲定律,如果事情有变坏的 ...

  6. oracle运维工作中每天巡检的必要性--job的相关问题

    今天上午十一点到十二点左右.生产库直接的同步出现了问题.导致了一段时间内系统无法正常使用,直接影响了几十万的用户量.这个情况看起来虽然很小,如果发生在不重要的时间之内,可以忽略不计.但是今天是业务运行 ...

  7. 项目运维工作的心得总结

    运维工作直接关系着产品运行的稳定,且运维工作比较复杂,不同项目有着不同的运维方式.所以在运维工作中应总结经验教训,并逐渐作为运维规范,从而提高运维水平,保障应用系统正常稳定运行. 参与运维工作已有不短 ...

  8. 计算机网络安全运维管理工作总结,计算机设备日常运维工作总结

    1.主要工作完成情况.亮点和取得的成绩 (一) 工程项目上线 参与工程. (二) 安全运行年考核 为了提高各类计算机设备的完好率及使用率,防范科技风险,每月都会将安全运行年考核情况通报给支行,督促各支 ...

  9. 服务器运维事项,云服务器的运维工作要注意的事项

    从字面意思来看,云服务器的运维指的是对云服务器的运营和维护. 运维,是以技术为依托.通过技术软件来实现服务器稳定安全工作的服务. 在日常的运维工作中,云服务器的配置以及技术需求,决定了运维需要技术人员 ...

最新文章

  1. YOLOv3模型剪枝,瘦身80%,提速100%,精度基本不变
  2. 【Vue】Vue1.0+Webpack1+Gulp项目升级构建方案的踩坑路
  3. Python高级特性(切片,迭代,列表生成式,生成器,迭代器)
  4. 百度数据可视化图表套件echart实战
  5. HBase数据存储格式
  6. 云计算-从基础到应用架“.NET研究”构系列-云计算的演进
  7. linux下DNS配置详解
  8. 浙江农林大学蓝桥杯程序设计竞赛校选拔赛(同步赛)签到题ABFGHIJ
  9. /proc/meminfo之谜
  10. c++如何让字符串重复输出_Python基础三(数据类型之数字和字符串)
  11. sqldeveloper 工具的使用——连接数据库的介绍
  12. Ubuntu20.04配置NTP服务器
  13. python选择题总结
  14. 【xinput1_3.dll下载】xinput1_3.dll丢失怎么修复win10
  15. python数据分析设置教程视频_炼数成金女讲师Python数据分析实战应用视频教程
  16. 5.spring-boot>redis配置使用
  17. SD-WAN 与传统 WAN
  18. 获取office版本
  19. iperf3 安装遇到 error while loading shared libraries: libiperf.so.0
  20. 有一种图片形式是“data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQ…==”...

热门文章

  1. 使用PHPCS+GIT钩子保障团队开发中代码风格一致性实践
  2. 课程三(Structuring Machine Learning Projects),第二周(ML strategy(2)) —— 0.Learning Goals...
  3. IBM挺进云计算 自家内采用私有云模式
  4. ping不通win7、8解决方法以及nc后门的制作
  5. 谷歌推出了其首款触屏笔记本电脑
  6. iPad如何越狱?4.2.1完美越狱教程 一 (DFU 绿霸越狱)
  7. Type TIMESTAMP(6) of table field ‘ts‘ does not match with the physical type TIMESTAMP(3) of the ‘ts‘
  8. Name node is in safe mode解决
  9. 修复win7+ubuntu18.10双系统引导
  10. 数值方法:数值微分与数值积分