Skip to content

Conversation

@k402xxxcenxxx
Copy link

@k402xxxcenxxx k402xxxcenxxx commented Jul 9, 2023


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:

  • Example configuration file for the change
[INPUT]
    Name snmp
    Tag snmp.test
    Target_Host 172.17.0.3
    Community public
    Oid_Type get
    Oid 1.3.6.1.2.1.1.3.0
    Timeout 1
    Retries 20
[OUTPUT]
    Name stdout
    Match *
  • Debug log output from testing the change
$ valgrind --leak-check=full --show-leak-kinds=all ./bin/fluent-bit -c in_snmp.conf  -v
==26== Memcheck, a memory error detector
==26== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==26== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==26== Command: ./bin/fluent-bit -c in_snmp.conf -v
==26== 
Fluent Bit v2.1.7
* Copyright (C) 2015-2022 The Fluent Bit Authors
* Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
* https://fluentbit.io

[2023/07/11 06:00:49] [ info] Configuration:
[2023/07/11 06:00:49] [ info]  flush time     | 1.000000 seconds
[2023/07/11 06:00:49] [ info]  grace          | 5 seconds
[2023/07/11 06:00:49] [ info]  daemon         | 0
[2023/07/11 06:00:49] [ info] ___________
[2023/07/11 06:00:49] [ info]  inputs:
[2023/07/11 06:00:49] [ info]      snmp
[2023/07/11 06:00:49] [ info] ___________
[2023/07/11 06:00:49] [ info]  filters:
[2023/07/11 06:00:49] [ info] ___________
[2023/07/11 06:00:49] [ info]  outputs:
[2023/07/11 06:00:49] [ info]      stdout.0
[2023/07/11 06:00:49] [ info] ___________
[2023/07/11 06:00:49] [ info]  collectors:
[2023/07/11 06:00:49] [ info] [fluent bit] version=2.1.7, commit=7182681f78, pid=26
[2023/07/11 06:00:49] [debug] [engine] coroutine stack size: 24576 bytes (24.0K)
[2023/07/11 06:00:49] [ info] [storage] ver=1.4.0, type=memory, sync=normal, checksum=off, max_chunks_up=128
[2023/07/11 06:00:49] [ info] [cmetrics] version=0.6.3
[2023/07/11 06:00:49] [ info] [ctraces ] version=0.3.1
[2023/07/11 06:00:49] [ info] [input:snmp:snmp.0] initializing
[2023/07/11 06:00:49] [ info] [input:snmp:snmp.0] storage_strategy='memory' (memory only)
[2023/07/11 06:00:49] [debug] [snmp:snmp.0] created event channels: read=21 write=22
[2023/07/11 06:00:49] [debug] [stdout:stdout.0] created event channels: read=23 write=24
[2023/07/11 06:00:49] [ info] [sp] stream processor started
[2023/07/11 06:00:49] [ info] [output:stdout:stdout.0] worker #0 started
[2023/07/11 06:00:50] [debug] [input chunk] update output instances with new chunk size diff=53, records=1, input=snmp.0
[2023/07/11 06:00:51] [debug] [task] created task=0x54cc990 id=0 OK
[2023/07/11 06:00:51] [debug] [output:stdout:stdout.0] task_id=0 assigned to thread #0
[0] snmp.test: [[1689055250.198336300, {}], {"iso.3.6.1.2.1.1.3.0"=>"57"}]
[2023/07/11 06:00:51] [debug] [input chunk] update output instances with new chunk size diff=53, records=1, input=snmp.0
[2023/07/11 06:00:51] [debug] [out flush] cb_destroy coro_id=0
[2023/07/11 06:00:51] [debug] [task] destroy task=0x54cc990 (task_id=0)
[2023/07/11 06:00:52] [debug] [task] created task=0x5532f10 id=0 OK
[2023/07/11 06:00:52] [debug] [output:stdout:stdout.0] task_id=0 assigned to thread #0
[0] snmp.test: [[1689055251.182227200, {}], {"iso.3.6.1.2.1.1.3.0"=>"58"}]
[2023/07/11 06:00:52] [debug] [out flush] cb_destroy coro_id=1
[2023/07/11 06:00:52] [debug] [input chunk] update output instances with new chunk size diff=53, records=1, input=snmp.0
[2023/07/11 06:00:52] [debug] [task] destroy task=0x5532f10 (task_id=0)
[2023/07/11 06:00:53] [debug] [task] created task=0x5598fd0 id=0 OK
[2023/07/11 06:00:53] [debug] [output:stdout:stdout.0] task_id=0 assigned to thread #0
[0] snmp.test: [[1689055252.137364100, {}], {"iso.3.6.1.2.1.1.3.0"=>"59"}]
[2023/07/11 06:00:53] [debug] [out flush] cb_destroy coro_id=2
[2023/07/11 06:00:53] [debug] [input chunk] update output instances with new chunk size diff=53, records=1, input=snmp.0
[2023/07/11 06:00:53] [debug] [task] destroy task=0x5598fd0 (task_id=0)
[2023/07/11 06:00:54] [debug] [task] created task=0x56010d0 id=0 OK
[0] snmp.test: [[1689055253.136002200, {}], {"iso.3.6.1.2.1.1.3.0"=>"60"}]
[2023/07/11 06:00:54] [debug] [output:stdout:stdout.0] task_id=0 assigned to thread #0
[2023/07/11 06:00:54] [debug] [out flush] cb_destroy coro_id=3
[2023/07/11 06:00:54] [debug] [input chunk] update output instances with new chunk size diff=53, records=1, input=snmp.0
[2023/07/11 06:00:54] [debug] [task] destroy task=0x56010d0 (task_id=0)
^C[2023/07/11 06:00:54] [engine] caught signal (SIGINT)
[2023/07/11 06:00:54] [debug] [task] created task=0x56671e0 id=0 OK
[2023/07/11 06:00:54] [debug] [output:stdout:stdout.0] task_id=0 assigned to thread #0
[0] snmp.test: [[1689055254.149898200, {}], {"iso.3.6.1.2.1.1.3.0"=>"61"}]
[2023/07/11 06:00:54] [ warn] [engine] service will shutdown in max 5 seconds
[2023/07/11 06:00:54] [ info] [input] pausing snmp.0
[2023/07/11 06:00:54] [debug] [out flush] cb_destroy coro_id=4
[2023/07/11 06:00:54] [debug] [task] destroy task=0x56671e0 (task_id=0)
[2023/07/11 06:00:55] [ info] [engine] service has stopped (0 pending tasks)
read_config_store open failure on /var/lib/snmp/snmp.conf
[2023/07/11 06:00:55] [ info] [input] pausing snmp.0
read_config_store open failure on /var/lib/snmp/snmp.conf
read_config_store open failure on /var/lib/snmp/snmp.conf
[2023/07/11 06:00:55] [ info] [output:stdout:stdout.0] thread worker #0 stopping...
[2023/07/11 06:00:55] [ info] [output:stdout:stdout.0] thread worker #0 stopped
==26== 
==26== HEAP SUMMARY:
==26==     in use at exit: 4,800 bytes in 1 blocks
==26==   total heap usage: 3,723 allocs, 3,722 frees, 2,543,883 bytes allocated
==26== 
==26== 4,800 bytes in 1 blocks are still reachable in loss record 1 of 1
==26==    at 0x483AB65: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==26==    by 0x4AE51F7: netsnmp_init_mib_internals (in /usr/lib/x86_64-linux-gnu/libnetsnmp.so.40.0.0)
==26==    by 0x4AE9C8B: ??? (in /usr/lib/x86_64-linux-gnu/libnetsnmp.so.40.0.0)
==26==    by 0x4AFB5F4: init_snmp (in /usr/lib/x86_64-linux-gnu/libnetsnmp.so.40.0.0)
==26==    by 0x42BBD6: in_snmp_init (in_snmp.c:372)
==26==    by 0x1FC78D: flb_input_instance_init (flb_input.c:1189)
==26==    by 0x1FC925: flb_input_init_all (flb_input.c:1248)
==26==    by 0x23D4B0: flb_engine_start (flb_engine.c:730)
==26==    by 0x1DED52: flb_lib_worker (flb_lib.c:638)
==26==    by 0x4855EA6: start_thread (pthread_create.c:477)
==26==    by 0x5069A2E: clone (clone.S:95)
==26== 
==26== LEAK SUMMARY:
==26==    definitely lost: 0 bytes in 0 blocks
==26==    indirectly lost: 0 bytes in 0 blocks
==26==      possibly lost: 0 bytes in 0 blocks
==26==    still reachable: 4,800 bytes in 1 blocks
==26==         suppressed: 0 bytes in 0 blocks
==26== 
==26== For lists of detected and suppressed errors, rerun with: -s
==26== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0
  • Attached Valgrind output that shows no leaks or memory corruption was found
