1、寫在前面html
結合以前寫的一篇文章:Centos7 之安裝Logstash ELK stack 日誌管理系統,上篇文章主要講了監控軟件的做用以及部署方法。而這篇文章介紹的是單獨監控nginx 日誌分析再進行可視化圖形展現,並在用戶前端使用nginx 來代理kibana的請求響應,訪問權限方面暫時使用HTTP 基本認證加密用戶登陸。(關於elk權限控制,我所瞭解的還有一種方式-Shield),等之後有時間了去搞下。下面開始正文吧。。。前端
注意:環境默認和上一篇大體同樣,默認安裝好了E、L、K、3個軟件便可。固然了,還有必需的java環境JDKjava
開始以前,請容許我插入一張圖,來自線上個人測試圖:(若是有須要的童鞋,能夠私信我,我能夠把登陸賬號給你。。) node
備註:因爲阿里雲主機已經刪除,沒法提供試看了哈。ios
nginx日誌文件其中一行:nginx
218.75.177.193 - - [03/Sep/2016:03:34:06 +0800] "POST /newRelease/everyoneLearnAjax HTTP/1.1" 200 370 "http://www.xxxxx.com/"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" "36.22.6.130"
nginx 服務器日誌的log_format格式:git
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
2、配置logstashweb
1.修改配置文件,/etc/logstash/conf.d下。建立一個新的配置文件,內容以下:redis
[root@log-monitor ~]# cat /etc/logstash/conf.d/nginx_access.conf input { file { path => [ "/data/nginx-logs/access.log" ] start_position => "beginning" ignore_older => 0 } } filter { grok { match => { "message" => "%{NGINXACCESS}" } } geoip { source => "http_x_forwarded_for" target => "geoip" database => "/etc/logstash/GeoLiteCity.dat" add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ] add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}" ] } mutate { convert => [ "[geoip][coordinates]", "float" ] convert => [ "response","integer" ] convert => [ "bytes","integer" ] replace => { "type" => "nginx_access" } remove_field => "message" } date { match => [ "timestamp","dd/MMM/yyyy:HH:mm:ss Z"] } mutate { remove_field => "timestamp" } } output { elasticsearch { hosts => ["127.0.0.1:9200"] index => "logstash-nginx-access-%{+YYYY.MM.dd}" } stdout {codec => rubydebug} }
文件內容大體解釋:數據庫
Logstash 分爲 Input、Output、Filter、Codec 等多種plugins。
Input:數據的輸入源也支持多種插件,如elk官網的beats、file、graphite、http、kafka、redis、exec等等等、、、
Output:數據的輸出目的也支持多種插件,如本文的elasticsearch,固然這可能也是最經常使用的一種輸出。以及exec、stdout終端、graphite、http、zabbix、nagios、redmine等等、、、
Filter:使用過濾器根據日誌事件的特徵,對數據事件進行處理過濾後,在輸出。支持grok、date、geoip、mutate、ruby、json、kv、csv、checksum、dns、drop、xml等等、、
Codec:編碼插件,改變事件數據的表示方式,它能夠做爲對輸入或輸出運行該過濾。和其它產品結合,如rubydebug、graphite、fluent、nmap等等。
具體以上插件的細節能夠去官網,介紹的挺詳細的。下面說下該篇中的配置文件的含義:
來源:飛走不可-原文http://www.cnblogs.com/hanyifeng/p/5857875.html
input段:
file:使用file 做爲輸入源
path: 日誌的路徑,支持/var/log*.log,及[ "/var/log/messages", "/var/log/*.log" ] 格式
start_position: 從文件的開始讀取事件。另外還有end參數
ignore_older: 忽略早於24小時(默認值86400)的日誌,設爲0,即關閉該功能,以防止文件中的事件因爲是早期的被logstash所忽略。
filter段:
grok:數據結構化轉換工具
match:匹配條件格式,將nginx日誌做爲message變量,並應用grok條件NGINXACCESS進行轉換
geoip:該過濾器從geoip中匹配ip字段,顯示該ip的地理位置
source:ip來源字段,這裏咱們選擇的是日誌文件中的最後一個字段,若是你的是默認的nginx日誌,選擇第一個字段便可(注:這裏寫的字段是/opt/logstash/patterns/nginx 裏面定義轉換後的)
target:指定插入的logstash字斷目標存儲爲geoip
database:geoip數據庫的存放路徑
add_field: 增長的字段,座標經度
add_field: 增長的字段,座標緯度
mutate: 數據的修改、刪除、類型轉換
convert: 將座標轉爲float類型
convert: http的響應代碼字段轉換成 int
convert: http的傳輸字節轉換成int
replace: 替換一個字段
remove_field: 移除message 的內容,由於數據已通過濾了一份,這裏沒必要在用到該字段了。否則會至關於存兩份
date: 時間處理,該插件很實用,主要是用你日誌文件中事件的事件來對timestamp進行轉換,導入老的數據必備!在這裏曾讓我困惑了好久哦。別再掉坑了
match:匹配到timestamp字段後,修改格式爲dd/MMM/yyyy:HH:mm:ss Z
mutate:數據修改
remove_field: 移除timestamp字段。
output段:
elasticsearch:輸出到es中
host: es的主機ip+端口或者es 的FQDN+端口
index: 爲日誌建立索引logstash-nginx-access-*,這裏也就是kibana那裏添加索引時的名稱
2.建立logstash配置文件以後,咱們還要去創建grok使用的表達式,由於logstash 的配置文件裏定義的使用轉換格式語法,先去logstash的安裝目錄,默認安裝位置:/opt/logstash/下,在該位置建立一個目錄patterns:
root@log-monitor ~]# mkdir -pv /opt/logstash/patterns mkdir: created directory ‘/opt/logstash/patterns’
在該目錄下建立格式文件,內容以下:
[root@log-monitor ~]# cat /opt/logstash/patterns/nginx 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} \"%{IPV4:http_x_forwarded_for}\"
注:該格式的最後有一個http_x_forwarded_for,由於咱們日誌是啓用了cdn代理的。日誌的第一段都是cdn的,最後一段纔是真正客戶的ip。
須要分析的nginx日誌路徑不在默認的位置,因此我根據logstash 的配置,建個目錄先,並將日誌文件拷貝進去:
[root@log-monitor ~]# mkdir -pv /data/nginx-logs/ [root@log-monitor ~]# ll /data/nginx-logs/ total 123476 -rw-r--r-- 1 nginx adm 126430102 Sep 9 16:02 access.log
3.而後就是logstash中配置的GeoIP的數據庫解析ip了,這裏是用了開源的ip數據源,用來分析客戶端的ip歸屬地。官網在這裏:MAXMIND
先把庫下載到本地:
[root@log-monitor ~]# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
解壓到當前路徑,並將它移動到上述咱們配置的路徑下,固然其它路徑也是能夠的,不過logstash 的配置文件也須要更改,以下:
[root@log-monitor ~]# gzip -d GeoLiteCity.dat.gz [root@log-monitor ~]# mv GeoLiteCity.dat /etc/logstash/.
測試下logstash 的配置文件吧,使用它自帶的命令去測試,以下:
[root@log-monitor ~]# /opt/logstash/bin/logstash -t -f /etc/logstash/conf.d/nginx_access.conf
Configuration OK
注:-t -f 參數順序不能亂,格式就是定死的,-f 後面要跟配置文件;還有就是該測試只能測試語法,標點符號。若是邏輯上有錯誤的話,仍是能啓動的。這裏就須要在正式啓動運行時,多關注日誌文件,位置:/var/log/logstash/logstash.log
3、配置Elasticsearch
1.先修改es的配置文件以下(存放路徑:/etc/elasticsearch/elasticsearch.yml):
[root@log-monitor ~]# egrep -v '^#|^$' /etc/elasticsearch/elasticsearch.yml node.name: es-1 path.data: /data/elasticsearch/ network.host: 127.0.0.1 http.port: 9200
其它內容都保持默認。主要修改了es的數據存放路徑,它默認的路徑在根目錄下,因爲容量過小,而/data容量大。 根據你的實際狀況考慮而定。
建立數據存放目錄:
[root@log-monitor ~]# mkdir -pv /data/elasticsearch
修改該文件的權限所屬者:
[root@log-monitor ~]# chown -R elasticsearch.elasticsearch /data/elasticsearch/
以後重啓es,重啓logstash。
[root@log-monitor ~]# systemctl restart elasticsearch
[root@log-monitor ~]# systemctl restart logstash
檢查啓動狀態:
[root@log-monitor ~]# netstat -ulntp | grep java tcp6 0 0 127.0.0.1:9200 :::* LISTEN 25988/java tcp6 0 0 127.0.0.1:9300 :::* LISTEN 25988/java
[root@log-monitor ~]# systemctl status logstash ● logstash.service - LSB: Starts Logstash as a daemon. Loaded: loaded (/etc/rc.d/init.d/logstash) Active: active (running) since Fri 2016-09-09 16:14:17 CST; 38s ago Docs: man:systemd-sysv-generator(8) Process: 27195 ExecStart=/etc/rc.d/init.d/logstash start (code=exited, status=0/SUCCESS) CGroup: /system.slice/logstash.service └─27201 /bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX... Sep 09 16:14:17 log-monitor systemd[1]: Starting LSB: Starts Logstash as a daemon.... Sep 09 16:14:17 log-monitor logstash[27195]: logstash started. Sep 09 16:14:17 log-monitor systemd[1]: Started LSB: Starts Logstash as a daemon..
logstash 的日誌查看:
[root@log-monitor ~]# tail -f /var/log/logstash/logstash.log {:timestamp=>"2016-09-09T16:14:26.732000+0800", :message=>"Pipeline main started"}
從上面能夠看到啓動是正常的,咱們在去看下es裏的索引,應該已經在倒入數據了。
[root@log-monitor ~]# curl 'localhost:9200/_cat/indices?v' health status index pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana 1 1 1 0 3.1kb 3.1kb yellow open logstash-nginx-access-2016.09.08 5 1 69893 0 24.2mb 24.2mb yellow open logstash-nginx-access-2016.09.09 5 1 339 0 273.8kb 273.8kb
從上面看到數據已經在慢慢的導入了。大概須要一段時間,由於涉及到日誌的過濾寫入等。不過也很快啦。咱們暫時不去配置kibana。先去安裝nginx作個代理。
4、安裝nginx 配置kibana代理
1.下載穩定版的nginx,這裏使用yum安裝。或者也能夠選擇編譯,我的以爲rpm包已經足夠可使用。
[root@log-monitor ~]# wget https://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.10.0-1.el7.ngx.x86_64.rpm
2.安裝,並修改默認的配置文件
[root@log-monitor ~]# yum localinstall nginx-1.10.0-1.el7.ngx.x86_64.rpm –y
先將默認的default.conf 移動到其它目錄中,或者直接刪除也能夠。我是直接刪除了。而後新建一個elk.conf配置文件,內容以下:
[root@log-monitor ~]# cat /etc/nginx/conf.d/elk.conf upstream elk { ip_hash; server 172.17.0.1:5601 max_fails=3 fail_timeout=30s; server 172.17.0.1:5601 max_fails=3 fail_timeout=30s; } server { listen 80; server_name localhost; server_tokens off; #close slow conn client_body_timeout 5s; client_header_timeout 5s; location / { proxy_pass http://elk/; index index.html index.htm; #auth auth_basic "ELK Private,Don't try GJ!"; auth_basic_user_file /etc/nginx/.htpasswd; } }
文件內容大體解釋:
此處省略500字
3.新建一個http基本認證用戶,使用的是httpd的一個工具組件,叫httpd-tools,用於生成加密的用戶數據庫
[root@log-monitor ~]# yum install httpd-tools –y
新建用戶:
[root@log-monitor ~]# htpasswd -cm /etc/nginx/.htpasswd elk New password: Re-type new password: Adding password for user elk
重啓nginx,並檢查狀態
[root@log-monitor ~]# systemctl start nginx [root@log-monitor ~]# systemctl status nginx ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2016-09-09 12:02:41 CST; 47s ago Docs: http://nginx.org/en/docs/ Process: 26422 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Process: 26420 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 26424 (nginx) CGroup: /system.slice/nginx.service ├─26424 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf └─26425 nginx: worker process Sep 09 12:02:41 log-monitor systemd[1]: Starting nginx - high performance web server... Sep 09 12:02:41 log-monitor nginx[26420]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Sep 09 12:02:41 log-monitor nginx[26420]: nginx: configuration file /etc/nginx/nginx.conf test is successful Sep 09 12:02:41 log-monitor systemd[1]: Started nginx - high performance web server. Sep 09 12:03:13 log-monitor systemd[1]: Started nginx - high performance web server. Sep 09 12:03:26 log-monitor systemd[1]: Started nginx - high performance web server.
檢查監聽端口:
[root@log-monitor ~]# netstat -ultpn | grep :8888 tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 26424/nginx: master
修改iptables防火牆,插入如下規則,容許外面訪問8888端口。因爲咱們最終是使用8888端口對外提供服務的,因此kibana的5601,以及es的9200、9300端口都不須要對外
[root@log-monitor ~]# iptables -I INPUT -p tcp -m state --state NEW --dport 8888 -j ACCEPT
4.訪問一下網站,驗證下:
來源:飛走不可-原文http://www.cnblogs.com/hanyifeng/p/5857875.html
輸入咱們創建的elk用戶,登錄後,能夠正常的訪問kibana界面便可,以下圖:
添加一個索引,這個索引名字就是咱們以前在logstash配置文件中導入es中的那個,本文中是logstash-nginx-access-*,以下圖:
查看索引,目前自由一個,設置爲加星,便是discover默認突出顯示的。
而後咱們點擊Discover,便可看到咱們倒入的數據了。以下圖:
來源:飛走不可-原文http://www.cnblogs.com/hanyifeng/p/5857875.html
最後這是個人dashboard,主要統計了web站點的客戶端ip地址歸屬地、總的http傳輸次數、top10 來源ip、top10 請求點擊頁面、錯誤請求趨勢、等等,以下,上幾張圖:
5、小結
ELK優點:
搭建的過程當中真的蠻辛苦的(畢竟都是英文),出了問題只能google,從不瞭解到熟悉,也算是種經歷啦。不發牢騷了。。
畫圖容易,就如虎大牛所說:「先學會了如何查,畫圖天然而然就簡單多了。固然還要知道其中每一個字段的含義」。個人下篇文章將會主要說下如何畫圖(包括上面這些圖中樣式哈)。有沒有點小福利的感受?
好啦,睡覺去啦,你們週末玩的happy呀!另外提早祝天下全部美麗可愛善良的教師們,節日快樂。Y(^_^)Y
該文章屬於原創,圖片均來自真實環境,如您感受文章還不錯,能夠轉載,但前提必定要註明出處以及來源網址,以下樣式:謝謝您的理解
來源:飛走不可-原文http://www.cnblogs.com/hanyifeng/p/5857875.html
參考資料:
https://www.elastic.co/guide/index.html
http://grokdebug.herokuapp.com/patterns
http://www.cnblogs.com/liuning8023/p/5502460.html
https://segmentfault.com/a/1190000000489528