logstash
• logstash 是什麼
– logstash是一個資料采集、加工處理以及傳輸的工具
• logstash 特點:
– 所有類型的資料集中處理
– 不同模式和格式資料的正常化
– 自定義日志格式的迅速擴充
– 為自定義資料源輕松添加插件
• logstash 安裝
– Logstash 依賴 java 環境,需要安裝 java-1.8.0-openjdk
– Logstash 沒有預設的配置檔案,需要手動配置
– logstash 安裝在 /opt/logstash 目錄下
# rpm -ivh logstash-2.3.4-1.noarch.rpm
# rpm -qc logstash
/etc/init.d/logstash
/etc/logrotate.d/logstash
/etc/sysconfig/logstash
// 檢視logstash 子產品插件清單
# /opt/logstash/bin/logstash-plugin list
logstash-codec-collectd
logstash-codec-json
.. ..
logstash-filter-anonymize
logstash-filter-checksum
.. ..
logstash-input-beats
logstash-input-exec
.. ..
logstash-output-cloudwatch
logstash-output-csv
logstash-patterns-core
第一清單示是 logstash 的子產品
第二清單示在那個區域段執行 codec 屬于編解碼 是字元編碼類型的 在全部的區域段可以運作
• logstash 工作結構
– { 資料源 } ==>
– input { } ==> //收集日志
– filter { } ==> //日志處理 整理格式
– output { } ==> //日志輸出
– { ES }
• logstash 裡面的類型
– 布爾值類型: ssl_enable => true
– 位元組類型:bytes => "1MiB"
– 字元串類型: name => "xkops"
– 數值類型: port => 22
– 數組: match => ["datetime","UNIX"]
– 哈希: options => {k => "v",k2 => "v2"}
– 編碼解碼: codec => "json"
– 路徑: file_path => "/tmp/filename"
– 注釋: #
• logstash 條件判斷
– 等于: ==
– 不等于: !=
– 小于: <
– 大于: >
– 小于等于: <=
– 大于等于: >=
– 比對正則: =~
– 不比對正則: !~
logstash 條件判斷
– 包含: in
– 不包含: not in
– 與: and
– 或: or
– 非與: nand
– 非或: xor
– 複合表達式: ()
– 取反符合: !()
配置 logastash
# cd /etc/logstash/
// logstash 預設沒有配置檔案
# vim logstash.conf
input{
stdin{} //标準輸入
}
filter{}
output{
stdout{} // 标準輸出
# /opt/logstash/bin/logstash -f logstash.conf // 相當cat
Settings: Default pipeline workers: 1
Pipeline main started
Hello word!!!
2018-01-26T13:21:22.031Z 0.0.0.0 Hello word!!!
– 上頁的配置檔案使用了 logstash-input-stdin 和logstash-output-stdout 兩個插件
檢視插件具體使用方法 的方式是 https://github.com/logstash-plugins
練習1
# vim logstash.conf
stdin{ codec => "json" }
stdout{}
# /opt/logstash/bin/logstash -f logstash.conf
12345 // 123456 不是 json 模式 所有報錯
A plugin had an unrecoverable error. Will restart this plugin.
Plugin: <LogStash::Inputs::Stdin codec=><LogStash::Codecs::JSON charset=>"UTF-8">>
Error: can't convert String into Integer {:level=>:error}
'abc'
2018-01-26T13:43:17.840Z 0.0.0.0 'abc'
'{"a":1,"b":2}'
2018-01-26T13:43:46.889Z 0.0.0.0 '{"a":1,"b":2}'
練習2
stdout{ codec => "rubydebug"} //調試資料的模式
'"aaa"'
{
"message" => "'\"aaa\"'",
"tags" => [
[0] "_jsonparsefailure"
],
"@version" => "1",
"@timestamp" => "2018-01-26T13:52:45.307Z",
"host" => "0.0.0.0"
{"aa":1,"bb":2}
"aa" => 1,
"bb" => 2,
"@timestamp" => "2018-01-26T13:53:00.452Z",
練習3
# touch /tmp/a.log /tmp/b.log
input{
file { //監控檔案
path => ["/tmp/a.log","/tmp/b.log"] //監控檔案 路徑
type => "testlog" //聲明檔案類型
}
}
filter{}
output{
stdout{ codec => "rubydebug"}
# /opt/logstash/bin/logstash -f logstash.conf
Settings: Default pipeline workers: 1
Pipeline main started
... //開始監控日志檔案
//切換到另一個終端測試
# cd /tmp/
// 為日志檔案 添加内容
# echo 12345 > a.log
// 這時在 會看見終端輸出監控資訊
{
"message" => "12345",
"@version" => "1",
"@timestamp" => "2018-01-27T00:45:15.470Z",
"path" => "/tmp/a.log",
"host" => "0.0.0.0",
"type" => "testlog"
# echo b123456 > b.log
"message" => "b123456",
"@timestamp" => "2018-01-27T00:45:30.487Z",
"path" => "/tmp/b.log",
# echo c123456 >> b.log
"message" => "c123456",
"@timestamp" => "2018-01-27T00:45:45.501Z",
// 預設記錄 讀取位置的檔案 在管理者家目錄 .sincedb_ 後面是一串哈希數
# cat /root/.sincedb_ab3977c541d1144f701eedeb3af4956a
3190503 0 64768 6
3190504 0 64768 16
# du -b /tmp/a.log du -b 檢視檔案類型
6/tmp/a.log
# du -b /tmp/b.log
16/tmp/b.log
// 進行優化
file {
start_position => "beginning" //設定當記錄位置的庫檔案不存在時 從檔案開始讀取
sincedb_path => "/var/lib/logstash/sincedb-access" //記錄位置的庫檔案 預設放在每個使用者下的 是以将記錄位置的庫檔案固定位置
path => ["/tmp/a.log","/tmp/b.log"]
type => "testlog"
}
stdout{ codec => "rubydebug"}
# rm -rf /root/.sincedb_ab3977c541d1144f701eedeb3af4956a
"message" => "12345",
"@version" => "1",
"@timestamp" => "2018-01-27T10:44:54.890Z",
"path" => "/tmp/a.log",
"host" => "0.0.0.0",
"type" => "testlog"
"message" => "b123456",
"@timestamp" => "2018-01-27T10:44:55.242Z",
"path" => "/tmp/b.log",
"message" => "c123456",
練習 tcp udp 插件
tcp {
host => "0.0.0.0"
port => 8888
type => "tcplog"
udp {
port => 9999
type => "udplog"
...
//在另一個終端檢視監聽端口
# netstat -pantu | grep -E "(8888|9999)"
tcp6 0 0 :::8888 :::* LISTEN 3098/java
udp6 0 0 :::9999 :::* 3098/java
模拟用戶端 發送資料封包
// 發送tcp 資料封包 exec 改變目前檔案描述符 标準輸入 标準輸出重新設定
# exec 9<>/dev/tcp/192.168.4.10/8888 //打開建立連接配接
# echo "hello world" >&9 //發送 hello world 字元串 給連接配接
# exec 9<&- // 關閉連接配接
// 接收到連接配接
"message" => "hello world",
"@timestamp" => "2018-01-27T11:01:35.356Z",
"host" => "192.168.4.11",
"port" => 48654,
"type" => "tcplog"
// 發送udp 資料封包
# exec 7<>/dev/udp/192.168.4.10/9999
# echo "is udp log" >&7
# exec 7<&-
"message" => "is udp log\n",
"@timestamp" => "2018-01-27T11:05:18.850Z",
"type" => "udplog",
"host" => "192.168.4.11"
// 發送檔案
# exec 8<>/dev/udp/192.168.4.10/9999
# cat /etc/hosts >&8
# exec 9<&-
// 接受到資訊
"message" => "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4\n::1 localhost localhost.localdomain localhost6 localhost6.localdomain6\n192.168.4.11\tes1\n192.168.4.12\tes2\n192.168.4.13\tes3\n192.168.4.14\tes4\n192.168.4.15\tes5\n",
"@timestamp" => "2018-01-27T11:10:31.099Z",
syslog 插件練習
syslog{
host => "192.168.4.10"
port => 514 //系統日志預設端口
type => "syslog"
# /opt/logstash/bin/logstash -f logstash.conf
. . .
# netstat -pantu | grep 514
tcp6 0 0 192.168.4.10:514 :::* LISTEN 3545/java
udp6 0 0 192.168.4.10:514 :::* 3545/java
//在用戶端主機上 自定義日志檔案 發給logstash 主機
# vim /etc/rsyslog.conf
# sed -n '74p' /etc/rsyslog.conf
local0.info@@192.168.4.10:514
# systemctl restart rsyslog.service
# logger -p local0.info -t testlog "hello world" // 發送一條測試日志
// logstash 主機收到日志
"message" => "hello world\n",
"@timestamp" => "2018-01-27T11:29:03.000Z",
"type" => "syslog",
"host" => "192.168.4.11",
"priority" => 134,
"timestamp" => "Jan 27 06:29:03",
"logsource" => "es1",
"program" => "testlog",
"severity" => 6,
"facility" => 16,
"facility_label" => "local0",
"severity_label" => "Informational"
擴充 如果想要把登入日志發給 logstash
# sed -n '75p' /etc/rsyslog.conf
authpriv.*@@192.168.4.10:514
//測試登入 登出
# exit
登出
Connection to 192.168.4.11 closed.
# ssh -X [email protected]
[email protected]'s password:
Last login: Sat Jan 27 05:27:21 2018 from 192.168.4.254
//實時接受的日志
"message" => "Accepted password for root from 192.168.4.254 port 50820 ssh2\n",
"@timestamp" => "2018-01-27T11:32:07.000Z",
"priority" => 86,
"timestamp" => "Jan 27 06:32:07",
"program" => "sshd",
"pid" => "3734",
"facility" => 10,
"facility_label" => "security/authorization",
"message" => "pam_unix(sshd:session): session opened for user root by (uid=0)\n",
• filter grok插件
– 解析各種非結構化的日志資料插件
– grok 使用正規表達式把飛結構化的資料結構化
– 在分組比對,正規表達式需要根據具體資料結構編寫
– 雖然編寫困難,但适用性極廣
– 幾乎可以應用于各類資料
• grok 正則分組比對
– 比對 ip 時間戳 和 請求方法
"(?<ip>(\d+\.){3}\d+) \S+ \S+
(?<time>.*\])\s+\"(?<method>[A-Z]+)"]
– 使用正則宏
%{IPORHOST:clientip} %{HTTPDUSER:ident} %{USER:auth}
\[%{HTTPDATE:timestamp}\] \"%{WORD:verb}
– 最終版本
%{COMMONAPACHELOG} \"(?<referer>[^\"]+)\"
\"(?<UA>[^\"]+)\"
練習 比對Apache 日志
# cd /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/
# vim grok-patterns //日志宏定義倉庫
....
COMMONAPACHELOG %{IPORHOST:clientip} %{HTTPDUSER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-)
# vim /tmp/test.log //在測試檔案中添加一條日志
220.181.108.115 - - [11/Jul/2017:03:07:16 +0800] "GET /%B8%DF%BC%B6%D7%E2%C1%DE%D6%F7%C8%CE/QYQiu_j.html HTTP/1.1" 200 20756 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "-" zxzs.buildhr.com 558742
# vim logstash.conf
input{
file{
start_position => "beginning"
sincedb_path => "/dev/null" //為了調試友善
path => [ "/tmp/test.log" ]
type => 'filelog'
}
}
filter{
grok{
match => ["message","%{COMMONAPACHELOG}"]
}
output{
stdout{ codec => "rubydebug"}
# /opt/logstash/bin/logstash -f logstash.conf Settings: Default pipeline workers: 1
Pipeline main started
{
"message" => "220.181.108.115 - - [11/Jul/2017:03:07:16 +0800] \"GET /%B8%DF%BC%B6%D7%E2%C1%DE%D6%F7%C8%CE/QYQiu_j.html HTTP/1.1\" 200 20756 \"-\" \"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)\" \"-\" zxzs.buildhr.com 558742",
"@version" => "1",
"@timestamp" => "2018-01-27T12:03:10.363Z",
"path" => "/tmp/test.log",
"host" => "0.0.0.0",
"type" => "filelog",
"clientip" => "220.181.108.115",
"ident" => "-",
"auth" => "-",
"timestamp" => "11/Jul/2017:03:07:16 +0800",
"verb" => "GET",
"request" => "/%B8%DF%BC%B6%D7%E2%C1%DE%D6%F7%C8%CE/QYQiu_j.html",
"httpversion" => "1.1",
"response" => "200",
"bytes" => "20756"
"message" => "",
"@version" => "1",
"@timestamp" => "2018-01-27T12:03:10.584Z",
"path" => "/tmp/test.log",
"host" => "0.0.0.0",
"type" => "filelog",
"tags" => [
[0] "_grokparsefailure"
]
編寫分析日志正則時 先區宏庫中尋找 找不到可以 去百度 盡量不要自己手動寫 會很累
練習 同時解析不同日志
sincedb_path => "/dev/null"
path => [ "/tmp/test.json" ]
type => 'jsonlog'
codec => 'json'
if [type] == "filelog"{
grok{
match => ["message","%{COMMONAPACHELOG}"]
}
[root@klog logstash]# /opt/logstash/bin/logstash -f logstash.conf
Settings: Default pipeline workers: 1
"@timestamp" => "2018-01-27T12:22:06.481Z",
"@timestamp" => "2018-01-27T12:22:06.834Z",
"@timestamp" => "2015-05-18T12:28:25.013Z",
"ip" => "79.1.14.87",
"extension" => "gif",
"response" => "200",
"geo" => {
"coordinates" => {
"lat" => 35.16531472,
"lon" => -107.9006142
},
"src" => "GN",
"dest" => "US",
"srcdest" => "GN:US"
},
"@tags" => [
[0] "success",
[1] "info"
],
"utc_time" => "2015-05-18T12:28:25.013Z",
"referer" => "http://www.slate.com/warning/b-alvin-drew",
"agent" => "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.50 Safari/534.24",
"clientip" => "79.1.14.87",
"bytes" => 774,
"host" => "motion-media.theacademyofperformingartsandscience.org",
"request" => "/canhaz/james-mcdivitt.gif",
"url" => "https://motion-media.theacademyofperformingartsandscience.org/canhaz/james-mcdivitt.gif",
"@message" => "79.1.14.87 - - [2015-05-18T12:28:25.013Z] \"GET /canhaz/james-mcdivitt.gif HTTP/1.1\" 200 774 \"-\" \"Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.50 Safari/534.24\"",
"spaces" => "this is a thing with lots of spaces wwwwoooooo",
"xss" => "<script>console.log(\"xss\")</script>",
"headings" => [
[0] "<h3>charles-bolden</h5>",
[1] "http://www.slate.com/success/barry-wilmore"
"links" => [
[0] "[email protected]",
[1] "http://facebook.com/info/anatoly-solovyev",
[2] "www.www.slate.com"
"relatedContent" => [],
"machine" => {
"os" => "osx",
"ram" => 8589934592
"@version" => "1",
"path" => "/tmp/test.json",
"type" => "jsonlog"
真正在項目中可以和開放人員商量 讓其提供json 格式的日志 這樣能輕松很多
output ES 插件
調試成功後,把資料寫入 ES 叢集
# cat logstash.conf
input{
file{
start_position => "beginning"
sincedb_path => "/dev/null"
path => [ "/tmp/test.log" ]
type => 'filelog'
}
path => [ "/tmp/test.json" ]
type => 'jsonlog'
codec => 'json'
}
filter{
if [type] == "filelog"{
grok{
match => ["message","%{COMMONAPACHELOG}"]
output{
elasticsearch {
hosts => ["192.168.4.11:9200","192.168.4.12:9200"]
index => "weblog"
flush_size => 2000
idle_flush_time => 10
# /opt/logstash/bin/logstash -f logstash.conf
Settings: Default pipeline workers: 1
Pipeline main started
本文轉自 Xuenqlve 51CTO部落格,原文連結:http://blog.51cto.com/13558754/2065865,如需轉載請自行聯系原作者