Program.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var hostname = config.LookupName();
  50. double seconds = config.Interval.TotalSeconds;
  51. if (config.Graphite != null)
  52. {
  53. Logger.Info(
  54. $"Graphite host: {config.Graphite.Host} port: {config.Graphite.Port} interval: {seconds} tags: {config.Graphite.Tags}");
  55. var writer = new GraphiteWriter(config.Graphite.Host,
  56. config.Graphite.Port,
  57. hostname,
  58. config.Graphite.Tags);
  59. return new MetricTimer(config.Interval, collector, writer);
  60. }
  61. else if (config.Prometheus != null)
  62. {
  63. Logger.Info($"Prometheus port: {config.Prometheus.Port}");
  64. var prometheusCollection = new PrometheusCollection(collector, hostname, Metrics.DefaultRegistry);
  65. var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port);
  66. return new PrometheusServer(server, collector);
  67. }
  68. else if (config.Timescale != null)
  69. {
  70. var writer = new TimescaleWriter(config.Timescale.Connection, config.Timescale.SetupTable, hostname);
  71. return new MetricTimer(config.Interval, collector, writer);
  72. }
  73. else
  74. {
  75. Logger.Info($"Influxdb address: {config.Influx.Address} db: {config.Influx.Db}");
  76. var writer = new InfluxWriter(config.Influx, hostname);
  77. return new MetricTimer(config.Interval, collector, writer);
  78. }
  79. }
  80. }
  81. }