Rsyslog 收集tomcat日志

簡(jiǎn)述
rsyslog 處理多服務(wù)器日志分散不容易管理晦攒,可以將多臺(tái)服務(wù)器日志發(fā)送到指定的日志服務(wù)器上分析气笙。

image.png

1. 準(zhǔn)備環(huán)境

2臺(tái)liunx機(jī)器,并且開通514端口
- 3.17.190.209 Server
- 3.21.75.99 Client
tomcat
- apache-tomcat-8.5.60

2.安裝

  1. 查看rsyslog 版本
    • 命令:rsyslogd -v
      rsyslog-version.png

注意版本不同配置格式也不同,但是高版本上面也可以使用低版本的配置
可以使用下面的命令升級(jí)成最新的
cd /etc/yum.repos.d
wget http://rpms.adiscon.com/v8-stable/rsyslog.repo
yum update rsyslog

  1. 安裝wget
    • 下載wget:yum install wget

wget 是liunx下載文件工具

  1. 安裝jdk

  2. 安裝tomcat

3. Client 配置Rsyslog

  1. 配置rsyslog.conf

    • vi /etc/rsyslog.conf
      默認(rèn)的路徑:etc/rsyslog.conf
    #### MODULES ###
    module(load="imuxsock" SysSock.Use="off")             #imuxsock模塊,支持本地系統(tǒng)日志的模塊
    module(load="imjournal" StateFile="imjournal.state")  #imjournal模塊醋火,支持對(duì)系統(tǒng)日志的訪問(此模塊與上一模塊默認(rèn)啟用
    module(load="imfile" PollingInterval="1")                #imfile模塊悠汽,支持對(duì)文件進(jìn)行操作
    module(load="imklog")                #imklog模塊,支持內(nèi)核日志的模塊
    module(load="immark")                #immark模塊芥驳,支持日志標(biāo)記
    
    #提供遠(yuǎn)程rsyslog日志的udp協(xié)議的接收支持
    module(load="imudp")               #imudp模塊柿冲,用于支持udp協(xié)議
    input(type="imudp" port="514")     #允許通過514端口接收使用udp協(xié)議的遠(yuǎn)程日志
    
    #提供遠(yuǎn)程rsyslog日志的tcp協(xié)議的接收支持
    module(load="imtcp")             #imtcp模塊,用于支持tcp協(xié)議
    input(type="imtcp" port="514")   #允許通過514端口接收使用tcp協(xié)議的遠(yuǎn)程日志
    
    #### GLOBAL DIRECTIVES ####
    global(workDirectory="/var/lib/rsyslog")    #工作目錄
    module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")    #定義日志格式默認(rèn)模板(可以自行設(shè)定兆旬,參看template部分)
    include(file="/etc/rsyslog.d/*.conf" mode="optional")     #需要引入的自定義配置文件的路徑
    
    #### RULES ####
    #內(nèi)核消息假抄,默認(rèn)不啟用
    #kern.*                                                 /dev/console
    
    # 記錄所有日志類型的,信息等級(jí)大于等于info級(jí)別的信息到messages文件(mail郵件信息丽猬,authpriv驗(yàn)證信息和corn時(shí)間和任務(wù)信息除外)
    *.info;mail.none;authpriv.none;cron.none                /var/log/messages
    
    # authpriv驗(yàn)證相關(guān)的所有信息存放在/var/log/secure
    authpriv.*                                              /var/log/secure
    
    # 郵件的所有信息存在/var/log/maillog宿饱;這里有一個(gè)“-”符號(hào)表示是使用異步的方式記錄
    mail.*                                                  -/var/log/maillog
    
    # 任務(wù)計(jì)劃有關(guān)的信息存放在/var/log/cron
    cron.*                                                  /var/log/cron
    
    # 記錄所有的≥emerg級(jí)別信息,發(fā)送給每個(gè)登錄到系統(tǒng)的日志
    *.emerg                                                 :omusrmsg:*
    
    # 記錄uucp脚祟,news.crit等
    uucp,news.crit                                          /var/log/spooler
    
    # 本地服務(wù)器的啟動(dòng)的所有日志
    local7.*                                                /var/log/boot.log
    
    #日志發(fā)送的配置谬以,@表示傳輸協(xié)議(@表示udp,@@表示tcp)由桌,后面是ip和端口为黎,格式可配置
    #*.* @@3.17.190.209:514
    
    # 定制規(guī)則
    ruleset(name="remote"){
        action(type="omfwd"               #omfwd輸出遠(yuǎn)程模式
           target="3.17.190.209"          #遠(yuǎn)程服務(wù)器的ip地址
           port="514"                     #端口
           protocol="tcp"                 #使用協(xié)議
           queue.type="linkedList"        #使用異步處理 
           queue.spoolDirectory="/var/log/rsyslog"       #隊(duì)列目錄
           queue.fileName="remoteQueue_3_17_190_209"     #隊(duì)列名稱
           queue.maxDiskSpace="5g"                       #隊(duì)列占最大磁盤空間
           queue.saveOnShutdown="on"                     #保存內(nèi)存數(shù)據(jù)如果rsyslog關(guān)閉
           action.resumeRetryCount="-1"                  #無(wú)限重試插入失敗
           )
     stop
    }
    
    ruleset(name="MyRuleSet") {
     action(type="omfile"        #輸出文件模式
       File="/var/log/test.out")  #輸出文件的位置
     stop
    }
    
    
  2. 配置tomcat-log.conf

    • vi /etc/rsyslog.d/tomcat-log.conf
      如果沒有就創(chuàng)建一個(gè)新的文件
     #工作目錄
     $WorkDirectory /var/log/rsyslog
    
     #輸入文件模式
     input(type="imfile"
          File="/var/log/apache-tomcat-8.5.60/logs/catalina.out"    #文件的位置
          Tag="tomcat_catalina"          #文件標(biāo)識(shí),服務(wù)器接收到消息可以使用這個(gè)tag分類
          Severity="info"                #日志級(jí)別
          Facility="local0"             
          PersistStateInterval="1"    #回寫偏移量數(shù)據(jù)到文件間隔時(shí)間(秒)
          ruleset="remote")          #規(guī)則集行您,rsyslog.conf中定義的rule名稱
     
     #測(cè)試從本機(jī)的test.in 寫到本地的test.out文件
     input(type="imfile"
          File="/var/log/test.in"
          Tag="test1"
          Severity="info"
          Facility="local0"
          ruleset="MyRuleSet")
    
    
  3. 保存文件碍舍,并且檢查配置文件是否正確

    • rsyslogd -N 1
      檢查文件.png
  4. 重啟rsyslog

    • systemctl restart rsyslog.service
  5. 查看啟動(dòng)日志

    • tail -f /var/log/messages

      要認(rèn)真查看確保沒有錯(cuò)誤
      rsyslog啟動(dòng)日志.png

