Notice
Recent Posts
Recent Comments
Link
ยซ   2024/10   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

R136A1

[inflearn] ELK ์Šคํƒ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ถ„์„ - ์„น์…˜5. ์‹ค๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ณธ๋ฌธ

PROJECT/๐Ÿ’™ํ•ญ๋งŒ

[inflearn] ELK ์Šคํƒ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ถ„์„ - ์„น์…˜5. ์‹ค๋ฐ์ดํ„ฐ ๋ถ„์„

r136a1x27 2022. 7. 23. 23:34

1. ์‹ค์ „ ์ธ๊ตฌ๋ถ„์„

logstash

https://catalog.data.gov/dataset ์—์„œ population by country ์ณค๋”๋‹ˆ ์•ˆ๋‚˜์˜ด

https://data.world/doe/population-bycountry-1980-2010 ์—์„œ ๋Œ€์‹  ๋‹ค์šด๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค

๋˜๋Š”... ๊ฐ•์˜์ž Github ch06 ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์— csv ํŒŒ์ผ ์žˆ์Œ(NIA, - ์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์‚ญ์ œ)

ElasticSearch, Kibana๊ฐ€ ์ž˜ ๋Œ์•„๊ฐ€๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

ps -ef | grep kibana

ps -ef | grep elasticsearch

 

cd /usr/share/logstash/bin/

 

logstash.conf

input {
  file {
    path => "csv ํŒŒ์ผ์ด ์žˆ๋Š” ์ ˆ๋Œ€๊ฒฝ๋กœ"
    start_position => "beginning" 
    sincedb_path => "/dev/null"  
  }
}

filter {
  csv {
      separator => ","
      columns => ["Country","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010"]
  }
  mutate {convert => ["1980", "float"]}
  mutate {convert => ["1981", "float"]}
  mutate {convert => ["1982", "float"]}
...
  mutate {convert => ["2009", "float"]}
  mutate {convert => ["2010", "float"]}
}

output {  
    elasticsearch {
        hosts => "localhost"
        index => "population"
    }
    stdout {}
}

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html#plugins-inputs-file-start_position

start_position => ์ตœ์ดˆ์— ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์ด๋Š” ์œ„์น˜

"beginning" ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฝ์–ด๋“ค์ธ๋‹ค๋Š” ์„ค์ • (ํŒŒ์ผ์„ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ๋กœ ์„ค์ •)

"end" ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฝ์€ ์œ„์น˜๋ถ€ํ„ฐ

(streaming data์˜ ๊ฒฝ์šฐ ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์™ธ, ๊ฐฑ์‹ ๋œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋งŒ์„ ๋ฐ›์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ)

 

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html#plugins-inputs-file-sincedb_path 

sincedb_path => ํŒŒ์ผ์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, offset์„ ์ €์žฅํ•˜๋Š” ์šฉ๋„

logstash ์žฌ์‹œ์ž‘ํ•˜๋ฉด start_position ์ƒ๊ด€ ์—†์ด ํ•ด๋‹น offset์—์„œ ์‹œ์ž‘

 

์ด๊ฑฐ ์ง€์ • ์•ˆํ•ด์ฃผ๋ฉด ์ฒซ ๋ฒˆ์งธ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋“ค์–ด๊ฐ€๋Š”๋ฐ

๋‘ ๋ฒˆ์งธ ์‹ค์Šตํ•  ๋–„...ํ•œ ๋ฒˆ ๋“ค์–ด๊ฐ„ data๋Š” logstash๊ฐ€ ๋„ฃ์ง€ ์•Š์Œ

 

์ฐธ๊ณ ) https://koocci-dev.tistory.com/20

 

์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์œ„ํ•ด text์ธ ์ž๋ฃŒํ˜•์„ ์ž„์˜๋กœ float์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์ž…๋ ฅํ•˜๋„๋ก

๋กœ์ปฌ ElasticSearch์— ๋ฐ”๋กœ ๋„ฃ๋Š”๊ฑฐ๋‹ˆ๊นŒ hosts๋Š” localhost๋กœ ์ถฉ๋ถ„

index๋ช…์€ population

 

----------

ํ˜„์žฌ์œ„์น˜: /usr/share/logstash/bin/

sudo  /usr/share/logstash/bin/logstash -f {logstash.conf ์ ˆ๋Œ€ ๊ฒฝ๋กœ}

์‚ฌ์‹ค logstash๋Š” ./logstash์™€ ๊ฐ™์ด ์ƒ๋Œ€ ๊ฒฝ๋กœ๋„ ์ƒ๊ด€ ์—†๋‹ค

์ด๊ฒŒ ์ž˜...๋“ค์–ด๊ฐ„๊ฑด๊ฐ€?

