【问题标题】:problem with shell script to get last logs for 10min获取最后日志 10 分钟的 shell 脚本问题
【发布时间】:2019-09-23 21:51:56
【问题描述】:

我编写了一个脚本来检查最后 10 分钟的最后一个 httpd 错误日志,但我得到一个错误,它一直在循环:问题是脚本的输出是一个错误循环,直到日志文件结束 我通过运行 cmd date --date='-10min' 来获取日期 - 10 分钟,然后我逐行解析日志文件,然后检查日志文件中每一行的小时和分钟是否大于或等于日期的小时和分钟 -10 分钟 这是输出的一部分:

./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found
./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found
./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found
./test2.sh: line 26: [: -ge: unary operator expected
./test2.sh: line 26: [Mon: command not found

当我在这里尝试调试它时,这是问题的一部分,并且它在日志文件的每一行中重复:

+ IFS=
+ read -r line
+ errorBool=0
+ [[ [Sun Apr 28 03:52:39.791442 2019] [autoindex:error] [pid 15012] 
[client 127.0.0.1:49054] AH01276: Cannot serve directory /var/www/html/: 
No matching DirectoryIndex (index.html,index.php) found, and server- 
generated directory index forbidden by Options directive == *:error* ]]
++ awk -F : '{printf $1}'
++ '[Sun' Apr 28 03:52:39.791442 '2019]' '[autoindex:error]' '[pid' 
'15012]' '[client' '127.0.0.1:49054]' AH01276: Cannot serve directory 
/var/www/html/: No matching DirectoryIndex '(index.html,index.php)' found, 
and server-generated directory index forbidden by Options directive
test2.sh: line 26: [Sun: command not found
++ awk '{printf $4}'
+ '[' -ge 12 ']'
test2.sh: line 26: [: -ge: unary operator expected

代码如下:

#!/bin/bash


#SCRIPT TO CHECK THE ERROR LOGS IN THE LAST 10 MINS

#VARIABLES

#NUMBER OF ERROR LOGS
errorCount=0
DATE_10_MIN=$(date --date='-10min' | awk '{print $4}' )
DATE_10_MIN_HOURS=$(date --date='-10min' | awk '{print $4}' | awk -F : 
'{print $1} ')
DATE_10_MIN_MIN=$(date --date='-10min' | awk '{print $4}' | awk -F : 
'{print $2} ')

#_______________________#

while IFS= read -r  line ; do


#BOOLEAN TO CHECK IF THE LINE HAVE THE EXPRESSION
errorBool=0


#if [[ $($line | awk '{print $4 }' | cut -c-8) -gt $DATE_10_MIN   ]] ; 
then

    if [[ $line  == *:error*  ]] ; then
            if [ [ $($line  | awk  '{print $4}' | awk -F : '{print $1}' )  
-ge $DATE_10_MIN_HOURS ] && [ $($line | awk  '{print $4}' | awk -F : 
'{print $2}')  -ge $DATE_10_MIN_MIN ] ]; then
            errorBool=1
            (( errorCount++ ))
            echo $line
            fi
    fi
 done < /var/log/httpd/error_log

 echo "There were $errorCount error logs in the last 10 mins "

【问题讨论】:

  • 欢迎来到本站! (1) 我看到你有if [ [ ] ]; then,括号之间有空格。如果删除空格,问题会消失吗? (2)edit your question 提供问题的详细信息?脚本是否因错误而终止,还是会产生意外结果? (3) 您是否还可以编辑问题以添加一个小的输入样本以及预期和实际输出?这将有助于我们进行测试。谢谢!
  • 我删除了空格并没有什么特别的,因为该脚本用于在最后 10 分钟内获取错误编号,该脚本的输出为:./test2.sh: line 26: [Mon:找不到命令 ./test2.sh:第 26 行:[:-ge:预期的一元运算符 ./test2.sh:第 26 行:[星期一:找不到命令 ./test2.sh:第 26 行:[:-ge:一元运算符预期 ./test2.sh:第 26 行:[星期一:找不到命令 ./test2.sh:第 26 行:[:-ge:预期一元运算符 ./test2.sh:第 26 行:[星期一:找不到命令它循环直到日志文件结束

标签: bash apache shell logging awk


【解决方案1】:

这适用于我的测试系统。要正确测试它,您必须更改输入数据中的日期:)。

代码

#!/bin/bash
#Script to check the error logs in the last 10 mins

#Variables.  Note: user variables should not be all uppercase.
errorCount=0
    # Number of error logs found
date_10_min_ago="$(date --date='-10min' +'%s')"
    # Time in seconds so it can be compared.
    # See https://unix.stackexchange.com/a/170982/63804 .

#_______________________#

while IFS= read -r line ; do
    if [[ $line  == *:error*  ]] ; then
        line_timestamp="$(awk -F '[][]' '{print $2}' <<<"$line")"
            # Get the date string, which is between brackets
        record_time="$(date --date="$line_timestamp" +'%s')"
            # Convert the date string to seconds.
            # Thanks to https://stackoverflow.com/a/1842754/2877364

        if (( record_time > date_10_min_ago)) ; then
            (( ++errorCount ))
            echo $line
        fi
    fi
done < 178.txt

echo "There were $errorCount error logs in the last 10 mins "

# vi: set ts=4 sts=4 sw=4 et ai:

示例输入

[Mon May 6 07:35:39.791442 2019] [autoindex:error] [pid 15012] [client 127.0.0.1:49054] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive - this error is older than 10 min
[Mon May 6 08:35:39.791442 2019] [autoindex:error] [pid 15012] [client 127.0.0.1:49054] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive
[Mon May 6 08:35:40.123456 2019] [autoindex:success] [pid 15012] [client 127.0.0.1:1337] Example input that doesn't contain the word "e r r o r"

输出

[Mon May 6 08:35:39.791442 2019] [autoindex:error] [pid 15012] [client 127.0.0.1:49054] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive
There were 1 error logs in the last 10 mins

说明

  • 如果日期是数字,比较日期会容易得多。 date +'%s' format 给出每个日期的秒数*
  • 您可以从带有awk -F '[][]' 的行中提取时间戳。 [][] 是匹配[] 的正则表达式。因此,awk -F '[][]' '{print $2}' 获取错误日志时间戳的文本。然后date 用于convert that date text 以秒为单位进行比较。
  • 通用 cmets:如果我想将所有输出放在一起,我更喜欢将我的所有 $() 包装为 "$()"。我认为这更清楚。
  • 正如@lojza 也提到的,一般来说,任何 bash 命令都必须完全在一行上。所以DATE_10_MIN_HOURS=$(some random code) 很好,但是

    DATE_10_MIN_HOURS=$(some random code
    and more code on a different line)
    

    会导致错误。

* 这可能无法在闰秒内按原样工作。

【讨论】:

  • 感谢它的帮助,也感谢您的建议:)
  • @taruk 谢谢!我也可以投票吗?你接受了答案,这很好——这意味着这个答案解决了你的问题。赞成票表明答案是有帮助的,这可能并不总是相同的;)。请参阅tour 了解更多有关该网站如何运作的信息。感谢您考虑此请求!
  • cxw,我认为他们需要 15 的“声誉”(我认为)才能获得该特权。 (不过我喜欢这个答案,+1。)@taruk——我绝对支持 cxw 的评论:正是投票将这个资源(stackoverflow)与许多其他“论坛”区分开来。一旦你有那些(15?)点,我鼓励你在看到你喜欢的帖子时在网站上投票(绝对是这个,因为它甚至回答了你的问题)。
  • @zdim 感谢您的提醒 - 我忘记了 15 级。是时候允许请求 OP 对他们自己的问题的答案进行投票了:)。
  • @cxw 哈——好主意!这确实应该被允许。 (也许......鼓励...... +1?直到他们得到他们的 15。)
【解决方案2】:

我在awk -F : 之后看到换行符,我会写:

DATE_10_MIN=`date --date='-10min' '+%H:%M:%S'` 
DATE_10_MIN_HOURS=`date --date='-10min' '+%H'`
DATE_10_MIN_MIN=`date --date='-10min' '+%M'`

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 2019-06-03
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多