Example #1
0
        public void CanParseStaticResolutionName()
        {
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/static-name.config"
            };
            var config       = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            Assert.Equal("my-cool-machine", results.LookupName());
        }
Example #2
0
        public async void CanInsertIntoInflux2Token()
        {
            var testContainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                        .WithDockerEndpoint(DockerUtils.DockerEndpoint())
                                        .WithImage("influxdb:2.0-alpine")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", "thisistheinfluxdbtoken")
                                        .WithPortBinding(8086, assignRandomHostPort: true)
                                        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));

            await using var container = testContainersBuilder.Build();
            await container.StartAsync();

            var baseUrl   = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/influx2.config"
            };
            var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

            config.AppSettings.Settings["influx2_address"].Value = baseUrl;
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            using var writer = new Influx2Writer(results.Influx2, "my-pc");
            for (int attempts = 0;; attempts++)
            {
                try
                {
                    await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                    var influxDBClient = InfluxDBClientFactory.Create(results.Influx2.Options);
                    var flux           = "from(bucket:\"mydb\") |> range(start: -1h)";
                    var queryApi       = influxDBClient.GetQueryApi();
                    var tables         = await queryApi.QueryAsync(flux, "myorg");

                    var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
                    Assert.Contains("/intelcpu/0/temperature/0", fields);
                    break;
                }
                catch (Exception)
                {
                    if (attempts >= 10)
                    {
                        throw;
                    }

                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
Example #3
0
        public void CanParseHiddenSensors()
        {
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/hidden-sensors.config"
            };
            var config       = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            Assert.True(results.IsHidden("/amdcpu/0/load/1"));
            Assert.True(results.IsHidden("/amdcpu/0/load/2"));
            Assert.False(results.IsHidden("/amdcpu/0/load/3"));
        }
Example #4
0
        public void CanParsePrometheusConfig()
        {
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/prometheus.config"
            };
            var config       = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            Assert.Null(results.Graphite);
            Assert.Null(results.Influx);
            Assert.NotNull(results.Prometheus);
            Assert.Equal(4446, results.Prometheus.Port);
            Assert.Equal("127.0.0.1", results.Prometheus.Host);
        }
Example #5
0
        public void CanParseSensorAlias()
        {
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/rename.config"
            };
            var config       = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            Assert.True(results.TryGetAlias("/amdcpu/0/load/1", out string alias));
            Assert.Equal("CPU Core 0 T0", alias);
            Assert.True(results.TryGetAlias("/amdcpu/0/load/2", out alias));
            Assert.Equal("CPU Core 0 T1", alias);
            Assert.False(results.TryGetAlias("/amdcpu/0/load/3", out alias));
        }
Example #6
0
        public void CanParseGraphiteConfig()
        {
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/graphite.config"
            };
            var config       = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            Assert.NotNull(results.Graphite);
            Assert.Null(results.Influx);
            Assert.Equal("myhost", results.Graphite.Host);
            Assert.Equal(2004, results.Graphite.Port);
            Assert.Equal(TimeSpan.FromSeconds(6), results.Interval);
            Assert.True(results.Graphite.Tags);
        }
Example #7
0
        public void CanParseTimescaleConfig()
        {
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/timescale.config"
            };
            var config       = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            Assert.Null(results.Graphite);
            Assert.Null(results.Influx);
            Assert.Null(results.Prometheus);
            Assert.NotNull(results.Timescale);
            Assert.Equal("Host=vm-ubuntu;Username=ohm;Password=123456", results.Timescale.Connection);
            Assert.False(results.Timescale.SetupTable);
        }
Example #8
0
        public void CanParseInfluxDbConfig()
        {
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/influx.config"
            };
            var config       = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            Assert.Null(results.Graphite);
            Assert.NotNull(results.Influx);
            Assert.Equal(TimeSpan.FromSeconds(6), results.Interval);
            Assert.Equal("http://192.168.1.15:8086/", results.Influx.Address.ToString());
            Assert.Equal("mydb", results.Influx.Db);
            Assert.Equal("my_user", results.Influx.User);
            Assert.Equal("my_pass", results.Influx.Password);
        }
