]> git.uio.no Git - uio-zabbix.git/blame - zabbix_trap_receiver.pl
Add scripts used to get monitor Wxgoos sensors
[uio-zabbix.git] / zabbix_trap_receiver.pl
CommitLineData
eda74f02
CMB
1#!/usr/bin/perl
2
3#
4# Zabbix
5# Copyright (C) 2001-2014 Zabbix SIA
6#
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.
11#
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.
16#
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.
20#
21
22#########################################
23#### ABOUT ZABBIX SNMP TRAP RECEIVER ####
24#########################################
25
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.
29#
30# Read more about using embedded perl with Net-SNMP:
31# http://net-snmp.sourceforge.net/wiki/index.php/Tut:Extending_snmpd_using_perl
32
33#################################################
34#### ZABBIX SNMP TRAP RECEIVER CONFIGURATION ####
35#################################################
36
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.
40#
41# Mandatory: yes
42# Default:
43$SNMPTrapperFile = '/var/log/zabbixtraps/zabbix_traps.tmp';
44
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.
48#
49# Mandatory: yes
50# Default:
51$DateTimeFormat = '%H:%M:%S %Y/%m/%d';
52
53###################################
54#### ZABBIX SNMP TRAP RECEIVER ####
55###################################
56
57use Fcntl qw(O_WRONLY O_APPEND O_CREAT);
58use POSIX qw(strftime);
59use Socket;
60
61sub zabbix_receiver
62{
63 my (%pdu_info) = %{$_[0]};
64 my (@varbinds) = @{$_[1]};
65
66 # open the output file
67 unless (sysopen(OUTPUT_FILE, $SNMPTrapperFile, O_WRONLY|O_APPEND|O_CREAT, 0666))
68 {
69 print STDERR "Cannot open [$SNMPTrapperFile]: $!\n";
70 return NETSNMPTRAPD_HANDLER_FAIL;
71 }
72
73 # get the host name
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';
78 }
79
80 # Reverse DNS lookup
81 $hostname = gethostbyaddr(inet_aton($hostname), AF_INET) or $hostname = `hostname`;
82
83 # print trap header
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;
89
90 # print the PDU info
91 print OUTPUT_FILE "PDU INFO:\n";
92 foreach my $key(keys(%pdu_info))
93 {
94 printf OUTPUT_FILE " %-30s %s\n", $key, $pdu_info{$key};
95 }
96
97 # print the variable bindings:
98 print OUTPUT_FILE "VARBINDS:\n";
99 foreach my $x (@varbinds)
100 {
101 printf OUTPUT_FILE " %-30s type=%-2d value=%s\n", $x->[0], $x->[2], $x->[1];
102 }
103
104 close (OUTPUT_FILE);
105
106 return NETSNMPTRAPD_HANDLER_OK;
107}
108
109NetSNMP::TrapReceiver::register("all", \&zabbix_receiver) or
110 die "failed to register Zabbix SNMP trap receiver\n";
111
112print STDOUT "Loaded Zabbix SNMP trap receiver\n";