@@ -117,6 +117,7 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx,
117117#define new_fleet_config_filename (a ) fleet_config_filename((a), "new")
118118#define cur_fleet_config_filename (a ) fleet_config_filename((a), "cur")
119119#define old_fleet_config_filename (a ) fleet_config_filename((a), "old")
120+ #define hdr_fleet_config_filename (a ) fleet_config_filename((a), "header")
120121
121122static int get_calyptia_files (struct flb_in_calyptia_fleet_config * ctx ,
122123 const char * url ,
@@ -388,7 +389,6 @@ static int is_timestamped_fleet_config_path(struct flb_in_calyptia_fleet_config
388389 errno = 0 ;
389390 val = strtol (fname , & end , 10 );
390391 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN )) || (errno != 0 && val == 0 )) {
391- flb_errno ();
392392 return FLB_FALSE ;
393393 }
394394
@@ -480,6 +480,23 @@ static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
480480 return ret ;
481481}
482482
483+ static int exists_header_fleet_config (struct flb_in_calyptia_fleet_config * ctx )
484+ {
485+ int ret = FLB_FALSE ;
486+ flb_sds_t cfgheadername ;
487+
488+ cfgheadername = hdr_fleet_config_filename (ctx );
489+ if (cfgheadername == NULL ) {
490+ flb_plg_error (ctx -> ins , "unable to allocate configuration name" );
491+ return FLB_FALSE ;
492+ }
493+
494+ ret = access (cfgheadername , F_OK ) == 0 ? FLB_TRUE : FLB_FALSE ;
495+ flb_sds_destroy (cfgheadername );
496+
497+ return ret ;
498+ }
499+
483500static void * do_reload (void * data )
484501{
485502 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)
16651682 }
16661683}
16671684
1685+ static flb_sds_t get_fleet_id_from_header (struct flb_in_calyptia_fleet_config * ctx )
1686+ {
1687+ struct mk_list * head ;
1688+ struct flb_cf_section * section ;
1689+ flb_sds_t fleet_id ;
1690+ flb_sds_t name ;
1691+ struct flb_cf * cf_hdr ;
1692+
1693+
1694+ if (exists_header_fleet_config (ctx )) {
1695+ cf_hdr = flb_cf_create_from_file (NULL , hdr_fleet_config_filename (ctx ));
1696+
1697+ if (cf_hdr == NULL ) {
1698+ flb_cf_destroy (cf_hdr );
1699+ return NULL ;
1700+ }
1701+
1702+ mk_list_foreach (head , & cf_hdr -> sections ) {
1703+ section = mk_list_entry (head , struct flb_cf_section , _head );
1704+
1705+ if (strcasecmp (section -> name , "custom" ) != 0 ) {
1706+ continue ;
1707+ }
1708+
1709+ name = flb_cf_section_property_get_string (cf_hdr , section , "name" );
1710+
1711+ if (!name ) {
1712+ flb_plg_error (ctx -> ins , "no name in fleet header" );
1713+ flb_cf_destroy (cf_hdr );
1714+ return NULL ;
1715+ }
1716+
1717+ if (strcasecmp (name , "calyptia" ) != 0 ) {
1718+ flb_sds_destroy (name );
1719+ continue ;
1720+ }
1721+ flb_sds_destroy (name );
1722+
1723+ fleet_id = flb_cf_section_property_get_string (cf_hdr , section , "fleet_id" );
1724+
1725+ if (!fleet_id ) {
1726+ flb_plg_error (ctx -> ins , "no fleet_id in fleet header" );
1727+ flb_cf_destroy (cf_hdr );
1728+ return NULL ;
1729+ }
1730+
1731+ flb_cf_destroy (cf_hdr );
1732+ return fleet_id ;
1733+ }
1734+ }
1735+
1736+ flb_cf_destroy (cf_hdr );
1737+ return NULL ;
1738+ }
1739+
16681740flb_sds_t fleet_config_get (struct flb_in_calyptia_fleet_config * ctx )
16691741{
16701742 flb_sds_t buf ;
16711743 struct mk_list * head ;
16721744 struct flb_custom_instance * c_ins ;
16731745 flb_ctx_t * flb = flb_context_get ();
1746+ flb_sds_t fleet_id = NULL ;
16741747
16751748
16761749 buf = flb_sds_create_size (2048 );
@@ -1679,7 +1752,6 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
16791752 return NULL ;
16801753 }
16811754
1682- /* [INPUT] */
16831755 mk_list_foreach (head , & flb -> config -> customs ) {
16841756 c_ins = mk_list_entry (head , struct flb_custom_instance , _head );
16851757 if (strcasecmp (c_ins -> p -> name , "calyptia" )) {
@@ -1690,8 +1762,21 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
16901762
16911763 fleet_config_get_properties (& buf , & c_ins -> properties );
16921764
1693- if (ctx -> fleet_id && flb_config_prop_get ("fleet_id" , & c_ins -> properties ) == NULL ) {
1694- flb_sds_printf (& buf , " fleet_id %s\n" , ctx -> fleet_id );
1765+ if (flb_config_prop_get ("fleet_id" , & c_ins -> properties ) == NULL ) {
1766+ if (ctx -> fleet_id != NULL ) {
1767+ flb_sds_printf (& buf , " fleet_id %s\n" , ctx -> fleet_id );
1768+ }
1769+ else {
1770+ fleet_id = get_fleet_id_from_header (ctx );
1771+
1772+ if (fleet_id == NULL ) {
1773+ flb_plg_error (ctx -> ins , "unable to get fleet_id from header" );
1774+ return NULL ;
1775+ }
1776+
1777+ flb_sds_printf (& buf , " fleet_id %s\n" , fleet_id );
1778+ flb_sds_destroy (fleet_id );
1779+ }
16951780 }
16961781 }
16971782 flb_sds_printf (& buf , "\n" );
@@ -2215,7 +2300,9 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
22152300 /* refresh calyptia settings before attempting to load the fleet
22162301 * configuration file.
22172302 */
2218- create_fleet_header (ctx );
2303+ if (exists_header_fleet_config (ctx ) == FLB_TRUE ) {
2304+ create_fleet_header (ctx );
2305+ }
22192306
22202307 /* if we load a new configuration then we will be reloaded anyways */
22212308 if (load_fleet_config (ctx ) == FLB_TRUE ) {
0 commit comments