Era Software

Writing data with Telegraf

Estimated reading time: 3 minutes

This page shows how to use Telegraf to write real-time data to EraSearch. In this guide, you'll do the following:

  • Use Telegraf to collect data about specific files
  • Configure Telegraf to write the data to EraSearch
  • Query the data using the EraSearch REST API

While the steps below use Telegraf's Filecount input plugin, you can customize the setup to use any Telegraf input plugin.

Before you begin

This guide assumes you're using self-hosted EraSearch and you've installed Telegraf. If you'd like to get started with self-hosted EraSearch, contact us at Era Software.

The steps below also assume you've installed jq, a JSON parser for the command line.

Instructions

Step 1: Configure the Filecount input plugin

Telegraf's Filecount input plugin tracks the size and number of files in directories. Follow these steps to set it up:

  1. Open your Telegraf configuration file
  2. Paste in the content below
  3. Replace YOUR_FILE_PATH with the path to the directory you want to monitor
## Filecount input plugin
[[inputs.filecount]]
  directories = ["YOUR_FILE_PATH"]

Note: If you don't have a Telegraf configuration file, go to your telegraf directory and enter the command below.

The command outputs telegraf.conf including the two plugins you need for this guide: Filecount and Elasticsearch.

$ telegraf -sample-config --input-filter filecount --output-filter elasticsearch > telegraf.conf

Step 2: Configure the EraSearch output plugin

To configure Telegraf to send data to EraSearch, paste the content below in your Telegraf configuration file, replacing:

  • YOUR_ERASEARCH_URL with your EraSearch hostname
    • Example: http://localhost:9200
  • YOUR_USERNAME and YOUR_PASSWORD with your EraSearch credentials
  • YOUR_INDEX_NAME with the target EraSearch index -- EraSearch creates the index for you
[[outputs.elasticsearch]]
  urls = [ "YOUR_ERASEARCH_URL" ]
  timeout = "5s"
  enable_sniffer = false
  enable_gzip = false
  health_check_interval = "0s"
  username = YOUR_USERNAME
  password = YOUR_PASSWORD

  index_name = "YOUR_INDEX_NAME"
  manage_template = false

Note: This step uses the Elasticsearch output plugin to let Telegraf work with EraSearch. That workflow is possible because the EraSearch REST API supports much of the Elasticsearch API.

Step 3: Start Telegraf and confirm your configuration

Start Telegraf with the relevant command. When Telegraf loads the Filecount and Elasticsearch plugins, it shows output like this:

2021-12-22T19:43:54Z I! Starting Telegraf x.x.x
2021-12-22T19:43:54Z I! Loaded inputs: filecount
2021-12-22T19:43:54Z I! Loaded aggregators:
2021-12-22T19:43:54Z I! Loaded processors:
2021-12-22T19:43:54Z I! Loaded outputs: elasticsearch
2021-12-22T19:43:54Z I! Tags enabled: host=Era-Software.local
2021-12-22T19:43:54Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"Era-Software.local", Flush Interval:10s
2021-12-22T19:43:54Z I! Elasticsearch version: 7.10.2

Step 4: Query your data in EraSearch

Use the EraSearch REST API to query the Telegraf data in EraSearch. Paste this cURL command in your terminal, replacing:

  • YOUR_ERASEARCH_URL with your EraSearch hostname
    • Example: http://localhost:9200
  • YOUR_INDEX_NAME with the EraSearch index you specified above
$ curl 'YOUR_ERASEARCH_URL/YOUR_INDEX_NAME/_search?q=_lid:*' | jq

The response shows information about your data and API request, including:

  • took - The time, in milliseconds, EraSearch took to serve the query request
  • _id - A unique, auto-generated numerical identifier for documents
  • count - The number of files in the specified directory
{
  "took": 0,
  "timed_out": false,
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "my_era_files",
        "_id": "8157407908185636864",
        "_score": 1,
        "_source": {
          "@timestamp": "2021-12-22T14:12:20-06:00",
          "filecount": {
            "count": 3905,
            "size_bytes": 1407199699
          },
          "measurement_name": "filecount",
          "tag": {
            "directory": "/File/path",
            "host": "Era-Software.local"
          },
          "_ts": 1640203941294,
          "_lid": 8157407908185637000
        }
      }
    ]
  }
}

Next steps

You're all set! You're now using Telegraf to send real-time log data to your self-hosted EraSearch instance. To learn more about writing data to EraSearch, visit these guides:

For more information about Telegraf, visit these pages: