将Ohai的数据存入Elasticsearch,并通过Kibana进行配置管理

这篇文章距离最后更新已经过去了1年以上。请注意。

提到Elasticsearch+Kibana,人们普遍知道它可用于可视化时序数据(以logstash格式保存的日志等数据)。

为了在配置管理中使用Elasticsearch,本次将导入ohai的数据,并使其可以在Kibana中进行搜索。

为什么要做呢?

Ohai是Chef用于收集节点信息的库,但也可以单独使用。
通常会将数据集中到Chef-Server上,但在其他环境中丢弃Ohai的数据(Json)可能是一种浪费。

如果直接将Json投入到良好的Elasticsearch中,会发生什么?

把Ohai数据发送到Elasticsearch的方法

以快速为优先,按照以下方针将文档注册到Elasticsearch。

    • Indexはohai

Typeはnode

IDはホスト名(もしくは一意に判別)
ドキュメントは上書き更新

基本上,在每个主机上使用curl命令,以这种方式PUT Ohai数据。

ohai | curl -XPUT http://localhost:9200/ohai/node/`hostname -f` -d @-'
#=> ohai | curl -XPUT http://localhost:9200/ohai/node/santiago.example.com -d @-'

这样一来,Node的属性就能轻松地进行搜索了。

示例环境

我在GitHub上准备了一个示例。

这是必要的环境。

    • java

 

    • ruby, bundler

 

    python

我将使用Rake任务进行以下操作。

    1. 下载Elasticsearch 1.1.1并将其解压到子目录中

 

    1. 下载Kibana 3.1并将其解压到子目录中

 

    1. 启动Elasticsearch(端口:9200)

 

    1. 将示例Ohai数据发布到Elasticsearch

 

    启动用于Kibana的Python SimpleHTTPServer(端口:8000)

安装

使用customink/fauxhai来创建Ohai的模拟。它可以为我生成虚拟的Ohai数据。这也可以单独使用。

$ bundle
Using excon 0.33.0
Using net-ssh 2.9.1
Using ipaddress 0.8.0
Using mime-types 1.25.1
Using mixlib-cli 1.5.0
Using mixlib-config 2.1.0
Using mixlib-log 1.6.0
Using rake 10.1.0
Using mixlib-shellout 1.4.0
Using coderay 1.1.0
Using systemu 2.5.2
Using yajl-ruby 1.2.0
Using method_source 0.8.2
Using slop 3.5.0
Using yard 0.8.7.4
Using webster 0.5.0
Using bundler 1.6.2
Using ohai 7.0.4
Using pry 0.9.12.6
Using pry-doc 0.6.0
Using fauxhai 2.1.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

接下来是耙地。

如果9200端口已被占用,请暂停其使用或修改Rakefile中的Put部分。

$ rake up
Prepare elasticsearch and kibana..
--2014-05-18 17:01:33--  https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.tar.gz

2014-05-18 17:01:54 (1.01 MB/s) - `elasticsearch-1.1.1.tar.gz' saved [19872305/19872305]

x elasticsearch-1.1.1/lib/lucene-codecs-4.7.2.jar
-- snip 
x elasticsearch-1.1.1/config/elasticsearch.yml
--2014-05-18 17:01:54--  https://download.elasticsearch.org/kibana/kibana/kibana-3.1.0.tar.gz

2014-05-18 17:01:58 (302 KB/s) - `kibana-3.1.0.tar.gz' saved [1073033/1073033]

x kibana-3.1.0/app/app.js
-- snip --
x kibana-3.1.0/README.md

