Compatible with InfluxDB v1.0.0-beta and Kapacitor v1.0.0-beta API's
InfluxData.Net is a portable .NET library to access the REST API of an InfluxDB database and Kapacitor processing tool.
InfluxDB is the data storage layer in InfluxData's TICK stack which is an open-source end-to-end platform for managing time-series data at scale.
Kapacitor is a data processing engine. It can process both stream (subscribe realtime) and batch (bulk query) data from InfluxDB. Kapacitor lets you define custom logic to process alerts with dynamic thresholds, match metrics for patterns, compute statistical anomalies, etc.
Support for other TICK stack layers is also planned and will be implemented in the future when they become stable from InfluxData side.
Original Lib This is a fork of InfluxDb.Net, (which is in turn a fork of InfluxDb.Net). Those NuGet libraries are only suitable for InfluxDB versions lower than v0.9.5.
Support for older versions
Currently older supported versions:
- InfluxDB: v0.9.2, v0.9.6
- Kapacitor: v0.10.0, v0.10.1
Installation You can download the InfluxData.Net Nuget package to install the latest version of InfluxData.Net Lib.
Please apply your changes to the develop branch it makes it a bit easier and cleaner for me to keep everything in order. For extra points in the FLOSS hall of fame, write a few tests for your awesome contribution as well. :) Thanks for your help!
To use InfluxData.Net InfluxDbClient you must first create an instance of InfluxDbClient:
var influxDbClient = new InfluxDbClient("http://yourinfluxdb.com:8086/", "username", "password", InfluxDbVersion.v_1_0_0);To use InfluxData.Net KapacitorClient you must first create an instance of KapacitorClient (Kapacitor doesn't support authentication yet, so use this overload for now):
var kapacitorClient = new KapacitorClient("http://yourkapacitor.com:9092/", KapacitorVersion.v_1_0_0);Clients modules (properties of Client object) can then be consumed and methods for communicating with InfluxDb/Kapacitor can be consumed.
If needed, a custom HttpClient can be used for making requests. Simply pass it into the InfluxDbClient or KapacitorClient as the last (optional) parameter.
Supported InfluxDbClient modules and API calls
- Client
- WriteAsync()
- QueryAsync()
- MultiQueryAsync()
- Database
- CreateDatabaseAsync()
- GetDatabasesAsync()
- DropDatabaseAsync()
- ContinuousQuery
- CreateContinuousQueryAsync()
- GetContinuousQueriesAsync()
- DeleteContinuousQueryAsync()
- BackfillAsync()
- Serie
- GetSeriesAsync()
- DropSeriesAsync()
- GetMeasurementsAsync()
- DropMeasurementAsync()
- Retention
- CreateRetentionPolicyAsync()
- GetRetentionPoliciesAsync()
- AlterRetentionPolicyAsync()
- DropRetentionPolicyAsync()
- Diagnostics
- PingAsync()
- GetStatsAsync()
- GetDiagnosticsAsync()
Supported KapacitorClient modules and API calls
- Task
- GetTaskAsync()
- GetTasksAsync()
- DefineTaskAsync()
- DeleteTaskAsync()
- EnableTaskAsync()
- DisableTaskAsync()
Can be used to do the most basic operations against InfluxDb API.
To write new data into InfluxDb, a Point object must be created first:
var pointToWrite = new Point()
{
Name = "reading", // serie/measurement/table to write into
Tags = new Dictionary<string, object>()
{
{ "SensorId", 8 },
{ "SerialNumber", "00AF123B" }
},
Fields = new Dictionary<string, object>()
{
{ "SensorState", "act" },
{ "Humidity", 431 },
{ "Temperature", 22.1 },
{ "Resistance", 34957 }
},
Timestamp = DateTime.UtcNow // optional (can be set to any DateTime moment)
};Point is then passed into Client.WriteAsync method together with the database name:
var response = await influxDbClient.Client.WriteAsync("yourDbName", pointToWrite);If you would like to write multiple points at once, simply create an IEnumerable collection of Point objects and pass it into the second WriteAsync overload:
var response = await influxDbClient.Client.WriteAsync("yourDbName", pointsToWrite);The Client.QueryAsync can be used to execute any officially supported InfluxDb query:
var query = "SELECT * FROM reading WHERE time > now() - 1h";
var response = await influxDbClient.Client.QueryAsync("yourDbName", query);The second QueryAsync overload will return the result of multiple queries executed at once. The response will be a flattened collection of multi-results series. This means that the resulting series from all queries will be extracted into a single collection. This has been implemented to make it easier on the developer in case he is querying the same measurement with different params multiple times at once.
var queries = new []
{
"SELECT * FROM reading WHERE time > now() - 1h",
"SELECT * FROM reading WHERE time > now() - 2h"
}
var response = await influxDbClient.Client.QueryAsync("yourDbName", queries);MultiQueryAsync also returns the result of multiple queries executed at once. Unlike the second QueryAsync overload, the results will not be flattened. This method will return a collection of results where each result contains the series of a corresponding query.
var queries = new []
{
"SELECT * FROM reading WHERE time > now() - 1h",
"SELECT * FROM reading WHERE time > now() - 2h"
}
var response = await influxDbClient.Client.MultiQueryAsync("yourDbName", queries);The database module can be used to manage the databases on the InfluxDb system.
You can create a new database in the following way:
var response = await influxDbClient.Database.CreateDatabaseAsync("newDbName");Gets a list of all databases accessible to the current user:
var response = await influxDbClient.Database.GetDatabasesAsync();Drops a database:
var response = await influxDbClient.Database.DropDatabaseAsync("dbNameToDrop");This module can be used to manage CQ's and to backfill with aggregate data.
To create a new CQ, a CqParams object must first be created:
var cqParams = new CqParams()
{
DbName = "yourDbName",
CqName = "reading_minMax_5m", // CQ name
Downsamplers = new List<string>()
{
"MAX(field_int) AS max_field_int",
"MIN(field_int) AS min_field_int"
},
DsSerieName = "reading.minMax.5m", // new (downsample) serie name
SourceSerieName = "reading", // source serie name to get data from
Interval = "5m",
FillType = FillType.Previous
// you can also add a list of tags to keep in the DS serie here
};To understand FillType, please refer to the fill() documentation. After that, simply call ContinuousQuery.CreateContinuousQueryAsync to create it:
var response = await influxDbClient.ContinuousQuery.CreateContinuousQueryAsync("yourDbName", cqParams);This will return a list of currently existing CQ's on the system:
var response = await influxDbClient.ContinuousQuery.GetContinuousQueriesAsync("yourDbName");Deletes a CQ from the database:
var response = await influxDbClient.ContinuousQuery.DeleteContinuousQueryAsync("yourDbName", "cqNameToDelete");The ContinuousQuery.BackfillAsync method can be used to manually calculate aggregate data for the data that was already in your DB, not only for the newly incoming data.
Similarly to CreateContinuousQueryAsync, a BackfillParams object needs to be created first:
var backfillParams = new BackfillParams()
{
Downsamplers = new List<string>()
{
"MAX(field_int) AS max_field_int",
"MIN(field_int) AS min_field_int"
},
DsSerieName = "reading.minMax.5m", // new (downsample) serie name
SourceSerieName = "reading", // source serie name to get data from
TimeFrom = DateTime.UtcNow.AddMonths(-1),
TimeTo = DateTime.UtcNow,
Interval = "5m",
FillType = FillType.None
// you can also add a list of "WHERE" clause filters here
// you can also add a list of tags to keep in the DS serie here
};To understand FillType, please refer to the fill() documentation. After that, simply call ContinuousQuery.BackfillAsync to execute the backfill:
var response = await influxDbClient.ContinuousQuery.BackfillAsync("yourDbName", backfillParams);This module provides methods for listing existing DB series and measures as well as methods for removing them.
####GetSeriesAsync
Gets list of series in the database. If measurementName (optional) param is provided, will only return series for that measurement. WHERE clauses can be passed in through the optional filters param.
var response = await influxDbClient.Serie.GetSeriesAsync("yourDbName");Drops data points from series in database. The series itself will remain in the index.
var response = await influxDbClient.Serie.DropSeriesAsync("yourDbName", "serieNameToDrop");####GetMeasurementsAsync
Gets list of measurements in the database. WHERE clauses can be passed in through the optional filters param.
var response = await influxDbClient.Serie.GetMeasurementsAsync("yourDbName");Drops measurements from series in database. Unlike DropSeriesAsync it will also remove the measurement from the DB index.
var response = await influxDbClient.Serie.DropMeasurementAsync("yourDbName", "measurementNameToDrop");This module currently supports only a single retention-policy action.
This example creates the retentionPolicyName policy to 1h and 3 copies:
var response = await influxDbClient.Retention.CreateRetentionPolicyAsync("yourDbName", "retentionPolicyName", "1h", 3);Gets a list of all retention policies in the speified database:
var response = await influxDbClient.Retention.GetRetentionPoliciesAsync("yourDbName");This example alter the retentionPolicyName policy to 1h and 3 copies:
var response = await influxDbClient.Retention.AlterRetentionPolicyAsync("yourDbName", "retentionPolicyName", "1h", 3);This example drops the retentionPolicyName policycopies:
var response = await influxDbClient.Retention.AlterRetentionPolicyAsync("yourDbName", "retentionPolicyName");This module can be used to get diagnostics information from InfluxDB server.
The PingAsync will return a Pong object which will return endpoint's InfluxDb version number, round-trip time and ping success status:
var response = await influxDbClient.Diagnostics.PingAsync();GetStatsAsync executes SHOW STATS and parses the results into Stats response object.
var response = await influxDbClient.Diagnostics.GetStatsAsync();GetDiagnosticsAsync executes SHOW DIAGNOSTICS and parses the results into Diagnostics response object.
var response = await influxDbClient.Diagnostics.GetDiagnosticsAsync();Can be used to do work with tasks (creation, deletion, listing, enablin, disabling..).
To get a single Kapacitor task, execute the following:
var response = await kapacitorClient.Task.GetTaskAsync("taskId");To get all Kapacitor tasks, execute the following:
var response = await kapacitorClient.Task.GetTasksAsync();To create/define a task, a DefineTaskParams object needs to be created first:
var taskParams = new DefineTaskParams()
{
TaskId = "someTaskId",
TaskType = TaskType.Stream,
DBRPsParams = new DBRPsParams()
{
DbName = "yourInfluxDbName",
RetentionPolicy = "default"
},
TickScript = "stream\r\n" +
" |from().measurement('reading')\r\n" +
" |alert()\r\n" +
" .crit(lambda: \"Humidity\" < 36)\r\n" +
" .log('/tmp/alerts.log')\r\n"
};After that simply call the DefineTaskAsync to create a new task:
var response = await kapacitorClient.Task.DefineTaskAsync(taskParams);You can also define tasks using the DefineTemplatedTaskParams as well. This allows you to define tasks with template ID's instad of specifying the TICKscript and type directly.
To delete a Kapacitor task, execute the following:
var response = await kapacitorClient.Task.DeleteTaskAsync("taskId");To enable a Kapacitor task, execute the following:
var response = await kapacitorClient.Task.EnableTaskAsync("taskId");To disable a Kapacitor task, execute the following:
var response = await kapacitorClient.Task.DisableTaskAsync("taskId");If you encounter a bug, performance issue, a malfunction or would like a feature to be implemented, please open a new issue.
Code and documentation are available according to the MIT License (see LICENSE).