logstash入门(简单而全面)

原文地址:http://logstash.net/docs/1.4.2/tutorials/getting-started-with-logstash

英语水平有限,如果有错误请各位指正

简介

Logstash是一个接收,处理,转发日志的工具。支持系统日志,webserver日志,错误日志,应用日志,总之包括所有可以抛出来的日志类型。怎么样听起来挺厉害的吧?

依赖条件:JAVA

Logstash运行仅仅依赖Java运行环境(jre)。各位可以在命令行下运行java -version命令

显示类似如下结果:

java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

为了确保成功运行Logstash建议大家使用较近期的jre版本。

可以获取开源版本的jre在: http://openjdk.java.net

或者你可以在官网下载Oracle jdk版本:http://www.oracle.com/technetwork/java/index.html

一旦jre已经成功在你的系统中安装完成,我们就可以继续了

启动和运行Logstash的两条命令示例

第一步我们先下载Logstash

curl -<span class="constant">O</span> <span class="symbol" style="color: rgb(153, 0, 115);">https:</span>/<span class="regexp" style="color: rgb(0, 153, 38);">/download.elasticsearch.org/logstash</span><span class="regexp" style="color: rgb(0, 153, 38);">/logstash/logstash</span>-<span class="number" style="color: rgb(0, 153, 153);">1.4</span>.<span class="number" style="color: rgb(0, 153, 153);">2</span>.tar.gz
现在你应该有了一个叫logstash-1.4.2.tar.gz的文件了。 我们把它解压一下
tar zxvf logstash-1.4.2.tar.gz
cd logstash-1.4.2
现在我们来运行一下:
bin/logstash -e 'input { stdin { } } output { stdout {} }'
我们现在可以在命令行下输入一些字符,然后我们将看到logstash的输出内容:
<span class="tag" style="color: rgb(0, 0, 128);">hello</span> <span class="tag" style="color: rgb(0, 0, 128);">world</span>
2013-11-21<span class="tag" style="color: rgb(0, 0, 128);">T01</span><span class="pseudo">:22</span><span class="pseudo">:14</span><span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">.405</span>+0000 0<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">.0</span><span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">.0</span><span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">.0</span> <span class="tag" style="color: rgb(0, 0, 128);">hello</span> <span class="tag" style="color: rgb(0, 0, 128);">world</span>
Ok,还挺有意思的吧... 以上例子我们在运行logstash中,定义了一个叫"stdin"的input还有一个"stdout"的output,无论我们输入什么字符,Logstash都会按照某种格式来返回我们输入的字符。这里注意我们在命令行中使用了  -e 参数,该参数允许Logstash直接通过命令行接受设置。这点尤其快速的帮助我们反复的测试配置是否正确而不用写配置文件。

让我们再试个更有意思的例子。首先我们在命令行下使用CTRL-C命令退出之前运行的Logstash。现在我们重新运行Logstash使用下面的命令:

bin/logstash -e 'input { stdin { } } output { stdout { codec => rubydebug } }'
我们再输入一些字符,这次我们输入"goodnight moon":
goodnight moon
{
  <span class="string" style="color: rgb(221, 17, 68);">"message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"goodnight moon"</span>,
  <span class="string" style="color: rgb(221, 17, 68);">"@timestamp"</span> => <span class="string" style="color: rgb(221, 17, 68);">"2013-11-20T23:48:05.335Z"</span>,
  <span class="string" style="color: rgb(221, 17, 68);">"@version"</span> => <span class="string" style="color: rgb(221, 17, 68);">"1"</span>,
  <span class="string" style="color: rgb(221, 17, 68);">"host"</span> => <span class="string" style="color: rgb(221, 17, 68);">"my-laptop"</span>
}
以上示例通过重新设置了叫"stdout"的output(添加了"codec"参数),我们就可以改变Logstash的输出表现。类似的我们可以通过在你的配置文件中添加或者修改inputs、outputs、filters,就可以使随意的格式化日志数据成为可能,从而订制更合理的存储格式为查询提供便利。

使用Elasticsearch存储日志

