日志分析工具ELK(四)

Logstash收集TCP日志

#Input plugins TCP插件
所需的配置选项
tcp {
    port =>...
}
[[email protected]-node1 ~]# cat tcp.conf
input {
    tcp {
        host =>"192.168.230.128"
        port =>"6666"
}

}
output {
    stdout{
        codec =>"rubydebug"
}

}
[[email protected] ~]# /opt/logstash/bin/logstash -f tcp.conf

打开另外一个窗口,进行测试查看

[[email protected] ~]# echo "hehe"|nc192.168.230.1286666
 [[email protected] ~]# echo "oldboy">/dev/tcp/192.168.230.128/6666 #伪终端
[[email protected] ~]# nc 192.168.230.1286666</etc/resolv.conf #还可以追加文件
查看第一个窗口

#TCP用于什么呢,在工作中用于这种要往哪个索引追加一些东西,它们之间漏掉了,通过某种方法写成文件,可以使用nc直接附加进去,也可以弄个文件再收一遍,但那个比较费劲
#如果文件较大,时间较长,可以使用screen

Filter grok

之前学习了Input Output 现在来学习Filter

Filter插件 grok

filter插件有很多,在这里就学习grok插件,使用正则匹配日志里的域来拆分。在实际生产中,apache日志不支持jason,就只能使用grok插件匹配;mysql慢查询日志也是无法拆分,只能石油grok正则表达式匹配拆分。
在如下链接,github上有很多写好的grok模板,可以直接引用
https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns
官方链接地址
https://www.elastic.co/guide/en/logstash/2.3/plugins-filters-grok.html
#Logstash附带120默认模式。你可以在这里找到
Logstash ships with about 120 patterns by default. You can find them here: https://github.com/logstash-plugins/logstash-patterns-core/tree/master/patterns. You can add your own trivially. (See the patterns_dir setting)

Examples: With that idea of a syntax and semantic, we can pull out useful fields from a sample log like this fictional http request log:

55.3.244.1 GET /index.html 158240.043

The pattern for this could be:

预定义的正则表达式,可以来引用
%{IP:client}%{WORD:method}%{URIPATHPARAM:request}%{NUMBER:bytes}%{NUMBER:duration}

A more realistic example, let’s read these logs from a file:

input {
file{
    path =>"/var/log/http.log"
}
}
filter {
  grok {
    match =>{"message"=>"%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}"}
}
}

After the grok filter, the event will have a few extra fields in it:
#使用filter grok后,会输出以下字段
    client:55.3.244.1
    method: GET
    request:/index.html
    bytes:15824
    duration:0.043

我们来测试一下
[[email protected]-node1 ~]# cat grok.conf
input {
    stdin {}
}
filter {
  grok {
    match =>{"message"=>"%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}"}
}
}
output {
    stdout {
    codec =>"rubydebug"
}
}

[[email protected]-node1 ~]# /opt/logstash/bin/logstash -f grok.conf
Settings: Default pipeline workers:2
Pipeline main started
55.3.244.1 GET /index.html 158240.043#输入这一行
{
"message"=>"55.3.244.1 GET /index.html 15824 0.043",
"@version"=>"1",
"@timestamp"=>"2017-01-05T15:21:49.510Z",
"host"=>"linux-node1.example.com",
"client"=>"55.3.244.1",#自动引入了client
"method"=>"GET",
"request"=>"/index.html",
"bytes"=>"15824",
"duration"=>"0.043"
}

那怎么自动引入的呢,系统在安装完软件的时候已经帮我们内置了
[[email protected]-node1 patterns]# pwd #在这个目录下的grok-patterns文件
/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns

七、Logstash解耦之消息队列

数据源Datasource把数据写到input插件中,output插件使用消息队列把消息写入到消息队列Message Queue中,Logstash indexing Instance启动logstash使用input插件读取消息队列中的信息,Fliter插件过滤后在使用output写入到elasticsearch中。

  如果生产环境中不适用正则grok匹配,可以写Python脚本从消息队列中读取信息,输出到elasticsearch中

redis用来解耦

上图架构的优点

解耦,松耦合

解除了由于网络原因不能直接连elasticsearch的情况

方便架构演变,增加新内容

消息队列可以使用rabbitmq,zeromq等,也可以使用redis,kafka(消息不删除,但是比较重量级)等

引入redis到架构中

#安装redis
yum-y install redis

#修改下配置文件
[[email protected] conf.d]# grep ‘^[a-z]‘/etc/redis.conf
daemonize yes  #修改这行为yes,改成在后台运行
pidfile /var/run/redis/redis.pid
port 6379
tcp-backlog 511
bind 192.168.230.128#监听的IP

