The Road of DBA 21_NoSQL_ELK---(2)

1.1、怎么修改filebeat默認(rèn)索引格式


索引格式為:服務(wù)名稱-域名-日志格式-時(shí)間(按月)

1)vim /etc/filebeat/filebeat.yml

filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  index: "nginx-www-access-%{[beat.version]}-%{+yyyy.MM}"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true

2)systemctl restart filebeat.service 

image
image

1.2裤翩、nginx修改多域名多日志


1)vim  /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
   log_format   json '{ "time_local": "$time_local", '            ##添加此項(xiàng)资盅,以json格式進(jìn)行刷寫日志
                           '"remote_addr": "$remote_addr", '
                           '"referer": "$http_referer", '
                           '"request": "$request", '
                           '"status": $status, '
                           '"bytes": $body_bytes_sent, '
                           '"agent": "$http_user_agent", '
                           '"x_forwarded": "$http_x_forwarded_for", '
                           '"up_addr": "$upstream_addr",'
                           '"up_host": "$upstream_http_host",'
                           '"upstream_time": "$upstream_response_time",'
                           '"request_time": "$request_time"'
    ' }';
    access_log  /var/log/nginx/access.log  json;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
}

2) [root@db01 /etc/nginx/conf.d]# cat bbs.conf   #配置子網(wǎng)頁(yè)
server {
    listen 80;
    server_name  www.oldboy.com;
    location / {
        root /html/www;
        index index.html;
    }
    access_log  /var/log/nginx/www_access.log  json;
}

3)[root@db01 /etc/nginx/conf.d]# cat bbs.conf 
server {
    listen 80;
    server_name  bbs.oldboy.com;
    location / {
        root /html/bbs;
        index index.html;
    }
    access_log  /var/log/nginx/bbs_access.log  json;
}

4)[root@db01 /etc/nginx/conf.d]# cat blog.conf 
server {
    listen 80;
    server_name  blog.oldboy.com;
    location / {
        root /html/blog;
        index index.html;
    }
    access_log  /var/log/nginx/blog_access.log  json;
}

5)mkdir /html/{www,blog,bbs} -p
   echo "db01 www" > /html/www/index.html
   echo "db01 bbs" > /html/bbs/index.html               ##創(chuàng)建一個(gè)區(qū)別顯示的首頁(yè)文件
   echo "db01 blog" > /html/blog/index.html
   chown -R nginx:nginx /html/
   nginx -t
   systemctl restart nginx 

6)filebeat配置文件
vim /etc/filebeak/filebeak.yml

filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/bbs_access.log
  json.keys_under_root: true                              ##支持json格式的兩個(gè)參數(shù)
  json.overwrite_keys: true                                  ##json重寫索引
  tags: ["bbs"]                                                       ##關(guān)鍵字

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/blog_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["blog"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/www_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["www"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx_www_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:                 ###索引名稱的重構(gòu)
        tags: "www"                    ###當(dāng)匹配到www的時(shí)候索引名字為上
    - index: "nginx_bbs_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "bbs"
    - index: "nginx_blog_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "blog"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"

setup.template.name: "nginx"                      ###重寫規(guī)則
setup.template.pattern: "nginx_*"                ##
setup.template.enabled: false                     ###關(guān)閉默認(rèn)格式
setup.template.overwrite: true                    ###啟用上面的規(guī)則

systemctl restart filebeat.service 

image
image

1.3、收集tomcat日志


1.安裝tomcat
yum install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp tomcat-javadoc -y

2.修改tomcat配置文件為json格式
vim /etc/tomcat/server.xml

第139行替換為:
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","method":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","AgentVersion":"%{User-Agent}i"}"/>

3.重啟tomcat
systemctl restart tomcat

4.修改filebeat配置文件
filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/bbs_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["bbs"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/blog_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["blog"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/www_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["www"]

- type: log
  enabled: true 
  paths:
    - /var/log/tomcat/localhost_access_log.*.txt
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["tomcat"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx_www_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "www"
    - index: "nginx_bbs_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "bbs"
    - index: "nginx_blog_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "blog"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"
    - index: "tomcat_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "tomcat"

setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true

systemctl restart filebeat.service 

image
image

1.4踊赠、收集elasticsearch日志

cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /var/log/nginx/bbs_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["bbs"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/blog_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["blog"]

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/www_access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["www"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]
- type: log
  enabled: true
  paths:
    - /var/log/tomcat/localhost_access_log.*.txt
  tags: ["tomcat"]
- type: log
  enabled: true
  paths:
    - /var/log/elasticsearch/linux58.log            ##監(jiān)控ES日志的報(bào)錯(cuò)情況
  tags: ["java"]
  multiline.pattern: '^\['                                    ##通過(guò)此參數(shù)可以定義message行的范圍
  multiline.negate: true                                   ##規(guī)則可以百度了解到
  multiline.match: after

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx_www_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "www"
    - index: "nginx_bbs_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "bbs"
    - index: "nginx_blog_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "blog"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"
    - index: "tomcat_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "tomcat"
    - index: "es_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "java"

setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true

systemctl restart filebeat.service 

image
image

制圖

)QP47@DMX9S556~U9K_%9W8.png

![]U6HC)78A~MVVO67V2BOV}F.png](https://upload-images.jianshu.io/upload_images/16832986-74884df895ef98ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

}9EB{PN3CN3OMYIXNNM(RTA.png
0OOZVGQQD}ZXDS_R%YI6OHJ.png
BNKK49CQ$2}_$AY)TGY)~M9.jpg
H}O66UBB5I4{[R$]OW9$]8G.png
OH1W$~4(B9TR0VHLSF76H%G.png

![ULMDNSAAQFG$Y(`H5XXYTJ.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末呵扛,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子筐带,更是在濱河造成了極大的恐慌今穿,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件伦籍,死亡現(xiàn)場(chǎng)離奇詭異蓝晒,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)帖鸦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門芝薇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人作儿,你說(shuō)我怎么就攤上這事洛二。” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵晾嘶,是天一觀的道長(zhǎng)妓雾。 經(jīng)常有香客問(wèn)我,道長(zhǎng)变擒,這世上最難降的妖魔是什么君珠? 我笑而不...
    開(kāi)封第一講書人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮娇斑,結(jié)果婚禮上策添,老公的妹妹穿的比我還像新娘。我一直安慰自己毫缆,他們只是感情好唯竹,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著苦丁,像睡著了一般浸颓。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上旺拉,一...
    開(kāi)封第一講書人閱讀 51,688評(píng)論 1 305
  • 那天产上,我揣著相機(jī)與錄音,去河邊找鬼蛾狗。 笑死晋涣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的沉桌。 我是一名探鬼主播谢鹊,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼留凭!你這毒婦竟也來(lái)了佃扼?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蔼夜,失蹤者是張志新(化名)和其女友劉穎兼耀,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體求冷,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡翠订,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了遵倦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡官撼,死狀恐怖梧躺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤掠哥,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布巩踏,位于F島的核電站,受9級(jí)特大地震影響续搀,放射性物質(zhì)發(fā)生泄漏塞琼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一禁舷、第九天 我趴在偏房一處隱蔽的房頂上張望彪杉。 院中可真熱鬧,春花似錦牵咙、人聲如沸派近。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)渴丸。三九已至,卻和暖如春另凌,著一層夾襖步出監(jiān)牢的瞬間谱轨,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工吠谢, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留土童,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓囊卜,卻偏偏與公主長(zhǎng)得像娜扇,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子栅组,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355