Automation
Automation

Scripting How-To: Demonstrate how a configuration can be committed from an op script

by Cordelia on ‎08-07-2015 06:26 AM - edited on ‎09-11-2017 05:36 PM by Administrator Administrator (1,325 Views)

Demonstrate How to Commit a Configuration from an Op Script

 

For SLAX version 1.0 and higher, you can use the disable-interface script to demonstrate how you can commit a configuration from an op script.

 

The disable-interface script deactivates interface 'ge-0/0/2' upon receiving a PING_TEST_FAILED event, which is generated by rpm when the ping test fails. The ability to commit configuration from an op script is effective in Junos OS Release 9.3.

 

Source Code and GitHub Links

 

The source code below is also available from the following GitHub locations:

 

 

Example Configuration

 

01	/*
02	 * This script by default tries to deactive interface 'ge-0/0/2'
03	 *
04	 * (i.e) Commit the following configuration
05	 *       "set interfaces ge-0/0/2 disable"
06	 *
07	 * Parameters: $interface -> Interface to disable
08	 *             $silent -> Decides where the output will go.
09	 *                           0 -> (Default) Output to stdout
10	 *                           1 -> Output to syslog
11	 *
12	 * Same script can be triggered from event-policy configuration to deactivate
13	 * an interface on some specific conditions.
14	 *
15	 * For Example:
16	 *
17	 * The following event-policy will trigger this script when
18	 * PING_TEST_FAILED event occurs. (PING_TEST_FAILED will be genereated by rpm
19	 * when the ping test is failed)
20	 *
21	 * This policy also demonstrates, how to restrict the execution of same script
22	 * again and again because of multiple occurances of the same event.
23	 *
24	 * Here, the only first occurance of PING_TEST_FAILED will trigger the script
25	 * and the script will be triggerd again only when the PING_TEST_FAILED did
26	 * not occur in the last 5 mins (300 secs)
27	 *
28	policy disable-interface-on-ping-failure {
29	    events ping_test_failed;
30	    within 300 {
31	        trigger on 1;
32	    }
33	    then {
34	        event-script disable-interface.slax {
35	            arguments {
36	                silent 1;
37	            }
38	        }
39	    }
40	}
41	 *
42	 * Sample rpm configuration used to test this.
43	 *
44	rpm {
45	    probe icmp-ping-probe {
46	        test ping-probe-test {
47	            probe-type icmp-ping;
48	            target address 10.1.2.1;
49	            test-interval 60;
50	        }
51	    }
52	}
53	 *
54	 *
55	 */

 

SLAX Script Contents

 