[INFO ] 2022-07-23 04:17:02.200 [Agent thread] configpathloader - No config files found in path 
{:path=>"/usr/share/logstash/logstash.conf"}
[ERROR] 2022-07-23 04:17:02.205 [Agent thread] sourceloader - No configuration found in the configured sources.

์•„๋ฌด๋ž˜๋„ ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค

 

sudo ./logstash -f logstash.conf๋กœ ํ–ˆ๋”๋‹ˆ ์˜ค๋ฅ˜

 

๋‚ด ์œ„์น˜๋Š” /usr/share/logstash/bin ์ธ๋ฐ /usr/share/logstash๋กœ ๋œจ๋Š” ๊ฑธ ๋ณด๋ฉด ./logstash๊ฐ€ ์ €๊ธฐ์„œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค

์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ๋„ฃ์–ด๋ณด์ž...

 

sudo ./logstash -f /usr/share/logstash/bin/logstash.conf

 

์ •์ƒ์ ์œผ๋กœ ์ž…๋ ฅ๋˜๋ฉด ์ด์˜๊ฒŒ ๋œธ

 

kibana

ํ™•์ธํ•ด๋ณด์ž. localhost:5601

์ธ๋ฑ์Šค ํŒจํ„ด ๋งค์นญํ•˜๊ธฐ

http://localhost:5601/app/management/kibana/indexPatterns

์ž˜ ๋œฌ๋‹ค!

๋””์Šค์ปค๋ฒ„ Discover

๋“ค์–ด๊ฐ€๋ณด์ž

http://localhost:5601/app/discover 

๋ฐ์ดํ„ฐ ํ•˜๋‚˜ ๋ˆŒ๋Ÿฌ์„œ Country, 1980, 2010๋งŒ column์— ๋œจ๊ฒŒํ•˜๊ธฐ

search์— korea ์ณ์„œ ํ™•์ธ ๊ฐ€๋Šฅ

์‹œ๊ฐํ™” visualize

y์ถ•์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ๊ด€์ฐฐํ•ด๋ณด๊ธฐ

 

ํ•œ๋‚ฑ csv์— ๋ถˆ๊ณผํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๊ฐํ™”ํ•˜์—ฌ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด Kibana์˜ ํž˜

 

2. ์‹ค์ „ ์ฃผ์‹ ๋ถ„์„

http://blog.webkid.io/visualize-datasets-with-elk ์—์„œ ์•„์ด๋””์–ด๋ฅผ ๋”ฐ์™”๋‹ค๊ณ  ๋ง์”€

finance YAHOO์—์„œ ํŠน์ • ์ข…๋ชฉ์˜ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

Facebook(=Meta) 5๋…„ ๋‹ค์šด

ps -ef | grep kibana

ps -ef | grep elasticsearch

์‚ฌ์‹ค wgetํ•˜๋Š” ๊ฒฝ๋กœ๋Š” ๋”ฑํžˆ ์ƒ๊ด€ ์—†๋‹ค

wget https://raw.githubusercontent.com/minsuk-heo/BigData/master/ch06/logstash_stock.conf 

vim์„ ํ†ตํ•ด์„œ path ์ •๋„๋งŒ ๋ฐ”๊ฟ”์ฃผ๊ธฐ

input {
  file {
    path => "csv์ ˆ๋Œ€๊ฒฝ๋กœ"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}
filter {
  csv {
      separator => ","
      columns => ["Date","Open","High","Low","Close","Volume","Adj Close"]
  }
  mutate {convert => ["Open", "float"]}
  mutate {convert => ["High", "float"]}
  mutate {convert => ["Low", "float"]}
  mutate {convert => ["Close", "float"]}
}
output {
    elasticsearch {
        hosts => "localhost"
        index => "stock"
    }
    stdout {}
}

cd /usr/share/logstash/bin/

sudo ./logstash -f /usr/share/logstash/bin/logstash_stock.conf

 

[FATAL] 2022-07-23 05:06:28.811 [LogStash::Runner] runner - Logstash could not be started because there is already another instance using the configured data directory.  If you wish to run multiple instances, you must change the "path.data" setting.

 

์„œ๋ฒ„ ํ™˜๊ฒฝ์—์„œ๋Š” ํ•จ๋ถ€๋กœ ์„œ๋ฒ„๋ฅผ ๋„๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์—

โ‘ 

systemctl status logstash
systemctl stop logstash
systemctl restart logstash

๋กœ logstash๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ์žฌ์‹œ์ž‘ํ•˜๋„๋ก ํ•˜์ž

โ‘ก

ps -ef | grep logstash

kill -9 [PID]

 

=> ์ด๊ฑฐ ์ž˜ ์•ˆ๋จ 

์˜ค๋ฅ˜ ๊ทธ๋Œ€๋กœ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ "Logstash ์—ฌ๋Ÿฌ๊ฐœ ์‹คํ–‰"์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋ฉด ์—ฌ๋Ÿฌ๊ฐœ ๋‚˜์˜ค๋Š”๋ฐ ์ด๊ฑฐ ๋”ฐ๋ผํ•˜๋ฉด ๋œ๋‹ค

https://se-you.tistory.com/1 

๋‚˜๋Š” ์‰ฝ๊ฒŒ์‰ฝ๊ฒŒ..๊ทธ๋ƒฅ ์žฌ์‹œ์ž‘ ํ•˜๊ธฐ๋กœ

 

๋งŒ์•ฝ ๊ทธ๋ƒฅ ์žฌ์‹œ์ž‘ํ•˜๊ณ  curl http://127.0.0.1:9200 ํ•ด์„œ ์‘๋‹ต์ด ์—†๋‹ค๋ฉด...

elasticsearch๋ž‘ kibana๋„ ์žฌ์‹œ์ž‘ํ•ด์ค˜์•ผ๋จ

[INFO ] 2022-07-23 05:18:56.503 [Ruby-0-Thread-9: :1] elasticsearch - Failed to perform request {:message=>"Connect to localhost:9200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)", :exception=>Manticore::SocketException, :cause=>org.apache.http.conn.HttpHostConnectException: Connect to localhost:9200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)}
[WARN ] 2022-07-23 05:18:56.503 [Ruby-0-Thread-9: :1] elasticsearch - Attempted to resurrect connection to dead ES instance, but got an error {:url=>"http://localhost:9200/", :exception=>LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError, :message=>"Elasticsearch Unreachable: [http://localhost:9200/][Manticore::SocketException] Connect to localhost:9200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)"}
----------
su

