public TriteURIMonitorWorker(string serverName, string monitorURI, int offsetCount, TriteURIMonitorWorkerSettings monitorSettings, Dictionary <string, string> tags, string measurement, ILogger <TriteURIMonitorWorker> logger) { this.serverName = serverName; this.monitorURI = monitorURI; this.logger = logger; timer = new Stopwatch(); this.nextScan = DateTime.Now + (monitorSettings.OffsetAmount * offsetCount); delay = monitorSettings.ScanDelay; influxClient = new LineProtocolClient( new Uri(monitorSettings.Influx.URI), monitorSettings.Influx.Database, monitorSettings.Influx.Username, monitorSettings.Influx.Password); measurementName = measurement; this.tags = tags; this.tags.Add("host", serverName); logger.LogInformation($"Starting worker for: {monitorURI} (Scan delay: {delay}, next scan: {nextScan})"); }
private static void ConfigureMetrics(IConfiguration configuration, IServiceCollection services) { var config = new MetricsConfiguration(); configuration.GetSection("metrics").Bind(config); var influx = config.InfluxDb; var serverBaseAddress = new Uri(influx.BaseUri); var client = new LineProtocolClient(serverBaseAddress, influx.Database, influx.Username, influx.Password); var collector = new QueuedMetricsCollector(client); services.AddHostedService(provider => { var timer = new HostedQueueTimer(collector, TimeSpan.FromSeconds(config.FlushInterval)); timer.OnError += e => Console.Error.WriteLine(e); return(timer); }); var predefinedTags = new Dictionary <string, string> { { "hostname", Environment.MachineName } }; services.AddSingleton(new MeasurementWriterFactory(predefinedTags, collector)); }
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 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); } }
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); }
/// <summary> /// /// </summary> /// <param name="database"></param> /// <param name="start"></param> /// <param name="end"></param> /// <param name="uri"></param> /// <param name="batchsize">size of the batches in ponts</param> /// <returns>nb of batchs done</returns> public async Task <Tuple <int, int> > writeInfludb() { //get properties for fields and tags (tags=indexed fields in influxdb) var tags = _csvdataloading.Datatype.GetProperties().Where(p => p.GetCustomAttributes().Any(a => a is TagAttribute)); var fields = _csvdataloading.Datatype.GetProperties().Where(p => p.GetCustomAttributes().Any(a => a is FieldAttribute)); string measurementname = (Options.tablename ?? _csvdataloading.Datatype.Name).ToLower(); var client = new LineProtocolClient(new Uri(Options.serveruri), Options.database); ConcurrentQueue <InfluxPayload> payloadQueue = new ConcurrentQueue <InfluxPayload>(); return(await _csvdataloading.BatchRunAsync( // Action per line item => { InfluxPayload bunch; if (!payloadQueue.TryPeek(out bunch)) { throw new Exception("Algo exception : unable to retreive payload bunch"); } if (item.Time.HasValue) //without time, no time-serie ! { var tagsvalue = new Dictionary <string, string>(tags.Select(t => new KeyValuePair <string, string>(t.Name.ToLower(), t.GetValue(item).ToString()))); var fieldsvalue = new Dictionary <string, object>(fields.Select(t => new KeyValuePair <string, object>(t.Name.ToLower(), t.GetValue(item)))); var influxline = new LineProtocolPoint(measurementname, fieldsvalue, tagsvalue, item.Time.Value); bunch.payload.Add(influxline); bunch.lines++; if (_verbose == Verbosity.verbose) { Console.WriteLine($"add point {bunch.lines} to PL #{bunch.batchnumber} : >{item}"); } } return false; }, //actionBatchStart async batchcount => { payloadQueue.Enqueue(new InfluxPayload(batchcount, Options.Verbose)); }, actionBatchEnd : async(batchcount, lines, totallines) => { InfluxPayload bunch; if (payloadQueue.TryDequeue(out bunch)) { Stopwatch sw = Stopwatch.StartNew(); if (Options.Verbose > Verbosity.mute) { Console.WriteLine($">>> start to send batch of data #{bunch.batchnumber} ({bunch.lines} lines) to influxdb. total line already sent : {totallines}"); } var resas = await client.WriteAsync(bunch.payload); if (!resas.Success) { Console.Error.WriteLine($">>>!!!!>>> Error sending to influx the batch #{bunch.batchnumber} : {resas.ErrorMessage}"); } Console.WriteLine($">>> batch #{bunch.batchnumber} ({bunch.lines} lines) sent to the server in {sw.ElapsedMilliseconds}ms : OK"); bunch.payload = null; bunch = null; } }, start : Options.startline, end : Options.endline, batchsize : Options.batchsize)); }
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 HttpLineProtocolEmitter(LineProtocolClient client) { if (client == null) { throw new ArgumentNullException(nameof(client)); } _client = client; }
public InfluxDbReporter(string name, Uri serverBaseAddress, string username, string password, string database, string breakerRate, TimeSpan interval, string retentionPolicy, string consistency) { ReportInterval = interval; Name = name; _influxDbClient = new LineProtocolClient(serverBaseAddress, database, username, password, retentionPolicy, consistency, breakerRate); }
static async Task <LineProtocolWriteResult> WriteAsync(string connectionString, string database, LineProtocolPayload source, CancellationToken cancellationToken = default(CancellationToken)) { if (source == null) { return(Task.FromResult(new LineProtocolWriteResult(true, string.Empty)).Result); } var client = new LineProtocolClient(new Uri(connectionString), database); return(await client.WriteAsync(source, cancellationToken)); }
public LineProtocolPayloadClient(LineProtocolClientOptions lineProtocolClientOptions, IInternalLogger logger = null) { _logger = logger; _lineProtocolClientOptions = lineProtocolClientOptions ?? throw new ArgumentNullException(nameof(lineProtocolClientOptions)); _lineProtocolClient = new InternalLineProtocolClient( _lineProtocolClientOptions.Server, _lineProtocolClientOptions.Database, _lineProtocolClientOptions.UserName, _lineProtocolClientOptions.Password); var interval = lineProtocolClientOptions.Interval.HasValue && lineProtocolClientOptions.Interval.Value > 0 ? lineProtocolClientOptions.Interval.Value : _defaultInterval; _blockCapacity = lineProtocolClientOptions.BlockCapacity.GetValueOrDefault(_defaultBlockCapacity); _pointMap = new ConcurrentDictionary <PointState, object>(); _flushTimer = new Timer(async state => await FlushCallback(state), null, TimeSpan.FromSeconds(interval), TimeSpan.FromSeconds(interval)); _logger?.LogInformation("Start LineProtocolCollector."); }
public InfluxMetricsSender(ILogger <InfluxMetricsSender> logger, ISatellitePassRepository passRepository, IOptions <InfluxMetricsConfiguration> options) { _logger = logger; _passRepository = passRepository; var config = options.Value; if (string.IsNullOrEmpty(config.Url)) { _logger.LogInformation("InfluxDB Url not set, skipping pass metrics"); } else { _client = new LineProtocolClient(new Uri(config.Url), config.Database, config.Username, config.Password); } }
/// <summary> /// Disposes off this instance. /// </summary> /// <param name="disposing"><c>true</c> if called from Dispose method. <c>false</c> if called from finalizer.</param> protected virtual void Dispose(bool disposing) { if (!this.disposedValue) { if (disposing) { this.SendCurrentPayload(); this.influxClient = null; // it's not IDisposable } // TODO: nicht verwaltete Ressourcen (nicht verwaltete Objekte) freigeben und Finalizer weiter unten überschreiben. // TODO: große Felder auf Null setzen. this.disposedValue = true; } }
public async Task ReportMetrics(DateTime reportTime, IEnumerable <ReportedValue> sensors) { var payload = new LineProtocolPayload(); var client = new LineProtocolClient(_config.Address, _config.Db, _config.User, _config.Password); foreach (var point in sensors.Select(x => NewPoint(reportTime, x))) { payload.Add(point); } var result = await client.WriteAsync(payload); if (!result.Success) { Logger.Error("Influxdb encountered an error: {0}", result.ErrorMessage); } }
static void Main(string[] args) { lineProtocolClient = new LineProtocolClient(new Uri("http://localhost:8086"), "rtl433"); var process = new Process(); process.StartInfo.FileName = "/usr/local/bin/rtl_433"; process.StartInfo.Arguments = "-F json -M newmodel"; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.OutputDataReceived += cmd_DataReceived; process.EnableRaisingEvents = true; process.Start(); process.BeginOutputReadLine(); process.WaitForExit(); }
/// <summary> /// Creates the Influx client or sets the field to null if Influx is not configured. /// </summary> private void CreateInfluxClient() { if (this.influxClient != null) { return; } string currentKey = "DatabaseUri"; string databaseUri = this.influxConfiguration.GetValue <string>(currentKey); if (string.IsNullOrWhiteSpace(databaseUri)) { throw new InvalidOperationException($"Influx database: No key '{currentKey}' in '{Program.InfluxSectionKey}' configration section"); } currentKey = "DatabaseName"; string databaseName = this.influxConfiguration.GetValue <string>(currentKey); if (string.IsNullOrWhiteSpace(databaseName)) { throw new InvalidOperationException($"Influx database: No key '{currentKey}' in '{Program.InfluxSectionKey}' configration section"); } string databaseUser = this.influxConfiguration.GetValue <string>("DatabaseUser"); if (string.IsNullOrWhiteSpace(databaseUser)) { databaseUser = null; } string databasePassword = this.influxConfiguration.GetValue <string>("DatabasePassword"); if (string.IsNullOrWhiteSpace(databasePassword)) { databasePassword = null; } if ((databaseUser == null) && (databasePassword != null)) { throw new InvalidOperationException($"Influx database: Inconsistent Influx database configuration: Found password but no user name in '{Program.InfluxSectionKey}' configration section"); } log.Info($"Initialized Influx reporting to URI '{databaseUri}', database '{databaseName}'"); this.influxClient = new LineProtocolClient(new Uri(databaseUri), databaseName, databaseUser, databasePassword); }
public async Task WriteAsync(Production systemProduction, Production production, Production consumption, List <Inverter> inverters) { var payload = new LineProtocolPayload(); AddProductionToPayload("inverters", systemProduction, payload); AddProductionToPayload("production", production, payload); AddProductionToPayload("consumption", consumption, payload); AddInvertersToPayload(inverters, payload); var client = new LineProtocolClient(url, database, username, password); var writeResult = await client.WriteAsync(payload); if (!writeResult.Success) { throw new Exception(writeResult.ErrorMessage); } }
public Station(string masterBrickHost, int masterBrickPort, string stationName, string influxUri, string influxDb, string influxUser, string influxPassword, int callbackperiod, int altitudeOffset = 0) { _masterBrickHost = masterBrickHost; _masterBrickPort = masterBrickPort; StationName = stationName; _influxClient = new LineProtocolClient(new Uri(influxUri), influxDb, influxUser, influxPassword); _sensors = new List <ISensor>(); AltitudeOffset = altitudeOffset; if (callbackperiod == -1) { Callbackperiod = 5000; } else { Callbackperiod = callbackperiod * 1000; } }
public InfluxDbReporter(string name, Uri serverBaseAddress, string username, string password, string database, string breakerRate, TimeSpan interval, string retentionPolicy, string consistency, ILoggerFactory loggerFactory) { ReportInterval = interval; Name = name; _logger = loggerFactory.CreateLogger <InfluxDbReporter>(); _influxDbClient = new LineProtocolClient(serverBaseAddress, database, loggerFactory, username, password, retentionPolicy, consistency, breakerRate); _successMeter = new MeterOptions { Context = InfluxDbMetricsContext, Name = "report_success" }; _failedMeter = new MeterOptions { Context = InfluxDbMetricsContext, Name = "report_failed" }; }
public async Task <WriteResult> WriteAsync(SystemProduction systemProduction, List <Inverter> inverters) { var payload = new LineProtocolPayload(); var systemPayloadAdded = AddSystemProductionToPayload(systemProduction, payload); var invertersPayloadAdded = AddInvertersToPayload(inverters, payload); if (!systemPayloadAdded && !invertersPayloadAdded) { return(WriteResult.NoNeedToWrite); } var client = new LineProtocolClient(url, database); var writeResult = await client.WriteAsync(payload); if (!writeResult.Success) { throw new Exception(writeResult.ErrorMessage); } return(WriteResult.Success); }
public InfluxDbProcessObserver(string url, string db, string table, string username, string password) { this.table = table; client = new LineProtocolClient(new Uri(url), db, username, password); }
void monitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e) { //This part of the code is needed to manage issues related to windows forms if (this.InvokeRequired) { this.BeginInvoke(new MonitoredItemNotificationEventHandler(monitoredItem_Notification), monitoredItem, e); return; } MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification; if (notification == null) { return; } //PROJECT //updating the Mould used (to structure the Database) //is call because of Mould_Type? if (monitoredItem.ClientHandle == Mould_Type_handle) { int value = (int)notification.Value.WrappedValue.Value; Mould_Type = value; //update screen label5.Text = "value: " + Utils.Format("{0}", Mould_Type.ToString()) + ";\nStatusCode: " + Utils.Format("{0}", notification.Value.StatusCode.ToString()) + ";\nSource timestamp: " + notification.Value.SourceTimestamp.ToString() + ";\nServer timestamp: " + notification.Value.ServerTimestamp.ToString(); } //is call because of Pressure_Array? DataValueCollection read2 = Read_Value("ns=3;s=Start_Piece"); if (monitoredItem.ClientHandle == Pressure_Array_handle && (bool)read2[0].Value) { double[] value = (double[])notification.Value.WrappedValue.Value; Pressure_Array = value; for (int i = 0; i < 28; i++) { Pressure_1[i] = Pressure_Array[i]; Pressure_2[i] = Pressure_Array[i + 28]; } label1.Text = Pressure_1[1].ToString(); DataValueCollection read = Read_Value("ns=3;s=Actual_Mould"); DataValueCollection read1 = Read_Value("ns=3;s=Piece_SN"); if ((int)read[0].Value == 1) { var payload = new LineProtocolPayload(); //when new value of Pressure_1 then update DB for (int j = 0; j < 28; j++) { DateTime t = DateTime.UtcNow.Subtract(TimeSpan.FromMilliseconds((27 - j) * 285)); var DBMould1 = new LineProtocolPoint( "Mould_1__MLQC_DB", new Dictionary <string, object> { { "Pressure 1", Pressure_1[j] }, { "Pressure 2", Pressure_2[j] }, { "Piece Serial Number", (int)read1[0].Value } }, null, t ); payload.Add(DBMould1); } var client = new LineProtocolClient(new Uri(DB_server), DB_database); //send data to DB var influxResult = client.WriteAsync(payload); } if ((int)read[0].Value == 2) { var payload = new LineProtocolPayload(); //when new value of Pressure_1 then update DB for (int j = 0; j < 28; j++) { DateTime t = DateTime.UtcNow.Subtract(TimeSpan.FromMilliseconds((27 - j) * 50)); var DBMould1 = new LineProtocolPoint( "Mould_2__MLQC_DB", new Dictionary <string, object> { { "Pressure 1 =", Pressure_1[j] }, { "Pressure 2 =", Pressure_2[j] } }, null, t ); payload.Add(DBMould1); } var client = new LineProtocolClient(new Uri(DB_server), DB_database); //send data to DB var influxResult = client.WriteAsync(payload); } } }
public InfluxDbBalanceRefreshedHandler(ILogger <InfluxDbBalanceRefreshedHandler> logger, LineProtocolClient lineProtocolClient) { _logger = logger; _lineProtocolClient = lineProtocolClient; }
public override CollectorConfiguration InfluxDB(Uri serverBaseAddress, string database, string username = null, string password = null) { _client = new LineProtocolClient(serverBaseAddress, database, username, password); return(_configuration); }
//LineProtocolPayload payload; public void initDB() { client = new LineProtocolClient(new Uri(databaseAddress), databaseName); //payload = new LineProtocolPayload(); }
public void Init(string connectionString) { influxdbUri = connectionString; client = new LineProtocolClient(new Uri(influxdbUri), "hal_data"); }
public QueuedMetricsCollector(LineProtocolClient client) { _client = client; _queue = new ConcurrentQueue <LineProtocolPoint>(); }
public InfluxDbPublisher(string uri, string database) { _client = new LineProtocolClient(new Uri(uri), database); }
public PlantController(LineProtocolClient client) { Client = client; }