1. 程式人生 > >ELK之logstash系統日誌和nginx日誌收集-4

ELK之logstash系統日誌和nginx日誌收集-4

tps 字符串 方便 主設備號 add code shadow art index

logstash常用參數
  1 path

   是必須的選項,每一個file配置,都至少有一個path

   2 exclude

   是不想監聽的文件,logstash會自動忽略該文件的監聽。配置的規則與path類似,支持字符串或者數組,但是要求必須是絕對路徑。

   3 start_position

   是監聽的位置,默認是end,即一個文件如果沒有記錄它的讀取信息,則從文件的末尾開始讀取,也就是說,僅僅讀取新添加的內容。對於一些更新的日誌類型的監聽,通常直接使用end就可以了;相反,beginning就會從一個文件的頭開始讀取。但是如果記錄過文件的讀取信息,這個配置也就失去作用了。

   4 sincedb_path

   這個選項配置了默認的讀取文件信息記錄在哪個文件中,默認是按照文件的inode等信息自動生成。其中記錄了inode、主設備號、次設備號以及讀取的位置。因此,如果一個文件僅僅是重命名,那麽它的inode以及其他信息就不會改變,因此也不會重新讀取文件的任何信息。類似的,如果復制了一個文件,就相當於創建了一個新的inode,如果監聽的是一個目錄,就會讀取該文件的所有信息。

收集單個系統日誌並輸出至文件

[[email protected] config]# cat system-log.conf 
input {
  file {
    type => "meassage-log"
    path => "/var/log/messages"
    start_position => "beginning" #"第一次從頭收集,之後從新添加的日誌收集"
  }
  file {
    type => "secure-log"
    path => "/var/log/secure"
    start_position => "beginning"
  }
}
output {
 file {
   path => "/tmp/%{type}.%{+yyyy.MM.dd}"
 }
}
語法檢測
../bin/logstash -f system-log.conf -t

運行查看結果
技術分享圖片
查看/tmp下面的文件即可

收集nginx日誌和系統日誌寫入到elasticsearch

修改nginx日誌格式為json格式 方便查看和Kibana 展示
log_format access_json ‘{"@timestamp":"$time_iso8601",‘
        ‘"host":"$server_addr",‘
        ‘"clientip":"$remote_addr",‘
        ‘"size":$body_bytes_sent,‘
        ‘"responsetime":$request_time,‘
        ‘"upstreamtime":"$upstream_response_time",‘
        ‘"upstreamhost":"$upstream_addr",‘
        ‘"http_host":"$host",‘
        ‘"url":"$uri",‘
        ‘"domain":"$host",‘
        ‘"xff":"$http_x_forwarded_for",‘
        ‘"referer":"$http_referer",‘
        ‘"status":"$status"}‘;
access_log /data/wwwlogs/access_nginx.log access_json;
[[email protected] config]# cat system-log.yml
 input {
   file {
      type => "system-message"
      path => "/var/log/mess     ages"
      start_position => "beginning"
    }
   file {
      type => "system-secure"
      path => "/var/log/secure"
      start_position => "beginning"
   }
   file {
     type => "nginx-access"
     path => "/data/wwwlogs/access_nginx.log"
     start_position => "beginning"
     codec => json
  }
 }

 output {
   if[type] == "nginx-access" {
     elasticsearch {
       index => "nginx-access-%{+YYYY.MM.dd}"
        hosts => ["192.168.1.252:9200"]
     }
   }
   if[type] == "system-message" {
     elasticsearch {
        index => "system-message-%{+YYYY.MM.dd}"
        hosts => ["192.168.1.252:9200"]
     }
   }
   if[type] == "system-secure" {
      elasticsearch {
        index => "system-secure-%{+YYYY.MM.dd}"
        hosts => ["192.168.1.252:9200"]
     }
   }
 }

運行訪問nginx生成日誌

[[email protected] config]# ab -c 100 -n 100 http://192.168.1.252/

技術分享圖片
技術分享圖片

ELK之logstash系統日誌和nginx日誌收集-4