SlideShare a Scribd company logo
Elastic Stack
Log Analysis – Day 2
講師:吳奕慶
Logstash 介紹
• Logstash 安裝及配置
• Logstash 外掛套件介紹與安裝
• Logstash input 資料來源輸入
• Logstash codec 編碼與解碼器應用解說
• Logstash filter 資料篩選配置
• Logstash output 輸出配置
2
Logstash 架構
3
Logstash 安裝
• Install Java Runtime or JDK
• 下載
wget https://download.elastic.co/logstash/logstash/package
s/centos/logstash-2.3.4-1.noarch.rpm
• yum localinstall logstash-2.3.4-1.noarch.rpm
4
啟動 Logstash
• 手動啟動
.bin/Logstash –e ‘’
• Daemon install & start
systemctl daemon-reload
systemctl enable logstash.service
systemctl start logstash
systemctl status logstash
5
Hello world ! Logstash
• .bin/logstash -e
'input{stdin{}}output{stdout{codec=>rubydebug}}'
• Type “Hello World!”
• Console output
{
"message" => "Hello World!",
"@version" => "1",
“@timestamp” => “2016-08-05T19:35:28.567Z”, // joda time
"host" => "dozwu.local”
}
6
Logstash 指令參數(1)
• -e : 執行的意思 (bin/logstash -e '')
• -f ,–-config : 解決 shell (-e) 的限制,將組態先寫入檔案。
 可以使用單一檔案或指定目錄
 副檔名使用 .conf
 指定目錄時,是以字母順序執行,建議用 .conf 以編號方式。
