public async Task WriteToInfluxDb(string sensorTyp, string sensorUnit, string sensorUnitName, double sensorValue) { Console.WriteLine($"{sensorTyp}: {sensorValue.ToString(CultureInfo.CurrentCulture)} {sensorUnit}"); var payload = new LineProtocolPayload(); var point = new LineProtocolPoint(StationName, new Dictionary <string, object> { // ReSharper disable once HeapView.BoxingAllocation { "value", sensorValue } }, new Dictionary <string, string> { { "type", sensorTyp }, { "unit", sensorUnit }, { "unitName", sensorUnitName } }, DateTime.UtcNow); payload.Add(point); var influxResult = await _influxClient.WriteAsync(payload); if (!influxResult.Success) { Console.Error.WriteLine($"Problem while writing to INfluxDB \n{influxResult.ErrorMessage}"); } }
public void can_format_payload() { var textWriter = new StringWriter(); var payload = new LineProtocolPayload(); var fieldsOne = new Dictionary <string, object> { { "key", "value" } }; var timestampOne = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc); var pointOne = new LineProtocolPoint("measurement", fieldsOne, MetricTags.None, timestampOne); var fieldsTwo = new Dictionary <string, object> { { "field1key", "field1value" }, { "field2key", 2 }, { "field3key", false } }; var timestampTwo = new DateTime(2017, 1, 2, 1, 1, 1, DateTimeKind.Utc); var pointTwo = new LineProtocolPoint("measurement", fieldsTwo, MetricTags.None, timestampTwo); payload.Add(pointOne); payload.Add(pointTwo); payload.Format(textWriter); textWriter.ToString().Should() .Be( "measurement key=\"value\" 1483232461000000000\nmeasurement field1key=\"field1value\",field2key=2i,field3key=f 1483318861000000000\n"); }
public async Task Writing_a_float_to_a_field_that_previously_accepted_booleans() { var client = new MockLineProtocolClient("hamlet"); var payload = new LineProtocolPayload(); payload.Add(new LineProtocolPoint( "tobeornottobe", new Dictionary <string, object> { { "booleanonly", true } } )); payload.Add(new LineProtocolPoint( "tobeornottobe", new Dictionary <string, object> { { "booleanonly", 5f } } )); client.Handler.Expect($"{client.BaseAddress}write?db=hamlet") .Respond(HttpStatusCode.BadRequest, "application/json", "{\"error\":\"field type conflict: input field \\\"booleanonly\\\" on measurement \\\"tobeornottobe\\\" is type float, already exists as type boolean dropped=1\"}"); var result = await client.WriteAsync(payload); Assert.False(result.Success); Assert.Equal("BadRequest Bad Request {\"error\":\"field type conflict: input field \\\"booleanonly\\\" on measurement \\\"tobeornottobe\\\" is type float, already exists as type boolean dropped=1\"}", result.ErrorMessage); }
public void Write(ProcessObservation o) { var point = new LineProtocolPoint(table, new Dictionary <string, object> { { "process_id", o.ProcessID }, { "parent_process_id", o.ParentProcessID } }, new Dictionary <string, string> { { "process_name", o.ProcessName }, { "event_name", o.EventName }, { "host", host } }, o.TimeCreated ); var payload = new LineProtocolPayload(); payload.Add(point); var result = client.WriteAsync(payload).Result; if (!result.Success) { Console.Error.WriteLine(result.ErrorMessage); } }
// TODO: add retry logic? public async Task <LineProtocolWriteResult> WriteAsync(LineProtocolPayload lineProtocolPayload, CancellationToken cancellationToken = default(CancellationToken)) { if (lineProtocolPayload == null || lineProtocolPayload.Count == 0) { return(new LineProtocolWriteResult(true, "0", "No input.")); } HttpResponseMessage response; using (HttpContent content = CreateHttpContent(lineProtocolPayload)) { response = await PostToLineProtocolEndpoint(_requestUri, content, cancellationToken); } string httpStatusCode = ((int)response.StatusCode).ToString(); if (response.IsSuccessStatusCode) { return(new LineProtocolWriteResult(true, httpStatusCode)); } string errorMessage = await response.Content.ReadAsStringAsync(); return(new LineProtocolWriteResult(false, httpStatusCode, errorMessage)); }
private async void UploadSensorValuesToInfluxDb() { while (true) { var sensorValues = new LineProtocolPoint( "1", new Dictionary <string, object> { { "GasValue", _gasValue }, { "TotalUsers", _totalUsers }, { "CurrentUsers", _currentUsers }, { "Requests", _cleaningRequests } }, new Dictionary <string, string>()); var payload = new LineProtocolPayload(); payload.Add(sensorValues); var client = new LineProtocolClient(new Uri("[REDACTED]"), "SensorValues", "[REDACTED]", "[REDACTED]"); await client.WriteAsync(payload); Task.Delay(60000).Wait(); } }
private bool AddSystemProductionToPayload(SystemProduction systemProduction, LineProtocolPayload payload) { if (systemProduction.ReadingTime <= 0) { return(false); } var readingTime = DateTimeOffset.FromUnixTimeSeconds(systemProduction.ReadingTime); var systemPoint = new LineProtocolPoint( "inverters", //Measurement new Dictionary <string, object> //Fields { { $"activecount", systemProduction.ActiveCount }, { $"whlifetime", systemProduction.WhLifeTime }, { $"WNow", systemProduction.WNow }, }, new Dictionary <string, string> //Tags { }, readingTime.UtcDateTime); //Timestamp payload.Add(systemPoint); return(true); }
static void Main(string[] args) { var path = "/Users/micl/Documents/Nasdaq/us/nasdaq/"; var database = "nasdaq"; LineProtocolClient client = new LineProtocolClient(new Uri("http://40.73.35.55:8086"), database); string[] subdirectiores = System.IO.Directory.GetDirectories(path); int subFolderCount = subdirectiores.Length; Console.WriteLine("Total found {0} sub folders", subFolderCount); int currentFolder = 1; DateTime start = DateTime.Now; foreach (string folder in subdirectiores) { Console.WriteLine("Now handling {0} / {1} folder, Symbol = {2}", currentFolder, subFolderCount, folder.Substring(folder.LastIndexOf("/") + 1, folder.Length - folder.LastIndexOf("/") - 1)); string[] files = Directory.GetFiles(folder, "*.json"); foreach (string f in files) { Console.WriteLine("Now reading file : {0}", f); var stock = new Stock(f); stock.LoadData(); Console.WriteLine("File loaded, total {0} items", stock.Data.Count); LineProtocolPayload payload = new LineProtocolPayload(); foreach (StockData d in stock.Data) { LineProtocolPoint point = new LineProtocolPoint( "tb_nasdaq", new Dictionary <string, object> { { "opening_price", d.opening_price }, { "highest_price", d.highest_price }, { "lowest_price", d.lowest_price }, { "closing_price", d.closing_price }, { "adjusted_closing_price", d.adjusted_closing_price }, { "trade_volume", d.trade_volume }, }, new Dictionary <string, string> { { "code", d.code } }, DateTime.Parse(d.date).ToUniversalTime()); payload.Add(point); } var result = client.WriteAsync(payload).GetAwaiter().GetResult(); if (!result.Success) { Console.WriteLine(result.ErrorMessage); } } currentFolder++; } DateTime end = DateTime.Now; Console.WriteLine("Total spent: {0} seconds", (end - start).TotalSeconds); }
private async static void add(IClient client, string status) { SensorDataPoint sdp = JsonSerializer.Deserialize <SensorDataPoint>(status); var cpuTime = new LineProtocolPoint( "sensors", new Dictionary <string, object> { { "temperature", sdp.temperature.ToString() }, { "humidity", sdp.humidity.ToString() } }, new Dictionary <string, string> { { "sensor", client.name }, { "location", _sensorDictionary[client.name].location } }, DateTime.UtcNow); var payload = new LineProtocolPayload(); payload.Add(cpuTime); var influx = new LineProtocolClient(new Uri("http://192.168.86.31:9999"), "firstdb"); var influxResult = await influx.WriteAsync(payload); if (!influxResult.Success) { Console.Error.WriteLine(influxResult.ErrorMessage); } }
private bool AddInvertersToPayload(List <Inverter> inverters, LineProtocolPayload payload) { var added = false; foreach (var inverter in inverters.Where(i => i.Production.LastReportDate > 0)) { var reportTime = DateTimeOffset.FromUnixTimeSeconds(inverter.Production.LastReportDate); var inverterPoint = new LineProtocolPoint( "inverter", //Measurement new Dictionary <string, object> //Fields { { $"lastreportwatts", inverter.Production.LastReportWatts }, { $"maxreportwatts", inverter.Production.MaxReportWatts }, }, new Dictionary <string, string> //Tags { { $"serialnumber", inverter.Production.SerialNumber }, }, reportTime.UtcDateTime); //Timestamp payload.Add(inverterPoint); added = true; } return(added); }
private static async Task Main(string[] args) { Console.WriteLine("Hello Old World!"); CollectorLog.RegisterErrorHandler((message, exception) => { Console.WriteLine($"{message}: {exception}"); }); var client = new LineProtocolClient(new Uri(DBUrl), DBName); var createEvents = new LineProtocolPayload(); for (var i = 0; i < PlanAmount; i++) { createEvents.Add(Create_PlanCreateDTO().ToLineProtocolPoint()); } var influxResult = await client.WriteAsync(createEvents); if (!influxResult.Success) { await Console.Error.WriteLineAsync(influxResult.ErrorMessage); } var changeEvents = new LineProtocolPayload(); PlanIDs.ForEach(planId => { for (var i = 0; i < ChangeEventPerPlanAmount; i++) { changeEvents.Add(Create_PlanChangeValueDTO(planId, i).ToLineProtocolPoint()); } }); influxResult = await client.WriteAsync(changeEvents); if (!influxResult.Success) { await Console.Error.WriteLineAsync(influxResult.ErrorMessage); } var deleteEvents = new LineProtocolPayload(); PlanIDs.ForEach(planId => { deleteEvents.Add(new PlanDeleteDTO { PlanId = planId, UserId = "1234567", PlanVersion = new Version(1, 2).ToString() }.ToLineProtocolPoint()); }); influxResult = await client.WriteAsync(deleteEvents); if (!influxResult.Success) { await Console.Error.WriteLineAsync(influxResult.ErrorMessage); } }
static void Main(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); while (true) { using ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api); var host = configuration.GetSection("Mikrotik").GetSection("Host").Value; var user = configuration.GetSection("Mikrotik").GetSection("User").Value; var password = configuration.GetSection("Mikrotik").GetSection("Password").Value; var influxDbEnabled = bool.Parse(configuration.GetSection("InfluxDb").GetSection("Enabled").Value); var influxDbUrl = configuration.GetSection("InfluxDb").GetSection("Url").Value; var influxDatabase = configuration.GetSection("InfluxDb").GetSection("Database").Value; var influxMeasurement = configuration.GetSection("InfluxDb").GetSection("Measurement").Value; var isConsoleOutput = bool.Parse(configuration.GetSection("App").GetSection("ConsoleOutput").Value); var sleepTimeout = int.Parse(configuration.GetSection("App").GetSection("SleepTimeout").Value); connection.Open(host, user, password); connection.ExecuteNonQuery("/ip/accounting/snapshot/take"); var interfaces = connection.LoadList <IpAddress>() .ToImmutableSortedDictionary(d => d.Address, a => a.Interface); var acctList = connection.LoadList <AccountingSnapshot>(); var payload = new LineProtocolPayload(); foreach (var item in acctList) { var point = new LineProtocolPoint( influxMeasurement, new Dictionary <string, object> { { "bytes", item.Bytes }, { "packets", item.Packets }, }, TagDirection(item, interfaces), DateTime.UtcNow); payload.Add(point); } if (influxDbEnabled) { var client = new LineProtocolClient(new Uri(influxDbUrl), influxDatabase); client.WriteAsync(payload); } if (isConsoleOutput) { var wr = new StringWriter(); payload.Format(wr); Console.WriteLine(wr.GetStringBuilder().ToString()); } Thread.Sleep(sleepTimeout); } }
public Task <LineProtocolWriteResult> WriteAsync(LineProtocolPayload payload, CancellationToken cancellationToken = default(CancellationToken)) { var writer = new StringWriter(); payload.Format(writer); return(SendAsync(writer.ToString(), cancellationToken)); }
public void when_null_point_ignore_and_dont_throw() { var payload = new LineProtocolPayload(); Action action = () => { payload.Add(null); }; action.ShouldNotThrow(); }
public LineProtocolWriteResult Write(LineProtocolPayload payload, CancellationToken cancellationToken = default(CancellationToken)) { var stringWriter = new StringWriter(); payload.Format(stringWriter); return(OnSend(stringWriter.ToString(), Precision.Nanoseconds, cancellationToken)); }
/// <summary> /// Sends metrics to the backend. /// </summary> /// <param name="payload">Metrics payload.</param> public void Send(LineProtocolPayload payload) { var payloadText = new StringWriter(); payload.Format(payloadText); byte[] content = Encoding.UTF8.GetBytes(payloadText.ToString()); this.udpClient.Send(content, content.Length); }
/// <summary> /// Sends metrics to the backend. /// </summary> /// <param name="payload">Metrics payload.</param> public void Send(LineProtocolPayload payload) { LineProtocolWriteResult result = this.WriteAsync(payload).GetAwaiter().GetResult(); if (!result.Success) { Trace.TraceError(result.ErrorMessage); } }
public InfluxPayload(int batchcount, Verbosity verbose) { lines = 0; batchnumber = batchcount; payload = new LineProtocolPayload(); if (verbose > Verbosity.lowlevel) { Console.WriteLine(">>>>>> new payload creation"); } }
public async Task <bool> Write(string db, string measurement, IReadOnlyDictionary <string, object> data, IReadOnlyDictionary <string, string> tags = null, DateTime?utcTimeStamp = null) { await _createDatabase(InfluxServerUrl, db); var ts = new Dictionary <string, string>(); //remember to check the container is runnings in the right timzone! if (utcTimeStamp != null) { //ts.Add("timestamp", utcTimeStamp?.ConvertToISO()); } else { utcTimeStamp = DateTime.UtcNow; } if (tags != null) { foreach (var key in tags.Keys) { ts.Add(key, tags[key]); } } var writer = new LineProtocolPoint( measurement, data, ts, utcTimeStamp); var payload = new LineProtocolPayload(); payload.Add(writer); var client = _getLineProtocolClient(db); try { var influxResult = await client.WriteAsync(payload); if (!influxResult.Success) { _logService.WriteError(influxResult.ErrorMessage); } return(influxResult.Success); } catch (Exception ex) { _logService.WriteError(ex.ToString()); return(false); } }
public LineProtocolClientTests() { _payload = new LineProtocolPayload(); var fieldsOne = new Dictionary <string, object> { { "key", "value" } }; var timestampOne = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc); var pointOne = new LineProtocolPoint("measurement", fieldsOne, MetricTags.Empty, timestampOne); _payload.Add(pointOne); }
public Task <LineProtocolWriteResult> WriteAsync(LineProtocolPayload source, CancellationToken cancellationToken = default(CancellationToken)) { if (source == null) { return(Task.FromResult(new LineProtocolWriteResult(true, string.Empty))); } var client = new LineProtocolClient(new Uri(connectionString), database); return(client.WriteAsync(source, cancellationToken)); }
private List <LineProtocolPoint> GetDataPoints(TelegrafReport telegrafReport) { var payloadField = telegrafReport.GetType().GetField("payload", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); LineProtocolPayload payload = (LineProtocolPayload)payloadField.GetValue(telegrafReport); var pointsField = payload.GetType().GetField("_points", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); return(pointsField.GetValue(payload) as List <LineProtocolPoint>); }
public async Task Send(CancellationToken cancellationToken) { if (_client == null) { return; } try { await _sendLock.WaitAsync(cancellationToken); _logger.LogInformation("Starting InfluxDB metrics send."); try { var allPasses = _passRepository.Get() .Where(x => x.StartTime > _lastPass) .OrderBy(x => x.StartTime) .ToList(); foreach (var chunk in allPasses.Chunk(500)) { var points = new LineProtocolPayload(); foreach (var pass in chunk) { points.Add(MapPass(pass)); } var writeResult = await _client.WriteAsync(points, cancellationToken); if (!writeResult.Success) { throw new Exception(writeResult.ErrorMessage); } _logger.LogInformation("Written {PassCount} passes to InfluxDB", chunk.Length); _lastPass = chunk.Max(x => x.StartTime); } _logger.LogInformation("InfluxDB write success!"); } catch (Exception ex) { _logger.LogError(ex, "Error while sending InfluxDB metrics"); } } finally { _sendLock.Release(); } }
public async Task Handle(WatchlistRefreshedNotification notification, CancellationToken cancellationToken) { _logger.LogInformation("start writing watchlist to influxdb"); try { var payload = new LineProtocolPayload(); foreach (var currency in notification.Currencies) { var point = new LineProtocolPoint( "watchlist", new Dictionary <string, object> { { "btc_volume", currency.BitcoinVolume }, //{ "high_trade", currency.HighTrade }, { "last_price", currency.LastPrice }, //{ "low_trade", currency.LowTrade }, //{ "previous_price", currency.PreviousPrice }, { "volume", currency.Volume }, { "server_time", currency.ServerTime.Ticks }, { "market", currency.MarketName }, }, new Dictionary <string, string> { //{ "primary_currency", currency.PrimaryCurrencyName }, //{ "secondary_currency", currency.SecondaryCurrencyName }, { "market_name", currency.MarketName }, { "exchange_code", currency.ExchangeCode } }, DateTime.UtcNow ); payload.Add(point); } var influxResult = await _lineProtocolClient.WriteAsync(payload); if (!influxResult.Success) { throw new Exception("unable to write data to influxdb! error: " + influxResult.ErrorMessage); } else { _logger.LogInformation("data successfully written to influxdb"); } } catch (Exception e) { _logger.LogError(e, "unable to write data to influxdb"); } }
public void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Free any other managed objects here. _payload = null; } } _disposed = true; }
private void SendCurrentPayload() { if ((this.influxClient != null) && (this.currentPayload != null)) { var result = this.influxClient.WriteAsync(this.currentPayload).Result; if (!result.Success) { log.Error($"Error (ignored) writing Influx data: {result.ErrorMessage}"); } } this.currentPayload = null; }
/// <summary> /// Writes metrics to the backend asynchronously. /// </summary> /// <param name="payload">Metrics payload.</param> /// <param name="cancellationToken">Optional operation cancellation token.</param> /// <returns><see cref="System.Threading.Tasks.Task"/>, that performs the write operation.</returns> private async Task <LineProtocolWriteResult> WriteAsync(LineProtocolPayload payload, CancellationToken cancellationToken = default(CancellationToken)) { var payloadText = new StringWriter(); payload.Format(payloadText); var content = new StringContent(payloadText.ToString(), Encoding.UTF8); var response = await this.httpClient.PostAsync(string.Empty, content, cancellationToken).ConfigureAwait(false); if (response.IsSuccessStatusCode) { return(new LineProtocolWriteResult(true, null)); } return(new LineProtocolWriteResult(false, string.Format("{0} {1}", response.StatusCode, response.ReasonPhrase))); }
public void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Free any other managed objects here. _payload = null; } } _logger.LogDebug("InfluxDB Reporter Disposed"); _disposed = true; }
public void Emit(PointData[] points) { var payload = new LineProtocolPayload(); foreach (var point in points) { payload.Add(new LineProtocolPoint(point.Measurement, point.Fields, point.Tags, point.UtcTimestamp)); } var influxResult = _client.Write(payload); if (!influxResult.Success) { throw new Exception(influxResult.ErrorMessage); } }
public void Emit(PointData[] points) { var payload = new LineProtocolPayload(); foreach (var point in points) { payload.Add(new LineProtocolPoint(point.Measurement, point.Fields, point.Tags, point.UtcTimestamp)); } var influxResult = _client.WriteAsync(payload).Result; if (!influxResult.Success) { CollectorLog.WriteLine(influxResult.ErrorMessage); } }