1. 程式人生 > 其它 >rsyslog 的rsyslog.conf配置詳解

rsyslog 的rsyslog.conf配置詳解

1、rsyslog 的rsyslog.conf配置詳解

https://blog.csdn.net/sunny_na/article/details/65444602

syslog服務和logrotate服務
======================================================================
rsyslog 是一個 syslogd 的多執行緒增強版。
現在Fedora和Ubuntu, rhel6預設的日誌系統都是rsyslog了
rsyslog負責寫入日誌, logrotate負責備份和刪除舊日誌, 以及更新日誌檔案
———————————————————————-
rsyslog 服務
———————————————————————-
軟體包:
[root

@centos~]# yum install rsyslog rsyslog-mysql logrotate
檢視當前rsyslog服務的狀態:
[root@centos~]#/etc/init.d/rsyslog status
rsyslogd (pid 1343) is running…
在centos6中, rsyslog服務預設是開機啟動的
我們先看一下它的程序::
[root@centos~]# ps -ef | grep rsyslogd | grep -v grep
root 1343 1 0 12:09 ? 00:00:00 /sbin/rsyslogd -c 4
從上面命令的輸出結果看到rsyslog執行時使用的引數是-c 4.
它的意思是指定rsyslog執行(相容)的版本號, 這個引數必須是第一個引數, 當然也可以省略, 預設為-c0, (命令列相容sysklogd)
這個引數是在檔案/etc/sysconfig/rsyslog中指定::
[root
@centos
~]# cat /etc/sysconfig/rsyslog

  1. # Options to syslogd

  2. # syslogd options are deprecated since rsyslog v3

  3. # if you want to use them, switch to compatibility mode 2 by “-c 2″

  4. SYSLOGD_OPTIONS=”-c 4″

[root@centos~]# chkconfig –list | grep rsyslog
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
注意,這裡的服務名是rsyslog!
———————————————————————-
配置檔案
———————————————————————-
/etc/rsyslog.conf
配置檔案的基本資訊
配置檔案中有很多內容, 但最主要的是指定需要記錄哪些服務和需要記錄什麼等級的資訊::
cat /etc/rsyslog.conf

  1. #rsyslog v3 config file

  2. # if you experience problems, check

  3. # http://www.rsyslog.com/troubleshoot for assistance

  4. #### MODULES #### 載入 模組

  5. $ModLoadimuxsock.so –> 模組名 # provides support for local system logging (e.g. via logger command) 本地系統日誌

  6. $ModLoadimklog.so # provides kernel logging support (previously done by rklogd)

  7. #$ModLoad immark.so # provides –MARK– message capability

  8. # Provides UDP syslog reception

  9. # 允許514埠接收使用UDP協議轉發過來的日誌

  10. #$ModLoad imudp.so

  11. #$UDPServerRun 514

  12. # Provides TCP syslog reception

  13. # 允許514埠接收使用TCP協議轉發過來的日誌

  14. #$ModLoad imtcp.so

  15. #$InputTCPServerRun 514

  16. #### GLOBAL DIRECTIVES ####

  17. 定義日誌格式預設模板

  18. # Use default timestamp format

  19. $ActionFileDefaultTemplateRSYSLOG_TraditionalFileFormat

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

  21. # not useful and an extreme performance hit

  22. #$ActionFileEnableSync on

  23. #### RULES ####

  24. # Log all kernel messages to the console.

  25. # Logging much else clutters up the screen.

  26. #kern.* /dev/console 關於核心的所有日誌都放到/dev/console(控制檯)

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

  28. # Don’t log private authentication messages!

  29. # 記錄所有日誌型別的info級別以及大於info級別的資訊到/var/log/messages,但是mail郵件資訊,authpriv驗證方面的資訊和cron時間任務相關的資訊除外

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

  31. # The authpriv file has restricted access.

  32. # authpriv驗證相關的所有資訊存放在/var/log/secure

  33. authpriv.* /var/log/secure

  34. # Log all the mail messages in one place.

  35. # 郵件的所有資訊存放在/var/log/maillog; 這裡有一個-符號, 表示是使用非同步的方式記錄, 因為日誌一般會比較大

  36. mail.* -/var/log/maillog

  37. # Log cron stuff

  38. # 計劃任務有關的資訊存放在/var/log/cron

  39. cron.* /var/log/cron

  40. # Everybody gets emergency messages

  41. # 記錄所有的大於等於emerg級別資訊, 以wall方式傳送給每個登入到系統的人

  42. *.emerg * *代表所有線上使用者

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

  44. # 記錄uucp,news.crit等存放在/var/log/spooler

  45. uucp,news.crit /var/log/spooler

  46. # Save boot messages also to boot.log 啟動的相關資訊

  47. local7.* /var/log/boot.log

  48. #:rawmsg, contains, “sdns_log” @@192.168.56.7:10514

  49. #:rawmsg, contains, “sdns_log” ~

  50. # ### begin forwarding rule ### 轉發規則

  51. # The statement between the begin … end define a SINGLE forwarding

  52. # rule. They belong together, do NOT split them. If you create multiple

  53. # forwarding rules, duplicate the whole block!

  54. # Remote Logging (we use TCP for reliable delivery)

  55. #

  56. # An on-disk queue is created for this action. If the remote host is

  57. # down, messages are spooled to disk and sent when it is up again.

  58. #$WorkDirectory /var/spppl/rsyslog # where to place spool files

  59. #$ActionQueueFileName fwdRule1 # unique name prefix for spool files

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

  61. #$ActionQueueSaveOnShutdown on # save messages to disk on shutdown

  62. #$ActionQueueType LinkedList # run asynchronously

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

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

  65. #*.* @@remote-host:514 # @@表示通過tcp協議傳送 @表示通過udp進行轉發

  66. #local3.info @@localhost:514

  67. #local7.* # @@192.168.56.7:514

  68. # ### end of the forwarding rule ###

格式::
日誌裝置(型別).(連線符號)日誌級別 日誌處理方式(action)
日誌裝置(可以理解為日誌型別):
———————————————————————-
auth –pam產生的日誌
authpriv –ssh,ftp等登入資訊的驗證資訊
cron –時間任務相關
kern –核心
lpr –列印
mail –郵件
mark(syslog)–rsyslog服務內部的資訊,時間標識
news –新聞組
user –使用者程式產生的相關資訊
uucp –unix to unix copy, unix主機之間相關的通訊
local 1~7 –自定義的日誌裝置
日誌級別:
———————————————————————-
debug –有調式資訊的,日誌資訊最多
info –一般資訊的日誌,最常用
notice –最具有重要性的普通條件的資訊
warning –警告級別
err –錯誤級別,阻止某個功能或者模組不能正常工作的資訊
crit –嚴重級別,阻止整個系統或者整個軟體不能正常工作的資訊
alert –需要立刻修改的資訊
emerg –核心崩潰等嚴重資訊
none –什麼都不記錄
從上到下,級別從低到高,記錄的資訊越來越少
詳細的可以檢視手冊: man 3 syslog
連線符號
———————————————————————-
.xxx: 表示大於等於xxx級別的資訊
.=xxx:表示等於xxx級別的資訊
.!xxx:表示在xxx之外的等級的資訊
Actions
———————————————————————-
1. 記錄到普通檔案或裝置檔案::
*.* /var/log/file.log # 絕對路徑
*.* /dev/pts/0
測試: logger -p local3.info ‘KadeFor is testing the rsyslog and logger ‘ logger 命令用於產生日誌
2. 轉發到遠端::
*.* @192.168.0.1 # 使用UDP協議轉發到192.168.0.1的514(預設)埠
*.* @@192.168.0.1:10514 # 使用TCP協議轉發到192.168.0.1的10514(預設)埠
3. 傳送給使用者(需要線上才能收到)::
*.* root
*.* root,kadefor,up01 # 使用,號分隔多個使用者
*.* * # *號表示所有線上使用者
4. 忽略,丟棄::
local3.* ~ # 忽略所有local3型別的所有級別的日誌
5. 執行指令碼::
local3.* ^/tmp/a.sh # ^號後跟可執行指令碼或程式的絕對路徑
# 日誌內容可以作為指令碼的第一個引數.
# 可用來觸發報警
.. note::
日誌記錄的順序有先後關係!
======================================================================
一個標準的簡單的配置檔案
======================================================================
::
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* /var/log/maillog
cron.* /var/log/cron
*.emerg *
uucp,news.crit /var/log/spooler
local7.* /var/log/boot.log
======================================================================
例項: 指定日誌檔案, 或者終端
======================================================================
[root@kadefor ule-sa3]# vi /etc/rsyslog.conf
[root@kadefor ule-sa3]# grep local3 !$
grep local3 /etc/rsyslog.conf
local3.* /var/log/local3.log
[root@kadefor ule-sa3]# rm -rf /var/log/local3.log
[root@kadefor ule-sa3]# /etc/init.d/rsyslog reload
Reloading system logger… [ OK ]
[root@kadefor ule-sa3]# ls /var/log/local3.log
/var/log/local3.log
[root@kadefor ule-sa3]# logger -t ‘LogTest’ -p local3.info ‘KadeFor is testing the rsyslog and logger’
[root@kadefor ule-sa3]# cat /var/log/local3.log
Jun 10 04:55:52 kadefor LogTest: KadeFor is testing the rsyslog and logger
[root@kadefor ule-sa3]#
自己實驗日誌傳送給某個終端
======================================================================
例項: 過濾特定的日誌到檔案, 忽略(丟棄)包含某個字串的日誌
======================================================================
# 過濾日誌, 由:號開頭
:msg, contains, “error” /var/log/error.log
:msg, contains, “error” ~ # 忽略包含error的日誌
:msg, contains, “user nagios” ~
:msg, contains, “user kadefor” ~
:msg, contains, “module-alsa-sink.c: ALSA woke us up to write new data to the device, but there was actually nothing to write” ~
local3.* ~
PS.
& ~ # 忽略所有的日誌
把包含’oracle’的日誌儲存在/var/log/oracle.log
======================================================================
例項: 使用模板來定義日誌格式
======================================================================
定義預設的日誌格式:

  1. $templatemyFormat,”%rawmsg%\n”

  2. $ActionFileDefaultTemplatemyFormat

  3. #如果不要$ActionFileDefaultTemplate myFormat這一行, 就需要像這樣來使用模板:

  4. #在日誌檔案後新增模板名, 並用;號分隔

  5. $templatemyFormat,”%rawmsg%\n”

  6. # The authpriv file has restricted access.

  7. authpriv.* /var/log/secure;myFormat

  8. # Log all the mail messages in one place.

  9. mail.* /var/log/maillog;myFormat

  10. # Log cron stuff

  11. cron.* /var/log/cron;myFormat

  12. # Everybody gets emergency messages

  13. *.emerg *

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

  15. uucp,news.crit /var/log/spooler;myFormat

  16. # Save boot messages also to boot.log

  17. local7.* /var/log/boot.log;myFormat

