快速搭建ELK日志分析系统

文档参考:http://www.javashuo.com/article/p-qywgmrtj-hm.htmlhtml

1、ELK搭建篇

——————————————————————————————————————————
官网地址:https://www.elastic.co/cn/
官网权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
安装指南:https://www.elastic.co/guide/en/elasticsearch/reference/5.x/rpm.html
ELK是Elasticsearch、Logstash、Kibana的简称,这三者是核心套件,但并不是所有。
Elasticsearch是实时全文搜索和分析引擎,提供搜集、分析、存储数据三大功能;是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统。它构建于Apache Lucene搜索引擎库之上。
Logstash是一个用来搜集、分析、过滤日志的工具。它支持几乎任何类型的日志,包括系统日志、错误日志和自定义应用程序日志。它能够从许多来源接收日志,这些来源包括 syslog、消息传递(例如 RabbitMQ)和JMX,它可以以多种方式输出数据,包括电子邮件、websockets和Elasticsearch。
Kibana是一个基于Web的图形界面,用于搜索、分析和可视化存储在 Elasticsearch指标中的日志数据。它利用Elasticsearch的REST接口来检索数据,不只容许用户建立他们本身的数据的定制仪表板视图,还容许他们以特殊的方式查询和过滤数据
快速搭建ELK日志分析系统node

环境

——————————————————————————————————————————
centos7.3两台
IP:172.20.2.207 安装:elasticsearch、logstash、Kibana、Nginx、Http、Redis
173.172.20.2.198 安装:logstash
备注:这次安装的ELK三个组件均为最新版(6.5.1)。在安装配置过程当中出现各类各样的问题。不过和其余版本的安装基本同样。这里再也不详细描述安装过程,将安装服务作成脚本。便于后续自动安装ELK。
ELK总的安装脚本:https://github.com/LWang22/-/blob/master/install_elk.sh
elastic安装脚本: https://github.com/LWang22/-/blob/master/install_elastic.sh
logstash安装脚本:https://github.com/LWang22/-/blob/master/install_logstash.sh
kibana安装脚本: https://github.com/LWang22/-/blob/master/install_kibana.shnginx

手动安装参考:https://www.cnblogs.com/yuhuLin/p/7018858.html

本次配置也是参考此连接。
此处详细描述下elastic-head的安装过程,以前安装是源码安装,其实和rpm安装差很少,但就是 npm install 有问题。这里安装npm的rpm安装方式。git

使用 git 安装elasticsearch-head
yum -y install npm
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install (安装模块到node_modules;安装的是grunt模块)
进入grunt模块,启动grunt模块。
nohup ./grunt server &
检查端口是否起来
netstat -anple | grep 9100
快速搭建ELK日志分析系统
浏览器访问测试是否正常
快速搭建ELK日志分析系统github

logStash的使用

——————————————————————————————————————————
执行logstash命令
./logstash -e 'input { stdin { } } output { stdout {} }'
运行成功后输入:
你好
stdout返回的结果:
快速搭建ELK日志分析系统
注:
-e 执行操做
input 标准输入
{ input } 插件
output 标准输出
{ stdout } 插件 web

经过rubydebug来输出下更详细的信息
#./logstash -e 'input { stdin { } } output { stdout {codec => rubydebug} }'
执行成功输入:
怎么这么慢
stdout输出的结果:
快速搭建ELK日志分析系统
若是标准输出还有elasticsearch中都须要保留应该怎么玩,看下面
#bin/logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["172.20.2.207:9200"] } stdout { codec => rubydebug }}'
运行成功之后输入:
I am elk
返回的结果(标准输出中的结果):
快速搭建ELK日志分析系统
上图引用apache

logstash使用配置文件

——————————————————————————————————————————
官方指南:
https://www.elastic.co/guide/en/logstash/current/configuration.html
建立配置文件elk.conf
#vim /etc/logstash/conf.d/elk.confnpm

文件中添加如下内容
input { stdin { } }
output {
elasticsearch { hosts => ["192.168.1.202:9200"] }
stdout { codec => rubydebug }
}json

使用配置文件运行logstash
#bin/logstash -f config/elk.conf
快速搭建ELK日志分析系统vim

logstash的数据类型

——————————————————————————————————————————

  1. Input插件
    权威指南:https://www.elastic.co/guide/en/logstash/current/input-plugins.html

file插件的使用
#vim /etc/logstash/conf.d/elk.conf
添加以下配置
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["172.20.2.207:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}

运行logstash指定elk.conf配置文件,进行过滤匹配
#bin/logstash -f config/elk.conf
快速搭建ELK日志分析系统
快速搭建ELK日志分析系统
logstash上面没有显示出来type类型,显示的_type。打开索引可查看具体的type。如图所示。

来一发配置安全日志的而且把日志的索引按类型作存放,继续编辑elk.conf文件

#vim /etc/logstash/conf.d/elk.conf
添加secure日志的路径
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}

