taraska90

zabbix_trap_receiver.pl

Apr 19th, 2016
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1.  
  2. ### Option: SNMPTrapperFile
  3. # Temporary file used for passing data to the server (or proxy). Must be the same
  4. # as in the server (or proxy) configuration file.
  5. #
  6. # Mandatory: yes
  7. # Default:
  8. $SNMPTrapperFile = '/var/log/snmptt/trap.log';
  9.  
  10. ### Option: DateTimeFormat
  11. # The date time format in strftime() format. Please make sure to have a corresponding
  12. # log time format for the SNMP trap items.
  13. #
  14. # Mandatory: yes
  15. # Default:
  16. $DateTimeFormat = '%H:%M:%S %Y/%m/%d';
  17.  
  18. ###################################
  19. #### ZABBIX SNMP TRAP RECEIVER ####
  20. ###################################
  21.  
  22. use Fcntl qw(O_WRONLY O_APPEND O_CREAT);
  23. use POSIX qw(strftime);
  24.  
  25. sub zabbix_receiver
  26. {
  27. my (%pdu_info) = %{$_[0]};
  28. my (@varbinds) = @{$_[1]};
  29.  
  30. # open the output file
  31. unless (sysopen(OUTPUT_FILE, $SNMPTrapperFile, O_WRONLY|O_APPEND|O_CREAT, 0666))
  32. {
  33. print STDERR "Cannot open [$SNMPTrapperFile]: $!\n";
  34. return NETSNMPTRAPD_HANDLER_FAIL;
  35. }
  36.  
  37. # get the host name
  38. my $hostname = $pdu_info{'receivedfrom'} || 'unknown';
  39. if ($hostname ne 'unknown') {
  40. $hostname =~ /\[(.*?)\].*/; # format: "UDP: [127.0.0.1]:41070->[127.0.0.1]"
  41. $hostname = $1 || 'unknown';
  42. }
  43.  
  44. # print trap header
  45. # timestamp must be placed at the beggining of the first line (can be omitted)
  46. # the first line must include the header "ZBXTRAP [IP/DNS address] "
  47. # * IP/DNS address is the used to find the corresponding SNMP trap items
  48. # * this header will be cut during processing (will not appear in the item value)
  49. printf OUTPUT_FILE "%s ZBXTRAP %s\n", strftime($DateTimeFormat, localtime), $hostname;
  50.  
  51. # print the PDU info
  52. print OUTPUT_FILE "PDU INFO:\n";
  53. foreach my $key(keys(%pdu_info))
  54. {
  55. printf OUTPUT_FILE " %-30s %s\n", $key, $pdu_info{$key};
  56. }
  57.  
  58. # print the variable bindings:
  59. print OUTPUT_FILE "VARBINDS:\n";
  60. foreach my $x (@varbinds)
  61. {
  62. printf OUTPUT_FILE " %-30s type=%-2d value=%s\n", $x->[0], $x->[2], $x->[1];
  63. }
  64.  
  65. close (OUTPUT_FILE);
  66.  
  67. return NETSNMPTRAPD_HANDLER_OK;
  68. }
  69.  
  70. NetSNMP::TrapReceiver::register("all", \&zabbix_receiver) or
  71. die "failed to register Zabbix SNMP trap receiver\n";
  72.  
  73. print STDOUT "Loaded Zabbix SNMP trap receiver\n";
Advertisement
Add Comment
Please, Sign In to add comment