Skip to content

Commit 4ccbd8f

Browse files
committed
snmp: update target port handling and improve peername construction
Signed-off-by: k402xxxcenxxx <[email protected]>
1 parent 055f31d commit 4ccbd8f

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

plugins/in_snmp/in_snmp.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

plugins/in_snmp/in_snmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct flb_snmp {
1414
netsnmp_session session;
1515

1616
char *target_host;
17-
int port;
17+
int target_port;
1818
int timeout;
1919
char *version;
2020
char *community;

0 commit comments

Comments
 (0)