1. 程式人生 > >logstash(一):搭建syslog測試環境

logstash(一):搭建syslog測試環境

首先建立logstash配置檔案(命名為syslog.conf),專門用於解析syslog日誌,如下:

#   監聽514埠
input {
    syslog {
        port => "514"
    }
}
#   輸出到控制檯
output {
    stdout {
        codec => rubydebug
    }
}

第二步,停止本機的rsyslog(syslog)服務,以避免514埠衝突:

#   停止接收伺服器的日誌服務
systemctl stop rsyslog

第三步,利用第一步建立的配置檔案啟動logstash:

#   進入logstash目錄
./bin/logstash -f syslog.conf

第四步,在本機上測試是否能接收到rsyslog日誌:

#   利用Linux自帶的logger程式
# T採用TCP協議, P為埠號,n為主機地址
logger -T -P 514 -n 127.0.0.1 'Hello,World!'

如果在控制檯輸出如下內容,則表示syslog服務執行正常:

{
          "severity" => 0,
        "@timestamp" => 2017-08-04T06:29:34.512Z,
          "@version"
=> "1", "host" => "127.0.0.1", "message" => "<5>Aug 4 02:29:34 root: Hello,World!\u0000", "priority" => 0, "facility" => 0, "severity_label" => "Emergency", "tags" => [ [0] "_grokparsefailure_sysloginput" ], "facility_label"
=> "kernel" }

到目前為止,日誌接收伺服器已配置成功,對於要傳送的機器,還需要繼續配置,找到/etc/rsyslog.conf檔案,新增如下內容:

#   指向日誌伺服器,假定為192.168.1.204
*.* @@192.168.1.204:514

然後在需要轉發日誌的機器上啟動rsyslog服務:

systemctl restart rsyslog
# 檢查rsyslog服務是否已啟動,如果有輸出,則工作正常
netstat -aulntp | grep rsyslog

如果長時間沒有日誌輸出,我們還可以用logger繼續模擬,如下:

logger -T -P 514 -n 192.168.1.204 'Hello,World!'

最後,logstash日誌伺服器可遠遠不斷地接收到日誌資訊:

{
          "severity" => 6,
        "@timestamp" => 2017-08-04T06:30:01.000Z,
          "@version" => "1",
              "host" => "192.168.101.203",
           "program" => "systemd",
           "message" => "Started Session 28 of user root.\n",
          "priority" => 30,
         "logsource" => "localhost",
          "facility" => 3,
    "severity_label" => "Informational",
         "timestamp" => "Aug  4 02:30:01",
    "facility_label" => "system"
}