==26== 
==26== HEAP SUMMARY:
==26==     in use at exit: 4,800 bytes in 1 blocks
==26==   total heap usage: 3,723 allocs, 3,722 frees, 2,543,883 bytes allocated
==26== 
==26== 4,800 bytes in 1 blocks are still reachable in loss record 1 of 1
==26==    at 0x483AB65: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==26==    by 0x4AE51F7: netsnmp_init_mib_internals (in /usr/lib/x86_64-linux-gnu/libnetsnmp.so.40.0.0)
==26==    by 0x4AE9C8B: ??? (in /usr/lib/x86_64-linux-gnu/libnetsnmp.so.40.0.0)
==26==    by 0x4AFB5F4: init_snmp (in /usr/lib/x86_64-linux-gnu/libnetsnmp.so.40.0.0)
==26==    by 0x42BBD6: in_snmp_init (in_snmp.c:372)
==26==    by 0x1FC78D: flb_input_instance_init (flb_input.c:1189)
==26==    by 0x1FC925: flb_input_init_all (flb_input.c:1248)
==26==    by 0x23D4B0: flb_engine_start (flb_engine.c:730)
==26==    by 0x1DED52: flb_lib_worker (flb_lib.c:638)
==26==    by 0x4855EA6: start_thread (pthread_create.c:477)
==26==    by 0x5069A2E: clone (clone.S:95)
==26== 
==26== LEAK SUMMARY:
==26==    definitely lost: 0 bytes in 0 blocks
==26==    indirectly lost: 0 bytes in 0 blocks
==26==      possibly lost: 0 bytes in 0 blocks
==26==    still reachable: 4,800 bytes in 1 blocks
==26==         suppressed: 0 bytes in 0 blocks
==26== 
==26== For lists of detected and suppressed errors, rerun with: -s
==26== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0

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.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

