#!/usr/bin/perl ############################################ # # snmptrap.pl # Version 1.0 # Author: Dennis Ploeger <develop@dieploegers.de> # ############################################ # # This Script has been designed for Zabbix 1.4.4 # I don't know, if it's working with older versions, but I don't suppose so. # # Description: # A better snmp trap-handler for zabbix. To use it, add a SNMP Trapper item # (character) to any host you would like to receive traps for. # After that, create a wildcard-host with a trapper item that will receive # all snmptraps for non-existent hosts. # # To use it, add this script to the snmptrapd.conf: # # traphandle default /bin/bash {zabbix-bin-path}/snmptrap.pl use constant TRUE => 1; use constant FALSE => 0; # configuration part # SNMP-Parsing $onlyinteresting = TRUE; # Only send interesting trap informations # (the host's uptime, the trap's oid, community and SMI-information) # Zabbix-Server $zabbix_server = "localhost"; # Hostname/IP-Address of zabbix-server $zabbix_port = 10051; # Port of zabbix-server (Default: 10051) $item = "snmptraps"; # Item to save snmptraps to # Wildcard-Host $wildcard_host = "<CONFIGUREME>"; # Hostname or IP-Adress of wildcard-host within zabbix $alltowildcard = FALSE; # Send all traps to wildcard host # Mapping of hostname/ip-address to zabbix hostname # Configure, how the hostname or ipaddress from the trap should be mapped to your zabbix host # Sadly, zabbix_sender only accept the configured name of the host in zabbix, not it's hostname # or ipaddress. Hopefully this will change in future versions. # For now, you have the following possibilities to map the real hostname or the ip address gathered # from the trap to your zabbix host by configuring "mapping_method" and possibly the "mapping_option": # # mapping_method: # "hostname" - just use the received hostname from the trap # "ip" - just use the received ip address from the trap # "hostname_nonfqdn" - extract the hostname from the fqdn hostname from the trap and use that one # "mapfile" - use the contents of the mapfile given in "mapping_option" as a "<hostname>:<zabbix-host>"-map # "mapfile_ip" - same as "mapfile", but use a "<ip-address>:<zabbix-host>"-map # "regexp" - use the first group of the regexp in "mapping_option" as the zabbix-host # "regexp_ip" - like "regexp", but use the ipaddress information $mapping_method = "hostname_nonfqdn"; $mapping_option = ""; # Zabbix-Programs $path_to_zabbix = "/usr/local/zabbix"; # Path to your zabbix-installation $zabbix_sender = $path_to_zabbix."/sbin/zabbix_sender"; # Zabbix-Sender-prog # End of configuration section # main() # hostname_nonfqdn is basically a simple regexp if ($mapping_method eq "hostname_nonfqdn") { $mapping_method = "regexp"; $mapping_option = qr/^([^\.]*)\..*$/i; } # gather hostname and ip address $hostname = <STDIN>; chomp($hostname); $ipaddress = <STDIN>; chomp($ipaddress); # map the hostname or the ip address to the zabbix host if (!$alltowildcard) { if ($mapping_method eq "hostname") { $zabbix_host = $hostname; } elsif ($mapping_method eq "ip") { $zabbix_host = $ipaddress; } elsif ($mapping_method eq "regexp") { $hostname =~ $mapping_option; $zabbix_host = $1; } elsif ($mapping_method eq "regexp_ip") { $ipaddress =~ $mapping_option; $zabbix_host = $1; } elsif ($mapping_method =~ /^mapfile/) { # Load mapping file if (! -r $mapping_option) { print STDERR "Mapping file ($mapping_option) is not readable. Quitting.\n"; exit 1; } open (MAP, "<$mapping_option"); $map = join("\n",<MAP>); close(MAP); if ($mapping_method eq "mapfile") { $map =~ /^($hostname:[^$]*)\n/gi; $zabbix_host = $1; } elsif ($mapping_method eq "mapfile_ip") { $map =~ /($ipaddress:[^$]*)$/gi; $zabbix_host = $1; } } } else { $zabbix_host = $wildcard_host; } $ipaddress =~ /.*\[([^]]*)\].*/; $ipaddress = $1; $command = $zabbix_sender." --zabbix-server ".$zabbix_server." --port ".$zabbix_port; while(<STDIN>) { ($oid, $value) = /([^\s]+)\s+(.*)/; if ($onlyinteresting) { # Filter out uninteresting trap informations if (($oid !~ /sysuptimeinstance/i) && ($oid !~ /snmpv2-smi/i) && ($oid !~ /snmptrapoid/i) && ($oid !~ /snmptrapcommunity/i) ) { next; } } $str = "$oid: $value"; $str =~ s/"/\\"/gi; while(1) { if ($zabbix_host eq $wildcard_host) { $str = "($hostname, $ipaddress) ".$str; } $mycommand = $command." --host $zabbix_host --key $item --value \"$str\""; $return = `$mycommand`; $return =~ /.*failed: ([0-9]*);.*/gi; if ($1 > 0) { if ($zabbix_host ne $wildcard_host) { print STDERR "Failed to send item to host. Sending it to wildcard host instead.\n"; $zabbix_host = $wildcard_host; } else { print STDERR "Failed to send item $str using command $mycommand\n"; exit 1; } } else { exit 0; } } }
Discussion
This script has a lot of errors and the default is not properly stationed with v 1.8.1 I have corrected the errors and improve it. I can send it here.
hello zalex_ua
I also interested on your script! Can you publish here? Regards
zalex_ua,
I'm very interested in this script. can you post your fixed version and instruction on how to use it? Thanks
Wait please. I still improve a script and will publish next week a new article. Also I will publish a powerful script for processing syslog. Example:http://img683.imageshack.us/img683/3268/syslog.png