소스 검색

Extract out SensorCollector

Nick Babcock 7 년 전
부모
커밋
36e356c1c1
4개의 변경된 파일97개의 추가작업 그리고 66개의 파일을 삭제
  1. 2 17
      OhmGraphite/MetricConfig.cs
  2. 11 48
      OhmGraphite/MetricTimer.cs
  3. 17 1
      OhmGraphite/Program.cs
  4. 67 0
      OhmGraphite/SensorCollector.cs

+ 2 - 17
OhmGraphite/MetricConfig.cs

@@ -8,14 +8,12 @@ namespace OhmGraphite
     {
         public string Host { get; }
         public int Port { get; }
-        public Computer Computer { get; }
         public TimeSpan Interval { get; }
 
-        public MetricConfig(string host, int port, Computer computer, TimeSpan interval)
+        public MetricConfig(string host, int port, TimeSpan interval)
         {
             Host = host;
             Port = port;
-            Computer = computer;
             Interval = interval;
         }
 
@@ -33,20 +31,7 @@ namespace OhmGraphite
             }
 
             var interval = TimeSpan.FromSeconds(seconds);
-
-            // We'll want to capture all available hardware metrics
-            // to send to graphite
-            var computer = new Computer
-            {
-                GPUEnabled = true,
-                MainboardEnabled = true,
-                CPUEnabled = true,
-                RAMEnabled = true,
-                FanControllerEnabled = true,
-                HDDEnabled = true
-            };
-
-            return new MetricConfig(host, port, computer, interval);
+            return new MetricConfig(host, port, interval);
         }
     }
 }

+ 11 - 48
OhmGraphite/MetricTimer.cs

@@ -14,25 +14,25 @@ namespace OhmGraphite
     {
         private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
 
-        private readonly Computer _computer;
         private readonly int _graphitePort;
         private readonly string _graphiteHost;
         private readonly Timer _timer;
+        private readonly SensorCollector _collector;
 
-        public MetricTimer(MetricConfig config)
+        public MetricTimer(MetricConfig config, SensorCollector collector)
         {
-            _computer = config.Computer;
             _graphitePort = config.Port;
             _graphiteHost = config.Host;
             _timer = new Timer(config.Interval.TotalMilliseconds) { AutoReset = true };
             _timer.Elapsed += ReportMetrics;
+            _collector = collector;
         }
 
         public void Start()
         {
             Logger.LogAction("starting metric timer", () =>
             {
-                _computer.Open();
+                _collector.Open();
                 _timer.Start();
             });
         }
@@ -41,7 +41,7 @@ namespace OhmGraphite
         {
             Logger.LogAction("stopping metric timer", () =>
             {
-                _computer.Close();
+                _collector.Close();
                 _timer.Stop();
             });
         }
@@ -78,18 +78,15 @@ namespace OhmGraphite
             using (var networkStream = client.GetStream())
             using (var writer = new StreamWriter(networkStream))
             {
-                foreach (var hardware in ReadHardware(_computer))
+                foreach (var sensor in _collector.ReadAllSensors())
                 {
-                    foreach (var sensor in ReadSensors(hardware))
-                    {
-                        var data = Normalize(sensor);
+                    var data = Normalize(sensor);
 
-                        // Graphite API wants <metric> <value> <timestamp>. We prefix the metric
-                        // with `ohm` as to not overwrite potentially existing metrics
-                        writer.WriteLine(FormatGraphiteData(host, epoch, data));
+                    // Graphite API wants <metric> <value> <timestamp>. We prefix the metric
+                    // with `ohm` as to not overwrite potentially existing metrics
+                    writer.WriteLine(FormatGraphiteData(host, epoch, data));
 
-                        sensorCount++;
-                    }
+                    sensorCount++;
                 }
             }
 
@@ -114,39 +111,5 @@ namespace OhmGraphite
             var name = sensor.Name.ToLower().Replace(" ", null).Replace('#', '.');
             return new Sensor(identifier, name, sensor.Value);
         }
