帮助写一个脚本。以下是日志格式。
我想编写一个在实时日志中搜索关键字的脚本。假设有些服务器已停止,它将显示 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 和服务器主机,并在摊牌时以及由哪个用户提供准确的时间戳。并向用户发送邮件,其中包含用户的所有详细信息。
请帮我
您可以使用这样的脚本实时读取文件:(备注:脚本不读取整个文件,只读取未来的新行)
#!/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
提取您想要的内容并将其发送。在谷歌上搜索一下,你会发现很多例子:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)