- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.8k
in_snmp: an input plugin for collect metrics by SNMP request #7671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
869e75c    to
    eb87261      
    Compare
  
    | I have found a leak in the code, and it can be resolved by applying this patch: net-snmp/net-snmp@4bd0d9a. However, I am unsure about the process of incorporating this fix into the fluent-bit project. Any suggestions or guidance on this matter would be greatly appreciated! | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've triggered a full build of all targets to confirm it builds for them, we probably need to modify dependencies to build.
Can you confirm it works for non-Linux targets as well? It may build but that doesn't mean it works...
        
          
                CMakeLists.txt
              
                Outdated
          
        
      | option(FLB_IN_PROC "Enable Process input plugin" Yes) | ||
| option(FLB_IN_SYSTEMD "Enable Systemd input plugin" Yes) | ||
| option(FLB_IN_DUMMY "Enable Dummy input plugin" Yes) | ||
| option(FLB_IN_SNMP "Enable SNMP input plugin" Yes) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To confirm - does this work on Windows and macOS as well? It will need disabling if not in their specific override cmake/ files.
| flex \ | ||
| bison \ | ||
| libyaml-dev \ | ||
| libsnmp-dev \ | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We presumably will also need this in the various package builds under packaging/distros too.
| @patrick-stephens thanks, I will try to build and run on Windows device | 
eb87261    to
    02ae8c0      
    Compare
  
    02ae8c0    to
    63597e4      
    Compare
  
    265dab6    to
    1c78626      
    Compare
  
    | I still fail to build it on Windows, so I choose to disable it in https://github.com/fluent/fluent-bit/blob/master/cmake/windows-setup.cmake. | 
| This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. | 
| @k402xxxcenxxx What's needed to finish this PR? Do you need help testing on a Windows device? | 
| 
 Hi @davidvasandani , Thanks for asking. I've been too busy lately to maintain this PR and I'm also considering not supporting this on Windows. Thanks again for your kind reminding. | 
1c78626    to
    9a4ed5a      
    Compare
  
    | This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. | 
| This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. | 
| @k402xxxcenxxx would you mind resolving the conflicts in this PR? | 
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 <[email protected]>
Add: unit test for snmp_walk Signed-off-by: k402xxxcenxxx <[email protected]>
Signed-off-by: k402xxxcenxxx <[email protected]>
Signed-off-by: k402xxxcenxxx <[email protected]>
Signed-off-by: k402xxxcenxxx <[email protected]>
Signed-off-by: k402xxxcenxxx <[email protected]>
9a4ed5a    to
    98a6aea      
    Compare
  
    | WalkthroughAdds a new SNMP input plugin and associated build/test support: Net‑SNMP CMake finder, plugin sources and header, plugin registration and CMake option, runtime tests with mock responses, Docker and distro packaging updates to install Net‑SNMP dev packages, and Windows build default disabling of SNMP. Changes
 Sequence Diagram(s)sequenceDiagram
    autonumber
    participant Init as Plugin Init
    participant SNMP as Net‑SNMP
    participant Collect as Collector
    participant Enc as Encoder
    participant Out as Fluent Bit Output
    Init->>SNMP: snmp_sess_init / open session (host,port,version,community)
    Init->>Enc: Init encoder
    loop periodic collection
        Collect->>SNMP: SNMP GET or GETNEXT (walk)
        SNMP-->>Collect: Response (OID, value) / errors
        Collect->>Collect: Parse & build MsgPack record(s)
        Collect->>Enc: Append record(s)
        Collect->>Out: Emit event
        Out-->>Collect: Ack
    end
    Collect->>SNMP: Cleanup (close session, unload MIBs)
    Collect->>Enc: Cleanup encoder
sequenceDiagram
    autonumber
    participant Test as Test Runner
    participant FB as Fluent Bit (test)
    participant Plugin as in_snmp
    participant Mock as Mock SNMP responder
    participant Validator as MsgPack Validator
    Test->>FB: Start in test mode (input + dummy output)
    FB->>Plugin: Init plugin
    loop scenarios (GET / WALK)
        Plugin->>Mock: Request SNMP data (mock)
        Mock-->>Plugin: Return mocked response
        Plugin->>Plugin: Encode to MsgPack
        Plugin->>Validator: Submit emitted record(s)
        Validator-->>Test: Validation result
    end
    Test->>FB: Stop and cleanup
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes 
 Suggested reviewers
 Poem
 Pre-merge checks and finishing touches❌ Failed checks (1 warning)
 ✅ Passed checks (2 passed)
 ✨ Finishing touches
 🧪 Generate unit tests (beta)
 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 15
