简体   繁体   English

监视关键字的日志文件,然后发送电子邮件通知

[英]Monitor log file for a keyword then send an email notification

I want to monitor a log that's continually generating, for a keyword to let me know if a component is down. 我想监视一个不断生成的日志,关键字让我知道组件是否已关闭。 Once the keyword is identified, I'd like to be notified via email to my personal gmail. 识别出关键字后,我希望通过电子邮件通知我的个人Gmail。

So far I've been able to accomplish the keyword monitoring and email notification separately but never together in a script. 到目前为止,我已经能够单独完成关键字监控和电子邮件通知,但从未在脚本中一起完成。 I've tried the following for monitoring: 我已尝试以下方法进行监控:

tail -fn0 user | awk '/disconnect_tcp_conn/ { print | "echo server down" }'

And this to email: 这是电子邮件:

mail -s "server down.... again" blahblah@gmail.com < /dev/null

I tried grep for monitoring but it seemed like awk worked best.. at least for how I used it. 我尝试使用grep进行监控,但似乎awk工作得最好..至少我是如何使用它的。 Just can't seem to get the syntax correct to make both work at the same time. 似乎无法使语法正确,以使两者同时工作。

I've tried: 我试过了:

tail -fn0 user | awk '
                    /disconnect_tcp_conn/ { print | "echo server down") }
                    /disconnect_tcp_conn/ { system( "mail -s "server down.... again" blahblah@gmail.com < /dev/null ") }'

and also: 并且:

tail -fn0 user | awk '
                    /disconnect_tcp_conn/ { print | "echo server down") }
                    /disconnect_tcp_conn/ { cmd=mail -s "server down.... again3" blahblah@gmail.com < /dev/null ") }'

Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!

You can have multiple lines in an {action} , and you can escape the double quotes \\" that you want to pass through system() : 您可以在多行{action} ,你可以逃脱双引号\\"要通过system()

tail -fn0 user | awk '/disconnect_tcp_conn/ { 
    system("mail -s \"server down... again\" blahblah@gmail.com </dev/null") 
    print "server down... again"
    exit
}'

It can simply also be done as below : 它也可以简单地完成如下:

tail -f /path/to/file | while read line
do case "$line" in
        *"server down"*) echo "$line" | mutt -s "Server Down" an_email@mail.com;
        ;;
   esac
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bash脚本监视日志,匹配关键字然后发送命令,恢复监视 - Bash script to monitor log, match keyword then send commands, resume monitoring 如果关键字触发然后执行命令,Shellscript 监视日志文件? - Shellscript to monitor a log file if keyword triggers then execute a command? Shellscript监视日志文件(如果关键字触发),然后运行snmptrap命令 - Shellscript to monitor a log file if keyword triggers then run the snmptrap command Bash 脚本用于监控多个路径并在添加新文件时结束 email 通知 - Bash script to monitor multiple paths and end email notification when a new file is added 如何从单个cronjob发送电子邮件和存储日志文件? - How to send email and store log file from a single cronjob? 监视与模式匹配的日志文件写入速率 - Monitor the rate of log file writes matching a pattern Jenkins管道脚本:通过发送电子邮件通知 - Jenkins Pipeline Script: Send Email notification through 在Linux中使用Shell脚本监视日志并发送警报邮件 - Monitor log and send Alert mail using shell script in Linux 命令行:监视日志文件并将数据添加到数据库 - Command line: monitor log file and add data to database 如何通过编程方式秘密地发送通知(电子邮件,短信等) - how to send notification (email, SMS, whatever) stealthy programacticly linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM