ELK---日誌分析系統

ELK就是一套完整的日誌分析系統html

ELK=Logstash+Elasticsearch+Kibananode

統一官網https://www.elastic.co/productspython

 

 

ELK模塊說明redis

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Logstash數據庫

做用:用於處理傳入的日誌,負責收集、過濾和寫出日誌json

Logstash分爲三個組件input,filter,outputbootstrap

輸入inputapi

經常使用file,redis,kafka緩存

示例:ruby

input

file {

          path => ['/var/log/neutron/dhcp-agent.log']   //日誌路徑

          tags => ['openstack','oslofmt', 'neutron', 'neutron-dhcp-agent'] 

          start_position => "beginning" 

          type => "neutron" 

          codec => multiline {  //合併行

               pattern => "^%{OPENSTACK_TRACE_BLOCK}"  //自定義變量

               what => "previous"  //上一行合併,next下一行合併

          }

     }

 

input {

kafka {

    zk_connect => "server:2181"

    topic_id => "nova"

    codec =>json

    reset_beginning => false

    consumer_threads => 2

decorate_events =>true

}

}

過濾filter

經常使用Date時間處理、Grok正則捕獲、GeoIP地址查詢

示例:

Fileter{

    grok {

        match => { "message" => "%{OPENSTACK_NORMAL}%{GREEDYDATA:message}"} 

        overwrite => ["message"]  //重寫message

        }

     }

}

Grok內置變量 

 

 

 

能夠自定義變量

1.自定義變量路徑

/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.2/patterns

2. 自定義變量存放在 /opt/logstash/patterns

在配置中添加

filter {

  grok {

    patterns_dir => ["/opt/logstash/patterns "]

    match => { "message" => "%{OPENSTACK_NORMAL}%{GREEDYDATA:message}"}}

  }

}

 

 

OPENSTACK_NORMAL ^%{TIMESTAMP_ISO8601:logdate} %{NUMBER:pid} %{LOGLEVEL:loglevel} %{NOTSPACE:module}%{SPACE}

 

2016-04-27 15:19:14.455 4392 DEBUG nova.api.openstack.wsgi [req-fde66cf0-6d28-4b0d-8671-bce33bb48665 0f288a5b5f19437db670ef94269bfd36 629fb63dd82e46fa937accc99d417059 - - -] Action: 'create', calling method: <bound methodServersController.createof<nova.api.openstack.compute.servers.ServersController object at 0x7c61d10>>, body: {"server": {"name": "test11", "imageRef": "c9620d95-fc3a-4090-b9e8-6c3909cc556e", "flavorRef": "100000000", "max_count": 1, "min_count": 1, "networks": [{"uuid": "e18f583f-c8cf-433a-8095-315712525ecd"}]}} _process_stack /usr/lib/python2.7/site-packages/nova/api/openstack/wsgi.py:789

 

 

output

經常使用Elasticserch、保存爲文件、輸出到HDFS、標準輸出

示例:

output {

elasticsearch {

hosts=>["server:9200"]   //老版本爲 host 新版本 hosts

document_type =>"%{type}"

workers => 2

index => "logstash-%{type}-%{+YYYY.MM.dd}"  //索引名稱

}

}

output {

kafka {

       bootstrap_servers => "server:9092"

       topic_id => "nova"

       compression_type => "snappy"

     }

}

 

 

 

 

 

 

 

 

 

 

 

elasticsearch

用於將導入數據創建動態倒排索引,創建磁盤緩存,提供磁盤同步控制,達到準實時檢索

 

DB 和 elasticsearch對比

 

Index索引

索引至關於數據庫的一個庫

Type

類型至關於數據庫的一個表

Document

文檔至關於數據庫的一行數據

Filed

屬性至關於數據庫的一個字段

Mapping

映射理解爲一種方案

查詢方式

1. query-string

 curl -XGET server:9200/logstash-nova-2016.04.27/nova/_search?q=pid.raw:1524'

2.DSL (經常使用)

  curl -XGET server:9200/logstash-nova-2016.04.27/nova/_search -d '{

    "query" : {

        "term" : { "pid.raw " : "1524" }

    }

  }

GET  查詢

POST 更新

PUT  建立

DELETE刪除

HEAD獲取基礎信息

集羣(Cluster)

ES集羣是一個或多個節點的集合,它們共同存儲了整個數據集,並提供了聯合索引以及可跨全部節點的搜索能力。

 

ES集羣須要修改配置文件

config/elasticsearch.yml

每臺es機器的配置文件中 cluster.name相同,node.name不一致

 

ES集羣內部實現HA,避免單點故障

集羣內部自動選擇一個主節點,監聽node節點狀態,若是發生故障提取節點副本分片,均衡分發給其餘節點。

 

節點(Node)

運行了單個實例的ES主機稱爲節點,它是集羣的一個成員,能夠存儲數據、參與集羣索引及搜索操做。

 

分片(shard)

分片存儲索引,一個索引可能會存在多個分片上。

 

Shard有兩種類型:primary和replica,即主shard及副本shard。

Primary shard建立完成,其Primary shard的數量將不可更改,默認是5

Replica shard是Primary Shard的副本,用於冗餘數據及提升搜索性能,默認是1。

 

說明:

Elasticsearch優化方案

  1. 使用SSD 在elasticsearch.yml配置數據存放位置

path.data: /mnt/data/elasticsearch  #數據存在掛載硬盤 進行配置

  1. 根據索引數量,調整分片數量
  2. 根據實際狀況調整內存
  3. Elasticsearch mapping調整,有些默認設置能夠禁用

Index中默認會有_all的域,這個會給查詢帶來方便,可是會增長索引時間和索引尺寸

"_all" : {"enabled" : false}

執行語句

PUT my_index

{

  "mappings": {

    "my_type": {

      "_all": {

        "enabled": false

      }

            }

          }

       }

 

Elasticsearch API說明

文檔API: 提供對文檔的增刪改查操做

搜索API: 提供對文檔進行某個字段的查詢

索引API: 提供對索引進行操做

查看API: 按照更直觀的形式返回數據,更適用於控制檯請求展現

集羣API: 對集羣進行查看和操做的API

 

 

查詢語法能夠參考官網進行學習:

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

安裝elasticsearch head插件管理es

 

 

 

 

 

Kibana

用於得到elasticsearch的數據進行展現

 

 

 

 

 

 

 

 

 

 

應用

建立虛擬機的日誌收集

Logstash   配置文件

input {

    file {

        path => ['/var/log/nova/nova-api.log']

        tags => ['openstack','oslofmt', 'oslofmt', 'nova', 'nova-api']

        start_position => "beginning"

        type => "nova"

    }

 

    file {

        path => ['/var/log/nova/nova-conductor.log']

        tags => ['openstack', 'oslofmt', 'nova', 'nova-conductor']

        start_position => "beginning"

        type => "nova"

            }

 

   

 

    file {

        path => ['/var/log/nova/nova-scheduler.log']

        tags => ['openstack', 'oslofmt',  'nova', 'nova-scheduler']

        start_position => "beginning"

        type => "nova"

            }

 

    file {             

        path => ['/var/log/nova/nova-compute.log']

        tags => ['openstack', 'oslofmt', 'nova', 'nova-compute']

        start_position => "beginning"

        type => "nova"

            }

 

   

    file {

        path => ['/var/log/neutron/server.log']

        tags => ['openstack','oslofmt', 'neutron', 'neutron-server']

        start_position => "beginning"

        type => "neutron"

            }

}

 

 

 

