翻译,原文地址:https://aggarwalarpit.wordpress.com/2015/12/03/configuring-elk-stack-to-analyse-apache-tomcat-logs/
Posted on December 3, 2015 by Arpit Aggarwal
在这篇文章,我将安装ElasticSearch, Logstash and Kibana分析Apache Tomcat服务日志。在安装之前,对各个组件做个简介!
下面,我将开始分别安装技术栈中的每一个组件,下面是步骤:
下载并解压 ElasticSearch .tar.gz 到一个目录下, 我下载的是 elasticsearch-2.1.0.tar.gz 并且以elasticsearch为文件名解压到/Users/ArpitAggarwal/目录下面
在bin目录下以./elasticsearch启动elasticsearch服务,如下:
$ cd /Users/ArpitAggarwal/elasticsearch/elasticsearch-2.1.0/bin
$ ./elasticsearch
上面的命令启动的elasticsearch可以通过 http://localhost:9200/ 访问,默认的索引访问地址 http://localhost:9200/_cat/indices?v
如果删除索引(所有),通过如下命令行:
curl -XDELETE 'http://localhost:9200/*/'
下面,我们安装配置kibana,指向我们的ElasticSearch实例,同样需要下载并解压.tar.gz到目录下,我的是kibana-4.3.0-darwin-x64.tar.gz,以kibana为文件名解压到/Users/ArpitAggarwal/
修改/Users/ArpitAggarwal/kibana/kibana-4.3.0-darwin-x64/config/kibana.yml配置,指向本地的ElasticSearch实例,替换elasticsearch.url的值为http://localhost:9200
通过bin目录下的./kibana启动kibana,如下:
$ cd /Users/ArpitAggarwal/kibana/kibana-4.3.0-darwin-x64/bin
$ ./kibana
可以通过 http://localhost:5601/ 访问kibana
下面,我们安装配置Nginx,指向我们的Kibana实例。同样需要下载并解压.tar.gz到目录下,我的是nginx-1.9.6.tar.gz,以nginx为文件名解压到/Users/ArpitAggarwal/,命令如下:
$ cd nginx-1.9.6
$ ./configure
$ make
$ make install
默认情况下,Nginx将被安装到/usr/local/nginx,但是Nginx提供了指定目录安装的方法,使用--prefix选项。如下:
./configure --prefix=/Users/ArpitAggarwal/nginx
Next, open the nginx configuration file at /Users/ArpitAggarwal/nginx/conf/nginx.conf and replace location block under server with below content: 下面,我们打开配置文件/Users/ArpitAggarwal/nginx/conf/nginx.conf,然后替换location段中的配置,内容如下:
location / {
# 指向kiban本地实例
proxy_pass http://localhost:5601;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
启动 Nginx, 如下:
cd /Users/ArpitAggarwal/nginx/sbin
./nginx
可以通过 http://localhost 访问nginx
下面,我们安装Logstash,执行如下命令:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
brew install logstash
上面的命令安装 Logstash 到 /usr/local/opt/。(译者注:此安装方法较特殊,建议参考官网方法)
我们需要让Logstash将数据从tomcat server日志目录送到ElasticSearch。创建一个目录,我们将创建logstash配置文件,我将它放到/Users/ArpitAggarwal/下,如下:
cd /Users/ArpitAggarwal/
mkdir logstash patterns
cd logstash
touch logstash.conf
cd ../patterns
touch grok-patterns.txt
将如下内容复制到:logstash.conf:
input {
file {
path => "/Users/ArpitAggarwal/tomcat/logs/*.log*"
start_position => beginning
type=> "my_log"
}
}
filter {
multiline {
patterns_dir => "/Users/ArpitAggarwal/logstash/patterns"
pattern => "\[%{TOMCAT_DATESTAMP}"
what => "previous"
}
if [type] == "my_log" and "com.test.controller.log.LogController" in [message] {
mutate {
add_tag => [ "MY_LOG" ]
}
if "_grokparsefailure" in [tags] {
drop { }
}
date {
match => [ "timestamp", "UNIX_MS" ]
target => "@timestamp"
}
} else {
drop { }
}
}
output {
stdout {
codec => rubydebug
}
if [type] == "my_log" {
elasticsearch {
manage_template => false
host => localhost
protocol => http
port => "9201"
}
}
}
下面,将https://github.com/elastic/logstash/blob/v1.2.2/patterns/grok-patterns的内容复制到 patterns/grok-patterns.txt
使用如下命令,检查logstash的配置
$ cd /usr/local/opt/
$ logstash -f /Users/ArpitAggarwal/logstash/logstash.conf --configtest --verbose —debug
启动Logstash,使数据送到ElasticSearch
$ cd /usr/local/opt/
$ logstash -f /Users/ArpitAggarwal/logstash/logstash.conf
喜欢 (2)or分享 (0)