Ad Widget

Collapse

Alternate SNMP trap processing script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • araw
    Member
    • May 2005
    • 31

    #1

    Alternate SNMP trap processing script




    Alternate script, processes traps and uses the trap OID in complete numerical format as the key ID. Using this you can have have traps individually per host, and thus set up triggers per hosts trap.

    That script in it's current form also connects to the MySQL database Zabbix is using, in order to verify the host exists, and the OID exists as an item for that host. You could of course remove this for a little added performance.

    Two arrays exist in order to filter out traps, either discard based on string values or trap OID.

    @discard_strings = ("Virtual-Access");
    @discard_oids = (".1.3.6.1.3.118.0.2");

    Yes you can do that in snmptrapd.conf, but at the moment I'd prefer to receive everything.

    You may also wish to remove the debugging output stuff. I've left it in for the time being because I'm still working on this. I'm releasing the script now so others can use it and improve it.

    Comments please ?

    [root@punk zabbix]# cat /etc/snmp/snmptrapd.conf
    # /etc/snmp/snmptrapd.conf

    traphandle default /usr/lib/zabbix/snmptrap.pl
    dontRetainLogs true

    # EOF
  • lorell
    Junior Member
    • Oct 2005
    • 14

    #2
    Did this ever get off the ground? The link didn't work when I tried it.

    Comment

    • maxlock
      Member
      • Mar 2005
      • 32

      #3
      Script from google cache......

      #!/usr/bin/perl

      # Colin Stubbs 2005
      #
      # snmptrap.pl - A script to process SNMP traps prior to delivery to
      # zabbix_server. Numeric OID becomes the Zabbix item key, the script
      # also checks the database to ensure the key exists first.

      use strict;
      use DBI;

      my($zabbix_server, $zabbix_port, $db_hostname, $db_port, $db_database,
      $db_user, $db_password, $dbh, $host_query, $dbrh, $target_host_id,
      $target_host, $target_key, $target_value, $zabbix_sender, $in_hostname,
      $in_ip, $in_trap_type, $in_uptime, $in_rest, $trap_type,
      $trap_type_oid, $trap_specific_value, $trap_specific_oid, @trap_rest,
      $target_host_item_id, $snmptranslate, $trap_type_oid_numeric,
      $trap_specific_oid_numeric, $trap_data, $count, $part, $trap_log,
      @discard_strings, @discard_oids, $tmp_string_1, $tmp_string_2);

      $snmptranslate = '/usr/bin/snmptranslate';

      $zabbix_server = '127.0.0.1';
      $zabbix_port = 10051;
      $zabbix_sender = '/usr/sbin/zabbix_sender';

      $db_hostname = 'localhost';
      $db_port = '3306';
      $db_database = 'zabbix';
      $db_user = 'zabbix';
      $db_password = 'zabbix';

      $trap_log = "/var/log/zabbix/snmptrap.log";

      @discard_strings = ("Virtual-Access");
      @discard_oids = (".1.3.6.1.3.118.0.2");

      $in_hostname = ;
      chomp($in_hostname);
      $in_ip = ;
      chomp($in_ip);
      $in_uptime = ;
      chomp($in_uptime);
      $in_trap_type = ;
      chomp($in_trap_type);
      $count = 0;
      while ()
      {
      if ($count < 3)
      {
      chomp($_);
      $in_rest = "$in_rest $_";
      $count++;
      }
      }

      $in_rest =~ s/\ //;

      if (open(LOG_OUTPUT, ">>${trap_log}"))
      {
      open(STDERR, ">&LOG_OUTPUT");
      open(STDOUT, ">&LOG_OUTPUT");
      }

      print STDERR "DEBUG: INPUT:\n'$in_hostname'\n'$in_ip'\n'$in_uptime'\n'$ in_trap_type'\n'$in_rest'\n";

      if (! defined($in_hostname)
      || ! defined($in_ip)
      || ! defined($in_trap_type)
      || ! defined($in_uptime))
      {
      print STDERR "ERROR: NULL input received\n";
      exit 1;
      }

      if ($in_ip !~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/)
      {
      print STDERR "ERROR: Invalid IP address received\n";
      exit 1;
      }

      ($trap_type, $trap_type_oid) = split(/\ /, $in_trap_type);
      @trap_rest = split(/\ /, $in_rest);

      foreach $tmp_string_1 (@trap_rest)
      {
      foreach $tmp_string_2 (@discard_strings)
      {
      if ($tmp_string_1 =~ /$tmp_string_2/)
      {
      print STDERR "ERROR: Discarded based on content '$tmp_string_1' =~ '$tmp_string_2'\n";
      exit 1;
      }
      }
      }

      # Translate to numeric OID's
      # easier to work since they do not change if extra MIB's are installed and
      # loaded by default
      $trap_type_oid_numeric = `$snmptranslate -O afbn $trap_type_oid 2>&1`;
      chomp($trap_type_oid_numeric);

      if ($trap_type_oid_numeric !~ /^\.[0-9]\.[0-9]/)
      {
      print STDERR "ERROR: Invalid trap OID '$trap_type_oid' received after translation\n";
      exit 1;
      }

      foreach $tmp_string_1 (@discard_oids)
      {
      if ($trap_type_oid_numeric =~ /^$tmp_string_1$/)
      {
      print STDERR "ERROR: Discarded based on OID '$tmp_string_1'\n";
      exit 1;
      }
      }

      $dbh = DBI->connect("DBI:mysql:$db_database:$db_hostname:$db_ port",
      $db_user, $db_password);

      if (defined($dbh))
      {
      $host_query = "SELECT hostid, host FROM hosts WHERE useip='1' AND ip='$in_ip' AND status='0';";
      $dbrh = $dbh->prepare("$host_query");
      $dbrh->execute;
      ($target_host_id, $target_host) = $dbrh->fetchrow();
      $dbrh->finish;

      if (defined($target_host))
      {
      print STDERR "OK: Hostname for '$in_ip' in Zabbix DB is '$target_host'\n";

      # SELECT itemid FROM items WHERE hostid='10010' AND type='2' AND status='0' AND key_='.1.3.6.1.6.3.1.1.5.3';
      $host_query = "SELECT itemid FROM items WHERE hostid='$target_host_id' AND type='2' AND status='0' AND key_='$trap_type_oid_numeric'";

      $dbrh = $dbh->prepare("$host_query");
      $dbrh->execute;
      ($target_host_item_id) = $dbrh->fetchrow();
      $dbrh->finish;

      if (defined($target_host_item_id))
      {
      print STDERR "OK: Item ID for '$in_ip', OID '$trap_type_oid_numeric' is '$target_host_item_id'\n";

      $count = 1;

      foreach $part (@trap_rest)
      {
      if ($count == 2)
      {
      $trap_data = "$part";
      }
      elsif ($count % 2 == 0)
      {
      $trap_data = "$trap_data $part";
      }

      $count++;
      }

      print STDERR "$zabbix_sender $zabbix_server $zabbix_port $target_host:$trap_type_oid_numeric '$trap_data'\n";
      system("$zabbix_sender $zabbix_server $zabbix_port $target_host:$trap_type_oid_numeric '$trap_data' 2>&1");
      }
      else
      {
      print STDERR "NO: No item configured for OID '$trap_type_oid_numeric'\n";
      }
      }
      else
      {
      print STDERR "ERROR: Failed to obtain hostname for IP '$in_ip'\n";
      }

      $dbh->disconnect;
      }
      else
      {
      print STDERR "ERROR: Failed to connect to database\n";
      exit 1;
      }



      # EOF

      Comment

      • tpetero
        Junior Member
        • Jan 2006
        • 1

        #4
        I had to add a few things

        I don't really know perl too well so when I was getting syntax errors running the script I changed:

        ....
        $in_hostname = ;
        chomp($in_hostname);
        $in_ip = ;
        chomp($in_ip);
        $in_uptime = ;
        chomp($in_uptime);
        $in_trap_type = ;
        chomp($in_trap_type);
        $count = 0;
        while ()
        {
        if ($count < 3)
        {
        .....

        To:

        ...

        $in_ip = <STDIN>;
        chomp($in_ip);
        $in_hostname = <STDIN>;
        chomp($in_hostname);
        $in_uptime = <STDIN>;
        chomp($in_uptime);
        $in_trap_type = <STDIN>;
        chomp($in_trap_type);
        $count = 0;
        while (<STDIN>)
        {
        if ($count < 3)
        {

        ....


        And it is working better for me, but I think it may still need to have the quotes stripped off because the zabbix_sender throws this:

        Usage: zabbix_sender <Zabbix server> <port> <server> <key> <value>
        If no arguments are given, zabbix_sender expects list of parameters
        from standard input.

        Comment

        Working...