在實現自動化分析前,需先規范日志的收集、存儲格式,確保日志數據的一致性和可處理性。
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中實現從日志收集、存儲到自動化分析、告警的全流程管理,提升系統運維效率和異常響應速度。