1
0

Program.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Configuration;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using NLog;
  7. using LibreHardwareMonitor.Hardware;
  8. using Prometheus;
  9. using Topshelf;
  10. namespace OhmGraphite
  11. {
  12. internal class Program
  13. {
  14. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  15. private static void Main()
  16. {
  17. string configPath = string.Empty;
  18. bool showVersion = false;
  19. HostFactory.Run(x =>
  20. {
  21. x.Service<IManage>(s =>
  22. {
  23. s.ConstructUsing(name =>
  24. {
  25. if (showVersion)
  26. {
  27. var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
  28. Console.WriteLine(version ?? "no version detected");
  29. Environment.Exit(0);
  30. }
  31. var configDisplay = string.IsNullOrEmpty(configPath) ? "default" : configPath;
  32. var config = Logger.LogFunction($"parse config {configDisplay}", () => MetricConfig.ParseAppSettings(CreateConfiguration(configPath)));
  33. var computer = new Computer
  34. {
  35. IsGpuEnabled = config.EnabledHardware.Gpu,
  36. IsMotherboardEnabled = config.EnabledHardware.Motherboard,
  37. IsCpuEnabled = config.EnabledHardware.Cpu,
  38. IsMemoryEnabled = config.EnabledHardware.Ram,
  39. IsNetworkEnabled = config.EnabledHardware.Network,
  40. IsStorageEnabled = config.EnabledHardware.Storage,
  41. IsControllerEnabled = config.EnabledHardware.Controller,
  42. IsPsuEnabled = config.EnabledHardware.Psu,
  43. IsBatteryEnabled = config.EnabledHardware.Battery,
  44. };
  45. var collector = new SensorCollector(computer, config);
  46. return CreateManager(config, collector);
  47. });
  48. s.WhenStarted(tc => tc.Start());
  49. s.WhenStopped(tc => tc.Dispose());
  50. });
  51. // Allow one to specify a command line argument when running interactively
  52. x.AddCommandLineDefinition("config", v => configPath = v);
  53. x.AddCommandLineSwitch("version", v => showVersion = v);
  54. x.UseNLog();
  55. x.RunAsLocalSystem();
  56. x.SetDescription(
  57. "Extract hardware sensor data and exports it to a given host and port in a graphite compatible format");
  58. x.SetDisplayName("Ohm Graphite");
  59. x.SetServiceName("OhmGraphite");
  60. x.OnException(ex => Logger.Error(ex, "OhmGraphite TopShelf encountered an error"));
  61. });
  62. }
  63. private static IAppConfig CreateConfiguration(string configPath)
  64. {
  65. if (string.IsNullOrEmpty(configPath))
  66. {
  67. // https://github.com/dotnet/runtime/issues/13051#issuecomment-510267727
  68. var processModule = Process.GetCurrentProcess().MainModule;
  69. if (processModule != null)
  70. {
  71. var pt = processModule.FileName;
  72. var fn = Path.Join(Path.GetDirectoryName(pt), "OhmGraphite.exe.config");
  73. var configMap1 = new ExeConfigurationFileMap { ExeConfigFilename = fn };
  74. var config1 = ConfigurationManager.OpenMappedExeConfiguration(configMap1, ConfigurationUserLevel.None);
  75. return new CustomConfig(config1);
  76. }
  77. }
  78. if (!File.Exists(configPath))
  79. {
  80. throw new ApplicationException($"unable to detect config: ${configPath}");
  81. }
  82. var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
  83. var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  84. return new CustomConfig(config);
  85. }
  86. private static IManage CreateManager(MetricConfig config, SensorCollector collector)
  87. {
  88. var hostname = config.LookupName();
  89. double seconds = config.Interval.TotalSeconds;
  90. if (config.Graphite != null)
  91. {
  92. Logger.Info(
  93. $"Graphite host: {config.Graphite.Host} port: {config.Graphite.Port} interval: {seconds} tags: {config.Graphite.Tags}");
  94. var writer = new GraphiteWriter(config.Graphite.Host,
  95. config.Graphite.Port,
  96. hostname,
  97. config.Graphite.Tags);
  98. return new MetricTimer(config.Interval, collector, writer);
  99. }
  100. else if (config.Prometheus != null)
  101. {
  102. Logger.Info($"Prometheus port: {config.Prometheus.Port}");
  103. var registry = PrometheusCollection.SetupDefault(collector);
  104. var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port, registry: registry, useHttps: config.Prometheus.UseHttps);
  105. return new PrometheusServer(server, collector);
  106. }
  107. else if (config.Timescale != null)
  108. {
  109. var writer = new TimescaleWriter(config.Timescale.Connection, config.Timescale.SetupTable, hostname);
  110. return new MetricTimer(config.Interval, collector, writer);
  111. }
  112. else if (config.Influx != null)
  113. {
  114. Logger.Info($"Influxdb address: {config.Influx.Address} db: {config.Influx.Db}");
  115. var writer = new InfluxWriter(config.Influx, hostname);
  116. return new MetricTimer(config.Interval, collector, writer);
  117. }
  118. else
  119. {
  120. Logger.Info($"Influx2 address: {config.Influx2.Options.Url}");
  121. var writer = new Influx2Writer(config.Influx2, hostname);
  122. return new MetricTimer(config.Interval, collector, writer);
  123. }
  124. }
  125. }
  126. }