通用技术 日志监控工具 ELK

胡刚 · 2016年03月03日 · 最后由 tammy212 回复于 2018年08月15日 · 2435 次阅读
本帖已被设为精华帖!

1.Elasticsearch 2.0.0 Install

1-1.下载文件到本地:

https://www.elastic.co/downloads/elasticsearch

wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.0.0/elasticsearch-2.0.0.tar.gz

在项目根目录下新建 data 文件夹

#mkdir data

1-2.设置服务 ip&&port

修改文件 elasticsearch.yml

 #pwd
/data2/elasticsearch-2.0.0/config
[root@10 config]# vim elasticsearch.yml 

修改成服务 ip 和 port

network.host: 10.13.1.139
http.port: 54321

1-3.安装插件

在项目根目录下没有 plugins 文件夹,需通过安装插件新建该文件夹,
the core plugins can be installed as follows:

 [root@10 bin]# pwd
/data2/elasticsearch-2.0.0/bin
 [root@10 bin]# ./plugin install analysis-icu       
-> Installing analysis-icu...
Plugins directory [/data2/elasticsearch-2.0.0/plugins] does not exist. Creating...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/analysis-icu/2.0.0/analysis-icu-2.0.0.zip ...
Downloading 

1-4.sudo 用户修改文件为读写执行 (启动服务时,需非 Root 账号)

非 Root 账号启动服务时,会新建文件

[root@10 elasticsearch-2.0.0]# chmod 777 *

1-5.启动服务

切换到非 Root 用户,启动服务

[root@10 ~]# su - hugang
[hugang@10 ~]$ cd /data2/elasticsearch-2.0.0/bin
[hugang@10 bin]$ ./elasticsearch -d

验证服务

[hugang@10 bin]$ curl -X GET http://10.13.1.139:54321
{
  "name" : "Madame Hydra",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.0.0",
    "build_hash" : "de54438d6af8f9340d50c5c786151783ce7d6be5",
    "build_timestamp" : "2015-10-22T08:09:48Z",
    "build_snapshot" : false,
    "lucene_version" : "5.2.1"
  },
  "tagline" : "You Know, for Search"
}

2.Logstash 2.0.0 Install

Logstash:Collect, Enrich & Transport Data;Logstash 负责收集,丰富和传输 log 数据,Pipeline 由三部分组成:

  1. input:指定来源
  2. filter:过滤规则
  3. output:指定输出

在配置文件指定这三个元素,运行 Logstash 时,需在执行脚本 bin/logstash 加上该配置文件作为脚本参数。
logstash pipeline

运行环境要求:JDK minimum version 1.7.0_51

2-1.下载 Logstash

https://www.elastic.co/downloads/logstash

wget https://download.elastic.co/logstash/logstash/logstash-2.0.0.tar.gz

2-2.新建配置文件

根据自己业务需求,新建 Pipeline 配置文件,文件名自定义,但是内容必须含有 input, filter, output 三元素。

collectlogtoredis.conf

input {
    file {
        path => "/data1/weibo8074/logs/exposure.log"
    }
}


filter {
    grok {
        match => { "message" => "(?<uida>[0-9]{10}) (?<uidb>[0-9]{10}) (?<idList>([0-9]{16},){1,}[0-9]{16})" }
    }
}

output {
    redis { host => "10.13.1.139" port => 6379 data_type => "list" key => "logstash:collect:exposurelog" }
}

input: 指定 Logstash 收集数据的来源,支持多种来源,比如:file, stdin, syslog, elasticsearch 等,具体可参照:https://www.elastic.co/guide/en/logstash/current/input-plugins.html

filter: 提取有效信息的规则,支持如下:grok, json, xml, csv 等,具体可参照: https://www.elastic.co/guide/en/logstash/current/filter-plugins.html

output: 将数据发送到特定目的地,支持如下:csv, kafka, syslog, stdout, redis 等,具体可参照: https://www.elastic.co/guide/en/logstash/current/output-plugins.html

collectlogtoredis.conf 分析

  • 指定 input 来源为 file, 格式如下:
file {
    path => ...
}

path 为配置选项(必填), 类型为 array, 可以如下定义, 支持通配符:

path => [ "/var/log/messages", "/var/log/*.log" ]
path => "/data/mysql/mysql.log"

stat_interval 配置选项:How often (in seconds) we stat files to see if they have been modified. 默认 1s.

match 为配置选项,Value type is hash, Default value is {};A hash of matches of field ⇒ value

For example:

filter {
  grok { match => { "message" => "Duration: %{NUMBER:duration}" } }
}

If you need to match multiple patterns against a single field, the value can be an array of patterns

filter {
  grok { match => { "message" => [ "Duration: %{NUMBER:duration}", "Speed: %{NUMBER:speed}" ] } }
}
  • 指定 output 为 redis, 指定 host, port, key 等。

2-3. 启动 Logstatsh 服务

执行 bin/logstash agent -f collectlogtoredis.conf

3.kibana 4.2.1

3-1.下载

https://www.elastic.co/downloads/kibana

wget https://download.elastic.co/kibana/kibana/kibana-4.2.1-linux-x64.tar.gz

3-2.配置 elasticsearch 地址

在 config/kibana.yml 修改成你本地 elasticsearch 服务地址

elasticsearch.url: "http://10.13.1.139:54321"

3-3.启动服务

./bin/kibana

访问 kibana 服务:

ip:5601

具体配置请参照:https://www.elastic.co/webinars/getting-started-with-kibana?baymax=rtp&elektra=blog&iesrc=ctr

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 12 条回复 时间 点赞

Linux 下的, 非常详细,先留下了

这个分析工具,移动应用测试,能用上不?

#2 楼 @t880216t ELK 由 3 个部件构成,主要是用来根据日志中心监控不同服务;如果是根据日志进行测试,可以使用 Logstash 部件进行日志分析,根据你的日志获取要断言的数据,key:value 形式存在 redis 等介质,get(key) 与预期校验; 你说的移动应用测试,具体是测试什么数据?

期待更好的实战~~~看看能不能一个系列

#3 楼 @neven7 我们是做穿戴设备的,通过蓝牙连接。当前项目安卓主要是通过问题点的 adb log、apk 内的 debug log 分析,IOS 主要是 system log、crash log。蓝牙的问题还需要 HCI log 分析、抓空中包等等。期待你更多的 demo。

胡刚 #14 · 2016年03月04日 Author

#4 楼 @monkey 好的,以后有机会弄一弄

持续学习中。。

楼主,其实你不用这么麻烦的,Kibana 可以作为 es 的一个插件存在,换而言之就是我们只需要搭建 es+logstash 即可,然后在 es 中安装 Kibana 的插件即可,如果有大数据分析的话,加上 Redis 即可。

速度保存,稍后马上去试试

思寒_seveniruby [该话题已被删除] 中提及了此贴 07月25日 09:46
12楼 已删除

赞一个

@neven7 你好,我们很多接口测试是需要通过看日志进行的。现在准备进行接口自动化,那能通过这个日志监控来达到接口自动化的断言吗?谢谢

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册