Linux日誌系統小記

Linux日誌系統小記


概述:最近做ssh無密碼認證實驗時,在ssh服務配置文件中發現 authpriv,當時有種似曾相識的感覺。乾脆就是複習了下syslogd這個daemon,又發現在Redhat 6 中syslogd改名爲rsyslogd.

博客是以 centos 6.6爲例

  • rsyslog簡介

  • rsyslog的配置文件rsyslog.conf說明

  • rsyslog配置文件語法格式

  • rsyslog啓動方法

  • 系統其它日誌和應用程序日誌

  • 日誌消息來源和等級英文參考

 

rsyslog簡介

rsyslog 是在redhat 6.x以後使用的系統日誌系統,其進程名爲 rsyslogd.(在redhat 5.x中日誌系統爲syslog進程名爲syslogd,本人當時man syslog 和 man rsyslogd 其實也差不多,沒有多大改進)。UNIX和Linux操作系統都是採用rsyslog進行系統日誌的管理和配置。Linux系統內核和許多程序會產生各種錯誤信息、警告信息和其他的提示信息。管理員瞭解系統的運行狀態是很有用的,所以應該根據信息的來源以及信息的重要程度把它們寫到不同日誌文件中去。而執行這個過程的程序就是rsyslog。

默認情況下linux日誌都保存在/var/log目錄。

 

rsyslog的配置文件rsyslog.conf

 

[root@zabbix ~]# cat /etc/rsyslog.conf         

默認rsyslog.conf配置文件比syslog.conf多這裏以rsyslog.conf爲例

# rsyslog v5 configuration file

# For more information see/usr/share/doc/rsyslog-*/rsyslog_conf.html

# If you experience problems, seehttp://www.rsyslog.com/doc/troubleshoot.html

#以上爲配置文件說明

#### MODULES ####

$ModLoad imuxsock # provides support for local system logging(e.g. via logger command)

$ModLoad imklog   #provides kernel logging support (previously done by rklogd)

#$ModLoad immark  #provides --MARK-- message capability

# Provides UDP syslog reception

#$ModLoad imudp

#$UDPServerRun 514

# Provides TCP syslog reception

#$ModLoad imtcp

#$InputTCPServerRun 514

#默認加載的模塊,基本上不需要改。

 

#### GLOBAL DIRECTIVES ####

# Use default timestamp format

$ActionFileDefaultTemplateRSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This featureis usually not required,

# not useful and an extreme performance hit

#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/

