日志管理
日志是系统运维中重要的信息源,通过有效的日志管理可以快速定位问题、分析系统行为和进行安全审计。
系统日志概述
主要日志文件位置
# 系统主要日志目录
/var/log/
# 常见系统日志文件
/var/log/messages # 系统消息日志
/var/log/syslog # 系统日志(Ubuntu/Debian)
/var/log/secure # 安全认证日志(CentOS/RHEL)
/var/log/auth.log # 认证日志(Ubuntu/Debian)
/var/log/cron # 定时任务日志
/var/log/maillog # 邮件系统日志
/var/log/boot.log # 系统启动日志
/var/log/dmesg # 内核环形缓冲区日志
应用程序日志
# Web服务器日志
/var/log/nginx/ # Nginx日志
/var/log/apache2/ # Apache日志
/var/log/httpd/ # Apache日志(CentOS)
# 数据库日志
/var/log/mysql/ # MySQL日志
/var/log/postgresql/ # PostgreSQL日志
# 其他应用日志
/var/log/redis/ # Redis日志
/var/log/mongodb/ # MongoDB日志
日志查看和分析
基本查看命令
# 查看日志文件
cat /var/log/messages
less /var/log/messages
tail /var/log/messages
# 实时查看日志
tail -f /var/log/messages
tail -F /var/log/messages # 文件轮转后继续跟踪
# 查看指定行数
head -n 50 /var/log/messages
tail -n 100 /var/log/messages
# 查看特定时间段日志
grep "2024-01-15" /var/log/messages
使用journalctl查看systemd日志
# 查看所有日志
journalctl
# 查看特定服务日志
journalctl -u nginx
journalctl -u sshd
# 实时查看日志
journalctl -f
journalctl -u nginx -f
# 查看特定时间范围日志
journalctl --since "2024-01-15 00:00:00"
journalctl --since "1 hour ago"
journalctl --since yesterday
# 查看系统启动日志
journalctl -b
journalctl -b -1 # 上次启动
日志搜索和过滤
# 搜索关键词
grep "error" /var/log/messages
grep -i "failed" /var/log/secure # 忽略大小写
# 搜索多个文件
grep "404" /var/log/nginx/*.log
# 使用正则表达式
grep -E "error|warning|critical" /var/log/messages
# 排除特定内容
grep -v "INFO" /var/log/application.log
# 统计匹配行数
grep -c "error" /var/log/messages
日志轮转管理
logrotate配置
# 主配置文件
/etc/logrotate.conf
# 应用特定配置
/etc/logrotate.d/
# 查看logrotate配置
cat /etc/logrotate.conf
ls /etc/logrotate.d/
创建logrotate配置
# 创建应用日志轮转配置
cat > /etc/logrotate.d/myapp << 'EOF'
/var/log/myapp/*.log {
daily # 每天轮转
missingok # 忽略不存在的文件
rotate 30 # 保留30个备份
compress # 压缩旧日志
delaycompress # 延迟压缩
notifempty # 空文件不轮转
sharedscripts # 共享脚本
postrotate
/bin/systemctl reload myapp
endscript
}
EOF
手动执行日志轮转
# 测试logrotate配置
logrotate -d /etc/logrotate.conf
# 强制执行日志轮转
logrotate -f /etc/logrotate.conf
# 查看logrotate状态
cat /var/lib/logrotate/status
日志分析工具
awk分析日志
# 统计访问IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
# 分析HTTP状态码
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c
# 统计每小时访问量
awk '{print $4}' /var/log/nginx/access.log | cut -c14-15 | sort | uniq -c
# 查找特定时间段日志
awk '/15\/Jan\/2024:09:/' /var/log/nginx/access.log
sed处理日志
# 提取特定时间段
sed -n '/2024-01-15 09:00/,/2024-01-15 10:00/p' /var/log/messages
# 删除空行
sed '/^$/d' /var/log/application.log
# 替换敏感信息
sed 's/password=[^&]*/password=****/g' /var/log/application.log
使用cut提取字段
# 提取nginx日志的IP和状态码
cut -d' ' -f1,9 /var/log/nginx/access.log
# 提取系统日志的时间和进程
cut -d' ' -f1-3,5 /var/log/messages
日志监控和告警
实时日志监控脚本
#!/bin/bash
# log_monitor.sh
LOG_FILE="/var/log/messages"
ERROR_COUNT_THRESHOLD=10
ALERT_EMAIL="admin@example.com"
# 监控错误日志
ERROR_COUNT=$(grep "$(date '+%b %d')" $LOG_FILE | grep -i error | wc -l)
if [ $ERROR_COUNT -gt $ERROR_COUNT_THRESHOLD ]; then
echo "High error count detected: $ERROR_COUNT errors today" | \
mail -s "Log Alert: High Error Count" $ALERT_EMAIL
fi
# 监控特定关键词
tail -f $LOG_FILE | while read line; do
if echo "$line" | grep -qi "out of memory\|segmentation fault\|kernel panic"; then
echo "Critical error detected: $line" | \
mail -s "CRITICAL: System Error" $ALERT_EMAIL
fi
done
使用multitail监控多个日志
# 安装multitail
yum install multitail # CentOS/RHEL
apt install multitail # Ubuntu/Debian
# 同时监控多个日志文件
multitail /var/log/messages /var/log/secure
# 使用配置文件
multitail -i /var/log/nginx/access.log -i /var/log/nginx/error.log
日志安全和审计
保护日志文件
# 设置适当权限
chmod 640 /var/log/secure
chown root:adm /var/log/secure
# 创建只读用户
useradd -r -s /bin/false logread
usermod -a -G adm logread
日志远程传输
# 配置rsyslog远程传输
echo "*.* @@logserver.example.com:514" >> /etc/rsyslog.conf
systemctl restart rsyslog
# 使用syslog-ng
echo 'destination d_remote { tcp("logserver.example.com" port(514)); };' >> /etc/syslog-ng/syslog-ng.conf
echo 'log { source(s_src); destination(d_remote); };' >> /etc/syslog-ng/syslog-ng.conf
日志分析实用技巧
性能分析
# 分析慢查询
grep "slow query" /var/log/mysql/mysql-slow.log
# 分析响应时间
awk '{if($10 > 1000) print $0}' /var/log/nginx/access.log
# 统计每分钟请求数
awk '{print $4}' /var/log/nginx/access.log | cut -c14-18 | sort | uniq -c
安全分析
# 查找失败的登录尝试
grep "Failed password" /var/log/secure
# 统计攻击来源IP
grep "Failed password" /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr
# 查找sudo使用记录
grep "sudo" /var/log/secure
故障排查
# 查找内存不足错误
grep -i "out of memory\|oom-killer" /var/log/messages
# 查找磁盘错误
grep -i "i/o error\|disk error" /var/log/messages
# 查找网络问题
grep -i "network\|connection" /var/log/messages
日志管理最佳实践
规范日志格式
- 统一时间格式
- 包含必要上下文信息
- 使用结构化日志格式(JSON等)
合理设置日志级别
- ERROR: 错误事件
- WARN: 警告事件
- INFO: 信息事件
- DEBUG: 调试信息
定期清理和归档
- 设置合理的保留期限
- 压缩历史日志
- 归档重要日志
建立监控告警
- 监控错误日志数量
- 关键字告警
- 日志文件大小监控
保障日志安全
- 限制访问权限
- 防止日志篡改
- 考虑远程备份