コード例 #1
0
        public async void CanInsertIntoInflux()
        {
            var config = new InfluxConfig(new Uri("http://influx:8086"), "mydb", "my_user", "my_pass");

            using (var writer = new InfluxWriter(config, "my-pc"))
                using (var client = new HttpClient())
                {
                    for (int attempts = 0; ; attempts++)
                    {
                        try
                        {
                            await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                            var resp = await client.GetAsync(
                                "http://influx:8086/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");

                            Assert.True(resp.IsSuccessStatusCode);
                            var content = await resp.Content.ReadAsStringAsync();

                            Assert.Contains("/intelcpu/0/temperature/0", content);
                            break;
                        }
                        catch (Exception ex)
                        {
                            if (attempts >= 10)
                            {
                                throw;
                            }

                            Thread.Sleep(TimeSpan.FromSeconds(1));
                        }
                    }
                }
        }
コード例 #2
0
        public async void InsertTagGraphiteTest()
        {
            // Let the tag engine time to breathe
            Thread.Sleep(TimeSpan.FromSeconds(2));

            using (var writer = new GraphiteWriter("graphite", 2003, "my-pc", tags: true))
                using (var client = new HttpClient())
                {
                    for (int attempts = 0; ; attempts++)
                    {
                        try
                        {
                            await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                            var resp = await client.GetAsync("http://graphite/render?format=csv&target=seriesByTag('sensor_type=Temperature','hardware_type=CPU')");

                            var content = await resp.Content.ReadAsStringAsync();

                            Assert.Contains("host=my-pc", content);
                            Assert.Contains("app=ohm", content);
                            Assert.Contains("sensor_type=Temperature", content);
                            break;
                        }
                        catch (Exception)
                        {
                            if (attempts >= 10)
                            {
                                throw;
                            }

                            Thread.Sleep(TimeSpan.FromSeconds(1));
                        }
                    }
                }
        }
コード例 #3
0
        public async void InsertOnlyTimescale()
        {
            var image = await new ImageFromDockerfileBuilder()
                        .WithDockerfile("timescale.dockerfile")
                        .WithDockerfileDirectory("docker")
                        .WithDeleteIfExists(true)
                        .WithName("ohm-graphite-insert-only-timescale")
                        .Build();

            var testContainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                        .WithImage(image)
                                        .WithEnvironment("POSTGRES_PASSWORD", "123456")
                                        .WithPortBinding(5432, assignRandomHostPort: true)
                                        .WithWaitStrategy(Wait.ForUnixContainer()
                                                          .UntilCommandIsCompleted("pg_isready -h 'localhost' -p '5432'"));

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

            string selectStr = $"Host={container.Hostname};Username=postgres;Password=123456;Port={container.GetMappedPublicPort(5432)};Database=timescale_built";
            var    epoch     = new DateTime(2001, 1, 13);

            string connStr = $"Host={container.Hostname};Username=ohm;Password=itsohm;Port={container.GetMappedPublicPort(5432)};Database=timescale_built";

            using var writer     = new TimescaleWriter(connStr, false, "my-pc");
            await using var conn = new NpgsqlConnection(selectStr);
            await writer.ReportMetrics(epoch, TestSensorCreator.Values());

            conn.Open();
            await using var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM ohm_stats", conn);
            Assert.Equal(3, Convert.ToInt32(cmd.ExecuteScalar()));
        }
コード例 #4
0
        public async void InsertGraphiteTest()
        {
            using (var writer = new GraphiteWriter("graphite", 2003, "my-pc", tags: false))
                using (var client = new HttpClient())
                {
                    for (int attempts = 0; ; attempts++)
                    {
                        try
                        {
                            await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                            var resp = await client.GetAsync(
                                "http://graphite/render?format=csv&target=ohm.my-pc.intelcpu.0.temperature.cpucore.1");

                            var content = await resp.Content.ReadAsStringAsync();

                            Assert.Contains("ohm.my-pc.intelcpu.0.temperature.cpucore.1", content);
                            break;
                        }
                        catch (Exception)
                        {
                            if (attempts >= 10)
                            {
                                throw;
                            }

                            Thread.Sleep(TimeSpan.FromSeconds(1));
                        }
                    }
                }
        }
