[英]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.