如果文件在5分钟内增长超过一定大小,则发出警报

时间:2018-02-20 10:24:18

标签: bash scripting

我试图编写一个脚本来检查文件在过去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

2 个答案:

答案 0 :(得分:4)

我注意到的第一件事是,在第一行中,变量赋值(output="~/log/logsize.output")会导致问题,因为~未在引号中展开。但是,此脚本中存在更多错误,我建议花更多时间学习shell脚本的基础知识;我建议将Greg’s Wiki作为一个很好的起点。

while ago,我更新了代码的使用指南,以便在https://www.shellcheck.net/中包含检查shell脚本的建议,这是一个很棒的资源。实际上,Shellcheck警告道具问题,并包含使用$HOME代替~的有用建议。而不是重新讨论所有 Shellcheck会警告你的问题,我只会提到一些它没有注意到的问题:

命令替换

currentsize=(stat '-c%s' "~/log/input.log")

我想你打算使用命令替换,以便currentsize变量包含stat命令的输出。这应该写成:

currentsize=$(stat '-c%s' "$HOME/log/input.log")

算术比较

Shellcheck在到达此行之前停止处理,但我注意到您使用>作为算术比较运算符:

if (${difference} > 1999999) ;

在POSIX(类Unix操作系统的标准)shell中,这些运算符用于输入和输出重定向 - 正如您在

中所做的那样
echo "Log File is within expected parameters" > "~/log/logalert.log"

POSIX shell中用于算术比较的正确运算符是-gt portable 测试方法是:

if [ "$difference" -gt 1999999 ]

注意:bashksh等外壳使用<>进行算术比较,但这只适用于 double 括弧。见Comparing integers: arithmetic expression or conditional expression。在Bash中,>[[构造一起使用时也可用于字符串比较。请参阅Bash Conditional Expressions

另外:如果字符串包含由shell特别解释的异常字符(例如,空格导致分词),则只需要引用字符串。但是,如果您已经习惯使用其他编程语言并且发现它更具可读性,那么这样做没有任何害处。

一般提示

  • 始终引用变量扩展(除非明确要求分词)
  • 调试时使用set -x
  • set -e对于获取错误通知非常有用,例如尝试访问不存在的变量的内容。

答案 1 :(得分:1)

我只是想提供我的解决方案:

#!/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 '\n' '-' | 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