🧹 Nitpick comments (10)
packaging/distros/ubuntu/Dockerfile (1)
28-28: SNMP headers included — good; minor package list cleanup on Xenial.Additions of libsnmp-dev are consistent and OK. On Ubuntu 16.04 stanza, software-properties-common appears twice (Line 27 and Line 28). Deduplicate to reduce noise.
- libpq-dev postgresql-server-dev-all software-properties-common \ - software-properties-common libyaml-dev libsnmp-dev apt-transport-https \ + libpq-dev postgresql-server-dev-all software-properties-common \ + libyaml-dev libsnmp-dev apt-transport-https \Also applies to: 60-60, 94-94, 124-124, 150-150, 174-174, 200-200, 224-224, 250-250
tests/runtime/in_snmp.c (3)
56-63: Avoid relying on map index/order; match by key.MsgPack maps are unordered. Search for the OID key and then validate its value to make the test robust.
- 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); + TEST_CHECK(obj->type == MSGPACK_OBJECT_MAP); + bool found = false; + for (uint32_t k = 0; k < obj->via.map.size; k++) { + msgpack_object mkey = obj->via.map.ptr[k].key; + msgpack_object mval = obj->via.map.ptr[k].val; + if (mkey.type == MSGPACK_OBJECT_STR && + mkey.via.str.size == strlen("iso.3.6.1.2.1.1.3.0") && + strncmp("iso.3.6.1.2.1.1.3.0", mkey.via.str.ptr, mkey.via.str.size) == 0) { + TEST_CHECK(mval.type == MSGPACK_OBJECT_STR); + TEST_CHECK(mval.via.str.size == strlen("123")); + TEST_CHECK(strncmp("123", mval.via.str.ptr, mval.via.str.size) == 0); + found = true; + break; + } + } + TEST_CHECK(found == true);
83-86: Confirm expected WALK value quoting.expected_vals include quotes (e.g., ""Fa0/0""). If the plugin emits raw strings without extra quotes, the comparison will fail. Please verify and adjust expected values accordingly.
Also applies to: 101-104
124-133: Add allocation checks and clean up env vars after the test.records allocation can fail (see flb_calloc behavior in tests). Also unset env vars to avoid cross-test interference.
- records = flb_calloc(1, sizeof(struct callback_records)); - records->num_records = 0; - records->records = NULL; + records = flb_calloc(1, sizeof(struct callback_records)); + TEST_CHECK(records != NULL); + if (!records) { + return; + } + records->num_records = 0; + records->records = NULL;And near the end:
- flb_destroy(ctx); + flb_destroy(ctx); + unsetenv("FLB_SNMP_PLUGIN_UNDER_TEST"); + unsetenv("TEST_SNMP_RESPONSE");plugins/in_snmp/CMakeLists.txt (1)
1-2: Wire include dirs and set link scope; optionally guard build on detection.Use the discovered include dirs and mark link as PRIVATE. If not already gated at top-level, wrap with NETSNMP_FOUND.
set(NETSNMP_LIBRARIES ${NETSNMP_LIBRARY}) set(NETSNMP_INCLUDE_DIRS ${NETSNMP_INCLUDE_DIR}) @@ FLB_PLUGIN(in_snmp "${src}" "") -target_link_libraries(flb-plugin-in_snmp ${NETSNMP_LIBRARIES}) +target_include_directories(flb-plugin-in_snmp PRIVATE ${NETSNMP_INCLUDE_DIRS}) +target_link_libraries(flb-plugin-in_snmp PRIVATE ${NETSNMP_LIBRARIES})If not globally enforced, consider:
# Optional guard if top-level doesn't gate this plugin already if (NOT NETSNMP_FOUND) message(FATAL_ERROR "Net-SNMP not found; in_snmp cannot be built") endif()Also applies to: 7-8
plugins/in_snmp/in_snmp.h (1)
7-7: Minimize transitive dependency from headers.Including net-snmp headers in a public plugin header couples consumers to Net-SNMP. Consider moving netsnmp_session into the .c file and storing an opaque pointer in the header.
plugins/in_snmp/in_snmp.c (2)
112-143: bool usage without an explicit include.If bool is not provided transitively, this will fail on some toolchains. Add <stdbool.h> or switch to int.
321-329: Collector interval is hard-coded to 1s, but default SNMP timeout is 5s.This can cause overlapping scheduled wakeups and continuous backlog under slow devices. Expose an interval option or set a safer default (>= timeout).
cmake/FindNetsnmp.cmake (2)
21-48: Library search paths are too narrow; prefer pkg-config and cover lib64/multiarch.Relying on hard-coded /usr/lib misses /usr/lib64, Debian multiarch, and Homebrew. Also, only netsnmp is typically required here.
Prefer:
find_package(PkgConfig REQUIRED) pkg_check_modules(NETSNMP REQUIRED IMPORTED_TARGET net-snmp) set(NETSNMP_INCLUDE_DIR ${NETSNMP_INCLUDE_DIR:-${NETSNMP_INCLUDE_DIRS}}) set(NETSNMP_LIBRARIES PkgConfig::NETSNMP)If keeping manual search, extend PATHS with /usr/lib64, /usr/lib/, /opt/homebrew/lib and set NETSNMP_INCLUDE_DIRS in addition to NETSNMP_INCLUDE_DIR for consumers.
Also applies to: 50-56
59-64: Expose include dirs consistently.Downstream CMake typically expects NETSNMP_INCLUDE_DIRS. Set it alongside NETSNMP_INCLUDE_DIR and include it in FIND_PACKAGE_HANDLE_STANDARD_ARGS.
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(NETSNMP - DEFAULT_MSG - NETSNMP_INCLUDE_DIR - NETSNMP_LIBRARIES -) +set(NETSNMP_INCLUDE_DIRS ${NETSNMP_INCLUDE_DIR}) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(NETSNMP + DEFAULT_MSG + NETSNMP_INCLUDE_DIRS + NETSNMP_LIBRARIES +)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (16)
- CMakeLists.txt(1 hunks)
- cmake/FindNetsnmp.cmake(1 hunks)
- cmake/windows-setup.cmake(1 hunks)
- dockerfiles/Dockerfile(1 hunks)
- dockerfiles/Dockerfile.centos7(1 hunks)
- packaging/distros/amazonlinux/Dockerfile(4 hunks)
- packaging/distros/centos/Dockerfile(8 hunks)
- packaging/distros/debian/Dockerfile(8 hunks)
- packaging/distros/raspbian/Dockerfile(3 hunks)
- packaging/distros/ubuntu/Dockerfile(9 hunks)
- plugins/CMakeLists.txt(1 hunks)
- plugins/in_snmp/CMakeLists.txt(1 hunks)
- plugins/in_snmp/in_snmp.c(1 hunks)
- plugins/in_snmp/in_snmp.h(1 hunks)
- tests/runtime/CMakeLists.txt(1 hunks)
- tests/runtime/in_snmp.c(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-09-08T11:21:33.975Z
Learnt from: cosmo0920
PR: fluent/fluent-bit#10851
File: include/fluent-bit/flb_simd.h:60-66
Timestamp: 2025-09-08T11:21:33.975Z
Learning: Fluent Bit currently only supports MSVC compiler on Windows, so additional compiler compatibility guards may be unnecessary for Windows-specific code paths.
Applied to files:
- CMakeLists.txt
🧬 Code graph analysis (2)
plugins/in_snmp/in_snmp.c (4)
include/fluent-bit/flb_mem.h (2)
flb_calloc(84-96)
flb_free(126-128)src/flb_log_event_encoder.c (6)
flb_log_event_encoder_begin_record(246-254)
flb_log_event_encoder_set_current_timestamp(289-292)
flb_log_event_encoder_commit_record(256-274)
flb_log_event_encoder_reset(31-40)
flb_log_event_encoder_init(42-74)
flb_log_event_encoder_destroy(99-116)include/fluent-bit/flb_input.h (1)
flb_input_config_map_set(715-741)src/flb_input.c (4)
flb_input_set_context(1610-1613)
flb_input_set_collector_time(1685-1704)
flb_input_collector_pause(2035-2072)
flb_input_collector_resume(2095-2151)
tests/runtime/in_snmp.c (3)
include/fluent-bit/flb_mem.h (2)
flb_calloc(84-96)
flb_free(126-128)plugins/out_s3/s3.c (1)
setenv(57-60)src/flb_lib.c (9)
flb_create(138-220)
flb_input(261-271)
flb_input_set(300-330)
flb_output(274-284)
flb_output_set(515-546)
flb_start(914-925)
flb_stop(942-985)
flb_lib_free(693-700)
flb_destroy(223-258)
🔇 Additional comments (10)
dockerfiles/Dockerfile (1)
58-58: LGTM! Dependency addition aligns with SNMP plugin integration.The addition of
libsnmp-devenables SNMP functionality in the builder-base stage. This addresses the earlier review comment about needing SNMP libraries across packaging targets, which has been consistently applied throughout the distribution-specific Dockerfiles in this PR.dockerfiles/Dockerfile.centos7 (1)
13-13: LGTM! Correct SNMP dependency for CentOS.The addition of
libsnmp-devel(RPM package naming) is consistent with the SNMP plugin integration and aligns with the Debian-based Dockerfile changes.cmake/windows-setup.cmake (1)
84-84: LGTM! Windows exclusion aligns with PR discussion.Explicitly disabling the SNMP input plugin on Windows is consistent with the PR comments indicating build failures on Windows and the decision to not support the platform initially. The placement among other Windows-disabled plugins (lines 49-84) follows the established pattern.
plugins/CMakeLists.txt (1)
284-284: LGTM! Plugin registration follows standard pattern.The SNMP input plugin registration is placed appropriately in alphabetical order and follows the standard registration pattern. The
REGISTER_IN_PLUGINmacro (lines 40-73) will automatically skip registration ifFLB_IN_SNMPis disabled (e.g., on Windows per cmake/windows-setup.cmake:84).tests/runtime/CMakeLists.txt (1)
49-49: LGTM! Runtime test registration follows established pattern.The SNMP runtime test registration is correctly placed in alphabetical order and uses the standard
FLB_RT_TESTmacro, which will conditionally compile the test only whenFLB_IN_SNMPis enabled.packaging/distros/raspbian/Dockerfile (3)
21-21: LGTM! Consistent SNMP dependency across Raspbian variants.The addition of
libsnmp-devto the Raspbian buster base image aligns with the SNMP plugin integration.
34-34: LGTM! Consistent SNMP dependency across Raspbian variants.The addition of
libsnmp-devto the Raspbian bullseye base image maintains consistency with other distribution variants.
47-47: LGTM! Consistent SNMP dependency across Raspbian variants.The addition of
libsnmp-devto the Raspbian bookworm base image completes the consistent SNMP support across all Raspbian versions (buster, bullseye, bookworm).packaging/distros/centos/Dockerfile (1)
26-26: LGTM! Comprehensive SNMP dependency coverage across CentOS variants.The addition of
libsnmp-develto all CentOS base images (versions 7, 8, 9, 10 across both x86_64 and arm64v8 architectures) provides complete SNMP plugin support across the CentOS packaging matrix. This addresses the earlier review feedback requesting packaging/distros updates.Also applies to: 61-61, 105-105, 144-144, 178-178, 207-207, 241-241, 270-270
packaging/distros/debian/Dockerfile (1)
31-31: SNMP development libs added across Debian variants — looks good.No issues spotted; package naming matches Debian conventions.
Also applies to: 62-62, 86-86, 112-112, 136-136, 162-162, 186-186, 212-212
| include(ExternalProject) | ||
| include(cmake/FindJournald.cmake) | ||
| include(cmake/FindMonkey.cmake) | ||
| include(cmake/FindNetsnmp.cmake) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify macOS support status for the SNMP plugin.
The inclusion of cmake/FindNetsnmp.cmake enables Net-SNMP discovery during configuration. While Windows support is explicitly disabled (cmake/windows-setup.cmake:84), the past review comments requested confirmation of macOS compatibility.
Please confirm whether macOS support is:
- Intended and tested (Net-SNMP is available via Homebrew)
- Disabled via cmake/macos-setup.cmake (similar to Windows)
- Left to fail at build-time if dependencies are missing
🏁 Script executed:
#!/bin/bash
# Check if macOS-specific SNMP configuration exists
echo "=== Checking for macOS-specific SNMP handling ==="
if [ -f "cmake/macos-setup.cmake" ]; then
    echo "Found cmake/macos-setup.cmake, checking for SNMP references:"
    rg -n "SNMP|snmp" cmake/macos-setup.cmake || echo "No SNMP configuration found in macOS setup"
else
    echo "No cmake/macos-setup.cmake found"
fi
echo ""
echo "=== Checking plugin registration for platform conditionals ==="
rg -n -A2 -B2 "REGISTER_IN_PLUGIN.*in_snmp" plugins/CMakeLists.txtLength of output: 390
🏁 Script executed:
#!/bin/bash
# Examine FindNetsnmp.cmake for platform-specific handling
echo "=== FindNetsnmp.cmake content ==="
cat cmake/FindNetsnmp.cmake
echo ""
echo "=== Checking plugins/in_snmp/CMakeLists.txt ==="
cat plugins/in_snmp/CMakeLists.txt
echo ""
echo "=== Checking windows-setup.cmake for SNMP handling ==="
rg -n -A3 -B3 "SNMP|snmp" cmake/windows-setup.cmake || echo "No SNMP references found"Length of output: 2098
🏁 Script executed:
#!/bin/bash
# Find how FLB_IN_SNMP is used in the build system
echo "=== FLB_IN_SNMP usage throughout CMakeLists.txt files ==="
rg -n "FLB_IN_SNMP" --type cmake
echo ""
echo "=== Check plugins/CMakeLists.txt around REGISTER_IN_PLUGIN(in_snmp) for conditionals ==="
rg -n -B10 "REGISTER_IN_PLUGIN.*in_snmp" plugins/CMakeLists.txt | head -30
echo ""
echo "=== Check if NETSNMP_FOUND is verified before using in_snmp plugin ==="
rg -n "NETSNMP_FOUND" --type cmake
echo ""
echo "=== Check main CMakeLists.txt for in_snmp conditionals ==="
rg -n "in_snmp\|IN_SNMP" CMakeLists.txtLength of output: 855
🏁 Script executed:
#!/bin/bash
# Find how in_snmp plugin directory is included in the build
echo "=== Search for add_subdirectory calls for in_snmp ==="
rg -n "add_subdirectory.*in_snmp" --type cmake
echo ""
echo "=== Check plugins/CMakeLists.txt for how FLB_IN_* flags gate plugin inclusion ==="
rg -n -B2 "add_subdirectory" plugins/CMakeLists.txt | head -50
echo ""
echo "=== Search for how FLB_IN_SNMP actually gates plugin building ==="
rg -n "FLB_IN_SNMP" . --type cmake -A2 -B2Length of output: 1753
🏁 Script executed:
#!/bin/bash
# Find REGISTER_IN_PLUGIN macro definition
echo "=== Search for REGISTER_IN_PLUGIN macro definition ==="
rg -n "macro.*REGISTER_IN_PLUGIN\|function.*REGISTER_IN_PLUGIN" --type cmake
echo ""
echo "=== Check where FindNetsnmp.cmake is included ==="
rg -n "FindNetsnmp\|find_package.*NETSNMP\|include.*FindNetsnmp" --type cmake
echo ""
echo "=== Check main CMakeLists.txt for NETSNMP handling ==="
rg -n -B3 -A3 "find_package.*SNMP\|FIND_PACKAGE.*SNMP\|include.*FindNetsnmp" CMakeLists.txt
echo ""
echo "=== Check CMakeLists.txt line 125 context (the line being reviewed) ==="
sed -n '120,130p' CMakeLists.txtLength of output: 601
🏁 Script executed:
#!/bin/bash
# Final verification: Check if there's any error handling or conditional build for SNMP
echo "=== Check plugins/in_snmp/CMakeLists.txt for NETSNMP_FOUND checks ==="
cat plugins/in_snmp/CMakeLists.txt
echo ""
echo "=== Search for conditional NETSNMP handling in cmake files ==="
rg -n "if.*NETSNMP_FOUND\|if.*NETSNMP_LIBRARY" --type cmake
echo ""
echo "=== Check if main CMakeLists.txt has error handling after FindNetsnmp.cmake ==="
sed -n '125,145p' CMakeLists.txt
echo ""
echo "=== Verify windows-setup.cmake applies before plugin registration ==="
rg -n -A20 "if.*WIN32\|if.*WINDOWS" CMakeLists.txt | head -30Length of output: 1096
Update FindNetsnmp.cmake to support Homebrew, or explicitly disable SNMP on macOS.
macOS support status remains unresolved. The current implementation will fail to build on macOS with Homebrew-installed Net-SNMP:
- FindNetsnmp.cmake searches only /usr/liband/usr/local/lib, missing Homebrew's standard install paths (/usr/local/opt/net-snmpon Intel,/opt/homebrew/opt/net-snmpon Apple Silicon)
- plugins/in_snmp/CMakeLists.txt unconditionally links against potentially empty ${NETSNMP_LIBRARY}without checkingNETSNMP_FOUND
- in_snmp plugin registration (plugins/CMakeLists.txt:284) is unconditional with no macOS guard, unlike the pattern used for other platform-specific plugins
The Windows disable mechanism (FLB_IN_SNMP=No in cmake/windows-setup.cmake:84) only gates tests, not the actual plugin build. Add equivalent macOS handling or update the SNMP finder to support Homebrew paths.
🤖 Prompt for AI Agents
In CMakeLists.txt around line 125 where FindNetsnmp.cmake is included, the build
will fail on macOS Homebrew installs because the finder only checks /usr/lib and
/usr/local/lib and the in_snmp plugin is unconditionally linked/registered;
update FindNetsnmp.cmake to also probe Homebrew prefixes
(/usr/local/opt/net-snmp and /opt/homebrew/opt/net-snmp) for include and lib
paths and set NETSNMP_FOUND appropriately, modify plugins/in_snmp/CMakeLists.txt
to conditionalize target_link_libraries and add_library calls on NETSNMP_FOUND
(error out or skip if not found), and guard the plugins/CMakeLists.txt in_snmp
registration with a platform or option check (e.g., skip on Apple or when
FLB_IN_SNMP=No) so macOS/Homebrew users can disable the plugin like Windows
does.
| cyrus-sasl-lib cyrus-sasl-devel openssl openssl-libs openssl-devel \ | ||
| postgresql-devel postgresql-libs glibc-devel \ | ||
| libyaml-devel zlib-devel libcurl-devel pkgconf-pkg-config \ | ||
| libyaml-devel libsnmp-devel zlib-devel libcurl-devel pkgconf-pkg-config \ | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Package name likely wrong on Amazon Linux — use net-snmp-devel.
On Amazon Linux (RPM-based), the devel package is commonly net-snmp-devel, not libsnmp-devel. Using libsnmp-devel may fail builds.
Please verify package availability and adjust:
-    libyaml-devel libsnmp-devel zlib-devel libcurl-devel pkgconf-pkg-config \
+    libyaml-devel net-snmp-devel zlib-devel libcurl-devel pkgconf-pkg-config \You can confirm via:
Also applies to: 51-51, 76-76, 103-103
🌐 Web query:
What is the package name for Net-SNMP development headers on Amazon Linux 2 and Amazon Linux 2023?
💡 Result:
The development headers package is net-snmp-devel on both Amazon Linux 2 and Amazon Linux 2023. [1][2]
Sources:
[1] Amazon Linux 2023 — AL2023 vs AL2 package comparison (shows net-snmp / net-snmp-devel).
[2] Amazon Linux 2023 package list (includes net-snmp-devel).
Replace libsnmp-devel with net-snmp-devel on Amazon Linux.
The development headers package is net-snmp-devel on both Amazon Linux 2 and Amazon Linux 2023. Using libsnmp-devel will fail the build.
Update lines 26, 51, 76, and 103:
-    libyaml-devel libsnmp-devel zlib-devel libcurl-devel pkgconf-pkg-config \
+    libyaml-devel net-snmp-devel zlib-devel libcurl-devel pkgconf-pkg-config \📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| libyaml-devel libsnmp-devel zlib-devel libcurl-devel pkgconf-pkg-config \ | |
| libyaml-devel net-snmp-devel zlib-devel libcurl-devel pkgconf-pkg-config \ | 
🤖 Prompt for AI Agents
In packaging/distros/amazonlinux/Dockerfile around lines 26, 51, 76, and 103,
the package name libsnmp-devel is incorrect for Amazon Linux and should be
replaced with net-snmp-devel; update each occurrence of libsnmp-devel to
net-snmp-devel so the build installs the correct development headers on Amazon
Linux 2 and 2023.
| 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." | ||
| }, | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oid_type help text mismatches behavior.
Help says “Use a 'field' …” but accepted values are "get" and "walk". Update the description for clarity.
🤖 Prompt for AI Agents
In plugins/in_snmp/in_snmp.c around lines 443 to 451, the help text for the
"oid_type" config entry is misleading (mentions "Use a 'field' …") while the
actual accepted values are "get" and "walk"; update the description to clearly
state the accepted values and their meaning (e.g., "set the type of SNMP
request: 'get' for a single OID, 'walk' to traverse an OID subtree") so the help
matches the implemented behavior.
| char *target_host; | ||
| int port; | ||
| int timeout; | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Port option is not used by the session.
ctx->port is never applied to netsnmp_session, so the configured port is ignored. Compose peername with the port or set the session remote port during init.
Apply in init (see suggested diff in in_snmp.c).
🤖 Prompt for AI Agents
In plugins/in_snmp/in_snmp.h around lines 16-18 the ctx->port field is never
applied to the netsnmp_session so user-configured ports are ignored; update the
SNMP session initialization in in_snmp.c to apply ctx->port by either composing
the session peername to include ":<port>" or by setting the session remote port
(e.g., ss->remote_port = ctx->port) after ss is created, ensuring you handle
defaults and validate the port value before assignment.
| void flb_test_snmp_records_message_get(struct callback_records *records) | ||
| { | ||
| 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); | ||
| } | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset msgpack offset per record to avoid partial/failed parses.
off must be reset for each buffer. Otherwise later records may not be parsed.
 void flb_test_snmp_records_message_get(struct callback_records *records)
 {
-    int i;
+    int i;
     msgpack_unpacked result;
     msgpack_object *obj;
-    size_t off = 0;
+    size_t off = 0;
     struct flb_time ftm;
@@
-    for (i = 0; i < records->num_records; i++) {
+    for (i = 0; i < records->num_records; i++) {
+        off = 0;
         msgpack_unpacked_init(&result);
@@
     }
 }
@@
 void flb_test_snmp_records_message_walk(struct callback_records *records)
 {
-    int i, j;
+    int i, j;
     msgpack_unpacked result;
     msgpack_object *obj;
-    size_t off = 0;
+    size_t off = 0;
     struct flb_time ftm;
@@
-    for (i = 0; i < records->num_records; i++) {
+    for (i = 0; i < records->num_records; i++) {
+        off = 0;
         msgpack_unpacked_init(&result);Also applies to: 68-109
🤖 Prompt for AI Agents
In tests/runtime/in_snmp.c around lines 41 to 66 (and similarly for 68-109), the
msgpack unpack offset variable `off` is not reset per-record causing later
records to be partially or not parsed; reset `off` to 0 immediately before each
call sequence that uses msgpack_unpack_next (i.e., inside the for loop, before
the while loop/unpack loop for each records->records[i]) so each buffer is
parsed from the start, then proceed to unpack and destroy as before.
| 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); | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent out-of-bounds reads when asserting WALK results.
obj->via.map.size may exceed 2; expected_keys/vals are size 2. Bound the loop or assert size first.
-            for (j = 0; j < obj->via.map.size; j++) {
+            {
+                size_t count = obj->via.map.size;
+                TEST_CHECK(count >= 2);
+                size_t limit = count < 2 ? count : 2;
+                for (j = 0; j < limit; j++) {
                 key = obj->via.map.ptr[j].key;
                 val = obj->via.map.ptr[j].val;
@@
-            }
+                }
+            }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| 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); | |
| } | |
| { | |
| size_t count = obj->via.map.size; | |
| TEST_CHECK(count >= 2); | |
| size_t limit = count < 2 ? count : 2; | |
| for (j = 0; j < limit; 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); | |
| } | |
| } | 
🤖 Prompt for AI Agents
In tests/runtime/in_snmp.c around lines 95 to 105, the loop iterates up to
obj->via.map.size but expected_keys and expected_vals only hold 2 entries, which
can cause out-of-bounds reads; modify the test to first assert that
obj->via.map.size == 2 (or at least <= 2) and then loop using that
asserted/limited size (e.g., for (j = 0; j < obj->via.map.size && j < 2; j++))
so comparisons only access valid expected_keys/expected_vals entries.
| /* 4 sec passed. It must have flushed */ | ||
| sleep(5); | ||
|  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix comment to match actual sleep.
Comment says 4 sec, code sleeps 5.
-    /* 4 sec passed. It must have flushed */
+    /* 5 sec passed. It must have flushed */📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| /* 4 sec passed. It must have flushed */ | |
| sleep(5); | |
| /* 5 sec passed. It must have flushed */ | |
| sleep(5); | |
🤖 Prompt for AI Agents
In tests/runtime/in_snmp.c around lines 156 to 158, the inline comment
incorrectly states "4 sec passed" while the code calls sleep(5); update the
comment to reflect the actual sleep duration (e.g., "5 sec passed. It must have
flushed") or change the sleep call to match the comment if 4 seconds was
intended; ensure comment and code stay consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
cmake/FindNetsnmp.cmake (1)
23-51: Consider broadening library search paths.The FIND_LIBRARY calls explicitly specify only
/usr/liband/usr/local/lib. While CMake will also search system default paths, you may want to either:
- Remove the explicit
PATHSspecification to rely fully on CMake's default search behavior- Add additional common paths such as
/usr/lib64,/usr/lib/x86_64-linux-gnu,/opt/homebrew/lib, and/opt/local/libfor better cross-platform supportExample for the first library:
SET(NETSNMP_NAMES netsnmp) FIND_LIBRARY(NETSNMP_LIBRARY NAMES ${NETSNMP_NAMES} - PATHS /usr/lib /usr/local/lib )Or add more paths:
SET(NETSNMP_NAMES netsnmp) FIND_LIBRARY(NETSNMP_LIBRARY NAMES ${NETSNMP_NAMES} - PATHS /usr/lib /usr/local/lib + PATHS /usr/lib /usr/lib64 /usr/local/lib /opt/homebrew/lib /opt/local/lib )
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
- cmake/FindNetsnmp.cmake(1 hunks)
- cmake/plugins_options.cmake(1 hunks)
🔇 Additional comments (3)
cmake/plugins_options.cmake (1)
52-52: LGTM! Plugin option follows the standard pattern.The SNMP input plugin option is correctly defined and positioned alphabetically among other input plugins.
cmake/FindNetsnmp.cmake (2)
16-21: Include path search looks good after previous fix.The FIND_PATH now correctly searches for
net-snmp/net-snmp-includes.hin common include directories. This addresses the previous review concern about searching for the wrong header.
58-58: Verify whether netsnmptrapd is intentionally excluded.The
NETSNMPTRAPD_LIBRARYis discovered but commented out of the finalNETSNMP_LIBRARIESaggregation. For a plugin that performs SNMP polling (rather than receiving traps), excluding trapd makes sense. However, please confirm this is intentional and consider adding a comment explaining why it's excluded.If the plugin indeed doesn't need trap handling, consider adding a clarifying comment:
${NETSNMPMIBS_LIBRARY} -# ${NETSNMPTRAPD_LIBRARY} +# ${NETSNMPTRAPD_LIBRARY} # trapd not needed for polling-only plugin )
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: k402xxxcenxxx <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
cmake/FindNetsnmp.cmake (2)
16-51: Consider using pkg-config for more robust discovery.Net-SNMP typically provides
net-snmp-configand pkg-config metadata. Modern CMake Find modules often usepkg_check_modulesorpkg_search_moduleas a primary or fallback discovery mechanism, which automatically handles distro-specific paths, linker flags, and versions without hardcoding paths.Example pattern to try pkg-config first:
# Try pkg-config first FIND_PACKAGE(PkgConfig QUIET) IF(PKG_CONFIG_FOUND) PKG_CHECK_MODULES(PC_NETSNMP QUIET netsnmp) ENDIF() FIND_PATH(NETSNMP_INCLUDE_DIR net-snmp/net-snmp-includes.h HINTS ${PC_NETSNMP_INCLUDE_DIRS} PATHS /usr/include /usr/local/include /opt/homebrew/include /opt/local/include ) FIND_LIBRARY(NETSNMP_LIBRARY NAMES ${NETSNMP_NAMES} HINTS ${PC_NETSNMP_LIBRARY_DIRS} PATHS /usr/lib /usr/local/lib /opt/homebrew/lib /opt/local/lib )
58-58: Document whyNETSNMPTRAPD_LIBRARYis excluded.The
netsnmptrapdlibrary is searched (lines 47–51) but commented out ofNETSNMP_LIBRARIES. Adding a brief comment explaining the reason (e.g., "not required for input plugin" or "optional component") would help future maintainers understand the intent.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: k402xxxcenxxx <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (4)
plugins/in_snmp/in_snmp.c (4)
458-462: Help text does not match actual behavior.The
oid_typehelp text mentions "Use a 'field'…" but the accepted values are "get" (for a single OID retrieval) and "walk" (to traverse a subtree), as validated in lines 142-150.Update the description for clarity:
{ 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." + "set the type of SNMP request: 'get' for a single OID, 'walk' to traverse an OID subtree." },
199-285: Critical memory leak: buffers reallocated per variable but never freed in loop.
oid_bufandbufare allocated inside the per-variable loop (lines 211 and 228) but only freed once at the end of the function (lines 308-309). In an SNMP walk returning multiple variables, all but the last allocation for each buffer will be leaked because the pointer is overwritten on each iteration.Apply this fix to free the buffers after each variable is processed:
} else { flb_plg_error(ctx->ins, "[TRUNCATED]"); if (response) { snmp_free_pdu(response); } ret = -1; goto collect_cleanup; } } + + /* Free per-iteration buffers to avoid leaks */ + if (buf) { + flb_free(buf); + buf = NULL; + } + if (oid_buf) { + flb_free(oid_buf); + oid_buf = NULL; + } if ((vars->type != SNMP_ENDOFMIBVIEW) &&
152-159: Pass NULL explicitly to snmp_error when session is NULL.When
snmp_open()fails and returns NULL, the first argument tosnmp_error()should beNULLrather than the NULLsspointer for clarity.Apply this diff:
ss = snmp_open(&ctx->session); if (!ss) { - snmp_error(ss, NULL, NULL, &err); + snmp_error(NULL, NULL, NULL, &err); flb_plg_error(ctx->ins, "%s", err); ret = -1; goto collect_cleanup; }
375-392: Port configuration ignored; session timeout/retries only set globally.Line 378 assigns only
target_hosttopeername, ignoring the configuredportvalue. SNMP requests will always use the default port 161. Additionally, timeout and retries are set as library-wide defaults (lines 370-371) but not on the session itself.Apply this fix to honor the port and set session-specific timeouts:
snmp_sess_init(&ctx->session); - ctx->session.peername = ctx->target_host; + /* Compose peername with port */ + { + size_t need = strlen(ctx->target_host) + 32; + char *peer = flb_calloc(1, need); + if (!peer) { + flb_plg_error(ctx->ins, "failed to allocate peername"); + flb_log_event_encoder_destroy(&ctx->log_encoder); + flb_free(ctx); + return -1; + } + snprintf(peer, need, "%s:%d", ctx->target_host, ctx->port); + ctx->session.peername = peer; + /* Note: Remember to free this in in_snmp_exit */ + } + + /* Set session-specific timeout (in microseconds) and retries */ + ctx->session.timeout = ctx->timeout * 1000000L; + ctx->session.retries = ctx->retries; if (strcmp(ctx->version, "1") == 0) {Then in
in_snmp_exit(), free the allocated peername: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); + /* Free allocated peername if it differs from target_host */ + if (ctx->session.peername && ctx->session.peername != ctx->target_host) { + flb_free((void *)ctx->session.peername); + } + flb_free(ctx);
🧹 Nitpick comments (1)
plugins/in_snmp/in_snmp.c (1)
276-280: Consider freeing err immediately after logging for cleaner resource management.While the current cleanup path at line 307 does free
err, freeing it immediately after use would make the code more maintainable and reduce the chance of leaks if the code is modified later.Optional improvement:
} else { snmp_error(ss, NULL, NULL, &err); flb_plg_error(ctx->ins, "%s", err); + if (err) { + free(err); + err = NULL; + } running = 0; }Note: Use
free()rather thanflb_free()sincesnmp_error()allocates withmalloc().
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
- plugins/in_snmp/in_snmp.c(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
plugins/in_snmp/in_snmp.c (4)
include/fluent-bit/flb_mem.h (2)
flb_calloc(84-96)
flb_free(126-128)src/flb_log_event_encoder.c (5)
flb_log_event_encoder_begin_record(246-254)
flb_log_event_encoder_commit_record(256-274)
flb_log_event_encoder_reset(31-40)
flb_log_event_encoder_init(42-74)
flb_log_event_encoder_destroy(99-116)include/fluent-bit/flb_input.h (1)
flb_input_config_map_set(715-741)src/flb_input.c (4)
flb_input_set_context(1610-1613)
flb_input_set_collector_time(1685-1704)
flb_input_collector_pause(2035-2072)
flb_input_collector_resume(2095-2151)
🔇 Additional comments (5)
plugins/in_snmp/in_snmp.c (5)
16-117: Test helper implementation looks good.The test support functions properly handle PDU cleanup and NULL checks for environment variables as recommended in previous reviews.
160-165: Proper cleanup path established.The OID parsing error path now correctly uses
goto collect_cleanupto ensure resources are freed, addressing the previous review concern.
304-312: Cleanup block is functional.The cleanup successfully frees allocated resources. Note that
responseis already freed in the loop (line 283) or was never allocated, so line 305 is defensive but doesn't leak.
396-424: Lifecycle functions correctly implemented.Pause, resume, and exit callbacks properly manage the collector state and cleanup resources. Previous concerns about config dereferencing and OpenSSL cleanup have been addressed.
471-482: Plugin registration structure is correct.The plugin registration properly declares all required callbacks and metadata for integration with Fluent Bit.
Enter
[N/A]in the box, if an item is not applicable to your change.Testing
Before we can approve your change; please submit the following in a comment:
There is one leak and can be fixed after this patch is release: net-snmp/net-snmp@4bd0d9a
If this is a change to packaging of containers or native binaries then please confirm it works for all targets.
ok-package-testlabel to test for all targets (requires maintainer to do).Documentation
fluent/fluent-bit-docs#1162
Backporting
Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.
Summary by CodeRabbit
New Features
Build / Packaging
Tests