Ad Widget

Collapse

SNMP-Trap Handling Howto

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 753713
    Senior Member
    • Apr 2006
    • 137

    #16
    mapping_method = mapfile or mapfile_ip without funktion

    Hi,

    I tested this script with mapping method hostname_nonfqdn and this script working fine.

    With mapping_method mapfile or mapfile_ip I have no funktion.

    Christian
    Last edited by 753713; 22-05-2008, 13:05.
    ______________________________
    Version : 2.0.x
    Server OS: Ubuntu 12.04
    Zabbix Servers: 12
    Monitored Windows Server: ~ 1000
    ______________________________

    Comment

    • onslo
      Junior Member
      • Sep 2005
      • 21

      #17
      I've got exactly the same problem as szettervall in that I can see the traps being sent to snmptrapd, but nothig is being sent to zabbix...

      daemon.log
      Code:
      Jul 24 14:32:23 debianZabbix snmptrapd[4947]: 2008-07-24 14:32:23 10.165.224.14(via UDP: [10.165.224.14]:53619) TRAP, SNMP v1, community public ^ISNMPv2-SMI::enterprises.9.9.43.2 Enterprise Specific Trap (1) Uptime: 131 days, 19:49:58.48 ^ISNMPv2-SMI::enterprises.9.9.43.1.1.6.1.3.130 = INTEGER: 1^ISNMPv2-SMI::enterprises.9.9.43.1.1.6.1.4.130 = INTEGER: 3^ISNMPv2-SMI::enterprises.9.9.43.1.1.6.1.5.130 = INTEGER: 2
      /usr/sbin/snmptrap.pl
      Code:
      #!/usr/bin/perl
      ############################################
      #
      # 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
      
      use constant TRUE => 1;
      use constant FALSE => 0;
      
      # configuration part
      
      # SNMP-Parsing
      
      $onlyinteresting = FALSE;          # Only send interesting trap informations
                                        # (the host's uptime, the trap's oid, community and SMI-information)
      
      # Zabbix-Server
      
      $zabbix_server = "10.165.10.10";    # 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 = "SNMP_Default"; # 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_method = "ip";
      $mapping_option = "";
      # Zabbix-Programs
      
      $path_to_zabbix = "/usr/sbin/";                     # Path to your zabbix-installation
      #$zabbix_sender = $path_to_zabbix."/sbin/zabbix_sender";    # Zabbix-Sender-prog
      $zabbix_sender = "/usr/bin/zabbix_sender";
      
      # 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;
      
              }
      
          }
      
      }
      /etc/snmp/snmptrapd.conf :
      Code:
      ###############################################################################
      #
      # EXAMPLE-trap.conf:
      #   An example configuration file for configuring the Net-SNMP snmptrapd agent.
      #
      ###############################################################################
      #
      # This file is intended to only be an example.  If, however, you want
      # to use it, it should be placed in /etc/snmp/snmptrapd.conf.
      # When the snmptrapd agent starts up, this is where it will look for it.
      #
      # All lines beginning with a '#' are comments and are intended for you
      # to read.  All other lines are configuration commands for the agent.
      
      #
      # PLEASE: read the snmptrapd.conf(5) manual page as well!
      #
      
      traphandle default /bin/bash /usr/sbin/snmptrap.pl
      Within Zabbix I have a host with the following details :
      Name : SNMP_Default
      IP Address : 0.0.0.0
      Port : 10050
      Link with Template : Template_Trapper

      The Template_Trapper has the following item configured :
      Description : snmptraps
      Type : Zabbix Trapper
      Key : snmptraps
      Type of information : Character
      Status : Active

      Can anybody see anything wrong with this ?
      Can anyone explain why the traps sent to snmptrapd are not being passed to Zabbix ?

      Incidently, if I execute this command then the information is displayed in Zabbix on the SNMP_Default host :

      Code:
      zabbix_sender -z 10.165.10.10 -p 10051 -s SNMP_Default -k snmptraps -o "testing"
      Please help !

      Thanks.
      Last edited by onslo; 24-07-2008, 16:09.

      Comment

      • ploeger
        Junior Member
        • Jun 2006
        • 24

        #18
        Originally posted by onslo
        traphandle default /bin/bash /usr/sbin/snmptrap.pl
        Is /usr/sbin/snmptrap.pl executable. Have you tried executing /usr/sbin/snmptrap.pl?

        And above the code you wrote /usr/sbin/snmptraps.pl (with an "s"). How is the name of the file?

        Regards
        Dennis

        Comment

        • onslo
          Junior Member
          • Sep 2005
          • 21

          #19
          Hi, thanks for replying...

          The file is called snmptrap. I have edited my original post to reflect this.

          The file is executable, I did "chmod 755" on it and ls -al shows it as executable.

          In case you need to know (and you probably do) this is a debian etch system running Zabbix 1.4.5 (from the Debian Lenny apt repos).

          To me it looks like snmptrapd is not calling the snmptrap.pl file at all, but I checked the startup script from snmpd and it shows the config file path for trapd as /etc/snmp/snmptrapd.conf just as expected.

          If i execute snmptrap.pl then nothing happens.... I get no output at all.

          Thanks
          Last edited by onslo; 24-07-2008, 16:08.

          Comment

          • onslo
            Junior Member
            • Sep 2005
            • 21

            #20
            OK, I fixed it

            replace /bin/bash in snmptrapd.conf with perl

            ie :

            Code:
            traphandle default perl /usr/sbin/snmptrap.pl

            Comment

            • ury20
              Junior Member
              • Oct 2008
              • 2

              #21
              napfile_ip

              Did anybody got the script working with mapfile_ip option?
              I played around with adding "print LOG ..." and I can see that script stops (or exits) after "close (MAP);" line.
              I love this script but I'm no programmer and I don't know how to debug this any further. So if anybody has some idea I would be really thankful.

              Comment

              • ploeger
                Junior Member
                • Jun 2006
                • 24

                #22
                Hi ury20!

                What do you mean? Does the script really stops? Or does it produce an error or unexpected results?

                Thanks.

                Kind regards

                Dennis

                Comment

                • ury20
                  Junior Member
                  • Oct 2008
                  • 2

                  #23
                  It actually does nothing after this line. No error messages.As I see (from log file) it it opens the map file, reads it and then closes it. When the if block for checking the map method should start I see nothing else. I added the print LOG line to see what $map and $zabbix_host would be and nothing gets written to log file. So I presume that script stops here (it is nothing written for later actions also).
                  I hope this makes any sense as I'm really no programmer (I just written som VB and sh scripts until now).
                  Can I add something to get eventual error printed to log file?

                  Oh ... my system is:
                  CentOS5 and Zabbix 1.4.4

                  Comment

                  • skullone
                    Member
                    • Mar 2007
                    • 46

                    #24
                    Theres a cleaner way to handle SNMP traps, and you can have the traps translated from OID's into plain english so you can write triggers for many items, without knowing every single OID to watch for:



                    Shameless plug

                    A modification you could also make, is to send the Host variable, instead of Default_Trapper, you can send it to the correct host using my method.
                    Just make sure triggers are templated at that point though.

                    Comment

                    • falbert
                      Junior Member
                      • Nov 2008
                      • 1

                      #25
                      snmptrapd centos 5.2 SELinix

                      I spent three days trying, snmptrapd worked from command line but didn't on normal mode (/etc/init.d/snmptrapd start). Finally the problem was related to audit, I change the security level to permisive and its running.

                      Anybody change domain/transitions for SELinux?

                      Regards,
                      Fernando.


                      Originally posted by ploeger
                      Hi there!

                      I've struggled with configuring zabbix 1.4.4 for snmp trap handling. For all you out there dealing with the same thoughts, have a look at this brand-new documentation in the wiki:

                      http://www.zabbix.com/wiki/doku.php?id=howto:snmptraps

                      Have fun.

                      Dennis

                      Comment

                      • axterics
                        Junior Member
                        • Jan 2009
                        • 12

                        #26
                        If you have spaces in wildcard-host like in the "ZABBIX Server" default HOST the script doesn't work.

                        Code:
                        $mycommand = $command." --host $zabbix_host --key $item --value \"$str\"";
                        should be changed in

                        Code:
                        $mycommand = $command." --host \"$zabbix_host\" --key $item --value \"$str\"";

                        Comment

                        • Robert Wagnon
                          Member
                          • Jan 2008
                          • 47

                          #27
                          snmptrap.pl for v1.6.x

                          #!/usr/bin/perl
                          ############################################
                          #
                          # snmptrap.pl
                          # Version 1.0
                          # Author: Dennis Ploeger <[email protected]>
                          #
                          # 2008-03-12 P.Hoffmann : Add debugging Log
                          # 2009-03-05 R.Wagnon : Modify zabbix_server error handling for 1.6.x
                          ############################################
                          #
                          # This Script has been designed for Zabbix 1.6
                          # 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, communit
                          y 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 = "snmptrap-catchall"; # Hostname or IP-Adress of wildcard-host w
                          ithin 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 you
                          r zabbix host
                          # Sadly, zabbix_sender only accept the configured name of the host in zabbix, no
                          t 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 "<h
                          ostname>:<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";
                          $mapping_option = "";

                          # Zabbix-Programs

                          $path_to_zabbix = "/home/zabbix"; # Path to your zabbix-inst
                          allation
                          $zabbix_sender = $path_to_zabbix."/bin/zabbix_sender"; # Zabbix-Sender-prog
                          $log = "/var/log/snmptrapper.log";

                          # End of configuration section

                          # main()

                          # creating the log file
                          open(LOG,">>".$log) || die("Log file openning error !");
                          print LOG "snmptraphandler for Zabbix (". localtime(time).")\n";

                          # 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. Quitti
                          ng.\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;

                          # Get the snmp trap message
                          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 = $str."$oid: $value\n";
                          $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;
                          print LOG "$return\n";

                          if ($1 > 0) {
                          if ($zabbix_host ne $wildcard_host) {
                          #print STDERR "Failed to send item to host. Sending it to wildca
                          rd 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 $st
                          r using command $mycommand\n";
                          print LOG "[". localtime(time)."] Exiting ...\n";
                          close LOG;
                          exit 1;
                          }
                          } else {
                          print LOG "[". localtime(time)."] Sending using command $mycomma
                          nd was OK :!\n";
                          print LOG "[". localtime(time)."] Exiting ...\n";
                          close LOG;
                          exit 0;
                          }
                          }
                          #-------------------------------------------------------------------------------
                          -
                          #Last edited by Robert Wagnon
                          Last edited by Robert Wagnon; 06-03-2009, 16:33.

                          Comment

                          • prequejo
                            Junior Member
                            • Feb 2007
                            • 3

                            #28
                            Hi,

                            I have the same problem using mapfile_ip option. I tried to debug the script with the following result:


                            Unmatched [ in regex; marked by <-- HERE in m/(:[ <-- HERE ^5.008005*)$/ at snmptrap.pl line 123

                            The line 123 is:

                            $map =~ /($ipaddress:[^$]*)$/gi;

                            I'm not a programmer and I can't to understand this perl expression.

                            Can anybody help me?

                            Regards.


                            Originally posted by ury20
                            It actually does nothing after this line. No error messages.As I see (from log file) it it opens the map file, reads it and then closes it. When the if block for checking the map method should start I see nothing else. I added the print LOG line to see what $map and $zabbix_host would be and nothing gets written to log file. So I presume that script stops here (it is nothing written for later actions also).
                            I hope this makes any sense as I'm really no programmer (I just written som VB and sh scripts until now).
                            Can I add something to get eventual error printed to log file?

                            Oh ... my system is:
                            CentOS5 and Zabbix 1.4.4

                            Comment

                            • roejice
                              Junior Member
                              • Mar 2007
                              • 2

                              #29
                              I had the same problem. I fixed it by changing that line to be:

                              Code:
                              $map =~ /$ipaddress:(.*)$/gi;
                              Good luck.

                              Originally posted by prequejo
                              Hi,

                              I have the same problem using mapfile_ip option. I tried to debug the script with the following result:


                              Unmatched [ in regex; marked by <-- HERE in m/(:[ <-- HERE ^5.008005*)$/ at snmptrap.pl line 123

                              The line 123 is:

                              $map =~ /($ipaddress:[^$]*)$/gi;

                              I'm not a programmer and I can't to understand this perl expression.

                              Can anybody help me?

                              Regards.

                              Comment

                              • prequejo
                                Junior Member
                                • Feb 2007
                                • 3

                                #30
                                Thank you, roejice :-)

                                Comment

                                Working...