Worker.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using NLog;
  2. using LibreHardwareMonitor.Hardware;
  3. using Prometheus;
  4. using System.Threading.Tasks;
  5. using System.Threading;
  6. using Microsoft.Extensions.Hosting;
  7. namespace OhmGraphite
  8. {
  9. class Worker : BackgroundService
  10. {
  11. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  12. private readonly MetricConfig config;
  13. public Worker(MetricConfig config)
  14. {
  15. this.config = config;
  16. }
  17. protected override Task ExecuteAsync(CancellationToken stoppingToken)
  18. {
  19. return Task.Run(() =>
  20. {
  21. using var app = CreateOhmGraphite(config);
  22. app.Start();
  23. stoppingToken.WaitHandle.WaitOne();
  24. }, stoppingToken);
  25. }
  26. private static IManage CreateOhmGraphite(MetricConfig config)
  27. {
  28. var computer = new Computer
  29. {
  30. IsGpuEnabled = config.EnabledHardware.Gpu,
  31. IsMotherboardEnabled = config.EnabledHardware.Motherboard,
  32. IsCpuEnabled = config.EnabledHardware.Cpu,
  33. IsMemoryEnabled = config.EnabledHardware.Ram,
  34. IsNetworkEnabled = config.EnabledHardware.Network,
  35. IsStorageEnabled = config.EnabledHardware.Storage,
  36. IsControllerEnabled = config.EnabledHardware.Controller,
  37. IsPsuEnabled = config.EnabledHardware.Psu,
  38. IsBatteryEnabled = config.EnabledHardware.Battery,
  39. };
  40. var collector = new SensorCollector(computer, config);
  41. return CreateManager(config, collector);
  42. }
  43. private static IManage CreateManager(MetricConfig config, SensorCollector collector)
  44. {
  45. var hostname = config.LookupName();
  46. double seconds = config.Interval.TotalSeconds;
  47. if (config.Graphite != null)
  48. {
  49. Logger.Info(
  50. $"Graphite host: {config.Graphite.Host} port: {config.Graphite.Port} interval: {seconds} tags: {config.Graphite.Tags}");
  51. var writer = new GraphiteWriter(config.Graphite.Host,
  52. config.Graphite.Port,
  53. hostname,
  54. config.Graphite.Tags);
  55. return new MetricTimer(config.Interval, collector, writer);
  56. }
  57. else if (config.Prometheus != null)
  58. {
  59. Logger.Info($"Prometheus port: {config.Prometheus.Port}");
  60. var registry = PrometheusCollection.SetupDefault(collector);
  61. var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port, url: config.Prometheus.Path, registry: registry, useHttps: config.Prometheus.UseHttps);
  62. return new PrometheusServer(server, collector);
  63. }
  64. else if (config.Timescale != null)
  65. {
  66. var writer = new TimescaleWriter(config.Timescale.Connection, config.Timescale.SetupTable, hostname);
  67. return new MetricTimer(config.Interval, collector, writer);
  68. }
  69. else if (config.Influx != null)
  70. {
  71. Logger.Info($"Influxdb address: {config.Influx.Address} db: {config.Influx.Db}");
  72. var writer = new InfluxWriter(config.Influx, hostname);
  73. return new MetricTimer(config.Interval, collector, writer);
  74. }
  75. else
  76. {
  77. Logger.Info($"Influx2 address: {config.Influx2.Options.Url}");
  78. var writer = new Influx2Writer(config.Influx2, hostname);
  79. return new MetricTimer(config.Interval, collector, writer);
  80. }
  81. }
  82. }
  83. }