[[email protected]-node1 conf.d]# systemctl start redis
[[email protected] conf.d]# netstat -ntpl|grep6379
tcp        00192.168.230.128:63790.0.0.0:*               LISTEN      2998/redis-server 1

#我们来测试一下
[[email protected] conf.d]# cat redis-out.conf
input {
    stdin{}

}
output {
    redis {
        host =>"192.168.230.128"
        port =>"6379"
        db =>"6"
        data_type =>"list"#数据类型为list
        key =>"demo"
}

}

#启动配置文件输入信息
[[email protected] conf.d]# /opt/logstash/bin/logstash -f redis-out.conf
Settings: Default pipeline workers:4
Pipeline main started
chuck #输入
sisi 

#开另外一个窗口连接,info查看
[[email protected] conf.d]# redis-cli -h 192.168.230.128
192.168.230.128:6379> info
# Server
redis_version:2.8.19
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:c0359e7aa3798aa2
redis_mode:standalone
os:Linux 3.10.0-123.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.3
process_id:6518
run_id:3ab08fa2b91c79194b9f5c15b7c54680461f6e07
tcp_port:6379
uptime_in_seconds:165
uptime_in_days:0
hz:10
lru_clock:10407823
config_file:/etc/redis.conf

# Clients
connected_clients:2
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:2211840
used_memory_human:2.11M
used_memory_rss:2895872
used_memory_peak:2211840
used_memory_peak_human:2.11M
used_memory_lua:35840
mem_fragmentation_ratio:1.31
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:2
rdb_bgsave_in_progress:0
rdb_last_save_time:1486802666
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:2
total_commands_processed:3
instantaneous_ops_per_sec:0
total_net_input_bytes:316
total_net_output_bytes:13
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.25
used_cpu_user:0.02
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Keyspace
db6:keys=3,expires=0,avg_ttl=0#输出的内容,创建了这个db 6 ,里边有一个key

192.168.230.128:6379> select 6#选择db 6
OK
192.168.230.128:6379[6]> keys *#里边有个demo,选择demo这个key
1)"demo"

这是一个列表,怎么查看这个消息
192.168.230.128:6379[6]> LINDEX demo -1# -1表示最后一行,从内容上看已经写进去了(信息、主机、时间戳等)
"{\"message\":\"sisi\",\"@version\":\"1\",\"@timestamp\":\"2017-01-26T13:14:37.766Z\",\"host\":\"linux-node1.example.com\"}"

192.168.230.128:6379[6]> LINDEX demo -2
"{\"message\":\"chuck\",\"@version\":\"1\",\"@timestamp\":\"2017-02-11T08:46:47.597Z\",\"host\":\"linux-node1.example.com\"}"

为了下一步写input插件到把消息发送到elasticsearch中,多在redis中写入写数据

[[email protected]-node1 ~]# /opt/logstash/bin/logstash -f redis-out.conf
Settings: Default filter workers:1
Logstash startup completed
chuck
sisi
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
k
l
m
n
g
s

#查看redis中名字为demo的key长度
192.168.230.128:6379[6]> LLEN demo
(integer)31

#使用redis发送消息到elasticsearch中
编写redis-in.conf

[[email protected]-node1 conf.d]# cp redis-out-conf redis-in-conf
[[email protected] conf.d]# cat redis-in-conf
input {
    redis {
        host =>"192.168.230.128"
        port =>"6379"
        db =>"6"
        data_type =>"list"
        key =>"demo"
}

}
output {
     elasticsearch {
                hosts =>["192.168.230.128:9200"]
                index =>"redis-demo-%{+YYY.MM.dd}"

}

}

#启动配置文件

[[email protected]-node1 conf.d]# /opt/logstash/bin/logstash -f redis-in-conf
Settings: Default pipeline workers:4
Pipeline main started

#不断刷新demo这个key的长度(读取很快,刷新一定要速度)
192.168.230.128:6379[6]> LLEN demo
(integer)25
192.168.230.128:6379[6]> LLEN demo
(integer)7#可以看到redis的消息正在写入到elasticsearch中
192.168.230.128:6379[6]> LLEN demo
(integer)0

在elasticsearch中查看增加了redis-demo,由于在不同时间点添加的,所以有两个索引

