001 version 1.0;
002
003 ns junos = "http://xml.juniper.net/junos/*/junos";
004 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
005 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
006
007 import "../import/junos.xsl";
008
009 /*
010 * Version: 1.0
011 * Author: Juniper Networks
012 */
013
014 /*
015 * Script toggles interface up or down based on passed args.
016 * It's designed to work with RPM scripts and be called as part
017 * of a JUNOS event on RPM failure or sucess. No state is
018 * kept between RPM probes on interface state, so script
019 * needs to read config for each time a probe passes or failes
020 * to check current interface state and new one passed in arg list
021 *
022 */
023
024 var $arguments = {
025 <argument> {
026 <name> "silent";
027 <description> "Decides where the output will go, 0 -> stdout, 1 -> syslog, 2 -> none";
028 }
029 <argument> {
030 <name> "interface";
031 <description> "Interface to deactivate";
032 }
033 <argument> {
034 <name> "new_intf_state";
035 <description> "New state of the interface disable -> disable interface, 1 -> enable interface";
036 }
037
038 }
039
040
041 match / {
042
043 /*
044 * Parse interface to get IFD and IFL
045 */
046 var $split = jcs:split("\\.", $interface);
047 var $ifd = $split[1];
048 var $unit = $split[2];
049
050 var $rpc-config-req = <get-configuration database="committed" inherit="inherit">;
051 var $configuration = jcs:invoke($rpc-config-req);
052 if ($new_intf_state == "enable") {
053 /*
054 * If interface is already disabled and new interface state is enable,
055 * populate the $xml var below to enable interface
056 */
057 if ($configuration/interfaces/interface[name=$ifd]/unit[name=$unit]/disable) {
058 var $xml = {
059 <configuration> {
060 <interfaces> {
061 <interface> {
062 <name> $ifd;
063 <unit> {
064 <name> $unit;
065 <disable delete="disable">;
066 } /* end unit */
067 } /* end <interface> */
068 } /* end <interfaces> */
069 } /* end <configuration> */
070 } /* end var $xml */
071
072 call doConfigChange($xml);
073 }
074 } else if ($new_intf_state == "disable") {
075 /*
076 * If interface is enabled and new interface state is disable,
077 * populate the $xml var below to disable interface
078 */
079 if (not($configuration/interfaces/interface[name=$ifd]/unit[name=$unit]/disable)) {
080 var $xml = {
081 <configuration> {
082 <interfaces> {
083 <interface> {
084 <name> $ifd;
085 <unit> {
086 <name> $unit;
087 <disable>;
088 } /* end unit */
089 } /* end <interface> */
090 } /* end <interfaces> */
091 } /* end <configuration> */
092 } /* end var $xml */
093
094 call doConfigChange($xml);
095 }
096 } else {
097 /*
098 * unrecognised new interface state.
099 */
100 call emit-error($message = "unrecognised new intf state");
101 }
102 }
103
104 template doConfigChange($xml) {
105 /*
106 * Open connection with mgd
107 */
108 var $con = jcs:open();
109
110 if (not($con)) {
111 call emit-error($message = "Not able to connect to local mgd");
112 }
113 var $config-private = <open-configuration> {
114 <private>;
115 }
116 var $private-results = jcs:execute($con, $config-private);
117
118 var $load-configuration = <load-configuration> {
119 copy-of $xml;
120 }
121
122 var $load-results = jcs:execute($con, $load-configuration);
123 /*
124 * Use load-configuration template defined in junos.xsl to load and
125 * commit the configuration
126 */
127
128 var $commit-configuration = <commit-configuration>;
129
130 var $commit-results = jcs:execute($con, $commit-configuration);
131
132 var $close-private = <close-configuration>;
133
134 var $close-configuration-results = jcs:execute($con, $close-private);
135 var $close-results = jcs:close($con);
136
137 /*
138 * Emit warnings
139 */
140 for-each ($commit-results//xnm:warning) {
141 call emit-warn($message = message);
142 }
143
144 if ($commit-results//xnm:error) {
145 for-each ($results//xnm:error) {
146 call emit-error($message = message);
147 }
148 } else {
149 call emit-success($message = "Deactivated interface");
150 }
151
152 if (not($silent)) {
153 <op-script-results> {
154 copy-of $results;
155 }
156 }
157 }
158
159 template emit-success($message) {
160
161 if ($silent == 0) {
162 expr jcs:output($message);
163 } else if ($silent == 1) {
164 expr jcs:syslog("user.info","toggle-interface.slax[Success]: ", $message);
165 }
166 }
167
168 template emit-error($message) {
169
170 if ($silent == 0) {
171 expr jcs:output($message);
172 } else if ($silent == 1) {
173 expr jcs:syslog("user.error", "disable-interface.slax[Error]: ", $message);
174 }
175 }
176
177 template emit-warn($message) {
178
179 if ($silent == 0) {
180 expr jcs:output($message);
181 } else if ($silent == 1) {
182 expr jcs:syslog("user.warning", "disable-interface.slax[Warning]: ", $message);
183 }
184 }
|