001	/*
002	 * Example script to demonstrate how a configuration can be committed
003	 * from op-script.
004	 *
005	 * This script by default tries to deactive interface 'ge-0/0/2'
006	 *
007	 * (i.e) Commit the following configuration
008	 *         "set interfaces ge-0/0/2 disable"
009	 *
010	 * Parameters: $interface -> Interface to disable
011	 *             $silent -> Decides where the output will go.
012	 *                          0 -> (Default) Output to stdout
013	 *                          1 -> Output to syslog
014	 *
015	 * Same script can be triggered from event-policy configuration to deactivate
016	 * an interface on some specific conditions.
017	 *
018	 * For Example:
019	 *
020	 * The following event-policy will trigger this script when
021	 * PING_TEST_FAILED event occurs. (PING_TEST_FAILED will be genereated by rpm
022	 * when the ping test is failed)
023	 *
024	 * This policy also demonstrates, how to restrict the execution of same script
025	 * again and again because of multiple occurences of the same event.
026	 *
027	 * Here, the only first occurance of PING_TEST_FAILED will trigger the script
028	 * and the script will be triggerd again only when the PING_TEST_FAILED did
029	 * not occur in the last 5 mins (300 secs)
030	 *
031	policy disable-interface-on-ping-failure {
032	    events ping_test_failed;
033	    within 300 {
034	        trigger on 1;
035	    }
036	    then {
037	        event-script disable-interface.slax {
038	            arguments {
039	                silent 1;
040	            }
041	        }
042	    }
043	}
044	 *
045	 * Sample rpm configuration used to test this.
046	 *
047	rpm {
048	    probe icmp-ping-probe {
049	        test ping-probe-test {
050	            probe-type icmp-ping;
051	            target address 10.1.2.1;
052	            test-interval 60;
053	        }
054	    }
055	}
056	 *
057	 *
058	 */
059	 
060	version 1.0;
061	ns junos = "http://xml.juniper.net/junos/*/junos";
062	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
063	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
064	 
065	import "../import/junos.xsl";
066	 
067	 
068	var $arguments = {
069	    <argument> {
070	    <name> "interface";
071	    <description> "Interface to deactivate";
072	    }
073	    <argument> {
074	    <name> "silent";
075	    <description> "Decides where the output will go, 0 -> stdout, 1 -> syslog";
076	    }
077	}
078	 
079	param $interface = "ge-0/0/2";
080	param $silent = 0;
081	 
082	match / {
083	 
084	    /*
085	     * Open connection with mgd
086	     */
087	    var $con = jcs:open();
088	 
089	    if (not($con)) {
090	    call emit-error($message = "Not able to connect to local mgd");
091	    }
092	 
093	    /*
094	     * Configuration to disable the interface
095	     */
096	    var $xml = {
097	    <configuration> {
098	         <interfaces> {
099	         <interface> {
100	             <name> $interface;
101	             <disable>;
102	         }
103	         }
104	    }
105	    }
106	 
107	    /*
108	     * Use load-configuration template defined in junos.xsl to load and
109	     * commit the configuration
110	     */
111	    var $results = {
112	    call jcs:load-configuration($connection = $con, $configuration = $xml);
113	    }
114	 
115	    /*
116	     * Emit warnings
117	     */
118	    for-each ($results//xnm:warning) {
119	    call emit-warn($message = message);
120	    }
121	 
122	    if ($results//xnm:error) {
123	    for-each ($results//xnm:error) {
124	        call emit-error($message = message);
125	    }
126	    } else {
127	    call emit-success($message = "Successfully deactivated the interface");
128	    }
129	 
130	    if (not($silent)) {
131	    <op-script-results> {
132	        copy-of $results;
133	    }
134	    }
135	 
136	    /*
137	     * Close the mgd connection
138	     */
139	    expr jcs:close($con);
140	 
141	}
142	 
143	template emit-success($message)
144	{
145	    if ($silent) {
146	    expr jcs:syslog("user.info", "disable-interface.slax[Success]: ", $message);
147	    } else {
148	    expr jcs:output($message);
149	    }
150	}
151	 
152	template emit-error($message)
153	{
154	    if ($silent) {
155	    expr jcs:syslog("user.error", "disable-interface.slax[Error]: ", $message);
156	    }
157	}
158	 
159	template emit-warn($message)
160	{
161	    if ($silent) {
162	    expr jcs:syslog("user.warning", "disable-interface.slax[Warning]: ", $message);
163	    }
164	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>disable-interface.slax</title>
04	  <author>rsankar</author>
05	  <synopsis>
06	    An example script to demostrate how a configuration can be committed from op-script
07	  </synopsis>
08	  <coe>op</coe>
09	  <type>interfaces</type>
10	 
11	  <description>
12	This script will deactive interface 'ge-0/0/2' upon receiving a PING_TEST_FAILED event,
13	which is generated by rpm when the ping test fails. The ability to commit
14	configuration from an op-script is effective in JUNOS 9.3
15	 
16	  </description>
17	 
18	  <example>
19	    <title>Configuration</title>
20	    <description>Configuration required for this op script</description>
21	    <config>example-1.conf</config>
22	  </example>
23	 
24	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
25	                src="../../../../../web/leaf.js"
26	            type="text/javascript"/>
27	</script>