ELKStack 基础介绍之 Logstash [三]

一、Logstash介绍

Logstash是一个完全开源的工具,他可以对你的日志进行收集、分析,并将其存储供以后使用(如,搜索),您可以使用它。说到搜索,logstash带有一个web界面,搜索和展示所有日志。 
  kibana也是一个开源和免费的工具,他可以帮助您汇总、分析和搜索重要数据日志并提供友好的web界面。他可以为 Logstash 和 ElasticSearch 提供的日志分析的 Web 界面。

提示: logstash与es没有任何关系 
image_1bdc0c7fn1ir6179p18n01bffgs19.png-69.5kB

ELK几个概念

INPUT

OUTPUT

首先LogStash是收集日志,它收集完日志就需要把日志存储下来,所以我们可以用运输者的身份来表示LogStash(INPUT or OUTPUT)LogStash可以在日志发送之前做一个过滤(OUTPUT之前)

二、LogStash部署与配置

和Elasticsearch一样,在开始部署LogStash之前也需要你的环境中正确的安装的JDK。可以下载安装Oracle的JDK或者使用 yum安装openjdk。

LogStash三大功能

 
  1. INPUT
  2. FILTER (支持过滤的功能)
  3. OUTPUT

image_1bdc0cn4v14l1gcj19mq10ncfdtm.png-59.6kB

1.设置时区

 
  1. [root@abcdocker ~]# yum install ntpdate y
  2. [root@abcdocker ~]# ntpdate time1.aliyun.com
  3. [root@abcdocker ~]# timedatectl set-timezone Asia/Shanghai #设置时区
  4. 另一种设置时区方法
  5. [root@abcdocker ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

像我这样高逼格的玩家一般都把这种命令用一条显示

 
  1. [root@abcdocker ~]# yum install ntpdate -y && ntpdate time1.aliyun.com && timedatectl set-timezone Asia/Shanghai

2.安装JDK

 
  1. [root@abcdocker ~]# yum install -y java
  2. [root@abcdocker ~]# java -version
  3. openjdk version "1.8.0_65"
  4. OpenJDK Runtime Environment (build 1.8.0_65-b17)
  5. OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)

3.YUM部署LogStash 
下载并安装GPG key

 
  1. [root@abcdocker ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch

我们可以查询是否导入成功,可能有很多key

 
  1. [root@abcdocker ~]# rpm -qa gpg-pubkey*
  2. gpg-pubkey-f4a80eb5-53a7ff4b
  3. gpg-pubkey-d88e42b4-52371eca

4.添加yum仓库

 
  1. [root@abcdocker ~]# cat /etc/yum.repos.d/logstash.repo
  2. [logstash-2.3]
  3. name=Logstash repository for 2.3.x packages
  4. baseurl=https://packages.elastic.co/logstash/2.3/centos
  5. gpgcheck=1
  6. gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
  7. enabled=1

5.安装logstash

 
  1. [root@abcdocker ~]# yum install -y logstash
  2. [root@abcdocker ~]# rpm -qa |grep logst
  3. logstash-2.3.4-1.noarch
  4. #请在2台服务器都进行安装

6. 配置logstash

提示:logstash实现input和ouput主要是依赖于logstash有需多的插件

 
  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'
  2. 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
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. www.abcdocker.com
  6. 2017-03-12T04:13:31.347Z abcdocker.com www.abcdocker.com
  7. input插件stdin(标准输入)
  8. output插件 stdout(标准输出)
  9. /opt/logstash/bin/logstash 前台启动
  10. 提示:标准输入和标准输出的意思就是我们输入什么就会输出什么
  11. 输入:www.abcdocker.com
  12. 输入:时间戳、节点名称、内容

使用codec rubydebug功能

 
  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug} }'
  2. 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
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. www.abcdocker.com
  6. {
  7. "message" => "www.abcdocker.com", #内容
  8. "@version" => "1", #版本
  9. "@timestamp" => "2017-03-12T04:18:06.702Z", #时间戳
  10. "host" => "abcdocker.com" #主机
  11. }
  12. => 代表等号
  13. 这样就可以让我们的输出内容显示的美观一些

7. 将日志写入es里面 
image_1bdc0d7uji05126f1f9g142210ml13.png-60kB

官方文档地址:https://www.elastic.co/guide/en/logstash/current/first-event.html 
INPUT插件:https://www.elastic.co/guide/en/logstash/current/input-plugins.html 
OUTPUT插件:https://www.elastic.co/guide/en/logstash/current/output-plugins.html

我们将日志写入到es需要查看output插件,同样我们也可以INPUT es日志,OUTPUT es日志 
写入到elasticsearch: 
https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html

配置主机及索引(生产场景都是写入配置文件中)

 
  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
  2. #使用output插件将数据写入es
  3. # hosts 定义主机名称
  4. # index 定义日期格式
  5. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
  6. 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
  7. Settings: Default pipeline workers: 1
  8. Pipeline main started
  9. www.abcdocker.com --- time

这时候信息已经不进行输出而是写入到ES中,我们可以到es上进行查看

访问地址: http://ip:9200/_plugin/head/

ES搭建文档:https://www.abcdocker.com/abcdocker/2234 
image_1bdc0dl3i122d68p1h1tjcb12dm1g.png-126.4kB

