Logstash 、 Web日志實(shí)時(shí)分析

ELK日志分析平臺(tái)

ELK架構(gòu)圖例

flowchart LR
subgraph Z1[web cluster]
  subgraph web1
    H1([apache]) --> F1([filebeat])
  end
  subgraph web2
    H2([apache]) --> F2([filebeat])
  end
  subgraph web3
    H3([apache]) --> F3([filebeat])
  end
end
subgraph Z2[Logstash]
  F1 & F2 & F3 --> A1{input} --> A2{filter} --> A3{output}
end
subgraph Z3[ES Cluster]
  ES1([Elasticsearch]);ES2([Elasticsearch]);ES3([Elasticsearch]);ES4([Elasticsearch]);ES5([Elasticsearch])
end
A3 --> ES1 & ES2 & ES3 & ES4 & ES5
ES1 & ES2 & ES3 & ES4 & ES5 --> K((kibana))
classDef WEB color:#ff0000,fill:#99ff99
class web1,web2,web3 WEB
classDef ZONE fill:#ffffc0,color:#ff00ff
class Z1,Z2,Z3 ZONE

logstash安裝

購(gòu)買云主機(jī)
主機(jī) IP地址 配置
logstash 192.168.1.47 最低配置2核2G
logstash云主機(jī)安裝
[root@logstash ~]# vim /etc/hosts
192.168.1.41    es-0001
192.168.1.42    es-0002
192.168.1.43    es-0003
192.168.1.44    es-0004
192.168.1.45    es-0005
192.168.1.47    logstash
[root@logstash ~]# yum install -y java-1.8.0-openjdk logstash
基礎(chǔ)配置樣例
[root@logstash ~]# ln -s /etc/logstash /usr/share/logstash/config
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  stdin {}
}

filter{ }

output{ 
  stdout{}
}
[root@logstash ~]# /usr/share/logstash/bin/logstash
插件與調(diào)試格式

使用json格式字符串測(cè)試 {"a":"1", "b":"2", "c":"3"}

[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  stdin { codec => "json" }
}

filter{ }

output{ 
  stdout{ codec => "rubydebug" }
}
[root@logstash ~]# /usr/share/logstash/bin/logstash

官方手冊(cè)地址

https://www.elastic.co/guide/en/logstash/current/index.html

input file插件
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  file {
    path => ["/tmp/c.log"]
    type => "test"
    start_position => "beginning"
    sincedb_path => "/var/lib/logstash/sincedb"
  }
}
filter{ }
output{ 
  stdout{ codec => "rubydebug" }
}
[root@logstash ~]# rm -rf /var/lib/logstash/plugins/inputs/file/.sincedb_*
[root@logstash ~]# /usr/share/logstash/bin/logstash
filter grok插件

正則表達(dá)式分組匹配格式: (?<名字>正則表達(dá)式)

正則表達(dá)式宏調(diào)用格式: %{宏名稱:名字}

宏文件路徑

/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-patterns-core-4.1.2/patterns

[root@logstash ~]# echo '192.168.1.252 - - [29/Jul/2020:14:06:57 +0800] "GET /info.html HTTP/1.1" 200 119 "-" "curl/7.29.0"' >/tmp/c.log
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  file {
    path => ["/tmp/c.log"]
    type => "test"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}
filter{ 
  grok {
    match => { "message" => "%{HTTPD_COMBINEDLOG}" }
  }
}
output{ 
  stdout{ codec => "rubydebug" }
}
[root@logstash ~]# /usr/share/logstash/bin/logstash
output elasticsearch插件
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  file {
    path => ["/tmp/c.log"]
    type => "test"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}
filter{ 
  grok {
    match => { "message" => "%{HTTPD_COMBINEDLOG}" }
  }
}
output{ 
  stdout{ codec => "rubydebug" }
  elasticsearch {
    hosts => ["es-0004:9200", "es-0005:9200"]
    index => "weblog-%{+YYYY.MM.dd}"
  }
}
[root@logstash ~]# /usr/share/logstash/bin/logstash

瀏覽器打開(kāi) head 插件,通過(guò) web 頁(yè)面瀏覽驗(yàn)證
http://公網(wǎng)IP:9200/_plugin/head/

filebeat配置

logstash beats插件
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  stdin { codec => "json" }
  file{
    path => ["/tmp/c.log"]
    type => "test"
    start_position => "beginning"
    sincedb_path => "/var/lib/logstash/sincedb"
  }
  beats {
    port => 5044
  }
} 

filter{ 
  grok {
    match => { "message" => "%{HTTPD_COMBINEDLOG}" }
  }
} 