现在,你也许会说:"它看起来还挺高大上的,不过手工输入字符,并把字符从控制台回显出来。实际情况并不实用"。说的好,那么接下来我们将建立Elasticsearch来存储输入到Logstash的日志数据。如果你还没有安装Elasticsearch,你可以 下载RPM/DEB包 或者手动下载tar包,通过以下命令:

curl -<span class="constant">O</span> <span class="symbol" style="color: rgb(153, 0, 115);">https:</span>/<span class="regexp" style="color: rgb(0, 153, 38);">/download.elasticsearch.org/elasticsearch</span><span class="regexp" style="color: rgb(0, 153, 38);">/elasticsearch/elasticsearch</span>-<span class="number" style="color: rgb(0, 153, 153);">1.1</span>.<span class="number" style="color: rgb(0, 153, 153);">1</span>.tar.gz
tar zxvf elasticsearch-<span class="number" style="color: rgb(0, 153, 153);">1.1</span>.<span class="number" style="color: rgb(0, 153, 153);">1</span>.tar.gz
cd elasticsearch-<span class="number" style="color: rgb(0, 153, 153);">1.1</span>.<span class="number" style="color: rgb(0, 153, 153);">1</span>/
.<span class="regexp" style="color: rgb(0, 153, 38);">/bin/elasticsearch</span>

注意

本篇文章实例使用Logstash 1.4.2和Elasticsearch 1.1.1。不同的Logstash版本都有对应的建议Elasticsearch版本。请确认你使用的Logstash版本!

更多有关安装和设置Elasticsearch的信息可以参考 Elasticsearch官网 。因为我们主要介绍Logstash的入门使用,Elasticsearch默认的安装和配置就已经满足我们要求。

言归正专,现在Elasticsearch已经运行并监听9200端口了(大家都搞定了,对吗?),通过简单的设置Logstash就可以使用Elasticsearch作为它的后端。默认的配置对于Logstash和Elasticsearch已经足够,我们忽略一些额外的选项来设置elasticsearch作为output:

bin/logstash -e 'input { stdin { } } output { elasticsearch { host => localhost } }'
随意的输入一些字符,Logstash会像之前一样处理日志(不过这次我们将不会看到任何的输出,因为我们没有设置stdout作为output选项)
you know, <span class="keyword" style="font-weight: bold;">for</span> logs
我们可以使用curl命令发送请求来查看ES是否接收到了数据:
curl 'http://localhost:9200/_search?pretty'
返回内容如下:
{
  "<span class="attribute" style="color: rgb(0, 128, 128);">took</span>" : <span class="value"><span class="number" style="color: rgb(0, 153, 153);">2</span></span>,
  "<span class="attribute" style="color: rgb(0, 128, 128);">timed_out</span>" : <span class="value">false</span>,
  "<span class="attribute" style="color: rgb(0, 128, 128);">_shards</span>" : <span class="value">{
    "<span class="attribute" style="color: rgb(0, 128, 128);">total</span>" : <span class="value"><span class="number" style="color: rgb(0, 153, 153);">5</span></span>,
    "<span class="attribute" style="color: rgb(0, 128, 128);">successful</span>" : <span class="value"><span class="number" style="color: rgb(0, 153, 153);">5</span></span>,
    "<span class="attribute" style="color: rgb(0, 128, 128);">failed</span>" : <span class="value"><span class="number" style="color: rgb(0, 153, 153);">0</span>
  }</span></span>,
  "<span class="attribute" style="color: rgb(0, 128, 128);">hits</span>" : <span class="value">{
    "<span class="attribute" style="color: rgb(0, 128, 128);">total</span>" : <span class="value"><span class="number" style="color: rgb(0, 153, 153);">1</span></span>,
    "<span class="attribute" style="color: rgb(0, 128, 128);">max_score</span>" : <span class="value"><span class="number" style="color: rgb(0, 153, 153);">1.0</span></span>,
    "<span class="attribute" style="color: rgb(0, 128, 128);">hits</span>" : <span class="value">[ {
      "<span class="attribute" style="color: rgb(0, 128, 128);">_index</span>" : <span class="value"><span class="string" style="color: rgb(221, 17, 68);">"logstash-2013.11.21"</span></span>,
      "<span class="attribute" style="color: rgb(0, 128, 128);">_type</span>" : <span class="value"><span class="string" style="color: rgb(221, 17, 68);">"logs"</span></span>,
      "<span class="attribute" style="color: rgb(0, 128, 128);">_id</span>" : <span class="value"><span class="string" style="color: rgb(221, 17, 68);">"2ijaoKqARqGvbMgP3BspJA"</span></span>,
      "<span class="attribute" style="color: rgb(0, 128, 128);">_score</span>" : <span class="value"><span class="number" style="color: rgb(0, 153, 153);">1.0</span></span>, "<span class="attribute" style="color: rgb(0, 128, 128);">_source</span>" : <span class="value">{"<span class="attribute" style="color: rgb(0, 128, 128);">message</span>":<span class="value"><span class="string" style="color: rgb(221, 17, 68);">"you know, for logs"</span></span>,"<span class="attribute" style="color: rgb(0, 128, 128);">@timestamp</span>":<span class="value"><span class="string" style="color: rgb(221, 17, 68);">"2013-11-21T18:45:09.862Z"</span></span>,"<span class="attribute" style="color: rgb(0, 128, 128);">@version</span>":<span class="value"><span class="string" style="color: rgb(221, 17, 68);">"1"</span></span>,"<span class="attribute" style="color: rgb(0, 128, 128);">host</span>":<span class="value"><span class="string" style="color: rgb(221, 17, 68);">"my-laptop"</span>}</span>
    }</span> ]
  }</span>
}</span>
恭喜,至此你已经成功利用Elasticsearch和Logstash来收集日志数据了。

Elasticsearch 插件(题外话)

这里介绍另外一个对于查询你的Logstash数据(Elasticsearch中数据 )非常有用的工具叫Elasticsearch-kopf插件。更多的信息请见 Elasticsearch插件 。安装elasticsearch-kopf,只要在你安装Elasticsearch的目录中执行以下命令即可:

bin/plugin -install lmenezes/<span class="KSFIND_CLASS" id="2KSFindDIV">elasticsearch-kopf</span>
接下来访问 http://localhost:9200/_plugin/kopf 来浏览保存在Elasticsearch中的数据,设置及映射!

多重输出

作为一个简单的例子来设置多重输出,让我们同时设置stdout和elasticsearch作为output来重新运行一下Logstash,如下:

bin/logstash -e 'input { stdin { } } output { elasticsearch { host => localhost } stdout { } }'
当我们输入了一些词组之后,这些输入的内容回回显到我们的终端,同时还会保存到Elasticsearch!(可以使用curl和kopf插件来验证)。

默认配置 - 按照每日日期建立索引

你将发现Logstash可以足够灵巧的在Elasticsearch上建立索引... 每天会按照默认格式是logstash-YYYY.MM.DD来建立索引。在午夜(GMT),Logstash自动按照时间戳更新索引。我们可以根据追溯多长时间的数据作为依据来制定保持多少数据,当然你也可以把比较老的数据迁移到其他的地方(重新索引)来方便查询,此外如果仅仅是简单的删除一段时间数据我们可以使用 Elasticsearch Curator 。

接下来

接下来我们开始了解更多高级的配置项。在下面的章节,我们着重讨论logstash一些核心的特性,以及如何和logstash引擎交互的。

事件的生命周期

Inputs,Outputs,Codecs,Filters构成了Logstash的核心配置项。Logstash通过建立一条事件处理的管道,从你的日志提取出数据保存到Elasticsearch中,为高效的查询数据提供基础。为了让你快速的了解Logstash提供的多种选项,让我们先讨论一下最常用的一些配置。更多的信息,请参考 Logstash事件管道 。

Inputs

input 及输入是指日志数据传输到Logstash中。其中常见的配置如下:

  • file:从文件系统中读取一个文件,很像UNIX命令 "tail -0a"
  • syslog:监听514端口,按照RFC3164标准解析日志数据
  • redis:从redis服务器读取数据,支持channel(发布订阅)和list模式。redis一般 在Logstash消费集群中 作为"broker"角色,保存events队列共Logstash消费。
  • lumberjack:使用lumberjack协议来接收数据,目前已经改为 logstash-forwarder