4. Server 配置Rsyslog

  1. 配置rsyslog.conf
    • vi /etc/rsyslog.conf
      默認(rèn)的路徑:etc/rsyslog.conf
    $FileGroup root         #文件所屬組
    $FileOwner root        #文件的擁有者
    $FileCreateMode 0777    #生成文件權(quán)限
    $DirCreateMode 0777     #生成文件目錄權(quán)限
    $Umask 0022
    $PrivDropToUser root       #可以刪除日志的用戶
    $PrivDropToGroup root    #可以刪除日志的用戶組
    
    #### MODULES ###
    module(load="imuxsock" SysSock.Use="off")             #imuxsock模塊,支持本地系統(tǒng)日志的模塊
    module(load="imjournal" StateFile="imjournal.state")  #imjournal模塊邑雅,支持對(duì)系統(tǒng)日志的訪問(此模塊與上一模塊默認(rèn)啟用
    module(load="imfile" PollingInterval="1")                #imfile模塊,支持對(duì)文件進(jìn)行操作
    module(load="imklog")                #imklog模塊妈经,支持內(nèi)核日志的模塊
    module(load="immark")                #immark模塊淮野,支持日志標(biāo)記
      
    #提供遠(yuǎn)程rsyslog日志的udp協(xié)議的接收支持
    module(load="imudp")               #imudp模塊,用于支持udp協(xié)議
    input(type="imudp" port="514")     #允許通過514端口接收使用udp協(xié)議的遠(yuǎn)程日志
    
    #提供遠(yuǎn)程rsyslog日志的tcp協(xié)議的接收支持
    module(load="imtcp")             #imtcp模塊吹泡,用于支持tcp協(xié)議
    input(type="imtcp" port="514")   #允許通過514端口接收使用tcp協(xié)議的遠(yuǎn)程日志
      
    #### GLOBAL DIRECTIVES ####
    global(workDirectory="/var/lib/rsyslog")    #工作目錄
    module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")    #定義日志格式默認(rèn)模板(可以自行設(shè)定骤星,參看template部分)
    include(file="/etc/rsyslog.d/*.conf" mode="optional")     #需要引入的自定義配置文件的路徑
      
    #### RULES ####
    #內(nèi)核消息,默認(rèn)不啟用
    #kern.*                                                 /dev/console
    
    # 記錄所有日志類型的爆哑,信息等級(jí)大于等于info級(jí)別的信息到messages文件(mail郵件信息洞难,authpriv驗(yàn)證信息和corn時(shí)間和任務(wù)信息除外)
    *.info;mail.none;authpriv.none;cron.none                /var/log/messages
    
    # authpriv驗(yàn)證相關(guān)的所有信息存放在/var/log/secure
    authpriv.*                                              /var/log/secure
     
    # 郵件的所有信息存在/var/log/maillog;這里有一個(gè)“-”符號(hào)表示是使用異步的方式記錄
    mail.*                                                  -/var/log/maillog
      
    # 任務(wù)計(jì)劃有關(guān)的信息存放在/var/log/cron
    cron.*                                                  /var/log/cron
    
    # 記錄所有的≥emerg級(jí)別信息揭朝,發(fā)送給每個(gè)登錄到系統(tǒng)的日志
    *.emerg                                                 :omusrmsg:*
    
    # 記錄uucp队贱,news.crit等
    uucp,news.crit                                          /var/log/spooler
    
    # 本地服務(wù)器的啟動(dòng)的所有日志
    local7.*                                                /var/log/boot.log
    #生成模板
    $template SpiceTmpl,"%msg:2:$%\n"
    $template CatalinaDynaFile,"/var/log/rsyslog/%fromhost-ip%/catalina_%$YEAR%-%$MONTH%-%$DAY%.log"
      
    #匹配規(guī)則色冀,文章后面將分享其他配置類型
    :fromhost-ip,contains,"3.21.75.99" ?CatalinaDynaFile;SpiceTmpl
    
    
  2. 保存文件,并且重新啟動(dòng)Rsyslog
    • systemctl restart rsyslog.conf
  3. 查看啟動(dòng)日志
    • tail -f /var/log/message
  4. 查看514端口是否開放
    如果沒有開放,請(qǐng)關(guān)閉514防火墻
    http://tool.chinaz.com/port/
    scanport.png

