了解 syslog
更新記錄
item | note |
---|---|
20160728 | 第一版 |
目錄
Syslog 說明
- Syslog常被稱為系統日誌或系統記錄
- syslog協定屬於一種主從式協定:syslog發送端會傳送出一個小的文字訊息(小於1024位元組)到syslog接收端
- 接收端通常名為: syslogd (syslog daemon)
- 系統日誌訊息可以被以UDP協定及╱或TCP協定來傳送
- hisi busybox的版本是無/etc/syslog.conf
- Facility code / Severity level , (說明由那個程式產生 / 說明此打印log等級)
- Each message is labeled with a facility code
- indicating the software type generating the message, and assigned a severity label
- A facility code is used to specify the type of program that is logging the message
- Facility code(0: kernel message, 1:user-level message,,, 23:local user 7)
- openlog() : 設定facility code(ex. LOG_LOCAL1)
- syslog() : 設定Severity level(0:Emergency, 1:Alert,,, 7:Debug)
1 | BusyBox v1.16.1 (2016-02-19 09:03:37 UTC) multi-call binary. |
Syslogd無關系統記錄檔
- /var/log/wtmp
記錄檔功能:記錄使用者(過去及目前)何時由何處進入系統、停留多久及系統開關機時間等訊息
檔案內容查閱指令:last [-option]
1 | $ last |
- /var/log/lastlog
記錄檔功能:記錄系統中每一位使用者最近一次login系統的資訊
1 | $ lastlog |
syslogd相關系統記錄檔
- /var/log/messages
這個檔案相當的重要,幾乎系統發生的錯誤訊息
Syslog 範例
Syslog 測試
- 記得先帶起syslogd,再來測試下例程式
1 | #include <syslog.h> |
- /var/log/message
1 | Jul 27 17:11:05 gk350a daemon.debug pppd[2879]: Script /etc/ppp/ip-up started (pid 2901) |
2879即為pppd的pid
3033即為a.out的pid
Syslog 說明
openlog()
opens a connection to the system logger for a programsyslog()
generates a log messageThe option argument to openlog()
option | note |
---|---|
LOG_CONS | Write directly to system console if there is an error while sending to system logger. |
LOG_PID | Include PID with each message. |
LOG_LOCAL0~7 | reserved for local use |
The facility argument is used to specify what type of program is logging the message
level
- The levels are, in order of decreasing importance:
- This determines the importance of the message
level | note |
---|---|
LOG_EMERG | system is unusable |
LOG_ALERT | action must be taken immediately |
LOG_CRIT | critical conditions |
LOG_ERR | error conditions |
LOG_WARNING | warning conditions |
LOG_NOTICE | normal, but significant, condition |
LOG_INFO | informational message |
LOG_DEBUG | debug-level message |
Busybox Syslogd 用法
- busybox syslogd
1 | Options: |
帶起方式如下
syslogd
- 預設產生/var/log/message
syslogd -O /mnt/log/mesasge -s 256
- 設定產生log檔的位置
- 設定rotate size 256K
syslogd -C256
- 設定使用256K RAM
- 需使用logread,來讀取內容
- ex. logread -f | nc 192.168.1.1 514
- ex. logread -f » /mnt/share/logfile
參考來源
- Chapter 11 Syslog and Log Files(記錄檔)
- 第十八章、認識與分析登錄檔
- Syslog
- openlog - Linux man page
- System Log in Linux (openlog, syslog, closelog)
- 使用 syslogd 记录远程主机的日志
- The Syslog Protocol - rfc5424
- System log in OpenWrt
- 目前OpenWrt皆以logd取代syslog
- ubox’s logd and logread
- ubox
- wiki syslog
- An Overview of the syslog Protocol
- klogd: The Kernel Logging Dæmon
- 將kernel訊息轉到syslogd的klogd
- 透過 printk() 所印出的訊息會先丟給 klogd,klogd 再傳給 syslogd 然後紀錄到 /var/log/message
- 使用 busybox 的 syslogd, klogd 如何做訊息的過濾?