在Debian系统中,可借助inotify-tools工具包,通过以下方式利用inotify进行日志分析:
安装工具:
sudo apt-get update && sudo apt-get install inotify-tools
实时监控日志文件:
使用inotifywait监控日志文件变化(如/var/log/syslog),触发事件时执行分析脚本:
inotifywait -m -e modify,create,delete /var/log/syslog | while read path action file; do
echo "$(date) - $action: $file" >> /var/log/log_analysis.log
# 调用分析工具(如grep、awk)处理日志
done
处理日志轮转:
监控日志目录,检测到新日志文件创建时(如logrotate轮转后),自动触发分析:
inotifywait -m -e create /var/log/ | while read path action file; do
if [[ "$file" == "syslog"* ]]; then
# 执行日志分析逻辑
fi
done
集成分析工具:
结合grep、awk等工具过滤关键日志,或调用logwatch、ELK Stack等工具进行深度分析:
inotifywait -m -e modify /var/log/auth.log | while read path action file; do
grep "Failed password" "$file" >> /var/log/failed_login.log
done
注意事项:
/var/log/)时,需确保脚本高效,防止资源占用过高。