filter {

   

    mutate {

        gsub => ['path', "/.+/", ""]

    }

   

    if "oslofmt" in [tags] {

        grok {

            match => { "message" => "%{OPENSTACK_NORMAL}%{GREEDYDATA:message}"}

            overwrite => ["message"]

        }

    }

   

    if "Traceback" in [message] or "File" in [message] or "RuntimeERROR" in [message] or "Command" in [message] or "Exit" in [message] or "Stdin" in [message]{

         multiline {   

            pattern => "^%{GREEDYDATA}"

            what => "previous"

        }

    }

   

    date {

        match => ["logdate", "yyyy-MM-dd HH:mm:ss.SSS",

                             "EEE MMM dd HH:mm:ss.SSSSSS yyyy",

                             "dd/MMM/yyyy:HH:mm:ss",

                             "dd-MMM-yyyy::HH:mm:ss",

                             "MMM dd HH:mm:ss",

                             "MMM  dd HH:mm:ss",

                             "yyyy-MM-dd HH:mm:ss.SSS"  ]

    }

 

    if [loglevel] in ["WARNING","WARN","TRACE", "ERROR"] {

        mutate {

            add_tag => ["something_wrong"]

        }

    }

       

}

 

 

output {

 

stdout { codec => rubydebug }

 

elasticsearch

 

{hosts=>["server:9200"]

 

document_type =>"%{type}"

 

workers => 16

 

index => "logstash-%{type}-%{+YYYY.MM.dd}"

}

}

 

 

Elasticsearch  

按照模塊query DSL 語句

nova-api

Curl-XGET"http://192.168.44.128:9200/logstash-nova-2016.04.27/nova/_search?pretty=true" -d

'{"query":

{"bool":

{"must":

[{"term":{"path.raw":"nova-api.log"}},{"query_string": {"default_field": "_all","query": "fde66cf0 783b26ba"}},

{"range": {"logdate.raw": {"gt": "2016-04-27 15:19:14.455","lt": "2016-04-27 15:19:21.999"}

}}]

}

}

}'

 

 nova-scheduler

curl-XGET"http://192.168.44.128:9200/logstash-nova-2016.04.27/nova/_search?pretty=true" -d

'{"query":

{"bool":

{"must":

[{"term":

{"path.raw":"nova-scheduler.log"}},{"query_string": {"default_field": "_all","query": "fde66cf0"}}],

"must_not": [ ],"should": [ ]

}

}

}'

 

nova-conductor

curl-XGET"http://192.168.44.128:9200/logstash-nova-2016.04.27/nova/_search?pretty=true"  -d

'{"query":

{"bool":

{"must":

[{"term":

{"path.raw":"nova.conductor.log"}},

{"query_string":{"default_field":"_all","query":"fde66cf0"}}],

"must_not": [ ],

"should": [ ]

}

}

}'

 

nova-compute

curl-XGET"http://192.168.44.128:9200/logstash-nova-2016.04.27/nova/_search?pretty=true"  -d

'{"query":

{"bool":

{"must":

[{"term":

{"path.raw":"nova-compute.log"}},

{"query_string":

{"default_field":"_all","query":"fde66cf0"}},

{"range":

{"logdate.raw":{"gt":"2016-04-2715:19:10.000","lt": "2016-04-2715:25:07.981"}}}],

"must_not":

[{"term":{"module.raw":"oslo_service.periodic_task"}},

{"term":{"module.raw":"oslo_concurrency.lockutils"}},

{"term": {"module.raw": "keystoneclient.session"}}],

"should": [ ]

}

}

}'

 

server.log

curl-XGET"http://192.168.44.128:9200/logstash-neutron-2016.04.27/neutron/_search?pretty=true"  -d

'{"query":

{"bool":

{"must":

[{"term": {"path.raw": "server.log"}},

{"query_string":{"default_field":"_all","query":"783b26ba ed1db9be"}},

{"range":{"logdate.raw":{"gt":"2016-04-2715:19:10.000","lt": "2016-04-27 15:21:00.000"}

}

}]

}

}

}'

相關文章
相關標籤/搜索