xuyangus 发表于 2018-11-6 08:52:25

Centos 6.5 安装nginx日志分析系统 elasticsearch + logstash + redis + kibana

  转载:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=17291169&id=4898582
  随着业务的增长,web服务器的增加,网站规模扩张,作为系统管理员需要分析网站的访问情况,在应用层方面,我们可以嵌入js来统计网站的pv 独立ip,回头率,访问区域热点图等,常见的有piwiki ,cnzz站长数据统计,在系统管理层方面常见的nginx 日志分析工具有很多,goAccess,awstats..这里主要介绍如何在centos 6.5 上面安装ELK,以及logstash的grok,mutate,进入正题
  192.168.1.49 # redis 服务器,角色broker
  192.168.1.139 # logstash 角色 indexer 服务器,集成elasticsearch, kibana,必须有安装web服务
  192.168.1.65# nginx服务器,角色生产服务器,logstash需要收集它的日志
  安装logstash-1.4.2
  点击(此处)折叠或打开

[*]  #yum -y install java-1.7.0-openjdk
[*]  #wget https://download.elasticsearch.org/logstash/logstash/logstash-1.4.2.tar.gz
[*]  #tar xzvf logstash-1.4.2.tar.gz -C /app/ && mv logstash-1.4.2 logstash
[*]  #mkdir-p /app/logstash/conf
  测试安装
  点击(此处)折叠或打开

[*]  # ./logstash -e 'input { stdin { } } output { stdout {} }'
  输入“hello,world”, 如果出现类似下图,说明logstash正常工作
http://blog.chinaunix.net/attachment/201503/16/17291169_1426499816KKM3.png
  下一步,安装 elasticsearch-1.4.2
  点击(此处)折叠或打开

[*]  #wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.tar.gz
[*]  #tar xzvfelasticsearch-1.4.2.tar.gz -C /app/
[*]  #cd /app/elasticsearch-1.4.2/config
  修改elasticsearch配置文件elasticsearch.yml,并且修改以下记录
  点击(此处)折叠或打开

[*]  discovery.zen.ping.multicast.enabled: false#关闭广播,如果局域网有机器开9300 端口,服务会启动不了
[*]  network.host: 192.168.1.139                  #指定主机地址,其实是可选的,但是最好指定因为后面跟kibana集成的时候会报http连接出错(直观体现好像是监听了:::9200 而不是0.0.0.0:9200)
[*]  http.cors.allow-origin: "/.*/"
[*]  http.cors.enabled: true                      #这2项都是解决跟kibana集成的问题,错误体现是 你的 elasticsearch 版本过低,其实不是
  启动elasticsearch
  点击(此处)折叠或打开

[*]  #./elasticsearch   # 配置阶段建议直接启动,日志会输出到stdout,-d 选项表示以daemon的方式启动,如果没有出现error ,表示服务正常启动
  测试logstash 跟elasticsearch数据交互
  点击(此处)折叠或打开

[*]  #bin/logstash -e 'input { stdin { } } output { elasticsearch { host => 192.168.1.139 } }'
[*]  输入you know, for logs
[*]  # curl 'http://192.168.1.139:9200/_search?pretty' # 如果有输出且没有出现错误表示服务器交互成功
安装kibana  点击(此处)折叠或打开

[*]  #cd /app/logstash/vendor
[*]  #vim kibana/config.js   #elasticsearch: "http://"+window.location.hostname+":9200",修改成"http://192.168.1.139:9200"
[*]  #cp -Rvkibana/path/to/wwwroot
访问url http://192.168.1.139/kibana/index.html 不报错表示OK  安装redis-server(192.168.1.49)
  点击(此处)折叠或打开

[*]  #tar xzvf redis-2.6.16.tar.gz -C /app
[*]  #cd /app/redis-2.6.16 && mkdir conf
[*]  #make target=linux26
[*]  #./src/redis-server redis.conf# daemonize yes 使用默认的配置文件
集成logstashredis(192.168.1.139)  点击(此处)折叠或打开

[*]  #vim /app/logstash/conf/nginx_acces.conf # 如下内容
[*]  input {
  redis {
  host => '192.168.1.49'# 我方便测试没有指定password,最好指定password
  data_type => 'list'
  port => "6379"
  key => 'logstash:redis' #自定义
  type => 'redis-input'   #自定义
  }
  }
  output {
  elasticsearch {
  host => "192.168.1.139"
  codec => "json"
  protocol => "http"#版本1.0+ 必须指定协议http
  }
  }