コード例 #5
0
        public async void InsertTagGraphiteTest()
        {
            // Let the tag engine time to breathe
            Thread.Sleep(TimeSpan.FromSeconds(2));

            using (var writer = new GraphiteWriter("graphite", 2003, "my-pc", tags: true))
                using (var client = new HttpClient())

                {
                    await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                    // wait for carbon to sync to disk
                    Thread.Sleep(TimeSpan.FromSeconds(4));

                    {
                        var resp = await client.GetAsync("http://graphite/render?format=csv&target=seriesByTag('sensor_type=Temperature','hardware_type=CPU')");

                        var content = await resp.Content.ReadAsStringAsync();

                        Assert.Contains("host=my-pc", content);
                        Assert.Contains("app=ohm", content);
                        Assert.Contains("sensor_type=Temperature", content);
                    }
                }
        }
コード例 #6
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));
                }
            }
        }
コード例 #7
0
        public async void CanInsertIntoInflux2()
        {
            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")
                                        .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 options = new InfluxDBClientOptions.Builder()
                          .Url(baseUrl)
                          .Authenticate("my-user", "my-password".ToCharArray())
                          .Bucket("mydb")
                          .Org("myorg")
                          .Build();

            var config = new Influx2Config(options);

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

                    var influxDBClient = InfluxDBClientFactory.Create(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));
                }
            }
        }
コード例 #8
0
        public async void InsertGraphiteTest()
        {
            var writer = new GraphiteWriter("graphite", 2003, "my-pc", tags: false);
            await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

            // wait for carbon to sync to disk
            Thread.Sleep(TimeSpan.FromSeconds(4));

            var client = new HttpClient();
            var resp   = await client.GetAsync("http://graphite/render?format=csv&target=ohm.my-pc.intelcpu.0.temperature.cpucore.1");

            var content = await resp.Content.ReadAsStringAsync();

            Assert.Contains("ohm.my-pc.intelcpu.0.temperature.cpucore.1", content);
        }
コード例 #9
0
ファイル: TimescaleTest.cs プロジェクト: wuast94/OhmGraphite
        public async void CanSetupTimescale()
        {
            const string connStr = "Host=timescale;Username=postgres;Password=123456";
            var          writer  = new TimescaleWriter(connStr, true, "my-pc");
            var          epoch   = new DateTime(2001, 1, 13);
            await writer.ReportMetrics(epoch, TestSensorCreator.Values());

            using (var conn = new NpgsqlConnection(connStr))
            {
                conn.Open();
                using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM ohm_stats", conn))
                {
                    Assert.Equal(3, Convert.ToInt32(cmd.ExecuteScalar()));
                }
            }
        }
コード例 #10
0
        public async void PrometheusTestServer()
        {
            var collector = new TestSensorCreator();
            var registry  = PrometheusCollection.SetupDefault(collector);
            var mserver   = new MetricServer("localhost", 21881, registry: registry);

            using var server = new PrometheusServer(mserver, collector);
            using var client = new HttpClient();
            server.Start();
            var resp = await client.GetAsync("http://localhost:21881/metrics");

            Assert.True(resp.IsSuccessStatusCode);
            var content = await resp.Content.ReadAsStringAsync();

            Assert.Contains("# HELP ohm_cpu_celsius Metric reported by open hardware sensor", content);
        }
