ELK就是一套完整的日志分析系统
ELK=Logstash+Elasticsearch+Kibana
统一官网https://www.elastic.co/products
ELK模块说明
Logstash
作用:用于处理传入的日志,负责收集、过滤和写出日志
Logstash分为三个组件input,filter,output
输入input
常用file,redis,kafka
示例:
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优化方案
- 使用SSD 在elasticsearch.yml配置数据存放位置
path.data: /mnt/data/elasticsearch #数据存在挂载硬盘 进行配置
- 根据索引数量,调整分片数量
- 根据实际情况调整内存
- 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"} } }] } } }' |