Start elasticsearch !!
Waiting es available....
put 30 ohai mocks to elasticsearch
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/gaditan.example.com -d @-' => omnios
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/spiderly.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/martlet.example.com -d @-' => omnios
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/pyemia.example.com -d @-' => fedora
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/discantus.example.com -d @-' => fedora
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/yogasana.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/araneae.example.com -d @-' => amazon
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/tongueman.example.com -d @-' => 
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/sluice.example.com -d @-' => freebsd
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/cupping.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/bayou.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/suterbery.example.com -d @-' => redhat
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/muirfowl.example.com -d @-' => fedora
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/unreached.example.com -d @-' => ubuntu
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/bombastic.example.com -d @-' => 
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/sproutful.example.com -d @-' => windows
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/drubbly.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/proaction.example.com -d @-' => debian
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/langite.example.com -d @-' => amazon
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/arrowless.example.com -d @-' => ubuntu
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/cloisonne.example.com -d @-' => 
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/thiasoi.example.com -d @-' => ubuntu
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/lingy.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/ductor.example.com -d @-' => ubuntu
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/sandproof.example.com -d @-' => oracle
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/tasteless.example.com -d @-' => windows
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/uglifier.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/tsumebite.example.com -d @-' => suse
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/scienced.example.com -d @-' => redhat
Example: 'ohai | curl -XPUT http://localhost:9200/ohai/node/workship.example.com -d @-' => gentoo
Serving HTTP on 0.0.0.0 port 8000 ...

上传样本数据后,Python的Web服务器已经启动。

查看样本数据

打开http://localhost:8000,你会看到Kibana已经启动了。
由于有现有数据,你可以直接使用示例仪表板。

kibana_01.png

当显示出记录列表时,只显示_id字段。

kibana_02.png

注册的样本主机可以直接在列表中确认。

kibana_03.png

请选中您想了解的领域,并添加列进行尝试。
这次我们将显示平台及其版本,以及使用Ohai执行时所使用的Ruby版本(全部都是由fauxhai生成的虚拟数据)。

kibana_04.png

定期在Node上使用Ohai + curl,就可以获取当前的配置信息。

只需稍微调整查询,可以生成这样的图表。

kibana_05.png

追加注释:Elasticsearch查询的输出

创建图表后,可以通过点击检查按钮来输出查询用的Json格式。这是一个用于汇总Node平台的查询。

curl -XGET 'http://localhost:9200/_all/_search?pretty' -d '{
  "facets": {
    "terms": {
      "terms": {
        "field": "platform",
        "size": 100,
        "order": "count",
        "exclude": []
      },
      "facet_filter": {
        "fquery": {
          "query": {
            "filtered": {
              "query": {
                "bool": {
                  "should": [
                    {
                      "query_string": {
                        "query": "*"
                      }
                    }
                  ]
                }
              },
              "filter": {
                "bool": {
                  "must": [
                    {
                      "match_all": {}
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}'

我们在样本节点中插入一些额外的东西,然后尝试用curl进行查询。

$ curl -XGET 'http://localhost:9200/_all/_search?pretty' -d '{
>   "facets": {

-- snip --

> }'

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 111,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "facets" : {
    "terms" : {
      "_type" : "terms",
      "missing" : 7,
      "total" : 104,
      "other" : 0,
      "terms" : [ {
        "term" : "suse",
        "count" : 17
      }, {
        "term" : "fedora",
        "count" : 11
      }, {
        "term" : "amazon",
        "count" : 11
      }, {
        "term" : "smartos",
        "count" : 9
      }, {
        "term" : "debian",
        "count" : 8
      }, {
        "term" : "omnios",
        "count" : 7
      }, {
        "term" : "centos",
        "count" : 7
      }, {
        "term" : "windows",
        "count" : 6
      }, {
        "term" : "openbsd",
        "count" : 6
      }, {
        "term" : "ubuntu",
        "count" : 5
      }, {
        "term" : "oracle",
        "count" : 5
      }, {
        "term" : "freebsd",
        "count" : 5
      }, {
        "term" : "gentoo",
        "count" : 4
      }, {
        "term" : "mac_os_x",
        "count" : 2
      }, {
        "term" : "redhat",
        "count" : 1
      } ]
    }
  }
}

非常容易使用。

停止样品

我会使用Ctrl+C停止Web服务器,并停止ElasticSearch。

$ rake down
Stop elasticsearch...

这样就可以了。

最后

如果你想要添加需要收集的数据,我认为你可以参考一下我的著作《Chef活用指南》,其中详细解释了Ohai的用法。

接下来计划将时间序列类型的数据从Chef-Handler发送到Elasticsearch。

bannerAds