• -t ,--configtest : 測試所寫的 .conf 語法是否正確
• -l, --log : 可指定 log 輸出的路徑檔案
7
Logstash 指令參數(2)
• -w, --pipeline-workers :
執行 filter , output 的 JVM 執行緒,預設等於 CPU kernel 數
• -b, --pipeline-batch-size:
• 指定執行 filter, output 前,累積多少筆資料才執行。
• 預設 250 筆
• 越大越好但是越耗 JVM 記憶體
• -u, --pipeline-batch-delay:
• 大量處理資料時最多延遲時間
• 預設 5ms
8
Logstash 指令參數(3)
• -p, --pluginpath :
可指定自行客製化的 logstash 外掛之路徑
• --verbose :
• 輸出詳細的資料處理資訊
• --debug :
• 輸出更詳盡的資料處理資訊,方便除錯。
• -r : 監看 .conf 是否變更,並自動重新載入
9
Logstash 外掛安裝及更新
• 使用 1.5 版本後,使用獨立的 gem package 安裝
• 列出所有已安裝的外掛
bin/logstash-plugin list
bin/logstash-plugin list --group output
• 安裝外掛
• bin/logstash-plugin install logstash-output-csv
• 本機安裝 bin/logstash-plugin install /tmp/logstash-filter-ntc.gem
• 更新外掛
• bin/logstash-plugin update logstash-output-csv
10
Logstash 語法(1)
• https://www.elastic.co/guide/en/logstash/current/input-
plugins.html
• Input 輸入處理
• Filter 過濾內容或新增特定欄位
• Output 輸出處理
11
Logstash 語法(2)-基本語法
• 基本語法
input {
stdin {
}
}
output{
stdout{
}
}
12
Logstash 語法(3)-資料型態
• bool
delete => true
• string
firstname => “YI-CHING”
• integer,float
zone => 615
• array
employees => [“James”, “Ellen”, “Jay”, “Ray”]
13
Logstash 語法(4)-資料型態
• hash
Profile =>{
name => ”Yi-Ching Wu”
city => “Taipei”
mobile => “0933-541-681”
}
• codec
codec => “json” , code => “plain”
• password
ntc_password => “password”
• path
ntc_path => "/tmp/logstash“
14
Logstash 條件判斷
• ==(等於), !=(不等於), <(小於), >(大於), <=(小於等於), >=(大於等於)
• =~(Regular Match), !~(Regular not Match)
• in(包含), not in(不包含)
• and(且), or(或), nand(not and), xor(not or)
• () , !() 條件括號
15
Logstash Input: File
input {
file {
codec =>... #可選項,預設是plain,可通過這個參數設置編碼方式
discover_interval =>... #可選項,number,每隔多久去檢查被監看的 path 下是否有新的檔案。預設值是 15 秒。
exclude =>... #可選項,array,排除不想監看處理的檔案。
sincedb_path =>... #可選項,string,預設在 $HOME/.sincedb,可修改 sincedb 檔案到其他位置。
sincedb_write_interval =>... #可選項, number,logstash 每隔多久寫一次 sincedb 檔案,預設是 15 秒。
stat_interval =>... #可選項, number, logstash 每隔一段時間檢查一次被監看檔案狀態是否有變更?預設是 1 秒。
start_position =>...
#可選項, string , logstash 讀取檔案的位置為何,預設是結束位置,也就是說 logstash 會以類似 tail -F 的執行指令。
如果你是要匯入所有原始資料,可為 “beginning”,就從頭開始讀取,有點類似 cat,但是讀取到最
後不會自動停止,
而是繼續變成 tail –F, 預設值 “end”。
path =>... # 必選項, array ,設定要處理的檔案路徑, 可設定多個路徑
tags =>... # 可選項, array,在資料處理過程中,由具體的外掛程式來添加或者刪除的標記
type =>... # 可選項, string,定義資料來源的類型,可任意定義,如 Linux 系統 Log 可以定義為"syslog"
}
} 16
Logstash Input: 多個類型 File 設定
input {
file {
path => "/tmp/google.csv"
start_position => beginning
type => "google"
add_field => {"corporate" => "%{type}"}
}
file {
type => "syslog"
path => [ "/var/log/messages", "/var/log/syslog", "/var/log/*.log" ]
#(* 萬用字元)
}
}
output{
stdout{
}
}
17
Logstash 練習(1) – file,csv
• 下載:
http://finance.yahoo.com/quote/GOOG/history?period1=1092844800&period2=1469
635200&interval=1d&filter=history&frequency=1d
• 參考: 05.Logstash股票csv練習.txt
• input : file
• filter : csv, column map, mutate(convert)
• output :elasticsearch, stdout
18
Logstash 練習(2) – file,system log
• 參考: 06.Logstash_system_log練習.txt
• input : file ,systemlog,多檔案路徑, (*) 萬用字元
• filter : grok, date
• output :elasticsearch, stdout
19
Logstash Input: RSyslog
• Centos6 以前使用 syslog 紀錄系統資訊
• Centos6 以後改用 rsyslog, 為多執行緒的程式
• 組態檔 : /etc/rsyslog.log
• 修改完後須重新啟動 systemctl restart rsyslog
20
Logstash 練習 – rsyslog
• 參考:07.Logstash_rsyslog練習.txt
• input : syslog
• filter : none
• output : stdout
21
Logstash Input: TCP
input {
tcp {
add_field=>... # hash 型態,可選項,預設 {}
codec=>... #可選項,預設 plain
data_timeout=>... # 可選項,number,預設 -1
host=>... #可選項,預設 0.0.0.0
mode=>... #可選項,“server” or “client” ,預設是 server
port=>... #埠號
ssl_cacert=>... #可選項,CA file 路徑
ssl_cert=>... #可選項,cert file 路徑
ssl_enable=>... # boolean 型態,可選項,預設 false
ssl_key=>... #一個可用的檔案系統路徑,可選項
ssl_key_passphrase=>... #密碼,可選項,預設 nil
ssl_verify=>... # boolean 型態,可選項,預設 false
tags=>... # array 型態,可選項
type=>... # string 型態,可選項
}
}
22
Logstash 練習 – TCP
• 參考:08.Logstash_TCP練習.txt
• input : TCP
• filter : none
• output : stdout
23
Logstash Codec 設定
• Codec 可將輸入或輸出的資料進行格式的轉換
• 格式類型:
https://www.elastic.co/guide/en/logstash/current/codec-
plugins.html
24
https://www.elastic.co/guide/en/logstash/current/cod
ec-plugins.html
Logstash Codec : plain
• 輸出或輸入可以為自由格式 plain text 純文字
25
Logstash Codec 範例: input plain
input {
stdin {
codec => “plain”
path => “/tmp/*.txt”
}
}
output {
stdout{ }
}
26
Logstash Codec : json
• 若資料內容為 json , codec => “json”
27
Logstash Codec 練習: input json
• 參考:11.logstash_codec_json練習.txt
28
Logstash Codec : jsonline
• 若資料內容為 json , codec => “jsonline”
29
Logstash Codec 練習: input jsonline
• 參考 09.logstash_input_jsonline練習.txt
30
Logstash Codec : rubydebug
• codec => “rubydebug”
• 使用 ruby awsome print 來剖析資料內容
31
Logstash Codec 練習: output rubydebug
input {
stdin {
codec => json
}
}
output {
stdout{
codec => rubydebug
}
}
-------
輸入
{firstname“:”Amber“,”lastname“:”Duke“}
=> 查看結果
32
Logstash Filter 設定
• json
• grok
• csv
33
Logstash Filter : Json
• 若資料格式為 Json 則可透過此設定將格式及內容轉換你需要的輸出
filter {
json {
add_field=> ... #hash(可選),預設{ }
add_tag=> ... #array(可選),預設[ ]
remove_field=> ... #array(可選),預設[ ]
remove_tag=> ... #array(可選),預設[ ]
source=> ... #string(必要)
target=> ... #string(可選)
}
}
34
Logstash Filter 練習: json
• 參考 09.logstash_input_jsonline練習.txt
input {
stdin { }
}
filter {
json {
source=>"message"
}
}
output {
stdout {codec=>rubydebug}
}
35
Logstash Filter : grok
• grok 是 logstash 中受歡迎的工具之一,可以協助你解析大部分
非結構化的資料來源
• pattern: https://github.com/logstash-plugins/logstash-patterns-
core/blob/master/patterns/grok-patterns
• 測試工具: http://grokdebug.herokuapp.com/
36
Logstash Filter 練習: grok
• 手動產出 Nginx log sample
• 練習一:參考 10.logstash_filter_grok練習.txt
• 練習二:線上 grok 測試工具
37
Logstash Output 設定
• 可設定將資料結果輸出的位置
• 如:
• Elasticsearch
• stdout
• file
• hadoop
• tcp
• etc.
38
Logstash Output 練習: file
input {
stdin { }
}
output {
file {
path => "/tmp/logtstash_output_file.log"
}
stdout {
codec => rubydebug
}
}
39
Logstash Output 練習: file by date
input {
stdin { }
}
output {
file {
path => "/tmp/logtstash_output_file.log"
}
stdout {
codec => rubydebug
}
}
40
Logstash Output 練習: file
input {
stdin { }
}
output {
file {
path => "/tmp/logtstash_output_file.log"
}
stdout {
codec => rubydebug
}
}
41
Logstash Output 練習: file by date
input {
stdin { }
}
output {
file {
path => ”/tmp/ %{+YYYY.MM.dd}-%{host}- out_file.log”
message_format => “ %{ip} ”
gzip => true
}
}
42
Logstash Output 練習: tcp
output {
tcp {
codec=>json_lines
host=>"127.0.0.1"
port=>5566
mode=>"server"
}
}
43
Logstash Output 練習: Elasticsearch
output {
elasticsearch {
hosts => ["127.0.0.1:9200"] #cluster => “NTC_ELK“
action => "index"
index => "stock_index"
}
}
44
Logstash Output 範例: Email
output {
email {
port => "25"
address => "smtp.trendmicro.com"
username => “admin@trendmicro.com"
password => “123456789"
authentication => "plain"
use_tls => true
from => " elk@trendmicro.com "
subject => "Warning: %{title}"
to => “doz@gmail.com"
via => "smtp"
body => "%{message}"
}
}
45
Kibana 安裝及設定
• wget https://download.elastic.co/kibana/kibana/kibana-
4.5.3-1.x86_64.rpm
• yum localinstall kibana-4.5.3-1.x86_64.rpm
• http://127.0.0.1:5601
46
Kibana 設定檔說明
• kibana.yml 為 kibana 預設的主要設定檔案
• port : 預設 5601。
• Host : 定義 kibana IP address。
• elasticsearch_url :elasticsearch 的位置。
• elasticsearch_preserve_host :設定 true,瀏覽器請求中的主機名稱作為 Kibana 發送
給 Elasticsearch 時請求的主機名稱。
• kibana_index : 預設名字為.kibana ,kibana 連接了elasticsearch服務後,也會建立一個
index 來儲存在 kibana 操作及設定的資訊。
• default_app_id : 要讓使用者預設是打開何介面,有 discover, visualize, dashboard
和 settings 選擇,預設為 discover。
• request_timeout :等待 Kibana 後端的回應的逾時時間,單位 ms,預設300000 ms
• shard_timeout :Elasticsearch 等待shard 回應的逾時時間;預設 0 表 disable 。
• verify_ssl :是否需要驗證 Elasticsearch SSL ,false 就是關閉,預設 true 開啟。
47
Kibana 介紹- Discover
48
• 主要讓我們可以在介面上做資料查詢,進而篩選資料.
Kibana 查詢語法(1)
• 單一 term 查詢
Jordan, ELK
• 指定欄位 field 查詢
field:value
city:Taipei, name:Jordan
• 萬用字元
? 比對單一字元 如: Ela?ticsearch
* 比對 0 到多個字元 如: E*
• 範圍查詢
releasedate : [2016-08-01 TO 2016-08-31]
releasedate : {2016-08-01 TO 2016-08-31}
49
Kibana 查詢語法(2)
• 邏輯操作 AND, OR
firstname:H* AND age:20
firstname:H* OR age:20
• + :查詢結果中必須包含 , - :不能包含
+city:T* -birthday:1980-01-01 name:J*
city 欄位結果中必須包含T開頭的,生日不能為1980-01-01,name 欄位 J 開頭
的有或沒有都可
• 分組
(city:T* OR gender:male) AND country:USA 先查詢city T 開頭或是性別為 male 的結果,
然後再與國家為 USA 的結合後查詢
50
Kibana 查詢語法(3)
• 欄位分組
firstname:(+J* -Jo*) 查詢 firstname 欄位 J 開頭的結果,並且排除
firstname 欄位 Jo 開頭的結果
• 特殊字元 (+ - && || ! () {} [] ^" ~ * ? : )
查詢時若條件遇到上述特殊字元,需使用  進行跳脫處理.
51
Kibana Discover 語法練習
• 請自行選擇感興趣的 Index 練習
52
Kibana 介紹- Visualize
53
• 用來將篩選過的資料進行視覺化的處理。
Kibana Visualize 視覺化圖形類型
54
引用:http://kibana.logstash.es/content/kibana/v4/visualize/
Kibana Visualize 練習
55
• 使用股票交易資料進行練習
Kibana Dashboard 介紹
• 可以將已儲存的 Visualize 圖表放進一個數位看板內供檢視
56
參考資料
• video : Elasticsearch from the bottom up
• book: Elasticsearch: The Definitive Guide
• book: Learning ELK Stack
• book: The Logstash book
• book: Kibana Essentials
• site: http://logz.io/blog/elasticsearch-cheat-sheet/
• site: How to Deploy the ELK Stack in Production
57
Document models of Elasticsearch
• Document 盡量以反正規化 (de-normalize) 形式儲存
• 同一個 Index 內可使用不同的 Type 對應不同的資料
• 同一個 Document 可以使用網狀 (Nested objects) 結構的方式
儲存
• Parent-child relationship –
• 兩者可以使用不同的 Mapping
• 1-many relationship
58
Nested objects example
59
Parent-child relationship example
60
• parent-child mapping
Parent-child relationship example(cont.1)
61
• parent bulk indexing
• child indexing
Parent-child relationship example(cont.2)
62
• finding parent with their child

More Related Content

What's hot (20)

PDF
Spark sql培训
Jiang Yu
 
PDF
Data Crawler using Python (I) | WeiYuan
Wei-Yuan Chang
 
PDF
SMACK Dev Experience
Chih-Hsuan Hsu
 
PDF
搜索技术分享
endless_yy
 
PPTX
FHIR Server 安裝與使用
Lorex L. Yang
 
PPTX
北護大/FHIR 開發簡介與應用
Lorex L. Yang
 
PPT
Coreseek/Sphinx 全文检索实践指南
HonestQiao
 
PPT
scrapy+sphinx搭建搜索引擎
Ping Yin
 
PPTX
From Java Stream to Java DataFrame
Chen-en Lu
 
PDF
Parse, cloud code 介紹
wantingj
 
PDF
Introduction to big data
邦宇 叶
 
PPTX
Lucene实践
晓锋 陈
 
PDF
Elastic Stack 最新动态
Elasticsearch
 
PPTX
陽明大學/FHIR 快速跳坑指南
Lorex L. Yang
 
PDF
2016-07-12 Introduction to Big Data Platform Security
Jazz Yao-Tsung Wang
 
PDF
Apache Zookeeper 分布式服务框架
Cabin WJ
 
PDF
Linux Binary Exploitation - Heap Exploitation
Angel Boy
 
PPTX
Web crawler - Scrapy
yafish
 
PPT
山頂洞人日記 - 回歸到最純樸的開發
koji lin
 
PDF
Linq 入门
greenerycn
 
Spark sql培训
Jiang Yu
 
Data Crawler using Python (I) | WeiYuan
Wei-Yuan Chang
 
SMACK Dev Experience
Chih-Hsuan Hsu
 
搜索技术分享
endless_yy
 
FHIR Server 安裝與使用
Lorex L. Yang
 
北護大/FHIR 開發簡介與應用
Lorex L. Yang
 
Coreseek/Sphinx 全文检索实践指南
HonestQiao
 
scrapy+sphinx搭建搜索引擎
Ping Yin
 
From Java Stream to Java DataFrame
Chen-en Lu
 
Parse, cloud code 介紹
wantingj
 
Introduction to big data
邦宇 叶
 
Lucene实践
晓锋 陈
 
Elastic Stack 最新动态
Elasticsearch
 
陽明大學/FHIR 快速跳坑指南
Lorex L. Yang
 
2016-07-12 Introduction to Big Data Platform Security
Jazz Yao-Tsung Wang
 
Apache Zookeeper 分布式服务框架
Cabin WJ
 
Linux Binary Exploitation - Heap Exploitation
Angel Boy
 
Web crawler - Scrapy
yafish
 
山頂洞人日記 - 回歸到最純樸的開發
koji lin
 
Linq 入门
greenerycn
 

Similar to Elastic stack day-2 (16)

PDF
Continuous Delivery with Ansible x GitLab CI (2e)
Chu-Siang Lai
 
PPTX
Elk 簡介與操作實例
JacksonFang
 
PDF
Continuous Delivery Workshop with Ansible x GitLab CI (2nd)
Chu-Siang Lai
 
PDF
Log分析-使用ELK.pdf
Click-AP Learning Technology Co.,Ltd.
 
PDF
Pymacs 初體驗
Kai-Yuan Cheng
 
ODP
新北市教師工作坊 -- Bash script programming 介紹
fweng322
 
PDF
Git tutorial for windows user (給 Windows user 的 Git 教學)
Cloud Tu
 
PDF
Bitbucket pipeline CI
Zero Huang
 
PDF
Continuous Delivery with Ansible x GitLab CI
Chu-Siang Lai
 
PPT
網路組-Ubuntu介紹
maryqute520
 
PDF
開發環境建置
Shengyou Fan
 
PDF
IoT Speech recognition Chatbot
Hung-Chih(Henry) Huang
 
PDF
現代 IT 人一定要知道的 Ansible 自動化組態技巧
Chu-Siang Lai
 
PDF
Continuous Delivery Workshop with Ansible x GitLab CI
Chu-Siang Lai
 
PDF
Java SE 7 技術手冊投影片第 12 章 - 通用API
Justin Lin
 
PDF
[系列活動] Python爬蟲實戰
台灣資料科學年會
 
Continuous Delivery with Ansible x GitLab CI (2e)
Chu-Siang Lai
 
Elk 簡介與操作實例
JacksonFang
 
Continuous Delivery Workshop with Ansible x GitLab CI (2nd)
Chu-Siang Lai
 
Log分析-使用ELK.pdf
Click-AP Learning Technology Co.,Ltd.
 
Pymacs 初體驗
Kai-Yuan Cheng
 
新北市教師工作坊 -- Bash script programming 介紹
fweng322
 
Git tutorial for windows user (給 Windows user 的 Git 教學)
Cloud Tu
 
Bitbucket pipeline CI
Zero Huang
 
Continuous Delivery with Ansible x GitLab CI
Chu-Siang Lai
 
網路組-Ubuntu介紹
maryqute520
 
開發環境建置
Shengyou Fan
 
IoT Speech recognition Chatbot
Hung-Chih(Henry) Huang
 
現代 IT 人一定要知道的 Ansible 自動化組態技巧
Chu-Siang Lai
 
Continuous Delivery Workshop with Ansible x GitLab CI
Chu-Siang Lai
 
Java SE 7 技術手冊投影片第 12 章 - 通用API
Justin Lin
 
[系列活動] Python爬蟲實戰
台灣資料科學年會
 
Ad

Elastic stack day-2

  • 1. Elastic Stack Log Analysis – Day 2 講師:吳奕慶
  • 2. Logstash 介紹 • Logstash 安裝及配置 • Logstash 外掛套件介紹與安裝 • Logstash input 資料來源輸入 • Logstash codec 編碼與解碼器應用解說 • Logstash filter 資料篩選配置 • Logstash output 輸出配置 2
  • 4. Logstash 安裝 • Install Java Runtime or JDK • 下載 wget https://download.elastic.co/logstash/logstash/package s/centos/logstash-2.3.4-1.noarch.rpm • yum localinstall logstash-2.3.4-1.noarch.rpm 4
  • 5. 啟動 Logstash • 手動啟動 .bin/Logstash –e ‘’ • Daemon install & start systemctl daemon-reload systemctl enable logstash.service systemctl start logstash systemctl status logstash 5
  • 6. Hello world ! Logstash • .bin/logstash -e 'input{stdin{}}output{stdout{codec=>rubydebug}}' • Type “Hello World!” • Console output { "message" => "Hello World!", "@version" => "1", “@timestamp” => “2016-08-05T19:35:28.567Z”, // joda time "host" => "dozwu.local” } 6
  • 7. Logstash 指令參數(1) • -e : 執行的意思 (bin/logstash -e '') • -f ,–-config : 解決 shell (-e) 的限制,將組態先寫入檔案。  可以使用單一檔案或指定目錄  副檔名使用 .conf  指定目錄時,是以字母順序執行,建議用 .conf 以編號方式。 • -t ,--configtest : 測試所寫的 .conf 語法是否正確 • -l, --log : 可指定 log 輸出的路徑檔案 7
  • 8. Logstash 指令參數(2) • -w, --pipeline-workers : 執行 filter , output 的 JVM 執行緒,預設等於 CPU kernel 數 • -b, --pipeline-batch-size: • 指定執行 filter, output 前,累積多少筆資料才執行。 • 預設 250 筆 • 越大越好但是越耗 JVM 記憶體 • -u, --pipeline-batch-delay: • 大量處理資料時最多延遲時間 • 預設 5ms 8
  • 9. Logstash 指令參數(3) • -p, --pluginpath : 可指定自行客製化的 logstash 外掛之路徑 • --verbose : • 輸出詳細的資料處理資訊 • --debug : • 輸出更詳盡的資料處理資訊,方便除錯。 • -r : 監看 .conf 是否變更,並自動重新載入 9
  • 10. Logstash 外掛安裝及更新 • 使用 1.5 版本後,使用獨立的 gem package 安裝 • 列出所有已安裝的外掛 bin/logstash-plugin list bin/logstash-plugin list --group output • 安裝外掛 • bin/logstash-plugin install logstash-output-csv • 本機安裝 bin/logstash-plugin install /tmp/logstash-filter-ntc.gem • 更新外掛 • bin/logstash-plugin update logstash-output-csv 10
  • 11. Logstash 語法(1) • https://www.elastic.co/guide/en/logstash/current/input- plugins.html • Input 輸入處理 • Filter 過濾內容或新增特定欄位 • Output 輸出處理 11
  • 12. Logstash 語法(2)-基本語法 • 基本語法 input { stdin { } } output{ stdout{ } } 12
  • 13. Logstash 語法(3)-資料型態 • bool delete => true • string firstname => “YI-CHING” • integer,float zone => 615 • array employees => [“James”, “Ellen”, “Jay”, “Ray”] 13
  • 14. Logstash 語法(4)-資料型態 • hash Profile =>{ name => ”Yi-Ching Wu” city => “Taipei” mobile => “0933-541-681” } • codec codec => “json” , code => “plain” • password ntc_password => “password” • path ntc_path => "/tmp/logstash“ 14
  • 15. Logstash 條件判斷 • ==(等於), !=(不等於), <(小於), >(大於), <=(小於等於), >=(大於等於) • =~(Regular Match), !~(Regular not Match) • in(包含), not in(不包含) • and(且), or(或), nand(not and), xor(not or) • () , !() 條件括號 15
  • 16. Logstash Input: File input { file { codec =>... #可選項,預設是plain,可通過這個參數設置編碼方式 discover_interval =>... #可選項,number,每隔多久去檢查被監看的 path 下是否有新的檔案。預設值是 15 秒。 exclude =>... #可選項,array,排除不想監看處理的檔案。 sincedb_path =>... #可選項,string,預設在 $HOME/.sincedb,可修改 sincedb 檔案到其他位置。 sincedb_write_interval =>... #可選項, number,logstash 每隔多久寫一次 sincedb 檔案,預設是 15 秒。 stat_interval =>... #可選項, number, logstash 每隔一段時間檢查一次被監看檔案狀態是否有變更?預設是 1 秒。 start_position =>... #可選項, string , logstash 讀取檔案的位置為何,預設是結束位置,也就是說 logstash 會以類似 tail -F 的執行指令。 如果你是要匯入所有原始資料,可為 “beginning”,就從頭開始讀取,有點類似 cat,但是讀取到最 後不會自動停止, 而是繼續變成 tail –F, 預設值 “end”。 path =>... # 必選項, array ,設定要處理的檔案路徑, 可設定多個路徑 tags =>... # 可選項, array,在資料處理過程中,由具體的外掛程式來添加或者刪除的標記 type =>... # 可選項, string,定義資料來源的類型,可任意定義,如 Linux 系統 Log 可以定義為"syslog" } } 16
  • 17. Logstash Input: 多個類型 File 設定 input { file { path => "/tmp/google.csv" start_position => beginning type => "google" add_field => {"corporate" => "%{type}"} } file { type => "syslog" path => [ "/var/log/messages", "/var/log/syslog", "/var/log/*.log" ] #(* 萬用字元) } } output{ stdout{ } } 17
  • 18. Logstash 練習(1) – file,csv • 下載: http://finance.yahoo.com/quote/GOOG/history?period1=1092844800&period2=1469 635200&interval=1d&filter=history&frequency=1d • 參考: 05.Logstash股票csv練習.txt • input : file • filter : csv, column map, mutate(convert) • output :elasticsearch, stdout 18
  • 19. Logstash 練習(2) – file,system log • 參考: 06.Logstash_system_log練習.txt • input : file ,systemlog,多檔案路徑, (*) 萬用字元 • filter : grok, date • output :elasticsearch, stdout 19
  • 20. Logstash Input: RSyslog • Centos6 以前使用 syslog 紀錄系統資訊 • Centos6 以後改用 rsyslog, 為多執行緒的程式 • 組態檔 : /etc/rsyslog.log • 修改完後須重新啟動 systemctl restart rsyslog 20
  • 21. Logstash 練習 – rsyslog • 參考:07.Logstash_rsyslog練習.txt • input : syslog • filter : none • output : stdout 21
  • 22. Logstash Input: TCP input { tcp { add_field=>... # hash 型態,可選項,預設 {} codec=>... #可選項,預設 plain data_timeout=>... # 可選項,number,預設 -1 host=>... #可選項,預設 0.0.0.0 mode=>... #可選項,“server” or “client” ,預設是 server port=>... #埠號 ssl_cacert=>... #可選項,CA file 路徑 ssl_cert=>... #可選項,cert file 路徑 ssl_enable=>... # boolean 型態,可選項,預設 false ssl_key=>... #一個可用的檔案系統路徑,可選項 ssl_key_passphrase=>... #密碼,可選項,預設 nil ssl_verify=>... # boolean 型態,可選項,預設 false tags=>... # array 型態,可選項 type=>... # string 型態,可選項 } } 22
  • 23. Logstash 練習 – TCP • 參考:08.Logstash_TCP練習.txt • input : TCP • filter : none • output : stdout 23
  • 24. Logstash Codec 設定 • Codec 可將輸入或輸出的資料進行格式的轉換 • 格式類型: https://www.elastic.co/guide/en/logstash/current/codec- plugins.html 24 https://www.elastic.co/guide/en/logstash/current/cod ec-plugins.html
  • 25. Logstash Codec : plain • 輸出或輸入可以為自由格式 plain text 純文字 25
  • 26. Logstash Codec 範例: input plain input { stdin { codec => “plain” path => “/tmp/*.txt” } } output { stdout{ } } 26
  • 27. Logstash Codec : json • 若資料內容為 json , codec => “json” 27
  • 28. Logstash Codec 練習: input json • 參考:11.logstash_codec_json練習.txt 28
  • 29. Logstash Codec : jsonline • 若資料內容為 json , codec => “jsonline” 29
  • 30. Logstash Codec 練習: input jsonline • 參考 09.logstash_input_jsonline練習.txt 30
  • 31. Logstash Codec : rubydebug • codec => “rubydebug” • 使用 ruby awsome print 來剖析資料內容 31
  • 32. Logstash Codec 練習: output rubydebug input { stdin { codec => json } } output { stdout{ codec => rubydebug } } ------- 輸入 {firstname“:”Amber“,”lastname“:”Duke“} => 查看結果 32
  • 33. Logstash Filter 設定 • json • grok • csv 33
  • 34. Logstash Filter : Json • 若資料格式為 Json 則可透過此設定將格式及內容轉換你需要的輸出 filter { json { add_field=> ... #hash(可選),預設{ } add_tag=> ... #array(可選),預設[ ] remove_field=> ... #array(可選),預設[ ] remove_tag=> ... #array(可選),預設[ ] source=> ... #string(必要) target=> ... #string(可選) } } 34
  • 35. Logstash Filter 練習: json • 參考 09.logstash_input_jsonline練習.txt input { stdin { } } filter { json { source=>"message" } } output { stdout {codec=>rubydebug} } 35
  • 36. Logstash Filter : grok • grok 是 logstash 中受歡迎的工具之一,可以協助你解析大部分 非結構化的資料來源 • pattern: https://github.com/logstash-plugins/logstash-patterns- core/blob/master/patterns/grok-patterns • 測試工具: http://grokdebug.herokuapp.com/ 36
  • 37. Logstash Filter 練習: grok • 手動產出 Nginx log sample • 練習一:參考 10.logstash_filter_grok練習.txt • 練習二:線上 grok 測試工具 37
  • 38. Logstash Output 設定 • 可設定將資料結果輸出的位置 • 如: • Elasticsearch • stdout • file • hadoop • tcp • etc. 38
  • 39. Logstash Output 練習: file input { stdin { } } output { file { path => "/tmp/logtstash_output_file.log" } stdout { codec => rubydebug } } 39
  • 40. Logstash Output 練習: file by date input { stdin { } } output { file { path => "/tmp/logtstash_output_file.log" } stdout { codec => rubydebug } } 40
  • 41. Logstash Output 練習: file input { stdin { } } output { file { path => "/tmp/logtstash_output_file.log" } stdout { codec => rubydebug } } 41
  • 42. Logstash Output 練習: file by date input { stdin { } } output { file { path => ”/tmp/ %{+YYYY.MM.dd}-%{host}- out_file.log” message_format => “ %{ip} ” gzip => true } } 42
  • 43. Logstash Output 練習: tcp output { tcp { codec=>json_lines host=>"127.0.0.1" port=>5566 mode=>"server" } } 43
  • 44. Logstash Output 練習: Elasticsearch output { elasticsearch { hosts => ["127.0.0.1:9200"] #cluster => “NTC_ELK“ action => "index" index => "stock_index" } } 44
  • 45. Logstash Output 範例: Email output { email { port => "25" address => "smtp.trendmicro.com" username => “[email protected]" password => “123456789" authentication => "plain" use_tls => true from => " [email protected] " subject => "Warning: %{title}" to => “[email protected]" via => "smtp" body => "%{message}" } } 45
  • 46. Kibana 安裝及設定 • wget https://download.elastic.co/kibana/kibana/kibana- 4.5.3-1.x86_64.rpm • yum localinstall kibana-4.5.3-1.x86_64.rpm • http://127.0.0.1:5601 46
  • 47. Kibana 設定檔說明 • kibana.yml 為 kibana 預設的主要設定檔案 • port : 預設 5601。 • Host : 定義 kibana IP address。 • elasticsearch_url :elasticsearch 的位置。 • elasticsearch_preserve_host :設定 true,瀏覽器請求中的主機名稱作為 Kibana 發送 給 Elasticsearch 時請求的主機名稱。 • kibana_index : 預設名字為.kibana ,kibana 連接了elasticsearch服務後,也會建立一個 index 來儲存在 kibana 操作及設定的資訊。 • default_app_id : 要讓使用者預設是打開何介面,有 discover, visualize, dashboard 和 settings 選擇,預設為 discover。 • request_timeout :等待 Kibana 後端的回應的逾時時間,單位 ms,預設300000 ms • shard_timeout :Elasticsearch 等待shard 回應的逾時時間;預設 0 表 disable 。 • verify_ssl :是否需要驗證 Elasticsearch SSL ,false 就是關閉,預設 true 開啟。 47
  • 48. Kibana 介紹- Discover 48 • 主要讓我們可以在介面上做資料查詢,進而篩選資料.
  • 49. Kibana 查詢語法(1) • 單一 term 查詢 Jordan, ELK • 指定欄位 field 查詢 field:value city:Taipei, name:Jordan • 萬用字元 ? 比對單一字元 如: Ela?ticsearch * 比對 0 到多個字元 如: E* • 範圍查詢 releasedate : [2016-08-01 TO 2016-08-31] releasedate : {2016-08-01 TO 2016-08-31} 49
  • 50. Kibana 查詢語法(2) • 邏輯操作 AND, OR firstname:H* AND age:20 firstname:H* OR age:20 • + :查詢結果中必須包含 , - :不能包含 +city:T* -birthday:1980-01-01 name:J* city 欄位結果中必須包含T開頭的,生日不能為1980-01-01,name 欄位 J 開頭 的有或沒有都可 • 分組 (city:T* OR gender:male) AND country:USA 先查詢city T 開頭或是性別為 male 的結果, 然後再與國家為 USA 的結合後查詢 50
  • 51. Kibana 查詢語法(3) • 欄位分組 firstname:(+J* -Jo*) 查詢 firstname 欄位 J 開頭的結果,並且排除 firstname 欄位 Jo 開頭的結果 • 特殊字元 (+ - && || ! () {} [] ^" ~ * ? : ) 查詢時若條件遇到上述特殊字元,需使用 進行跳脫處理. 51
  • 52. Kibana Discover 語法練習 • 請自行選擇感興趣的 Index 練習 52
  • 53. Kibana 介紹- Visualize 53 • 用來將篩選過的資料進行視覺化的處理。
  • 55. Kibana Visualize 練習 55 • 使用股票交易資料進行練習
  • 56. Kibana Dashboard 介紹 • 可以將已儲存的 Visualize 圖表放進一個數位看板內供檢視 56
  • 57. 參考資料 • video : Elasticsearch from the bottom up • book: Elasticsearch: The Definitive Guide • book: Learning ELK Stack • book: The Logstash book • book: Kibana Essentials • site: http://logz.io/blog/elasticsearch-cheat-sheet/ • site: How to Deploy the ELK Stack in Production 57
  • 58. Document models of Elasticsearch • Document 盡量以反正規化 (de-normalize) 形式儲存 • 同一個 Index 內可使用不同的 Type 對應不同的資料 • 同一個 Document 可以使用網狀 (Nested objects) 結構的方式 儲存 • Parent-child relationship – • 兩者可以使用不同的 Mapping • 1-many relationship 58
  • 61. Parent-child relationship example(cont.1) 61 • parent bulk indexing • child indexing
  • 62. Parent-child relationship example(cont.2) 62 • finding parent with their child

Editor's Notes

  • #5: https://elk-docker.readthedocs.io/#installation
  • #19: http://finance.yahoo.com/quote/GOOG/history?period1=1092844800&period2=1469635200&interval=1d&filter=history&frequency=1d https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/haproxy http://www.evanlin.com/using-logstash-elsticsearch-and-kibana/ https://github.com/Jheng-Yu-Chen/elk-training/blob/master/Practice.txt
  • #20: http://finance.yahoo.com/quote/GOOG/history?period1=1092844800&period2=1469635200&interval=1d&filter=history&frequency=1d https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/haproxy http://www.evanlin.com/using-logstash-elsticsearch-and-kibana/ https://github.com/Jheng-Yu-Chen/elk-training/blob/master/Practice.txt
  • #22: http://finance.yahoo.com/quote/GOOG/history?period1=1092844800&period2=1469635200&interval=1d&filter=history&frequency=1d https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/haproxy http://www.evanlin.com/using-logstash-elsticsearch-and-kibana/ https://github.com/Jheng-Yu-Chen/elk-training/blob/master/Practice.txt
  • #24: http://finance.yahoo.com/quote/GOOG/history?period1=1092844800&period2=1469635200&interval=1d&filter=history&frequency=1d https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/haproxy http://www.evanlin.com/using-logstash-elsticsearch-and-kibana/ https://github.com/Jheng-Yu-Chen/elk-training/blob/master/Practice.txt
  • #38: grok 線上測試工具練習解答: %{TIMESTAMP_ISO8601:timestamp} %{LOGLEVEL:log-level} \[%{DATA:class}\]:%{GREEDYDATA:message}
  • #50: Date:[2009-08-01 TO 2016-08-31]
  • #54: { "script" : "doc['volume'].value * 100"}
  • #58: http://logz.io/blog/deploy-elk-production/