惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Privacy & Cybersecurity Law Blog
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
爱范儿
爱范儿
博客园 - 司徒正美
量子位
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园_首页
月光博客
月光博客
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Hacker News: Ask HN
Hacker News: Ask HN
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
H
Heimdal Security Blog
小众软件
小众软件
Attack and Defense Labs
Attack and Defense Labs
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cloudbric
Cloudbric
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Forbes - Security
Forbes - Security
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
W
WeLiveSecurity
IT之家
IT之家
U
Unit 42
H
Hacker News: Front Page
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
S
Schneier on Security
F
Fortinet All Blogs
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs

博客园 - Danny Chen

python asyncio 获取协程返回值和使用callback c#等待所有子线程执行完毕方法 Python virtualenv 查看当前正在运行的python进程 Mac 删除/卸载 自己安装的python python读取txt文件最后一行(文件大+文件小) pycharm 注册码/License server 2017年最新 Linux cat命令详解 在Mac平台上安装配置ELK时的一些总结 C#中用schema验证xml的合法性 C#中XML与对象之间的序列化、反序列化 C#位运算 SQL Server 权限管理 如何用C#动态编译、执行代码 [C#]手把手教你打造Socket的TCP通讯连接(一) C#动态调用WCF接口,两种方式任你选。 动态调用WCF服务 矩阵的坐标变换(转) 【.NET线程--进阶(一)】--线程方法详解
Mac上搭建ELK
Danny Chen · 2017-10-20 · via 博客园 - Danny Chen

转载自我的个人博客:http://blog.ywheel.cn/post/2017/03/04/setup_elk_on_mac/

最近的项目需要对文本数据各字段进行快速检索、组合查询、模糊查询,在架构选择上选择了Elasticsearch作为支撑这些功能的存储和搜索引擎。其他的不说了,刚好我的第一台mac到了,直接搞起。

什么是ELK

日志分析平台可以有多种技术架构的选型,但经过了多年的演变,现在比较流行的应该就是ELK了。 ELK三个字母的意义如下:

  1. Elasticsearch
  2. Logstash
  3. Kibana

架构图下图:

ELK架构图

图中的Shipper和Indexer都可以是Logstash, Broker一般为Redis,也可以是kafka等。而Search & Storage则主要是Elasticsearch了,一方面接收上游index好的文档,另一方面提供API支持对内容的检索。而kibana则是一个web interface, 可以提供简单易用的界面让用户方便的写出搜索的表达式来访问Elasticsearch.

对于这三部分都有很多深入的点,以后有机会要深入学习和记录。

使用brew安装

既然使用了mac,那么使用brew安装程序则是一个最简单不过的方式了。

首先安装Elasticsearch,直接输入以下命令即可:

brew install elasticsearch
  • 1

但可能遇到问题,比如要求Java的版本是1.8(我安装的Elasticsearch的按本是5.2.2),这里面可能涉及到还要安装brew cast用来安装java8, 然后又提示还有其他依赖(后悔没记录下来。。。),而需要xcode-command-tool, 折腾了不少时间。

安装完成后,可以查看elasticsearch的版本:

$ elasticsearch --
  • 1
  • 2

启动和停止elasticsearch也很简单:

brew services start elasticsearch
brew services stop elasticsearch
  • 1
  • 2

浏览器访问http://localhost:9200可以看到Elasticsearch的信息:

