Jump to contents

User Guide for GridDB Telegraf Output Plugin

Revision: 1.6.0-10082-768e556f

1 Overview

The GridDB output plugin for Telegraf is a Telegraf output plugin to insert data into GridDB database using the HTTP/HTTPS method.

2 Installation

2.1 Requirements

Building of the plugin and execution have been tested in the following environment:

  • Operation system: CentOS 7 or higher
  • Go version 1.15.13 or higher
  • Telegraf version 1.18 release
  • InfluxDB v2
  • Web API version: GridDB version 4.5.0

2.2 Install Go

Refer to https://linuxize.com/post/how-to-install-go-on-centos-8/ or https://golang.org/doc/install to set up and install Go.

Alternatively, you can install Go on CentOS by using yum Refer to https://www.cyberithub.com/install-go-on-centos/ .:

$ yum install golang

Verify that you have installed Go by opening a command prompt and typing the following command:

$ go version

2.3 Install the GridDB output plugin for Telegraf

2.3.1 Build instructions

Step 1: Download the source code of the GridDB output plugin for Telegraf.

Step 2: Download the telegraf source code through go.

$ go get -d "github.com/influxdata/telegraf"

Step 3: Copy the source code of the GridDB output plugin for Telegraf to the telegraf root directory.

$ cp -R ./plugins  ~/go/src/github.com/influxdata/telegraf

Step 4: Register the GridDB output plugin in Telegraf by adding the below line to the import section of the file ../telegraf/plugins/outputs/all/all.go:

_ "github.com/influxdata/telegraf/plugins/outputs/griddb"

Step 5: Run the following commands to build Telegraf.

$ cd ~/go/src/github.com/influxdata/telegraf
$ make telegraf

2.3.2 Running Telegraf

The following example performs data transfer from InfluxDB v2 to GridDB:

  1. Preparation:

    $ cd ./sample

    The sample directory contains a telegraf configuration example file (griddb.conf), and a javascript program file (parseInfluxDBResponse.js) to parse the InfluxDB v2 response. Need to install csv-parse v4.16.3 to parse InfluxDB CSV response:

    $ npm install csv-parse@4.16.3

    It will add ./node_modules/csv-parse to the current directory:

    sample/
    ├── griddb.conf
    ├── parseInfluxDBResponse.js
    └── node_modules/
        └── csv-parse/
    

    Copy the telegraf executable file from ~/go/src/github.com/influxdata/telegraf/telegraf to the ./sample directory:

    $ cp ~/go/src/github.com/influxdata/telegraf/telegraf ./

    The final directory structure will be the following:

    sample/
    ├── telegraf
    ├── griddb.conf
    ├── parseInfluxDBResponse.js
    └── node_modules/
        └── csv-parse/
    
  2. Run Telegraf with the example configuration file:

    $ ./telegraf --config "griddb.conf"

[Note]: If your machine is behind a proxy server, it is necessary to configure the proxy by using the following environment variables:

export https_proxy=http://username:password@host:port
export http_proxy=http://username:password@host:port
export no_proxy="host1,host2"

Add the proxy settings to the /etc/environment file; then run the command: source /etc/environment.

3 How to use

3.1 Input plugin configuration

The input plugin is out of scope of this guideline, refer to https://docs.influxdata.com/telegraf/v1.19/plugins/ for detail.

The next two sections provide some examples you can use:

3.1.1 Exec input plugin

The Exec input plugin is used to execute one or more Linux commands. In these examples, a command to query data from InfluxDB is used.

[Note]:

npm install csv-parse 4.16.3

Parameters

Parameter Description
commands A list of commands used to query data from InfluxDB (required)
timeout Timeout for each command to complete
data_format The format of the output obtained after running the command specified in the command parameter (required)
csv_header_row_count The number of header rows in data_format (required)

Examples

Configuration of ./griddb.conf

  • Configure the Exec input plugin to query data from InfluxDB:
