Compatible with InfluxDB v1.0.0-beta and Kapacitor v1.0.0-beta API's
NOTE: The library will most probably work just as fine with newer versions of the TICK stack as well but it hasn't been tested against them.
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);
Additional, optional params for InfluxDbClient are a custom HttpClient
if you think you need control over it, and throwOnWarning
which will throw an InfluxDataWarningException
if the InfluxDb API returns a warning as a part of the response. That should preferably be used only for debugging purposes.
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()
- User
- CreateUserAsync()
- GetUsersAsync()
- DropUserAsync()
- SetPasswordAsync()
- GetPrivilegesAsync()
- GrantAdministratorAsync()
- RevokeAdministratorAsync()
- GrantPrivilegeAsync()
- RevokePrivilegeAsync()
- ContinuousQuery
- CreateContinuousQueryAsync()
- GetContinuousQueriesAsync()
- DeleteContinuousQueryAsync()
- BackfillAsync()
- Serie
- GetSeriesAsync()
- DropSeriesAsync()
- GetMeasurementsAsync()
- DropMeasurementAsync()
- GetTagKeysAsync()
- GetTagValuesAsync()
- GetFieldKeysAsync()
- CreateBatchWriter()
- 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");
The user module can be used to manage database users on the InfluxDb system. The requests in the user module must be called with user credentials that have administrator privileges or authentication must be disabled on the server.
Creates a new user. The user can either be created as a regular user or an administrator user by specifiy the desired value for the isAdmin
parameter when calling the method.
To create a new user:
var response = await influxDbClient.User.CreateUserAsync("userName");
To create a new administrator:
var response = await influxDbClient.User.CreateUserAsync("userName", true);
Gets a list of users for the system:
var users = await influxDbClient.User.GetUsersAsync();
Drops an existing user:
var response = await influxDbClient.User.DropUserAsync("userNameToDrop");
Sets a user's password:
var response = await influxDbClient.User.SetPasswordAsync("userNameToUpdate", "passwordToSet");
Gets a list of a user's granted privileges:
var grantedPrivilges = await influxDbClient.User.GetPrivilegesAsync("userNameToGetPrivilegesFor");
Grants administrator privileges to a user:
var response = await influxDbClient.User.GrantAdministratorAsync("userNameToGrantTo");
Revokes administrator privileges from a user:
var response = await influxDbClient.User.RevokeAdministratorAsync("userNameToRevokeFrom");
Grants the specified privilege to a user for a given database:
var response = await influxDbClient.User.GrantPrivilegeAsync("userNameToGrantTo", Privileges.Read, "databaseName");
Revokes the specified privilege from a user for a given database:
var response = await influxDbClient.User.RevokePrivilegeAsync("userNameToRevokeFrom", Privileges.Read, "databaseName");
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.
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");
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");
Gets a list of tag keys for a given database and measurement.
var response = await influxDbClient.Serie.GetTagKeysAsync("yourDbName", "measurementNameToGetTagsFor");
Gets a list of tag values for a given database, measurement, and tag key.
var response = await influxDbClient.Serie.GetTagValuesAsync("yourDbName", "measurementNameToGetTagsValuesFor", "tagNameToGetValuesFor");
Gets a list of field keys for a given database and measurement. The returned list of field keys also specify the field type per key.
var response = await influxDbClient.Serie.GetFieldKeysAsync("yourDbName", "measurementNameToGetFieldKeysFor");
Creates a BatchWriter
instance which can then be shared by multiple threads/processes to be used
for batch Point
writing in intervals (for example every five seconds). It will keep the points in-memory
for a specified interval. After the interval times out, the collection will get dequeued and "batch-written"
to InfluxDb. The BatchWriter
will keep checking the collection for new points after each interval times
out until stopped. For thread safety, the BatchWriter
uses the BlockingCollection
internally.
var batchWriter = await influxDbClient.Serie.CreateBatchWriter("yourDbName");
Starts the async batch writing task. You can set the interval after which the points will be submitted to the InfluxDb API (or use the default 1000ms). You can also instruct the BatchWriter to not stop if the BatchWriter encounters an error by setting the continueOnError to true.
batchWriter.Start(5000);
Stops the async batch writing task.
batchWriter.Stop();
Adds a single Point
item to the blocking collection.
var point = new Point() { ... };
batchWriter.AddPoint(point);
Adds a multiple Point
items to the collection.
var points = new Point[10] { ... };
batchWriter.AddPoints(points);
OnError event handler. You can attach to it to handle any exceptions that might be thrown by the API.
// Attach to the event handler
batchWriter.OnError += BatchWriter_OnError;
// OnError handler method
private void BatchWriter_OnError(object sender, Exception e)
{
// Handle the error here
}
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).