Filters

Fillters 在Logstash处理链中担任中间处理组件。他们经常被组合起来实现一些特定的行为来,处理匹配特定规则的事件流。常见的filters如下:

  • grok:解析无规则的文字并转化为有结构的格式。Grok 是目前最好的方式来将无结构的数据转换为有结构可查询的数据。有120多种匹配规则,会有一种满足你的需要。
  • mutate:mutate filter 允许改变输入的文档,你可以从命名,删除,移动或者修改字段在处理事件的过程中。
  • drop:丢弃一部分events不进行处理,例如:debug events。
  • clone:拷贝 event,这个过程中也可以添加或移除字段。
  • geoip:添加地理信息(为前台kibana图形化展示使用)
Outputs

outputs是logstash处理管道的最末端组件。一个event可以在处理过程中经过多重输出,但是一旦所有的outputs都执行结束,这个event也就完成生命周期。一些常用的outputs包括:

  • elasticsearch:如果你计划将高效的保存数据,并且能够方便和简单的进行查询...Elasticsearch是一个好的方式。是的,此处有做广告的嫌疑,呵呵。
  • file:将event数据保存到文件中。
  • graphite:将event数据发送到图形化组件中,一个很流行的开源存储图形化展示的组件。 http://graphite.wikidot.com/ 。
  • statsd:statsd是一个统计服务,比如技术和时间统计,通过udp通讯,聚合一个或者多个后台服务,如果你已经开始使用statsd,该选项对你应该很有用。
Codecs

codecs 是基于数据流的过滤器,它可以作为input,output的一部分配置。Codecs可以帮助你轻松的分割发送过来已经被序列化的数据。流行的codecs包括 json,msgpack,plain(text)。

  • json:使用json格式对数据进行编码/解码
  • multiline:将汇多个事件中数据汇总为一个单一的行。比如:java异常信息和堆栈信息
获取完整的配置信息,请参考  Logstash文档 中 "plugin configuration"部分。

更多有趣Logstash内容

使用配置文件

使用-e参数 在命令行中指定配置是很常用的方式,不过如果需要配置更多设置则需要很长的内容。这种情况,我们首先创建一个简单的配置文件,并且指定logstash使用这个配置文件。如我们创建一个文件名是"logstash-simple.conf"的配置文件并且保存在和Logstash相同的目录中。内容如下:

input { stdin { } }
output {
  elasticsearch { host => localhost }
  stdout { codec => rubydebug }
}
接下来,执行命令:
bin/logstash -f logstash-simple.conf
我们看到logstash按照你刚刚创建的配置文件来运行例子,这样更加的方便。注意,我们使用-f参数来从文件获取而代替之前使用-e参数从命令行中获取配置。以上演示非常简单的例子,当然解析来我们继续写一些复杂一些的例子。

过滤器

filters是一个行处理机制将提供的为格式化的数据整理成你需要的数据,让我们看看下面的一个例子,叫grok filter的过滤器。
input { stdin { } }

