全部博文(150)
分类: 系统运维
2018-11-09 17:06:28
使上篇文章介绍的自动删除脚本更具备通用性,可以根据zabbix 监控传递过来的文件系统参数自动删除给文件系统下面的后缀为"log"的日志。
方法
结论
经测试可以根绝参数正确执行删除
#!/bin/sh # 该脚本接受一个参数,即ZABBIX的宏变量{TRIGGER NAME},其中包含超过80%的文件系统名字 # {TRIGGER NAME}示例:Free disk space is less than 20% on volume /goluk # bash下求最后一个参数, filesystem="${@: -1}" # 定义大日志 large_file_size="5000M" # 定义超期日志天数 number=30 function delete_older_logs() { if [ "$number" -ge "7" ] ; then n2=`expr "$number" - 4` find $filesystem -type f -name "*.log*" -mtime +$number |xargs rm -f find $filesystem -type f -name "*.out*" -mtime +$number |xargs rm -f find $filesystem -type f -name "*.log" -mtime +$n2 -size +$large_file_size |xargs rm -f find $filesystem -type f -name "*.log" -mtime +$n2 -size +$large_file_size |xargs rm -f else echo "Log files within 7 days will not be deleted." fi } if [ "$filesystem" == "" ] ; then echo "Nothing to delete,exit." exit fi fs_is_full=`df --output=pcent $filesystem |tail -1 |awk -F % '{print $1}'` while [[ "$fs_is_full" -ge "80" ]] do delete_older_logs number=`expr "$number" - "3"` fs_is_full=`df --output=pcent $filesystem |tail -1 |awk -F % '{print $1}'` if [ "$number" -le "7" ] && [ "$fs_is_full" -ge "80" ] ; then echo " Need manual clean your log files." |mail -s "$filesystem on `hostname` more than 80%" lihui@goluk.com exit fi done