Logstash的插件

Logstash的插件:html

input插件:node

File:從指定的文件中讀取事件流;nginx

 

使用FileWatch(Ruby Gem庫)監聽文件的變化。正則表達式

.sincedb:記錄了每一個被監聽的文件的inode, major number, minor nubmer, pos; redis

一下是一個收集日誌簡單的示例: apache

input {vim

file {centos

path => ["/var/log/messages"]ruby

type => "system"網絡

start_position => "beginning"

  }

}

output {

stdout {

codec => rubydebug

  }
}

["/var/log/messages"]中能夠包含多個文件[item1, item2,...] start_position => "beginning"表示從第一行開始讀

udp:經過udp協議從網絡鏈接來讀取Message,其必備參數爲port,用於指明本身監聽的端口,host則用指明本身監聽的地址

collectd:性能監控程序,基於c語言開發,以守護進程方式運行,可以收集系統性能各方面的數據,並將收集的結果存儲下來,可以通

過network插件,把本身在本機收集到的數據發送給其餘主機

collectd的包在epel源中,yum -y install epel-release;yum -y install collectd, collecctd的配置文件爲/etc/collectd.conf

vim /etc/collectd.conf,將Global settings for the daemon下的Hostname設置一個名字:Hostname "node1"

找到LoadPlugin section,將LoadPlugin df去掉註釋,LoadPlugin network啓動

在<Plugin network> </Plugin> 的下面再定義一段:

<Plugin network>

<Server "192.168.204.135" "25826">

</Server>

</Plugin>

表示將數據傳給192.168.204.135主機,此主機監聽的端口爲25826

service collectd start

192.168.204.135安裝了lostash,下面是一個UDP的配置文件示例

input {

udp {

port => 25826

codec => collectd {}

type => "collectd"
  }
}

output {

stdout {

codec => rubydebug
  }
}

codec => collectd {} 將collectd發送過來的信息作專門的編碼

type => "collectd" 類型能夠隨意取名

logstash -f /etc/logstash/conf.d/udp.conf --configtest logstash -f /etc/logstash/conf.d/udp.conf

這是就能夠收到來之collectd的信息了

redis插件:

從redis讀取數據,支持redis channel和lists兩種方式

filter插件:

用於在將event經過output發出以前對其實現某些處理功能

grok:用於分析並結構化文本數據;目前 是logstash中將非結構化日誌數據轉化爲結構化的可查詢數據的不二之選。

syslog, apache, nginx

模式定義位置:/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-0.3.0/patterns/grok-patterns

語法格式:

%{SYNTAX:SEMANTIC}

SYNTAX:預約義模式名稱;

SEMANTIC:匹配到的文本的自定義標識符;

例如:1.1.1.1 GET /index.html 30 0.23

{ "message" => "%{IP:clientip} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %

{NUMBER:duration}" }

vim groksample.conf 一個配置示例

input {

stdin {}
  }

filter {

grok {

match => { "message" => "%{IP:clientip} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %

{NUMBER:duration}" }
  }
}

output {

stdout {

codec => rubydebug
  }
}

logstash -f /etc/logstash/conf.d/groksample.conf --configtest

logstash -f /etc/logstash/conf.d/groksample.conf

輸入1.1.1.1 GET /index.html 30 0.23, 得出結果

1.1.1.1 GET /index.html 30 0.23

{

"message" => "1.1.1.1 GET /index.html 30 0.231.1.1.1 GET /index.html 30 0.23",

"@version" => "1",

"@timestamp" => "2016-07-20T11:55:31.944Z",

"host" => "centos7",

"clientip" => "1.1.1.1",

"method" => "GET",

"request" => "/index.html",

"bytes" => "30",

"duration" => "0.231"

}

自定義grok的模式:grok的模式是基於正則表達式編寫,其元字符與其它用到正則表達式的工具awk/sed/grep/pcre差異不大

自定義的機會通常不大

匹配apache log示例 vim apachesample.conf

input {

file {

path => ["/var/log/httpd/access_log"]

type => "apachelog"

start_position => "beginning"
  }
}

filter {

grok {

match => { "message" => "%{COMBINEDAPACHELOG}" }

  }
}

output {

stdout {

codec => rubydebug
  }
}

nginx log的匹配方式:

將以下信息添加至 /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-0.3.0/patterns/grok-patterns文

件的尾部

#Nginx log

NGUSERNAME [a-zA-Z\.\@\-\+_%]+

NGUSER %{NGUSERNAME}

NGINXACCESS %{IPORHOST:clientip} - %{NOTSPACE:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%

{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})\" %

{NUMBER:response} (?:%{NUMBER:bytes}|-) %{QS:referrer} %{QS:agent} %{NOTSPACE:http_x_forwarded_for}

yum -y install epel-release;yum -y install nginx;systemctl start nginx

vim nginxsample.conf

input {

file {

path => ["/var/log/nginx/access.log"]

type => "nginxlog"

start_position => "beginning"
  }
}

filter {

grok {

match => { "message" => "%{NGINXACCESS}" }
  }
}

output {

stdout {

codec => rubydebug
  }
}
logstash -f /etc/logstash/conf.d/nginxsample.conf

相關文章
相關標籤/搜索