Skip to content

Commit

Permalink
in_docker: Add unit test and make configurable paths (mostly for test…
Browse files Browse the repository at this point in the history
…ing)

Signed-off-by: Hiroshi Hatake <hatake@calyptia.com>
  • Loading branch information
cosmo0920 committed Oct 27, 2023
1 parent b9d0dfb commit f8780b3
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 42 deletions.
54 changes: 37 additions & 17 deletions plugins/in_docker/cgroup_v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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, "/");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
49 changes: 34 additions & 15 deletions plugins/in_docker/cgroup_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
#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;
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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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-");
Expand All @@ -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-");
Expand All @@ -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-");
Expand All @@ -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, "/");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions plugins/in_docker/docker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}
};
Expand Down
Loading

0 comments on commit f8780b3

Please sign in to comment.