Skip to content

Commit

Permalink
Merge pull request #213 from nickbabcock/inf-revert
Browse files Browse the repository at this point in the history
Revert accidental influx experiment
  • Loading branch information
nickbabcock authored May 8, 2021
2 parents 07178dc + 95d60a4 commit ff8780f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 74 deletions.
33 changes: 0 additions & 33 deletions OhmGraphite.Test/InfluxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,5 @@ public async void CanInsertIntoPasswordLessInfluxdb()
}
}
}

//[Fact, Trait("Category", "integration")]
//public async void CanInsertIntoInflux2()
//{
// var config = new InfluxConfig(new Uri("http://influx2:8086"), "mydb", "my_user", "my_pass");
// using (var writer = new InfluxWriter(config, "my-pc"))
// using (var client = new HttpClient())
// {
// for (int attempts = 0; ; attempts++)
// {
// try
// {
// await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

// var resp = await client.GetAsync(
// "http://influx2:8086/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
// Assert.True(resp.IsSuccessStatusCode);
// var content = await resp.Content.ReadAsStringAsync();
// Assert.Contains("/intelcpu/0/temperature/0", content);
// break;
// }
// catch (Exception)
// {
// if (attempts >= 10)
// {
// throw;
// }

// Thread.Sleep(TimeSpan.FromSeconds(1));
// }
// }
// }
//}
}
}
50 changes: 9 additions & 41 deletions OhmGraphite/InfluxWriter.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,42 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using InfluxDB.LineProtocol.Client;
using InfluxDB.LineProtocol.Payload;
using NLog;
using LibreHardwareMonitor.Hardware;

namespace OhmGraphite
{
public class InfluxWriter : IWriteMetrics
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly HttpClient _client = new HttpClient();

private readonly InfluxConfig _config;
private readonly string _localHost;

public InfluxWriter(InfluxConfig config, string localHost)
{
_config = config;
_localHost = localHost;

if (!string.IsNullOrEmpty(_config.User) && !string.IsNullOrEmpty(_config.Password))
{
var raw = Encoding.UTF8.GetBytes($"{_config.User}:{_config.Password}");
var encoded = Convert.ToBase64String(raw);
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
}
}

public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
{
var payload = new LineProtocolPayload();
var password = _config.User != null ? (_config.Password ?? "") : null;
var client = new LineProtocolClient(_config.Address, _config.Db, _config.User, password);

foreach (var point in sensors.Select(x => NewPoint(reportTime, x)))
{
payload.Add(point);
}

// can't use influx db client as they don't have one that is both 1.x and 2.x compatible
// so we implement our own compatible client
var formattedData = new StringWriter();
payload.Format(formattedData);
formattedData.Flush();
var outData = Encoding.UTF8.GetBytes(formattedData.ToString());
var content = new ByteArrayContent(outData);

var queries = new List<string>();

// passwordless authentication
if (!string.IsNullOrEmpty(_config.User) && string.IsNullOrEmpty(_config.Password))
{
queries.Add($"u={_config.User}&p=");
}

if (!string.IsNullOrEmpty(_config.Db))
{
queries.Add($"db={_config.Db}");
}

var qs = string.Join("&", queries);
qs = !string.IsNullOrEmpty(qs) ? $"?{qs}" : "";
var addrPath = new Uri(_config.Address, "/write");
var url = $"{addrPath}{qs}";
var response = await _client.PostAsync(url, content);
if (!response.IsSuccessStatusCode)
var result = await client.WriteAsync(payload);
if (!result.Success)
{
var err = await response.Content.ReadAsStringAsync();
Logger.Error("Influxdb encountered an error: {0}", err);
Logger.Error("Influxdb encountered an error: {0}", result.ErrorMessage);
}
}

Expand Down

0 comments on commit ff8780f

Please sign in to comment.