filter {
  grok {
    match => { <span class="string" style="color: rgb(221, 17, 68);">"message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"%{COMBINEDAPACHELOG}"</span> }
  }
  date {
    match => [ <span class="string" style="color: rgb(221, 17, 68);">"timestamp"</span> , <span class="string" style="color: rgb(221, 17, 68);">"dd/MMM/yyyy:HH:mm:ss Z"</span> ]
  }
}

output {
  elasticsearch { host => localhost }
  stdout { codec => rubydebug }
}
执行Logstash按照如下参数:
bin/logstash -f logstash-filter.conf

现在粘贴下面一行信息到你的终端(当然Logstash就会处理这个标准的输入内容):

127.0.0.1 - - [11/Dec/2013:00:01:45 -0800] "GET /xampp/status.php HTTP/1.1" 200 3891 "http://cadenza/xampp/navi.php" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0"
你将看到类似如下内容的反馈信息:
{
<span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"127.0.0.1 - - [11/Dec/2013:00:01:45 -0800] \"GET /xampp/status.php HTTP/1.1\" 200 3891 \"http://cadenza/xampp/navi.php\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0\""</span>,
<span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"@timestamp"</span> => <span class="string" style="color: rgb(221, 17, 68);">"2013-12-11T08:01:45.000Z"</span>,
<span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"@version"</span> => <span class="string" style="color: rgb(221, 17, 68);">"1"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"host"</span> => <span class="string" style="color: rgb(221, 17, 68);">"cadenza"</span>,
<span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"clientip"</span> => <span class="string" style="color: rgb(221, 17, 68);">"127.0.0.1"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"ident"</span> => <span class="string" style="color: rgb(221, 17, 68);">"-"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"auth"</span> => <span class="string" style="color: rgb(221, 17, 68);">"-"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="string" style="color: rgb(221, 17, 68);">"timestamp"</span> => <span class="string" style="color: rgb(221, 17, 68);">"11/Dec/2013:00:01:45 -0800"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"verb"</span> => <span class="string" style="color: rgb(221, 17, 68);">"GET"</span>,
<span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"request"</span> => <span class="string" style="color: rgb(221, 17, 68);">"/xampp/status.php"</span>,
<span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"httpversion"</span> => <span class="string" style="color: rgb(221, 17, 68);">"1.1"</span>,
<span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"response"</span> => <span class="string" style="color: rgb(221, 17, 68);">"200"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"bytes"</span> => <span class="string" style="color: rgb(221, 17, 68);">"3891"</span>,
<span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"referrer"</span> => <span class="string" style="color: rgb(221, 17, 68);">"\"http://cadenza/xampp/navi.php\""</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"agent"</span> => <span class="string" style="color: rgb(221, 17, 68);">"\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0\""</span>
}
正像你看到的那样,Logstash(使用了grok过滤器)能够将一行的日志数据(Apache的"combined log"格式)分割设置为不同的数据字段。这一点对于日后解析和查询我们自己的日志数据非常有用。比如:HTTP的返回状态码,IP地址相关等等,非常的容易。很少有匹配规则没有被grok包含,所以如果你正尝试的解析一些常见的日志格式,或许已经有人为了做了这样的工作。如果查看详细匹配规则,参考  logstash grok patterns 。

另外一个过滤器是date filter。这个过滤器来负责解析出来日志中的时间戳并将值赋给timestame字段(不管这个数据是什么时候收集到logstash的)。你也许注意到在这个例子中@timestamp字段是设置成December 11, 2013, 说明logstash在日志产生之后一段时间进行处理的。这个字段在处理日志中回添到数据中的,举例来说... 这个值就是logstash处理event的时间戳。

实用的例子

Apache 日志(从文件获取)

现在,让我们使用一些非常实用的配置... apache2访问日志!我们将从本地读取日志文件,并且通过条件设置处理满足我们需要的event。首先,我们创建一个文件名是logstash-apache.conf的配置文件,内容如下(你可以根据实际情况修改你的文件名和路径):

input {
  file {
    path => <span class="string" style="color: rgb(221, 17, 68);">"/tmp/access_log"</span>
    start_position => beginning
  }
}

filter {
  <span class="keyword" style="font-weight: bold;">if</span> [path] =~ <span class="string" style="color: rgb(221, 17, 68);">"access"</span> {
    mutate { replace => { <span class="string" style="color: rgb(221, 17, 68);">"type"</span> => <span class="string" style="color: rgb(221, 17, 68);">"apache_access"</span> } }
    grok {
      match => { <span class="string" style="color: rgb(221, 17, 68);">"message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"%{COMBINEDAPACHELOG}"</span> }
    }
  }
  date {
    match => [ <span class="string" style="color: rgb(221, 17, 68);">"timestamp"</span> , <span class="string" style="color: rgb(221, 17, 68);">"dd/MMM/yyyy:HH:mm:ss Z"</span> ]
  }
}

output {
  elasticsearch {
    host => localhost
  }
  stdout { codec => rubydebug }
}
接下来,我们按照上面的配置创建一个文件(在例子中是"/tmp/access.log"),可以将下面日志信息作为文件内容(也可以用你自己的webserver产生的日志):
71.141.244.242 - kurt [18/May/2011:01:48:10 -0700] "GET /admin HTTP/1.1" 301 566 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
134.39.72.245 - - [18/May/2011:12:40:18 -0700] "GET /favicon.ico HTTP/1.1" 200 1189 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; .NET4.0C; .NET4.0E)"
98.83.179.51 - - [18/May/2011:19:35:08 -0700] "GET /css/main.css HTTP/1.1" 200 1837 "http://www.safesand.com/information.htm" "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
现在使用-f参数来执行一下上面的例子:
bin/logstash -f logstash-apache.conf

你可以看到apache的日志数据已经导入到ES中了。这里logstash会按照你的配置读取,处理指定的文件,任何后添加到文件的内容也会被捕获处理最后保存到ES中。此外,数据中type的字段值会被替换成"apache_access"(这个功能在配置中已经指定)。

这个配置只是让Logstash监控了apache access_log,但是在实际中往往并不够用可能还需要监控error_log,只要在上面的配置中改变一行既可以实现,如下:

input {
  file {
    path => <span class="string" style="color: rgb(221, 17, 68);">"/tmp/*_log"</span>
...
现在你可以看到logstash处理了error日志和access日志。然而,如果你检查了你的数据(也许用 elasticsearch-kopf ),你将发现access_log日志被分成不同的字段,但是error_log确没有这样。这是因为我们使用了“grok”filter并仅仅配置匹配combinedapachelog日志格式,这样满足条件的日志就会自动的被分割成不同的字段。我们可以通过控制日志按照它自己的某种格式来解析日志,不是很好的吗?对吧。

此外,你也许还会发现Logstash不会重复处理文件中已经处理过得events。因为Logstash已经记录了文件处理的位置,这样就只处理文件中新加入的行数。漂亮!

条件判断

我们利用上一个例子来介绍一下条件判断的概念。这个概念一般情况下应该被大多数的Logstash用户熟悉掌握。你可以像其他普通的编程语言一样来使用if,else if和else语句。让我们把每个event依赖的日志文件类型都标记出来(access_log,error_log其他以log结尾的日志文件)。

input {
  file {
    path => <span class="string" style="color: rgb(221, 17, 68);">"/tmp/*_log"</span>
  }
}

filter {
  <span class="keyword" style="font-weight: bold;">if</span> [path] =~ <span class="string" style="color: rgb(221, 17, 68);">"access"</span> {
    mutate { replace => { type => <span class="string" style="color: rgb(221, 17, 68);">"apache_access"</span> } }
    grok {
      match => { <span class="string" style="color: rgb(221, 17, 68);">"message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"%{COMBINEDAPACHELOG}"</span> }
    }
    date {
      match => [ <span class="string" style="color: rgb(221, 17, 68);">"timestamp"</span> , <span class="string" style="color: rgb(221, 17, 68);">"dd/MMM/yyyy:HH:mm:ss Z"</span> ]
    }
  } <span class="keyword" style="font-weight: bold;">else</span> <span class="keyword" style="font-weight: bold;">if</span> [path] =~ <span class="string" style="color: rgb(221, 17, 68);">"error"</span> {
    mutate { replace => { type => <span class="string" style="color: rgb(221, 17, 68);">"apache_error"</span> } }
  } <span class="keyword" style="font-weight: bold;">else</span> {
    mutate { replace => { type => <span class="string" style="color: rgb(221, 17, 68);">"random_logs"</span> } }
  }
}

output {
  elasticsearch { host => localhost }
  stdout { codec => rubydebug }
}

我想你已经注意到了,我们使用"type"字段来标记每个event,但是我们实际上没有解析"error"和”random"类型的日志... 而实际情况下可能会有很多很多类型的错误日志,如何解析就作为练习留给各位读者吧,你可以依赖已经存在的日志。

Syslog

Ok,现在我们继续了解一个很实用的例子:syslog。Syslog对于Logstash是一个很长用的配置,并且它有很好的表现(协议格式符合RFC3164)。Syslog实际上是UNIX的一个网络日志标准,由客户端发送日志数据到本地文件或者日志服务器。在这个例子中,你根本不用建立syslog实例;我们通过命令行就可以实现一个syslog服务,通过这个例子你将会看到发生什么。

首先,让我们创建一个简单的配置文件来实现logstash+syslog,文件名是 logstash-syslog.conf

input {
  tcp {
    port => <span class="number" style="color: rgb(0, 153, 153);">5000</span>
    type => syslog
  }
  udp {
    port => <span class="number" style="color: rgb(0, 153, 153);">5000</span>
    type => syslog
  }
}

filter {
  <span class="keyword" style="font-weight: bold;">if</span> [type] == <span class="string" style="color: rgb(221, 17, 68);">"syslog"</span> {
    grok {
      match => { <span class="string" style="color: rgb(221, 17, 68);">"message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}"</span> }
      add_field => [ <span class="string" style="color: rgb(221, 17, 68);">"received_at"</span>, <span class="string" style="color: rgb(221, 17, 68);">"%{@timestamp}"</span> ]
      add_field => [ <span class="string" style="color: rgb(221, 17, 68);">"received_from"</span>, <span class="string" style="color: rgb(221, 17, 68);">"%{host}"</span> ]
    }
    syslog_pri { }
    date {
      match => [ <span class="string" style="color: rgb(221, 17, 68);">"syslog_timestamp"</span>, <span class="string" style="color: rgb(221, 17, 68);">"MMM  d HH:mm:ss"</span>, <span class="string" style="color: rgb(221, 17, 68);">"MMM dd HH:mm:ss"</span> ]
    }
  }
}

output {
  elasticsearch { host => localhost }
  stdout { codec => rubydebug }
}

执行logstash:

bin/logstash -f logstash-syslog.conf

通常,需要一个客户端链接到Logstash服务器上的5000端口然后发送日志数据。在这个简单的演示中我们简单的使用telnet链接到logstash服务器发送日志数据(与之前例子中我们在命令行标准输入状态下发送日志数据类似)。首先我们打开一个新的shell窗口,然后输入下面的命令:

telnet localhost 5000

你可以复制粘贴下面的样例信息(当然也可以使用其他字符,不过这样可能会被grok filter不能正确的解析):

Dec <span class="number" style="color: rgb(0, 153, 153);">23</span> <span class="number" style="color: rgb(0, 153, 153);">12</span>:<span class="number" style="color: rgb(0, 153, 153);">11</span>:<span class="number" style="color: rgb(0, 153, 153);">43</span> louis postfix/smtpd[<span class="number" style="color: rgb(0, 153, 153);">31499</span>]: connect <span class="keyword" style="font-weight: bold;">from</span> unknown[<span class="number" style="color: rgb(0, 153, 153);">95.75</span><span class="number" style="color: rgb(0, 153, 153);">.93</span><span class="number" style="color: rgb(0, 153, 153);">.154</span>]
Dec <span class="number" style="color: rgb(0, 153, 153);">23</span> <span class="number" style="color: rgb(0, 153, 153);">14</span>:<span class="number" style="color: rgb(0, 153, 153);">42</span>:<span class="number" style="color: rgb(0, 153, 153);">56</span> louis named[<span class="number" style="color: rgb(0, 153, 153);">16000</span>]: client <span class="number" style="color: rgb(0, 153, 153);">199.48</span><span class="number" style="color: rgb(0, 153, 153);">.164</span><span class="number" style="color: rgb(0, 153, 153);">.7</span><span class="preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#64817: query (cache) 'amsterdamboothuren.com/MX/IN' denied</span>
Dec <span class="number" style="color: rgb(0, 153, 153);">23</span> <span class="number" style="color: rgb(0, 153, 153);">14</span>:<span class="number" style="color: rgb(0, 153, 153);">30</span>:<span class="number" style="color: rgb(0, 153, 153);">01</span> louis CRON[<span class="number" style="color: rgb(0, 153, 153);">619</span>]: (www-data) CMD (php /usr/share/cacti/site/poller.php >/dev/<span class="keyword" style="font-weight: bold;">null</span> <span class="number" style="color: rgb(0, 153, 153);">2</span>>/<span class="keyword" style="font-weight: bold;">var</span>/log/cacti/poller-error.log)
Dec <span class="number" style="color: rgb(0, 153, 153);">22</span> <span class="number" style="color: rgb(0, 153, 153);">18</span>:<span class="number" style="color: rgb(0, 153, 153);">28</span>:<span class="number" style="color: rgb(0, 153, 153);">06</span> louis rsyslogd: [origin software=<span class="string" style="color: rgb(221, 17, 68);">"rsyslogd"</span> swVersion=<span class="string" style="color: rgb(221, 17, 68);">"4.2.0"</span> x-pid=<span class="string" style="color: rgb(221, 17, 68);">"2253"</span> x-info=<span class="string" style="color: rgb(221, 17, 68);">"http://www.rsyslog.com"</span>] rsyslogd was HUPed, type <span class="string" style="color: rgb(221, 17, 68);">'lightweight'</span>.
之后你可以在你之前运行Logstash的窗口中看到输出结果,信息被处理和解析!
{
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"Dec 23 14:30:01 louis CRON[619]: (www-data) CMD (php /usr/share/cacti/site/poller.php >/dev/null 2>/var/log/cacti/poller-error.log)"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"@timestamp"</span> => <span class="string" style="color: rgb(221, 17, 68);">"2013-12-23T22:30:01.000Z"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="string" style="color: rgb(221, 17, 68);">"@version"</span> => <span class="string" style="color: rgb(221, 17, 68);">"1"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="string" style="color: rgb(221, 17, 68);">"type"</span> => <span class="string" style="color: rgb(221, 17, 68);">"syslog"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="string" style="color: rgb(221, 17, 68);">"host"</span> => <span class="string" style="color: rgb(221, 17, 68);">"0:0:0:0:0:0:0:1:52617"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="string" style="color: rgb(221, 17, 68);">"syslog_timestamp"</span> => <span class="string" style="color: rgb(221, 17, 68);">"Dec 23 14:30:01"</span>,
<span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"syslog_hostname"</span> => <span class="string" style="color: rgb(221, 17, 68);">"louis"</span>,
<span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"syslog_program"</span> => <span class="string" style="color: rgb(221, 17, 68);">"CRON"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"syslog_pid"</span> => <span class="string" style="color: rgb(221, 17, 68);">"619"</span>,
<span class="indent">  </span><span class="indent">  </span>  <span class="string" style="color: rgb(221, 17, 68);">"syslog_message"</span> => <span class="string" style="color: rgb(221, 17, 68);">"(www-data) CMD (php /usr/share/cacti/site/poller.php >/dev/null 2>/var/log/cacti/poller-error.log)"</span>,
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"received_at"</span> => <span class="string" style="color: rgb(221, 17, 68);">"2013-12-23 22:49:22 UTC"</span>,
<span class="indent">  </span><span class="indent">  </span>   <span class="string" style="color: rgb(221, 17, 68);">"received_from"</span> => <span class="string" style="color: rgb(221, 17, 68);">"0:0:0:0:0:0:0:1:52617"</span>,
<span class="indent">  </span><span class="string" style="color: rgb(221, 17, 68);">"syslog_severity_code"</span> => <span class="number" style="color: rgb(0, 153, 153);">5</span>,
<span class="indent">  </span><span class="string" style="color: rgb(221, 17, 68);">"syslog_facility_code"</span> => <span class="number" style="color: rgb(0, 153, 153);">1</span>,
<span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"syslog_facility"</span> => <span class="string" style="color: rgb(221, 17, 68);">"user-level"</span>,
<span class="indent">  </span><span class="indent">  </span> <span class="string" style="color: rgb(221, 17, 68);">"syslog_severity"</span> => <span class="string" style="color: rgb(221, 17, 68);">"notice"</span>
}
恭喜各位,看到这里你已经成为一个合格的Logstash用户了。你将可以轻松的配置,运行Logstash,还可以发送event给Logstash,但是这个过程随着使用还会有很多值得深挖的地方。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值