将all.conf的内容改为经由redis
编写shipper.conf作为redis收集logstash配置文件
[[email protected]-node1 conf.d]# cat shipper.conf
input{
  syslog {
type=>"system-syslog"
    host =>"192.168.230.128"
    port =>"514"
}
file{
    path =>"/var/log/nginx/access_json.log"
    codec => json
    start_position =>"beginning"
type=>"nginx-log"
}

file{
     path =>"/var/log/messages"
type=>"system"
     start_position =>"beginning"
}
file{
     path =>"/var/log/elasticsearch/check-cluster.log"
type=>"es-error"
     start_position =>"beginning"
        codec => multiline {
           pattern =>"^\["
           negate => true
           what =>"previous"
}
}
}
output{
if[type]=="system"{
    redis {
        host =>"192.168.230.128"
        port =>"6379"
        db =>"6"
        data_type =>"list"
        key =>"system"
}
}

if[type]=="es-error"{
     redis {
                host =>"192.168.230.128"
                port =>"6379"
                db =>"6"
                data_type =>"list"
                key =>"es-error"
}
}
if[type]=="system-syslog"{
 redis {
                host =>"192.168.230.128"
                port =>"6379"
                db =>"6"
                data_type =>"list"
                key =>"system-syslog"
}
}
if[type]=="nginx-log"{
     redis {
                host =>"192.168.230.128"
                port =>"6379"
                db =>"6"
                data_type =>"list"
                key =>"nginx-log"
}
}
}

#在redis中查看keys
192.168.230.128:6379[6]> select 6
192.168.230.128:6379[6]> keys *
1)"system"
2)"system-syslog"
3)"es-error"

编写indexer.conf作为redis发送elasticsearch配置文件
[[email protected]-node2 /]# cat indexer.conf
input{
        redis {
type=>"system"
                host =>"192.168.230.128"
                port =>"6379"
                db =>"6"
                data_type =>"list"
                key =>"system"
}

         redis {
type=>"es-error"
                host =>"192.168.230.128"
                port =>"6379"
                db =>"6"
                data_type =>"list"
                key =>"es-error"
}
     redis {
type=>"system-syslog"
                host =>"192.168.230.128"
                port =>"6379"
                db =>"6"
                data_type =>"list"
                key =>"system-syslog"
}
         redis {
type=>"nginx-log"
                host =>"192.168.230.128"
                port =>"6379"
                db =>"6"
                data_type =>"list"
                key =>"nginx-log"
}
}
output{
if[type]=="system"{
    elasticsearch {
        hosts =>["192.168.230.128:9200"]
        index =>"system-%{+YYY.MM.dd}"
}
}
if[type]=="es-error"{
        elasticsearch {
                hosts =>["192.168.230.128:9200"]
                index =>"es-error-%{+YYY.MM.dd}"

}
}
if[type]=="system-syslog"{
        elasticsearch {
                hosts =>["192.168.230.128:9200","192.168.230.129:9200"]
                index =>"system-syslog-%{+YYY.MM.dd}"
}
}
if[type]=="nginx-log"{
        elasticsearch {
                hosts =>["192.168.230.128:9200","192.168.230.129:9200"]
                index =>"nginx-log-%{+YYY.MM.dd}"

}
}
}

#启动shipper.conf
[[email protected] conf.d]# /opt/logstash/bin/logstash -f shipper.conf
Settings: Default pipeline workers:4
Pipeline main started

由于日志量小,很快就会全部被发送到elasticsearch,key也就没了,所以多写写数据到日志中
[[email protected]-node1 conf.d]# for n in `seq 10000`;doecho$n>>/var/log/nginx/access_json.log;done
[[email protected] conf.d]# for n in `seq 10000`;doecho$n>>/var/log/messages;done
[[email protected]e1 conf.d]# for n in `seq 10000`;doecho$n>>/var/log/elasticsearch/check-cluster.log;done

查看key的长度看到key在增长
192.168.230.128:6379[6]> LLEN nginx-log
(integer)2450
192.168.230.128:6379[6]> LLEN nginx-log
(integer)2680
192.168.230.128:6379[6]> LLEN nginx-log
(integer)2920

#启动indexer.conf
[[email protected] conf.d]# /opt/logstash/bin/logstash -f indexer.conf
Settings: Default pipeline workers:4
Pipeline main started

#查看key的长度看到key在减小

192.168.230.128:6379[6]> LLEN nginx-log
(integer)20000
192.168.230.128:6379[6]> LLEN nginx-log
(integer)19875
192.168.230.128:6379[6]> LLEN nginx-log
(integer)19875
192.168.230.128:6379[6]> LLEN nginx-log
(integer)19750
192.168.230.128:6379[6]> LLEN nginx-log
(integer)19750

kibana查看nginx-log索引

实时写入测试,节点1启动shipper.conf
[[email protected]-node1 conf.d]# /opt/logstash/bin/logstash -f shipper.conf
Settings: Default pipeline workers:4
Pipeline main started

