filebeat+elk简单搭配
參考:
https://www.cnblogs.com/configure/p/7607302.html (kibana登陸認(rèn)證[借用nginx轉(zhuǎn)發(fā)])
https://www.elastic.co/cn/products(官網(wǎng))
https://zhuanlan.zhihu.com/p/23049700(filebeat)
https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html#plugins-filters-date-match(logstash時(shí)間戳轉(zhuǎn)換)
https://blog.csdn.net/wuyinggui10000/article/details/77879016(elk時(shí)區(qū)問題)
架構(gòu)簡介:
利用filebeat收集日志給logstash,logstash統(tǒng)一格式傳遞給elasticsearch,再利用kibana圖形界面進(jìn)行展示
系統(tǒng):全都是centos 6系列版本
1.elasticsearch的安裝配置:
yum install ?https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.0.rpm -y
vi ?/etc/elasticsearch/elasticsearch.yml?
/etc/init.d/elasticsearch start
/etc/elasticsearch/elasticsearch.yml 配置如下:
cluster.name: lesu-elk
node.name: node-1
path.data: /home/elasticsearch ? #保存數(shù)據(jù)的地方
path.logs: /var/log/elasticsearch
debug:
1.elasticsearch依賴于java,所以有時(shí)候會報(bào)java相關(guān)錯(cuò),此時(shí) yum install java -y即可
2.切換數(shù)據(jù)目錄是要注意權(quán)限,我這里就遇到權(quán)限不足,所以需要 chown ?-R elasticsearch ?/home/elasticsearch
測試:
curl 'http://localhost:9200/?pretty' 顯示為:
{
?"name" : "node-1",
?"cluster_name" : "lesu-elk",
?"cluster_uuid" : "goAOXrJpQLuqfoHzl7LJMg",
?"version" : {
? ?"number" : "6.3.0",
? ?"build_flavor" : "default",
? ?"build_type" : "rpm",
? ?"build_hash" : "424e937",
? ?"build_date" : "2018-06-11T23:38:03.357887Z",
? ?"build_snapshot" : false,
? ?"lucene_version" : "7.3.1",
? ?"minimum_wire_compatibility_version" : "5.6.0",
? ?"minimum_index_compatibility_version" : "5.0.0"
?},
?"tagline" : "You Know, for Search"
}
即表示正常
2.logstash的安裝配置
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
vi ?/etc/yum.repos.d/logstash.repo
yum install logstash ?-y
ln -s /usr/local/bin/logstash ?/usr/share/logstash/bin/logstash
ln -s ?/etc/logstash ?/usr/share/logstash/config
#默認(rèn)logstash可執(zhí)行文件不在linux PATH變量搜索范圍內(nèi),第二條則是logstash找不到自己環(huán)境變量的配置文件,所以需要這兩個(gè)軟連接
vi /etc/logstash/filebeat.conf
logstash -t ?/etc/logstash/filebeat.conf #這個(gè)是配置文件測試語句
logstash -f ?/etc/logstash/filebeat.conf #這個(gè)是啟動命令
/etc/yum.repos.d/logstash.repo 內(nèi)容如下
[logstash-6.x]
name=Elastic 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
logstash的調(diào)試配置如下:
input {
?stdin{
? ?}
}
filter {
grok {
match => { "message" => "%{ATS}" }?
}
date {
match => ["time", "yyyy-MM-dd HH:mm:ss,SSS","UNIX"]?
target => "time"
}
ruby {?
? code => "event.set('time', event.get('time').time.localtime + 8*60*60)"?
}
}
output {
stdout{
codec => rubydebug
}
}
#主要在于用stdin和stdout兩個(gè)插件,用來實(shí)時(shí)輸入和輸出
最終的線上配置:
input {
?beats {
? ? port => "5044" ? ??
? ?}
}
filter {
grok {
match => { "message" => "%{ATS}" }?
}
date {
match => ["time", "yyyy-MM-dd HH:mm:ss,SSS","UNIX"]?
target => "time"
}
ruby {?
? code => "event.set('time', event.get('time').time.localtime + 8*60*60)"?
}
}
output {
elasticsearch {
? ? ? ?hosts => ["127.0.0.1:9200"]
? ? ? ?index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
? ?}
}
線上配置簡介:
logstash可以簡單看成三段式的流水線處理,輸入-->數(shù)據(jù)處理-->輸出
輸入就是input這段,可以來自標(biāo)準(zhǔn)輸入stdin、redis等等,這里因?yàn)楦鷉ilebeat結(jié)合,所以用beats監(jiān)聽5044端口等待來自filebeat的數(shù)據(jù)
數(shù)據(jù)處理是filter段,這里采用grok插件對數(shù)據(jù)進(jìn)行分割,我的數(shù)據(jù)是自定義的squid日志,數(shù)據(jù)如下
1500047983.032 494 192.168.124.4 TCP_MISS/200 656 359 http://linzb.com/wx_auth/WechatQrcode/694a37e9c2b7616fd53119fcd7120927/2 - DIRECT/6.6.6.6 image/png
默認(rèn)的grok-patterns沒有現(xiàn)成的規(guī)則可以用,這里我根據(jù)需求只分割image/png的部分前面的部分,所以在/usr/share/logstash/vendor/bundle/jruby/2.3.0/gems/logstash-patterns-core-4.1.2/patterns/grok-patterns中添加自定義條目:
ATS %{NUMBER:time}\s+%{NUMBER:duration}\s%{IP:client_address}\s%{WORD:cache_result}/%{POSINT:status_code}\s%{NUMBER:bytes}\s%{NUMBER:bytes_source}\s%{NOTSPACE:url}\s-\s%{WORD:hierarchy_code}/%{IP:source_address}
%{NUMBER:time}: NUMBER是grok-patterns文件中定義好的匹配規(guī)則變量,這個(gè)語句大意可以理解為,過濾匹配NUMBER規(guī)則的數(shù)據(jù),保存為time字段,其他依次類推
\s:這里代表一個(gè)空格
因?yàn)槲业臄?shù)據(jù)是UNIX的時(shí)間格式(而且NUMBER匹配的是數(shù)字,不是時(shí)間,所以elasticsearch也沒法識別),在elasticsearch中不直觀,所以我這里也用了date插件,轉(zhuǎn)換時(shí)間格式
match => ["time", "yyyy-MM-dd HH:mm:ss,SSS","UNIX"] 含義是匹配time字段,UNIX代表原來的數(shù)據(jù)格式是UNIX時(shí)間
target => "time" 則表示修改后的數(shù)據(jù)存儲為time字段,這里相當(dāng)于修改自身了,也可以保存為其他字段,然后用remove_field插件移除原有字段
ruby那段的語句則是為了修改時(shí)區(qū),event.set是設(shè)置時(shí)間字段,event.get是獲取時(shí)間字段,具體請參考:https://blog.csdn.net/wuyinggui10000/article/details/77879016
output就很簡單了,輸出數(shù)據(jù)給elasticsearch,比較關(guān)鍵的是%{[@metadata][beat]},它可以獲取來自filebeat傳遞的變量,這樣我們就可以為不同的節(jié)點(diǎn)傳遞不同的索引,用以區(qū)分
3.filebeat的安裝配置:
yum install https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.3.0-x86_64.rpm -y
vi /etc/filebeat/filebeat.yml
/etc/init.d/filebeat start
/etc/filebeat/filebeat.yml內(nèi)容如下:?
filebeat.prospectors:
- input_type: log
?paths:
? ?- /home/ats_log/squid*
output.logstash:
?hosts: ["8.8.8.8:5044"]?
?index: 192.168.124.127
path:指明要收集哪些日志
index:這個(gè)字段就是跟前面logstash的%{[@metadata][beat]}相對應(yīng),傳遞給logstash處理作為elasticsearch的索引
注:filebeat用/var/lib/filebeat/registry記錄采取日志的位置,所以要重新讀取日志的話,就修改這里面對應(yīng)日志的offset字段,或者簡單點(diǎn)刪掉這個(gè)文件,不過這時(shí)就必須先停掉filebeat
4.kibana的安裝配置
yum install ?https://artifacts.elastic.co/downloads/kibana/kibana-6.3.0-x86_64.rpm ?-y
vi /etc/kibana/kibana.yml
/etc/init.d/kibana start
kibana.yml配置很簡單,默認(rèn)就可以主要字段如下:
server.port: 5601
server.host: "127.0.0.1"
elasticsearch.url: "http://localhost:9200"
5.為kibana添加認(rèn)證功能(這里為了防止鏈接失效,所以直接摘抄自:https://www.cnblogs.com/configure/p/7607302.html)
?
? vi ?/etc/yum.repos.d/nginx.repo?
yum -y install nginx httpd-tools
mkdir -p /etc/nginx/passwd
htpasswd -c -b /etc/nginx/passwd/kibana.passwd user ******
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.backup
vim /etc/nginx/conf.d/default.conf
service nginx restart
?/etc/yum.repos.d/nginx.repo 內(nèi)容如下:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
?/etc/nginx/conf.d/default.conf 內(nèi)容如下:
server {
listen 80;
?
auth_basic "Kibana Auth";
auth_basic_user_file /etc/nginx/passwd/kibana.passwd;
?
location / {
proxy_pass http://127.0.0.1:5601;
proxy_redirect off;
}
}
轉(zhuǎn)載于:https://blog.51cto.com/linzb/2135687
總結(jié)
以上是生活随笔為你收集整理的filebeat+elk简单搭配的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何通过 Linux Bash 技术,让
- 下一篇: React Native基础入门教程:初