# Use FluxDB v2
[[inputs.exec]]
    commands = [ 
        '''
            bash -c 'curl -sS --location \
                --request POST "{your_influxdb_host:port}/api/v2/query/?orgID={your_influxdb_orgID}" \
                --header "Accept: application/json" \
                --header "Content-type: application/vnd.flux" \
                --header "Authorization: Token {influxdb_authentication_token}" \
                --data-raw '"'"' 
                    from(bucket:"bucketname")
                    |>range(start:-10y, stop: 10y)
                    |> filter(fn:(r) => r._measurement == "tablename")
                    '"'"' \
                | node parseInfluxDBResponse.js
         '
        ''' ]

    timeout = "10s"
    data_format = "influx"
    csv_header_row_count = 1

  • Alternatively, you can create a run.sh file which includes a command to query data from InfluxDB:

    • Create a run.sh file as below:
    curl -sS --location \
                    --request POST "{your_influxdb_host:port}/api/v2/query/?orgID={your_influxdb_orgID}" \
                    --header "Accept: application/json" \
                    --header "Content-type: application/vnd.flux" \
                    --header "Authorization: Token {influxdb_authentication_token}" \
                    --data-raw '
                        from(bucket:"bucketname")
                        |>range(start:-10y, stop: 10y)
                        |> filter(fn:(r) => r._measurement == "tablename")
                        ' \
                    | node parseInfluxDBResponse.js
    
    • Configure the input plugin as below:
    # Use FluxDB v2
    [[inputs.exec]]
        commands = ["sh /path/to/file/run.sh"]
        timeout = "10s"
        data_format = "influx"
        csv_header_row_count = 1
    

You can create as many inputs as the number of commands used to query data from InfluxDB which you want to execute (for example, multiple query commands for different buckets and for different time ranges).

  • This can be done by creating a new .sh file to query other data.

    Example for run2.sh:

    curl -sS --location \
                    --request POST "{your_influxdb_host:port}/api/v2/query/?orgID={your_influxdb_orgID}" \
                    --header "Accept: application/json" \
                    --header "Content-type: application/vnd.flux" \
                    --header "Authorization: Token {influxdb_authentication_token}" \
                    --data-raw '
                        from(bucket:"bucketname2")
                        |>range(start:-1y, stop: 1y)
                        |> filter(fn:(r) => r._measurement == "tablename2")
                        ' \
                    | node parseInfluxDBResponse.js
    
  • Add the file to ./griddb.conf as an input plugin configuration [[inputs.exec]].

    Example for multiple inputs:

    # input 1
    [[inputs.exec]]
        commands = ["sh /path/to/file/run.sh"]
        timeout = "10s"
        data_format = "influx"
        csv_header_row_count = 1
        
    # input 2
    [[inputs.exec]]
        commands = ["sh /path/to/file/run2.sh"]
        timeout = "10s"
        data_format = "influx"
        csv_header_row_count = 1
    

3.1.2 Configure a command for querying data

[Note]: Refer to https://docs.influxdata.com/influxdb/v1.8/guides/query_data/ for detail.

Parameters

Parameter Description
orgID Organization ID in InfluxDB (required)
Authorization Authentication token for obtaining data from InfluxDB (required)
bucket A bucket name in InfluxDB (required)
r._measurement A table of buckets which you want to get data from. If you want to get data from all tables, comment it out with "//" or delete this line.
range Time range for which data is obtained from InfluxDB

Examples

Example command for querying one table in a bucket:

curl -sS --location \
                --request POST "{your_influxdb_host:port}/api/v2/query/?orgID={your_influxdb_orgID}" \
                --header "Accept: application/json" \
                --header "Content-type: application/vnd.flux" \
                --header "Authorization: Token {influxdb_authentication_token}" \
                --data-raw '
                    from(bucket:"bucketname")
                    |>range(start:-10y, stop: 10y)
                    |> filter(fn:(r) => r._measurement == "tablename")
                   ' \
                | node parseInfluxDBResponse.js

Example command for querying all tables in a bucket:

curl -sS --location \
                --request POST "{your_influxdb_host:port}/api/v2/query/?orgID={your_influxdb_orgID}" \
                --header "Accept: application/json" \
                --header "Content-type: application/vnd.flux" \
                --header "Authorization: Token {influxdb_authentication_token}" \
                --data-raw '
                    from(bucket:"bucketname")
                    |>range(start:-10y, stop: 10y)
                   ' \
                | node parseInfluxDBResponse.js

[Note]: parseInfluxDBResponse.js, which is included in the source code of the GridDB output plugin for Telegraf, is used to parse InfluxDB responses.