fluent/fluent-bit-docs#1162
Backporting

  • Backport to latest stable release.

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

    • SNMP input plugin added — collects SNMP GET and GETNEXT (walk), configurable session options, test-mode support, and structured log event encoding.
  • Build / Packaging

    • Build now detects and links Net‑SNMP; SNMP input build option enabled by default.
    • Container images and build setups include SNMP development packages; SNMP explicitly disabled in default Windows config.
  • Tests

    • End-to-end runtime tests added for SNMP GET and WALK scenarios.

@k402xxxcenxxx k402xxxcenxxx changed the title Feature/in snmp in_snmp: an input plugin for collect metrics by SNMP request Jul 15, 2023
@k402xxxcenxxx k402xxxcenxxx marked this pull request as ready for review July 15, 2023 07:17
@k402xxxcenxxx
Copy link
Author

k402xxxcenxxx commented Jul 15, 2023

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!

@patrick-stephens patrick-stephens added the ok-package-test Run PR packaging tests label Jul 17, 2023
Copy link
Collaborator

@patrick-stephens patrick-stephens left a 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)
Copy link
Collaborator

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 \
Copy link
Collaborator

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.

@k402xxxcenxxx
Copy link
Author

@patrick-stephens thanks, I will try to build and run on Windows device

@k402xxxcenxxx
Copy link
Author

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.
And I'm trying to build to various distribution

