From 1aeb6c0cec5987d867805ef9c3b63b8c86fbe289 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 27 Oct 2023 19:50:19 -0300 Subject: [PATCH] in_calyptia_fleet: update configuration upon invocation. Use a header.conf file to save all the base settings so they can be updated and maintained between invocations. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 145 ++++++++++++------ 1 file changed, 101 insertions(+), 44 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index d4bf4c22288..da9bed4854e 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,7 @@ #include #include #include +#include #include #include @@ -1287,12 +1289,100 @@ static int calyptia_config_rollback(struct flb_in_calyptia_fleet_config *ctx, return 0; } +static void fleet_config_get_properties(flb_sds_t *buf, struct mk_list *props) +{ + struct mk_list *head; + struct flb_kv *kv; + + mk_list_foreach(head, props) { + kv = mk_list_entry(head, struct flb_kv, _head); + + if (kv->key != NULL && kv->val != NULL) { + flb_sds_printf(buf, " %s ", kv->key); + flb_sds_cat_safe(buf, kv->val, strlen(kv->val)); + flb_sds_cat_safe(buf, "\n", 1); + } + } +} + +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; + + buf = flb_sds_create_size(2048); + + if (!buf) { + return NULL; + } + + /* [INPUT] */ + mk_list_foreach(head, &ctx->config->customs) { + c_ins = mk_list_entry(head, struct flb_custom_instance, _head); + if (strcasecmp(c_ins->p->name, "calyptia")) { + continue; + } + flb_sds_printf(&buf, "[CUSTOM]\n"); + flb_sds_printf(&buf, " name %s\n", c_ins->p->name); + + 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); + } + } + flb_sds_printf(&buf, "\n"); + + return buf; +} + +static int create_fleet_header(struct flb_in_calyptia_fleet_config *ctx) +{ + flb_sds_t hdrname; + FILE *fp; + flb_sds_t header; + int rc = FLB_FALSE; + + + hdrname = fleet_config_filename(ctx, "header"); + if (hdrname == NULL) { + goto hdrname_error; + } + + header = fleet_config_get(ctx); + if (header == NULL) { + goto header_error; + } + + fp = fopen(hdrname, "w+"); + if (fp == NULL) { + goto file_open_error; + } + + if (fwrite(header, strlen(header), 1, fp) < 1) { + goto file_error; + } + + rc = FLB_TRUE; + +file_error: + fclose(fp); +file_open_error: + flb_sds_destroy(header); +header_error: + flb_sds_destroy(hdrname); +hdrname_error: + return rc; +} + static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_connection *u_conn) { flb_sds_t cfgname; flb_sds_t cfgnewname; flb_sds_t header; + flb_sds_t hdrname; time_t time_last_modified; int ret = -1; @@ -1306,50 +1396,11 @@ static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx, flb_sds_printf(&ctx->fleet_files_url, "/v1/fleets/%s/files", ctx->fleet_id); } - header = flb_sds_create_size(4096); - - if (ctx->fleet_name == NULL) { - flb_sds_printf(&header, - "[CUSTOM]\n" - " Name calyptia\n" - " api_key %s\n" - " fleet_id %s\n" - " add_label fleet_id %s\n" - " fleet.config_dir %s\n" - " calyptia_host %s\n" - " calyptia_port %d\n" - " calyptia_tls %s\n", - ctx->api_key, - ctx->fleet_id, - ctx->fleet_id, - ctx->config_dir, - ctx->ins->host.name, - ctx->ins->host.port, - tls_setting_string(ctx->ins->use_tls) - ); - } - else { - flb_sds_printf(&header, - "[CUSTOM]\n" - " Name calyptia\n" - " api_key %s\n" - " fleet_name %s\n" - " fleet_id %s\n" - " add_label fleet_id %s\n" - " fleet.config_dir %s\n" - " calyptia_host %s\n" - " calyptia_port %d\n" - " calyptia_tls %s\n", - ctx->api_key, - ctx->fleet_name, - ctx->fleet_id, - ctx->fleet_id, - ctx->config_dir, - ctx->ins->host.name, - ctx->ins->host.port, - tls_setting_string(ctx->ins->use_tls) - ); - } + create_fleet_header(ctx); + + hdrname = fleet_config_filename(ctx, "header"); + header = flb_sds_create_size(32); + flb_sds_printf(&header, "@include %s\n\n", hdrname); /* create the base file. */ ret = get_calyptia_file(ctx, u_conn, ctx->fleet_url, header, @@ -1745,6 +1796,7 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in, } ctx->ins = in; ctx->collect_fd = -1; + ctx->config = config; /* Load the config map */ @@ -1803,6 +1855,11 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in, /* Set the context */ flb_input_set_context(in, ctx); + /* refresh calyptia settings before attempting to load the fleet + * configuration file. + */ + create_fleet_header(ctx); + /* if we load a new configuration then we will be reloaded anyways */ if (load_fleet_config(ctx) == FLB_TRUE) { return 0;