【问题标题】:Log monitoring using shell script and send mail使用shell脚本监控日志并发送邮件
【发布时间】:2017-11-16 01:07:48
【问题描述】:

帮助编写脚本。以下是日志格式。 我想编写一个在 LIVE 日志中搜索关键字的脚本。假设有些人已经停止了服务器,它会显示 shutdown 或 force_shutdown 它还在日志中显示“服务器关闭已由 $user 启动”。

<Apr 19, 2017 1:11:00 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to STARTING> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <Log Management> <BEA-170027> <The Server has established connection with the Domain level Diagnostic Service successfully.> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to shutdown> 
<Apr 19, 2017 1:11:06 PM EDT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to force_shutdown> 

<Jan 9, 2008 6:50:30 PM EST> <Notice> <WebLogicServer> <BEA-000388> <JVM
called WLS shutdown hook. The server will force shutdown now> 
<Jan 9, 2008 6:50:30 PM EST> <Alert> <WebLogicServer> <BEA-000396> <Server shutdown has been requested by <WLS Kernel>> 
<Jan 9, 2008 6:50:30 PM EST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to FORCE_SHUTTING_DOWN>

想要 grep 所有信息服务器 IP 和服务器主机,以及摊牌时的确切时间戳以及哪个用户。并向用户发送包含所有详细信息的邮件。 请帮帮我

【问题讨论】:

    标签: bash shell scripting sh weblogic12c


    【解决方案1】:

    您可以使用这样的脚本读取实时文件:(备注:该脚本不会读取整个文件,只会读取新的后续行)

    #!/bin/bash
    
    LOG_FILE="/var/log/foo"
    
    tail -n 0 -f "$LOG_FILE" | while IFS= read -r line; do
         echo $line
    done
    

    之后您可以使用grep轻松搜索您想要的字符串

    #!/bin/bash
    
    LOG_FILE="/var/log/foo"
    SEARCHED="Server shutdown"
    
    tail -n 0 -f "$LOG_FILE" | while IFS= read -r line; do
        if [ $(echo "${line}" | grep "${SEARCHED}") ] ; then
             echo "String find in : $line"
        fi
    done
    

    最后一部分,您可以使用awk 解析该行以提取您想要的内容并将其发送。在谷歌上搜索,你会找到很多例子:)

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2010-12-19
      • 2011-07-06
      • 2010-12-21
      • 1970-01-01
      • 2014-10-13
      相关资源
      最近更新 更多