[ZBX-20462] zabbix_trap_receiver.pl is missing an import Created: 2022 Jan 20  Updated: 2022 Jan 24  Resolved: 2022 Jan 24

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: None
Affects Version/s: 6.0.0beta3
Fix Version/s: None

Type: Incident report Priority: Trivial
Reporter: Robert Szulist Assignee: Zabbix Support Team
Resolution: Won't fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Steps to reproduce:

  1. Download script from  offcial repo 
  2. execute `perl zabbix_trap_receiver.pl`

Result:

The execution will fail with the following error:

 

$ perl zabbix_trap_receiver.pl
Undefined subroutine &NetSNMP::TrapReceiver::register called at zabbix_trap_receiver.pl line 112.
$ echo $?
255 

 

Expected:
The script should display "Loaded Zabbix SNMP trap receiver"

 

Resolution:

To resolve the issue, just `use` the proper module

--- zabbix_trap_receiver.pl     2022-01-20 08:04:53.009371733 +0000
+++ zabbix_trap_receiver_fixed.pl       2022-01-20 08:09:11.914492493 +0000
@@ -56,6 +56,7 @@ use Fcntl qw(O_WRONLY O_APPEND O_CREAT);
 use POSIX qw(strftime);
+use NetSNMP::TrapReceiver; sub zabbix_receiver
 { 


 Comments   
Comment by Oleksii Zagorskyi [ 2022 Jan 20 ]

You probably had to add the "use ..." line because you have custom installation of the library.

Normally you do not need the line and library is available for just initialization as it is now.

I'd close this case as cannot reproduce.

Comment by Andrejs Sitals (Inactive) [ 2022 Jan 21 ]

zalex_ua , what does "custom installation of the library" mean? I did "sudo apt install libsnmp-perl" on my dev box, and I have the same issue.

Even if in some specific setups it seems to be working without "use <package>", that's not the right way to use perl. All packages (or at least specific symbols from those packages) that are used should be imported. The code also uses constants NETSNMPTRAPD_HANDLER_FAIL and NETSNMPTRAPD_HANDLER_OK from the package which are treated as barewords without that "use" statement.

Adding "use strict;" and "use warnings;" at the top of every perl script/package also is highly advisable, it can help with finding some nasty issues before users find them.

@@ -1,5 +1,8 @@
 #!/usr/bin/env perl
 
+#use strict;
+#use warnings;
+
 #
 # Zabbix

I also don't see the "execute" permission being set on that script in git. How is this script usually executed?

Comment by Oleksii Zagorskyi [ 2022 Jan 21 ]

Let me add a note about strange thing discovered when I tested this case yesterday.
We have this lines order:

NetSNMP::TrapReceiver::register("all", \&zabbix_receiver) or
	die "failed to register Zabbix SNMP trap receiver\n";

print STDOUT "Loaded Zabbix SNMP trap receiver\n";

 and how the message is printed actually when "snmptrapd" is stopped, not when started:

Jan 21 15:58:02 it0 systemd[1]: Stopping Simple Network Management Protocol (SNMP) Trap Daemon....
Jan 21 15:58:02 it0 snmptrapd[2968004]: 2022-01-21 15:58:02 NET-SNMP version 5.9 Stopped.
Jan 21 15:58:02 it0 snmptrapd[2968004]: Stopping snmptrapd
Jan 21 15:58:02 it0 snmptrapd[2968004]: Loaded Zabbix SNMP trap receiver
Jan 21 15:58:02 it0 systemd[1]: snmptrapd.service: Deactivated successfully.
Jan 21 15:58:02 it0 systemd[1]: Stopped Simple Network Management Protocol (SNMP) Trap Daemon..

Jan 21 15:58:19 it0 systemd[1]: Started Simple Network Management Protocol (SNMP) Trap Daemon..
Jan 21 15:58:19 it0 snmptrapd[2968289]: Loaded the SNMPTT embedded snmptrapd handler
Jan 21 15:58:19 it0 snmptrapd[2968289]: NET-SNMP version 5.9 AgentX subagent connected
Jan 21 15:58:19 it0 snmptrapd[2968289]: NET-SNMP version 5.9

(empty line added between commands to indicate where I stop and then start the service)

Last lines in my /etc/snmp/snmptrapd.conf:

perl do "/usr/lib/snmptt/snmptthandler-embedded";
perl do "/my/bin/zabbix_trap_receiver.pl";

Yes, 2 handlers are loaded. Order of conf lines does not matter here - checked.

I do not think it was this was in the past, so maybe something changed in recent versions of "snmptrapd". This is on Ubuntu 21.10.
Current behavior may mislead.

Comment by Andrejs Sitals (Inactive) [ 2022 Jan 21 ]

From the description of the issue:

  1. execute `perl zabbix_trap_receiver.pl`

From zalex_ua comment:

Last lines in my /etc/snmp/snmptrapd.conf:
perl do "/my/bin/zabbix_trap_receiver.pl";

I guess that explains it - zabbix_trap_receiver.pl is not supposed to be used as a standalone script, it's meant to be used via snmptrapd.conf which probably takes care about importing the required module. That line in the snmptrapd.conf doesn't just execute perl, it is a "keyword" rather than an "executable" (it could be that perl isn't executed at all).

Here's some more technical info - https://manpages.debian.org/unstable/libsnmp-perl/NetSNMP::TrapReceiver.3pm.en.html

Generated at Mon Mar 17 09:42:37 EET 2025 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.