非常詳細的rsyslogd & logrotate配置文件解析

rsyslog服務和logrotate服務
======================================================================
rsyslog 是一個 syslogd 的多線程增強版
現在Fedora和Ubuntu, rhel6默認的日誌系統都是rsyslog了

rsyslog服務和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. $ModLoad imuxsock.so  –> 模塊名    # provides support for local system logging (e.g. via logger command) 本地系統日誌

  6. $ModLoad imklog.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. $ActionFileDefaultTemplate RSYSLOG_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. $template myFormat,”%rawmsg%\n”  

  2. $ActionFileDefaultTemplate myFormat  

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

  4. #在日誌文件後添加模板名, 並用;號分隔

  5. $template myFormat,”%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



原網址: http://smartest.blog.51cto.com/3585938/1280933


———————————————————————-
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. $ModLoad imuxsock.so  –> 模塊名    # provides support for local system logging (e.g. via logger command) 本地系統日誌

  6. $ModLoad imklog.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. $ActionFileDefaultTemplate RSYSLOG_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. $template myFormat,”%rawmsg%\n”  

  2. $ActionFileDefaultTemplate myFormat  

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

  4. #在日誌文件後添加模板名, 並用;號分隔

  5. $template myFormat,”%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



原網址: http://smartest.blog.51cto.com/3585938/1280933

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