Skip to content

NAS-140368 / 27.0.0-BETA.1 / Add SNMP trap C extension using net-snmp#1

Open
ixhamza wants to merge 2 commits intomasterfrom
NAS-140368
Open

NAS-140368 / 27.0.0-BETA.1 / Add SNMP trap C extension using net-snmp#1
ixhamza wants to merge 2 commits intomasterfrom
NAS-140368

Conversation

@ixhamza
Copy link
Copy Markdown
Member

@ixhamza ixhamza commented Mar 26, 2026

Summary

Replace pysnmp with a native C extension for sending SNMP trap notifications. Uses net-snmp's libnetsnmp for session management, PDU construction, and SNMPv3 USM authentication.

Exposes send_alert(), send_alert_cancellation(), and get_engine_id(). OIDs derived from TRUENAS-MIB notification section.

Testing

Tested with snmptrapd (net-snmp 5.9.4) as trap receiver:

v2c:

  • Basic alert/cancellation with community string
  • All 7 alert levels (info through emergency)
  • Validation errors (missing host/port/community/alert_id/level/message)
  • Edge cases (unicode, long messages, empty messages, special chars)
  • 100-trap stress test

v3:

  • MD5 + DES (authPriv)
  • SHA + AES (authPriv)
  • MD5 authNoPriv
  • SHA authNoPriv
  • noAuthNoPriv
  • Protocol switching within same process (MD5↔SHA, auth↔noauth, v2c↔v3)
  • Wrong auth key correctly rejected by receiver
  • Correct credentials work after wrong credentials
  • Engine ID stable across calls, changes per process

Verified against upstream:

  • Trap PDU structure matches snmptrap CLI output
  • v3 session setup follows snmptrap.c approach
  • USM user table handling tested for credential switching

Next steps

  • Middleware integration to follow to wire into snmp_trap.py and replace pysnmp
  • Add to build system

@ixhamza ixhamza requested a review from a team March 26, 2026 20:08
@bugclerk bugclerk changed the title Add SNMP trap C extension using net-snmp NAS-140368 / 27.0.0-BETA.1 / Add SNMP trap C extension using net-snmp Mar 26, 2026
@bugclerk
Copy link
Copy Markdown
Contributor

@anodos325
Copy link
Copy Markdown

anodos325 commented Mar 27, 2026

Can we add a github runner pipeline that minimally builds the debian package and runs mypy stub tests?

https://github.com/truenas/truenas_pyos/tree/master/.github/workflows

For example. Though in this case we probably don't need to use a qemu VM.

I think a tests dir with some basic pytest tests might also be useful.

@ixhamza
Copy link
Copy Markdown
Member Author

ixhamza commented Mar 27, 2026

@anodos325 - Yeah, that's a very good point. Will add.

ixhamza added 2 commits March 27, 2026 22:35
Replace pysnmp with a native C extension for sending SNMP trap
notifications. Uses net-snmp's libnetsnmp for session management,
PDU construction, and SNMPv3 USM authentication.

Exposes send_alert(), send_alert_cancellation(), and get_engine_id()
to Python. Supports SNMPv2c (community), SNMPv3 authNoPriv (MD5/SHA),
and SNMPv3 authPriv (DES/AES). OIDs derived from TRUENAS-MIB
notification section.
CI pipeline builds debian package, runs mypy stubtest for stub
validation, and pytest for argument validation and constants.

Tests cover validation errors, engine ID, and constants sync with the C
module. Network/receiver tests are not included as they require a
running snmptrapd instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants