這邊希望達到的目標: 每次有人使用 server service 時,log 紀錄便會依據 logstash 的 filter 規則處理過後存進 elasticsearch ,然後使用者能用 kibana 來看到分析該些 log 資料的視覺圖
上述流程如下圖所示
其它套件:
kopf: 用來觀看 elasticsearch server 的各種狀態以及測試搜尋等功能
web service: flask(web application framework) + nginx(web server)
- get app code
you can use any service code you want
here I use our own service code
first git clone position-engine.git from git
can see app.py (main service file) & .git
need to run service with flask
- install flask use virtualenv
create virtualenv "flask_env" with python3
- virtualenv -p python3 flask_env
switch environment to flask_env
- . flask_env/bin/activate
if you can see the environment name in front of the user name, means you success activate the environment
in flask_env, install Flask
- pip install Flask
this is for position-engine
- pip install requests
in virtualenv remember do not use sudo command to install package
it's will install packages as root which means install packages out of virtualenv
- run flask service
when install finish, use python command can run the service
- python app.py
![](https://hackpad-attachments.imgix.net/hackpad.com_09SKCwQ0sLp_p.377820_1434641343976_0.0.png?fit=max&w=882)
if you see the info like the pic, you success
- nginx
but now we don't have any web service for outside user to acccess
so we need to setup nginx for service
install nginx
- sudo apt-get install nginx
In Ubuntu 14.04, by default, Nginx automatically starts when it is installed.
and now you access your IP address can see the welcome page
![](https://hackpad-attachments.imgix.net/hackpad.com_09SKCwQ0sLp_p.377820_1434638576098_00.png?fit=max&w=882)
nginx service command
start service
- sudo service nginx start
stop service
- sudo service nginx stop
restart service
- sudo service nginx restart
make sure web server will restart automatically when the server is rebooted by typing:
- sudo update-rc.d nginx defaults
setting nginx configuration
now we want to manage the request which service should process
in this case we need use port 8120 to access the flask service
- copy one new file in /etc/nginx/sites-available/
- sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example
- sudo vim /etc/nginx/sites-available/example
add
location /position-service {
proxy_pass the url your want to access;
location /position-service {
proxy_pass the url your want to access;
}
when finish modify, set link to /etc/nginx/sites-enable/ dir
- sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example
delete default link(sites-enable/default) or comment the code in this file(sites-available/default)
- sudo rm /etc/nginx/sites-enable/default
you can use -s reload command to reload your setting without stop the service
- sudo nginx -s reload
now you can access the service use this url:
http://IP address/position-service
proxy module reference: http://nginx.org/en/docs/http/ngx_http_proxy_module.html
- Elasticsearch
照著上面的連結設定好後,理論上可以從設好的位置看到 kopf 的頁面
上面那條 header 應該會是綠色的,但因為我的副本數只有 1,在這樣的情況下基於安全考量 kopf 會提醒你這是不健康的狀態,只要把副本數往上調即可變回綠色的狀態(2016-01-27 update)
- Logstash
這邊安裝的是 Logstash 1.5.1 版
下載/安裝
- sudo dpkg -i logstash_1.5.1-1_all.deb
啟動
- sudo service logstash start
輸入這個指令就能讓 logstash 的服務啟動
這時有兩種解法
1. 在 /etc/logstash/conf.d/ 目錄底下新增 logstash.conf 的檔案,然後啟動服務
sudo service logstash start
2. 使用 -f 指令去運行指定的 logstash.conf 檔案
/opt/logstash/bin/logstash -f logstash.conf
而 logstash.conf 撰寫的部分在下面的設定 config 檔章節會在進行說明
logstash service 的指令其實是一隻 script,這隻 script 會根據你後面代的參數去 /etc/init.d/ 資料夾裡找相對應的服務
如果你是第一次啟動的話,理論上來說去 log 檔裡應該會看到找不到 config files 的這則訊息
這時有兩種解法
1. 在 /etc/logstash/conf.d/ 目錄底下新增 logstash.conf 的檔案,然後啟動服務
sudo service logstash start
2. 使用 -f 指令去運行指定的 logstash.conf 檔案
/opt/logstash/bin/logstash -f logstash.conf
而 logstash.conf 撰寫的部分在下面的設定 config 檔章節會在進行說明
logstash service 的指令其實是一隻 script,這隻 script 會根據你後面代的參數去 /etc/init.d/ 資料夾裡找相對應的服務
這個 service 的指令其實是一隻 script,這隻 script 會根據你後面代的參數去 /etc/init.d/ 資料夾裡找相對應的服務
也就是說其實你可以去 /etc/init.d/logstash 檔案裡看更多啟動的相關資訊
一般比較需要知道的有
- log 存放位置: /var/log/logstash/
- config 檔案存放位置: /etc/logstash/conf.d/
- 主程式存放位置: /opt/logstash/bin/logstash/
P.S. 這邊是用 deb 檔安裝所以預設位置都設好了,如果是 git 下載檔案運行的話,直接在下載的 logstash 資料夾裡下 bin/logstash 指令即可
設定 config 檔
logstash 的 config 檔可以用 json 格式來撰寫
主要分成 input/filter/output 三個區塊
- input
直接拿 nginx 的 access.log 來使用,logstash 有支援直接讀取檔案的功能
- path: 讀取檔案位置
- type: 存進 elasticsearch 後這會變成其中一個辨識欄位
- start_position: 要從何處開始讀取資料 "begging" or "end",預設是 "end"
logstash 讀取檔案的機制是每當檔案有變動時就會啟動
- filter
可以用 grok debugger 來看寫的規則是否能正確 filter 出資訊
grok debugger: https://grokdebug.herokuapp.com/
grok debugger 上手教學: https://www.youtube.com/watch?v=YIKm6WUgFTY
access log:
%{IP:client_ip} - (?<client_user>[a-zA-Z\.\@\-\+_%]+) \[%{HTTPDATE:access_time}\] "%{WORD:method} (?<request_url>%{URIPATHPARAM}|%{URI}) HTTP/%{NUMBER:http_version}" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?<from>%{QS}) %{QS:other}
error log:
[
(?<access_time>\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[%{DATA:severity}\] (%{NUMBER:pid}#%{NUMBER}: \*%{NUMBER}|\*%{NUMBER}) %{DATA:err_message}(?:, client: (?<client_ip>%{IP}|%{HOSTNAME}))(?:, server: (?<server_ip>%{IPORHOST}|%{QS}))(?:, request: %{QS:request})?(?:, host: %{QS:host_info})
,
(?<access_time>\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[%{DATA:severity}\] (%{NUMBER:pid}#%{NUMBER}: \*%{NUMBER}|\*%{NUMBER}) %{DATA:err_message}(?:, client: (?<client_ip>%{IP}|%{HOSTNAME}))(?:, server: (?<server_ip>%{IPORHOST}|%{QS}))(?:, request: %{QS:request})?(?:, upstream: %{QS:upstream})?(?:, host: "(?<host_info>%{IPORHOST}|(?<host_ip>%{IP}):%{POSINT:host_port})")
]
- output
output 這部分是有用到 elasticsearch 這個 plugin
- host: 資料要存放的地方,這邊因為前面設定 elasticsearch 的時候有設定過 hosts,所以直接輸入 name 就 OK 了,不然就要輸入 IP:port,這邊要注意的是要使用的 port 是 9300,不是 9200
設定完後可以用這個測試 config 檔案格式是否正確
/opt/logstash/bin/logstash agent -f logstash.conf --configtest
config reference
- Kibana
log 分析視覺化頁面
這邊安裝的是 4.1.0 版,linux 64-bit 版本,直接下載官網的壓縮檔案
下載/安裝
- sudo tar zcvf kibana-4.1.0-linux-x64.tar.gz
要使用 Kibana 的話,很多人是建議要把 ruby 環境建置好...
- sudo apt-get install ruby
- sudo apt-get install rubygems-integration
- sudo gem install bundler
但我遇到的問題是沒有 "jade" 這個 module
所以我安裝了 npm 系列去安裝 "jade" 這個 module
- sudo apt-get install npm
- sudo npm install jade
先進入解壓縮完的資料夾,輸入
- bin/kibana
就能成功啟動 kibana 服務
nginx setting
一開始 kibana 會讓你選擇 index,如果前面的 log 資料有順利存進 elasticsearch 的話,就能直接選到,接著就能進入 kibana 的分析頁面了
nginx setting
![](https://hackpad-attachments.imgix.net/hackpad.com_09SKCwQ0sLp_p.377820_1439377402536_001.png?fit=max&w=882)
一開始 kibana 會讓你選擇 index,如果前面的 log 資料有順利存進 elasticsearch 的話,就能直接選到,接著就能進入 kibana 的分析頁面了
沒有留言:
張貼留言