MetricConfig.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Security;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Text.RegularExpressions;
  8. namespace OhmGraphite
  9. {
  10. public class MetricConfig
  11. {
  12. private readonly INameResolution _nameLookup;
  13. public MetricConfig(TimeSpan interval, INameResolution nameLookup, GraphiteConfig graphite, InfluxConfig influx,
  14. PrometheusConfig prometheus, TimescaleConfig timescale, Dictionary<string, string> aliases, List<Regex> hiddenSensors, Influx2Config influx2)
  15. {
  16. _nameLookup = nameLookup;
  17. Interval = interval;
  18. Graphite = graphite;
  19. Influx = influx;
  20. Prometheus = prometheus;
  21. Timescale = timescale;
  22. Aliases = aliases;
  23. HiddenSensors = hiddenSensors;
  24. Influx2 = influx2;
  25. }
  26. public string LookupName() => _nameLookup.LookupName();
  27. public TimeSpan Interval { get; }
  28. public GraphiteConfig Graphite { get; }
  29. public InfluxConfig Influx { get; }
  30. public Influx2Config Influx2 { get; }
  31. public PrometheusConfig Prometheus { get; }
  32. public TimescaleConfig Timescale { get; }
  33. public Dictionary<string, string> Aliases { get; }
  34. public List<Regex> HiddenSensors { get; }
  35. public static MetricConfig ParseAppSettings(IAppConfig config)
  36. {
  37. if (!int.TryParse(config["interval"], out int seconds))
  38. {
  39. seconds = 5;
  40. }
  41. var interval = TimeSpan.FromSeconds(seconds);
  42. INameResolution nameLookup = NameLookup(config["name_lookup"] ?? "netbios");
  43. InstallCertificateVerification(config["certificate_verification"] ?? "True");
  44. var type = config["type"] ?? "graphite";
  45. GraphiteConfig gconfig = null;
  46. InfluxConfig iconfig = null;
  47. PrometheusConfig pconfig = null;
  48. TimescaleConfig timescale = null;
  49. Influx2Config influx2 = null;
  50. switch (type.ToLowerInvariant())
  51. {
  52. case "graphite":
  53. gconfig = GraphiteConfig.ParseAppSettings(config);
  54. break;
  55. case "influxdb":
  56. case "influx":
  57. iconfig = InfluxConfig.ParseAppSettings(config);
  58. break;
  59. case "influx2":
  60. influx2 = Influx2Config.ParseAppSettings(config);
  61. break;
  62. case "prometheus":
  63. pconfig = PrometheusConfig.ParseAppSettings(config);
  64. break;
  65. case "timescale":
  66. case "timescaledb":
  67. timescale = TimescaleConfig.ParseAppSettings(config);
  68. break;
  69. }
  70. // Trim off the LibreHardwareMonitor "/name" suffix so that it is just the
  71. // the sensor ID.
  72. var aliases = config.GetKeys().Where(x => x.EndsWith("/name"))
  73. .ToDictionary(
  74. x => x.Remove(x.LastIndexOf("/name", StringComparison.Ordinal)),
  75. x => config[x]
  76. );
  77. // Do a similar trim with "/hidden" suffix except treat the prefix as a glob to hide
  78. // a group of sensors like if one wants to hide all power sensors they can do:
  79. // /*/power/*/hidden
  80. // Code to detect interpret strings as globs is shamelessly taken from stackoverflow:
  81. // https://stackoverflow.com/a/4146349/433785
  82. var hiddenSensors = config.GetKeys().Where(x => x.EndsWith("/hidden"))
  83. .Select(x => x.Remove(x.LastIndexOf("/hidden", StringComparison.Ordinal)))
  84. .Select(pattern => new Regex(
  85. "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
  86. RegexOptions.IgnoreCase | RegexOptions.Singleline
  87. )).ToList();
  88. return new MetricConfig(interval, nameLookup, gconfig, iconfig, pconfig, timescale, aliases, hiddenSensors, influx2);
  89. }
  90. private static INameResolution NameLookup(string lookup)
  91. {
  92. switch (lookup.ToLowerInvariant())
  93. {
  94. case "dns":
  95. return new DnsResolution();
  96. case "netbios":
  97. return new NetBiosResolution();
  98. default:
  99. return new StaticResolution(lookup);
  100. }
  101. }
  102. public static void InstallCertificateVerification(string type)
  103. {
  104. switch (type.ToLowerInvariant())
  105. {
  106. // Do not change default .net behavior when given True
  107. case "true":
  108. break;
  109. // Do not verify certificate
  110. case "false":
  111. ServicePointManager.ServerCertificateValidationCallback =
  112. (sender, certificate, chain, errors) => true;
  113. break;
  114. // Else assume that it points to a file path of a self signed
  115. // certificate that we will check against
  116. default:
  117. var cert = new X509Certificate2(type);
  118. ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) =>
  119. errors == SslPolicyErrors.None ||
  120. string.Equals(cert.Thumbprint, certificate.GetCertHashString(), StringComparison.InvariantCultureIgnoreCase);
  121. break;
  122. }
  123. }
  124. public bool TryGetAlias(string v, out string alias) => Aliases.TryGetValue(v, out alias);
  125. public bool IsHidden(string id) => HiddenSensors.Any(x => x.IsMatch(id));
  126. }
  127. }