output{ 
  stdout{ codec => "rubydebug" }
  elasticsearch {
    hosts => ["es-0004:9200", "es-0005:9200"]
    index => "weblog-%{+YYYY.MM.dd}"
  }
}
[root@logstash ~]# /usr/share/logstash/bin/logstash
web服務(wù)安裝filebeat
[root@web ~]# yum install -y filebeat
[root@web ~]# vim /etc/filebeat/filebeat.yml
24:  enabled: true
28:  - /var/log/httpd/access_log
45:    fields: 
46:       my_type: apache
148, 150 注釋掉
161: output.logstash:
163:   hosts: ["192.168.1.47:5044"]
180, 181, 182 注釋掉
[root@web ~]# grep -Pv "^\s*(#|$)" /etc/filebeat/filebeat.yml
[root@web ~]# systemctl enable --now filebeat

網(wǎng)站日志分析實(shí)戰(zhàn)

1无畔、停止 kibana 服務(wù)

[root@kibana ~]# systemctl stop kibana

2辽装、清空 elasticsearch 中所有數(shù)據(jù)

[root@kibana ~]# curl -XDELETE http://es-0001:9200/*

訪問(wèn) web 頁(yè)面闽寡,瀏覽器打開(kāi) head 插件代兵,通過(guò) web 頁(yè)面瀏覽驗(yàn)證

3、配置 web 日志爷狈,獲取用戶真實(shí)IP
通過(guò) ELB 把 web 服務(wù)發(fā)布公網(wǎng)
https://support.huaweicloud.com/elb_faq/elb_faq_0090.html

4植影、配置 filebeat
詳見(jiàn)配置文件 filebeat.yml
重啟服務(wù)

[root@web ~]# systemctl restart filebeat

5、配置 logstash
詳見(jiàn)配置文件 logstash.conf
啟動(dòng)服務(wù)

[root@logstash ~]# /usr/share/logstash/bin/logstash

6涎永、配置 kibana
啟動(dòng)服務(wù)思币,通過(guò)web頁(yè)面配置 kibana

[root@kibana ~]# systemctl start kibana
常見(jiàn)錯(cuò)誤

使用通配符刪除報(bào)錯(cuò)

[root@es-0001 ~]# curl -XDELETE http://localhost:9200/*
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Wildcard expressions or all indices are not allowed"}],"type":"illegal_argument_exception","reason":"Wildcard expressions or all indices are not allowed"},"status":400}
# 由于設(shè)置了destructive_requires_name 參數(shù),不允許使用通配符
# 查看及解決方式
[root@es-0001 ~]# curl -XGET http://es-0001:9200/_cluster/settings?pretty
{
  "persistent" : {
    "action" : {
      "destructive_requires_name" : "true"
    }
  },
  "transient" : { }
}
[root@es-0001 ~]# curl -XPUT http://localhost:9200/_cluster/settings -d '
{
  "persistent": {
      "action": {
        "destructive_requires_name": "false"
      }
   }
}'
[root@es-0001 ~]# curl -XDELETE http://localhost:9200/*
{"acknowledged":true}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末土辩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子抢野,更是在濱河造成了極大的恐慌拷淘,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件指孤,死亡現(xiàn)場(chǎng)離奇詭異启涯,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)恃轩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門结洼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人叉跛,你說(shuō)我怎么就攤上這事松忍。” “怎么了筷厘?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵鸣峭,是天一觀的道長(zhǎng)宏所。 經(jīng)常有香客問(wèn)我,道長(zhǎng)摊溶,這世上最難降的妖魔是什么爬骤? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮莫换,結(jié)果婚禮上霞玄,老公的妹妹穿的比我還像新娘。我一直安慰自己拉岁,他們只是感情好坷剧,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著膛薛,像睡著了一般听隐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上哄啄,一...
    開(kāi)封第一講書(shū)人閱讀 51,155評(píng)論 1 299
  • 那天雅任,我揣著相機(jī)與錄音,去河邊找鬼咨跌。 笑死沪么,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的锌半。 我是一名探鬼主播禽车,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼刊殉!你這毒婦竟也來(lái)了殉摔?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤记焊,失蹤者是張志新(化名)和其女友劉穎逸月,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體遍膜,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡碗硬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瓢颅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片恩尾。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖挽懦,靈堂內(nèi)的尸體忽然破棺而出翰意,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布猎物,位于F島的核電站虎囚,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蔫磨。R本人自食惡果不足惜淘讥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望堤如。 院中可真熱鬧蒲列,春花似錦、人聲如沸搀罢。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)榔至。三九已至抵赢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間唧取,已是汗流浹背铅鲤。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留枫弟,地道東北人邢享。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像淡诗,于是被迫代替她去往敵國(guó)和親骇塘。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容