#在节点2上启动indexer.conf
[[email protected] /]# /opt/logstash/bin/logstash -f indexer.conf
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default pipeline workers:1
Pipeline main started

#在nginx log上增加点东西
[[email protected] conf.d]# for n in `echo zsq`;doecho$n>>/var/log/nginx/access_json.log;done

Kibana搜索查看关键字

时间: 2024-08-08 15:36:59

日志分析工具ELK(四)的相关文章

日志分析工具ELK(五)

八.Kibana实践 选择绝对时间和相对时间 搜索 还可以添加相关信息 自动刷新页面时间,也可以关闭 创建图像,可视化 编辑Markdown,创建一个值班联系表 值班联系表 保存 再创建一个饼图;查看下状态码 保存 再来一个柱状图 保存为访问IP TOP5 添加展示 再添加一个折线图 保存为HTTP响应时间,Dashboard添加,然后保存 再添加一个统计,统计数量 选择system-syslog 保存为日志数据统计 Dashboard添加,然后保存 生产最好加一个Nginx用户认证,不能谁都能

日志分析工具ELK(一)

一.ELK介绍 1.1 elasticsearch 1.1.1 elasticsearch介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便. 1.1.2 elasticsearch几个重要术语 ?   NRT elasti

日志分析工具ELK(二)

五.Logstash日志收集实践 在学习Logstash之前,我们需要先了解以下几个基本概念: logstash收集日志基本流程: input-->codec-->filter-->codec-->output 1.input:从哪里收集日志. 2.filter:发出去前进行过滤 3.output:输出至Elasticsearch或Redis消息队列 4.codec:输出至前台,方便边实践边测试 5.数据量不大日志按照月来进行收集 处理流程(图解) Logstash安装 Logst

日志分析工具ELK(三)

目前官网更新特别快,不到半年时间就更新了好几个版本,目前最新的是5.1 以下安装配置使用4.5版本的 https://www.elastic.co/guide/en/kibana/4.5/index.html Installing Kibana with yum #yum 安装Kibana Warning #警告 The repositories set up in this procedure are not compatible with distributions using versio

开源日志分析系统ELK平台搭建部署

开源日志分析系统ELK平台搭建部署 一.前言 日志主要包括系统日志.应用程序日志和安全日志.系统运维和开发人员可以通过日志了解服务器软硬件信息.检查配置过程中的错误及错误发生的原因.经常分析日志可以了解服务器的负荷,性能安全性,从而及时采取措施纠正错误. 通常,日志被分散的储存不同的设备上.如果你管理数十上百台服务器,你还在使用依次登录每台机器的传统方法查阅日志.这样是不是感觉很繁琐和效率低下.当务之急我们使用集中化的日志管理,例如:开源的syslog,将所有服务器上的日志收集汇总. 集中化管理

AWStats日志分析工具

awstats官方网站:http://awstats.sourceforge.net/ perl官方网站:http://www.perl.com/ Awstats是一个非常简洁而且强大的统计工具.它可以统计您站点的如下信息: 一:访问量,访问次数,页面浏览量,点击数,数据流量等精确到每月.每日.每小时的数据二:访问者国家.访问者IP.操作系统.浏览器等三:Robots/Spiders的统计四:纺客持续时间五:对不同Files type 的统计信息六:Pages-URL的统计七:其它信息(搜索关键

project03日志分析工具AWStats

++++++++++++++++++++ 日志分析工具AWStats ++++++++++++++++++++ http://www.oschina.net/project http://www.oschina.net/project/tag/147/log-analyzer --统计日志分析工具 http://www.awstats.org/ --官方网站 http://www.nltechno.com/awstats/awstats.pl?config=destailleur.fr  --官

Apache日志分析工具awstats配置

awstats软件下载地址: http://www.awstats.org/#DOWNLOAD https://awstats.sourceforge.io/#download 一.准备工作 1.一台Linux系统虚拟机或服务器,这里我使用的是CentOS7.3系统虚拟机 IP地址:192.168.115.120 域名:www. awstats.com 在CentOS7.3中执行下面的内容添加域名解析 echo "192.168.115.120  www. awstats.com" &

mysql慢查询日志分析工具mysqlsla

一.介绍    mysqlsla是一个分析mysql慢日志的工具,可以分析出慢查询的原因,包括执行某条sql出现的次数及在slow log数据的百分比.执行时间.等待销的时间等. 公司的数据库有很多慢查询日志,导致的系统的负载很高,而mysql慢查询日志文件内容格式不太好看,经推荐使用mysqlsla:使用方便,操作简单. 二.安装mysqlsla 系统环境 CentOS release 6.6 (Final) 2.6.32-504.el6.x86_64 官网已经不能下载,所需要的文件已在百度云