Program.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using NLog;
  3. using OpenHardwareMonitor.Hardware;
  4. using Topshelf;
  5. namespace OhmGraphite
  6. {
  7. internal class Program
  8. {
  9. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  10. private static void Main(string[] args)
  11. {
  12. HostFactory.Run(x =>
  13. {
  14. x.Service<MetricTimer>(s =>
  15. {
  16. // We need to know where the graphite server lives and how often
  17. // to poll the hardware
  18. var config = Logger.LogFunction("parse config", MetricConfig.ParseAppSettings);
  19. double seconds = config.Interval.TotalSeconds;
  20. IWriteMetrics writer = null;
  21. if (config.Graphite != null)
  22. {
  23. Logger.Info($"Graphite host: {config.Graphite.Host} port: {config.Graphite.Port} interval: {seconds} tags: {config.Graphite.Tags}");
  24. writer = new GraphiteWriter(config.Graphite.Host,
  25. config.Graphite.Port,
  26. Environment.MachineName,
  27. config.Graphite.Tags);
  28. }
  29. else
  30. {
  31. Logger.Info($"Influxdb address: {config.Influx.Address} db: {config.Influx.Db}");
  32. writer = new InfluxWriter(config.Influx, Environment.MachineName);
  33. }
  34. // We'll want to capture all available hardware metrics
  35. // to send to graphite
  36. var computer = new Computer
  37. {
  38. GPUEnabled = true,
  39. MainboardEnabled = true,
  40. CPUEnabled = true,
  41. RAMEnabled = true,
  42. FanControllerEnabled = true,
  43. HDDEnabled = true
  44. };
  45. var collector = new SensorCollector(computer);
  46. s.ConstructUsing(name =>
  47. Logger.LogFunction("creating timer",
  48. () => new MetricTimer(config.Interval, collector, writer)));
  49. s.WhenStarted(tc => tc.Start());
  50. s.WhenStopped(tc => tc.Stop());
  51. });
  52. x.UseNLog();
  53. x.RunAsLocalSystem();
  54. x.SetDescription(
  55. "Extract hardware sensor data and exports it to a given host and port in a graphite compatible format");
  56. x.SetDisplayName("Ohm Graphite");
  57. x.SetServiceName("OhmGraphite");
  58. x.OnException(ex => Logger.Error(ex, "OhmGraphite TopShelf encountered an error"));
  59. });
  60. }
  61. }
  62. }