快速搭建ELK日志分析系统

文档参考:https://www.cnblogs.com/yuhuLin/p/7018858.html

一、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日志分析系统

环境

——————————————————————————————————————————
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.sh

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

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

使用 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日志分析系统

logStash的使用

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

通过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日志分析系统
上图引用

logstash使用配置文件

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

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

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

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日志分析系统

二、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
其实前面已经展示了。这里不再重复展示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章