在实现自动化分析前,需先规范日志的收集、存储格式,确保日志数据的一致性和可处理性。
rsyslog是Ubuntu默认的日志收集工具,可将分散在各个服务的日志集中存储到指定目录(如/var/log/centralized/),便于后续统一分析。
编辑配置文件/etc/rsyslog.conf,添加以下内容将所有日志转发到集中目录:
*.* /var/log/centralized/syslog 重启rsyslog服务使配置生效:sudo systemctl restart rsyslog。
日志文件过大不仅占用磁盘空间,还会降低分析效率。logrotate可自动压缩、删除旧日志,并保留指定数量的归档文件。
编辑/etc/logrotate.d/rsyslog(针对rsyslog日志),添加以下配置:
/var/log/centralized/syslog { daily # 每天轮转 missingok # 忽略缺失文件 rotate 7 # 保留7份归档 compress # 压缩旧日志(如.gz格式) notifempty # 空日志不轮转 create 0640 root adm # 新日志文件权限 } logrotate默认通过cron每日自动运行,无需手动触发。
根据需求复杂度选择合适的工具,以下是常见方案的配置步骤:
logwatch是一款轻量级日志分析工具,可自动生成包含错误、警告等关键信息的邮件报告,适合快速了解系统状态。
安装logwatch:sudo apt install logwatch
编辑配置文件/usr/share/logwatch/default.conf/logwatch.conf,调整以下参数:
Title = "Ubuntu System Log Summary" # 报告标题 LogFile = syslog # 分析的日志文件 *OnlyService = sshd # 仅分析sshd服务(可选) MailTo = your_email@example.com # 接收报告的邮箱 设置cron每日自动运行(默认已配置):sudo systemctl enable logwatch.timer,报告将发送至指定邮箱。
通过bash脚本结合grep、awk等命令,可实现自定义的自动化分析任务(如统计错误日志数量、检测失败登录)。
示例脚本count_errors.sh(统计syslog中的ERROR数量):
#!/bin/bash ERROR_COUNT=$(grep -c "ERROR" /var/log/centralized/syslog) echo "$(date): Total ERROR logs: $ERROR_COUNT" >> /var/log/error_stats.log 赋予执行权限:chmod +x count_errors.sh
设置cron每小时运行:编辑/etc/crontab,添加以下行:
0 * * * * root /path/to/count_errors.sh 该脚本会将错误数量记录到/var/log/error_stats.log中,便于后续查看趋势。
ELK Stack(Elasticsearch+Logstash+Kibana)适合大规模日志分析,支持实时搜索、可视化仪表板和告警。
sudo apt install elasticsearch,修改/etc/elasticsearch/elasticsearch.yml中的network.host为localhost,启动服务:sudo systemctl start elasticsearch。sudo apt install logstash,创建配置文件/etc/logstash/conf.d/logstash.conf,定义输入(从rsyslog接收日志)、过滤(提取关键字段)、输出(发送到Elasticsearch):input { file { path => "/var/log/centralized/syslog" start_position => "beginning" } } filter { grok { match => { "message" => "%{SYSLOGTIMESTAMP:timestamp} %{HOSTNAME:hostname} %{DATA:program}(?:\[%{POSINT:pid}\])?: %{GREEDYDATA:message}" } } date { match => [ "timestamp", "MMM dd HH:mm:ss", "MMM d HH:mm:ss", "yyyy-MM-dd HH:mm:ss" ] } } output { elasticsearch { hosts => ["localhost:9200"] } stdout { codec => rubydebug } } 启动Logstash:sudo systemctl start logstash。sudo apt install kibana,修改/etc/kibana/kibana.yml中的server.host为localhost,启动服务:sudo systemctl start kibana。http://localhost:5601,即可创建仪表盘展示错误日志趋势、服务状态等可视化内容。自动化分析的核心价值在于及时响应异常,可通过以下方式实现告警:
在logwatch配置中,通过MailTo参数指定接收邮箱,当检测到ERROR日志时,自动发送报告。
或在Shell脚本中添加邮件发送功能(需安装mailutils):
#!/bin/bash ERROR_COUNT=$(grep -c "ERROR" /var/log/centralized/syslog) if [ "$ERROR_COUNT" -gt 5 ]; then # 阈值设置为5 echo "ERROR count exceeds threshold: $ERROR_COUNT" | mail -s "High Error Count Alert" your_email@example.com fi 设置cron每小时运行该脚本。
ELK Stack的Watcher插件可实现基于条件的实时告警(如10分钟内出现10次ERROR日志)。
安装Watcher插件:sudo bin/elasticsearch-plugin install x-pack(需企业版许可,或使用开源替代方案如ElastAlert)。
配置Watcher规则(示例:检测10分钟内ERROR日志超过5次):
{ "trigger": { "schedule": { "interval": "10m" } }, "input": { "search": { "request": { "indices": ["syslog-*"], "body": { "query": { "match": { "message": "ERROR" } }, "aggs": { "errors_per_10m": { "date_histogram": { "field": "@timestamp", "interval": "10m" } } } } } } }, "condition": { "compare": { "ctx.payload.aggregations.errors_per_10m.buckets.0.doc_count": { "gt": 5 } } }, "actions": { "email_alert": { "email": { "to": "your_email@example.com", "subject": "High ERROR Count Alert", "body": "ERROR count in last 10 minutes: {{ctx.payload.aggregations.errors_per_10m.buckets.0.doc_count}}" } } } } 通过Kibana管理Watcher规则,启用后即可实时接收告警。
确保日志文件和自动化脚本的权限正确,防止未授权访问:
sudo chmod 640 /var/log/centralized/syslog # 仅root和adm组可读 sudo chown root:adm /var/log/centralized/syslog sudo chmod +x /path/to/count_errors.sh # 脚本仅root可执行 每周检查logwatch报告、cron运行日志(/var/log/syslog | grep cron),确保自动化任务正常执行。若发现脚本失败或告警未触发,及时排查原因(如脚本路径错误、邮件服务配置问题)。
通过以上步骤,可在Ubuntu中实现从日志收集、存储到自动化分析、告警的全流程管理,提升系统运维效率和异常响应速度。