@k402xxxcenxxx k402xxxcenxxx temporarily deployed to pr August 21, 2023 10:22 — with GitHub Actions Inactive
@k402xxxcenxxx k402xxxcenxxx temporarily deployed to pr August 21, 2023 10:22 — with GitHub Actions Inactive
@k402xxxcenxxx k402xxxcenxxx temporarily deployed to pr August 21, 2023 10:22 — with GitHub Actions Inactive
@k402xxxcenxxx k402xxxcenxxx temporarily deployed to pr August 21, 2023 10:53 — with GitHub Actions Inactive
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot added the Stale label Dec 11, 2023
@github-actions github-actions bot removed the Stale label Aug 16, 2024
@davidvasandani
Copy link

@k402xxxcenxxx What's needed to finish this PR? Do you need help testing on a Windows device?

@k402xxxcenxxx
Copy link
Author

@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.
I'll try to handle on this PR as soon as possible.

Thanks again for your kind reminding.

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot added Stale and removed Stale labels Mar 16, 2025
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot added the Stale label Aug 28, 2025
@eschabell
Copy link

@k402xxxcenxxx would you mind resolving the conflicts in this PR?

@github-actions github-actions bot removed the Stale label Oct 24, 2025
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]>
@coderabbitai
Copy link

coderabbitai bot commented Oct 24, 2025

Walkthrough

Adds 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

Cohort / File(s) Summary
CMake Net‑SNMP Finder
cmake/FindNetsnmp.cmake
New Find module locating Net‑SNMP include dir and libraries; exposes NETSNMP_INCLUDE_DIR(S), NETSNMP_LIBRARIES, per-library variables and name lists, uses standard FindPackage handling and marks variables ADVANCED.
Top‑level CMake & options
CMakeLists.txt, cmake/plugins_options.cmake, cmake/windows-setup.cmake
CMakeLists.txt now includes the new finder; cmake/plugins_options.cmake adds FLB_IN_SNMP option (default ON); cmake/windows-setup.cmake sets FLB_IN_SNMP default to No for Windows builds.
Plugin registration
plugins/CMakeLists.txt
Registers new input plugin in_snmp via REGISTER_IN_PLUGIN("in_snmp").
SNMP plugin build files
plugins/in_snmp/CMakeLists.txt
Adds plugin target, sets NETSNMP_LIBRARIES/NETSNMP_INCLUDE_DIRS, registers FLB_PLUGIN(in_snmp ...) and links flb-plugin-in_snmp against Net‑SNMP.
SNMP plugin sources & header
plugins/in_snmp/in_snmp.c, plugins/in_snmp/in_snmp.h
New input plugin implementation and header: SNMP session setup, GET/GETNEXT (walk) logic, encoder usage, lifecycle callbacks (init/collect/pause/resume/exit), config map, test-mode hooks, and struct flb_snmp.
Runtime tests
tests/runtime/CMakeLists.txt, tests/runtime/in_snmp.c
Adds runtime test target and test harness exercising GET and WALK via mocked SNMP responses, MsgPack validation, and registers the test in TEST_LIST.
Docker / packaging updates
dockerfiles/Dockerfile, dockerfiles/Dockerfile.centos7, packaging/distros/...
Adds libsnmp-dev / libsnmp-devel to package install lists across multiple Dockerfiles and distro packaging Dockerfiles (amazonlinux, centos, debian, raspbian, ubuntu) to provide Net‑SNMP development headers/libraries.

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
Loading
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
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

  • Areas needing extra attention:
    • plugins/in_snmp/in_snmp.c — SNMP protocol correctness, memory management, error and edge-case handling, thread/FD interactions.
    • tests/runtime/in_snmp.c — test determinism, mock correctness, MsgPack parsing/validation.
    • cmake/FindNetsnmp.cmake and plugins/in_snmp/CMakeLists.txt — cross‑platform finder/linker behavior.
    • Multiple Dockerfile and packaging edits — ensure consistent package lists across distros.

Suggested reviewers

  • leonardo-albertovich
  • edsiper
  • niedbalski
  • koleini
  • fujimotos
  • patrick-stephens

