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

in_calyptia_fleet: fix registering agent after first invocation. #9516

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
97 changes: 92 additions & 5 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx,
#define new_fleet_config_filename(a) fleet_config_filename((a), "new")
#define cur_fleet_config_filename(a) fleet_config_filename((a), "cur")
#define old_fleet_config_filename(a) fleet_config_filename((a), "old")
#define hdr_fleet_config_filename(a) fleet_config_filename((a), "header")

static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
const char *url,
Expand Down Expand Up @@ -383,7 +384,6 @@ static int is_timestamped_fleet_config_path(struct flb_in_calyptia_fleet_config
errno = 0;
val = strtol(fname, &end, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) {
flb_errno();
return FLB_FALSE;
}

Expand Down Expand Up @@ -475,6 +475,23 @@ static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
return ret;
}

static int exists_header_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
int ret = FLB_FALSE;
flb_sds_t cfgheadername;

cfgheadername = hdr_fleet_config_filename(ctx);
if (cfgheadername == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}

ret = access(cfgheadername, F_OK) == 0 ? FLB_TRUE : FLB_FALSE;
flb_sds_destroy(cfgheadername);

return ret;
}

static void *do_reload(void *data)
{
struct reload_ctx *reload = (struct reload_ctx *)data;
Expand Down Expand Up @@ -1659,12 +1676,68 @@ static void fleet_config_get_properties(flb_sds_t *buf, struct mk_list *props)
}
}

static flb_sds_t get_fleet_id_from_header(struct flb_in_calyptia_fleet_config *ctx)
{
struct mk_list *head;
struct flb_cf_section *section;
flb_sds_t fleet_id;
flb_sds_t name;
struct flb_cf *cf_hdr;


if (exists_header_fleet_config(ctx)) {
cf_hdr = flb_cf_create_from_file(NULL, hdr_fleet_config_filename(ctx));

if (cf_hdr == NULL) {
flb_cf_destroy(cf_hdr);
return NULL;
}

mk_list_foreach(head, &cf_hdr->sections) {
section = mk_list_entry(head, struct flb_cf_section, _head);

if (strcasecmp(section->name, "custom") != 0) {
continue;
}

name = flb_cf_section_property_get_string(cf_hdr, section, "name");

if (!name) {
flb_plg_error(ctx->ins, "no name in fleet header");
flb_cf_destroy(cf_hdr);
return NULL;
}

if (strcasecmp(name, "calyptia") != 0) {
flb_sds_destroy(name);
continue;
}
flb_sds_destroy(name);

fleet_id = flb_cf_section_property_get_string(cf_hdr, section, "fleet_id");

if (!fleet_id) {
flb_plg_error(ctx->ins, "no fleet_id in fleet header");
flb_cf_destroy(cf_hdr);
return NULL;
}

flb_cf_destroy(cf_hdr);
return fleet_id;
}
}

flb_cf_destroy(cf_hdr);
return NULL;
}

flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
{
flb_sds_t buf;
struct mk_list *head;
struct flb_custom_instance *c_ins;
flb_ctx_t *flb = flb_context_get();
flb_sds_t fleet_id = NULL;


buf = flb_sds_create_size(2048);
Expand All @@ -1673,7 +1746,6 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
return NULL;
}

/* [INPUT] */
mk_list_foreach(head, &flb->config->customs) {
c_ins = mk_list_entry(head, struct flb_custom_instance, _head);
if (strcasecmp(c_ins->p->name, "calyptia")) {
Expand All @@ -1684,8 +1756,21 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)

fleet_config_get_properties(&buf, &c_ins->properties);

if (ctx->fleet_id && flb_config_prop_get("fleet_id", &c_ins->properties) == NULL) {
flb_sds_printf(&buf, " fleet_id %s\n", ctx->fleet_id);
if (flb_config_prop_get("fleet_id", &c_ins->properties) == NULL) {
if (ctx->fleet_id != NULL) {
flb_sds_printf(&buf, " fleet_id %s\n", ctx->fleet_id);
}
else {
fleet_id = get_fleet_id_from_header(ctx);

if (fleet_id == NULL) {
flb_plg_error(ctx->ins, "unable to get fleet_id from header");
return NULL;
}

flb_sds_printf(&buf, " fleet_id %s\n", fleet_id);
flb_sds_destroy(fleet_id);
}
}
}
flb_sds_printf(&buf, "\n");
Expand Down Expand Up @@ -2209,7 +2294,9 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
/* refresh calyptia settings before attempting to load the fleet
* configuration file.
*/
create_fleet_header(ctx);
if (exists_header_fleet_config(ctx) == FLB_TRUE) {
create_fleet_header(ctx);
}

/* if we load a new configuration then we will be reloaded anyways */
if (load_fleet_config(ctx) == FLB_TRUE) {
Expand Down
Loading