Forráskód Böngészése

Allow hardware to be disabled

By default, all hardware sensor collection is enabled to allow for
minimal configuration in common use cases. However, some hardware may be
susceptible to instability when polled. Hiding all of the sensors from
unstable hardware isn't sufficient as sensor name filtering occurs after
querying hardware. Thus there is configuration to determine what
hardware is enabled.

The snippet below shows all the options that can be used to disable hardware.

```xml
<add key="/cpu/enabled" value="FaLsE" />
<add key="/gpu/enabled" value="false" />
<add key="/motherboard/enabled" value="false" />
<add key="/ram/enabled" value="false" />
<add key="/network/enabled" value="false" />
<add key="/storage/enabled" value="false" />
<add key="/controller/enabled" value="false" />
```

Since disabling sensors at the hardware level is more efficient than a
glob to hide desired sensors, disabling hardware is desirable even if
the underlying hardware is stable.

When hardware is disabled, all instances of that hardware are disabled.
For instance, if one has multiple storage devices and only one is
unstable, disabling storage hardware will halt sensor collection from
all of them.

Implementation note:

LibreHardwareMonitor also has this option but it is saved as

```xml
<add key="mainboardMenuItem" value="false" />
```

While I enjoy users being able to copy over some of their LHM configs,
the name doesn't make sense in this case (as we aren't dealing with menu
items here).

Instead I followed the naming precedent set forth by hiding and renaming
sensors by reusing the slash notation. It's not a perfect fit as those
are specific to hardware implementations, such as "/amdcpu/" whereas
disabling hardware looks like "/cpu/enabled". I find this discrepancy to
be tolerable.
Nick Babcock 3 éve
szülő
commit
f6752f71d3

+ 10 - 0
OhmGraphite.Test/ConfigTest.cs

@@ -37,6 +37,14 @@ namespace OhmGraphite.Test
             Assert.Equal(2003, results.Graphite.Port);
             Assert.Equal(TimeSpan.FromSeconds(5), results.Interval);
             Assert.False(results.Graphite.Tags);
+
+            Assert.True(results.EnabledHardware.Cpu);
+            Assert.True(results.EnabledHardware.Gpu);
+            Assert.True(results.EnabledHardware.Motherboard);
+            Assert.True(results.EnabledHardware.Controller);
+            Assert.True(results.EnabledHardware.Network);
+            Assert.True(results.EnabledHardware.Ram);
+            Assert.True(results.EnabledHardware.Storage);
         }
 
         [Fact]
@@ -130,6 +138,8 @@ namespace OhmGraphite.Test
             Assert.True(results.IsHidden("/amdcpu/0/power/1"));
             Assert.True(results.IsHidden("/nvidia-gpu/0/power/1"));
             Assert.False(results.IsHidden("/nvme/0/factor/power_cycles"));
+
+            Assert.False(results.EnabledHardware.Cpu);
         }
 
         [Fact]

+ 21 - 2
OhmGraphite/MetricConfig.cs

@@ -8,12 +8,15 @@ using System.Text.RegularExpressions;
 
 namespace OhmGraphite
 {
+    public record EnabledHardware(bool Cpu, bool Gpu, bool Motherboard, bool Ram, bool Network, bool Storage,
+        bool Controller);
+
     public class MetricConfig
     {
         private readonly INameResolution _nameLookup;
 
         public MetricConfig(TimeSpan interval, INameResolution nameLookup, GraphiteConfig graphite, InfluxConfig influx,
-            PrometheusConfig prometheus, TimescaleConfig timescale, Dictionary<string, string> aliases, List<Regex> hiddenSensors, Influx2Config influx2)
+            PrometheusConfig prometheus, TimescaleConfig timescale, Dictionary<string, string> aliases, List<Regex> hiddenSensors, Influx2Config influx2, EnabledHardware enabledHardware)
         {
             _nameLookup = nameLookup;
             Interval = interval;
@@ -24,6 +27,7 @@ namespace OhmGraphite
             Aliases = aliases;
             HiddenSensors = hiddenSensors;
             Influx2 = influx2;
+            EnabledHardware = enabledHardware;
         }
 
         public string LookupName() => _nameLookup.LookupName();
@@ -31,6 +35,7 @@ namespace OhmGraphite
         public GraphiteConfig Graphite { get; }
         public InfluxConfig Influx { get; }
         public Influx2Config Influx2 { get; }
+        public EnabledHardware EnabledHardware { get; }
         public PrometheusConfig Prometheus { get; }
         public TimescaleConfig Timescale { get; }
         public Dictionary<string, string> Aliases { get; }
@@ -47,6 +52,7 @@ namespace OhmGraphite
 
             INameResolution nameLookup = NameLookup(config["name_lookup"] ?? "netbios");
             InstallCertificateVerification(config["certificate_verification"] ?? "True");
+            var enabledHardware = ParseEnabledHardware(config);
 
             var type = config["type"] ?? "graphite";
             GraphiteConfig gconfig = null;
@@ -96,7 +102,20 @@ namespace OhmGraphite
                     RegexOptions.IgnoreCase | RegexOptions.Singleline
                 )).ToList();
 
