Skip to content

Commit

Permalink
Add: timeout and retris setting for net snmp lib
Browse files Browse the repository at this point in the history
Signed-off-by: k402xxxcenxxx <[email protected]>
  • Loading branch information
k402xxxcenxxx committed Dec 15, 2024
1 parent ae52940 commit 5602f0d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
52 changes: 24 additions & 28 deletions plugins/in_snmp/in_snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
int ret = 0;
struct flb_snmp *ctx = in_context;

netsnmp_session session, *ss;
netsnmp_session *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;

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

char *err;


init_snmp(PLUGIN_NAME);

init_snmp(PLUGIN_NAME);
snmp_sess_init(&session);

session.peername = ctx->target_host;

if (strcmp(ctx->version, "1") == 0) {
session.version = SNMP_VERSION_1;
} else if (strcmp(ctx->version, "2c") == 0) {
session.version = SNMP_VERSION_2c;
} else {
flb_plg_error(ctx->ins, "Unsupported SNMP version : %s", ctx->version);
return -1;
}

if (strcmp(ctx->oid_type, "get") == 0) {
is_walk = false;
} else if (strcmp(ctx->oid_type, "walk") == 0) {
Expand All @@ -158,10 +141,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
return -1;
}

session.community = (u_char *) ctx->community;
session.community_len = strlen(ctx->community);

ss = snmp_open(&session);
ss = snmp_open(&ctx->session);
if (!ss) {
snmp_error(ss, NULL, NULL, &err);
flb_plg_error(ctx->ins, "%s", err);
Expand Down Expand Up @@ -275,7 +255,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
flb_plg_error(ctx->ins, "Error in packet. Reason: %s", snmp_errstring(response->errstat));
running = 0;
} else if (status == STAT_TIMEOUT) {
flb_plg_error(ctx->ins, "Timeout: No response from %s", session.peername);
flb_plg_error(ctx->ins, "Timeout: No response from %s", ctx->session.peername);
running = 0;
} else {
snmp_error(ss, NULL, NULL, &err);
Expand Down Expand Up @@ -367,10 +347,31 @@ static int in_snmp_init(struct flb_input_instance *in,

netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS, 1);
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 1);
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD, 1);
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_SAVE, 1);
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_TIMEOUT, ctx->timeout);
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RETRIES, ctx->retries);

SOCK_STARTUP;
init_snmp(PLUGIN_NAME);

snmp_sess_init(&ctx->session);

ctx->session.peername = ctx->target_host;

if (strcmp(ctx->version, "1") == 0) {
ctx->session.version = SNMP_VERSION_1;
} else if (strcmp(ctx->version, "2c") == 0) {
ctx->session.version = SNMP_VERSION_2c;
} else {
flb_plg_error(ctx->ins, "Unsupported SNMP version : %s", ctx->version);
flb_free(ctx);
return -1;
}

ctx->session.community = (u_char *) ctx->community;
ctx->session.community_len = strlen(ctx->community);

return 0;
}

Expand Down Expand Up @@ -448,11 +449,6 @@ static struct flb_config_map config_map[] = {
0, FLB_TRUE, offsetof(struct flb_snmp, oid),
"set the OID of the SNMP request."
},
{
FLB_CONFIG_MAP_STR, "name", "",
0, FLB_TRUE, offsetof(struct flb_snmp, name),
"set the name for the variable from SNMP request."
},
{0}
};

Expand Down
4 changes: 3 additions & 1 deletion plugins/in_snmp/in_snmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_log_event_encoder.h>
#include <net-snmp/net-snmp-includes.h>

struct flb_snmp {
int coll_fd;
struct flb_input_instance *ins;
struct flb_log_event_encoder log_encoder;

netsnmp_session session;

char *target_host;
int port;
int timeout;
Expand All @@ -18,7 +21,6 @@ struct flb_snmp {
int retries;
char *oid_type;
char *oid;
char *name;
};

#endif

0 comments on commit 5602f0d

Please sign in to comment.