5 # Copyright (C) 2001-2014 Zabbix SIA
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #########################################
23 #### ABOUT ZABBIX SNMP TRAP RECEIVER ####
24 #########################################
26 # This is an embedded perl SNMP trapper receiver designed for sending data to the server.
27 # The receiver will pass the received SNMP traps to Zabbix server or proxy running on the
28 # same machine. Please configure the server/proxy accordingly.
30 # Read more about using embedded perl with Net-SNMP:
31 # http://net-snmp.sourceforge.net/wiki/index.php/Tut:Extending_snmpd_using_perl
33 #################################################
34 #### ZABBIX SNMP TRAP RECEIVER CONFIGURATION ####
35 #################################################
37 ### Option: SNMPTrapperFile
38 # Temporary file used for passing data to the server (or proxy). Must be the same
39 # as in the server (or proxy) configuration file.
43 $SNMPTrapperFile = '/var/log/zabbixtraps/zabbix_traps.tmp';
45 ### Option: DateTimeFormat
46 # The date time format in strftime() format. Please make sure to have a corresponding
47 # log time format for the SNMP trap items.
51 $DateTimeFormat = '%H:%M:%S %Y/%m/%d';
53 ###################################
54 #### ZABBIX SNMP TRAP RECEIVER ####
55 ###################################
57 use Fcntl qw(O_WRONLY O_APPEND O_CREAT);
58 use POSIX qw(strftime);
63 my (%pdu_info) = %{$_[0]};
64 my (@varbinds) = @{$_[1]};
66 # open the output file
67 unless (sysopen(OUTPUT_FILE, $SNMPTrapperFile, O_WRONLY|O_APPEND|O_CREAT, 0666))
69 print STDERR "Cannot open [$SNMPTrapperFile]: $!\n";
70 return NETSNMPTRAPD_HANDLER_FAIL;
74 my $hostname = $pdu_info{'receivedfrom'} || 'unknown';
75 if ($hostname ne 'unknown') {
76 $hostname =~ /\[(.*?)\].*/; # format: "UDP: [127.0.0.1]:41070->[127.0.0.1]"
77 $hostname = $1 || 'unknown';
81 $hostname = gethostbyaddr(inet_aton($hostname), AF_INET) or $hostname = `hostname`;
84 # timestamp must be placed at the beggining of the first line (can be omitted)
85 # the first line must include the header "ZBXTRAP [IP/DNS address] "
86 # * IP/DNS address is the used to find the corresponding SNMP trap items
87 # * this header will be cut during processing (will not appear in the item value)
88 printf OUTPUT_FILE "%s ZBXTRAP %s\n", strftime($DateTimeFormat, localtime), $hostname;
91 print OUTPUT_FILE "PDU INFO:\n";
92 foreach my $key(keys(%pdu_info))
94 printf OUTPUT_FILE " %-30s %s\n", $key, $pdu_info{$key};
97 # print the variable bindings:
98 print OUTPUT_FILE "VARBINDS:\n";
99 foreach my $x (@varbinds)
101 printf OUTPUT_FILE " %-30s type=%-2d value=%s\n", $x->[0], $x->[2], $x->[1];
106 return NETSNMPTRAPD_HANDLER_OK;
109 NetSNMP::TrapReceiver::register("all", \&zabbix_receiver) or
110 die "failed to register Zabbix SNMP trap receiver\n";
112 print STDOUT "Loaded Zabbix SNMP trap receiver\n";