1
0

Program.cs 5.1 KB

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