5. 測(cè)試寫入local文件

  1. 讀取client 文件/var/log/test.in, 寫入client /var/log/test.out 文件
    這個(gè)規(guī)則在上面的client 配置里面已經(jīng)配置了輸入文件類型柱嫌,位置和輸出文件類型锋恬,位置。

  2. 查看實(shí)時(shí)日志test.out

    • tail -f /var/log/test.out
      test.out.png
  3. 客戶端重新開一個(gè)窗口编丘,添加日志到test.in

  • echo yanglibo date: date +%Y-%m-%d:%H:%M:%S >> /var/log/test.in
  1. 測(cè)試結(jié)果与学,test.out 應(yīng)該同時(shí)接收到文件
    測(cè)試結(jié)果.png

6. 測(cè)試寫入遠(yuǎn)程server

  1. 讀取tomat 日志文件 /var/log/apache-tomcat-8.5.60/logs/catalina.out, 寫入server 3.17.190.209 /var/log/rsyslog/3.17.190.209/catalina-2020-12-03.out
    這個(gè)輸出路徑是上面server rsyslog.conf 里面 template,filter 已經(jīng)配置好的可以查看上面的配置
  2. 啟動(dòng)tomcat
    • ./var/log/apache-tomcat-8.5.60/bin/startup.sh
  3. 查看服務(wù)器端生成的文件
    • tail -f /var/log/rsyslog/3.17.190.209/catalina-2020-12-03.out
  4. 結(jié)果
    start tomcat.png
  • echo lbyang date: date +%Y-%m-%d:%H:%M:%S >> /var/log/apache-tomcat-8.5.60/logs/catalina.out
    手動(dòng)寫入文件.png

附錄

1. 登錄服務(wù)器AWS問題
我使用的是AWS使用.pem 證書的方式登錄嘉抓,用戶是來(lái)賓用戶索守。很多權(quán)限都沒有,切換到root 加自定義密碼方式

  • sudo passwd root #回車然后設(shè)置密碼
  • su root #切換用戶
  • vi /etc/ssh/sshd_config #打開文件抑片,修改下面的內(nèi)容為yes
    passwordAuthentication yes
  • systemctl restart sshd.service #重啟服務(wù)

2. 目錄權(quán)限問題
沒有辦法自定義目錄卵佛。比如/home/lb/tomcat/logs/Catalina.out,沒有辦法讀取啟動(dòng)rsyslog報(bào)錯(cuò),沒有權(quán)限
如果把tomat 安裝到 /var/log/下面就可以讀取

3. 參考的網(wǎng)址

property ( in English)
Message Properties:
These are extracted by rsyslog parsers from the original message. All message properties start with a letter.

The following message properties exist:

msg
the MSG part of the message (aka “the message” ; ))

rawmsg
the message excactly as it was received from the socket. Should be useful for debugging.

hostname
hostname from the message

source
alias for HOSTNAME

fromhost
hostname of the system the message was received from (in a relay chain, this is the system immediately in front of us and not necessarily the original sender). This is a DNS-resolved name, except if that is not possible or DNS resolution has been disabled.

fromhost-ip
The same as fromhost, but alsways as an IP address. Local inputs (like imklog) use 127.0.0.1 in this property.

syslogtag
TAG from the message

programname
the “static” part of the tag, as defined by BSD syslogd. For example, when TAG is “named[12345]”, programname is “named”.

pri
PRI part of the message - undecoded (single value)

pri-text
the PRI part of the message in a textual form with the numerical PRI appended in brackes (e.g. “l(fā)ocal0.err<133>”)

iut
the monitorware InfoUnitType - used when talking to a MonitorWare backend (also for Adiscon LogAnalyzer)

syslogfacility
the facility from the message - in numerical form

syslogfacility-text
the facility from the message - in text form

syslogseverity
severity from the message - in numerical form

syslogseverity-text
severity from the message - in text form

syslogpriority
an alias for syslogseverity - included for historical reasons (be careful: it still is the severity, not PRI!)

syslogpriority-text
an alias for syslogseverity-text

timegenerated
timestamp when the message was RECEIVED. Always in high resolution

timereported
timestamp from the message. Resolution depends on what was provided in the message (in most cases, only seconds)

timestamp
alias for timereported

protocol-version
The contents of the PROTCOL-VERSION field from IETF draft draft-ietf-syslog-protcol

