`
郑云飞
  • 浏览: 808739 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

用Kibana+Logstash+Elasticsearch快速搭建实时日志查询、收集与分析系统

阅读更多

搭建该平台的目的就是为了运维、研发很方便的进行日志的查询。Kibana一个免费的web壳;Logstash集成各种收集日志插件,还是一个比较优秀的正则切割日志工具;Elasticsearch一个开源的搜索引擎框架(支持群集架构方式)。

1 安装需求

 

1.1 理论拓扑

 

1.2 安装环境

先看看都需要安装什么软件包
ruby 运行Kibana 必须,
rubygems 安装ruby扩展必须
bundler 功能类似于yum
JDK 运行java程序必须
redis 用来处理日志队列
logstash 收集、过滤日志
ElasticSearch 全文搜索服务(logstash集成了一个)
kibana 页面展示
 

192.168.233.128 logstash index,kibana,JDK
192.168.233.129 logstash agent,JDK
192.168.233.130 redis
192.168.233.131 ElasticSearch,JDK

logstash分为 index和agent ,agent负责监控、过滤日志,index负责收集日志并将日志交给ElasticSearch 做搜索。 此外 logstash 的收集方式分为 standalone 和 centralized。

standalone 是所有功能都在一个服务器上面,自发自收,centralized 就是集中收集,一台服务器接收所有shipper(个人理解就是logstash agent)的日志。其实 logstash本身不分 什么 shipper 和 collector ,只不过就是配置文件不同而已,我们这次按照集中的方式来测试。

在 logstash index上安装基础的软件环境:

 

vi /etc/apt/sources.list

deb http://ftp.debian.org/debian/ squeeze main non-free

deb-src http://ftp.debian.org/debian/ squeeze main non-free

# apt-get update

# apt-cache search sun-java

# apt-get install sun-java6-jdk sun-java6-jre

# java -version

 

# apt-get install ruby rubygems

# gem install bundler

开始安装logstash

 
  1. 其实logstash 就是一个java脚本,不需要安装... 下载即用
  2. [192.168.233.128 root@nodec:/soft]
  3. # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar
  4. 现在看看 这个脚本应该怎么去执行
  5. [192.168.233.128 root@nodec:/soft]
  6. # java -jar /soft/logstash-1.1.0-monolithic.jar -h
  7. No such command "-h"
  8. Available commands:
  9. -v
  10. -V
  11. --version
  12. agent
  13. web
  14. test
  15. 显然没有 -h 参数,不过列出了能用的参数,但是logstash的参数可不止这些,
  16. java -jar /soft/logstash-1.1.0-monolithic.jar agent --help
  17. 这些是在agent模式下的命令参数
  18. -f, --config CONFIGFILE
  19. Load the logstash config from a specific file, directory, or a wildcard. If given a directory or wildcard, config files will be readinorder lexigraphically.
  20. -e CONFIGSTRING
  21. Use the given string as the configuration data. Same syntax as the config file. If not input is specified, 'stdin { type => stdin }'isdefault. If nooutputis specified, 'stdout { debug => true }}'isdefault.
  22. -w, --filterworks COUNT
  23. Run COUNT filter workers (default: 1)
  24. --watchdog-timeout TIMEOUT
  25. Set watchdog timeout value.
  26. -l, --log FILE
  27. Log to a given path. Defaultisto log to stdout
  28. -v
  29. Increase verbosity. There are multiple levels of verbosity available with'-vv' currently being the highest
  30. --pluginpath PLUGIN_PATH
  31. A colon-delimted path to find other logstash plugins in
  32. java -jar /soft/logstash-1.1.0-monolithic.jar web --help
  33. 下面的是在web界面的参数
  34. --log FILE
  35. Log to a given path. Defaultis stdout.
  36. --address ADDRESS
  37. Address on which to start webserver. Defaultis 0.0.0.0.
  38. --port PORT
  39. Port on which to start webserver. Defaultis 9292.
  40. -B, --elasticsearch-bind-host ADDRESS
  41. Address on which to bind elastic search node.
  42. -b, --backend URL
  43. The backend URL to use. Defaultis elasticsearch:/// (assumes multicast discovery). You can specify elasticsearch://[host][:port]/[clustername]
如果上面的这些命令都能执行正常的话就表示 logstash可以使用了,但要让他启动还需要一个配置文件 
input { 
redis { 
  host => '192.168.233.130' 
  data_type => 'list' 
  port => "6379" 
  key => 'logstash:redis' 
  type => 'redis-input' 
       } 
       } 
output { 
    elasticsearch { 
        host => '192.168.233.131' 
        port => "9300" 
        } 
       } 
 解释一下 logstash的配置文件由 input filter output 等几个基本的部分组成,顾名思义 input 就是在那收集数据,output就是输出到哪,filter代表一个过滤规则意思是什么内容 会被收集。 上面这段是让 logstash 去192.168.233.130 这个redis服务器上去收集日志 redis端口为6379,key是 logstash:redis 类型为 redis-input ,(注意:这几个值必须跟logstash agent的 output 所对应),收集完成后输出到 elasticsearch ,embedded => true 的意思是使用logstash 内嵌的 elasticsearch。如果有独立的elasticsearch服务器,需要将 这条改为 host => 'elasticsearch的ip' port => 端口  
 安装redis 
 
下载安装就比较简单了
 
 下载安装就比较简单了
 

配置文件里的那几个路径要提前建好 mkdir -p /data/redis cd /data/redis/ mkdir {db,log,etc}
  1. [192.168.233.130 root@nodea:/soft]
  2. # wget http://redis.googlecode.com/files/redis-2.4.14.tar.gz
  3. [192.168.233.130 root@nodea:/data/redis/etc]
  4. # make –j24
  5. [192.168.233.130 root@nodea:/data/redis/etc]
  6. # make install
 
 
 




  1. 我用的是最简单的 配置,


  2. [192.168.233.130 root@nodea:/data/redis/etc]


  3. # vim redis.conf


  4. #this is the config file for redis


  5. pidfile /var/run/redis.pid


  6. port 6379


  7. timeout 0


  8. loglevel verbose


  9. logfile /data/redis/log/redis.log


  10. dbfilename dump.rdb


  11. dir /data/redis/db/


  12. vm-swap-file /tmp/redis.swap


  13. activerehashing yes


  14. 启动命令如下


  15. [192.168.233.130 root@nodea:/data/redis/etc]


  16. # redis-server /data/redis/etc/redis.conf &
  看看 redis服务的状态
 
 
  1. [192.168.233.130 root@nodea:/data/redis/etc]
  2. # lsof -i:6379
  3. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  4. redis-ser 2732 root 4u IPv4 7946 TCP *:6379 (LISTEN)
  5. redis-ser 2732 root 5u IPv4 7963 TCP localhost.localdomain:6379->localhost.localdomain:19214 (ESTABLISHED)
  6. java 2733 root 9u IPv4 7959 TCP localhost.localdomain:19214->localhost.localdomain:6379 (ESTABLISHED)
 
 
 安装elasticsearch 
  elasticsearch会依赖于JAVA_HOME环境变量,所以需要设置JAVA_HOME环境变量。首先查看你的Java的安装路径
 
 
 wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.18.7.zip
 unzip elasticsearch-0.18.7.zip 
 mv elasticsearch-0.18.7 /usr/local/share/elasticsearch 
 
 cd /usr/local/share/elasticsearch/bin/ 
./elasticsearch -f
 
 好了,可以让logstash index 开始启动了 
 
 
 好了,看到9292 端口启动就代表 启动成功了,检查一下
 
 现在可以通过浏览器访问一下 http://192.168.233.128:9292 看看logstash是的页面是个什么样子
  
现在还不能搜索因为现在还没有数据,其实这个时候 http://192.168.233.128:9200 也是可以访问的,
很多开发自己写代码来调用elasticsearch 来实现他们自己的需要,这里就不多说了。
  1. [192.168.233.128 root@nodec:/soft]
  2. # lsof -i:9292
  3. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  4. java 5205 root 465u IPv4 130805 TCP *:armtechdaemon (LISTEN)
  5. 其实logstash还启动了一个端口9200,因为启动了内嵌的 elasticsearch,这个9200是 elasticsearch在监听
  6. [192.168.233.128 root@nodec:/soft]
  7. # lsof -i:9200
  8. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  9. java 5205 root 160u IPv4 130682 TCP *:wap-wsp (LISTEN)

  1. [192.168.233.128 root@nodec:/soft]
  2. # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf -- web &
  3. [1] 5205
  4. ...这里要等待约5秒钟... 为什么?去问开发者吧
  5. [192.168.233.128 root@nodec:/soft]
  6. # I, [2013-03-19T03:23:10.749000 #5205] INFO -- : Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T03:23:10.732000 -0700","message":"Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  7. file:/soft/logstash-1.1.0-monolithic.jar!/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53 warning: already initialized constant WFKV_
  8. Mizuno 0.5.0 (Jetty 8.0.y.z-SNAPSHOT) listening on 0.0.0.0:9292
  9. 解释一下 上面的命令 agent 代理模式 -f 指定配置文件 --web 其实是个分隔符等于又启动了一个命令,后面的参数就是开启一个web页面默认端口是9292,这个命令如果拆成两个就是这个样子
  10. java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf &
  11. java -jar /soft/logstash-1.1.0-monolithic.jar web &
配置logstash的agent
登录到 服务器 192.168.233.129 安装基本软件包和logstash
安装sun-java6-jre sun-java6-jdk
 
  1. [192.168.233.129 root@noded:/soft]
  2. # atp-get install ruby
  3. 192.168.233.129 root@noded:/soft]
  4. # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar
  5. [192.168.233.129 root@noded:/soft]
  6. # vim redis.conf
  7. input {
  8. file {
  9. type => "producer"
  10. path => "/soft/apache.log"
  11. }
  12. file {
  13. type => "php-log"
  14. path => "/soft/php.log"
  15. }
  16. }
  17. filter {
  18. grep {
  19. match => [ "@message", "mysql|GET|error" ]
  20. }
  21. }
  22. output {
  23. redis {
  24. host => '192.168.233.130'
  25. data_type => 'list'
  26. key => 'logstash:redis'
  27. }
  28. }

大概说一下这个配置文件 input 里的file就是要监视的文件了 这里我监视了两个文件,如果这两个文件有追加的内容就会通过下面的output设置发给 redis服务器
filter 里的grep 意思就是 grep... 后面这段就是日志内容里面只要有匹配 mysql或GET或error的内容就会被过滤出来,发送到 logstash index
以上就是一个比较简单的配置文件了,让我们启动他

 
  1. [192.168.233.129 root@noded:/soft]
  2. # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf &
  3. I, [2013-03-19T19:45:35.762000 #2721] INFO -- : Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.752000 -0700","message":"Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  4. I, [2013-03-19T19:45:35.778000 #2721] INFO -- : Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.778000 -0700","message":"Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  5. I, [2013-03-19T19:45:35.804000 #2721] INFO -- : Using beta plugin 'grep'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.803000 -0700","message":"Using beta plugin 'grep'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
  6. I, [2013-03-19T19:45:35.854000 #2721] INFO -- : Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.853000 -0700","message":"Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}


只要没有 warning 和 error就算是正常启动了
启动之前请确定 192.168.233.130的 redis服务器已经启动,不然会报错。

 

最后我们回到 logstash agent 上面测试一下

 
  1. [192.168.233.129 root@noded:/soft]
  2. # echo GET12313 >> apache.log
  3. [192.168.233.129 root@noded:/soft]
  4. # echo errorabcd >> apache.log


ok 到 http://192.168.233.128:9292 去搜索一下 刚才的两个内容


嗯,就是这样了,我现在找个php的错误日志给他追加到php.log文件里
[192.168.233.129 root@noded:/soft]
# cat php-error.log >> php.log
在看看 logstash的页面 搜索一下 error


OK,最后就是 Kibana了 ,我把Kibana装在了 logstash index上面
下载地址为 http://kibana.org/intro.html

 
  1. [192.168.233.128 root@nodec:/soft]
  2. # tar xf Kibana-0.2.0.tar.gz
  3. [192.168.233.128 root@nodec:/soft]
  4. # cd Kibana-0.2.0
  5. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  6. # bundle install
  7. ( /var/lib/gems/1.8/bin/bundle install )
  8. 直接安装就好了,非常简单,因为之前咱们已经安装好了 bundle
  9. 编辑配置文件,指定 elasticsearch 的位置
  10. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  11. # vim KibanaConfig.rb
  12. .....
  13. Elasticsearch = "192.168.18.131:9200"
  14. KibanaPort = 5601
  15. KibanaHost = '0.0.0.0'
  16. .....
  17. 主要是这几个参数
  18. 启动的话需要ruby
  19. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  20. # /usr/bin/ruby kibana.rb &
  21. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  22. # == Sinatra/1.3.5 has taken the stage on 5601 for development with backup from Thin
  23. >> Thin web server (v1.5.0 codename Knife)
  24. >> Maximum connections setto 1024
  25. >> Listening on 0.0.0.0:5601, CTRL+C to stop
  26. 如果ruby的东西都不缺的话,启动会很顺利,ok 现在看看5601端口的状态
  27. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
  28. # lsof -i:5601
  29. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  30. ruby 3116 root 5u IPv4 28947 TCP *:esmagent (LISTEN)


访问一下 试试看 http://192.168.233.128:5601 尝试搜索一下php的错误日志,比如mysql


呵呵,要的就是这个效果,日志会实时的汇总到 logstash index 上供我们查询,当然这只是开始使用logstash的第一步而已,更多的高级功能可以看看官方文档
http://logstash.net/docs/1.1.9/
商品主图商品主图商品主图

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics