Program.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Configuration;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using NLog;
  6. using LibreHardwareMonitor.Hardware;
  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. // https://github.com/dotnet/runtime/issues/13051#issuecomment-510267727
  59. var processModule = Process.GetCurrentProcess().MainModule;
  60. if (processModule != null)
  61. {
  62. var pt = processModule.FileName;
  63. var fn = Path.Join(Path.GetDirectoryName(pt), "OhmGraphite.exe.config");
  64. var configMap1 = new ExeConfigurationFileMap { ExeConfigFilename = fn };
  65. var config1 = ConfigurationManager.OpenMappedExeConfiguration(configMap1, ConfigurationUserLevel.None);
  66. return new CustomConfig(config1);
  67. }
  68. }
  69. if (!File.Exists(configPath))
  70. {
  71. throw new ApplicationException($"unable to detect config: ${configPath}");
  72. }
  73. var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
  74. var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  75. return new CustomConfig(config);
  76. }
  77. private static IManage CreateManager(MetricConfig config, SensorCollector collector)
  78. {
  79. var hostname = config.LookupName();
  80. double seconds = config.Interval.TotalSeconds;
  81. if (config.Graphite != null)
  82. {
  83. Logger.Info(
  84. $"Graphite host: {config.Graphite.Host} port: {config.Graphite.Port} interval: {seconds} tags: {config.Graphite.Tags}");
  85. var writer = new GraphiteWriter(config.Graphite.Host,
  86. config.Graphite.Port,
  87. hostname,
  88. config.Graphite.Tags);
  89. return new MetricTimer(config.Interval, collector, writer);
  90. }
  91. else if (config.Prometheus != null)
  92. {
  93. Logger.Info($"Prometheus port: {config.Prometheus.Port}");
  94. var registry = PrometheusCollection.SetupDefault(collector);
  95. var server = new MetricServer(config.Prometheus.Host, config.Prometheus.Port, registry: registry);
  96. return new PrometheusServer(server, collector);
  97. }
  98. else if (config.Timescale != null)
  99. {
  100. var writer = new TimescaleWriter(config.Timescale.Connection, config.Timescale.SetupTable, hostname);
  101. return new MetricTimer(config.Interval, collector, writer);
  102. }
  103. else
  104. {
  105. Logger.Info($"Influxdb address: {config.Influx.Address} db: {config.Influx.Db}");
  106. var writer = new InfluxWriter(config.Influx, hostname);
  107. return new MetricTimer(config.Interval, collector, writer);
  108. }
  109. }
  110. }
  111. }