content/kapacitor/v1/working/flux/_index.md
Use Kapacitor 1.6+ to run Flux tasks against InfluxDB and other data sources. Leverage the full library of Flux functionality to build powerful data processing and monitoring tasks in Kapacitor.
Before you get started with Flux tasks, consider:
task option inside of a Flux task script.
This includes the task name and execution schedule.Select which version of InfluxDB you're using:
{{< tabs-wrapper >}} {{% tabs %}} InfluxDB 1.x InfluxDB Cloud or 2.x {{% /tabs %}}
<!------------------------- BEGIN InfluxDB 1.x content ------------------------>{{% tab-content %}}
({{< req "Optional but encouraged" >}})
When Kapacitor executes a Flux task, it can store information about the task execution (run) in an InfluxDB database. To store this data, do the following:
Create a new database in InfluxDB to store Flux task run and log data in.
CREATE DATABASE kapacitorfluxtasks
To prevent large amounts of Kapacitor Flux task log data on disk, update
the default autogen retention policy with a finite retention period or
create a new retention policy (RP) with a finite retention period.
{{< code-tabs-wrapper >}} {{% code-tabs %}} Update RP Create new RP {{% /code-tabs %}} {{% code-tab-content %}}
-- Syntax
ALTER RETENTION POLICY <rp-name> ON <db-name> DURATION <new-retention-duration>
-- Example
ALTER RETENTION POLICY autogen ON kapacitorfluxtasks DURATION 3d
{{% /code-tab-content %}}
{{% code-tab-content %}}
-- Syntax
CREATE RETENTION POLICY <rp-name> on <db-name> DURATION <retention-duration>
-- Example
CREATE RETENTION POLICY threedays on kapacitorfluxtasks DURATION 3d
{{% /code-tab-content %}}
{{< /code-tabs-wrapper >}}
Update or add the following settings under [fluxtask] in your kapacitor.conf:
truekapacitor.conf
to use to store Flux task data.
To disable Flux task logging, set to "none".kapacitor_fluxtask_logs database. To specify another database to write task log data to, use the "db-name" naming convention (including the retention policy "db-name/rp" is not supported). If the specified database does not already exist in InfluxDB, Kapacitor attempts to create the database. If authentication is turned on, permissions to CREATE DATABASE are required. For more information, see Authentication and authorization in InfluxDB."")"")"runs".# ...
[fluxtask]
enabled = true
task-run-influxdb = "default"
task-run-bucket = "kapacitor_fluxtask_logs"
task-run-org = ""
task-run-orgid = ""
task-run-measurement = "runs"
# ...
For more information about Kapacitor [fluxtask] configuration options,
see Configure Kapacitor.
Create a Flux task script. Include the task option in your script to configure the Kapacitor Flux task. For more information about writing Flux tasks, see:
from() and
to() functions
require your InfluxDB host and token.
username:password syntax.
Otherwise, use an empty string ("") for your token.When querying or writing to InfluxDB 1.x with Flux, use the
database-name/retention-policy-name pattern to specify your bucket.
{{< keep-url >}}
option task = {
name: "example-task-name",
every: 1h,
offset: 10m
}
host = "http://localhost:8086"
token = ""
from(bucket: "example-db/example-rp", host: host, token: token)
|> range(start: -task.every)
|> filter(fn: (r) => r._measurement == "example-measurement")
|> aggregateWindow(every: 10m, fn: mean)
|> to(bucket: "example-db/example-rp-downsampled", host: host, token: token)
Use the kapacitor flux task create command to add your Flux script as a Kapacitor Flux task.
kapacitor flux task create --file /path/to/example-task.flux
For more details about creating Kapacitor Flux tasks, see Create a Kapacitor Flux task. {{% /tab-content %}}
<!-------------------------- END InfluxDB 1.x content -------------------------> <!---------------------- BEGIN InfluxDB Cloud/2.x content --------------------->{{% tab-content %}} {{% warn %}}
If you're using InfluxDB Cloud or InfluxDB OSS 2.x, consider using native InfluxDB tasks for data processing. {{% /warn %}}
Configure Kapacitor to connect to InfluxDB Cloud or InfluxDB OSS 2.x. For detailed instructions, see the following:
Update or add the following settings under [fluxtask] your kapacitor.conf:
truekapacitor.conf
to use to store Flux task data.
To disable Flux task logging, set to "none".kapacitor_fluxtask_logs bucket. To specify another bucket to write task log data to, use the _tasks system bucket or create a new bucket. If the specified bucket does not already exist in InfluxDB, Kapacitor attempts to create it with POST /api/v2/buckets, in which case your API token must have permissions to create buckets in InfluxDB. For more information, see Manage API tokens."runs".# ...
[fluxtask]
enabled = true
task-run-influxdb = "InfluxDB"
task-run-bucket = "kapacitor_fluxtask_logs"
task-run-org = "example-org"
task-run-measurement = "runs"
# ...
Create a Flux task script. Include the task option in your script to configure the Kapacitor Flux task. For more information about writing Flux tasks, see:
from()](/flux/v0/stdlib/influxdata/influxdb/from/) and
to() functions
require your InfluxDB host and token.
username:password syntax.
Otherwise, use an empty string ("") for your token.{{< keep-url >}}
option task = {
name: "example-task-name",
every: 1h,
offset: 10m
}
host = "http://localhost:8086"
token = ""
from(bucket: "example-bucket", host: host, token: token)
|> range(start: -task.every)
|> filter(fn: (r) => r._measurement == "example-measurement")
|> aggregateWindow(every: 10m, fn: mean)
|> to(bucket: "example-bucket-downsampled", host: host, token: token)
Use the kapacitor flux task create command to add your Flux script as a Kapacitor Flux task.
kapacitor flux task create --file /path/to/example-task.flux
For more details about creating Kapacitor Flux tasks, see Create a Kapacitor Flux task. {{% /tab-content %}}
<!----------------------- END InfluxDB Cloud/2.x content ---------------------->{{< /tabs-wrapper >}}