Logstash:運用 makelogs 創建測試日誌

我們在使用 Elasticsearch 時,經常想找一些數據來進行測試,比如我們想試一下 Kibana 的可視化工具。我們有時想有很多的數據,但是有時就是找不到合適的數據。那麼我們該怎麼辦呢?當然,我們有一種簡單的辦法就是使用 Kibana 給我們提供的測試數據,但是它可能也有很多的侷限性:

 

另外一種方法就是使用一些工具來生成我們想要數量或要求的數據。在今天的文章中,我們就來介紹這樣的一個工具 makelogs

 

下載 makelogs

我們在地址 https://github.com/elastic/makelogs 下載 makelogs:

git clone https://github.com/elastic/makelogs
cd makelogs

按照項目的要求,我們進行如下的安裝:

npm install -g @elastic/makelogs

在實際使用中,我發現還需要安裝另外的兩個模塊:

npm install update-notifier
npm install commander --save

這樣我們的安裝就完成了。

 

運行 makelogs

接下來,我們就可以運行 makelogs。爲了能夠了解這個工具的作用,我們使用如下的命令:

./bin/makelogs --help
$ ./bin/makelogs --help
Usage: makelogs [options]

A utility to generate sample log data.

Options:
  -c, --count <number>       Total event that will be created, accepts
                             expressions like "1m" for 1 million (b,m,t,h)
                             (default: 14000)
  -d, --days <number>        Number of days ± today to generate data for. Use
                             one number or two separated by a slash, e.g.
                             "1/10" to go back one day, and forward 10
                             (default: 1)
  --url <url>                Elasticsearch url, overrides host and auth, can
                             include any url part.
  -h, --host <host>          The host name and port (default:
                             "localhost:9200")
  --auth <auth>              user:password when you want to connect to a
                             secured elasticsearch cluster over basic auth
                             (default: null)
  --indexPrefix <name>       Name of the prefix of the index (default:
                             "logstash-")
  -s, --shards <number>      The number of primary shards (default: 1)
  -r, --replicas <number>    The number of replica shards (default: 0)
  --dry                      Test/Parse your arguments, but don't actually do
                             anything
  --reset                    Clear all {prefix}-* indices and the makelogs
                             index template before generating (default:
                             prompt)
  --no-reset                 Do not clear all {prefix}-* indices and the
                             makelogs index template before generating
                             (default: prompt)
  --verbose                  Log more info to the console
  --trace                    Log every request to elasticsearch, including
                             request bodies. BE CAREFUL!
  -o, --omit <...>           Omit a field from every event. See "formatting an
                             omit path"
  -i, --indexInterval <...>  The interval that indices should roll over,
                             either "daily", "monthly", "yearly", or a number
                             of documents. (default: 100000)
  --types                    Pass to enable types in index and document
                             creation
  --indexTemplatesV1         Pass to enable types in index templates v1
                             compatibility
  -V, --version              output the version number
  --help                     This help message

formatting an omit path
=======================

To remove a single top level property, you can simply specify the name of the property you want to remove:

  $ makelogs --omit request

To remove multiple properties, simply use omit multiple times

  $ makelogs --omit request --omit referrer

You can use dot notation to access properties of object values

  $ makelogs --omit machine.os

Or, you can omit the entire object value

  $ makelogs --omit machine

Finally, to traverse into arrays/nested fields you must use the [] operator

  $ makelogs --omit relatedContent[].title

上面顯示了這個命令的所有的幫助信息。

接下來我們使用上面的命令來生成我們想要的 logstash 日誌:

makelogs -c 10

等上面的命令運行完後,我們在 Kibana 裏進行檢查:

GET _cat/indices

我們看到一個新生成的叫做 logstash-0 的索引。我們使用如下的命令來檢查文檔的個數:

GET logstash-0/_count

上面顯示共有 11 個文檔生成。

我們可以對這個索引進行搜索:

我們可以看到這些被 makelogs 所生成的 logstash 文檔。這些文檔可以成爲我們對數據分析的一個基礎供我們進行練手。在生產環境中,當然不能進行使用了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章