1
0

Program.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Configuration;
  3. using System.IO;
  4. using NLog;
  5. using LibreHardwareMonitor.Hardware;
  6. using OhmGraphite.Test;
  7. using Prometheus;
  8. using Topshelf;
  9. namespace OhmGraphite
  10. {
  11. internal class Program
  12. {
  13. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  14. private static void Main()
  15. {
  16. string configPath = string.Empty;
  17. HostFactory.Run(x =>
  18. {
  19. x.Service<IManage>(s =>
  20. {
  21. // We'll want to capture all available hardware metrics
  22. // to send to graphite
  23. var computer = new Computer
  24. {
  25. IsGpuEnabled = true,
  26. IsMotherboardEnabled = true,
  27. IsCpuEnabled = true,
  28. IsMemoryEnabled = true,
  29. IsNetworkEnabled = true,
  30. IsStorageEnabled = true,
  31. IsControllerEnabled = true
  32. };
  33. s.ConstructUsing(name =>
  34. {
  35. var configDisplay = string.IsNullOrEmpty(configPath) ? "default" : configPath;
  36. var config = Logger.LogFunction($"parse config {configDisplay}", () => MetricConfig.ParseAppSettings(CreateConfiguration(configPath)));
  37. var collector = new SensorCollector(computer, config);
  38. return CreateManager(config, collector);
  39. });
  40. s.WhenStarted(tc => tc.Start());
  41. s.WhenStopped(tc => tc.Dispose());
  42. });
  43. // Allow one to specify a command line argument when running interactively
  44. x.AddCommandLineDefinition("config", v => configPath = v);
  45. x.UseNLog();
  46. x.RunAsLocalSystem();
  47. x.SetDescription(
  48. "Extract hardware sensor data and exports it to a given host and port in a graphite compatible format");
  49. x.SetDisplayName("Ohm Graphite");
  50. x.SetServiceName("OhmGraphite");
  51. x.OnException(ex => Logger.Error(ex, "OhmGraphite TopShelf encountered an error"));
  52. });
  53. }
  54. private static IAppConfig CreateConfiguration(string configPath)
  55. {
  56. if (string.IsNullOrEmpty(configPath))
  57. {
  58. return new AppConfigManager();
  59. }
  60. if (!File.Exists(configPath))
  61. {
  62. throw new ApplicationException($"unable to detect config: ${configPath}");
  63. }
  64. var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
  65. var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  66. return new CustomConfig(config);
  67. }
  68. private static IManage CreateManager(MetricConfig config, SensorCollector collector)
  69. {
  70. var hostname = config.LookupName();
  71. double seconds = config.Interval.TotalSeconds;
  72. if (config.Graphite != null)
  73. {
  74. Logger.Info(
  75. $"Graphite host: {config.Graphite.Host} port: {config.Graphite.Port} interval: {seconds} tags: {config.Graphite.Tags}");
  76. var writer = new GraphiteWriter(config.Graphite.Host,
  77. config.Graphite.Port,
  78. hostname,
  79. config.Graphite.Tags);
  80. return new MetricTimer(config.Interval, collector, writer);
  81. }
  82. else if (config.Prometheus != null)
  83. {
  84. Logger.Info($"Prometheus port: {config.Prometheus.Port}");
  85. var registry = PrometheusCollection.SetupDefault(collector);
  86. var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port, registry: registry);
  87. return new PrometheusServer(server, collector);
  88. }
  89. else if (config.Timescale != null)
  90. {
  91. var writer = new TimescaleWriter(config.Timescale.Connection, config.Timescale.SetupTable, hostname);
  92. return new MetricTimer(config.Interval, collector, writer);
  93. }
  94. else
  95. {
  96. Logger.Info($"Influxdb address: {config.Influx.Address} db: {config.Influx.Db}");
  97. var writer = new InfluxWriter(config.Influx, hostname);
  98. return new MetricTimer(config.Interval, collector, writer);
  99. }
  100. }
  101. }
  102. }