Ad Widget

Collapse

Problem with snmptrap and zabbix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • knight2087
    Junior Member
    • Dec 2009
    • 3

    #1

    Problem with snmptrap and zabbix

    hello,
    I have a problem to receive trap and add it to zabbix for some week ago!
    The problem is that i receive trap from equipment ( i can see that in /var/log/messages ), when this trap is received the script snmptrap.pl is running correctly but at the end nothing appear in zabbix web page.

    This is host config:

    this is item config on this host :


    and this is my snmptrap script:
    Code:
     cat /etc/snmp/snmptrapd.conf
    # Example configuration file for snmptrapd
    #
    # No traps are handled by default, you must edit this file!
    #
    # authCommunity   log,execute,net public
    disableAuthorization yes
    # traphandle default /bin/bash /etc/zabbix/bin/snmptrap.sh
    traphandle default perl /etc/zabbix/bin/snmptrap.pl
    
    cat /etc/zabbix/bin/snmptrap.pl
    
    # Version 1.0
    # Author: Dennis Ploeger <[email protected]>
    #
    ############################################
    #
    # 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
    touch /tmp/bidondon ;
    
    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 = "defaultTrapperHost"; # Hostname or IP-Adress of wildcard-host within zabbix
    $alltowildcard = TRUE;           # 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/sbin";                     # Path to your zabbix-installation
    $zabbix_sender = $path_to_zabbix."/zabbix_sender";    # Zabbix-Sender-prog
    $log = "/tmp/snmptraper.log";
    
    open(LOG,">>".$log) || die("Log file openning error !");
    print LOG "snmptraphandler for Zabbix (". localtime(time).")\n";
    
    
    # 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;
    
    }
    print LOG "[". localtime(time)."] zabbix_host= $zabbix_host\n";
    
    $ipaddress =~ /.*\[([^]]*)\].*/;
    $ipaddress = $1;
    
    $command = $zabbix_sender." --zabbix-server ".$zabbix_server." --port ".$zabbix_port;
    
    while(<STDIN>) {
    
        ($oid, $value) = /([^\s]+)\s+(.*)/;
     print LOG "[". localtime(time)."] OID: $oid\n";
        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;
     }
    print LOG "[". localtime(time)."] str= $str\n";
        print LOG "[". localtime(time)."] command= $command\n";
    
        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";
     print LOG "[". localtime(time)."] ERROR: Failed to send item to host ($zabbix_host). Sending it to wildcard host instead.\n ;
                    $zabbix_host = $wildcard_host;
    
                } else {
    
                    print STDERR "Failed to send item $str using command $mycommand\n";
     print LOG "[". localtime(time)."] ERROR: Failed to send item $str using command $mycommand\n";
                    print LOG "[". localtime(time)."] Exiting ...\n";
                    close LOG;
    
                    exit 1;
    
                }
    
            } else {
     print LOG "[". localtime(time)."] Sendind using command $mycommand was OK :!\n";
                    print LOG "[". localtime(time)."] Exiting ...\n";
                    close LOG;
    
                exit 0;
    
            }
    
        }
    do you have idea to resolve my problem, TX
    Last edited by knight2087; 18-12-2009, 17:37.
  • knight2087
    Junior Member
    • Dec 2009
    • 3

    #2
    I have resolv the problem:

    i change the script to snmptrap.sh and i modify the end of the script
    Code:
    cat /etc/zabbix/bin/snmptrap.sh
    #!/bin/bash
    
    #
    # Zabbix
    # Copyright (C) 2000,2001,2002,2003 Alexei Vladishev
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; either version 2 of the License, or
    # (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    #
    
    # CONFIGURATIONr
    touch /tmp/bidon
    ZABBIX_SERVER="localhost";
    ZABBIX_PORT="10051";
    
    ZABBIX_SENDER="/usr/sbin/zabbix_sender";
    
    KEY="snmptraps";
    HOST="defaultTrapperHost";
    
    # END OF CONFIGURATION
    
    read hostname
    read ip
    read uptime
    read oid
    read address
    read community
    read enterprise
    
    oid=`echo $oid|cut -f2 -d' '`
    address=`echo $address|cut -f2 -d' '`
    community=`echo $community|cut -f2 -d' '`
    enterprise=`echo $enterprise|cut -f2 -d' '`
    
    oid=`echo $oid|cut -f11 -d'.'`
    community=`echo $community|cut -f2 -d'"'`
    
    str="$hostname $address $community $enterprise $oid"
    
    $ZABBIX_SENDER -z $ZABBIX_SERVER -p $ZABBIX_PORT -s $HOST -k $KEY -o "$str"
    this is the solution to my problem $ZABBIX_SENDER -z $ZABBIX_SERVER -p $ZABBIX_PORT -s $HOST -k $KEY -o "$str"

    Comment

    Working...