Skip to content

Commit 3a2207a

Browse files
committed
Add: timeout and retris setting for net snmp lib
1 parent ec7a8d3 commit 3a2207a

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

plugins/in_snmp/in_snmp.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
114114
int ret = 0;
115115
struct flb_snmp *ctx = in_context;
116116

117-
netsnmp_session session, *ss;
117+
netsnmp_session *ss;
118118
netsnmp_pdu *pdu;
119119
netsnmp_pdu *response;
120120

@@ -131,24 +131,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
131131
bool is_walk = false;
132132

133133
char *err;
134-
135-
136-
init_snmp(PLUGIN_NAME);
137134

138-
init_snmp(PLUGIN_NAME);
139-
snmp_sess_init(&session);
140-
141-
session.peername = ctx->target_host;
142-
143-
if (strcmp(ctx->version, "1") == 0) {
144-
session.version = SNMP_VERSION_1;
145-
} else if (strcmp(ctx->version, "2c") == 0) {
146-
session.version = SNMP_VERSION_2c;
147-
} else {
148-
flb_plg_error(ctx->ins, "Unsupported SNMP version : %s", ctx->version);
149-
return -1;
150-
}
151-
152135
if (strcmp(ctx->oid_type, "get") == 0) {
153136
is_walk = false;
154137
} else if (strcmp(ctx->oid_type, "walk") == 0) {
@@ -158,10 +141,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
158141
return -1;
159142
}
160143

161-
session.community = (u_char *) ctx->community;
162-
session.community_len = strlen(ctx->community);
163-
164-
ss = snmp_open(&session);
144+
ss = snmp_open(&ctx->session);
165145
if (!ss) {
166146
snmp_error(ss, NULL, NULL, &err);
167147
flb_plg_error(ctx->ins, "%s", err);
@@ -275,7 +255,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
275255
flb_plg_error(ctx->ins, "Error in packet. Reason: %s", snmp_errstring(response->errstat));
276256
running = 0;
277257
} else if (status == STAT_TIMEOUT) {
278-
flb_plg_error(ctx->ins, "Timeout: No response from %s", session.peername);
258+
flb_plg_error(ctx->ins, "Timeout: No response from %s", ctx->session.peername);
279259
running = 0;
280260
} else {
281261
snmp_error(ss, NULL, NULL, &err);
@@ -367,10 +347,31 @@ static int in_snmp_init(struct flb_input_instance *in,
367347

368348
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS, 1);
369349
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 1);
350+
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD, 1);
351+
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_SAVE, 1);
352+
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_TIMEOUT, ctx->timeout);
353+
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RETRIES, ctx->retries);
370354

371355
SOCK_STARTUP;
372356
init_snmp(PLUGIN_NAME);
373357

358+
snmp_sess_init(&ctx->session);
359+
360+
ctx->session.peername = ctx->target_host;
361+
362+
if (strcmp(ctx->version, "1") == 0) {
363+
ctx->session.version = SNMP_VERSION_1;
364+
} else if (strcmp(ctx->version, "2c") == 0) {
365+
ctx->session.version = SNMP_VERSION_2c;
366+
} else {
367+
flb_plg_error(ctx->ins, "Unsupported SNMP version : %s", ctx->version);
368+
flb_free(ctx);
369+
return -1;
370+
}
371+
372+
ctx->session.community = (u_char *) ctx->community;
373+
ctx->session.community_len = strlen(ctx->community);
374+
374375
return 0;
375376
}
376377

@@ -448,11 +449,6 @@ static struct flb_config_map config_map[] = {
448449
0, FLB_TRUE, offsetof(struct flb_snmp, oid),
449450
"set the OID of the SNMP request."
450451
},
451-
{
452-
FLB_CONFIG_MAP_STR, "name", "",
453-
0, FLB_TRUE, offsetof(struct flb_snmp, name),
454-
"set the name for the variable from SNMP request."
455-
},
456452
{0}
457453
};
458454

plugins/in_snmp/in_snmp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
#include <fluent-bit/flb_info.h>
55
#include <fluent-bit/flb_input.h>
66
#include <fluent-bit/flb_log_event_encoder.h>
7+
#include <net-snmp/net-snmp-includes.h>
78

89
struct flb_snmp {
910
int coll_fd;
1011
struct flb_input_instance *ins;
1112
struct flb_log_event_encoder log_encoder;
1213

14+
netsnmp_session session;
15+
1316
char *target_host;
1417
int port;
1518
int timeout;
@@ -18,7 +21,6 @@ struct flb_snmp {
1821
int retries;
1922
char *oid_type;
2023
char *oid;
21-
char *name;
2224
};
2325

2426
#endif

0 commit comments

Comments
 (0)