structured-data
The contents of the STRUCTURED-DATA field from IETF draft draft-ietf-syslog-protocol

app-name
The contents of the APP-NAME field from IETF draft draft-ietf-syslog-protocol

procid
The contents of the PROCID field from IETF draft draft-ietf-syslog-protocol

msgid
The contents of the MSGID field from IETF draft draft-ietf-syslog-protocol

inputname
The name of the input module that generated the message (e.g. “imuxsock”, “imudp”). Note that not all modules necessarily provide this property. If not provided, it is an empty string. Also note that the input module may provide any value of its liking. Most importantly, it is not necessarily the module input name. Internal sources can also provide inputnames. Currently, “rsyslogd” is defined as inputname for messages internally generated by rsyslogd, for example startup and shutdown and error messages. This property is considered useful when trying to filter messages based on where they originated - e.g. locally generated messages (“rsyslogd”, “imuxsock”, “imklog”) should go to a different place than messages generated somewhere.

System Properties:
These properties are provided by the rsyslog core engine. They are not related to the message. All system properties start with a dollar-sign.
For example, timereported contains the timestamp from the message. Depending on how long the message was in the relay chain, this can be quite old. In contrast, $now is the system time when the message is being processed. Depending on your needs, you need one or the other. Usually, the message-based timestamp is the more important one, but that really depdends on the use case.

The following system properties exist:

$bom
The UTF-8 encoded Unicode byte-order mask (BOM). This may be useful in templates for RFC5424 support, when the character set is know to be Unicode.

$now
The current date stamp in the format YYYY-MM-DD

$year
The current year (4-digit)

$month
The current month (2-digit)

$day
The current day of the month (2-digit)

$hour
The current hour in military (24 hour) time (2-digit)

$hhour
The current half hour we are in. From minute 0 to 29, this is always 0 while from 30 to 59 it is always 1.

qhour The current quarter hour we are in. Much likeHHOUR, but values range from 0 to 3 (for the four quater hours that are in each hour)

$minute
The current minute (2-digit)

$myhostname
The name of the current host as it knows itself (probably useful for filtering in a generic way)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蓝丙,一起剝皮案震驚了整個(gè)濱河市级遭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌渺尘,老刑警劉巖挫鸽,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異鸥跟,居然都是意外死亡丢郊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門医咨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)枫匾,“玉大人,你說(shuō)我怎么就攤上這事拟淮「绍裕” “怎么了?”我有些...
    開封第一講書人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵很泊,是天一觀的道長(zhǎng)角虫。 經(jīng)常有香客問我,道長(zhǎng)委造,這世上最難降的妖魔是什么戳鹅? 我笑而不...
    開封第一講書人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮昏兆,結(jié)果婚禮上枫虏,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好隶债,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開白布腾它。 她就那樣靜靜地躺著,像睡著了一般燃异。 火紅的嫁衣襯著肌膚如雪携狭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,036評(píng)論 1 285
  • 那天回俐,我揣著相機(jī)與錄音逛腿,去河邊找鬼。 笑死仅颇,一個(gè)胖子當(dāng)著我的面吹牛单默,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播忘瓦,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼搁廓,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了耕皮?” 一聲冷哼從身側(cè)響起境蜕,我...
    開封第一講書人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎凌停,沒想到半個(gè)月后粱年,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡罚拟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年台诗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赐俗。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拉队,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出阻逮,到底是詐尸還是另有隱情粱快,我是刑警寧澤,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布叔扼,位于F島的核電站事哭,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏币励。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一珊拼、第九天 我趴在偏房一處隱蔽的房頂上張望食呻。 院中可真熱鬧,春花似錦、人聲如沸仅胞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)干旧。三九已至渠欺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間椎眯,已是汗流浹背挠将。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留编整,地道東北人舔稀。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像掌测,于是被迫代替她去往敵國(guó)和親内贮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容