file {
    path => "/var/log/secure"
    type => "secure"
    start_position => "beginning"
}

}

output {

if [type] == "system" {

    elasticsearch {
        hosts => ["172.20.2.207:9200"]
        index => system-%{+YYYY.MM.dd}"
    }
}

if [type] == "secure" {

    elasticsearch {
        hosts => ["172.20.2.207:9200"]
        index => "secure-%{+YYYY.MM.dd}"
    }
}

}

运行logstash指定elk.conf配置文件,进行过滤匹配
#bin/logstash -f config/elk.conf
快速搭建ELK日志分析系统
这些都没有问题,接下来安装kibana,能够在前台展现。

kibana的安装和使用


kibana的安装较简单,由于目前仅仅是简单使用,所以调整的参数不是不少。
安装kibana环境
官方安装手册:https://www.elastic.co/guide/en/kibana/current/install.html
由于上述已经安装过kibana了,所以保证kiban.yml开启如下配置便可。
快速搭建ELK日志分析系统
在bin目录下执行,在后台运行起来。nohup仍是有缺陷,后面会改为supervisor
nohup ./kibana &
查看5601端口监听状况(kibana监听端口:5601)
快速搭建ELK日志分析系统
在web浏览器访问172.20.2.207:5601

快速搭建ELK日志分析系统
这里咱们不使用kibana提供的模板数据,探索咱们的本身的数据。所以选择 Explore on my own
快速搭建ELK日志分析系统
选择 Connect to your Elasticsearch index ;建立索引(建立索引和elastic上面看到的同样,这样kibana就能根据elastic存储的数据进行搜索展现了)
快速搭建ELK日志分析系统
建立完索引后尽能够在Discover界面查看到elastic保存的数据了。
快速搭建ELK日志分析系统

2、ELK实战篇

——————————————————————————————————————————
好,如今索引也能够建立了,如今能够来输出nginx、apache、message、secrue的日志到前台展现(Nginx有的话直接修改,没有自行安装)
编辑nginx配置文件,修改如下内容(在http模块下添加)

log_format json '{"@timestamp":"$time_iso8601",'
         '"@version":"1",'
         '"client":"$remote_addr",'
         '"url":"$uri",'
         '"status":"$status",'
         '"domian":"$host",'
         '"host":"$server_addr",'
         '"size":"$body_bytes_sent",'
         '"responsetime":"$request_time",'
         '"referer":"$http_referer",'
         '"ua":"$http_user_agent"'
      '}';

修改access_log的输出格式为刚才定义的json
access_log logs/elk.access.log json;

继续修改apache的配置文件

LogFormat "{ \
    \"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \
    \"@version\": \"1\", \
    \"tags\":[\"apache\"], \
    \"message\": \"%h %l %u %t \\\"%r\\\" %>s %b\", \
    \"clientip\": \"%a\", \
    \"duration\": %D, \
    \"status\": %>s, \
    \"request\": \"%U%q\", \
    \"urlpath\": \"%U\", \
    \"urlquery\": \"%q\", \
    \"bytes\": %B, \
    \"method\": \"%m\", \
    \"site\": \"%{Host}i\", \
    \"referer\": \"%{Referer}i\", \
    \"useragent\": \"%{User-agent}i\" \
   }" ls_apache_json

同样修改输出格式为上面定义的json格式
CustomLog logs/access_log ls_apache_json

编辑logstash配置文件,进行日志收集
vim /etc/logstash/conf.d/full.conf

input {
file {
    path => "/var/log/messages"
    type => "system"
    start_position => "beginning"
}   

file {
    path => "/var/log/secure"
    type => "secure"
    start_position => "beginning"
}   

file {
    path => "/etc/httpd/logs/access_log"
    type => "http"
    start_position => "beginning"
}   

file {
    path => "/var/log/nginx/access.log"
    type => "nginx"
    start_position => "beginning"
}

}

output {

if [type] == "system" { 

    elasticsearch {
        hosts => ["172.20.2.207:9200"]
        index => "system-%{+YYYY.MM.dd}"
    }       
}   

if [type] == "secure" {

    elasticsearch {
        hosts => ["172.20.2.207:9200"]
        index => "secure-%{+YYYY.MM.dd}"
    }
}

if [type] == "http" {

    elasticsearch {
        hosts => ["172.2.20.207:9200"]
        index => "http-%{+YYYY.MM.dd}"
    }
}

if [type] == "nginx" {

    elasticsearch {
        hosts => ["172.20.2.207:9200"]
        index => "nginx-%{+YYYY.MM.dd}"
    }
}

}运行看看效果如何bin/logstash -f config/elk.conf其实前面已经展现了。这里再也不重复展现。

相关文章
相关标签/搜索