Poem

🐇 I hopped through headers, found netsnmp in the loam,

I fetched the OIDs and bundled them for home.
I walked the trees and packed each tiny key,
Tests chirped, images grew a snmp dev tree.
A rabbit's patch — small code, big telemetry.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "in_snmp: an input plugin for collect metrics by SNMP request" accurately describes the primary change in the changeset. The PR adds a complete new SNMP input plugin implementation to Fluent Bit, including plugin source files, CMake configuration, Docker build dependencies, test harnesses, and build system integration. The title directly and clearly communicates the plugin name, type, and core purpose, allowing a teammate reviewing the history to immediately understand that this change introduces SNMP-based metric collection capability.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 6345fd1 and 98a6aea.

📒 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-dev enables 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_PLUGIN macro (lines 40-73) will automatically skip registration if FLB_IN_SNMP is 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_TEST macro, which will conditionally compile the test only when FLB_IN_SNMP is enabled.

packaging/distros/raspbian/Dockerfile (3)

21-21: LGTM! Consistent SNMP dependency across Raspbian variants.

The addition of libsnmp-dev to the Raspbian buster base image aligns with the SNMP plugin integration.


34-34: LGTM! Consistent SNMP dependency across Raspbian variants.

The addition of libsnmp-dev to the Raspbian bullseye base image maintains consistency with other distribution variants.


47-47: LGTM! Consistent SNMP dependency across Raspbian variants.

The addition of libsnmp-dev to 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-devel to 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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 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:

  1. Intended and tested (Net-SNMP is available via Homebrew)
  2. Disabled via cmake/macos-setup.cmake (similar to Windows)
  3. 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.txt

Length 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.txt

Length 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 -B2

Length 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.txt

Length 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 -30

Length 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/lib and /usr/local/lib, missing Homebrew's standard install paths (/usr/local/opt/net-snmp on Intel, /opt/homebrew/opt/net-snmp on Apple Silicon)
  • plugins/in_snmp/CMakeLists.txt unconditionally links against potentially empty ${NETSNMP_LIBRARY} without checking NETSNMP_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 \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 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.

Suggested change
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.

Comment on lines +443 to +451
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."
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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.

Comment on lines +16 to +18
char *target_host;
int port;
int timeout;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.

Comment on lines +41 to +66
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);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

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.

Comment on lines +95 to +105
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);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

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.

Suggested change
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.

Comment on lines +156 to +158
/* 4 sec passed. It must have flushed */
sleep(5);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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.

Suggested change
/* 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.

Copy link

@coderabbitai coderabbitai bot left a 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/lib and /usr/local/lib. While CMake will also search system default paths, you may want to either:

  • Remove the explicit PATHS specification 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/lib for better cross-platform support

Example 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

📥 Commits

Reviewing files that changed from the base of the PR and between 98a6aea and b714203.

📒 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.h in 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_LIBRARY is discovered but commented out of the final NETSNMP_LIBRARIES aggregation. 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]>
Copy link

@coderabbitai coderabbitai bot left a 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-config and pkg-config metadata. Modern CMake Find modules often use pkg_check_modules or pkg_search_module as 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 why NETSNMPTRAPD_LIBRARY is excluded.

The netsnmptrapd library is searched (lines 47–51) but commented out of NETSNMP_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.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b714203 and 1060f6d.

📒 Files selected for processing (1)
  • cmake/FindNetsnmp.cmake (1 hunks)

k402xxxcenxxx and others added 2 commits October 30, 2025 20:36
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: k402xxxcenxxx <[email protected]>
Copy link

@coderabbitai coderabbitai bot left a 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_type help 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_buf and buf are 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 to snmp_error() should be NULL rather than the NULL ss pointer 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_host to peername, ignoring the configured port value. 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 than flb_free() since snmp_error() allocates with malloc().

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d0b844c and 0abef31.

📒 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_cleanup to ensure resources are freed, addressing the previous review concern.


304-312: Cleanup block is functional.

The cleanup successfully frees allocated resources. Note that response is 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-required ok-package-test Run PR packaging tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants