日志监控是Linux服务器管理的重要环节,它能帮助管理员及时发现系统问题、安全威胁和性能瓶颈。以下是全面的Linux日志监控方案:
/var/log/messages
- 常规系统消息/var/log/auth.log
- 认证和安全相关消息/var/log/kern.log
- 内核日志/var/log/cron
- 定时任务日志/var/log/maillog
- 邮件服务器日志/var/log/httpd/
- Apache日志目录/var/log/mysql.log
- MySQL数据库日志# 实时查看日志更新
tail -f /var/log/messages
# 查看特定服务的日志
journalctl -u nginx.service -f
# 按关键词过滤日志
grep "error" /var/log/syslog
multitail -i /var/log/messages -i /var/log/auth.log
lnav /var/log/messages
# 示例配置 /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
/etc/init.d/nginx reload > /dev/null
endscript
}
logrotate -vf /etc/logrotate.conf
#!/bin/bash
# 监控错误日志并发送警报
ERROR_COUNT=$(grep -i "error" /var/log/messages | wc -l)
if [ "$ERROR_COUNT" -gt 10 ]; then
echo "High error count detected: $ERROR_COUNT" | mail -s "Server Alert" admin@example.com
fi
# 监控磁盘空间
DISK_USAGE=$(df -h | grep "/dev/sda1" | awk '{print $5}' | cut -d'%' -f1)
if [ "$DISK_USAGE" -gt 90 ]; then
echo "Disk usage exceeded 90%" | mail -s "Disk Alert" admin@example.com
fi
通过以上方法,您可以建立全面的Linux服务器日志监控体系,及时发现并解决系统问题,保障服务器稳定运行。