$IncludeConfig /etc/rsyslog.d/*.conf

#上爲全局配置,基本上不需要改。

 

#配置文件接下來的 規則rule 是我們最關心的,它關係到日誌文件存放在哪,怎麼歸類之類,你下想你/var/log/下一般會看到很文件,他們都是歸好類的。

#### RULES ####

# Log all kernel messages to the console.

# Logging much else clutters up the screen.

#kern.*                                                /dev/console

#

# Log anything (except mail) of level info or higher.

# Don't log private authentication messages!

*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# 把除郵件、授權和定時任務以外的其他info級別的信息記錄到/var/log/messages日誌文件中

# The authpriv file has restricted access.

authpriv.*                                             /var/log/secure

# 把所有授權信息記錄到/var/log/secure日誌文件中,這也是爲什麼用戶登錄記錄會在這個文件了,你可以tail/var/log/secure 查看你最近的登錄記錄。

# Log all the mail messages in one place.

mail.*                                                  -/var/log/maillog

# 把所有級別的郵件信息記錄到/var/log/maillog日誌文件中

# Log cron stuff

cron.*                                                 /var/log/cron

# 把所有級別的定時任務信息記錄到/var/log/cron日誌文件中

# Everybody gets emergency messages

*.emerg                                                 *

# 把emerg級別的信息發送給所有登錄用戶

# Save news errors of level crit and higher in a special file.

uucp,news.crit                                         /var/log/spooler

#uccp子系統和重要網絡新聞子系統信息記錄到/var/log/spooler日誌文件中

# Save boot messages also to boot.log

local7.*                                               /var/log/boot.log

# 把所有的系統啓動信息記錄到/var/log/boot.log日誌文件中,可以看到開機是啓動那些東東。

 

#配置文件以下部分爲轉發規則forwarding rule,你想想啊,每臺linux服務器都記錄了自己的日誌,幾臺還好,多了話難到要一臺一臺的登錄上去看嗎?rsyslog很聰明他已經想好了。可以轉發到一臺上,分析這很容易羅。

# ### begin forwarding rule ###

# The statement between the begin ... end define a SINGLEforwarding

# rule. They belong together, do NOT split them. If you createmultiple

# forwarding rules, duplicate the whole block!

# Remote Logging (we use TCP for reliable delivery)

# An on-disk queue is created for this action. If the remotehost is

# down, messages are spooled to disk and sent when it is upagain.

#$WorkDirectory /var/lib/rsyslog # where to place spool files

#$ActionQueueFileName fwdRule1 # unique name prefix for spoolfiles

#$ActionQueueMaxDiskSpace 1g  # 1gb space limit (use as much as possible)

#$ActionQueueSaveOnShutdown on # save messages to disk onshutdown

#$ActionQueueType LinkedList  # run asynchronously

#$ActionResumeRetryCount -1   # infinite retries if host is down

# remote host is: name/ip:port, e.g. 192.168.0.1:514, portoptional

#*.* @@remote-host:514

#去掉#號生效  *.*爲所有日誌型 ,所以以上行意思爲 這臺服務器連接到遠程remote-host的514端口,並將所有日誌轉發一份到遠程 remote-host這臺服務器。

# ### end of the forwarding rule ###

 

# A template to for higher precision timestamps + severitylogging

$templateSpiceTmpl,"%TIMESTAMP%.%TIMESTAMP:::date-subseconds% %syslogtag%%syslogseverity-text%:%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"

 

:programname, startswith, "spice-vdagent"/var/log/spice-vdagent.log;SpiceTmpl

 

rsyslog配置文件語法格式

[消息來源.消息級別]   [動作]  [facility.priority] [action]

[消息來源.消息級別][動作]之間以Tab鍵進行分隔,同一行rsyslog配置中允許出現多個[消息來源.消息級別],但必須要使用分號“;”進行分隔,

例如:mail.*; cron.*       /var/log/test.log

消息來源表示發出消息的子系統。

rsyslog消息來源及說明

 

   

 

   

authpriv

安全/授權信息

mail

郵件子系統

cron

定時任務

news

網絡新聞子系統

daemon

守護進程

syslog

syslogd內部產生的信息

ftp

ftp守護進程

user

一般用戶級別信息

kern

內核信息

uucp

UUCP子系統

lpr

打印機子系統

local0-local7

本地用戶

優先級代表消息的緊急程度,如下表所示按級別由高到低列出了消息級別及說明。

rsyslog消息級別及說明

 

   

 

   

emerg

最緊急的消息

warning

警告消息

alert

緊急消息

notice

普通但重要的消息

crit

重要消息

info

通知性消息

err

出錯消息

debug

調試級的消息——消息量最多

rsyslog消息級別是向上匹配的,也就是說如果指定了一個消息級別,那麼指定級別及比該指定級別更高級的消息都會被包括進去。例如,warning表示所有大於或者等於warning級別的消息都會被處理,包括emergalertcriterrwarning。如果指定的是err級別,那麼就只是所有錯誤信息。

如果只想匹配某個確定級別的消息,而不希望包括更高級別的消息,可以使用等號“=”進行指定。例如希望處理cronnotice級別的消息:cron.=notice       /var/log/test.log

除此之外,rsyslog還支持兩個特殊的消息級別關鍵字:”!=” “*”以及none。其中”!=”爲不等於,“*”表示匹配所有來源或級別的消息,none表示忽略所有消息。

[動作]

是用於指定消息的處理方式。rsyslog支持把消息保存到日誌文件中、發送給指定的用戶、顯示在終端上或者通過網絡發送到另外一臺rsyslog服務器上進行處理。表中列出了所有可用的動作及其說明。

   

   

文件名

將消息保存到指定的文件中

@主機名或IP地址

轉發消息到另外一臺syslog服務器上進行處理

*

把消息發送到所有用戶的終端上

/dev/console

把消息發送到本地主機的終端上

| 程序

通過管道把消息重定向到指定的程序

用戶名列表

把消息發送給指定的用戶,用戶名以逗號“,”進行分隔

 

rsyslog啓動方法

1,  以chkconfig方式來開機自動啓動chkconfig  rsyslog on/off

2,  以 service 方式 servicersyslog start|stop|restart|reload|force-reload|status

3,  以 killall方式  killall –HUP rsyslogd

 

系統其它日誌和應用程序日誌

Linux系統中有些程序進程自己單獨產生應用程序日誌系統,(如http日誌在 /var/log/http/ 這個目錄,由其進本身生成),另外系統還提供了大量的其他日誌文件dmesg、wtmp、btmp、.bash_history。

dmesg日誌:記錄內核日誌信息

日誌文件/var/log/dmesg中記錄了系統啓動過程中的內核日誌信息,包括系統的設備信息,以及在啓動和操作過程中系統記錄的任何錯誤和問題的信息,例如,用戶新添加了一個磁盤,如果該磁盤設備能被Linux系統正確識別,那麼在dmesg日誌文件中應該能夠看到它的信息。

wtmp和btmp用戶登錄日誌

/var/log/wtmp和/var/log/btmp是Linux系統上用於保存用戶登錄信息的日誌文件。其中wtmp用於保存用戶成功登錄的記錄,而btmp則用於保存用戶登錄失敗的日誌記錄,它們爲系統安全審計提供了重要的信息依據。這兩個文件都是二進制的,無法直接使用文本編輯工具打開,必須通過last和lastb命令進行查看。

查看成功的用戶登錄記錄用last,查看失敗用戶登錄記錄用lastb。


wKioL1SqUzvwayC4AAHUlJBkcKY132.jpg

wKioL1SqUzuiAOzNAAJP6ypMkCo337.jpg


日誌消息來源和等級英文參考:

The  facility is one of the following keywords: auth, authpriv, cron, daemon,kern, lpr,

mail, mark, news, security(same as auth), syslog, user, uucp and local0 through local7.

The  keyword security  should not be usedanymore and mark is only for internal use and

therefore should not be usedin applications.  Anyway, you may want tospecify and redi-

rect  these messages here.  The facilityspecifies the subsystem that produced the mes-

sage, i.e. all mail programslog with the mail facility (LOG_MAIL) if  they  log using

syslog.

 

The  priority is one of the following keywords, in ascending order: debug,info, notice,

warning, warn (same aswarning), err, error (same as err), crit,  alert,  emerg, panic

(same  as emerg).   The keywords error, warnand panic are deprecated and should not be

used anymore.  The priority defines the severity of themessage


本文是 巧妙絕情 一個字一個圖打出來,參考了好多資料,感謝他們的分享,基於open source分享精神,轉載請註明出出。
支持我,請點擊 巧妙絕情 謝謝



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章