-
-        private static IEnumerable<IHardware> ReadHardware(IComputer computer)
-        {
-            foreach (var hardware in computer.Hardware)
-            {
-                yield return hardware;
-
-                foreach (var subHardware in hardware.SubHardware)
-                {
-                    yield return subHardware;
-                }
-            }
-        }
-
-        private static IEnumerable<Sensor> ReadSensors(IHardware hardware)
-        {
-            hardware.Update();
-            foreach (var sensor in hardware.Sensors)
-            {
-                var id = sensor.Identifier.ToString();
-
-                // Only report a value if the sensor was able to get a value
-                // as 0 is different than "didn't read". For example, are the
-                // fans really spinning at 0 RPM or was the value not read.
-                if (sensor.Value.HasValue)
-                {
-                    yield return new Sensor(id, sensor.Name, sensor.Value.Value);
-                }
-                else
-                {
-                    Logger.Debug($"{id} did not have a value");
-                }
-            }
-        }
     }
 }

+ 17 - 1
OhmGraphite/Program.cs

@@ -1,4 +1,5 @@
 using NLog;
+using OpenHardwareMonitor.Hardware;
 using Topshelf;
 
 namespace OhmGraphite
@@ -17,7 +18,22 @@ namespace OhmGraphite
                     var config = Logger.LogFunction("parse config", MetricConfig.ParseAppSettings);
                     var seconds = config.Interval.TotalSeconds;
                     Logger.Info($"Host: {config.Host} port: {config.Port} interval: {seconds}");
-                    s.ConstructUsing(name => Logger.LogFunction("creating timer", () => new MetricTimer(config)));
+
+                    // We'll want to capture all available hardware metrics
+                    // to send to graphite
+                    var computer = new Computer
+                    {
+                        GPUEnabled = true,
+                        MainboardEnabled = true,
+                        CPUEnabled = true,
+                        RAMEnabled = true,
+                        FanControllerEnabled = true,
+                        HDDEnabled = true
+                    };
+
+                    var collector = new SensorCollector(computer);
+
+                    s.ConstructUsing(name => Logger.LogFunction("creating timer", () => new MetricTimer(config, collector)));
                     s.WhenStarted(tc => tc.Start());
                     s.WhenStopped(tc => tc.Stop());
                 });

+ 67 - 0
OhmGraphite/SensorCollector.cs

@@ -0,0 +1,67 @@
+using System.Collections.Generic;
+using System.Linq;
+using NLog;
+using OpenHardwareMonitor.Hardware;
+
+namespace OhmGraphite
+{
+    public class SensorCollector
+    {
+        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
+        private readonly Computer _computer;
+
+        public SensorCollector(Computer computer)
+        {
+            _computer = computer;
+        }
+
+        public void Open()
+        {
+            _computer.Open();
+        }
+
+        public void Close()
+        {
+            _computer.Close();
+        }
+
+        public IEnumerable<Sensor> ReadAllSensors()
+        {
+            return ReadHardware().SelectMany(ReadSensors);
+        }
+
+        private IEnumerable<IHardware> ReadHardware()
+        {
+            foreach (var hardware in _computer.Hardware)
+            {
+                yield return hardware;
+
+                foreach (var subHardware in hardware.SubHardware)
+                {
+                    yield return subHardware;
+                }
+            }
+        }
+
+        private static IEnumerable<Sensor> ReadSensors(IHardware hardware)
+        {
+            hardware.Update();
+            foreach (var sensor in hardware.Sensors)
+            {
+                var id = sensor.Identifier.ToString();
+
+                // Only report a value if the sensor was able to get a value
+                // as 0 is different than "didn't read". For example, are the
+                // fans really spinning at 0 RPM or was the value not read.
+                if (sensor.Value.HasValue)
+                {
+                    yield return new Sensor(id, sensor.Name, sensor.Value.Value);
+                }
+                else
+                {
+                    Logger.Debug($"{id} did not have a value");
+                }
+            }
+        }
+    }
+}