image_1bdc0e2c8ms819698vq1vm113a01t.png-167.2kB
这样我们就可以看到我们输出的www.abcdocker.com ---time的信息,以及日志的时间等。我们写入完之后直接刷新就可以显示。

我们可以将信息打印到es里面,又进行输出

 
  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
  2. 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
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. 123456789000
  6. 3{
  7. "message" => "123456789000",
  8. "@version" => "1",
  9. "@timestamp" => "2017-04-05T08:19:52.469Z",
  10. "host" => "abcdocker.com"
  11. }
  12. 33333333333
  13. {
  14. "message" => "333333333333",
  15. "@version" => "1",
  16. "@timestamp" => "2017-04-05T08:19:56.412Z",
  17. "host" => "abcdocker.com"
  18. }
  19. 111111111
  20. {
  21. "message" => "111111111",
  22. "@version" => "1",
  23. "@timestamp" => "2017-04-05T08:19:59.885Z",
  24. "host" => "abcdocker.com"
  25. }

image_1bdc0ejp91gbn1kll10rd106e1k5b2a.png-208.4kB

提示:我们学习logstash需要灵活的运用本身的插件,来实现我们想要的任何的功能

我们现在把logstash放在前台运行,是为了更好的学习插件。现在我们需要把logstash运行在后台

提示:在收集的时候所有的机器都需要安装一个logstash(通过网络可以不需要)因为在INPUT和OUTPUT插件有一个通过http的插件,这样可以通过网络进行传输。如果是文件我所有的节点都需要安装一个logstash

yum安装的目录需要将配置文件放在/etc/logstash/conf.d目录下,因为脚本会在这个目录进行读取

 
  1. [root@abcdocker ~]# vim /etc/init.d/logstash

image_1bdc0euk318mnbi419g51g70jfb2n.png-145.1kB

脚本启动的时候会去这个目录下找配置文件

编写配置文件 
我们进入/etc/logstash/conf.d下进行编辑配置文件

 
  1. [root@abcdocker ~]# cd /etc/logstash/conf.d/
  2. [root@abcdocker conf.d]# cat demo.conf
  3. input{
  4. stdin{}
  5. }
  6. filter{
  7. }
  8. output{
  9. elasticsearch {
  10. hosts => ["192.168.56.11:9200"]
  11. index => "logstash-%{+YYYY.MM.dd}"
  12. }
  13. stdout{
  14. codec => rubydebug
  15. }
  16. }
  17. #配置文件的写法和命令行的区别并不大

配置文件小提示:

  1. 配置文件的语法需要包含INPUT or OUTPUT,其中filter可以没有。
  2. INPUT和OUTPUT中的{}不可以搞错,其中INPUT存放INPUT插件,OUTPUT存放OUTPUT插件
  3. (其中插件也需要有{花括号}) “=>”代表等于,写一个数组需要使用[中括号]多个可以使用逗号分隔。
  4. 字符串需要使用“双引号”
  5. #号代表注释
  6. 日志中信息都是一行一行的,在logstash中叫做事件(因为logstash可以将多行进行合并)
  7. 流程: 事件 --> input --> codec --> filter --> codec --> output

指定配置文件启动

 
  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/demo.conf
  2. 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
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
  6. {
  7. "message" => "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
  8. "@version" => "1",
  9. "@timestamp" => "2017-04-05T09:36:19.770Z",
  10. "host" => "abcdocker.com"
  11. }
  12. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  13. {
  14. "message" => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  15. "@version" => "1",
  16. "@timestamp" => "2017-04-05T09:36:23.651Z",
  17. "host" => "abcdocker.com"
  18. }
  19. ccccccccccccccccccccccccccccccccccccccc
  20. {
  21. "message" => "ccccccccccccccccccccccccccccccccccccccc",
  22. "@version" => "1",
  23. "@timestamp" => "2017-04-05T09:36:27.317Z",
  24. "host" => "abcdocker.com"
  25. }

我们在查看head插件 
image_1bdc0fe481qdg5a31d8k14mvht334.png-292.3kB
这时候我们写的配置文件已经可以实现简单的输出和将数据写入ES中

三、插件介绍

INPUT插件介绍

file插件地址:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html

1.收集mesassage错误日志

 
  1. [root@abcdocker ~]# vim /etc/logstash/conf.d/file.conf
  2. input {
  3. file{
  4. path => ["/var/log/messages","/var/log/secure"]
  5. type => "system-log"
  6. start_position => "beginning"
  7. }
  8. }
  9. filter{
  10. }
  11. output{
  12. elasticsearch {
  13. hosts => ["192.168.56.11:9200"]
  14. index => "system-log-%{+YYYY.MM}"
  15. }
  16. }
  17. path 定义日志路径
  18. start_position 记录日志从头读取数据还是从尾读取数据
  19. type 可以设置类型,进行if判断(简单来说可以将日志进行分类,错误放在一起,正常放在一起)
  20. 提示:关于hosts后面端口可写可不写,不写默认是9200
  21. [root@abcdocker ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/file.conf
  22. 启动

我们可以访问head进行查看 
image_1bdc0g3391j6q9n1hvuorp2o33h.png-312.1kB

image_1bdc0gm1tif5oeggh21kc4tqm3u.png-354.4kB

基础介绍完毕

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值