-            return new MetricConfig(interval, nameLookup, gconfig, iconfig, pconfig, timescale, aliases, hiddenSensors, influx2);
+            return new MetricConfig(interval, nameLookup, gconfig, iconfig, pconfig, timescale, aliases, hiddenSensors, influx2, enabledHardware);
+        }
+
+        private static EnabledHardware ParseEnabledHardware(IAppConfig config)
+        {
+            return new (
+                config["/cpu/enabled"]?.ToLowerInvariant() != "false",
+                config["/gpu/enabled"]?.ToLowerInvariant() != "false",
+                config["/motherboard/enabled"]?.ToLowerInvariant() != "false",
+                config["/ram/enabled"]?.ToLowerInvariant() != "false",
+                config["/network/enabled"]?.ToLowerInvariant() != "false",
+                config["/storage/enabled"]?.ToLowerInvariant() != "false",
+                config["/controller/enabled"]?.ToLowerInvariant() != "false"
+            );
         }
 
         private static INameResolution NameLookup(string lookup)

+ 12 - 13
OhmGraphite/Program.cs

@@ -20,23 +20,22 @@ namespace OhmGraphite
             {
                 x.Service<IManage>(s =>
                 {
-                    // We'll want to capture all available hardware metrics
-                    // to send to graphite
-                    var computer = new Computer
-                    {
-                        IsGpuEnabled = true,
-                        IsMotherboardEnabled = true,
-                        IsCpuEnabled = true,
-                        IsMemoryEnabled = true,
-                        IsNetworkEnabled = true,
-                        IsStorageEnabled = true,
-                        IsControllerEnabled = true
-                    };
-
                     s.ConstructUsing(name =>
                     {
                         var configDisplay = string.IsNullOrEmpty(configPath) ? "default" : configPath;
                         var config = Logger.LogFunction($"parse config {configDisplay}", () => MetricConfig.ParseAppSettings(CreateConfiguration(configPath)));
+
+                        var computer = new Computer
+                        {
+                            IsGpuEnabled = config.EnabledHardware.Gpu,
+                            IsMotherboardEnabled = config.EnabledHardware.Motherboard,
+                            IsCpuEnabled = config.EnabledHardware.Cpu,
+                            IsMemoryEnabled = config.EnabledHardware.Ram,
+                            IsNetworkEnabled = config.EnabledHardware.Network,
+                            IsStorageEnabled = config.EnabledHardware.Storage,
+                            IsControllerEnabled = config.EnabledHardware.Controller
+                        };
+
                         var collector = new SensorCollector(computer, config);
                         return CreateManager(config, collector);
                     });

+ 20 - 0
README.md

@@ -272,6 +272,26 @@ There are several ways to determine the sensor id of a metric:
 Sensor added: /lpc/nct6792d/fan/1 "Fan #2"
 ```
 
+### Disabling Hardware
+
+By default, all hardware sensor collection is enabled to allow for minimal configuration in common use cases. However, some hardware may be susceptible to instability when polled. Hiding all of the sensors from unstable hardware isn't sufficient as sensor name filtering occurs after querying hardware. Thus there is configuration to determine what hardware is enabled.
+
+The snippet below shows all the options that can be used to disable hardware.
+
+```xml
+<add key="/cpu/enabled" value="FaLsE" />
+<add key="/gpu/enabled" value="false" />
+<add key="/motherboard/enabled" value="false" />
+<add key="/ram/enabled" value="false" />
+<add key="/network/enabled" value="false" />
+<add key="/storage/enabled" value="false" />
+<add key="/controller/enabled" value="false" />
+```
+
+Since disabling sensors at the hardware level is more efficient than a glob to hide desired sensors, disabling hardware is desirable even if the underlying hardware is stable.
+
+When hardware is disabled, all instances of that hardware are disabled. For instance, if one has multiple storage devices and only one is unstable, disabling storage hardware will halt sensor collection from all of them.
+
 ### Certificates
 
 When connecting to a service that presents a self signed certificate, one can specify `certificate_verification`

+ 2 - 0
assets/hidden-sensors.config

@@ -10,5 +10,7 @@
     <add key="/amdcpu/0/load/2/hidden" />
     <add key="/amdcpu/*/clock/*/hidden" />
     <add key="/*/power/*/hidden" />
+
+    <add key="/cpu/enabled" value="FaLsE" />
   </appSettings>
 </configuration>