如果文件在5分钟内增长超过一定大小,则发出警报
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果文件在5分钟内增长超过一定大小,则发出警报相关的知识,希望对你有一定的参考价值。
我正在尝试编写一个脚本来检查文件在过去5分钟内增长了多少,如果在那段时间内增长超过20MB,则写入日志。
到目前为止,我已经设法写了这个;
output="$HOME/log/logsize.output" # This file is where the values are written to before being compared
currentsize=$(stat '-c%s' "$HOME/log/input.log") # This is the current size of the log file
echo $currentsize >> $output
oldsize=$(sed 'x;$!d' < "$output") # Sets the previous reading as "oldsize" by pulling the second to last line of $output
difference=$(("$currentsize" - "$oldsize")) # This is the difference in size between the current and previous readings
if $difference > "1999999" # Checks the difference, if greater than 1999999 write an error.
then
echo "Warning! Log File has exceeded the expected rate of growth. Please investigate." > "$HOME/log/logalert.log"
else
echo "Log File is within expected parameters" > "$HOME/log/logalert.log"
fi
当我运行这个脚本时,这是我收到的输出;
line 23: "2910" - "2910": syntax error: operand expected (error token is ""2910" - "2910"")
解决了!
这是我最终做的工作
#!/bin/bash
#########################################################################
# Author - Jack Arnold
#
# Last Updated: 20.02.18
#########################################################################
#
# This script exists to periodically check the file size of a log file.
# If this file has grown 20MB or more since the last loop of this script
# it will write out an alert to ~/logs/logsize.log
#
#########################################################################
# Variables for the script.
output="$HOME/log/logsize.output" # This file is where the values are written to before being compared
currentsize=$(stat '-c%s' "$HOME/log/input.log") # This is the current size of the log file
echo "$currentsize" >> "$output"
oldsize=$(sed 'x;$!d' < "$output") # Sets the previous reading as "oldsize" by pulling the second to last line of $output
difference=$((currentsize - oldsize)) # This is the difference in size between the current and previous readings
if [[ $difference > "1999999" ]] # Checks the difference, if greater than 1999999 write an error.
then
echo "Warning! Log File has exceeded the expected rate of growth. Please investigate." > "$HOME/log/logalert.log"
else
echo "Log File is within expected parameters" > "$HOME/log/logalert.log"
fi
我注意到的第一件事是,在第一行中,变量赋值(output="~/log/logsize.output"
)会导致问题,因为~
没有用引号扩展。但是,这个脚本中有很多错误,我建议花更多的时间学习shell脚本的基础知识;我建议把Greg’s Wiki作为一个很好的起点。
一个while ago,我更新了bash标签的使用指南,以便它包含在https://www.shellcheck.net/中检查shell脚本的建议,这是一个很棒的资源。事实上,Shellcheck警告波浪问题,并包括使用$HOME
而不是~
的有用建议。而不是重新讨论Shellcheck会警告你的所有问题,我只会提到一些它没有注意到的问题:
Command substitution
currentsize=(stat '-c%s' "~/log/input.log")
我想你打算使用命令替换,以便currentsize
变量包含stat
命令的输出。这应写成:
currentsize=$(stat '-c%s' "$HOME/log/input.log")
Arithmetic comparisons
Shellcheck在到达此行之前停止处理,但我注意到您使用>
作为算术比较运算符:
if (${difference} > 1999999) ;
在POSIX(类Unix操作系统的标准)shell中,这些运算符用于输入和输出重定向 - 正如你所做的那样
echo "Log File is within expected parameters" > "~/log/logalert.log"
POSIX shell中算术比较的正确运算符是-gt
,测试它的可移植方法是:
if [ "$difference" -gt 1999999 ]
注意:诸如bash
和ksh
之类的shell通过使用<
和>
进行算术比较来扩展POSIX,但这仅适用于双括号内。见Comparing integers: arithmetic expression or conditional expression。在Bash中,当与>
构造一起使用时,[[
也可用于字符串比较。见Bash Conditional Expressions。
另外:如果字符串包含由shell特别解释的异常字符(例如,空格导致分词),则只需要引用字符串。但是,如果您已经习惯使用其他编程语言并且发现它更具可读性,那么这样做没有任何害处。
General tips
- 始终引用变量扩展(除非明确要求分词)
- 调试时使用
set -x
。 set -e
也可用于获取错误通知,例如尝试访问不存在的变量的内容。
我只是想提供我的解决方案:
#!/bin/bash
output="$HOME/log/logsize.output"
if [ ! -f $HOME/log/logsize.output ]
then
touch $HOME/log/logsize.output
fi
while [ 1 ]
do
stat '-c%s' $HOME/log/input.log >> "${output}"
math=$(tail -n2 "${output}" | tr '
' '-' | sed 's/.$//g')
# 20971520 bytes is 20 Mbytes. Uncomment this string, and comment mine with -100 argument. Mine string is only for the example.
# if [ $(($math)) -lt -20971520 ]
if [ $(($math)) -lt -100 ]
then
echo "Attemption! The file have has grown by more then 20Mbytes!"
fi
# Replace sleep 5 by sleep 600 for 1 per 5 minute check.
sleep 5
done
您可以按照自己的意愿运行它:./filechange.sh &
或cron(如果你想要cron,删除while
循环和sleep
)它是如何工作的:
$ ls -l
total 4
-rwxr-xr-x 1 sahaquiel sahaquiel 400 Feb 20 15:00 filechange.sh
-rw-r--r-- 1 sahaquiel sahaquiel 0 Feb 20 14:58 input.log
-rw-r--r-- 1 sahaquiel sahaquiel 0 Feb 20 14:59 logsize.output
$ ./filechange.sh &
[1] 29829
# Adding 150 random numbers in input.log file
[sahaquiel@sahaquiel-PC log]$ i=0; while [ $i -lt 150 ]; do echo $RANDOM >> input.log; i=$(($i + 1)); done
# filechange.sh echo in my shell:
[sahaquiel@sahaquiel-PC log]$ Attemption! The file have has grown by more then 20Mbytes!
$ ls -l
total 12
-rwxr-xr-x 1 sahaquiel sahaquiel 400 Feb 20 15:00 filechange.sh
-rw-r--r-- 1 sahaquiel sahaquiel 849 Feb 20 15:00 input.log
-rw-r--r-- 1 sahaquiel sahaquiel 14 Feb 20 15:00 logsize.output
以上是关于如果文件在5分钟内增长超过一定大小,则发出警报的主要内容,如果未能解决你的问题,请参考以下文章