一、ELK平臺(tái)搭建準(zhǔn)備
1.1 平臺(tái)環(huán)境:
OS:CentOS release 6.4(Final)
ElasticSearch:6.3.2
Logstash:6.3.2
Kibana:6.3.2
JRE:1.8
注:由于Logstash的運(yùn)行依賴于Java環(huán)境, 而Logstash 1.5以上版本不低于java 1.7,因此推薦使用最新版本的Java。因?yàn)槲覀冎恍枰狫ava的運(yùn)行環(huán)境,所以可以只安裝JRE,不過這里我依然使用JDK
1.2 ELK下載:https://www.elastic.co/downloads/
cd /data/package/ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.tar.gz wget https://artifacts.elastic.co/downloads/logstash/logstash-6.3.2.tar.gz wget https://artifacts.elastic.co/downloads/kibana/kibana-6.3.2-linux-x86_64.tar.gz
1.3 安裝準(zhǔn)備
#配置iptables,保證內(nèi)網(wǎng)之間可以互通 [root@Elk_Server]# iptables -F;iptables -I INPUT -s 192.168.0.0/16 -j ACCEPT [root@Elk_Server]# service iptables save;service iptables restart #關(guān)閉selinux [root@Elk_Server]# setenforce 0 [root@Elk_Server]# vim /etc/sysconfig/selinux SELINUX=disabled
1.4 時(shí)間同步
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime yum install ntpdate -y;ntpdate time.windows.com echo '01 00 * * * ntpdate time.windows.com' >>/etc/crontab
1.5 配置yum源
#導(dǎo)入公鑰 [root@Elk_Server ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch [root@Elk_Server ~]# vim /etc/yum.repos.d/elasticsearch.repo [elasticsearch-6.x] name=Elasticsearch repository for 6.x packages baseurl=https://artifacts.elastic.co/packages/6.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
1.6 安裝JAVA 1.8.0_151, 請(qǐng)見 http://www.sfodin.cn/Linux/2019-08/159733.htm
二、安裝Elasticsearch單機(jī)版集群
這個(gè)其實(shí)也是ELK中的核心,啟動(dòng)的時(shí)候一定要注意,因?yàn)閑s不可以進(jìn)行root賬戶啟動(dòng),所以你還需要開啟一個(gè)elsearch賬戶。**
2.1.1 yum方式安裝
[root@Elk_Server ~]# sudo yum install elasticsearch -y
2.1.2 源碼安裝(本文為編譯安裝配置)
#將解壓后的文件復(fù)制三份 tar zxvf elasticsearch-6.3.2.tar.gz -C /usr/local/ mv /usr/local/elasticsearch-6.3.2 /usr/local/es-master cp -rf /usr/local/es-master /usr/local/es-data1 cp -rf /usr/local/es-master /usr/local/es-data2 groupadd elasticsearch useradd elasticsearch -g elasticsearch
2.2 創(chuàng)建elasticsearch數(shù)據(jù)目錄
mkdir -p /data{,1,2}/elasticsearch/data mkdir -p /data{,1,2}/elasticsearch/logs chown -R elasticsearch:elasticsearch /data{,1,2}/elasticsearch
2.3 編輯Elasticsearch集群配置文件
cp /usr/local/es-master/config/elasticsearch.yml /usr/local/es-master/config/elasticsearch.yml-bak #創(chuàng)建軟連接 mkdir /etc/elasticsearch/ ln -s /usr/local/es-master/config/elasticsearch.yml /etc/elasticsearch/es-master.yml ln -s /usr/local/es-data1/config/elasticsearch.yml /etc/elasticsearch/es-data1.yml ln -s /usr/local/es-data2/config/elasticsearch.yml /etc/elasticsearch/es-data2.yml
主節(jié)點(diǎn)配置:
vim /etc/elasticsearch/es-master.yml # ======================== Elasticsearch Configuration ========================= # cluster.name: elk-cluster node.name: node-master node.attr.rack: r1 # ----------------------------------- Paths ------------------------------------ #設(shè)置data存放的路徑為/data/elasticsearch/data path.data: /data/elasticsearch/data #設(shè)置logs日志的路徑為/data/elasticsearch/logs path.logs: /data/elasticsearch/logs # ---------------------------------- Network ----------------------------------- #本機(jī)地址 network.host: 192.168.6.108 #開啟監(jiān)聽的端口為9200 http.port: 9200 #tcp通訊端口 transport.tcp.port: 9330 #是否作為主機(jī) node.master: true #是否作為數(shù)據(jù)節(jié)點(diǎn) node.data: false node.max_local_storage_nodes: 3 #在刪除索引時(shí),是否需要明確指定名稱.該值為false時(shí),則可通過正則或_all刪除: action.destructive_requires_name: true # --------------------------------- Discovery ---------------------------------- #集群信息,默認(rèn)的通訊接口是9300 discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"] #在新集群搭建初期,總會(huì)出現(xiàn)某幾個(gè)節(jié)點(diǎn)與其他節(jié)點(diǎn)通信異常導(dǎo)致節(jié)點(diǎn)頻繁加入、退出集群。這個(gè)過程是自動(dòng)執(zhí)行的。通過配置discovery.zen.ping_timeout來控制節(jié)點(diǎn)加入某個(gè)集群或者開始選舉的響應(yīng)時(shí)間(默認(rèn)3s)。 discovery.zen.ping_timeout: 60s #這個(gè)關(guān)閉了自動(dòng)創(chuàng)建索引。為的也是安全考慮,否則即使是內(nèi)網(wǎng),也有很多掃描程序,一旦開啟,掃描程序會(huì)自動(dòng)給你創(chuàng)建很多索引。 action.auto_create_index: false # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: #設(shè)置集群中N個(gè)節(jié)點(diǎn)啟動(dòng)時(shí)進(jìn)行數(shù)據(jù)恢復(fù),默認(rèn)為1。 gateway.recover_after_nodes: 2 #設(shè)置初始化數(shù)據(jù)恢復(fù)進(jìn)程的超時(shí)時(shí)間,默認(rèn)是5分鐘。 gateway.recover_after_time: 3m #設(shè)置集群中節(jié)點(diǎn)的數(shù)量,默認(rèn)為2,一旦這N個(gè)節(jié)點(diǎn)啟動(dòng),就會(huì)立即進(jìn)行數(shù)據(jù)恢復(fù)。 gateway.expected_nodes: 3 # ---------------------------------- Various ----------------------------------- #開啟跨域訪問支持,默認(rèn)為false http.cors.enabled: true #跨域訪問允許的域名地址,(允許所有域名)以上使用正則 http.cors.allow-origin: /.*/
數(shù)據(jù)1節(jié)點(diǎn)配置:
vim /etc/elasticsearch/es-data1.yml # ======================== Elasticsearch Configuration ========================= # cluster.name: elk-cluster node.name: node-data1 node.attr.rack: r1 # ----------------------------------- Paths ------------------------------------ #設(shè)置data存放的路徑為/dat1/elasticsearch/data path.data: /data1/elasticsearch/data #設(shè)置logs日志的路徑為/data1/elasticsearch/logs path.logs: /data1/elasticsearch/logs # ---------------------------------- Network ----------------------------------- #本機(jī)地址 network.host: 192.168.6.108 #開啟監(jiān)聽的端口為9201 http.port: 9201 #tcp通訊端口 transport.tcp.port: 9331 #是否作為主機(jī) node.master: false #是否作為數(shù)據(jù)節(jié)點(diǎn) node.data: true node.max_local_storage_nodes: 3 #在刪除索引時(shí),是否需要明確指定名稱.該值為false時(shí),則可通過正則或_all刪除: action.destructive_requires_name: true # --------------------------------- Discovery ---------------------------------- #集群信息,默認(rèn)的通訊接口是9300 discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"] discovery.zen.ping_timeout: 60s action.auto_create_index: false # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: #設(shè)置集群中N個(gè)節(jié)點(diǎn)啟動(dòng)時(shí)進(jìn)行數(shù)據(jù)恢復(fù),默認(rèn)為1。 gateway.recover_after_nodes: 2 #設(shè)置初始化數(shù)據(jù)恢復(fù)進(jìn)程的超時(shí)時(shí)間,默認(rèn)是5分鐘。 gateway.recover_after_time: 3m gateway.expected_nodes: 3 # ---------------------------------- Various ----------------------------------- #開啟跨域訪問支持,默認(rèn)為false http.cors.enabled: true #跨域訪問允許的域名地址,(允許所有域名)以上使用正則 http.cors.allow-origin: /.*/
數(shù)據(jù)2節(jié)點(diǎn)配置:
vim /etc/elasticsearch/es-data2.yml # ======================== Elasticsearch Configuration ========================= # cluster.name: elk-cluster node.name: node-data2 node.attr.rack: r1 # ----------------------------------- Paths ------------------------------------ #設(shè)置data存放的路徑為/data2/elasticsearch/data path.data: /data2/elasticsearch/data #設(shè)置logs日志的路徑為/data2/elasticsearch/logs path.logs: /data2/elasticsearch/logs # ---------------------------------- Network ----------------------------------- #本機(jī)地址 network.host: 192.168.6.108 #開啟監(jiān)聽的端口為9202 http.port: 9202 #tcp通訊端口 transport.tcp.port: 9332 #是否作為主機(jī) node.master: false #是否作為數(shù)據(jù)節(jié)點(diǎn) node.data: true node.max_local_storage_nodes: 3 #在刪除索引時(shí),是否需要明確指定名稱.該值為false時(shí),則可通過正則或_all刪除: action.destructive_requires_name: true # --------------------------------- Discovery ---------------------------------- #集群信息,默認(rèn)的通訊接口是9300 discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"] discovery.zen.ping_timeout: 60s #這個(gè)關(guān)閉了自動(dòng)創(chuàng)建索引。為的也是安全考慮,否則即使是內(nèi)網(wǎng),也有很多掃描程序,一旦開啟,掃描程序會(huì)自動(dòng)給你創(chuàng)建很多索引。 action.auto_create_index: false # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: #設(shè)置集群中N個(gè)節(jié)點(diǎn)啟動(dòng)時(shí)進(jìn)行數(shù)據(jù)恢復(fù),默認(rèn)為1。 gateway.recover_after_nodes: 2 #設(shè)置初始化數(shù)據(jù)恢復(fù)進(jìn)程的超時(shí)時(shí)間,默認(rèn)是5分鐘。 gateway.recover_after_time: 3m gateway.expected_nodes: 3 # ---------------------------------- Various ----------------------------------- #開啟跨域訪問支持,默認(rèn)為false http.cors.enabled: true #跨域訪問允許的域名地址,(允許所有域名)以上使用正則 http.cors.allow-origin: /.*/
使用外網(wǎng)搭建集群時(shí)需注意:
network.host需要修改為0.0.0.0,同時(shí)要暴露你的外網(wǎng)地址,代碼如下:
network.host: 0.0.0.0 network.publish_host: xx.xx.xx.xx
discovery.zen.ping.unicast.hosts修改為各個(gè)服務(wù)器的外網(wǎng)地址和通訊端口即可。
2.4 安裝head插件
head插件能夠生成集群的統(tǒng)計(jì)數(shù)據(jù),并提供瀏覽器查詢,同時(shí)還能對(duì)elasticsearch索引進(jìn)行結(jié)構(gòu)化查詢。
cd /usr/local/es-master/ mkdir head &&cd head git clone https://github.com/mobz/elasticsearch-head.git cd elasticsearch-head/ #install node v7.x curl --silent --location https://rpm.nodesource.com/setup_7.x | bash - sudo yum install nodejs npm config set registry=http://registry.npm.taobao.org npm install ##運(yùn)行緩慢 npm install -g grunt-cli ##修改配置文件 [root@Elk_Server elasticsearch-head]# vim Gruntfile.js connect: { server: { options: { port: 9100, hostname: '192.168.6.108', ##添加這行,冒號(hào)后面有空格 base: '.', keepalive: true } } }
2.5 系統(tǒng)參數(shù)調(diào)整
##修改es三個(gè)節(jié)點(diǎn)的JVM運(yùn)行內(nèi)存,這個(gè)可以根據(jù)需要更改,但是-Xms和-Xmx的值必須一樣,不然啟動(dòng)報(bào)錯(cuò) [root@Elk_Server ]# vim /usr/local/es-master/config/jvm.options [root@Elk_Server ]# vim /usr/local/es-data1/config/jvm.options [root@Elk_Server ]# vim /usr/local/es-data2/config/jvm.options ------------------------------------------------------------------------- -Xms2g -Xmx2g ##修改Linux最大打開文件數(shù) vim /etc/security/limits.conf * soft nofile 655350 ##在末尾添加 * hard nofile 655350 ##這一步需要重啟linux使配置生效 reboot [root@Elk_Server~]# ulimit -n 655350 ##修改sysctl.conf文件 [root@Elk_Server ~]# vim /etc/sysctl.conf vm.max_map_count=655350 [root@Elk_Server ~]# sysctl -p
2.6 將ES的head插件啟動(dòng)
cd /usr/local/es-master/head/elasticsearch-head && grunt server & [3] 5946 ##會(huì)開啟9100端口。
2.7 啟動(dòng)Elasticsearch(ES只能使用普通用戶啟動(dòng))
chown -R elasticsearch:elasticsearch /usr/local/es-* chown -R elasticsearch:elasticsearch /data{,1,2}/elasticsearch ##切換到普通用戶啟動(dòng)elasticsearch su - elasticsearch nohup /usr/local/es-master/bin/elasticsearch & # -d或&以后臺(tái)的方式進(jìn)行啟動(dòng)Elasticsearch [1] 6202 ##啟動(dòng)成功是會(huì)開啟9200,9300兩個(gè)端口的。 ##傳輸端口為9300接受http請(qǐng)求的端口為9200 ##在瀏覽器打開可以利用head插件查看elasticsearch的集群及索引情況
2.8 主節(jié)點(diǎn)啟動(dòng)好后,用同樣的方法啟動(dòng)子節(jié)點(diǎn)就可以了。
如果報(bào)錯(cuò),解決方式: http://www.sfodin.cn/Linux/2019-08/159734.htm
2.9 驗(yàn)證啟動(dòng)
ps axu |grep elasticsearch 方法一: curl 'http://192.168.6.108:9200/_search?pretty' ----- { "took" : 9, "timed_out" : false, "_shards" : { "total" : 0, "successful" : 0, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : 0.0, "hits" : [ ] } } 方法二: //或者通過瀏覽器訪問:http://192.168.6.108:9200/
#檢查es版本信息 curl -u <user>:<passwd> http://192.168.6.108:9200 #此時(shí)觀察ES集群狀態(tài): curl http://192.168.6.108:9200/_cluster/health?pretty #觀察集群內(nèi)各索引狀態(tài): curl http://192.168.6.108:9200/_cat/indices?pretty #查詢elasticsearch節(jié)點(diǎn)狀態(tài): curl -XGET http://192.168.6.108:9200/_cat/shards |grep UNASSIGNED #查看節(jié)點(diǎn)列表 http://IP:9200/_cat/nodes?v curl 'IP:9200/_cat/nodes?v' #列出所有索引及存儲(chǔ)大小 http://IP:9200/_cat/indices?v curl 'IP:9200/_cat/indices?v'--查詢所有索引及數(shù)據(jù)大小 #創(chuàng)建索引 創(chuàng)建索引名為XX,默認(rèn)會(huì)有5個(gè)分片,1個(gè)索引 curl -XPUT 'IP:9200/XX?pretty' #添加一個(gè)類型 curl -XPUT 'IP:9200/XX/external/2?pretty' -d ' { "gwyy": "John" }' #更新一個(gè)類型 curl -XPOST 'IP:9200/XX/external/1/_update?pretty' -d ' { "doc": {"name": "Jaf"} }' #刪除指定索引 curl -XDELETE 'IP:9200/_index?pretty'
三、安裝Logstash:
Logstash的功能如下:
其實(shí)它就是一個(gè)收集器而已,我們需要為它指定Input和Output(當(dāng)然Input和Output可以為多個(gè))。由于我們需要把Java代碼中Log4j的日志輸出到ElasticSearch中,因此這里的Input就是Log4j,而Output就是ElasticSearch。
3.1 安裝Logstash
tar zxvf logstash-6.3.2.tar.gz -C /usr/local/ mv /usr/local/logstash-6.3.2 /usr/local/logstash ln -s /usr/local/logstash/bin/logstash /usr/bin/ mkdir -p /data/logs/logstash
3.2 驗(yàn)證安裝是否成功,從下圖中可以看到,當(dāng)前開啟的是9600端口
logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["192.168.6.108:9200"] } stdout { codec => rubydebug }}' ###等待片刻后出現(xiàn)“The stdin plugin is now waiting for input:”,輸入“test”回車,返回下面標(biāo)準(zhǔn)的輸出。 ----- [2018-07-31T07:34:23,968][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600} test { "@version" => "1", "host" => "Elk_Server", "message" => "test", "@timestamp" => 2018-07-31T07:34:40.422Z }
3.3 logstash配置
##接下來是重點(diǎn),也是ELK的難點(diǎn)吧,就是將日志進(jìn)行分析,使用正則匹配過濾日志中想要的內(nèi)容。
[root@Elk_Server logstash]# cat /data/logs/test.log 120.79.189.51 - - [10/Jun/2018:12:58:12 +0800] "POST /wp-cron.php?doing_wp_cron=1528606692.3628709316253662109375 HTTP/1.1" 499 0 "-" "WordPress/4.8; http://120.79.189.51"
##這是日志中的一行,logstash的正則匹配默認(rèn)是匹配一行,以回車符作為分隔符。
##當(dāng)然如果日志是json格式的那么就沒有需要處理直接輸出即可。
#我們到config目錄中新建一個(gè)logstash.conf配置。 [root@Elk_Server logstash]# vim /etc/logstash/conf.d/logstash.conf input { file { path => "/data/logs/test.log" type => "test.log" start_position => "beginning" } } filter { grok { match => { "message" => "(?<ip_addr>d+?.d+?.d+?.d+?)s-s-s[(?<log_date>d+?/w+?/d+?):(?<log_time>d+?:d+?:d+?)s+" } } } output { stdout {codec => rubydebug} }
##這是logstash的日志分析文件,總共分為三個(gè)大部分,input、flter、output,其中input是輸入日志,這里選擇的是file文件,path是文件的路徑,type是文件的類型,這個(gè)可以自定義,主要用來區(qū)分每個(gè)日志,start_position是設(shè)定為beginning是指從文件的開頭開始讀取。
##flter是處理日志的部分,使用grok這個(gè)強(qiáng)大的組件進(jìn)行信息過濾,對(duì)于日志的正則匹配最總會(huì)以json的格式輸出,所以正則匹配的格式是(?<字段名>正則表達(dá)式過濾將要匹配的內(nèi)容)在每個(gè)括號(hào)內(nèi)創(chuàng)建過濾的內(nèi)容,將不需要的內(nèi)容放在括號(hào)外,可以一直做匹配知道日志結(jié)束,這里我只是簡(jiǎn)單匹配日志前面的ip地址和日期時(shí)間。
針對(duì)正則匹配字段詳細(xì)說明:
##正則匹配字段通過“(?<字段名>正則表達(dá)式)”來定義,我們看到“(?<ip_addr>d+?.d+?.d+?.d+?”匹配了日志中的IP地址,然后將不需要的內(nèi)容放在括號(hào)外面匹配,接著是日期和時(shí)間,使用同樣的方式,然后不需要的內(nèi)容放在外面因?yàn)檫@里我只匹配了前面的一點(diǎn)內(nèi)容,如果有需要提取,可以一直匹配,知道這一行結(jié)束,logstash默認(rèn)只會(huì)匹配日志以行作為分割,下一行又是新的一次匹配了。
#output是處理輸出部分,這里我只是簡(jiǎn)單輸出到終端,先實(shí)驗(yàn)正則匹配沒問題后,將輸出指向elasticsearch。
[root@Elk_Server logstash]# logstash -r -f /etc/logstash/conf.d/logstash.conf .............. { "path" => "/data/logs/test.log", "host" => "Elk_Server", "type" => "test.log", "log_date" => "10/Jun/2018", "@version" => "1", "log_time" => "12:58:12", "@timestamp" => 2018-07-31T09:45:16.247Z, "ip_addr" => "120.79.189.51", "message" => "120.79.189.51 - - [10/Jun/2018:12:58:12 +0800] "POST /wp-cron.php?doing_wp_cron=1528606692.3628709316253662109375 HTTP/1.1" 499 0 "-" "WordPress/4.8; http://120.79.189.51"" }
接下來將日志輸出到elasticsearch,修改output里面的內(nèi)容即可。
output { elasticsearch { ##指定輸出到ES action => "index" ##建立索引 hosts => "192.168.6.108:9200" ##ES地址端口 index => "test_log-%{+yyyy.MM.dd}" ##索引的名稱 } stdout {codec => rubydebug} }
3.4 驗(yàn)證配置,并報(bào)告錯(cuò)誤
logstash -f /etc/logstash/conf.d/logstash.conf --config.test_and_exit \或者 logstash -t -f /etc/logstash/conf.d/logstash.conf
3.5 服務(wù)啟動(dòng)
nohup logstash -f /etc/logstash/conf.d/logstash.conf --config.reload.automatic -l /data/logs/logstash & \或者 nohup logstash -r -f /etc/logstash/conf.d/logstash.conf -l /data/logs/logstash &
–config.reload.automatic選項(xiàng)啟用自動(dòng)配置重新加載,這樣您每次修改配置文件時(shí)都不必停止并重新啟動(dòng)Logstash。
3.6 結(jié)合filebeat案例
日志內(nèi)容如下:
2018-12-20 17:40:50.665 INFO 1 --- [Thread-422] f.s.j.e.common.iml.AbstractJobWorker : no task in app f.s.job.executor.social.users's job 0 2018-12-22 11:23:55.947 INFO -- [cluster-ClusterId{value='5xxx001145200', description='null'}-dds-xxxc9e41.mongodb.rds.aliyuncs.com:3717] org.mongodb.xxx.cluster : Server xxx:3717 is no longer a member of the replica set. Removing from client view of cluster.
filebeat配置文件:
#=========================== Filebeat inputs ============================= filebeat.inputs: - type: log enabled: true paths: - /data/log/*.log fields: app: tomcat type: all fields_under_root: true #multiline.pattern: '^[' multiline.pattern: '^d+-d+-d+s+d+:d+:d+' multiline.negate: true multiline.match: after multiline.max_lines: 200 multiline.timeout: 10s #tags: ["filebeat"] #exclude_lines: ['^d+-d+-d+s+d+:d+:d+s+DEBUG'] #include_lines: ['^ERR', '^WARN'] #----------------------------- Logstash output -------------------------------- output.logstash: # enabled: true hosts: ["127.0.0.1:5044"] # index => "filebeat-%{+YYYY.MM.dd}" # worker: 4 # bulk_max_size: 1024 # compression_level: 6 # loadbalance: false # backoff.max: 120s
logstash配置文件:
[root@Elk_Server logstash]# vim /etc/logstash/conf.d/logstash.conf input { beats { host => '0.0.0.0' port => "5044" } } filter { grok { match => { "message" => ["%{TIMESTAMP_ISO8601:date}s+(?:%{LOGLEVEL:loglevel}|%{LOGLEVEL:loglevel}s+d) -+ [%{NOTSPACE:servicename}] %{JAVALOGMESSAGE:message}", "%{TIMESTAMP_ISO8601:date}s+(?:%{LOGLEVEL:loglevel}|%{LOGLEVEL:loglevel}s+d) -+ [%{NOTSPACE:servicename}{%{DATA:description}}-%{DATA:db}] %{JAVALOGMESSAGE:message}"] } overwrite => [ "message" ] } #geoip { # source => "remote_addr" # target => "geoip" # database => "/opt/GeoLite2-City.mmdb" # add_field => ["[geoip][coordinates]", "%{[geoip][longitude]}"] # add_field => ["[geoip][coordinates]", "%{[geoip][latitude]}"] #} date { locale => "en" match => ["date", "dd/MMM/yyyy:HH:mm:ss Z"] } } output { elasticsearch { hosts => [ "192.168.6.108:9200" ] index => "logstash-%{type}-%{+YYYY.MM.dd}" } # stdout {codec => rubydebug} }
四、安裝Kibana
4.1 安裝Kibana
tar zxvf kibana-6.3.2-linux-x86_64.tar.gz -C /usr/local/ mv /usr/local/kibana-6.3.2-linux-x86_64 /usr/local/kibana
4.2 配置Kibana
cd /usr/local/kibana/ vim config/kibana.yml #把以下注釋放開,使配置起作用。 ----- server.port: 5601 server.host: "192.168.6.108" elasticsearch.url: "http://192.168.6.108:9200" kibana.index: ".kibana"
4.3 啟動(dòng)Kibana并進(jìn)行測(cè)試訪問,從日志中可以看出,當(dāng)前開了5601端口
./bin/kibana #后臺(tái)啟動(dòng) nohup /usr/local/kibana/bin/kibana & ----- log [03:30:39.833] [info][status][plugin:kibana@6.2.4] Status changed from uninitialized to green - Ready log [03:30:39.882] [info][status][plugin:elasticsearch@6.2.4] Status changed from uninitialized to yellow - Waiting for Elasticsearch log [03:30:40.026] [info][status][plugin:timelion@6.2.4] Status changed from uninitialized to green - Ready log [03:30:40.033] [info][status][plugin:console@6.2.4] Status changed from uninitialized to green - Ready log [03:30:40.037] [info][status][plugin:metrics@6.2.4] Status changed from uninitialized to green - Ready log [03:30:40.061] [info][listening] Server running at http://192.168.30.211:5601 log [03:30:40.117] [info][status][plugin:elasticsearch@6.2.4] Status changed from yellow to green - Ready
4.4 查看啟動(dòng)沒有報(bào)錯(cuò),可以通過192.168.6.108:5601在瀏覽器進(jìn)行訪問了。
添加索引
①點(diǎn)擊Discover出現(xiàn)以下界面,在logstash日志分析文件里面有建立了一個(gè)索引,索引的名稱是test_log-${日期}這樣的形式,它是 Elasticsearch 中的一個(gè)索引名稱開頭。Kibana 會(huì)自動(dòng)檢測(cè)在 Elasticsearch 中是否存在該索引名稱。
②按照注釋配置,然后點(diǎn)擊Next step,在第二頁(yè) 選擇@timestamp點(diǎn)擊create創(chuàng)建
③創(chuàng)建完成之后,可以看到以下一個(gè)界面,紅框內(nèi)是 自動(dòng)生成的域,也可以理解為 跟數(shù)據(jù)庫(kù)中的字段類似,其中有一個(gè)message字段,就是我們想要的日志信息。
④再次點(diǎn)擊Discover出現(xiàn)以下界面,可以看到默認(rèn)搜索的是最后15分鐘的日志,可以通過點(diǎn)擊設(shè)置搜索的時(shí)間范圍.
⑤可以點(diǎn)擊右側(cè)域的add設(shè)置需要顯示的字段
添加完成之后,日志顯示如下:
⑥也可以查看請(qǐng)求體和響應(yīng)體