Example #9
0
        public void SensorsAddedWhenHardwareAdded()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return;
            }

            var computer = new Computer();

            using var collector = new SensorCollector(computer, MetricConfig.ParseAppSettings(new BlankConfig()));

            collector.Open();
            var unused = collector.ReadAllSensors().Count();

            computer.IsCpuEnabled         = true;
            computer.IsMotherboardEnabled = true;
            computer.IsStorageEnabled     = true;
            computer.IsMemoryEnabled      = true;

            var addedCount = collector.ReadAllSensors().Count();

            // On CI platforms there may be no detected hardware
            if (addedCount <= 0)
            {
                return;
            }

            computer.IsCpuEnabled         = false;
            computer.IsMotherboardEnabled = false;
            computer.IsStorageEnabled     = false;
            computer.IsMemoryEnabled      = false;

            var removedCount = collector.ReadAllSensors().Count();

            Assert.True(addedCount > removedCount, "addedCount > removedCount");
        }
Example #10
0
        public async void CanInsertIntoInflux2TokenTls()
        {
            // We do some fancy docker footwork where we informally connect
            // these two containers. In the future I believe test containers will
            // be able to natively handle adding these to a docker network
            var testContainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                        .WithDockerEndpoint(DockerUtils.DockerEndpoint())
                                        .WithImage("influxdb:2.0-alpine")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
                                        .WithEnvironment("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", "thisistheinfluxdbtoken")
                                        .WithPortBinding(8086, assignRandomHostPort: true)
                                        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));

            await using var container = testContainersBuilder.Build();
            await container.StartAsync();

            var cmd = $"apk add openssl && openssl req -x509 -nodes -newkey rsa:4096 -keyout /tmp/key.pem -out /tmp/cert.pem -days 365 -subj '/C=US/ST=Oregon/L=Portland/O=Company Name/OU=Org/CN=www.example.com' && /usr/bin/ghostunnel server --listen=0.0.0.0:8087 --target={container.IpAddress}:8086 --unsafe-target --disable-authentication --key /tmp/key.pem --cert=/tmp/cert.pem";
            var tlsContainerBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                      .WithDockerEndpoint(DockerUtils.DockerEndpoint())
                                      .WithImage("squareup/ghostunnel")
                                      .WithExposedPort(8087)
                                      .WithPortBinding(8087, assignRandomHostPort: true)
                                      .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8087))
                                      .WithEntrypoint("/bin/sh")
                                      .WithCommand("-c", cmd);

            await using var tlsContainer = tlsContainerBuilder.Build();
            await tlsContainer.StartAsync();

            var baseUrl   = $"https://{tlsContainer.Hostname}:{tlsContainer.GetMappedPublicPort(8087)}";
            var configMap = new ExeConfigurationFileMap {
                ExeConfigFilename = "assets/influx2.config"
            };
            var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

            config.AppSettings.Settings["influx2_address"].Value = baseUrl;
            var customConfig = new CustomConfig(config);
            var results      = MetricConfig.ParseAppSettings(customConfig);

            MetricConfig.InstallCertificateVerification("false");
            try
            {
                using var writer = new Influx2Writer(results.Influx2, "my-pc");
                for (int attempts = 0;; attempts++)
                {
                    try
                    {
                        await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                        var influxDbClient = InfluxDBClientFactory.Create(results.Influx2.Options);
                        var flux           = "from(bucket:\"mydb\") |> range(start: -1h)";
                        var queryApi       = influxDbClient.GetQueryApi();
                        var tables         = await queryApi.QueryAsync(flux, "myorg");

                        var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
                        Assert.Contains("/intelcpu/0/temperature/0", fields);
                        break;
                    }
                    catch (Exception)
                    {
                        if (attempts >= 10)
                        {
                            throw;
                        }

                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }
                }
            }
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = null;
            }
        }