======================================================================
例項: remote log 遠端傳送與接收:
======================================================================
如果要修改為非514的埠, 需要設定selinux
只要在rsyslog.conf中加入
*.* @192.168.0.10
*.* @192.168.0.10:10514 # 帶埠號
*.* @@192.168.0.10 # TCP
但是沒有定義儲存在遠端的哪一個檔案啊?
其實儲存在什麼檔案, 那是遠端日誌伺服器接收到日誌之後它自己的事情了.
例1:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
———————————————————————-
Client(send):
———————————————————————-
::
local3.* @@192.0.2.1:10514
# if you need to forward to other systems as well, just
# add additional config lines:
# *.* @@other-server.example.net:10514
# 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
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* /var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg *
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
———————————————————————-
Server(receive): <1>
———————————————————————-
::
# for TCP use:
$modload imtcp
$InputTCPServerRun 10514
# for UDP use:
$modload imudp
$UDPServerRun 514
# 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
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* /var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg *
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local3.* /var/log/local3.log # 測試用
例2 (僅做了解, 不做要求)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#配置服務端(接收)
———————————————————————-
vi /etc/rsyslog.conf #在檔案開始加上,同時確保514埠能夠被客戶端用tcp訪問
$ModLoad imtcp.so # needs to be done just once #使用tcp方式
$InputTCPMaxSessions 500 # tcp接收連線數為500個
$InputTCPServerRun 514 # tcp接收資訊的埠
$template logformat,”%TIMESTAMP:::date-mysql% %FROMHOST-IP%%msg%\n” # 定義一個名為logformat模板, 為資訊加上日誌時間
$template DynFile,”/var/log/tlog%$year%%$month%%$day%.log” # 定義日誌檔案的名稱,按照年月日
:rawmsg, contains, “sdns_log” ?DynFile;logformat # 把rawmsg(也可以使用msg)日誌中包含sdns_log標誌的資訊寫到DynFile定義的日誌檔案裡
:rawmsg, contains, “sdns_log” ~ # 這個表示丟棄包含sdns_log標誌的資訊, 一般都加上它, 以免多個日誌檔案記錄重複的日誌
#配置客戶端(傳送)
———————————————————————-
vi /etc/rsyslog.conf #在檔案開始加上
#把包含sdns_log的資訊通過tcp發到192.168.1.2 @@表示tcp @表示udp
:rawmsg, contains, “sdns_log” @@192.168.1.2 # 預設514埠
#這個表示丟棄包含sdns_log標誌的資訊,防止這個資訊寫到本機的/var/log/message
:rawmsg, contains, “sdns_log” ~
#測試
———————————————————————-
在客戶端上執行
logger -p user.info “sdns_log 34334″
在服務端的/var/log/目錄裡是否有tlog*日誌產生
補充:
———————————————————————-
如果要把不同伺服器傳送過來的日誌儲存到不同的檔案, 可以這樣操作:
:fromhost-ip, isequal, “192.168.0.160″ /var/log/host160.log
:FROMHOST-IP, isequal, “192.168.0.161″ /var/log/host161.log
:FROMHOST-IP, startswith, “192.168.1.” /var/log/network1.log
:FROMHOST-IP, startswith, “192.168.2.” /var/log/network2.log
練習:
======================================================================
1. 實現把ssh服務的日誌自定義儲存到/var/log/newsshd.log (先不做)
2. mail日誌儲存在遠端日誌伺服器/var/log/newmail.log
3. 過濾日誌, 如果日誌包含有”daydayup”, 則執行指令碼/tmp/a.sh
指令碼內容:
#!/bin/bash
echo “KO::** $1″ > /dev/tty2
======================================================================
logrotate服務
======================================================================
rotate 輪換,日誌切換
logrotate服務的啟動方式
logrotate是一個日誌管理程式,用來把舊的日誌檔案刪除(備份),並建立新的日誌檔案,這個過程稱為“轉儲”。我們可以根據日誌的大小,或者根據其使用的天數來轉儲。
logrotate 的執行由crond服務實現。在/etc/cron.daily目錄中,有個檔案logrotate,它實際上是個shell script,用來啟動logrotate。logrotate程式每天由cron在指定的時間(/etc/crontab)啟動。
因此,使用ps是無法檢視到logrotate的。如果它沒有起來,就要檢視一下crond服務有沒有在執行。
在執行logrotate時,需要指定其配置檔案/etc/logrotate.conf
這 個配置檔案的註釋寫得很清楚,沒有必要再羅嗦了。只想強調下面這行,它的作用包含存放在/etc/logrotate.d目錄下面的配置檔案,不可或缺。 如果你安裝了一個新的服務,它的日誌轉儲的規則可以建立一個專門的配置檔案,放在/etc/logrotate.d下面。它其實也因為下面的這句話,在 logrotate服務啟動時被讀取。
每個存放在/etc/logrotate.d目錄裡的檔案,都有上面格式的配置資訊。在{}中定義的規則,如果與logrotate.conf中的衝突,以/etc/logrotatate.d/中的檔案定義的為準。
logrotate啟動指令碼放在 /etc/cron.daily/logrotate 中,可人工執行命令進行測試:
/usr/sbin/logrotate -f /etc/logrotate.conf
dateext表示轉儲檔案會以日期來結束*
::
[root@kadefor log]# vim /etc/logrotate.conf
# see “man logrotate” for details
# rotate log files weekly
weekly –每週輪轉一次
# keep 4 weeks worth of backlogs
rotate 4 –保留四個
# create new (empty) log files after rotating old ones
create –rotate後,建立一個新的空檔案
# uncomment this if you want your log files compressed
#compress –預設是不壓縮的
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d –這個目錄下面配置檔案生效
# no packages own wtmp — we’ll rotate them here
/var/log/wtmp { –定義/var/log/wtmp這個日誌檔案
monthly –每月輪轉一次,取代了上面的全域性設定的每週輪轉一次
minsize 1M –定義日誌必須要大於1M大小才會去輪轉
create 0664 root utmp –新的日誌檔案的許可權,屬主,屬主
rotate 1 –保留一個,取代了上面的全域性設定的保留四個
}
/var/log/btmp {
missingok –如果日誌丟失, 不報錯
monthly
create 0600 root utmp
rotate 1
}
::
# sample logrotate configuration file
compress
# 全域性設定, 壓縮
/var/log/messages {
rotate 5 # 保留5份日誌
weekly # 每週輪換一次
postrotate # 輪換之後重啟syslogd服務
/usr/bin/killall -HUP syslogd
# rhel6中為:/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
# 可檢視/etc/logrotate.d/下的配置檔案
endscript
}
“/var/log/httpd/access.log” /var/log/httpd/error.log { # 指定多個檔案, 如果有特殊字元需要用單引號
rotate 5
mail[email protected]
size 100k # 超過100k後切換日誌, 並把老的日誌傳送郵件給[email protected]
sharedscripts # 共享指令碼. 下面的postrotate指令碼只執行一次.
postrotate
/usr/bin/killall -HUP httpd
endscript
}
/var/log/news/* { # 少用萬用字元, 因會它會包括已經切換過的日誌, 要用的話最好在*號後加上副檔名, 如*.log
monthly
rotate 2
olddir /var/log/news/old
missingok
postrotate
kill -HUP ‘cat /var/run/inn.pid‘
endscript
nocompress
}
例:
修改/etc/logrotate.conf
/var/log/wtmp {
monthly
minsize 10k
create 0664 a b
rotate 2
}
logrotate -f /etc/logrotate.conf –強制輪轉
logrotate -vf /etc/logrotate.conf –再加一個-v引數檢視輪轉的過程
———————————
[root@kadefor log]# vim /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
sharedscripts –表示切換時指令碼只執行一次
postrotate –表示rotate後執行的指令碼
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
/bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
endscript –表示指令碼結束
}
[root@kadefor log]# logger -t ‘aaaa’ ‘bbbbbb’–在日誌里加一個內容tag和內容
[root@kadefor log]# tail /var/log/messages
Jun 12 19:38:55 kadefor dhclient[3166]: bound to 192.168.1.101 — renewal in 3384 seconds.
Jun 12 20:34:22 kadefor aaaa: bbbbbb