systemctl stop elasticsearch.service
systemctl start elasticsearch.service
systemctl enable elasticsearch.service

systemctl stop kibana
systemctl start kibana
systemctl enable kibana

๋‹ค์‹œ ์ปค๋งจ๋“œ ์ž…๋ ฅํ•˜๋ฉด ์ด์˜๊ฒŒ ๋“ค์–ด๊ฐ

Stack Management - Kibana - Index Patterns์—์„œ stock Index ๋“ฑ๋กํ•˜๊ธฐ

    - ์ด ๋•Œ, Timestamp๋ฅผ Date๋กœ ์ง€์ •ํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ ์•ˆ๋จ

Analytics - Dicover ์—์„œ ๋ฐ์ดํ„ฐ ์Œฉ์œผ๋กœ ๊ด€์ฐฐํ•ด๋ณด๊ธฐ (column: Date, Open, Close)

    - Date ์ˆœ์œผ๋กœ ์ •๋ ฌ์€ ๋ชปํ•˜๋‚˜..? ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์‹œ๊ฐ„์ธ TimeStamp๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋Š”๋ฐ...

    - stock_toggled๋กœ ์ €์žฅ

Analytics - Visualized Library - Create Visualization

y์ถ•: Average, Close

x์ถ•: Date ํ•˜๋ ค๋Š”๋ฐ ๊ฐ•์˜์—์„œ๋Š” Date Histogram ์•„๋ž˜ Date ํ•„๋“œ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ ์‹ค์Šต์—์„œ๋Š” ์•ˆ๋จ

์›์ธ์„ ์ฐพ์ž๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ๋•Œ filter์—์„œ Date๋ฅผ ๋”ฐ๋กœ ์ง€์ •์„ ์•ˆํ•ด์ค˜์„œ text๋กœ ๋“ค์–ด๊ฐ„ ๊ฒƒ ๊ฐ™์Œ

๊ทธ๋Ÿฌ๋‚˜ ๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด convert์—๋Š” int, float, string, boolean๊ณผ ๊ฐ™์€๊ฒƒ๋ฐ–์— ์—†์Œ

https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-convert 

https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html 

์ด๊ฑธ ์“ฐ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ?

filter {
  date {
    match => [ "์นผ๋Ÿผ๋ช…", "๋ฐ์ดํ„ฐ์˜ ๋‚ ์งœ ์–‘์‹" ]
    timezone => "Asia/Seoul"
    locale => "ko"
  }
}

filter {
  date {
    match => [ "Date", "yyyy-MM-dd" ]
    timezone => "Asia/Seoul"
    locale => "ko"
  }
}

 

๋งคํ•‘ ํ™•์ธํ•ด๋ณด๊ธฐ curl -XGET http://localhost:9200/stock2/?pretty 

(๋Œ๊ฒ ๋‹ค)

์—ฌ์ „ํžˆ Date๊ฐ€ text, ๋Œ€์‹  @timestamp๊ฐ€ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์‹œ๊ฐ„์—์„œ Date๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

๊ทผ๋ฐ Kibana์—์„œ ํ™•์ธํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ํ•œ ๊ฐœ๋ฐ–์— ์—†์Œ

 

๋ชป๊ณ ์น˜๊ฒ ๋‹ค~

Comments