Wednesday, October 16, 2013

An Event script to change static routing based upon interface flaps


An Event script to change static routing based upon interface flaps

user@router# run show interfaces ge-0/1/3 terse 
Interface               Admin Link Proto    Local                 Remote
ge-0/1/3                up    up  
ge-0/1/3.0              up    up   inet     192.10.1.1/24   

[edit]
user@router# show routing-options 
static {
    route 50.0.0.0/24 next-hop 192.10.1.2;
}

[edit]


DISABLED THE PHYSICAL INTERFACE

[edit]
user@router# run show interfaces ge-0/1/3 terse    
Interface               Admin Link Proto    Local                 Remote
ge-0/1/3                up    down
ge-0/1/3.0              up    down inet     192.10.1.1/24   

CHECK the router to see if the static route is deactivated.

[edit]
user@router# show routing-options                  
static {
    inactive: route 50.0.0.0/24 next-hop 192.10.1.2;
}

[edit]
user@router# run show log messages   
Jul 17 11:19:53 router clear-log[27080]: logfile cleared
Jul 17 11:20:00  router rpd[1336]: EVENT <UpDown> ge-0/1/3.0 index 69 <Broadcast Multicast> address #0 0.5.85.d4.40.22
Jul 17 11:20:00  router rpd[1336]: EVENT UpDown ge-0/1/3.0 index 69 192.10.1.1/24 -> 192.10.1.255 <Broadcast Multicast Localup>
Jul 17 11:20:00  router mib2d[1362]: SNMP_TRAP_LINK_DOWN: ifIndex 133, ifAdminStatus up(1), ifOperStatus down(2), ifName ge-0/1/3
Jul 17 11:20:00  router rpd[1336]: EVENT <UpDown> index 133 <Broadcast Multicast> address #0 0.5.85.d4.40.22
Jul 17 11:20:01  router file[27087]: UI_CFG_AUDIT_OTHER: User 'root' deactivate: [juniper-config routing-options static route 50.0.0.0/24]
Jul 17 11:20:03  router rpd: proceeding. -C
Jul 17 11:20:05  router rpd[1336]: RPD_TASK_REINIT: Reinitializing
Jul 17 11:20:05  router rpd[1336]: task_reconfigure reinitializing done
Jul 17 11:20:06  router cscript: removing static route
Jul 17 11:20:06  router cscript: Route-change[warning]: Disabling static route 50.0.0.0/24
Jul 17 11:20:06  router root: invoke-commands: Executed /tmp/evt_cmd_kzGMAH, output to /tmp/evt_op_K7nJxd in text format
Jul 17 11:20:06  router root: transfer-file: Transferred /tmp/evt_op_K7nJxd



REENABLED THE PHYSICAL INTERFACE

[edit]
user@router# run show interfaces ge-0/1/3 terse 
Interface               Admin Link Proto    Local                 Remote
ge-0/1/3                up    up  
ge-0/1/3.0              up    up   inet     192.10.1.1/24   

CHECK THE STATIC ROUTE
[edit]
user@router# show routing-options           
static {
    route 50.0.0.0/24 next-hop 192.10.1.2;
}

[edit]
user@router# run show log messages   

Jul 17 11:20:55  router rpd[1336]: EVENT <UpDown> ge-0/1/3.0 index 69 <Up Broadcast Multicast> address #0 0.5.85.d4.40.22
Jul 17 11:20:55  router rpd[1336]: EVENT UpDown ge-0/1/3.0 index 69 192.10.1.1/24 -> 192.10.1.255 <Up Broadcast Multicast>
Jul 17 11:20:55  router mib2d[1362]: SNMP_TRAP_LINK_UP: ifIndex 133, ifAdminStatus up(1), ifOperStatus up(1), ifName ge-0/1/3
Jul 17 11:20:55  router mib2d[1362]: SNMP_TRAP_LINK_UP: ifIndex 159, ifAdminStatus up(1), ifOperStatus up(1), ifName ge-0/1/3.0
Jul 17 11:20:55  router rpd[1336]: EVENT <UpDown> index 133 <Up Broadcast Multicast> address #0 0.5.85.d4.40.22
Jul 17 11:20:56  router file[27353]: UI_CFG_AUDIT_OTHER: User 'root' activate: [juniper-config routing-options static route 50.0.0.0/24]
Jul 17 11:20:58  router rpd: proceeding. -C
Jul 17 11:21:00  router rpd[1336]: RPD_TASK_REINIT: Reinitializing
Jul 17 11:21:00  router rpd[1336]: task_reconfigure reinitializing done
Jul 17 11:21:01  router cscript: Enabling static route 50.0.0.0/24
Jul 17 11:21:01  router cscript: Route-change[warning]: Enabling static route 50.0.0.0/24
Jul 17 11:21:01  router root: invoke-commands: Executed /tmp/evt_cmd_4j1XoH, output to /tmp/evt_op_NxKJIo in text format
Jul 17 11:21:01  router cscript: Enabling static route 50.0.0.0/24
Jul 17 11:21:01  router cscript: Route-change[warning]: Enabling static route 50.0.0.0/24
Jul 17 11:21:01  router root: transfer-file: Transferred /tmp/evt_op_NxKJIo
Jul 17 11:21:01  router root: invoke-commands: Executed /tmp/evt_cmd_QrBmxm, output to /tmp/evt_op_pP79Iy in text format
Jul 17 11:21:01  router root: transfer-file: Transferred /tmp/evt_op_pP79Iy

[edit]
user@router# show event-options 
policy track-interface-down {
    events snmp_trap_link_down;
    attributes-match {
        snmp_trap_link_down.interface-name matches ge-0/1/3;
    }
    then {
        event-script route-change.slax {
            arguments {
                action 1;
                prefix 50.0.0.0/24;
            }
            output-filename track-int;
            destination local;
            output-format text;
        }
    }
}
policy track-interface-up {
    events snmp_trap_link_up;
    attributes-match {
        snmp_trap_link_up.interface-name matches ge-0/1/3;
    }
    then {
        event-script route-change.slax {
            arguments {
                action 0;
                prefix 50.0.0.0/24;
            }
            output-filename track-int;
            destination local;
            output-format text;
        }
    }
}
destinations {
    local {
        archive-sites {
            /var/home/user/;
        }
    }
}

Source code: route-change.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";

import "../import/junos.xsl";
 var $arguments = {
    <argument> {
    <name> "action";
    <description> "0 to enable, 1 to disable";
    }
    <argument> {
        <name> "prefix";
        <description> "route prefix with mask, ie 10.0.0.0/24";
    }
}
param $action = 0;
param $prefix;

match / {
    /*
     * Open connection with mgd
     */
    var $con = jcs:open();

    if (not($con)) {
        call emit-error($message = "Not able to connect to local mgd");
    }

        if ($action = 1) {
            expr jcs:output("Disabling Route");
            var $check-int = jcs:invoke ("get-configuration");
            for-each ($check-int/routing-options/static/route) {        
                      if (name = $prefix) {
                      expr jcs:output(name);
                       /*
                        *   REMOVE route
                        */
                          var $disable = <configuration> {
                                           <routing-options> {
                                                <static> {  
                                           <route inactive="inactive"> {    
           
                                                          <name> $prefix;   
                                                            }
                                                    }
                                        }

                          }
                          call jcs:load-configuration($connection = $con, $configuration = $disable);
                          expr jcs:syslog("user.info","removing static route");
                          var $message = concat ("Disabling static route " , $prefix);
                          call emit-success($message);
                      }
            }
        }  
        if ($action = 0) {
            expr jcs:output("Enabling Route");
             /*
              *   ADD route
              */
            var $int = <configuration> {
                           <routing-options> {
                                      <static> {
                                            <route active="active"> {
                                               <name> $prefix;
                                      }
                                }
                       }
             }
         call jcs:load-configuration($connection = $con, $configuration = $int);
         expr jcs:syslog("user.info","Enabling static route " , $prefix);
         var $message = concat ("Enabling static route " , $prefix);
         /*
          * Emit messages
          */
          call emit-fail($message);

        }
     /*
      * Close the mgd connection
      */
      expr jcs:close($con);

}
template emit-fail ($message) {   
    expr jcs:syslog("user.info", "Route-change[warning]: ", $message);
}

template emit-success ($message) {
    expr jcs:syslog("user.info", "Route-change[warning]: ", $message);
}


No comments:

Post a Comment