@@ -378,7 +378,31 @@ static int in_snmp_init(struct flb_input_instance *in,
378378
379379 snmp_sess_init (& ctx -> session );
380380
381- ctx -> session .peername = ctx -> target_host ;
381+ {
382+ flb_plg_error (ctx -> ins , "target_port : %d" , ctx -> target_port );
383+
384+ /* compose peername by using target_host and port */
385+ size_t host_len = strlen (ctx -> target_host );
386+ int port_len = snprintf (NULL , 0 , "%d" , ctx -> target_port );
387+ size_t static_len = strlen ("udp::" ); // "udp:" + ":"
388+ size_t need = static_len + host_len + port_len + 1 ;
389+ char * peer = flb_calloc (1 , need );
390+ if (!peer ) {
391+ flb_plg_error (ctx -> ins , "oom building peername" );
392+ flb_free (ctx );
393+ return -1 ;
394+ }
395+ int written = snprintf (peer , need , "udp:%s:%d" , ctx -> target_host , ctx -> target_port );
396+ if (written < 0 || (size_t )written >= need ) {
397+ flb_plg_error (ctx -> ins , "peername truncated, need %zu bytes" , need );
398+ flb_free (peer );
399+ flb_free (ctx );
400+ return -1 ;
401+ }
402+ flb_plg_error (ctx -> ins , "peer : %s" , peer );
403+
404+ ctx -> session .peername = peer ;
405+ }
382406
383407 if (strcmp (ctx -> version , "1" ) == 0 ) {
384408 ctx -> session .version = SNMP_VERSION_1 ;
@@ -434,8 +458,8 @@ static struct flb_config_map config_map[] = {
434458 "set the target host IP to collect metrics through SNMP."
435459 },
436460 {
437- FLB_CONFIG_MAP_INT , "port " , "161" ,
438- 0 , FLB_TRUE , offsetof(struct flb_snmp , port ),
461+ FLB_CONFIG_MAP_INT , "target_port " , "161" ,
462+ 0 , FLB_TRUE , offsetof(struct flb_snmp , target_port ),
439463 "set the host port to collect metrics through SNMP."
440464 },
441465 {
0 commit comments