Friday, March 14, 2014

How to pass an argument from an Event to a script - Juniper scripting

If you want to know more about how arguments are passed to a script using an event option, you can use this event script.

When an Event happens, such as when an interface goes down, a syslog message will be generated. Some information contained within the syslog message can be passed to a script.

My script will create a text file called "interface_down.txt" and writes the passed argument which is name of the interface that went down and the timestamp of when it occurred.

To learn more about what arguments can be passed during an event such as a LINK DOWN you need to look more into the details of the syslog:

jnpr@R1> help syslog SNMP_TRAP_LINK_DOWN    
Name:          SNMP_TRAP_LINK_DOWN
Message:       ifIndex <snmp-interface-index>, ifAdminStatus <admin-status>, ifOperStatus <operational-status>, ifName
               <interface-name>
Help:          linkDown trap was sent
Description:   The SNMP agent process (snmpd) generated a linkDown trap because the indicated interface changed state to
               'down'.
Type:          Event: This message reports an event, not an error
Severity:      warning
Facility:      LOG_DAEMON


Note the Message and <objects>. These are the arguments that can be passed. Also not that the type is "Event" and not an error.

The following configuration should be added to the router:


[edit]

user@router# show event-options

policy track_interface {

    events SNMP_TRAP_LINK_DOWN;

    then {

        event-script check-interface.slax {

            arguments {

                interface-name "{$SNMP_TRAP_LINK_DOWN.interface-name}";

            }

        }

    }

}

event-script {

    traceoptions {

        flag output;

    }

    file check-interface.slax;

}

Note that the syslog message $SNMP_TRAP_LINK_DOWN followed by the "." and the message <object> is needed.

Below is the output:

-------------------------------------

[edit]

user@router# run show interfaces terse

Interface               Admin Link Proto    Local                 Remote

ge-0/0/0                up    down

ge-1/0/0                up    up

ge-1/0/0.0              up    up   inet     10.1.1.1/24

                                   mpls

dsc                     up    up

fxp0                    up    up

fxp0.0                  up    up   inet     172.19.90.187/23

fxp1                    up    up

fxp1.0                  up    up   inet     10.0.0.4/8

                                            128.0.0.4/2

                                   inet6    fe80::200:ff:fe00:4/64

                                            fec0::a:0:0:4/64

                                   tnp      0x4

gre                     up    up

ipip                    up    up

lo0                     up    up

lo0.0                   up    up   inet     1.1.1.1/24

lo0.16384               up    up   inet     127.0.0.1           --> 0/0

lo0.16385               up    up   inet

lsi                     up    up

mtun                    up    up

pimd                    up    up

pime                    up    up

tap                     up    up



/* I manually disconnect the interface*/ 



[edit]

user@router# run file show /var/tmp/interface_down.txt

<?xml version="1.0"?>

Following interface: ge-1/0/0 went down on Fri Mar 14 11:55:43 2014


[edit]

user@router# run show interfaces terse

Interface               Admin Link Proto    Local                 Remote

ge-0/0/0                up    down

ge-1/0/0                up    down

ge-1/0/0.0              up    down inet     10.1.1.1/24

                                   mpls

dsc                     up    up

fxp0                    up    up

fxp0.0                  up    up   inet     172.19.90.187/23

fxp1                    up    up

fxp1.0                  up    up   inet     10.0.0.4/8

                                            128.0.0.4/2

                                   inet6    fe80::200:ff:fe00:4/64

                                            fec0::a:0:0:4/64

                                   tnp      0x4

gre                     up    up

ipip                    up    up

lo0                     up    up

lo0.0                   up    up   inet     1.1.1.1/24

lo0.16384               up    up   inet     127.0.0.1           --> 0/0

lo0.16385               up    up   inet

lsi                     up    up

mtun                    up    up

pimd                    up    up

pime                    up    up

tap                     up    up



Then you can use the argument passed to the script that is specific to the actual event. 

below is the event script file, it needs to be placed in /var/db/scripts/event folder 

-----------------

jnpr@R1> file show /var/db/scripts/event/check-interface.slax 
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";
ns ext = "http://xmlsoft.org/XSLT/namespace";
ns redirect extension  = "org.apache.xalan.xslt.extensions.Redirect";

import "../import/junos.xsl";

var $arguments ={
<argument>{
<name> "interface-name";
<description> "Interface that has been disabled";
   }
}


param $interface-name;

match /
{

   <event-script-results>
    {
       var $file = "/var/tmp/interface_down.txt";
        <redirect:write select="$file">{
        expr "Following interface: " _ $interface-name _ " went down on " _  $localtime;
        }

    }
}


No comments:

Post a Comment