ELK--01 Filebeat拆分索引


ELK--01


1.ELK簡介


elk的組成
E  Elasticsearch  java編寫
F  filebeat 	  go編寫   #注意:那台有服務那台需要裝filebeat
K  kibana 	      java編寫
L  logstash	      java編寫

注意:java編寫的服務啟動慢,占用內存大
      go語言編寫的服務,輕便,速度快
      
#filebeat工作原理
1.類似tail -f 只讀取最新的日志並發給ES
2.會自動記錄讀取的最新行,不會重讀。


2.日志分析的需求


1.找出訪問排名前10的IP  所有的流量 
2.找出訪問排名前10的URL 
3.再分析一下11點到12點之間的數據 
4.對比昨天這個時間段和今天這個時間段訪問頻次有什么變化 
5.對比上周某天某個時間段某個特定鏈接的流量和今天這個這個時間段鏈接的流量
6.找出特定頁面被訪問了多少次 
7.找出有問題的IP
  並告訴我這個IP地址都訪問了什么頁面?
  對比前幾天他來過嗎?
  他什么時間開始攻擊的?什么時間結束攻擊的?
8.找出訪問最慢的前10個頁面
  並且統計平均響應時間
  並且對比昨天這個時間段訪問也這么慢嗎? 
  從哪一天開始變慢的?
9.找出搜索引擎今天抓取了多少次?
  抓取了哪些頁面?
  響應時間如何?
10.找出偽造成搜索引擎的IP地址

3.日志收集分類


系統層:message secure
代理層:nginx haproxy
web層:nginx php tomcat 
db層: mysql redis mongo es
存儲層:nfs gfs

4.准備單機環境


db01需要安裝的服務  nginx filebeat es kibana es-head 
db02需要安裝的服務  nginx filebeat

0.需要時間同步
[root@db01 ~]# yum install -y ntpdate
#手動更新時間
ntpdate time1.aliyun.com
#編寫定時任務
[root@db01 ~]# crontab -l
#時間同步
*/3 * * * * /usr/sbin/ntpdate ntpdate ntp1.aliyun.com &>/dev/null

1.es單機環境准備
[root@db01 ~]# cat >/etc/elasticsearch/elasticsearch.yml <<EOF
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
EOF

#停掉es
[root@db01 ~]#  systemctl stop elasticsearch
#刪除es的數據
[root@db01 ~]#  rm -rf /var/lib/elasticsearch/*
#刪除kibana的數據
[root@db01 ~]#  rm -rf /var/lib/kibana/*
#啟動es
[root@db01 ~]#  systemctl start elasticsearch
#啟動kibana
[root@db01 ~]#  systemctl start kibana
#查看es的日志,確認是否后啟動成功
[root@db01 ~]#  tail -f /var/log/elasticsearch/elasticsearch.log
#查看端口是否存在
[root@db01 ~]#  netstat -lntup|egrep "5601|9200"
tcp        0      0 10.0.0.51:5601          0.0.0.0:*               LISTEN      15081/node          
tcp6       0      0 10.0.0.51:9200          :::*                    LISTEN      14843/java          
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      14843/java 


5.filebeat收集Nginx普通格式日志 ( 在DB01安裝配置 )


0.卸載es環境創建的分詞器
/usr/share/elasticsearch/bin/elasticsearch-plugin remove analysis-ik --purge

systemctl restart elasticsearch.service

1.安裝Nginx( 配置nginx源 )
[root@db01 ~]# cat >/etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

#安裝nginx
[root@db01 ~]# yum install nginx -y 
#重新啟動nginx並加入開機自啟
[root@db01 ~]# systemctl start nginx 
[root@db01 ~]# systemctl enable nginx
#測試連接
[root@db01 ~]# curl 127.0.0.1

2.配置Nginx並創建測試頁面
#刪除默認配置文件
[root@db01 ~]# rm -rf /etc/nginx/conf.d/default.conf 
#配置nginx配置文件
[root@db01 ~]# cat >/etc/nginx/conf.d/www.conf<<EOF
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /code/www;
        index  index.html index.htm;
    }
}
EOF

