Syslog 是網路可視性的基石。完善的日誌策略不僅能加速故障定位,更是事後鑑識、合規稽核的關鍵證據。本文涵蓋 Syslog 設施與嚴重等級體系、路由器端配置、遠端 Syslog 伺服器 (Linux/Windows) 部署、日誌輪轉與備份、安全強化 (防竄改)、常見分析場景。
每條訊息格式:<%priority%>timestamp hostname %FACILITY-SEVERITY-MNEMONIC: message
| 數值 | 關鍵字 | 中文 | 典型事件 | 建議動作 |
|---|---|---|---|---|
| 0 | emergencies | 系統不可用 | 路由器崩潰、重啟 | 立即響應、頁面告警 |
| 1 | alerts | 需立即處理 | 關鍵介面中斷、鄰居失聯 | 15 分鐘內響應 |
| 2 | critical | 嚴重狀況 | 路由表大幅波動、記憶體耗盡 | 1 小時內響應 |
| 3 | errors | 錯誤狀況 | 介面錯誤計數器暴增、ACL 拒絕 | 納入日報分析 |
| 4 | warnings | 警告 | 介面 Up/Down、鄰居狀態變更 | 納入日報分析 |
| 5 | notifications | 通知 (預設) | 配置變更、登入/登出、路由重發佈 | 審計留存 |
| 6 | informational | 資訊 | TCP 連線建立/拆除、DHCP 租約 | 高流量環境可關閉 |
| 7 | debugging | 除錯 | debug 指令輸出 | 嚴禁生產環境長開 |
! 1. 啟用日誌服務與時間戳 (毫秒級、含時區、年份)
service timestamps log datetime msec localtime show-timezone year
service timestamps debug datetime msec localtime show-timezone year
service sequence-numbers ! 序號便於排序去重
service private-config-encryption ! 加密敏感資訊 (密鑰、密碼)
! 2. 緩衝區設定 (RAM 中暫存,重啟遺失)
logging buffered 52428800 notifications ! 50 MB 緩衝區,記錄 notifications(5) 以上
logging buffered 8192 debugging ! 除錯單獨小緩衝區 (可選)
! 3. 主控台/終端機/監視器 即時顯示等級
logging console warnings ! Console 只顯示 warnings(4) 以上,避免刷屏
logging monitor notifications ! VTY/SSH 顯示 notifications(5) 以上
logging trap notifications ! 發送至 Syslog Server 的等級
! 4. 遠端 Syslog 伺服器 (支援多台、可指定 VRF/源介面)
logging host 10.1.1.10 transport udp 514 ! 標準 UDP 514
logging host 10.1.1.11 transport tcp 6514 ! 可靠傳輸 TCP 6514 (IOS 15.2+)
logging source-interface Loopback0 ! 統一源 IP,利於防火牆放行
logging facility local7 ! 設施 Local7 (128-135),便於伺服器端分流
! 5. 速率限制防止日誌洪水淹沒 CPU/頻寬
logging rate-limit 10000 except emergencies ! 非 emergencies 限制 10k 訊息/秒
! 6. 原始訊息格式 (可選,便於解析)
logging origin-id hostname ! 加入主機名
! logging origin-id ip ! 或用 IP
! logging origin-id string "DC1-CORE-R1" ! 或自訂字串
! 7. 特定模組等級微調 (範例)
logging history notifications ! 發送 SNMP trap 等級
logging snmp-trap emergencies alerts critical errors warnings notifications
# /etc/rsyslog.d/20-cisco.conf
# 接收 UDP 514 (需 root 權限啟用)
$ModLoad imudp
$UDPServerRun 514
# 接收 TCP 6514 (可靠傳輸)
$ModLoad imtcp
$InputTCPServerRun 6514
# 依據 Facility (local7) 分流檔案
if $syslogfacility-text == 'local7' then {
action(type="omfile"
file="/var/log/cisco/routers.log"
template="RSYSLOG_TraditionalFileFormat"
asyncWriting="on"
queue.type="LinkedList"
queue.size="100000"
queue.dequeuebatchsize="1000")
stop
}
# 標準格式範本 (含 Priority、Timestamp、Hostname、Tag、Message)
template(name="CiscoFormat" type="string"
string="%TIMESTAMP:::date-rfc3339% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n")
# 日誌輪轉 /etc/logrotate.d/cisco
/var/log/cisco/routers.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 0640 root adm
sharedscripts
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
logging host ... transport tls) 或 VPN 隧道傳輸。| 場景 | 關鍵關鍵字/模式 | 分析目的 |
|---|---|---|
| 介面抖動/頻繁 Up/Down | %LINK-3-UPDOWN、%LINEPROTO-5-UPDOWN、Carrier transitions | 定位不穩纜路、光模組、電源、STP 環路 |
| 鄰居關係建立/失效 | %OSPF-5-ADJCHG、%BGP-5-ADJCHANGE、%HSRP-5-STATECHANGE | 掌握收斂時間、找出不穩定鏈路、協議參數錯誤 |
| 安全事件 | %SEC-6-IPACCESSLOG (ACL log)、%SEC_LOGIN-4-LOGIN_FAILED、%AUTH-6-USERAUTH | 偵測掃描、暴力破解、未授權登入、異常命令執行 |
import pandas as pd
import re
# 解析單行 Cisco syslog
pattern = re.compile(r'<%(\d+)>(\S+) (\S+) %(\w+)-(\d+)-(\w+): (.+)')
rows = []
with open('/var/log/cisco/routers.log') as f:
for line in f:
m = pattern.match(line)
if m:
pri, ts, host, fac, sev, mnemo, msg = m.groups()
rows.append({'pri':int(pri), 'ts':ts, 'host':host, 'fac':fac, 'sev':int(sev), 'mnemo':mnemo, 'msg':msg})
df = pd.DataFrame(rows)
# 統計過去 1 小時各嚴重等級分布
last_hour = df[df['ts'] > (pd.Timestamp.now() - pd.Timedelta(hours=1))]
print(last_hour['sev'].value_counts().sort_index())
# Top 10 錯誤訊息
print(df[df['sev']<=3]['mnemo'].value_counts().head(10))
建立「配置標準化 → 集中收集 → 結構化解析 → 自動化告警 → 定期審計」的日誌生命週期管理體系,將 Syslog 從「事後查閱」轉型為「主動防禦」的感測神經。