コード例 #11
0
        public async void InsertTagGraphiteTest()
        {
            var testContainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                        .WithDockerEndpoint(DockerUtils.DockerEndpoint())
                                        .WithImage("graphiteapp/graphite-statsd")
                                        .WithEnvironment("REDIS_TAGDB", "y")
                                        .WithPortBinding(2003, assignRandomHostPort: true)
                                        .WithPortBinding(80, assignRandomHostPort: true)
                                        .WithPortBinding(8080, assignRandomHostPort: true)
                                        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8080));

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

            var port = container.GetMappedPublicPort(2003);

            using var writer = new GraphiteWriter(container.Hostname, port, "my-pc", tags: true);
            using var client = new HttpClient();
            for (int attempts = 0;; attempts++)
            {
                try
                {
                    await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                    var resp = await client.GetAsync(
                        $"http://{container.Hostname}:{container.GetMappedPublicPort(80)}/render?format=csv&target=seriesByTag('sensor_type=Temperature','hardware_type=CPU')");

                    var content = await resp.Content.ReadAsStringAsync();

                    Assert.Contains("host=my-pc", content);
                    Assert.Contains("app=ohm", content);
                    Assert.Contains("sensor_type=Temperature", content);
                    break;
                }
                catch (Exception)
                {
                    if (attempts >= 10)
                    {
                        throw;
                    }

                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
コード例 #12
0
        public async void CanInsertIntoPasswordLessInfluxdb()
        {
            var testContainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                        .WithDockerEndpoint(DockerUtils.DockerEndpoint())
                                        .WithImage("influxdb:1.8-alpine")
                                        .WithEnvironment("INFLUXDB_DB", "mydb")
                                        .WithEnvironment("INFLUXDB_USER", "my_user")
                                        .WithEnvironment("INFLUXDB_HTTP_AUTH_ENABLED", "false")
                                        .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 config  = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", null);

            using var writer = new InfluxWriter(config, "my-pc");
            using var client = new HttpClient();
            for (int attempts = 0;; attempts++)
            {
                try
                {
                    await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());

                    var resp = await client.GetAsync(
                        $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");

                    Assert.True(resp.IsSuccessStatusCode);
                    var content = await resp.Content.ReadAsStringAsync();

                    Assert.Contains("/intelcpu/0/temperature/0", content);
                    break;
                }
                catch (Exception)
                {
                    if (attempts >= 10)
                    {
                        throw;
                    }

                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
コード例 #13
0
        public async void InsertOnlyTimescale()
        {
            const string selectStr = "Host=timescale;Username=postgres;Password=123456;Database=timescale_built";
            var          epoch     = new DateTime(2001, 1, 13);

            const string connStr = "Host=timescale;Username=ohm;Password=itsohm;Database=timescale_built";

            using (var writer = new TimescaleWriter(connStr, false, "my-pc"))
                using (var conn = new NpgsqlConnection(selectStr))
                {
                    await writer.ReportMetrics(epoch, TestSensorCreator.Values());

                    conn.Open();
                    using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM ohm_stats", conn))
                    {
                        Assert.Equal(3, Convert.ToInt32(cmd.ExecuteScalar()));
                    }
                }
        }
コード例 #14
0
ファイル: PrometheusTest.cs プロジェクト: wuast94/OhmGraphite
        public async void PrometheusTestServer()
        {
            var collector            = new TestSensorCreator();
            var prometheusCollection = new PrometheusCollection(collector, "my-pc");
            var mserver = new MetricServer("localhost", 21881);
            var server  = new PrometheusServer(mserver, collector, prometheusCollection);

            try
            {
                server.Start();
                var client = new HttpClient();
                var resp   = await client.GetAsync("http://localhost:21881/metrics");

                Assert.True(resp.IsSuccessStatusCode);
                var content = await resp.Content.ReadAsStringAsync();

                Assert.Contains("# HELP intelcpu_0_temperature_0 Metric reported by open hardware sensor", content);
            }
            finally
            {
                server.Stop();
            }
        }
コード例 #15
0
        public async void CanSetupTimescale()
        {
            var testContainersBuilder = new TestcontainersBuilder <TestcontainersContainer>()
                                        .WithDockerEndpoint(DockerUtils.DockerEndpoint())
                                        .WithImage("timescale/timescaledb:latest-pg12")
                                        .WithEnvironment("POSTGRES_PASSWORD", "123456")
                                        .WithPortBinding(5432, assignRandomHostPort: true)
                                        .WithWaitStrategy(Wait.ForUnixContainer()
                                                          .UntilCommandIsCompleted("pg_isready -h 'localhost' -p '5432'"));

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

            string connStr = $"Host={container.Hostname};Username=postgres;Password=123456;Port={container.GetMappedPublicPort(5432)}";
            var    epoch   = new DateTime(2001, 1, 13);

            using var writer     = new TimescaleWriter(connStr, true, "my-pc");
            await using var conn = new NpgsqlConnection(connStr);
            await writer.ReportMetrics(epoch, TestSensorCreator.Values());

            conn.Open();
            await using var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM ohm_stats", conn);
            Assert.Equal(3, Convert.ToInt32(cmd.ExecuteScalar()));
        }
コード例 #16
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;
            }
        }