diff --git a/plugins/in_docker/cgroup_v1.c b/plugins/in_docker/cgroup_v1.c index a6fe355e220..17d4d384160 100644 --- a/plugins/in_docker/cgroup_v1.c +++ b/plugins/in_docker/cgroup_v1.c @@ -25,11 +25,14 @@ #include "docker.h" /* This method returns list of currently running docker ids. */ -static struct mk_list *get_active_dockers() +static struct mk_list *get_active_dockers(struct flb_docker *ctx) { DIR *dp; struct dirent *ep; struct mk_list *list; + char path[SYSFS_FILE_PATH_SIZE]; + + path[0] = '\0'; list = flb_malloc(sizeof(struct mk_list)); if (!list) { @@ -38,7 +41,9 @@ static struct mk_list *get_active_dockers() } mk_list_init(list); - dp = opendir(DOCKER_CGROUP_V1_CPU_DIR); + snprintf(path, sizeof(path), "%s/%s", ctx->sysfs_path, DOCKER_CGROUP_V1_CPU_DIR); + + dp = opendir(path); if (dp != NULL) { ep = readdir(dp); @@ -102,20 +107,24 @@ static char *read_line(FILE *fin) } /* This routine returns path to docker's cgroup CPU usage file. */ -static char *get_cpu_used_file(char *id) +static char *get_cpu_used_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(105, sizeof(char)); + len = flb_sds_len(ctx->sysfs_path); + path = (char *) flb_calloc(92 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } + strcat(path, ctx->sysfs_path); + strcat(path, "/"); strcat(path, DOCKER_CGROUP_V1_CPU_DIR); strcat(path, "/"); strcat(path, id); @@ -126,19 +135,23 @@ static char *get_cpu_used_file(char *id) } /* This routine returns path to docker's cgroup memory limit file. */ -static char *get_mem_limit_file(char *id) +static char *get_mem_limit_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(116, sizeof(char)); + len = flb_sds_len(ctx->sysfs_path); + path = (char *) flb_calloc(102 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } + strcat(path, ctx->sysfs_path); + strcat(path, "/"); strcat(path, DOCKER_CGROUP_V1_MEM_DIR); strcat(path, "/"); strcat(path, id); @@ -149,19 +162,23 @@ static char *get_mem_limit_file(char *id) } /* This routine returns path to docker's cgroup memory used file. */ -static char *get_mem_used_file(char *id) +static char *get_mem_used_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(116, sizeof(char)); + len = flb_sds_len(ctx->sysfs_path); + path = (char *) flb_calloc(102 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } + strcat(path, ctx->sysfs_path); + strcat(path, "/"); strcat(path, DOCKER_CGROUP_V1_MEM_DIR); strcat(path, "/"); strcat(path, id); @@ -171,20 +188,23 @@ static char *get_mem_used_file(char *id) return path; } -static char *get_config_file(char *id) +static char *get_config_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(107, sizeof(char)); + len = flb_sds_len(ctx->containers_path); + path = (char *) flb_calloc(91 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } - strcat(path, DOCKER_LIB_ROOT); + + strcat(path, ctx->containers_path); strcat(path, "/"); strcat(path, id); strcat(path, "/"); @@ -230,7 +250,7 @@ static char *get_container_name(struct flb_docker *ctx, char *id) FILE *f = NULL; char *line; - config_file = get_config_file(id); + config_file = get_config_file(ctx, id); if (!config_file) { return NULL; } @@ -268,7 +288,7 @@ static cpu_snapshot *get_docker_cpu_snapshot(struct flb_docker *ctx, char *id) cpu_snapshot *snapshot = NULL; FILE *f; - usage_file = get_cpu_used_file(id); + usage_file = get_cpu_used_file(ctx, id); if (!usage_file) { return NULL; } @@ -314,7 +334,7 @@ static uint64_t get_docker_mem_used(struct flb_docker *ctx, char *id) uint64_t mem_used = 0; FILE *f; - usage_file = get_mem_used_file(id); + usage_file = get_mem_used_file(ctx, id); if (!usage_file) { return 0; } @@ -344,9 +364,9 @@ static uint64_t get_docker_mem_used(struct flb_docker *ctx, char *id) } /* Returns memory limit for a docker in bytes. */ -static uint64_t get_docker_mem_limit(char *id) +static uint64_t get_docker_mem_limit(struct flb_docker *ctx, char *id) { - char *limit_file = get_mem_limit_file(id); + char *limit_file = get_mem_limit_file(ctx, id); uint64_t mem_limit = 0; FILE *f; @@ -380,7 +400,7 @@ static mem_snapshot *get_docker_mem_snapshot(struct flb_docker *ctx, char *id) } snapshot->used = get_docker_mem_used(ctx, id); - snapshot->limit = get_docker_mem_limit(id); + snapshot->limit = get_docker_mem_limit(ctx, id); return snapshot; } diff --git a/plugins/in_docker/cgroup_v2.c b/plugins/in_docker/cgroup_v2.c index bc0c6732ce4..b44633c7d2b 100644 --- a/plugins/in_docker/cgroup_v2.c +++ b/plugins/in_docker/cgroup_v2.c @@ -25,7 +25,7 @@ #include "docker.h" /* This method returns list of currently running docker ids. */ -static struct mk_list *get_active_dockers() +static struct mk_list *get_active_dockers(struct flb_docker *ctx) { DIR *dp; struct dirent *ep; @@ -33,6 +33,9 @@ static struct mk_list *get_active_dockers() docker_info *docker; char *p = NULL; char *container_id = NULL; + char path[SYSFS_FILE_PATH_SIZE]; + + path[0] = '\0'; list = flb_malloc(sizeof(struct mk_list)); if (!list) { @@ -41,7 +44,9 @@ static struct mk_list *get_active_dockers() } mk_list_init(list); - dp = opendir(DOCKER_CGROUP_V2_DOCKER_SERVICE_DIR); + snprintf(path, sizeof(path), "%s/%s", ctx->sysfs_path, DOCKER_CGROUP_V2_DOCKER_SERVICE_DIR); + + dp = opendir(path); if (dp != NULL) { ep = readdir(dp); @@ -114,20 +119,24 @@ static char *read_line(FILE *fin) } /* This routine returns path to docker's cgroup CPU usage file. */ -static char *get_cpu_used_file(char *id) +static char *get_cpu_used_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(115, sizeof(char)); + len = flb_sds_len(ctx->sysfs_path); + path = (char *) flb_calloc(101 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } + strcat(path, ctx->sysfs_path); + strcat(path, "/"); strcat(path, DOCKER_CGROUP_V2_DOCKER_SERVICE_DIR); strcat(path, "/"); strcat(path, "docker-"); @@ -140,19 +149,23 @@ static char *get_cpu_used_file(char *id) } /* This routine returns path to docker's cgroup memory limit file. */ -static char *get_mem_limit_file(char *id) +static char *get_mem_limit_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(121, sizeof(char)); + len = flb_sds_len(ctx->sysfs_path); + path = (char *) flb_calloc(108 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } + strcat(path, ctx->sysfs_path); + strcat(path, "/"); strcat(path, DOCKER_CGROUP_V2_DOCKER_SERVICE_DIR); strcat(path, "/"); strcat(path, "docker-"); @@ -165,19 +178,23 @@ static char *get_mem_limit_file(char *id) } /* This routine returns path to docker's cgroup memory used file. */ -static char *get_mem_used_file(char *id) +static char *get_mem_used_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(121, sizeof(char)); + len = flb_sds_len(ctx->sysfs_path); + path = (char *) flb_calloc(108 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } + strcat(path, ctx->sysfs_path); + strcat(path, "/"); strcat(path, DOCKER_CGROUP_V2_DOCKER_SERVICE_DIR); strcat(path, "/"); strcat(path, "docker-"); @@ -189,20 +206,22 @@ static char *get_mem_used_file(char *id) return path; } -static char *get_config_file(char *id) +static char *get_config_file(struct flb_docker *ctx, char *id) { char *path; + int len = 0; if (!id) { return NULL; } - path = (char *) flb_calloc(107, sizeof(char)); + len = flb_sds_len(ctx->containers_path); + path = (char *) flb_calloc(91 + len, sizeof(char)); if (!path) { flb_errno(); return NULL; } - strcat(path, DOCKER_LIB_ROOT); + strcat(path, ctx->containers_path); strcat(path, "/"); strcat(path, id); strcat(path, "/"); @@ -248,7 +267,7 @@ static char *get_container_name(struct flb_docker *ctx, char *id) FILE *f = NULL; char *line; - config_file = get_config_file(id); + config_file = get_config_file(ctx, id); if (!config_file) { return NULL; } @@ -287,7 +306,7 @@ static cpu_snapshot *get_docker_cpu_snapshot(struct flb_docker *ctx, char *id) FILE *f; char *line = NULL; - usage_file = get_cpu_used_file(id); + usage_file = get_cpu_used_file(ctx, id); if (!usage_file) { return NULL; } @@ -363,7 +382,7 @@ static uint64_t get_docker_mem_used(struct flb_docker *ctx, char *id) char *usage_file = NULL; uint64_t mem_used = 0; - usage_file = get_mem_used_file(id); + usage_file = get_mem_used_file(ctx, id); if (!usage_file) { return 0; } @@ -378,7 +397,7 @@ static uint64_t get_docker_mem_used(struct flb_docker *ctx, char *id) static uint64_t get_docker_mem_limit(struct flb_docker *ctx, char *id) { int c; - char *limit_file = get_mem_limit_file(id); + char *limit_file = get_mem_limit_file(ctx, id); uint64_t mem_limit; char *line = NULL; FILE *f; diff --git a/plugins/in_docker/docker.c b/plugins/in_docker/docker.c index 8b86392fe94..21f47c3701f 100644 --- a/plugins/in_docker/docker.c +++ b/plugins/in_docker/docker.c @@ -482,7 +482,7 @@ static int cb_docker_collect(struct flb_input_instance *ins, (void) config; /* Get current active dockers. */ - active = ctx->cgroup_api.get_active_docker_ids(); + active = ctx->cgroup_api.get_active_docker_ids(ctx); filtered = apply_filters(ctx, active); if (!filtered) { @@ -564,10 +564,15 @@ static struct flb_config_map config_map[] = { "A space-separated list of containers to exclude" }, { - FLB_CONFIG_MAP_STR, "path.sysfs", SYSFS_PATH, + FLB_CONFIG_MAP_STR, "path.sysfs", DEFAULT_SYSFS_PATH, 0, FLB_TRUE, offsetof(struct flb_docker, sysfs_path), "sysfs mount point" }, + { + FLB_CONFIG_MAP_STR, "path.containers", DEFAULT_CONTAINERS_PATH, + 0, FLB_TRUE, offsetof(struct flb_docker, containers_path), + "containers directory" + }, /* EOF */ {0} }; diff --git a/plugins/in_docker/docker.h b/plugins/in_docker/docker.h index 79695d9c6d1..b6eb6cbb56f 100644 --- a/plugins/in_docker/docker.h +++ b/plugins/in_docker/docker.h @@ -29,7 +29,8 @@ /* Distinguish cgroup v2 or v1 */ #define SYSFS_FILE_PATH_SIZE 512 -#define SYSFS_PATH "/sys/fs/cgroup" +#define DEFAULT_SYSFS_PATH "/sys/fs/cgroup" +#define DEFAULT_CONTAINERS_PATH "/var/lib/docker/containers" #define CGROUP_V2_PATH "cgroup.controllers" #define CGROUP_V1 1 #define CGROUP_V2 2 @@ -43,23 +44,20 @@ #define DOCKER_CGROUP_V2_LONG_ID_LEN 77 /* docker-CONTAINERID.scope: 7 + 64 + 6 */ /* Files from sysfs containing metrics (cgroups v1) */ -#define DOCKER_CGROUP_V1_MEM_DIR "/sys/fs/cgroup/memory/docker" -#define DOCKER_CGROUP_V1_CPU_DIR "/sys/fs/cgroup/cpu/docker" +#define DOCKER_CGROUP_V1_MEM_DIR "memory/docker" +#define DOCKER_CGROUP_V1_CPU_DIR "cpu/docker" #define DOCKER_CGROUP_V1_MEM_LIMIT_FILE "memory.limit_in_bytes" #define DOCKER_CGROUP_V1_MEM_USAGE_FILE "memory.usage_in_bytes" #define DOCKER_CGROUP_V1_CPU_USAGE_FILE "cpuacct.usage" /* Files from sysfs containing metrics (cgroups v2) */ -#define DOCKER_CGROUP_V2_DOCKER_SERVICE_DIR "/sys/fs/cgroup/system.slice" +#define DOCKER_CGROUP_V2_DOCKER_SERVICE_DIR "system.slice" #define DOCKER_CGROUP_V2_MEM_USAGE_FILE "memory.current" -#define DOCKER_CGROUP_V2_MEM_PEAK_FILE "memory.peak" -#define DOCKER_CGROUP_V2_MEM_STAT_FILE "memory.stat" #define DOCKER_CGROUP_V2_MEM_MAX_FILE "memory.max" #define DOCKER_CGROUP_V2_CPU_USAGE_FILE "cpu.stat" #define DOCKER_CGROUP_V2_CPU_USAGE_KEY "usage_usec" #define DOCKER_CGROUP_V2_CPU_USAGE_TEMPLATE DOCKER_CGROUP_V2_CPU_USAGE_KEY" %lu" -#define DOCKER_LIB_ROOT "/var/lib/docker/containers" #define DOCKER_CONFIG_JSON "config.v2.json" #define DOCKER_NAME_ARG "\"Name\"" #define DEFAULT_INTERVAL_SEC "1" @@ -113,8 +111,9 @@ struct flb_docker { /* cgroup version used by host */ int cgroup_version; - /* proc and sys paths, overwriting mostly for testing */ + /* sys path, overwriting mostly for testing */ flb_sds_t sysfs_path; + flb_sds_t containers_path; }; int in_docker_collect(struct flb_input_instance *i_ins, diff --git a/tests/runtime/CMakeLists.txt b/tests/runtime/CMakeLists.txt index d6d3e710153..b458ca9314b 100644 --- a/tests/runtime/CMakeLists.txt +++ b/tests/runtime/CMakeLists.txt @@ -56,6 +56,7 @@ if(FLB_OUT_LIB) FLB_RT_TEST(FLB_IN_TCP "in_tcp.c") FLB_RT_TEST(FLB_IN_FORWARD "in_forward.c") FLB_RT_TEST(FLB_IN_FLUENTBIT_METRICS "in_fluentbit_metrics.c") + FLB_RT_TEST(FLB_IN_DOCKER "in_docker.c") endif() # Filter Plugins diff --git a/tests/runtime/data/docker/cgroupv1/cpu/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/cpuacct.usage b/tests/runtime/data/docker/cgroupv1/cpu/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/cpuacct.usage new file mode 100644 index 00000000000..3fdfe39791f --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/cpu/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/cpuacct.usage @@ -0,0 +1 @@ +34840870 diff --git a/tests/runtime/data/docker/cgroupv1/cpu/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/cpuacct.usage b/tests/runtime/data/docker/cgroupv1/cpu/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/cpuacct.usage new file mode 100644 index 00000000000..5d05c05178b --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/cpu/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/cpuacct.usage @@ -0,0 +1 @@ +27762470638 diff --git a/tests/runtime/data/docker/cgroupv1/memory/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/memory.limit_in_bytes b/tests/runtime/data/docker/cgroupv1/memory/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/memory.limit_in_bytes new file mode 100644 index 00000000000..564113cfaff --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/memory/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/memory.limit_in_bytes @@ -0,0 +1 @@ +9223372036854771712 diff --git a/tests/runtime/data/docker/cgroupv1/memory/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/memory.usage_in_bytes b/tests/runtime/data/docker/cgroupv1/memory/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/memory.usage_in_bytes new file mode 100644 index 00000000000..bceefd80764 --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/memory/docker/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/memory.usage_in_bytes @@ -0,0 +1 @@ +6299648 diff --git a/tests/runtime/data/docker/cgroupv1/memory/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/memory.limit_in_bytes b/tests/runtime/data/docker/cgroupv1/memory/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/memory.limit_in_bytes new file mode 100644 index 00000000000..564113cfaff --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/memory/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/memory.limit_in_bytes @@ -0,0 +1 @@ +9223372036854771712 diff --git a/tests/runtime/data/docker/cgroupv1/memory/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/memory.usage_in_bytes b/tests/runtime/data/docker/cgroupv1/memory/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/memory.usage_in_bytes new file mode 100644 index 00000000000..c4a44547248 --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/memory/docker/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/memory.usage_in_bytes @@ -0,0 +1 @@ +207499264 diff --git a/tests/runtime/data/docker/cgroupv1/var/lib/containers/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/config.v2.json b/tests/runtime/data/docker/cgroupv1/var/lib/containers/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/config.v2.json new file mode 100644 index 00000000000..aca73092a1b --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/var/lib/containers/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/config.v2.json @@ -0,0 +1 @@ +{"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":29069,"ExitCode":0,"Error":"","StartedAt":"2023-10-27T08:44:36.155139396Z","FinishedAt":"0001-01-01T00:00:00Z","Health":null},"ID":"3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0","Created":"2023-10-27T08:44:35.32433294Z","Managed":false,"Path":"bash","Args":[],"Config":{"Hostname":"3d02975d4378","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","FLB_NIGHTLY_BUILD="],"Cmd":["bash"],"Image":"flb-centos/6","Volumes":{"/output":{}},"WorkingDir":"/source/fluent-bit/build","Entrypoint":null,"OnBuild":null,"Labels":{"org.label-schema.build-date":"20180804","org.label-schema.license":"GPLv2","org.label-schema.name":"CentOS Base Image","org.label-schema.schema-version":"1.0","org.label-schema.vendor":"CentOS"}},"Image":"sha256:3b0ad3331dc8a77acc3806a08670bd032ad9f09dead969665227f53c5e60b5c0","ImageManifest":null,"NetworkSettings":{"Bridge":"","SandboxID":"65f97cd334d78f75c52c9c78e0364ecfd0163eefaaba7ae40d256d7c98b7396b","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"23f3d00ab7e93205b435592d7476ba780635bd724e4d0e13288dd6bb5e056d56","EndpointID":"b6b2a5d3f4b0a44b6c01200100f14b4d254d5d80b710709eb525fb6b812f6311","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":{},"SandboxKey":"/var/run/docker/netns/65f97cd334d7","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":true,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0-json.log","Name":"/gracious_perlman","Driver":"devicemapper","OS":"linux","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{"/output":{"Source":"","Destination":"/output","RW":true,"Name":"65bc4548d84ec6d8d5b0e562c92b9390ac7eaf699fcdb510f6c59d9701225661","Driver":"local","Type":"volume","ID":"6b25dc17d1550ae57fb1bce6eaf4ac2d84e02236b851c3de3e380097a371ccfa","Spec":{},"SkipMountpointCreation":false}},"SecretReferences":null,"ConfigReferences":null,"MountLabel":"","ProcessLabel":"","AppArmorProfile":"docker-default","SeccompProfile":"","NoNewPrivileges":false,"HostnamePath":"/var/lib/docker/containers/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/hostname","HostsPath":"/var/lib/docker/containers/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/3d02975d4378f74f9a5005206e14745ce76947d970fad44ac6a5c5b80a3ccdf0/resolv.conf","LocalLogCacheMeta":{"HaveNotifyEnabled":false}} diff --git a/tests/runtime/data/docker/cgroupv1/var/lib/containers/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/config.v2.json b/tests/runtime/data/docker/cgroupv1/var/lib/containers/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/config.v2.json new file mode 100644 index 00000000000..4bde9f70164 --- /dev/null +++ b/tests/runtime/data/docker/cgroupv1/var/lib/containers/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/config.v2.json @@ -0,0 +1 @@ +{"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":5107,"ExitCode":0,"Error":"","StartedAt":"2023-10-27T04:52:26.05689545Z","FinishedAt":"2023-10-27T04:48:30.219844245Z","Health":null},"ID":"c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750","Created":"2023-09-20T10:31:52.089320958Z","Managed":false,"Path":"/run.sh","Args":[],"Config":{"Hostname":"c652e8674cc6","Domainname":"","User":"472","AttachStdin":false,"AttachStdout":true,"AttachStderr":true,"ExposedPorts":{"3000/tcp":{}},"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/share/grafana/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","GF_PATHS_CONFIG=/etc/grafana/grafana.ini","GF_PATHS_DATA=/var/lib/grafana","GF_PATHS_HOME=/usr/share/grafana","GF_PATHS_LOGS=/var/log/grafana","GF_PATHS_PLUGINS=/var/lib/grafana/plugins","GF_PATHS_PROVISIONING=/etc/grafana/provisioning"],"Cmd":null,"Image":"grafana/grafana-enterprise","Volumes":null,"WorkingDir":"/usr/share/grafana","Entrypoint":["/run.sh"],"OnBuild":null,"Labels":{"com.docker.compose.config-hash":"afbb178592bb8885a21ad0a453860ce2e046564771fd7f87b59f4cde49c57e79","com.docker.compose.container-number":"1","com.docker.compose.depends_on":"","com.docker.compose.image":"sha256:d0cc3278a354c735e94015ab3cef97ac1f3f3eb61e7bfe748a1789c91cd975f2","com.docker.compose.oneoff":"False","com.docker.compose.project":"loki","com.docker.compose.project.config_files":"/media/Data3/Gitrepo/work/loki/docker-compose.yaml","com.docker.compose.project.working_dir":"/media/Data3/Gitrepo/work/loki","com.docker.compose.service":"grafana","com.docker.compose.version":"2.21.0","maintainer":"Grafana Labs \u003chello@grafana.com\u003e"}},"Image":"sha256:d0cc3278a354c735e94015ab3cef97ac1f3f3eb61e7bfe748a1789c91cd975f2","ImageManifest":null,"NetworkSettings":{"Bridge":"","SandboxID":"953b8faa301f242af2eacc0758cb3d851f07556aa9a9e936425888b4536a2db6","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"loki_default":{"IPAMConfig":null,"Links":null,"Aliases":["grafana","grafana","c652e8674cc6"],"NetworkID":"e4fd739d2990b8807c102051422208bf674441d175e2ac0e3137a1f9becbd049","EndpointID":"041933e568c5a99b852e4eaf656832217c880782e4b9fb5f11dfe41f82a91dc5","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:12:00:02","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":{"3000/tcp":[{"HostIp":"0.0.0.0","HostPort":"3000"},{"HostIp":"::","HostPort":"3000"}]},"SandboxKey":"/var/run/docker/netns/953b8faa301f","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750-json.log","Name":"/grafana","Driver":"devicemapper","OS":"linux","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{},"SecretReferences":null,"ConfigReferences":null,"MountLabel":"","ProcessLabel":"","AppArmorProfile":"docker-default","SeccompProfile":"","NoNewPrivileges":false,"HostnamePath":"/var/lib/docker/containers/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/hostname","HostsPath":"/var/lib/docker/containers/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/c652e8674cc618ab3bda66c5bb5af93a88f9fa4efec4e4c0851c68dde4b25750/resolv.conf","LocalLogCacheMeta":{"HaveNotifyEnabled":false}} diff --git a/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/cpu.stat b/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/cpu.stat new file mode 100644 index 00000000000..5acbeea1f3b --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/cpu.stat @@ -0,0 +1,9 @@ +usage_usec 53929 +user_usec 23161 +system_usec 30767 +core_sched.force_idle_usec 0 +nr_periods 0 +nr_throttled 0 +throttled_usec 0 +nr_bursts 0 +burst_usec 0 diff --git a/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/memory.current b/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/memory.current new file mode 100644 index 00000000000..c46feb95fb1 --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/memory.current @@ -0,0 +1 @@ +1208320 diff --git a/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/memory.max b/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/memory.max new file mode 100644 index 00000000000..355295a05af --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/system.slice/docker-043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57.scope/memory.max @@ -0,0 +1 @@ +max diff --git a/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/cpu.stat b/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/cpu.stat new file mode 100644 index 00000000000..24ca577a80a --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/cpu.stat @@ -0,0 +1,9 @@ +usage_usec 22713 +user_usec 15142 +system_usec 7571 +core_sched.force_idle_usec 0 +nr_periods 0 +nr_throttled 0 +throttled_usec 0 +nr_bursts 0 +burst_usec 0 diff --git a/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/memory.current b/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/memory.current new file mode 100644 index 00000000000..9f416a73250 --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/memory.current @@ -0,0 +1 @@ +823296 diff --git a/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/memory.max b/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/memory.max new file mode 100644 index 00000000000..a5e19aef5cf --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/system.slice/docker-1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c.scope/memory.max @@ -0,0 +1 @@ +314572800 diff --git a/tests/runtime/data/docker/cgroupv2/var/lib/containers/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57/config.v2.json b/tests/runtime/data/docker/cgroupv2/var/lib/containers/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57/config.v2.json new file mode 100644 index 00000000000..b11ae1c0d2a --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/var/lib/containers/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57/config.v2.json @@ -0,0 +1 @@ +{"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":49272,"ExitCode":0,"Error":"","StartedAt":"2023-10-25T09:52:09.982799735Z","FinishedAt":"0001-01-01T00:00:00Z","Health":null},"ID":"043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57","Created":"2023-10-25T09:52:09.570906007Z","Managed":false,"Path":"bash","Args":[],"Config":{"Hostname":"043b521509b6","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["bash"],"Image":"ubuntu:jammy","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{"org.opencontainers.image.ref.name":"ubuntu","org.opencontainers.image.version":"22.04"}},"Image":"sha256:e4c58958181a5925816faa528ce959e487632f4cfd192f8132f71b32df2744b4","ImageManifest":null,"NetworkSettings":{"Bridge":"","SandboxID":"1775a1fa35f3d0f183ef97a8ff3390518a095c12c5a12207b9f8a7c058a2b3e9","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"eb1721bfd3c14e9b4cf7247a9fd09261b0150a3db03249c8140330a2fb1bfaf4","EndpointID":"9269b217c2bdf0c0f7d7ef79d402f1a2b642cafc7c1fc25eef1db07fb91673d0","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":{},"SandboxKey":"/var/run/docker/netns/1775a1fa35f3","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":true,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57-json.log","Name":"/objective_bassi","Driver":"overlay2","OS":"linux","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{},"SecretReferences":null,"ConfigReferences":null,"MountLabel":"","ProcessLabel":"","AppArmorProfile":"docker-default","SeccompProfile":"","NoNewPrivileges":false,"HostnamePath":"/var/lib/docker/containers/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57/hostname","HostsPath":"/var/lib/docker/containers/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/043b521509b63b46e785a897f9264cecb3531ebc83d5072fcaeeeae6336bce57/resolv.conf","LocalLogCacheMeta":{"HaveNotifyEnabled":false}} diff --git a/tests/runtime/data/docker/cgroupv2/var/lib/containers/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c/config.v2.json b/tests/runtime/data/docker/cgroupv2/var/lib/containers/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c/config.v2.json new file mode 100644 index 00000000000..143383b8f45 --- /dev/null +++ b/tests/runtime/data/docker/cgroupv2/var/lib/containers/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c/config.v2.json @@ -0,0 +1 @@ +{"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":335950,"ExitCode":0,"Error":"","StartedAt":"2023-10-26T06:18:51.210422206Z","FinishedAt":"0001-01-01T00:00:00Z","Health":null},"ID":"1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c","Created":"2023-10-26T06:18:50.955247084Z","Managed":false,"Path":"bash","Args":[],"Config":{"Hostname":"1a38a7677732","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["bash"],"Image":"debian:bullseye","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"Image":"sha256:7fc304c3e97a96858362b53cf42c7ed881dc38caa9a32903b2da14d56af6c690","ImageManifest":null,"NetworkSettings":{"Bridge":"","SandboxID":"4dbbc45a1ecc5368d868bfbec17af9c1030c08c9f0670b9463c6de7558307076","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"eb1721bfd3c14e9b4cf7247a9fd09261b0150a3db03249c8140330a2fb1bfaf4","EndpointID":"b90749919387ead36cb1a1564e28be640642d592977d321b84bf7cf8015ec510","Gateway":"172.17.0.1","IPAddress":"172.17.0.3","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:03","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":{},"SandboxKey":"/var/run/docker/netns/4dbbc45a1ecc","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":true,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c-json.log","Name":"/confident_snyder","Driver":"overlay2","OS":"linux","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{},"SecretReferences":null,"ConfigReferences":null,"MountLabel":"","ProcessLabel":"","AppArmorProfile":"docker-default","SeccompProfile":"","NoNewPrivileges":false,"HostnamePath":"/var/lib/docker/containers/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c/hostname","HostsPath":"/var/lib/docker/containers/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/1a38a7677732c264a914122d82b7b03e083fdadf8d0586b9df57db33a8f62e0c/resolv.conf","LocalLogCacheMeta":{"HaveNotifyEnabled":false}} diff --git a/tests/runtime/in_docker.c b/tests/runtime/in_docker.c new file mode 100644 index 00000000000..e3ad4d9802d --- /dev/null +++ b/tests/runtime/in_docker.c @@ -0,0 +1,118 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2023 The Fluent Bit Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "flb_tests_runtime.h" + +#define DPATH_DOCKER_SYSFS_CGROUPV1 FLB_TESTS_DATA_PATH "/data/docker/cgroupv1" +#define DPATH_DOCKER_CONTAINER_CGROUPV1 FLB_TESTS_DATA_PATH "/data/docker/cgroupv1/var/lib/containers" +#define DPATH_DOCKER_SYSFS_CGROUPV2 FLB_TESTS_DATA_PATH "/data/docker/cgroupv2" +#define DPATH_DOCKER_CONTAINER_CGROUPV2 FLB_TESTS_DATA_PATH "/data/docker/cgroupv2/var/lib/containers" + +int check_metric(flb_ctx_t *ctx, char *name) { + struct mk_list *tmp; + struct mk_list *head; + struct cfl_list *inner_tmp; + struct cfl_list *inner_head; + + struct flb_input_instance *i_ins; + struct cmt_counter *counter; + + int number_of_metrics = 0; + + mk_list_foreach_safe(head, tmp, &ctx->config->inputs) { + i_ins = mk_list_entry(head, struct flb_input_instance, _head); + cfl_list_foreach_safe(inner_head, inner_tmp, &i_ins->cmt->counters) { + counter = cfl_list_entry(inner_head, struct cmt_counter, _head); + + if (strlen(name) != 0 && strcmp(name, counter->opts.name) == 0) { + return 0; + } + number_of_metrics++; + } + } + return number_of_metrics; + +} + +void do_create(flb_ctx_t *ctx, char *system, ...) +{ + int in_ffd; + int out_ffd; + va_list va; + char *key; + char *value; + + in_ffd = flb_input(ctx, (char *) system, NULL); + + va_start(va, system); + while ((key = va_arg(va, char *))) { + value = va_arg(va, char *); + TEST_CHECK(value != NULL); + TEST_CHECK(flb_input_set(ctx, in_ffd, key, value, NULL) == 0); + } + va_end(va); + + out_ffd = flb_output(ctx, (char *) "null", NULL); + TEST_CHECK(out_ffd >= 0); + + TEST_CHECK(flb_service_set(ctx, "Flush", "0.5", + "Grace", "1", + NULL) == 0); +} + +void do_destroy(flb_ctx_t *ctx) { + flb_stop(ctx); + flb_destroy(ctx); +} + +void flb_test_in_docker_cgroupv1() { + flb_ctx_t *ctx = flb_create(); + do_create(ctx, + "docker", + "path.sysfs", DPATH_DOCKER_SYSFS_CGROUPV1, + "path.containers", DPATH_DOCKER_CONTAINER_CGROUPV1, + NULL); + TEST_CHECK(flb_start(ctx) == 0); + sleep(1); + TEST_CHECK(check_metric(ctx, "cpu_used") != 0); + TEST_CHECK(check_metric(ctx, "mem_used") != 0); + TEST_CHECK(check_metric(ctx, "mem_limit") != 0); + do_destroy(ctx); +} + +void flb_test_in_docker_cgroupv2() { + flb_ctx_t *ctx = flb_create(); + do_create(ctx, + "docker", + "path.sysfs", DPATH_DOCKER_SYSFS_CGROUPV2, + "path.containers", DPATH_DOCKER_CONTAINER_CGROUPV2, + NULL); + TEST_CHECK(flb_start(ctx) == 0); + sleep(1); + TEST_CHECK(check_metric(ctx, "cpu_used") != 0); + TEST_CHECK(check_metric(ctx, "mem_used") != 0); + TEST_CHECK(check_metric(ctx, "mem_limit") != 0); + do_destroy(ctx); +} + +TEST_LIST = { + {"cgroup_v1", flb_test_in_docker_cgroupv1}, + {"cgroup_v2", flb_test_in_docker_cgroupv2}, + {NULL, NULL}};