Network OS
Network OS

Can I create a custom trap?

by Administrator Administrator on ‎02-01-2016 02:08 PM - edited 2 weeks ago by Administrator Administrator
02.01.16   |   02:08 PM

Question

Can I create a custom trap?

Answer

Yes, you can use the jnxEventTrap event script to create customized traps as needed.

 

In the following example, a Junos OS operations (op) script is triggered when a UI_COMMIT_NOT_CONFIRMED event is received. The Junos OS op script example matches the complete message of the event and generates an SNMP trap.

 

version 1.0;

ns junos ="http://xml.juniper.net/junos/*/junos";

ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";

ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

 

param $event;

param $message;

 

match / {

 

  /*

  * trapm utilty wants the following characters in the value to be escaped

  * '[', ']', ' ', '=', and ','

  */

 var $event-escaped = {

    call escape-string($text = $event, $vec = '[] =,');

  }

  var $message-escaped = {

    call escape-string($text = $message, $vec = '[] =,');

  }

    <op-script-results> {

  var $rpc = <request-snmp-spoof-trap> {

    <trap> "jnxEventTrap";

    <variable-bindings> "jnxEventTrapDescr[0]='Event-Trap' , "

  _ "jnxEventAvAttribute[1]='event' , "

  _ "jnxEventAvValue[1]='" _ $event-escaped _ "' , "

  _ "jnxEventAvAttribute[2]='message' , "

  _ "jnxEventAvValue[1]='" _ $message-escaped _ "'";

    }

  var $res = jcs:invoke($rpc);

    }

}

template escape-string ($text, $vec) {

    if (jcs:empty($vec)) {

      expr $text;

   } else {

      var $index = 1;

      var $from = substring($vec, $index, 1);

      var $changed-value = {

        call replace-string($text, $from) {

          with $to = {

            expr "\\";

            expr $from;

          }

        }

      }

      call escape-string($text = $changed-value, $vec = substring($vec, $index

+ 1));

   }

}

template replace-string ($text, $from, $to) {

  if (contains($text, $from)) {

      var $before = substring-before($text, $from);

      var $after = substring-after($text, $from);

      var $prefix = $before _ $to;

 

      expr $before;

      expr $to;

      call replace-string($text = $after, $from, $to);

  } else {

      expr $text;

  }

}

After creating your customized trap, you must configure a policy on your device to tell the device what actions to take after it receives the trap.

 

Here is an example of a configured policy under the [edit event-options] hierarchy:

 

[edit event-options]

user@host> show

policy trap-on-event {

  events UI_COMMIT_NOT_CONFIRMED;

  attributes-match {

    UI_COMMIT_NOT_CONFIRMED.message matches complete;

  }

  then {

    event-script ev-syslog-trap.junos-op {

       arguments {

         event UI_COMMIT_NOT_CONFIRMED;

         message "{$$.message}";

       }

    }

  }

}

 

For more information, click SNMP MIBs and Traps.