Program.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using OpenHardwareMonitor.Hardware;
  3. using System.Configuration;
  4. using NLog;
  5. using Topshelf;
  6. namespace OhmGraphite
  7. {
  8. class Program
  9. {
  10. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  11. static void Main(string[] args)
  12. {
  13. HostFactory.Run(x =>
  14. {
  15. x.Service<MetricTimer>(s =>
  16. {
  17. // We need to know where the graphite server lives and how often
  18. // to poll the hardware
  19. ParseConfig(out string host, out int port, out int seconds);
  20. // We'll want to capture all available hardware metrics
  21. // to send to graphite
  22. var computer = new Computer
  23. {
  24. GPUEnabled = true,
  25. MainboardEnabled = true,
  26. CPUEnabled = true,
  27. RAMEnabled = true,
  28. FanControllerEnabled = true,
  29. HDDEnabled = true
  30. };
  31. s.ConstructUsing(name => new MetricTimer(computer, TimeSpan.FromSeconds(seconds), host, port));
  32. s.WhenStarted(tc => tc.Start());
  33. s.WhenStopped(tc => tc.Stop());
  34. });
  35. x.RunAsLocalSystem();
  36. x.SetDescription("Extract hardware sensor data and exports it to a given host and port in a graphite compatible format");
  37. x.SetDisplayName("Ohm Graphite");
  38. x.SetServiceName("OhmGraphite");
  39. x.OnException(ex =>
  40. {
  41. Logger.Error(ex, "OhmGraphite TopShelf encountered an error");
  42. });
  43. });
  44. }
  45. private static void ParseConfig(out string host, out int port, out int seconds)
  46. {
  47. host = ConfigurationManager.AppSettings["host"] ?? "localhost";
  48. if (!int.TryParse(ConfigurationManager.AppSettings["port"], out port))
  49. {
  50. port = 2003;
  51. }
  52. if (!int.TryParse(ConfigurationManager.AppSettings["interval"], out seconds))
  53. {
  54. seconds = 5;
  55. }
  56. Logger.Info($"Host: {host} port: {port} interval: {seconds}");
  57. }
  58. }
  59. }