3.2 Configure the output plugin

3.2.1 Parameters

Parameter Description
api_url URL of GridDB Web API (required)
Valid URL example: https://127.0.0.1:8080/griddb/v2/
cluster_name The name of a cluster in GridDB (required)
database The name of a database in GridDB into which you want to insert data. The default is public.
username The GridDB user. The GridDB user must have WRITE permissions to the database specified in the parameter "database". (required)
password The password of the GridDB user (required)
update_mode Insert mode option: 'replace' or 'append'. The default is 'append'.
containers A list of tables that you want to insert into GridDB in array format. If the list is empty, all tables will be outputted.
timestamp_column The name of the timestamp column (the first column) in a timeseries container in GridDB. This column always has a row key. If this parameter is not specified, a collection container without a row key is created in GridDB.
  • When the append mode is specified to insert data, if a container does not exist, a new one is created to insert data. If a container already exists, the current one is kept to insert data.
  • When the replace mode is specified to insert data, if a container does not exist, a new one is created to insert data. If a container already exists, the current one is deleted and a new one is created to insert data.

3.2.2 Example

Below is a detailed example for using the GridDB output plugin.

# GridDB plugin for Telegraf to transfer non-GridDB resources to GridDB
[[outputs.griddb]]
    ## GridDB WebAPI URL
    api_url = "https://127.0.0.1:8080/griddb/v2/"

    ## Database name
    ## # Optional; the default is public.
    database = "database"

    ## Cluster name
    cluster_name = "clustername"

    ## GridDB username
    username = "username"

    ## GridDB password
    password = "password"

    ## Delete the existing container if it exists.
    ## # Optional; the default is append.
    ## # Accepted values: replace, append
    ##     - replace: Override the existing container.
    ##     - append:  Append new rows to the existing container.
    update_mode = "append"
    
    ## A list of tables to be transferred to GridDB
    ## # Example: ["cpu", "ram", "product"]
    ## # An empty list [] means ALL containers.
    ## # Optional; the default is [].
    containers = []
    timestamp_column = "timestamp"

3.3 Example for full input-output plugin configuration

Example: The full configuration for the InfluxDB input plugin and the GridDB output plugin:

# Telegraf Configuration
###############################################################################
#                            OUTPUT PLUGINS                                   #
###############################################################################
# GridDB plugin for Telegraf to transfer non-GridDB resources to GridDB
[[outputs.griddb]]
    ## GridDB WebAPI URL
    api_url = "https://{please_fill_full_uri}"
    ## Database name
    database = "database"
    ## Cluster name
    cluster_name = "clustername"
    ## GridDB username
    username = "username"
    ## GridDB password
    password = "password"
    ## Delete the existing container if it exists.
    update_mode = "append" 
    ## A list of tables to be transferred to GridDB
    containers = []
    ## Name of the timestamp column. If timestampColumn is empty, a timestamp column is not added to the first column of the container.
    timestamp_column = "timestamp"

###############################################################################
#                            INPUT PLUGINS                                    #
###############################################################################

# Use FluxDB v2
[[inputs.exec]]
    commands = [ 
        '''
            bash -c 'curl -sS --location \
                --request POST "{your_influxdb_host:port}/api/v2/query/?orgID={your_influxdb_orgID}" \
                --header "Accept: application/json" \
                --header "Content-type: application/vnd.flux" \
                --header "Authorization: Token {influxdb_authentication_token}" \
                --data-raw '"'"' 
                    from(bucket:"bucketname")
                    |>range(start:-10y, stop: 10y)
                    |> filter(fn:(r) => r._measurement == "tablename")
                    '"'"' \
                | node parseInfluxDBResponse.js
         '
        ''' ]

    timeout = "10s"
    data_format = "influx"
    csv_header_row_count = 1

[Note]:

  • If there is too much data in InfluxDB, change the interval parameter in the agent configuration file griddb.conf to prevent migration failure.
    Example:

    # Configuration for the telegraf agent
    [agent]
        ## Data collection interval for all inputs
        ## To migrate data from InfluxDB to GridDB, the interval is set to a large enough number.
        ## After all data is migrated, the telegraf process can be terminated.
        interval = "99999999s"
    
  • Refer to the full configuration file griddb.conf in the source code of the GridDB output plugin for Telegraf for detail.