From e039c5634cd7b7271fe38cc8b64e68e6ab0cae5c Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Wed, 23 Oct 2024 11:52:27 -0300 Subject: [PATCH] in_calyptia_fleet: fix registering agent after first invocation. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 97 ++++++++++++++++++- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index 0128733aed6..f61068ceede 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -117,6 +117,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, @@ -388,7 +389,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; } @@ -480,6 +480,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; @@ -1665,12 +1682,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); @@ -1679,7 +1752,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")) { @@ -1690,8 +1762,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"); @@ -2215,7 +2300,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) {