验证配置文件  点击(此处)折叠或打开

[*]  #bin/logstash -f nginx_access.conf -t# 无误后启动
[*]  #bin/logstash -f nginx_access.conf--verbose # 要检查错误 --debug
http://blog.chinaunix.net/attachment/201503/17/17291169_14265595530I9j.png  安装logstash 日志入口节点(192.168.1.65),logstash 安装方式和139上面雷同,主要是配置文件nginx_access.conf
  点击(此处)折叠或打开

[*]  input {
[*]  file {
[*]  type => "nginx_access"
[*]  path => "/app/nginx/logs/test.log"
[*]  }
[*]  }
[*]
[*]
[*]  output {
[*]  stdout { codec => rubydebug }
[*]  redis {
[*]  host => '192.168.1.49'
[*]  data_type => 'list'
[*]  key => 'logstash:redis'
[*]  }
[*]  }
测试节点跟redis的交互,如图所示http://blog.chinaunix.net/attachment/201503/17/17291169_1426560456xDn2.png
  redis服务器上面如图
http://blog.chinaunix.net/attachment/201503/17/17291169_1426560495MwnM.png
  OK,没有问题,下一步如何用logstash 分析nginx 访问日志
  ==============================================================
  logstash 的工作流程分为3个核心部分,inputfilter output,input 事件定义数据来源,filter 定义如何处理数据流,output顾名思义输出到哪儿,常见的工作是如何格式化输出日志
  大部分都是用filter的grok,mutate,grok 按官方的解释是格式化日志输出方便以后查询,是按照预先定义的pattern 解析日志,mutate 用的最多是修改日志,格式化“filed”,
  如图是未经格式化的nginx日志
http://blog.chinaunix.net/attachment/201503/17/17291169_1426561518vhVc.png
  经过格式化后日志
http://blog.chinaunix.net/attachment/201503/17/17291169_1426561983JPXV.png
  设定NGINX 访问grok
  点击(此处)折叠或打开

[*]  #cd /app/logstash/patterns
[*]  #vim nginx#内容如下,本例只针对linux的默认访问日志
[*]  NGUSERNAME +
  NGUSER %{NGUSERNAME}
  NGINXACCESS %{IPORHOST:remote_addr} - - \[%{HTTPDATE:time_local}\] "%{WORD:method} %{URIPATH:path}(?:%{URIPARAM:param})? HTTP/%{NUMBER:httpversion}" %{INT:status} %{INT:body_bytes_sent} %{QS:http_referer} %{QS:http_user_agent}
  #NGINXACCESS %{IPORHOST:remote_addr} - - \[%{HTTPDATE:time_local}\] "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{INT:status} %{INT:body_bytes_sent} %{QS:http_referer} %{QS:http_user_agent}
[*]  #chown 1002:1002 nginx # 修改文件属组,否则无法加载pattern
  关于pattern的debug 可以用官网推荐的线上debug工具 https://grokdebug.herokuapp.com/,附图
http://blog.chinaunix.net/attachment/201503/17/17291169_1426563217D3JY.png
  修改logstash nginx_access配置文件,内容如下
  点击(此处)折叠或打开

[*]  input {
[*]  file {
[*]  type => "nginx_access"
[*]  path => "/app/nginx/logs/test.log"
[*]  }
[*]  }
[*]  #input { stdin { } }#方便测试
[*]
[*]  filter {
[*]  grok {
[*]  match => { "message" => "%{NGINXACCESS}" }
[*]  }
[*]  #mutate {
[*]  #gsub => ["param","\?",""]
[*]  #split => ["request" ,"?"]
[*]  #add_field => ["params", "%{request}"] #split 数组取值
[*]  #remove_field => ["request"]
[*]  # }
[*]  # date {
[*]  # match => [ "time_local" , "dd/MMM/yyyy:HH:mm:ss Z" ]
[*]  # }
[*]
[*]  }
[*]
[*]
[*]  output {
[*]  stdout { codec => rubydebug }
[*]  redis {
[*]  host => '192.168.1.49'
[*]  data_type => 'list'
[*]  key => 'logstash:redis'
[*]  }
[*]  }
  附上kibana 展示图一张
http://blog.chinaunix.net/attachment/201503/17/17291169_14265635627iLS.png

页: [1]
查看完整版本: Centos 6.5 安装nginx日志分析系统 elasticsearch + logstash + redis + kibana