#根據配置文件創建目錄
[root@db01 ~]# mkdir /code/www/ -p

#創建測試頁面內容
[root@db01 ~]# echo "db01-www" > /code/www/index.html
#檢查
[root@db01 ~]# nginx -t
#重啟nginx
[root@db01 ~]# systemctl restart nginx
#測試連接
[root@db01 ~]# curl 127.0.0.1
#查看日志
[root@db01 ~]# tail -f /var/log/nginx/access.log

3.安裝filebet
[root@db01 ~]# rpm -ivh filebeat-6.6.0-x86_64.rpm

4.配置filebeat
[root@db01 ~]# cat >/etc/filebeat/filebeat.yml<<EOF
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
EOF

5.啟動測試
[root@db01 ~]# systemctl start filebeat

6.檢查結果
[root@db01 ~]# tail -f /var/log/filebeat/filebeat
[root@db01 ~]# curl -s 127.0.0.1:9200/_cat/indices|awk '{print $3}'

7.es-head插件查看

8.kibana添加索引
Management-->kibana-->Index Patterns-->filebeat-6.6.0-2020.02.13

es插件安裝

kibana添加索引


6.filebeat收集Nginx的json格式日志


1.普通Nginx日志不足的地方:
- 日志都在一個value里,不能拆分單獨顯示和搜索
- 索引名稱沒有意義

2.理想中的情況
{
    $remote_addr : 192.168.12.254
    - : -
    $remote_user : -
    [$time_local]: [10/Sep/2019:10:52:08 +0800]
    $request: GET /jhdgsjfgjhshj HTTP/1.0
    $status : 404
    $body_bytes_sent : 153
    $http_referer : -
    $http_user_agent :ApacheBench/2.3
    $http_x_forwarded_for:-
}

3.目標
將Nginx日志轉換成json格式

4.修改nginx配置文件使日志轉換成json
log_format json '{ "time_local": "$time_local", '
                          '"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;

#清空舊日志
[root@db01 ~]# > /var/log/nginx/access.log

#檢查並重啟nginx
[root@db01 ~]# nginx -t
[root@db01 ~]# systemctl restart nginx 

5.修改filebeat配置文件
cat >/etc/filebeat/filebeat.yml<<EOF
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"]
EOF

6.刪除舊的ES索引
es-head >> filebeat-6.6.0-2019.11.15 >> 動作 >>刪除 

7.刪除kibana里面的日日志信息

8.重啟filebeat
[root@db01 ~]# systemctl restart filebeat

9.curl 一下nginx,並在es-head插件查看
[root@db01 ~]# curl 127.0.0.1
db01-www


7.filebeat自定義ES索引名稱


1.理想中的索引名稱
filebeat-6.6.0-2020.02.13
nginx-6.6.0-2019.11.15

2.filebeat配置
[root@db01 ~]# cat >/etc/filebeat/filebeat.yml<<EOF
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-%{[beat.version]}-%{+yyyy.MM}"

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

3.重啟filebeat
[root@db01 ~]# systemctl restart filebeat

4.生成新日志並檢查
[root@db01 ~]# curl 127.0.0.1

5.es-head插件查看並在中kibana添加


8.filebeat按照服務類型拆分索引


1.理想中的情況:
nginx-access-6.6.0-2020.02
nginx-error-6.6.0-2020.02

2.filebeat配置
#第一種方法:
[root@db01 ~]# cat >/etc/filebeat/filebeat.yml <<EOF
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        source: "/var/log/nginx/access.log"
    - index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        source: "/var/log/nginx/error.log"
    
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
EOF

#第二種方法:
[root@db01 ~]# cat >/etc/filebeat/filebeat.yml <<EOF   
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["access"]

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

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  indices:
    - index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "access"
    - index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"
    
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
EOF

3.重啟filebeat
[root@db01 ~]# systemctl restart filebeat 

4.生成正確和錯誤的測試數據
[root@db01 ~]# curl 127.0.0.1/haahha
[root@db01 ~]# curl 127.0.0.1

5.檢查是否生成對應的索引
nginx-access-6.6.0-2020.02
nginx-error-6.6.0-2020.02



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM