From cf8b9ffd9ab42ac25b12cbb0cbf253dfe8775ed8 Mon Sep 17 00:00:00 2001 From: k402xxxcenxxx Date: Mon, 13 Mar 2023 15:12:04 +0000 Subject: [PATCH 1/6] snmp: borrow dummy plugin as a start point Add: ChatGPT in_snmp Add: snmp get with fix request Add: snmp with log recorder Add: print value by sprint_realloc_by_type Fix: refine code. Add: walk get values TMP Signed-off-by: k402xxxcenxxx --- CMakeLists.txt | 1 + cmake/FindNetsnmp.cmake | 73 ++++++ plugins/CMakeLists.txt | 1 + plugins/in_snmp/CMakeLists.txt | 8 + plugins/in_snmp/in_snmp.c | 443 +++++++++++++++++++++++++++++++++ plugins/in_snmp/in_snmp.h | 24 ++ tests/runtime/CMakeLists.txt | 1 + tests/runtime/in_snmp.c | 39 +++ 8 files changed, 590 insertions(+) create mode 100644 cmake/FindNetsnmp.cmake create mode 100644 plugins/in_snmp/CMakeLists.txt create mode 100644 plugins/in_snmp/in_snmp.c create mode 100644 plugins/in_snmp/in_snmp.h create mode 100644 tests/runtime/in_snmp.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e6340bbfc9..6af09fc6ab2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,6 +97,7 @@ include(GNUInstallDirs) include(ExternalProject) include(cmake/FindJournald.cmake) include(cmake/FindMonkey.cmake) +include(cmake/FindNetsnmp.cmake) include(cmake/macros.cmake) include(cmake/platform_feature_checks.cmake) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH}) diff --git a/cmake/FindNetsnmp.cmake b/cmake/FindNetsnmp.cmake new file mode 100644 index 00000000000..2cbcbb26cc4 --- /dev/null +++ b/cmake/FindNetsnmp.cmake @@ -0,0 +1,73 @@ +# - Find Net-SNMP +# +# -*- cmake -*- +# +# Find the Net-SNMP module +# +# NETSNMP_INCLUDE_DIR - where to find Net-SNMP.h, etc. +# NETSNMP_LIBRARIES - List of libraries when using Net-SNMP. +# NETSNMP_FOUND - True if Net-SNMP found. + +IF (NETSNMP_INCLUDE_DIR) + # Already in cache, be silent + SET(NETSNMP_FIND_QUIETLY TRUE) +ENDIF (NETSNMP_INCLUDE_DIR) + +FIND_PATH(NETSNMP_INCLUDE_DIR snmp.h + /usr/include/net-snmp/library +) + +SET(NETSNMP_NAMES netsnmp) +FIND_LIBRARY(NETSNMP_LIBRARY + NAMES ${NETSNMP_NAMES} + PATHS /usr/lib /usr/local/lib +) + +SET(NETSNMPAGENT_NAMES netsnmpagent) +FIND_LIBRARY(NETSNMPAGENT_LIBRARY + NAMES ${NETSNMPAGENT_NAMES} + PATHS /usr/lib /usr/local/lib +) + +SET(NETSNMPHELPERS_NAMES netsnmphelpers) +FIND_LIBRARY(NETSNMPHELPERS_LIBRARY + NAMES ${NETSNMPHELPERS_NAMES} + PATHS /usr/lib /usr/local/lib +) + +SET(NETSNMPMIBS_NAMES netsnmpmibs) +FIND_LIBRARY(NETSNMPMIBS_LIBRARY + NAMES ${NETSNMPMIBS_NAMES} + PATHS /usr/lib /usr/local/lib +) + +SET(NETSNMPTRAPD_NAMES netsnmptrapd) +FIND_LIBRARY(NETSNMPTRAPD_LIBRARY + NAMES ${NETSNMPTRAPD_NAMES} + PATHS /usr/lib /usr/local/lib +) + +SET(NETSNMP_LIBRARIES + ${NETSNMP_LIBRARY} + ${NETSNMPAGENT_LIBRARY} + ${NETSNMPHELPERS_LIBRARY} + ${NETSNMPMIBS_LIBRARY} +# ${NETSNMPTRAPD_LIBRARY} +) + + +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(NETSNMP + DEFAULT_MSG + NETSNMP_INCLUDE_DIR + NETSNMP_LIBRARIES +) + +MARK_AS_ADVANCED( + NETSNMP_LIBRARY + NETSNMPAGENT_LIBRARY + NETSNMPHELPERS_LIBRARY + NETSNMPMIBS_LIBRARY + NETSNMPTRAPD_LIBRARY + NETSNMP_INCLUDE_DIR + ) \ No newline at end of file diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index deb24958038..4c643736b9a 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -223,6 +223,7 @@ REGISTER_IN_PLUGIN("in_prometheus_scrape") REGISTER_IN_PLUGIN("in_emitter") REGISTER_IN_PLUGIN("in_tail") REGISTER_IN_PLUGIN("in_dummy") +REGISTER_IN_PLUGIN("in_snmp") REGISTER_IN_PLUGIN("in_head") REGISTER_IN_PLUGIN("in_health") REGISTER_IN_PLUGIN("in_http") diff --git a/plugins/in_snmp/CMakeLists.txt b/plugins/in_snmp/CMakeLists.txt new file mode 100644 index 00000000000..3ea9115b809 --- /dev/null +++ b/plugins/in_snmp/CMakeLists.txt @@ -0,0 +1,8 @@ +set(NETSNMP_LIBRARIES ${NETSNMP_LIBRARY}) +set(NETSNMP_INCLUDE_DIRS ${NETSNMP_INCLUDE_DIR}) + +set(src + in_snmp.c) + +FLB_PLUGIN(in_snmp "${src}" "") +target_link_libraries(flb-plugin-in_snmp ${NETSNMP_LIBRARIES}) \ No newline at end of file diff --git a/plugins/in_snmp/in_snmp.c b/plugins/in_snmp/in_snmp.c new file mode 100644 index 00000000000..3eb810bf0ad --- /dev/null +++ b/plugins/in_snmp/in_snmp.c @@ -0,0 +1,443 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "in_snmp.h" + +#define PLUGIN_NAME "snmp" + +int snmp_plugin_under_test() +{ + if (getenv("FLB_SNMP_PLUGIN_UNDER_TEST") != NULL) { + return FLB_TRUE; + } + + return FLB_FALSE; +} + +int mock_snmp_synch_response(netsnmp_session *ss, netsnmp_pdu *pdu, netsnmp_pdu **response) +{ + char *mock_status = NULL; + netsnmp_pdu *mock_response = NULL; + netsnmp_variable_list *mock_netsnmp_variable_list = NULL; + oid mock_objid[MAX_OID_LEN]; + size_t mock_objid_len; + + mock_status = getenv("TEST_SNMP_RESPONSE"); + + if (strcmp(mock_status, "normal") == 0) { + // if ((mock_response = flb_calloc(1, sizeof(netsnmp_pdu))) == NULL) { + // flb_errno(); + // flb_free(mock_status); + // return NULL; + // } + + // if ((mock_netsnmp_variable_list = flb_calloc(1, sizeof(netsnmp_variable_list))) == NULL) { + // flb_errno(); + // flb_free(mock_response); + // flb_free(mock_status); + // return NULL; + // } + + // // sysUpTime + // mock_objid_len = MAX_OID_LEN; + // if (snmp_parse_oid("1.3.6.1.2.1.1.3.0", mock_objid, &mock_objid_len) == NULL) { + // flb_errno(); + // flb_free(mock_netsnmp_variable_list); + // flb_free(mock_response); + // flb_free(mock_status); + // return NULL; + // } + + // mock_netsnmp_variable_list->next_variable = NULL; + // mock_netsnmp_variable_list->name = NULL; + // mock_netsnmp_variable_list->val.string = NULL; + // mock_netsnmp_variable_list->val_len = 0; + // mock_netsnmp_variable_list->data = NULL; + // mock_netsnmp_variable_list->dataFreeHook = NULL; + // mock_netsnmp_variable_list->index = 0; + + // if (snmp_set_var_objid(mock_netsnmp_variable_list, mock_objid, mock_objid_len)) { + // flb_errno(); + // flb_free(mock_netsnmp_variable_list); + // flb_free(mock_response); + // flb_free(mock_status); + // return NULL; + // } + + // if (snmp_set_var_typed_integer(mock_netsnmp_variable_list, ASN_TIMETICKS, 123)) { + // flb_errno(); + // flb_free(mock_netsnmp_variable_list); + // flb_free(mock_response); + // flb_free(mock_status); + // return NULL; + // } + + // mock_response->variables = mock_netsnmp_variable_list; + // mock_response->errstat == SNMP_ERR_NOERROR; + // *response = mock_response; + + // flb_free(mock_status); + + return STAT_SUCCESS; + } +} + +static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *config, void *in_context) +{ + int ret = 0; + struct flb_snmp *ctx = in_context; + + netsnmp_session session, *ss; + netsnmp_pdu *pdu; + netsnmp_pdu *response; + + oid anOID[MAX_OID_LEN], name[MAX_OID_LEN], end_oid[MAX_OID_LEN]; + size_t anOID_len, name_len, end_len; + + netsnmp_variable_list *vars; + int status; + + u_char *buf = NULL, *oid_buf = NULL; + size_t buf_len = 256, oid_buf_len = 256, out_len = 0, oid_out_len = 0; + int running = 1; + int buf_overflow = 0; + bool is_walk = false; + + char *err; + + init_snmp(PLUGIN_NAME); + snmp_sess_init(&session); + + session.peername = strdup(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) { + is_walk = true; + } else { + flb_plg_error(ctx->ins, "Unsupported oid_type : %s", ctx->oid_type); + return -1; + } + + session.community = (u_char *) strdup(ctx->community); + session.community_len = strlen(ctx->community); + + SOCK_STARTUP; + ss = snmp_open(&session); + if (!ss) { + snmp_error(ss, NULL, NULL, &err); + flb_plg_error(ctx->ins, "%s", err); + SOCK_CLEANUP; + return -1; + } + + anOID_len = MAX_OID_LEN; + if (snmp_parse_oid(strdup(ctx->oid), anOID, &anOID_len) == NULL) { + flb_plg_error(ctx->ins, "Fail to parse oid"); + SOCK_CLEANUP; + return -1; + } + + if (is_walk) { + memmove(end_oid, anOID, anOID_len*sizeof(oid)); + end_len = anOID_len; + end_oid[end_len-1]++; + } + + memmove(name, anOID, anOID_len*sizeof(oid)); + name_len = anOID_len; + + ret = flb_log_event_encoder_begin_record(&ctx->log_encoder); + + if (ret == FLB_EVENT_ENCODER_SUCCESS) { + ret = flb_log_event_encoder_set_current_timestamp( + &ctx->log_encoder); + } + + while (running) { + if (is_walk) { + pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); + } else { + pdu = snmp_pdu_create(SNMP_MSG_GET); + } + + snmp_add_null_var(pdu, name, name_len); + + if (snmp_plugin_under_test() == FLB_TRUE) { + flb_plg_error(ctx->ins, "Ha Ha"); + + status = mock_snmp_synch_response(ss, pdu, &response); + } else { + status = snmp_synch_response(ss, pdu, &response); + } + + if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { + + for(vars = response->variables; vars; vars = vars->next_variable) { + if (is_walk && snmp_oid_compare(end_oid, end_len, + vars->name, vars->name_length) <= 0) { + // not part of this subtree + running = 0; + continue; + } + + oid_out_len = 0; + out_len = 0; + + if ((oid_buf = flb_calloc(oid_buf_len, 1)) == NULL) { + flb_plg_error(ctx->ins, "[TRUNCATED]"); + return -1; + } else { + netsnmp_sprint_realloc_objid_tree(&oid_buf, &oid_buf_len, &oid_out_len, + 1, &buf_overflow, + vars->name, vars->name_length); + if (buf_overflow) { + flb_plg_error(ctx->ins, "[TRUNCATED]"); + return -1; + } + } + + if ((buf = flb_calloc(buf_len, 1)) == NULL) { + flb_plg_error(ctx->ins, "[TRUNCATED]"); + return -1; + } else { + if (sprint_realloc_by_type(&buf, &buf_len, &out_len, + 1, vars, NULL, NULL, + NULL)) { + // append values + if (ret == FLB_EVENT_ENCODER_SUCCESS) { + ret = flb_log_event_encoder_append_body_values( + &ctx->log_encoder, + FLB_LOG_EVENT_CSTRING_VALUE((char *) oid_buf), + FLB_LOG_EVENT_CSTRING_VALUE((char *) buf)); + } + } else { + flb_plg_error(ctx->ins, "[TRUNCATED]"); + return -1; + } + } + + if ((vars->type != SNMP_ENDOFMIBVIEW) && + (vars->type != SNMP_NOSUCHOBJECT) && + (vars->type != SNMP_NOSUCHINSTANCE)) { + if (snmp_oid_compare(name, name_len, + vars->name, + vars->name_length) >= 0 && is_walk) { + flb_plg_error(ctx->ins, "Error: OID not increasing"); + running = 0; + } + memmove(name, vars->name, + vars->name_length * sizeof(oid)); + name_len = vars->name_length; + } else { + flb_plg_error(ctx->ins, "an exception value"); + running = 0; + } + } + } else { + if (status == STAT_SUCCESS) { + 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); + running = 0; + } else { + snmp_error(ss, NULL, NULL, &err); + flb_plg_error(ctx->ins, "%s", err); + running = 0; + } + } + if (response) { + snmp_free_pdu(response); + } + } + + if (ret == FLB_EVENT_ENCODER_SUCCESS) { + ret = flb_log_event_encoder_commit_record(&ctx->log_encoder); + } + + if (ret == FLB_EVENT_ENCODER_SUCCESS) { + flb_input_log_append(ins, NULL, 0, + ctx->log_encoder.output_buffer, + ctx->log_encoder.output_length); + + ret = 0; + } else { + flb_plg_error(ctx->ins, "Error encoding record : %d", ret); + ret = -1; + } + + flb_log_event_encoder_reset(&ctx->log_encoder); + + snmp_close(ss); + + flb_free(buf); + flb_free(oid_buf); + + SOCK_CLEANUP; + + return ret; +} + +/* Initialize plugin */ +static int in_snmp_init(struct flb_input_instance *in, + struct flb_config *config, void *data) +{ + int ret = -1; + struct flb_snmp *ctx = NULL; + struct timespec tm; + + /* Allocate space for the configuration */ + ctx = flb_calloc(1, sizeof(struct flb_snmp)); + if (!ctx) { + flb_errno(); + return -1; + } + ctx->ins = in; + + ret = flb_input_config_map_set(in, (void *)ctx); + if (ret == -1) { + flb_free(ctx); + return -1; + } + + // Set plugin context + flb_input_set_context(in, ctx); + + /* interval settings */ + tm.tv_sec = 1; + tm.tv_nsec = 0; + + ret = flb_input_set_collector_time(in, + in_snmp_collect, + tm.tv_sec, + tm.tv_nsec, config); + if (ret < 0) { + flb_plg_error(ctx->ins, "could not set collector for snmp input plugin"); + return -1; + } + + ctx->coll_fd = ret; + + ret = flb_log_event_encoder_init(&ctx->log_encoder, + FLB_LOG_EVENT_FORMAT_DEFAULT); + + if (ret != FLB_EVENT_ENCODER_SUCCESS) { + flb_plg_error(ctx->ins, "error initializing event encoder : %d", ret); + + /* done */ + flb_free(ctx); + + return -1; + } + + 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); + + return 0; +} + +static void in_snmp_pause(void *data, struct flb_config *config) +{ + struct flb_snmp *ctx = data; + + flb_input_collector_pause(ctx->coll_fd, ctx->ins); +} + +static void in_snmp_resume(void *data, struct flb_config *config) +{ + struct flb_snmp *ctx = data; + + flb_input_collector_resume(ctx->coll_fd, ctx->ins); +} + +static int in_snmp_exit(void *data, struct flb_config *config) +{ + (void) *config; + struct flb_snmp *ctx = data; + + flb_log_event_encoder_destroy(&ctx->log_encoder); + + flb_free(ctx); + + return 0; +} + +/* Configuration properties map */ +static struct flb_config_map config_map[] = { + { + FLB_CONFIG_MAP_STR, "target_host", "127.0.0.1", + 0, FLB_TRUE, offsetof(struct flb_snmp, target_host), + "set the target host IP to collect metrics through SNMP." + }, + { + FLB_CONFIG_MAP_INT, "port", "161", + 0, FLB_TRUE, offsetof(struct flb_snmp, port), + "set the host port to collect metrics through SNMP." + }, + { + FLB_CONFIG_MAP_INT, "timeout", "5", + 0, FLB_TRUE, offsetof(struct flb_snmp, timeout), + "set the timeout to when doing a SNMP request." + }, + { + FLB_CONFIG_MAP_STR, "version", "2c", + 0, FLB_TRUE, offsetof(struct flb_snmp, version), + "set the version of the SNMP request." + }, + { + FLB_CONFIG_MAP_STR, "community", "public", + 0, FLB_TRUE, offsetof(struct flb_snmp, community), + "set the community of the SNMP request." + }, + { + FLB_CONFIG_MAP_INT, "retries", "3", + 0, FLB_TRUE, offsetof(struct flb_snmp, retries), + "set the retry times to do SNMP request when fail." + }, + { + FLB_CONFIG_MAP_STR, "oid_type", "get", + 0, FLB_TRUE, offsetof(struct flb_snmp, oid_type), + "set the type of the SNMP request. Use a 'field' to collect a variable by OID." + }, + { + FLB_CONFIG_MAP_STR, "oid", "1.3.6.1.2.1.1.3.0", + 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} +}; + +struct flb_input_plugin in_snmp_plugin = { + .name = "snmp", + .description = "Collect metrics through SNMP", + .cb_init = in_snmp_init, + .cb_pre_run = NULL, + .cb_collect = in_snmp_collect, + .cb_flush_buf = NULL, + .config_map = config_map, + .cb_pause = in_snmp_pause, + .cb_resume = in_snmp_resume, + .cb_exit = in_snmp_exit +}; diff --git a/plugins/in_snmp/in_snmp.h b/plugins/in_snmp/in_snmp.h new file mode 100644 index 00000000000..179f4d98a06 --- /dev/null +++ b/plugins/in_snmp/in_snmp.h @@ -0,0 +1,24 @@ +#ifndef FLB_IN_SNMP_H +#define FLB_IN_SNMP_H + +#include +#include +#include + +struct flb_snmp { + int coll_fd; + struct flb_input_instance *ins; + struct flb_log_event_encoder log_encoder; + + char *target_host; + int port; + int timeout; + char *version; + char *community; + int retries; + char *oid_type; + char *oid; + char *name; +}; + +#endif \ No newline at end of file diff --git a/tests/runtime/CMakeLists.txt b/tests/runtime/CMakeLists.txt index 7c5143cbfca..02a1d241622 100644 --- a/tests/runtime/CMakeLists.txt +++ b/tests/runtime/CMakeLists.txt @@ -44,6 +44,7 @@ if(FLB_OUT_LIB) endif() FLB_RT_TEST(FLB_IN_HEAD "in_head.c") FLB_RT_TEST(FLB_IN_DUMMY "in_dummy.c") + FLB_RT_TEST(FLB_IN_SNMP "in_snmp.c") FLB_RT_TEST(FLB_IN_HTTP "in_http.c") FLB_RT_TEST(FLB_IN_ELASTICSEARCH "in_elasticsearch.c") FLB_RT_TEST(FLB_IN_MQTT "in_mqtt.c") diff --git a/tests/runtime/in_snmp.c b/tests/runtime/in_snmp.c new file mode 100644 index 00000000000..0af6e22a287 --- /dev/null +++ b/tests/runtime/in_snmp.c @@ -0,0 +1,39 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +#include +#include "flb_tests_runtime.h" + +void flb_test_snmp() +{ + int ret; + flb_ctx_t *ctx; + int in_ffd; + int out_ffd; + + /* mocks calls- signals that we are in test mode */ + setenv("FLB_SNMP_PLUGIN_UNDER_TEST", "true", 1); + setenv("TEST_SNMP_RESPONSE", "normal", 1); + + ctx = flb_create(); + + in_ffd = flb_input(ctx, (char *) "snmp", NULL); + TEST_CHECK(in_ffd >= 0); + + out_ffd = flb_output(ctx, (char *) "null", NULL); + TEST_CHECK(out_ffd >= 0); + flb_output_set(ctx, out_ffd, "match", "test", NULL); + + ret = flb_start(ctx); + TEST_CHECK(ret == 0); + + sleep(8); + + flb_stop(ctx); + flb_destroy(ctx); +} + +/* Test list */ +TEST_LIST = { + {"snmp", flb_test_snmp}, + {NULL, NULL} +}; \ No newline at end of file From 014716acd7b20283e825a538f65237e24f9f5bfa Mon Sep 17 00:00:00 2001 From: k402xxxcenxxx Date: Mon, 19 Jun 2023 15:13:11 +0000 Subject: [PATCH 2/6] Add: unit test Add: unit test for snmp_walk Signed-off-by: k402xxxcenxxx --- plugins/in_snmp/in_snmp.c | 131 +++++++++++++++++------------- tests/runtime/in_snmp.c | 165 +++++++++++++++++++++++++++++++++++--- 2 files changed, 228 insertions(+), 68 deletions(-) diff --git a/plugins/in_snmp/in_snmp.c b/plugins/in_snmp/in_snmp.c index 3eb810bf0ad..c1ca64f3edb 100644 --- a/plugins/in_snmp/in_snmp.c +++ b/plugins/in_snmp/in_snmp.c @@ -30,63 +30,82 @@ int mock_snmp_synch_response(netsnmp_session *ss, netsnmp_pdu *pdu, netsnmp_pdu size_t mock_objid_len; mock_status = getenv("TEST_SNMP_RESPONSE"); + if ((mock_response = flb_calloc(1, sizeof(netsnmp_pdu))) == NULL) { + goto error; + } + + if (strcmp(mock_status, "snmp_get") == 0) { + if ((mock_netsnmp_variable_list = flb_calloc(1, sizeof(netsnmp_variable_list))) == NULL) { + goto error; + } + // sysUpTime + mock_objid_len = MAX_OID_LEN; + if (snmp_parse_oid("1.3.6.1.2.1.1.3.0", mock_objid, &mock_objid_len) == NULL) { + goto error; + } + + if (snmp_set_var_objid(mock_netsnmp_variable_list, mock_objid, mock_objid_len)) { + goto error; + } + + if (snmp_set_var_typed_integer(mock_netsnmp_variable_list, ASN_TIMETICKS, 123)) { + goto error; + } + + mock_response->variables = mock_netsnmp_variable_list; + mock_response->errstat = SNMP_ERR_NOERROR; + *response = mock_response; + + return STAT_SUCCESS; + } else if (strcmp(mock_status, "snmp_walk") == 0) { + if ((mock_netsnmp_variable_list = flb_calloc(1, sizeof(netsnmp_variable_list))) == NULL) { + goto error; + } + // ifName.1 + mock_objid_len = MAX_OID_LEN; + if (snmp_parse_oid("1.3.6.1.2.1.31.1.1.1.1.1", mock_objid, &mock_objid_len) == NULL) { + goto error; + } + + if (snmp_set_var_objid(mock_netsnmp_variable_list, mock_objid, mock_objid_len)) { + goto error; + } + + if (snmp_set_var_typed_value(mock_netsnmp_variable_list, ASN_OCTET_STR, "Fa0/0", strlen("Fa0/0"))) { + goto error; + } + + if ((mock_netsnmp_variable_list->next_variable = flb_calloc(1, sizeof(netsnmp_variable_list))) == NULL) { + goto error; + } + // ifName.2 + mock_objid_len = MAX_OID_LEN; + if (snmp_parse_oid("1.3.6.1.2.1.31.1.1.1.1.2", mock_objid, &mock_objid_len) == NULL) { + goto error; + } + + if (snmp_set_var_objid(mock_netsnmp_variable_list->next_variable, mock_objid, mock_objid_len)) { + goto error; + } + + if (snmp_set_var_typed_value(mock_netsnmp_variable_list->next_variable, ASN_OCTET_STR, "Fa0/1", strlen("Fa0/1"))) { + goto error; + } - if (strcmp(mock_status, "normal") == 0) { - // if ((mock_response = flb_calloc(1, sizeof(netsnmp_pdu))) == NULL) { - // flb_errno(); - // flb_free(mock_status); - // return NULL; - // } - - // if ((mock_netsnmp_variable_list = flb_calloc(1, sizeof(netsnmp_variable_list))) == NULL) { - // flb_errno(); - // flb_free(mock_response); - // flb_free(mock_status); - // return NULL; - // } - - // // sysUpTime - // mock_objid_len = MAX_OID_LEN; - // if (snmp_parse_oid("1.3.6.1.2.1.1.3.0", mock_objid, &mock_objid_len) == NULL) { - // flb_errno(); - // flb_free(mock_netsnmp_variable_list); - // flb_free(mock_response); - // flb_free(mock_status); - // return NULL; - // } - - // mock_netsnmp_variable_list->next_variable = NULL; - // mock_netsnmp_variable_list->name = NULL; - // mock_netsnmp_variable_list->val.string = NULL; - // mock_netsnmp_variable_list->val_len = 0; - // mock_netsnmp_variable_list->data = NULL; - // mock_netsnmp_variable_list->dataFreeHook = NULL; - // mock_netsnmp_variable_list->index = 0; - - // if (snmp_set_var_objid(mock_netsnmp_variable_list, mock_objid, mock_objid_len)) { - // flb_errno(); - // flb_free(mock_netsnmp_variable_list); - // flb_free(mock_response); - // flb_free(mock_status); - // return NULL; - // } - - // if (snmp_set_var_typed_integer(mock_netsnmp_variable_list, ASN_TIMETICKS, 123)) { - // flb_errno(); - // flb_free(mock_netsnmp_variable_list); - // flb_free(mock_response); - // flb_free(mock_status); - // return NULL; - // } - - // mock_response->variables = mock_netsnmp_variable_list; - // mock_response->errstat == SNMP_ERR_NOERROR; - // *response = mock_response; - - // flb_free(mock_status); + mock_response->variables = mock_netsnmp_variable_list; + mock_response->errstat = SNMP_ERR_NOERROR; + *response = mock_response; return STAT_SUCCESS; + } else { + return STAT_ERROR; } + +error: + flb_errno(); + if (mock_netsnmp_variable_list) flb_free(mock_netsnmp_variable_list); + if (mock_response) flb_free(mock_response); + return STAT_ERROR; } static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *config, void *in_context) @@ -175,20 +194,18 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); } else { pdu = snmp_pdu_create(SNMP_MSG_GET); + running = 0; } snmp_add_null_var(pdu, name, name_len); if (snmp_plugin_under_test() == FLB_TRUE) { - flb_plg_error(ctx->ins, "Ha Ha"); - status = mock_snmp_synch_response(ss, pdu, &response); } else { status = snmp_synch_response(ss, pdu, &response); } if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { - for(vars = response->variables; vars; vars = vars->next_variable) { if (is_walk && snmp_oid_compare(end_oid, end_len, vars->name, vars->name_length) <= 0) { @@ -206,7 +223,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co } else { netsnmp_sprint_realloc_objid_tree(&oid_buf, &oid_buf_len, &oid_out_len, 1, &buf_overflow, - vars->name, vars->name_length); + vars->name, vars->name_length); if (buf_overflow) { flb_plg_error(ctx->ins, "[TRUNCATED]"); return -1; diff --git a/tests/runtime/in_snmp.c b/tests/runtime/in_snmp.c index 0af6e22a287..5ea2777d144 100644 --- a/tests/runtime/in_snmp.c +++ b/tests/runtime/in_snmp.c @@ -1,37 +1,180 @@ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ #include +#include #include "flb_tests_runtime.h" -void flb_test_snmp() +struct callback_record { + void *data; + size_t size; +}; + +struct callback_records { + int num_records; + struct callback_record *records; +}; + +int callback_add_record(void* data, size_t size, void* cb_data) +{ + struct callback_records *ctx = (struct callback_records *)cb_data; + + if (size > 0) { + flb_info("[test] flush record"); + if (ctx->records == NULL) { + ctx->records = (struct callback_record *) + flb_calloc(1, sizeof(struct callback_record)); + } else { + ctx->records = (struct callback_record *) + flb_realloc(ctx->records, + (ctx->num_records+1)*sizeof(struct callback_record)); + } + if (ctx->records == NULL) { + return -1; + } + ctx->records[ctx->num_records].size = size; + ctx->records[ctx->num_records].data = data; + ctx->num_records++; + } + return 0; +} + +void flb_test_snmp_records_message_get(struct callback_records *records) { - int ret; - flb_ctx_t *ctx; + int i; + msgpack_unpacked result; + msgpack_object *obj; + size_t off = 0; + struct flb_time ftm; + + TEST_CHECK(records->num_records > 0); + for (i = 0; i < records->num_records; i++) { + msgpack_unpacked_init(&result); + + while (msgpack_unpack_next(&result, records->records[i].data, + records->records[i].size, &off) == MSGPACK_UNPACK_SUCCESS) { + flb_time_pop_from_msgpack(&ftm, &result, &obj); + TEST_CHECK(obj->type == MSGPACK_OBJECT_MAP); + TEST_CHECK(strncmp("iso.3.6.1.2.1.1.3.0", + obj->via.map.ptr[0].key.via.str.ptr, + obj->via.map.ptr[0].key.via.str.size) == 0); + TEST_CHECK(strncmp("123", + obj->via.map.ptr[0].val.via.str.ptr, + obj->via.map.ptr[0].val.via.str.size) == 0); + } + msgpack_unpacked_destroy(&result); + } +} + +void flb_test_snmp_records_message_walk(struct callback_records *records) +{ + int i, j; + msgpack_unpacked result; + msgpack_object *obj; + size_t off = 0; + struct flb_time ftm; + msgpack_object key; + msgpack_object val; + + char expected_keys[2][26] = { + "iso.3.6.1.2.1.31.1.1.1.1.1", + "iso.3.6.1.2.1.31.1.1.1.1.2" + }; + + char expected_vals[2][7] = { + "\"Fa0/0\"", + "\"Fa0/1\"" + }; + + TEST_CHECK(records->num_records > 0); + for (i = 0; i < records->num_records; i++) { + msgpack_unpacked_init(&result); + while (msgpack_unpack_next(&result, records->records[i].data, + records->records[i].size, &off) == MSGPACK_UNPACK_SUCCESS) { + flb_time_pop_from_msgpack(&ftm, &result, &obj); + TEST_CHECK(obj->type == MSGPACK_OBJECT_MAP); + for (j = 0; j < obj->via.map.size; j++) { + key = obj->via.map.ptr[j].key; + val = obj->via.map.ptr[j].val; + + TEST_CHECK(strncmp(expected_keys[j], + key.via.str.ptr, + 26) == 0); + TEST_CHECK(strncmp(expected_vals[j], + val.via.str.ptr, + 7) == 0); + } + } + msgpack_unpacked_destroy(&result); + } +} + + +void do_test_records(char *response, void (*records_cb)(struct callback_records *), ...) +{ + flb_ctx_t *ctx = NULL; int in_ffd; int out_ffd; - + va_list va; + char *key; + char *value; + int i; + struct flb_lib_out_cb cb; + struct callback_records *records; + /* mocks calls- signals that we are in test mode */ setenv("FLB_SNMP_PLUGIN_UNDER_TEST", "true", 1); - setenv("TEST_SNMP_RESPONSE", "normal", 1); + setenv("TEST_SNMP_RESPONSE", response, 1); + + records = flb_calloc(1, sizeof(struct callback_records)); + records->num_records = 0; + records->records = NULL; + cb.cb = callback_add_record; + cb.data = (void *)records; + /* initialize */ ctx = flb_create(); - in_ffd = flb_input(ctx, (char *) "snmp", NULL); + in_ffd = flb_input(ctx, "snmp", NULL); TEST_CHECK(in_ffd >= 0); + TEST_CHECK(flb_input_set(ctx, in_ffd, "tag", "test", NULL) == 0); + + va_start(va, records_cb); + while ((key = va_arg(va, char *))) { + value = va_arg(va, char *); + TEST_CHECK(value != NULL); + TEST_CHECK(flb_input_set(ctx, in_ffd, key, value, NULL) == 0); + } + va_end(va); - out_ffd = flb_output(ctx, (char *) "null", NULL); + out_ffd = flb_output(ctx, (char *) "lib", &cb); TEST_CHECK(out_ffd >= 0); - flb_output_set(ctx, out_ffd, "match", "test", NULL); + TEST_CHECK(flb_output_set(ctx, out_ffd, "match", "test", NULL) == 0); - ret = flb_start(ctx); - TEST_CHECK(ret == 0); + /* Start test */ + TEST_CHECK(flb_start(ctx) == 0); - sleep(8); + /* 4 sec passed. It must have flushed */ + sleep(5); + + records_cb(records); flb_stop(ctx); + + for (i = 0; i < records->num_records; i++) { + flb_lib_free(records->records[i].data); + } + flb_free(records->records); + flb_free(records); + flb_destroy(ctx); } +void flb_test_snmp() +{ + do_test_records("snmp_get", flb_test_snmp_records_message_get, NULL); + do_test_records("snmp_walk", flb_test_snmp_records_message_walk, NULL); +} + /* Test list */ TEST_LIST = { {"snmp", flb_test_snmp}, From ae52940dd7b3d777093c27a4e88dca608f7e080e Mon Sep 17 00:00:00 2001 From: k402xxxcenxxx Date: Tue, 11 Jul 2023 04:04:21 +0000 Subject: [PATCH 3/6] Fix: memory leak due to openssl lib used by net-snmp Signed-off-by: k402xxxcenxxx --- plugins/in_snmp/in_snmp.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/plugins/in_snmp/in_snmp.c b/plugins/in_snmp/in_snmp.c index c1ca64f3edb..bba01a058cb 100644 --- a/plugins/in_snmp/in_snmp.c +++ b/plugins/in_snmp/in_snmp.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -131,10 +132,13 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co char *err; + + init_snmp(PLUGIN_NAME); + init_snmp(PLUGIN_NAME); snmp_sess_init(&session); - session.peername = strdup(ctx->target_host); + session.peername = ctx->target_host; if (strcmp(ctx->version, "1") == 0) { session.version = SNMP_VERSION_1; @@ -154,10 +158,9 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co return -1; } - session.community = (u_char *) strdup(ctx->community); + session.community = (u_char *) ctx->community; session.community_len = strlen(ctx->community); - SOCK_STARTUP; ss = snmp_open(&session); if (!ss) { snmp_error(ss, NULL, NULL, &err); @@ -167,7 +170,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co } anOID_len = MAX_OID_LEN; - if (snmp_parse_oid(strdup(ctx->oid), anOID, &anOID_len) == NULL) { + if (snmp_parse_oid(ctx->oid, anOID, &anOID_len) == NULL) { flb_plg_error(ctx->ins, "Fail to parse oid"); SOCK_CLEANUP; return -1; @@ -304,10 +307,8 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co snmp_close(ss); - flb_free(buf); - flb_free(oid_buf); - - SOCK_CLEANUP; + if (buf) flb_free(buf); + if (oid_buf) flb_free(oid_buf); return ret; } @@ -367,6 +368,9 @@ 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); + SOCK_STARTUP; + init_snmp(PLUGIN_NAME); + return 0; } @@ -392,6 +396,12 @@ static int in_snmp_exit(void *data, struct flb_config *config) flb_log_event_encoder_destroy(&ctx->log_encoder); flb_free(ctx); + snmp_shutdown(PLUGIN_NAME); + + unload_all_mibs(); + SOCK_CLEANUP; + + OPENSSL_cleanup(); return 0; } From 5602f0d442356d57aa9c2cacf7678eba0880ff2e Mon Sep 17 00:00:00 2001 From: k402xxxcenxxx Date: Tue, 11 Jul 2023 06:32:04 +0000 Subject: [PATCH 4/6] Add: timeout and retris setting for net snmp lib Signed-off-by: k402xxxcenxxx --- plugins/in_snmp/in_snmp.c | 52 ++++++++++++++++++--------------------- plugins/in_snmp/in_snmp.h | 4 ++- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/plugins/in_snmp/in_snmp.c b/plugins/in_snmp/in_snmp.c index bba01a058cb..9e23cc482e4 100644 --- a/plugins/in_snmp/in_snmp.c +++ b/plugins/in_snmp/in_snmp.c @@ -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; @@ -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) { @@ -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); @@ -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); @@ -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; } @@ -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} }; diff --git a/plugins/in_snmp/in_snmp.h b/plugins/in_snmp/in_snmp.h index 179f4d98a06..98a92ba6040 100644 --- a/plugins/in_snmp/in_snmp.h +++ b/plugins/in_snmp/in_snmp.h @@ -4,12 +4,15 @@ #include #include #include +#include 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; @@ -18,7 +21,6 @@ struct flb_snmp { int retries; char *oid_type; char *oid; - char *name; }; #endif \ No newline at end of file From 33f35556f377b0a280bc8528fc920ed96beed6f7 Mon Sep 17 00:00:00 2001 From: k402xxxcenxxx Date: Tue, 11 Jul 2023 06:39:34 +0000 Subject: [PATCH 5/6] Add: snmp in Dockerfile Signed-off-by: k402xxxcenxxx --- dockerfiles/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/dockerfiles/Dockerfile b/dockerfiles/Dockerfile index d0fc7fd3845..82eb50e720c 100644 --- a/dockerfiles/Dockerfile +++ b/dockerfiles/Dockerfile @@ -55,6 +55,7 @@ RUN echo "deb http://deb.debian.org/debian bookworm-backports main" >> /etc/apt/ flex \ bison \ libyaml-dev \ + libsnmp-dev \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* From 9a4ed5ab60948d6ae80967f51567bd67036edc93 Mon Sep 17 00:00:00 2001 From: k402xxxcenxxx Date: Fri, 18 Aug 2023 05:58:29 +0000 Subject: [PATCH 6/6] Fix: not support Windows. add libsnmp in various Dockerfile Signed-off-by: k402xxxcenxxx --- cmake/windows-setup.cmake | 1 + dockerfiles/Dockerfile.centos7 | 2 +- packaging/distros/amazonlinux/Dockerfile | 8 ++++---- packaging/distros/centos/Dockerfile | 15 ++++++++------- packaging/distros/debian/Dockerfile | 12 ++++++------ packaging/distros/raspbian/Dockerfile | 4 ++-- packaging/distros/ubuntu/Dockerfile | 14 +++++++------- 7 files changed, 29 insertions(+), 27 deletions(-) diff --git a/cmake/windows-setup.cmake b/cmake/windows-setup.cmake index 60230408b2e..3652873d4ab 100644 --- a/cmake/windows-setup.cmake +++ b/cmake/windows-setup.cmake @@ -57,6 +57,7 @@ if(FLB_WINDOWS_DEFAULTS) set(FLB_IN_ELASTICSEARCH Yes) set(FLB_IN_SPLUNK Yes) set(FLB_IN_PROMETHEUS_REMOTE_WRITE Yes) + set(FLB_IN_SNMP No) # OUTPUT plugins # ============== diff --git a/dockerfiles/Dockerfile.centos7 b/dockerfiles/Dockerfile.centos7 index 9ff9ee88ad9..1e97409bedd 100644 --- a/dockerfiles/Dockerfile.centos7 +++ b/dockerfiles/Dockerfile.centos7 @@ -10,7 +10,7 @@ RUN sed -i -e "s/^mirrorlist=http:\/\/mirrorlist.centos.org/#mirrorlist=http:\/\ yum install -y rpm-build curl ca-certificates gcc gcc-c++ cmake make bash \ wget unzip systemd-devel wget flex bison \ cyrus-sasl-lib cyrus-sasl-devel openssl openss-libs openssl-devel \ - postgresql-libs postgresql-devel postgresql-server postgresql libyaml-devel && \ + postgresql-libs postgresql-devel postgresql-server postgresql libyaml-devel libsnmp-devel && \ yum install -y epel-release && \ yum install -y cmake3 diff --git a/packaging/distros/amazonlinux/Dockerfile b/packaging/distros/amazonlinux/Dockerfile index fa9b17c5bd7..3a8101ebe63 100644 --- a/packaging/distros/amazonlinux/Dockerfile +++ b/packaging/distros/amazonlinux/Dockerfile @@ -18,7 +18,7 @@ RUN yum -y update && \ wget unzip systemd-devel wget flex bison \ cyrus-sasl-lib cyrus-sasl-devel openssl openss-libs openssl-devel \ postgresql-devel postgresql-libs \ - cmake3 libyaml-devel zlib-devel && \ + cmake3 libyaml-devel libsnmp-devel zlib-devel && \ yum clean all # amazonlinux/2.arm64v8 base image @@ -32,7 +32,7 @@ RUN yum -y update && \ wget unzip systemd-devel wget flex bison \ cyrus-sasl-lib cyrus-sasl-devel openssl openss-libs openssl-devel \ postgresql-devel postgresql-libs \ - cmake3 libyaml-devel zlib-devel && \ + cmake3 libyaml-devel libsnmp-devel zlib-devel && \ yum clean all FROM amazonlinux:2023 as amazonlinux-2023-base @@ -43,7 +43,7 @@ RUN yum -y update && \ wget unzip systemd-devel wget flex bison \ cyrus-sasl-lib cyrus-sasl-devel openssl openssl-libs openssl-devel \ postgresql-devel postgresql-libs \ - cmake3 libyaml-devel zlib-devel && \ + cmake3 libyaml-devel libsnmp-devel zlib-devel && \ yum clean all # hadolint ignore=DL3029 @@ -57,7 +57,7 @@ RUN yum -y update && \ wget unzip systemd-devel wget flex bison \ cyrus-sasl-lib cyrus-sasl-devel openssl openssl-libs openssl-devel \ postgresql-devel postgresql-libs \ - cmake3 libyaml-devel zlib-devel && \ + cmake3 libyaml-devel libsnmp-devel zlib-devel && \ yum clean all # Common build for all distributions now diff --git a/packaging/distros/centos/Dockerfile b/packaging/distros/centos/Dockerfile index 8e70b32d478..5080042a0e7 100644 --- a/packaging/distros/centos/Dockerfile +++ b/packaging/distros/centos/Dockerfile @@ -19,7 +19,7 @@ RUN sed -i -e "s/^mirrorlist=http:\/\/mirrorlist.centos.org/#mirrorlist=http:\/\ yum install -y rpm-build curl ca-certificates gcc gcc-c++ cmake make bash \ wget unzip systemd-devel wget flex bison \ cyrus-sasl-lib cyrus-sasl-devel openssl openss-libs openssl-devel \ - postgresql-libs postgresql-devel postgresql-server postgresql libyaml-devel && \ + postgresql-libs postgresql-devel postgresql-server postgresql libyaml-devel libsnmp-devel && \ yum install -y epel-release && \ yum install -y cmake3 && \ yum clean all @@ -39,8 +39,9 @@ RUN sed -i -e "s/^mirrorlist=http:\/\/mirrorlist.centos.org/#mirrorlist=http:\/\ yum install -y rpm-build curl ca-certificates gcc gcc-c++ cmake make bash \ wget unzip systemd-devel wget flex bison \ cyrus-sasl-lib cyrus-sasl-devel openssl openss-libs openssl-devel \ - postgresql-libs postgresql-devel postgresql-server postgresql libyaml-devel && \ - yum install -y epel-release && \ + postgresql-libs postgresql-devel postgresql-server postgresql libyaml-devel libsnmp-devel && \ + wget -q http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && \ + rpm -ivh epel-release-latest-7.noarch.rpm && \ yum install -y cmake3 && \ yum clean all @@ -68,7 +69,7 @@ RUN yum -y update && \ wget unzip systemd-devel wget flex bison \ postgresql-libs postgresql-devel postgresql-server postgresql \ cyrus-sasl-lib cyrus-sasl-devel openssl openssl-libs openssl-devel \ - libyaml-devel zlib-devel && \ + libyaml-devel libsnmp-devel zlib-devel && \ yum clean all ARG FLB_OUT_PGSQL=On @@ -92,7 +93,7 @@ RUN yum -y update && \ wget unzip systemd-devel wget flex bison \ postgresql-libs postgresql-devel postgresql-server postgresql \ cyrus-sasl-lib cyrus-sasl-devel openssl openssl-libs openssl-devel \ - libyaml-devel zlib-devel && \ + libyaml-devel libsnmp-devel zlib-devel && \ yum clean all ARG FLB_OUT_PGSQL=On @@ -111,7 +112,7 @@ RUN dnf -y install 'dnf-command(config-manager)' && dnf -y config-manager --set- wget unzip systemd-devel wget flex bison \ postgresql-libs postgresql-devel postgresql-server postgresql \ cyrus-sasl-lib cyrus-sasl-devel openssl openssl-libs openssl-devel \ - libyaml-devel zlib-devel && \ + libyaml-devel libsnmp-devel zlib-devel && \ dnf clean all ARG FLB_OUT_PGSQL=On @@ -129,7 +130,7 @@ RUN dnf -y install 'dnf-command(config-manager)' && dnf -y config-manager --set- wget unzip systemd-devel wget flex bison \ postgresql-libs postgresql-devel postgresql-server postgresql \ cyrus-sasl-lib cyrus-sasl-devel openssl openssl-libs openssl-devel \ - libyaml-devel zlib-devel && \ + libyaml-devel libsnmp-devel zlib-devel && \ dnf clean all ARG FLB_OUT_PGSQL=On diff --git a/packaging/distros/debian/Dockerfile b/packaging/distros/debian/Dockerfile index d37871501cc..660f765a073 100644 --- a/packaging/distros/debian/Dockerfile +++ b/packaging/distros/debian/Dockerfile @@ -19,7 +19,7 @@ RUN apt-get -qq update && \ cmake make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl1.1 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # debian/buster.arm64v8 base image @@ -34,7 +34,7 @@ RUN apt-get -qq update && \ cmake make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl1.1 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # debian/bullseye base image @@ -47,7 +47,7 @@ RUN apt-get -qq update && \ cmake make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl1.1 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # debian/bullseye.arm64v8 base image @@ -62,7 +62,7 @@ RUN apt-get -qq update && \ cmake make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl1.1 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # debian/bookworm base image @@ -75,7 +75,7 @@ RUN apt-get -qq update && \ cmake make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl3 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # debian/bookworm.arm64v8 base image @@ -90,7 +90,7 @@ RUN apt-get -qq update && \ cmake make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl3 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # Common build for all distributions now diff --git a/packaging/distros/raspbian/Dockerfile b/packaging/distros/raspbian/Dockerfile index 03ca9708772..1d244e56f6c 100644 --- a/packaging/distros/raspbian/Dockerfile +++ b/packaging/distros/raspbian/Dockerfile @@ -18,7 +18,7 @@ RUN apt-get update && \ make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl1.1 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # raspbian/bullseye base image @@ -31,7 +31,7 @@ RUN apt-get update && \ cmake make bash sudo wget unzip dh-make \ libsystemd-dev zlib1g-dev flex bison \ libssl1.1 libssl-dev libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev libyaml-dev pkg-config && \ + libsasl2-2 libsasl2-dev libyaml-dev libsnmp-dev pkg-config && \ apt-get install -y --reinstall lsb-base lsb-release # Common build for all distributions now diff --git a/packaging/distros/ubuntu/Dockerfile b/packaging/distros/ubuntu/Dockerfile index 2ca0704580e..2af8b498101 100644 --- a/packaging/distros/ubuntu/Dockerfile +++ b/packaging/distros/ubuntu/Dockerfile @@ -20,7 +20,7 @@ RUN apt-get update && \ apt-get install -y curl ca-certificates build-essential libsystemd-dev cmake \ make bash wget unzip nano vim valgrind dh-make flex bison \ libpq-dev postgresql-server-dev-all software-properties-common \ - software-properties-common libyaml-dev apt-transport-https \ + software-properties-common libyaml-dev libsnmp-dev apt-transport-https \ pkg-config libsasl2-2 libsasl2-dev openssl libssl-dev libssl1.0 zlib1g-dev && \ wget -q -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | \ gpg --dearmor - | tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null && \ @@ -39,7 +39,7 @@ RUN apt-get update && \ cmake make bash wget unzip nano vim valgrind dh-make flex bison \ libpq-dev postgresql-server-dev-all \ libsasl2-2 libsasl2-dev openssl libssl-dev libssl1.1 \ - software-properties-common libyaml-dev apt-transport-https pkg-config zlib1g-dev && \ + software-properties-common libyaml-dev libsnmp-dev apt-transport-https pkg-config zlib1g-dev && \ wget -q -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | \ gpg --dearmor - | tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null && \ apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main' && \ @@ -61,7 +61,7 @@ RUN apt-get update && \ cmake make bash wget unzip nano vim valgrind dh-make flex bison \ libpq-dev postgresql-server-dev-all \ libsasl2-2 libsasl2-dev openssl libssl-dev libssl1.1 \ - software-properties-common libyaml-dev apt-transport-https pkg-config zlib1g-dev && \ + software-properties-common libyaml-dev libsnmp-dev apt-transport-https pkg-config zlib1g-dev && \ wget -q -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | \ gpg --dearmor - | tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null && \ apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main' && \ @@ -79,7 +79,7 @@ RUN apt-get update && \ apt-get install -y curl ca-certificates build-essential libsystemd-dev \ cmake make bash wget unzip nano vim valgrind dh-make flex bison \ libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev openssl libssl-dev libssl1.1 libyaml-dev pkg-config zlib1g-dev && \ + libsasl2-2 libsasl2-dev openssl libssl-dev libssl1.1 libyaml-dev libsnmp-dev pkg-config zlib1g-dev && \ apt-get install -y --reinstall lsb-base lsb-release # ubuntu/20.04.arm64v8 base image @@ -93,7 +93,7 @@ RUN apt-get update && \ apt-get install -y curl ca-certificates build-essential libsystemd-dev \ cmake make bash wget unzip nano vim valgrind dh-make flex bison \ libpq-dev postgresql-server-dev-all \ - libsasl2-2 libsasl2-dev openssl libssl-dev libssl1.1 libyaml-dev pkg-config zlib1g-dev && \ + libsasl2-2 libsasl2-dev openssl libssl-dev libssl1.1 libyaml-dev libsnmp-dev pkg-config zlib1g-dev && \ apt-get install -y --reinstall lsb-base lsb-release # ubuntu/22.04 base image @@ -105,7 +105,7 @@ RUN apt-get update && \ apt-get install -y curl ca-certificates build-essential libsystemd-dev \ cmake make bash wget unzip nano vim valgrind dh-make flex bison \ libpq-dev postgresql-server-dev-all libpq5 \ - libsasl2-2 libsasl2-dev openssl libssl-dev libssl3 libyaml-dev pkg-config zlib1g-dev && \ + libsasl2-2 libsasl2-dev openssl libssl-dev libssl3 libyaml-dev libsnmp-dev pkg-config zlib1g-dev && \ apt-get install -y --reinstall lsb-base lsb-release # ubuntu/22.04.arm64v8 base image @@ -119,7 +119,7 @@ RUN apt-get update && \ apt-get install -y curl ca-certificates build-essential libsystemd-dev \ cmake make bash wget unzip nano vim valgrind dh-make flex bison \ libpq-dev postgresql-server-dev-all libpq5 \ - libsasl2-2 libsasl2-dev openssl libssl-dev libssl3 libyaml-dev pkg-config zlib1g-dev && \ + libsasl2-2 libsasl2-dev openssl libssl-dev libssl3 libyaml-dev libsnmp-dev pkg-config zlib1g-dev && \ apt-get install -y --reinstall lsb-base lsb-release # ubuntu/24.04 base image