📖 知识点简介
日志是运维排障的第一手资料。学了 grep、awk、tail -f 之后,面对几百 MB 的 access.log 或 syslog,手动搜索效率太低。进阶工具能帮你自动汇总、可视化、定时报告:
- Logwatch — 日志分析报告生成器,适合系统日志(auth、cron、yum 等)的每日巡检
- GoAccess — 实时 Web 日志分析器,针对 Nginx/Apache access log 生成交互式仪表盘
两者搭配:Logwatch 兜底系统安全巡检,GoAccess 聚焦 Web 访问分析。
🔧 核心命令/配置整理
1. Logwatch 安装与配置
# 安装(EPEL 源)
yum install -y epel-release
yum install -y logwatch
# 或 Ubuntu
apt install -y logwatch
# 查看默认配置
cat /usr/share/logwatch/default.conf/logwatch.conf
# 生成一次报告(不发送邮件,直接输出到终端)
logwatch --detail High --service All --range today --print
# 常用参数组合
logwatch --detail Med --service sshd --range yesterday --print
logwatch --detail High --service pam_unix --range "7 days ago" --print
# 配置定时报告(默认 crontab 在 /etc/cron.daily/0logwatch)
# 默认每天早上发邮件到 root,修改接收邮箱:
vim /usr/share/logwatch/default.conf/logwatch.conf
# MailTo = ops@company.com
2. Logwatch 关键配置项
# /usr/share/logwatch/default.conf/logwatch.conf
Detail = High # 报告详略: Low / Med / High
MailTo = root # 收件人
Range = yesterday # 时间范围: yesterday, today, all
Service = All # 监控的服务,可用 All 或指定 sshd, sudo, yum...
Format = text # 输出格式: text / html
指定多个 Service:
# 只关注 ssh 登录和 sudo 提权记录
logwatch --service sshd --service sudo --range today --print
3. GoAccess 安装与 Web 日志分析
# 安装 GoAccess
yum install -y goaccess
# 或从源码编译(获取最新版)
wget https://tar.goaccess.io/goaccess-1.9.3.tar.gz
tar -xzf goaccess-1.9.3.tar.gz
cd goaccess-1.9.3
./configure --enable-utf8 --enable-geoip=mmdb
make && make install
# 实时分析 Nginx access log(交互式终端 UI)
goaccess /var/log/nginx/access.log -o /tmp/report.html --log-format=COMBINED
# 生成本地 HTML 报告
goaccess /var/log/nginx/access.log \
--log-format=COMBINED \
--date-format='%d/%b/%Y' \
--time-format='%T' \
-o /var/www/html/report.html
# 实时模式(按 F5 刷新)
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --real-time-html
4. 自定义 Nginx 日志格式(如果用了非标准格式)
# /etc/nginx/nginx.conf
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
然后在 GoAccess 中指定对应格式:
goaccess /var/log/nginx/access.log \
--log-format='%h - %^ [%d:%t %^] "%r" %s %b "%R" "%u" "%^"' \
--date-format='%d/%b/%Y' --time-format='%T' \
-o /var/www/html/report.html
🧪 实操示例:每日日志巡检流水线
场景:每天 08:00 生成系统日志报告 + Web 访问热力图
# 步骤 1:定时生成 GoAccess HTML 报告
mkdir -p /var/www/logs-reports
cat > /usr/local/bin/gen-log-report.sh << 'SCRIPT'
#!/bin/bash
DATE=$(date +%Y%m%d)
LOGFILE="/var/log/nginx/access.log"
OUTDIR="/var/www/logs-reports"
goaccess $LOGFILE --log-format=COMBINED -o $OUTDIR/report-$DATE.html
# 保留最近 30 天,清理旧的
find $OUTDIR -name "report-*.html" -mtime +30 -delete
SCRIPT
chmod +x /usr/local/bin/gen-log-report.sh
# 步骤 2:添加到 crontab
crontab -e
# 每天 07:55 生成报告
55 7 * * * /usr/local/bin/gen-log-report.sh
# 步骤 3:配置 Logwatch 每天邮件报告
# 确保 /etc/cron.daily/0logwatch 存在且可执行
ls -l /etc/cron.daily/0logwatch
# 步骤 4:手动巡检示例
echo "=== 今日 SSH 登录异常 ==="
logwatch --service sshd --range today --print | grep -i "failed|invalid|error"
echo "=== 访问量 Top 10 IP ==="
goaccess /var/log/nginx/access.log --log-format=COMBINED -o - 2>/dev/null | \
awk '/Visitors/ {print $2}'
⚠️ 常见坑点/注意事项
1. Logwatch 默认日志格式限制
Logwatch 强依赖 syslog 的原始格式。如果系统用了 rsyslog 的自定义模板($template),Logwatch 可能匹配不上。解决方案:保持默认 syslog 格式,或编写自定义 Logwatch 服务过滤器(/usr/share/logwatch/scripts/services/)。
2. GoAccess 日志格式必须精确匹配
- 先核对 Nginx 的
log_format定义 - 用
goaccess --debug-file=/tmp/goaccess-debug.log调试解析问题 - 常用格式速查:
COMBINED— Nginx 默认 combined 格式VCOMBINED— 带X-Forwarded-ForW3C— IIS 标准格式
3. 大型日志文件性能
- GoAccess 加载 1GB+ 日志时占用内存较高(约 200-400MB)
- 建议用
split拆分或限制范围:goaccess --date-spec=day - Logwatch 对大文件采用流式处理,内存占用很低
4. 报告不要暴露到公网
GoAccess 生成的 HTML 会暴露 IP、UA、URL 路径等数据。建议:
# Nginx 加 Basic Auth
location /logs-reports/ {
alias /var/www/logs-reports/;
auth_basic "Ops Dashboard";
auth_basic_user_file /etc/nginx/.htpasswd;
}
5. Logwatch 邮件没收到?
# 检查本地邮件队列
mailq
# 检查 postfix/sendmail 是否运行
systemctl status postfix
# 临时改为本地输出测试
logwatch --detail High --range today --print
一句话总结:Logwatch 是自动化的系统日志看门狗,GoAccess 是 Web 流量的实时雷达——两者加起来,等于运维的"上帝视角"。