简体   繁体   English

使用 wget 每 5 分钟下载一次日志文件并检测更改

[英]using wget to download log file every 5 mins & detect changes

i am writing a bash script to accomplish the following.我正在编写一个 bash 脚本来完成以下任务。

  1. script runs wget every five minutes to download a small log from a static url.脚本每五分钟运行一次 wget 以从静态 url 下载一个小日志。
  2. script uses diff to see if there are any new entries made to the log file (new entries are made at the end of log file).脚本使用 diff 来查看日志文件中是否有任何新条目(新条目在日志文件末尾创建)。
  3. if new log entries are found - extract the new entries to a new file, format them properly, send me an alert, return to #1.如果找到新的日志条目 - 将新条目提取到新文件中,正确格式化它们,向我发送警报,返回 #1。
  4. if no new log entries are found, go back to #1.如果没有找到新的日志条目,请返回 #1。
wget "https://url-to-logs.org" -O new_log
if diff -q new_log old_log; then
echo "no new log entries to send."
else
echo "new log entries found, sending alert."
diff -u new_log old_log > new_entries

#some logic i have to take the output of "new_entries", properly format the text and send the alert.

rm -rf old_log new_entries
cp new_log old_log
rm -rf new_log
fi

there is one additional thing - every night at midnight the server hosting the logs deletes all entries and displays a blank file until new log entries are made for the new day.还有一件事 - 每晚午夜,托管日志的服务器都会删除所有条目并显示一个空白文件,直到为新的一天创建新的日志条目。

i guess i could always run a cron job at midnight to run "rm -rf" and "touch" the old_log file, but curious if an easier way to do this exists.我想我总是可以在午夜运行一个 cron 作业来运行“rm -rf”并“触摸”old_log 文件,但很好奇是否存在更简单的方法。

thanks in advance for any/all input and help.提前感谢任何/所有输入和帮助。

If your logs are not rotating - ie the old log is guaranteed to be the prefix of the new log, you can just use tail to get the new suffix - something like this:如果您的日志没有轮换 - 即旧日志保证是新日志的前缀,您可以使用tail获取新后缀 - 如下所示:

tail -n+$(( $(wc -l old_log) + 1 )) new_log > new_entries

If there are no new lines in new_log , the new_entries file will be empty, which you can check using stat or some other way.如果new_log中没有新行,则new_entries文件将为空,您可以使用stat或其他方式检查。

If your logs are rotating, you should first use grep to check if the last line from the old log exists in the new log, and if not - assume the entire new log is new:如果您的日志正在轮换,您应该首先使用grep检查旧日志的最后一行是否存在于新日志中,如果不存在 - 假设整个新日志是新的:


if ! egrep -q "^$(tail -n1 old_log)\$" new_log; then cat new_log > new_entries; fi

If all the lines in your log file are unique then you could use grep :如果日志文件中的所有行都是唯一的,那么您可以使用grep

wget "https://url-to-logs.org" -O new_log || exit 1

if new_entries=$(grep -vxFf old_log new_log)
then
    # format and send alert
    printf '%s\n' "$new_entries"
fi

mv -f new_log old_log

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM