Skip to content

Writing data with Telegraf

Estimated time to read: 5 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.
  • View the data in EraSearch.

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 content is intended for engineers and developers using EraSearch on EraCloud or self-hosted EraSearch:

This page also assumes you've installed Telegraf version 1.21.4+ and 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 this content:
    ## Filecount input plugin
    [[inputs.filecount]]
      directories = ["YOUR_FILE_PATH"]
    
  3. Replace YOUR_FILE_PATH with the path to the directory you want to monitor.

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_SERVICE_URI with your EraCloud service URI.
  • YOUR_API_KEY with your EraCloud API key.
  • YOUR_INDEX_NAME with the target EraSearch index -- EraSearch creates the index for you.
[[outputs.elasticsearch]]
  urls = [ "YOUR_SERVICE_URI" ]
  timeout = "5s"
  enable_sniffer = false
  enable_gzip = false
  health_check_interval = "0s"
  auth_bearer_token = "YOUR_API_KEY"

  index_name = "YOUR_INDEX_NAME"
  manage_template = false

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

  • YOUR_ERASEARCH_URL with your EraSearch URL.

    Example: http://localhost:9200.

  • YOUR_API_KEY with your EraSearch RBAC API key. If you're not using RBAC, remove auth_bearer_token = "YOUR_API_KEY" from the file.

  • 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"
  auth_bearer_token = "YOUR_API_KEY"

  index_name = "YOUR_INDEX_NAME"
  manage_template = false

Note

The configuration above 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: View your data in EraSearch

Access EraSearch's UI by visiting your EraCloud account and clicking search icon. Your logs are in the index you specified above.

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

  • YOUR_ERASEARCH_URL with your EraSearch URL.

    Example: http://localhost:9200.

  • YOUR_INDEX_NAME with the EraSearch index you specified above.

  • YOUR_API_KEY with your EraSearch RBAC API key. If you're not using RBAC, remove -H 'Authorization: Bearer YOUR_API_KEY' from the command.
$ curl 'YOUR_ERASEARCH_URL/YOUR_INDEX_NAME/_search?q=_lid:*' \
  -H 'Authorization: Bearer YOUR_API_KEY' | 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 EraSearch. For more information about Telegraf, visit these pages:

For other ways to get data into your database, visit the write-integrations reference. To learn more about exploring, querying, and visualizing your data in EraSearch, visit these pages:


Last update: September 27, 2022