{
name: "bWXgrRX",
cluster_name: "elasticsearch_ywheel",
cluster_uuid: "m99a1gFWQzKECuwnBfnTug",
version: {
number: "5.2.2",
build_hash: "f9d9b74",
build_date: "2017-02-24T17:26:45.835Z",
build_snapshot: false,
lucene_version: "6.4.1"
},
tagline: "You Know, for Search"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

接着安装logstash:

brew install logstash
  • 1

安装好后查看版本:

$ logstash --
  • 1
  • 2

浏览器访问http://localhost:9600可以看到如下信息:

{
host: "ywheeldeMacBook-Pro.local",
version: "5.2.2",
http_address: "127.0.0.1:9600",
id: "70b78f4a-fe0f-4187-bf71-fe1f60b74e0a",
name: "ywheeldeMacBook-Pro.local",
build_date: "2017-02-24T17:46:55Z",
build_sha: "57984d20eb28b0df40a59077c600ec1a399d46f5",
build_snapshot: false
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Kibana不需要通过brew安装,直接下载压缩包后,解压后执行./kibana即可。不过我还是在/usr/local/bin/下创建了kibanakibana-plugin的软连接, elasticsearch,elasticsearch-plugin,logstashlogstash-plugin都在这个目录下,以后安装插件的话,还都需要用上这些*-plugin.

Kibana安装完成后,需要在config/kibana.yml文件中,确认elasticsearch.url: "http://localhost:9200"

测试写入和查询

写入Elasticsearch可以很简单,其本身就提供了RESTFul的API接口,参考https://www.elastic.co/guide/en/kibana/3.0/import-some-data.html ,通过以下命令创建shakespeare index:

curl -XPUT http://localhost:9200/shakespeare -d '
{
 "mappings" : {
  "_default_" : {
   "properties" : {
    "speaker" : {"type": "string", "index" : "not_analyzed" },
    "play_name" : {"type": "string", "index" : "not_analyzed" },
    "line_id" : { "type" : "integer" },
    "speech_number" : { "type" : "integer" }
   }
  }
 }
}
';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

通过以下命令将数据写入Elasticsearch:

curl -XPUT localhost:9200/_bulk --data-binary @shakespeare.json
  • 1

数据写入后,到kibana目录运行./kibana,启动后访问:http://localhost:5601/ , 看到kibana界面后会提示”Configure an index pattern”。, 如下图:

kibana 1

刚才在写入数据的时候已经创建了shakespeare index, 且不是按照时间分布的日志文件(shakespeare只有一个json文件),因此,取消勾选Index contains time-based envents,输入shakespeare后,就能看到create按钮了。

kibana 2

点击kibana的Discover页面,输入WESTMORELAND查询,可以看到有110个结果:

kibana 3

监控和安全

在Elasticsearch 5.x的时代,监控和管理由X-Pack统一完成,包含:

  1. 安全:用户权限管理
  2. 告警:自动告警
  3. 监控:监控Elasticsearch集群的状态
  4. 报告:发送报告、导出数据
  5. 图表:可视化数据

在安装X-Pack之前,需要停止Kibana和Elasticsearch:

elasticsearch-plugin install x-pack
kibana-plugin install x-pack
  • 1
  • 2

安装完成后,启动elasticsearch和kibana,访问kibana时发现需要登录了, 默认用户名和密码是elastic/changeme。

kibana login

后续可以在Management面板中进行用户和角色的配置,也可以看到新增了Reporting。

kibana management

在Monitoring页面中可以看到Elasticsearch和Kibana的状态,点击Indices还可以看到具体索引的状态。

kibana monitoring 1

kibana monitoring 1

告警功能和报表功能后续再进行详细研究。之前在A家的时候,记得有个基于日志的告警功能:当service的日志中出现了ERROR或FATAL,可以自动触发告警。有了X-Pack后,这个功能应该也是可以通过ELK来实现的啦。

通过访问http://localhost:9200/_cat/indices?v查看Elasticsearch的Index, 可以发现几个新的与监控有关的index:

health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .monitoring-es-2-2017.03.04     COZvO_dlSkqdEtntrZrzFA   1   1      10240          154      4.5mb          4.5mb
green  open   .security                       XEeHRF5NT0ud2jpxOzsoHw   1   0          1            0      2.8kb          2.8kb
yellow open   .kibana                         p-cJGBCXQySNGR0924jRdQ   1   1          2            1      9.8kb          9.8kb
yellow open   .monitoring-data-2              QZt0hpTISUO_58pWoG5Hyw   1   1          3            0      6.9kb          6.9kb
yellow open   .monitoring-kibana-2-2017.03.04 nLHKuL1KTiCE2lsWz8tdkA   1   1        849            0      245kb          245kb
yellow open   shakespeare                     zPCLp4KmTkiu7m4tYcA_Iw   5   1     111396            0     28.1mb         28.1mb
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用Logstash导入博文

在上面的操作中,直接使用了elasticsearch的API接口来进行数据的导入,而使用logstash也能够很方便的写入elasticsearch。 首先得生成一个logstash的conf文件,比如我想建立我的博客的索引,在我的家目录下创建了my_blog.conf文件:

input{
    file{
        path => ["/Users/ywheel/my_blog/content/about.md"]
    }
}   
output{
    elasticsearch{
        hosts => ["localhost:9200"]
        index => "my_blog"
        user => "elastic"
        password => "changeme"
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注意拜X-Pack所赐,这配置文件里面对elasticsearch需要用户名和密码。然后敲入logstash -f my_blog.conf来执行,但却一直不成功。后来翻logstash的文档 https://www.elastic.co/guide/en/logstash/current/pipeline.html ,里面写了一句这样的话:

Inputs

You use inputs to get data into Logstash. Some of the more commonly-used inputs are:

- file: reads from a file on the filesystem, much like the UNIX command tail -0F
- syslog: listens on the well-known port 514 for syslog messages and parses according to the RFC3164 format
- redis: reads from a redis server, using both redis channels and redis lists. Redis is often used as a "broker" in a centralized Logstash installation, which queues Logstash events from remote Logstash "shippers".
- beats: processes events sent by Filebeat.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

file这个input相当于使用tail来获取文件中的数据的啊, 我的about.md压根就没有变化,因此也没有内容被写入了Elasticsearch。于是,我把conf改成了这样:

input{
    file{
        path => ["/Users/ywheel/test.md"]
    }
}   
output{
    elasticsearch{
        hosts => ["localhost:9200"]
        index => "my_blog"
        user => "elastic"
        password => "changeme"
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

运行logstash -f my_blog_conf后,再运行cat /Users/ywheel/my_blog/content/about.md > /Users/ywheel/test.md, 然后发现数据写入了Elasticsearch, index也多了一个my_blog。到Kibana中添加my_blog这个index pattern后,就可以在Discover进行搜索了。比如我搜索“程序员”:

kibana search

看来中文分词得改进一下,不过现在也已经很酷了! 以后可以对整个博客进行全文检索了~~