1
0

Program.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using NLog;
  3. using OpenHardwareMonitor.Hardware;
  4. using Prometheus;
  5. using Topshelf;
  6. namespace OhmGraphite
  7. {
  8. internal class Program
  9. {
  10. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  11. private static void Main(string[] args)
  12. {
  13. HostFactory.Run(x =>
  14. {
  15. x.Service<IManage>(s =>
  16. {
  17. // We'll want to capture all available hardware metrics
  18. // to send to graphite
  19. var computer = new Computer
  20. {
  21. GPUEnabled = true,
  22. MainboardEnabled = true,
  23. CPUEnabled = true,
  24. RAMEnabled = true,
  25. FanControllerEnabled = true,
  26. HDDEnabled = true,
  27. NICEnabled = true
  28. };
  29. var collector = new SensorCollector(computer);
  30. // We need to know where the graphite server lives and how often
  31. // to poll the hardware
  32. var config = Logger.LogFunction("parse config", () => MetricConfig.ParseAppSettings(new AppConfigManager()));
  33. var metricsManager = CreateManager(config, collector);
  34. s.ConstructUsing(name => metricsManager);
  35. s.WhenStarted(tc => tc.Start());
  36. s.WhenStopped(tc => tc.Stop());
  37. });
  38. x.UseNLog();
  39. x.RunAsLocalSystem();
  40. x.SetDescription(
  41. "Extract hardware sensor data and exports it to a given host and port in a graphite compatible format");
  42. x.SetDisplayName("Ohm Graphite");
  43. x.SetServiceName("OhmGraphite");
  44. x.OnException(ex => Logger.Error(ex, "OhmGraphite TopShelf encountered an error"));
  45. });
  46. }
  47. private static IManage CreateManager(MetricConfig config, SensorCollector collector)
  48. {
  49. double seconds = config.Interval.TotalSeconds;
  50. if (config.Graphite != null)
  51. {
  52. Logger.Info(
  53. $"Graphite host: {config.Graphite.Host} port: {config.Graphite.Port} interval: {seconds} tags: {config.Graphite.Tags}");
  54. var writer = new GraphiteWriter(config.Graphite.Host,
  55. config.Graphite.Port,
  56. Environment.MachineName,
  57. config.Graphite.Tags);
  58. return new MetricTimer(config.Interval, collector, writer);
  59. }
  60. else if (config.Prometheus != null)
  61. {
  62. Logger.Info($"Prometheus port: {config.Prometheus.Port}");
  63. var prometheusCollection = new PrometheusCollection(collector, Environment.MachineName, Metrics.DefaultRegistry);
  64. var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port);
  65. return new PrometheusServer(server, collector);
  66. }
  67. else if (config.Timescale != null)
  68. {
  69. var writer = new TimescaleWriter(config.Timescale.Connection, config.Timescale.SetupTable, Environment.MachineName);
  70. return new MetricTimer(config.Interval, collector, writer);
  71. }
  72. else
  73. {
  74. Logger.Info($"Influxdb address: {config.Influx.Address} db: {config.Influx.Db}");
  75. var writer = new InfluxWriter(config.Influx, Environment.MachineName);
  76. return new MetricTimer(config.Interval, collector, writer);
  77. }
  78. }
  79. }
  80. }