01 - InfluxDB: First Steps and Guide-Lines
Inhaltsverzeichnis
InfluxLine Protocol
InfluxDB line protocol is a text based format for writing points to InfluxDB.
Syntax
<measurement>,<tag_key>=<tag_value>,<tag_key>=<tag_value> <field_key>=<field_value>,<field_key>=<field_value> <timestamp>
The whitespace between the tags and the fields are required!
Example:
win_process_usage,ProcessName=ClusSvc,ID=1333 Handles=200,PagedmemorySize=102400,CPU=7
Measurement = win_process_usace
Tags = ProcessName, ID
Fields = Handles, PagedmemorySize, CPU
Syntax description
Element | Optional/Required | Description | Type |
---|---|---|---|
Measurement | Required | The measurement name. InfluxDB accepts one measurement per point. | String |
Tag set | Optional | All tag key-value pairs for the point. | Tag keys and tag values are both strings. |
Field set | Required. Points must have at least one field. | All field key-value pairs for the point. | Field keys are strings. Field values can be floats, integers, strings, or Booleans. |
Timestamp | Optional. InfluxDB uses the server’s local nanosecond timestamp in UTC if the timestamp is not included with the point. | The timestamp for the data point. InfluxDB accepts one timestamp per point. | Unix nanosecond timestamp. Specify alternative precisions with the InfluxDB API. |
PowerShell-Example to write metrics directly to an InfluxDB v1.x
Invoke-WebRequest 'http://<your_InfluxDB_server>:8086/write?db=<yourdb>' -Method POST -Body 'test_measurement_cpu,host=server01,os=windows cpuload=33 1587646932000000000'
If the response statuscode is 204 this means, it was a success!
The Timestamp is optional. (1587646932000000000 means 23. April 2020, 14:02:12.)
PowerShell-Example to write metrics directly to an InfluxDB v2.x
$Header = @{Authorization = "Token <YOUR_AUTH_TOKEN>"} Invoke-WebRequest -Uri 'http://<your_InfluxDB_server>:8086/api/v2/write?org=<INFLUX_YOUR_ORG>&bucket=<YOUR_BUCKET>' -Header $Header -Method POST -Body 'test_measurement_cpu,host=server01,os=windows cpuload=33 1587646932000000000'
If the response statuscode is 204 this means, it was a success!
The Timestamp is optional. (1587646932000000000 means 23. April 2020, 14:02:12.)
PowerShell-Example to write metrics with the Telegraf Agent to an InfluxDB
Telegraf configuration name "inputs.exec.process.conf"
[[inputs.exec]] commands = ['powershell -NoProfile -file "C:\Program Files\Telegraf\scripts\input.process.ps1"'] timeout = "1m" data_format = "influx"
PowerShell-Skript named "input.process.ps1"
$Processes = Get-Process -Name * | Select-Object ProcessName, Id, Handles, PagedmemorySize, CPU ForEach ($Process in $Processes) { Write-Output "win_process_usage,ProcessName=$($Process.ProcessName) ID=$($Process.Id),Handles=$($Process.Handles),PagedmemorySize=$($Process.PagedmemorySize),CPU=$($Process.CPU)" }
Testing the Telegraf configuration and the PowerShell script
telegraf.exe --config "C:\Program Files\Telegraf\telegraf.d\inputs.exec.process.conf" --test
More informations: https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_reference/
Useful Queries / Snippets
Count unique tag values
// Count unique values for each tag in a bucket import "influxdata/influxdb/schema" cardinalityByTag = (bucket) => schema.tagKeys(bucket: bucket) |> map(fn: (r) => ({ tag: r._value, _value: if contains(set: ["_stop","_start"], value:r._value) then 0 else (schema.tagValues(bucket: bucket, tag: r._value) |> count() |> findRecord(fn: (key) => true, idx: 0))._value })) |> group(columns:["tag"]) |> sum() cardinalityByTag(bucket: "<YOUR_BUCKET>")
Time (Docs: https://www.influxdata.com/blog/tldr-tech-tips-flux-timestamps)
Convert function "now" to Epoche Timestamp (Unix) in ns
uint(v: now()) // Returns 1568808000000000000
Convert Epoche Timestamp (Unix) to ISO Format (RFC3339)
uint(v: 1568808000000000000) // Returns 2019-09-18T12:00:00.000000000Z
Find letters in _value
from(bucket: "<YOUR_BUCKET>") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r._value =~ /\d/)