Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env: add new ${FLUENT_BIT_PID} environment variable #9563

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugins/in_cpu/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,15 @@ static int cb_cpu_init(struct flb_input_instance *in,
int ret;
struct flb_cpu *ctx;
(void) data;

/* Allocate space for the configuration */
ctx = flb_calloc(1, sizeof(struct flb_cpu));
if (!ctx) {
flb_errno();
return -1;
}
ctx->ins = in;

ret = flb_input_config_map_set(in, (void *)ctx);
if (ret == -1) {
flb_free(ctx);
Expand Down Expand Up @@ -559,6 +559,7 @@ static int cb_cpu_init(struct flb_input_instance *in,
/* Get CPU load, ready to be updated once fired the calc callback */
if (ctx->pid > 0) {
ret = proc_cpu_pid_load(ctx, ctx->pid, &ctx->cstats);
flb_plg_info(in, "monitoring PID %i", ctx->pid);
}
else {
ret = proc_cpu_load(ctx->n_processors, &ctx->cstats);
Expand Down
23 changes: 22 additions & 1 deletion src/flb_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,26 @@ static int env_preset(struct flb_env *env)
}
}

/* Set the current process ID */
ret = snprintf(tmp, sizeof(tmp) - 1, "%i", getpid());
if (ret <= 0) {
flb_error("[env] could not set ${FLUENT_BIT_PID}");
return -1;
}
tmp[ret] = '\0';

ret = flb_env_set(env, "FLUENT_BIT_PID", tmp);
if (ret < 0) {
flb_error("[env] could not set ${FLUENT_BIT_PID}");
return -1;
}

return 0;
}

struct flb_env *flb_env_create()
{
int ret;
struct flb_env *env;
struct flb_hash_table *ht;

Expand All @@ -86,7 +101,11 @@ struct flb_env *flb_env_create()

env->warn_unused = FLB_TRUE;
env->ht = ht;
env_preset(env);
ret = env_preset(env);
if (ret == -1) {
flb_env_destroy(env);
return NULL;
}

return env;
}
Expand All @@ -109,6 +128,8 @@ int flb_env_set(struct flb_env *env, const char *key, const char *val)
klen = strlen(key);
vlen = strlen(val);

printf("[env set] key: %s, val: %s (vlen: %i)\n", key, val, vlen);

/* Check if the key is already set */
id = flb_hash_table_get(env->ht, key, klen, &out_buf, &out_size);
if (id >= 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/flb_hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static int entry_set_value(struct flb_hash_table_entry *entry, void *val, size_t
* If the entry already contains a previous value in the heap, just remove
* the previously assigned memory.
*/
if (entry->val_size > 0) {
if (entry->val_size > 0 && entry->val) {
flb_free(entry->val);
}

Expand All @@ -268,6 +268,7 @@ static int entry_set_value(struct flb_hash_table_entry *entry, void *val, size_t
entry->val = flb_malloc(val_size + 1);
if (!entry->val) {
flb_errno();
entry->val_size = 0;
return -1;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/internal/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,31 @@ void test_translate_long_env()
}


static void test_internal_vars()
{
int64_t pid;
char *tmp;
struct flb_env *env;

env = flb_env_create();
TEST_CHECK(env != NULL);

/* HOSTNAME */
tmp = flb_env_get(env, "HOSTNAME");
TEST_CHECK(tmp != NULL);

/* FLUENT_BIT_PID */
tmp = flb_env_get(env, "FLUENT_BIT_PID");
TEST_CHECK(tmp != NULL);

pid = getpid();
TEST_CHECK(pid == atoll(tmp));

flb_env_destroy(env);
}

TEST_LIST = {
{ "translate_long_env" , test_translate_long_env},
{ "internal_vars" , test_internal_vars},
{ NULL, NULL }
};
Loading