Blogs

Scripting How-To: The utility-mib-helper script simplifies working with the SNMP Utility MIB

By Erdem posted 08-11-2015 02:43

  

Overview

 

Simplifies working with the SNMP Utility MIBThis applies to SLAX version 1.0 and higher.

 

Description

 

utility-mib-helper performs a handful of small but very useful tasks related to the Utility MIB.

Possible Actions

 

  • walk-mib - This performs a MIB walk of all the Utility MIB values. Each object can be displayed in either oid or ascii format.
  • get-instance - Retrieves an instance from the Utility MIB. The object-type must be specified.
  • set-instance - Sets an instance into the Utility MIB. The object-type must be specified.
  • clear-instance - Removes an instance from the Utility MIB. The object-type must be specified.
  • clear-instance-regex - Removes multiple instances from the Utility MIB, based on a regular
    expression. The object-types are ignored.

Minimum JUNOS Version: 8.4
Latest Script Version: 1.0
MD5 Checksum: 1ed1d48364f3cc62aa5feb87b9eb7e4a
SHA-256 Checksum: 51df53bcaea3ef2891122b185271c711c26e4ce6cc34570a65bce49c631bd2aa

 

Source Code

 

GitHub Links

 

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

Example Output


01	jnpr@Jawa-RE0> op utility-mib-helper set-instance example1 object-type string object-value 123
02	Utility mib result: successfully populated utility mib database
03	 
04	jnpr@Jawa-RE0> op utility-mib-helper set-instance example2 object-type counter object-value 123
05	Utility mib result: successfully populated utility mib database
06	 
07	jnpr@Jawa-RE0> op utility-mib-helper set-instance example2 object-type string object-value hey!
08	Utility mib result: successfully populated utility mib database
09	 
10	jnpr@Jawa-RE0> op utility-mib-helper walk-mib ascii
11	jnxUtilCounter32Value.example2 = 123
12	jnxUtilStringValue.example1 = 123
13	jnxUtilStringValue.example2 = hey!
14	 
15	jnpr@Jawa-RE0> op utility-mib-helper clear-instance-regex .*
16	[3 instances will be cleared]
17	Clearing jnxUtilCounter32Value.example2:
18	Utility mib result: successfully de-populated utility mib database
19	Clearing jnxUtilStringValue.example1:
20	Utility mib result: successfully de-populated utility mib database
21	Clearing jnxUtilStringValue.example2:
22	Utility mib result: successfully de-populated utility mib database
23	 
24	jnpr@Jawa-RE0> op utility-mib-helper walk-mib ascii
25	 
26	jnpr@Jawa-RE0>

SLAX Script Contents


001	/*
002	 * Author        : Curtis Call
003	 * Version       : 1.0
004	 * Last Modified : October 8, 2009
005	 * Platform      : all
006	 * Release       : 8.4 and above
007	 * License       : Public Domain
008	 *
009	 * Description   : utility-mib-helper is a script designed to perform helpful actions
010	 * to the SNMP Utility MIB:
011	 *
012	 * walk-mib - Display the Utility MIB with the instances names optionally printed in
013	 * ASCII.  This ability to view them in ascii was added into JUNOS 9.6, but
014	 * utility-mib-helper can be used to achieve this as far back as JUNOS 8.4.
015	 *
016	 * clear-instance - Clears a specific instance from the Utility MIB.
017	 *
018	 * clear-instance-regex - Clears multiple instances based on a regex value
019	 *
020	 * get-instance - Displays a specific instance in the Utility MIB.
021	 *
022	 * set-instance - Add/Edit an instance in the Utility MIB
023	 *
024	 */
025	version 1.0;
026	 
027	ns junos = "http://xml.juniper.net/junos/*/junos";
028	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
029	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
030	ns ext = "http://xmlsoft.org/XSLT/namespace";
031	 
032	import "../import/junos.xsl";
033	 
034	/* Command line arguments */
035	var $arguments = {
036	    <argument> {
037	        <name> "walk-mib";
038	        <description> "Walk all instance values in the Utility MIB.  Either 'ascii' or 'oid' format should be selected.";
039	    }
040	    <argument> {
041	        <name> "get-instance";
042	        <description> "Display instance in Utility MIB.  An object-type is required.";
043	    }
044	    <argument> {
045	        <name> "clear-instance";
046	        <description> "Remove instance from Utility MIB.  An object-type is required.";
047	    }
048	    <argument> {
049	        <name> "clear-instance-regex";
050	        <description> "Clear multiple instances from Utility MIB, regardless of object-type, using a regex.";
051	    }
052	    <argument> {
053	        <name> "set-instance";
054	        <description> "Add/edit instance in Utility MIB.  An object-type and object-value are required.";
055	    }
056	    <argument> {
057	        <name> "object-type";
058	        <description> "The object data type: string, integer, unsigned-integer, counter, counter64.";
059	    }
060	    <argument> {
061	        <name> "object-value";
062	        <description> "The object data value.";
063	    }
064	}
065	param $walk-mib;
066	param $clear-instance;
067	param $get-instance;
068	param $set-instance;
069	param $clear-instance-regex;
070	param $object-type;
071	param $object-value;
072	 
073	 
074	/*
075	 * Main match / template.  Determine what action should be taken and call the appropriate
076	 * template to perform it.
077	 */
078	match / {
079	    <op-script-results> {
080	     
081	        /* Count actions, exit if more than one is attempted */
082	        var $action-count = { call count-actions(); }
083	        if( $action-count == 0 ) {
084	            expr jcs:output("Nothing to do...");
085	        }
086	        else if( $action-count  > 1 ) {
087	            expr jcs:output("Only one action at a time please...");
088	        }
089	        else if( string-length( $walk-mib ) > 0 ) {
090	            call walk-mib();
091	        }
092	        else if( string-length( $clear-instance-regex ) > 0 ) {
093	            call clear-instance-regex();
094	        }
095	        /* Complain about a missing type - the rest need one */
096	        else if( string-length( $object-type ) == 0 ) {
097	            expr jcs:output("An object-type must be specified for that action.");
098	        }
099	        else if( string-length( $clear-instance ) > 0 ) {
100	            call clear-instance();
101	        }
102	        else if( string-length( $get-instance ) > 0 ) {
103	            call get-instance();
104	        }
105	        /* Complain about a missing value - the rest need one */
106	        else if( string-length( $object-value ) == 0 ) {
107	            expr jcs:output("An object-value must be specified for that action.");
108	        }
109	        else if( string-length( $set-instance ) > 0 ) {
110	            call set-instance();
111	        }
112	    }
113	}
114	 
115	 
116	/*
117	 * count-actions - Determine how many command-line actions were requested and return
118	 * the count in the result tree.
119	 */
120	template count-actions() {
121	    var $count-rtf = {
122	        if( string-length( $walk-mib ) > 0 ) {
123	            <count>;
124	        }
125	        if( string-length( $clear-instance ) > 0 ) {
126	            <count>;
127	        }
128	        if( string-length( $get-instance ) > 0 ) {
129	            <count>;
130	        }
131	        if( string-length( $set-instance ) > 0 ) {
132	            <count>;
133	        }
134	        if( string-length( $clear-instance-regex ) > 0 ) {
135	            <count>;
136	        }
137	    }
138	     
139	    /* Convert the old-school way */
140	    var $count-set = ext:node-set( $count-rtf );
141	     
142	    /* Write the count */
143	    expr count( $count-set/count );
144	}
145	 
146	 
147	/*
148	 * walk-mib - Walks the Utility MIB in ascii or oid mode
149	 */
150	template walk-mib() {
151	 
152	    /* Gather walk output */
153	    var $walk-rpc = {
154	        <walk-snmp-object> {
155	            <snmp-object-name> "jnxUtil";
156	        }
157	    }
158	    var $mib-objects = jcs:invoke( $walk-rpc );
159	 
160	    for-each( $mib-objects/snmp-object[ jcs:regex( "jnxUtil.*Value\\.", name ) ] ) {
161	        var $name = {
162	            if( $walk-mib == "oid" ) {
163	                expr name;
164	            }
165	            else {
166	                call translate-to-instance-format( $oid-name = name );
167	            }
168	        }
169	        expr jcs:output( $name, " = ", object-value );
170	    }
171	}
172	 
173	 
174	/*
175	 * get-instance - Writes the instance value to the console
176	 */
177	template get-instance() {
178	     
179	    var $instance = $get-instance;
180	    var $type = $object-type;
181	 
182	    /* Retrieve the OID and type name */
183	    var $oid-name = { call build-oid-name( $instance, $type ); }
184	    var $type-name = { call get-type-name( $type ); }
185	     
186	    /* Build RPC and invoke it */
187	    var $rpc = {
188	        <get-snmp-object> {
189	            <snmp-object-name> $oid-name;
190	        }
191	    }
192	    var $object = jcs:invoke( $rpc );
193	 
194	    /* If there is an error then report it */
195	    if( $object/..//xnm:error ) {
196	        for-each( $object/..//xnm:error ) {
197	            expr jcs:output( "ERROR: ", . );
198	        }
199	    }
200	    /* Display value */
201	    else {
202	        expr jcs:output( $type-name, ".", $instance, " = ", $object/snmp-object/object-value );
203	    }
204	}
205	  
206	  
207	/*
208	 * clear-instance - Removes the instance from the MIB
209	 */
210	template clear-instance() {
211	     
212	    var $instance = $clear-instance;
213	    var $type = $object-type;
214	 
215	    /* Build RPC and invoke it */
216	    var $rpc = {
217	        <request-snmp-utility-mib-clear> {
218	            <instance> $instance;
219	            <object-type> $type;
220	        }
221	    }
222	    var $results = jcs:invoke( $rpc );
223	    /* Copy results to result tree to display to user */
224	    copy-of $results;
225	}
226	 
227	 
228	/*
229	 * clear-instance-regex - Removes multiple instances from the MIB based on the regex
230	 */
231	template clear-instance-regex() {
232	     
233	    var $instance-regex = $clear-instance-regex;
234	 
235	    /* First walk the MIB and gather all instances */
236	    var $walk-rpc = {
237	        <walk-snmp-object> {
238	            <snmp-object-name> "jnxUtil";
239	        }
240	    }
241	    var $mib-objects = jcs:invoke( $walk-rpc );
242	 
243	    /* Gather all instances that match the regex, regardless of the object-type */
244	    var $objects-rtf = {
245	        for-each( $mib-objects/snmp-object[ jcs:regex( "jnxUtil.*Value\\.", name ) ] ) {
246	            var $name = { call translate-to-instance-format( $oid-name = name ); }
247	             
248	            /* Check if the instance matches the regex */
249	            var $result = jcs:regex( $instance-regex, substring-after( $name, "." ) );
250	            if( not( jcs:empty( $result ) ) ) {
251	                <object> $name;
252	            }
253	        }
254	    }
255	    /* old-school conversion */
256	    var $objects-set = ext:node-set( $objects-rtf );
257	 
258	    /* Report how many will be cleared */
259	    if( count( $objects-set/object ) > 0 ) {
260	        expr jcs:output( "[" , count( $objects-set/object ), " instances will be cleared]" );
261	    }
262	    else {
263	        expr jcs:output("No instances match that regular expression." );
264	    }
265	     
266	    /* Go through and clear each one */
267	    for-each( $objects-set/object ) {
268	        /* Display the name */
269	        <output> "Clearing " _ . _ ":";
270	         
271	        var $type = { call get-object-type( $mib-type-name = substring-before( ., "." ) ); }
272	        var $instance = substring-after( ., "." );
273	        /* Only remove if it has a valid type */
274	        if( string-length( $type ) > 0 ) {
275	            /* Build RPC and invoke it */
276	            var $rpc-clear = {
277	                <request-snmp-utility-mib-clear> {
278	                    <instance> $instance;
279	                    <object-type> $type;
280	                }
281	            }
282	            var $results = jcs:invoke( $rpc-clear );
283	            /* Copy results to result tree to display to user */
284	            copy-of $results;
285	        }
286	    }
287	}
288	 
289	 
290	/*
291	 * set-instance - Sets the instance in the MIB
292	 */
293	template set-instance() {
294	     
295	    var $instance = $set-instance;
296	    var $type = $object-type;
297	 
298	    /* Build RPC and invoke it */
299	    var $rpc = {
300	        <request-snmp-utility-mib-set> {
301	            <instance> $instance;
302	            <object-type> $type;
303	            <object-value> $object-value;
304	        }
305	    }
306	    var $results = jcs:invoke( $rpc );
307	    /* Copy results to result tree to display to user */
308	    copy-of $results;
309	}
310	 
311	 
312	/*
313	 * get-type-name - Converts $type into MIB string type name
314	 *
315	 * $type - Data type = string, counter, count64, integer, or unsigned-integer
316	 *
317	 * Returns the MIB string type name in the result tree
318	 */
319	template get-type-name( $type ) {
320	    if( $type == "string" ) {
321	        expr "jnxUtilStringValue";
322	    }
323	    else if( $type == "counter" ) {
324	        expr "jnxUtilCounter32Value";
325	    }
326	    else if( $type == "integer" ) {
327	        expr "jnxUtilIntegerValue";
328	    }
329	    else if( $type == "counter64" ) {
330	        expr "jnxUtilCounter64Value";
331	    }
332	    else if( $type == "counter" ) {
333	        expr "jnxUtilUintValue";
334	    }
335	    else {
336	        expr jcs:output("ERROR: Invalid data type: ", $type );
337	    }
338	}
339	 
340	 
341	/*
342	 * get-object-type - Converts MIB string type name into object-type
343	 *
344	 * $mib-type-name - jnxUtilStringValue, jnxUtilCounter32Value, jnxUtilIntegerValue,
345	 * jnxUtilUintValue, or unxUtilCounter64Value
346	 *
347	 * Returns the object-type in the result tree
348	 */
349	template get-object-type( $mib-type-name ) {
350	    if( $mib-type-name == "jnxUtilStringValue" ) {
351	        expr "string";
352	    }
353	    else if( $mib-type-name == "jnxUtilCounter32Value" ) {
354	        expr "counter";
355	    }
356	    else if( $mib-type-name == "jnxUtilIntegerValue" ) {
357	        expr "integer";
358	    }
359	    else if( $mib-type-name == "jnxUtilCounter64Value" ) {
360	        expr "counter64";
361	    }
362	    else if( $mib-type-name == "jnxUtilUintValue" ) {
363	        expr "counter";
364	    }
365	    else {
366	        expr jcs:output("ERROR: Invalid type name: ", $mib-type-name );
367	    }
368	}
369	 
370	 
371	/*
372	 * translate-to-instance-format - Translates an OID name into a corresponding
373	 * instance name
374	 *
375	 * $oid-name - The oid name returned by <walk-snmp-object>.
376	 *
377	 * Returns the instance formatted name in the result tree
378	 */
379	template translate-to-instance-format( $oid-name ) {
380	     
381	    /* Get the type portion of name */
382	    var $type-name = substring-before( $oid-name, "." );
383	     
384	    /* The oid portion */
385	    var $oid = substring-after( $oid-name, "." );
386	     
387	    /* Break up the parts into different numbers */
388	    var $number-set = jcs:split("\\.", $oid );
389	     
390	    /* Convert everything to characters */
391	    var $instance-name = {
392	        for-each( $number-set ) {
393	            var $number = .;
394	            /* Use substring to workaround PR 436699 */
395	            expr substring( $ascii-set/char[num == $number]/sym, 1, 1 );
396	        }
397	    }
398	     
399	    /* Combine the two parts and write to result tree */
400	    expr $type-name _ "." _ $instance-name;
401	}
402	  
403	  
404	/*
405	 * build-oid-name - Translates the $instance and $type into a valid OID name for
406	 * use with <get-snmp-object>.
407	 *
408	 * $instance - The Utility MIB instance name
409	 * $type - string, counter, counter64, integer, or unsigned-integer
410	 *
411	 * Writes the full OID name to the result tree.
412	 */
413	template build-oid-name( $instance, $type ) {
414	 
415	    /* Build the type portion of the name */
416	    var $string-portion = { call get-type-name( $type ); }
417	 
418	    /* Split the name into characters */   
419	    var $characters-rtf = { call split-chars( $string = $instance ); }
420	    /* Convert old-school style */
421	    var $characters-set = ext:node-set( $characters-rtf );
422	     
423	    var $number-portion = {
424	        for-each( $characters-set/char ) {
425	     
426	            /* Convert the characters to their ASCII code equivalent */
427	            var $char = .;
428	            /* Use substring to workaround PR 436699 */
429	            var $ascii-char = $ascii-set/char[ substring( sym, 1, 1 ) == $char ];
430	            expr "." _ $ascii-char/num;
431	        }
432	    }
433	     
434	    /* Return the full string */
435	    expr $string-portion _ $number-portion;
436	}
437	 
438	 
439	/*
440	 * split-chars - Recursive template that converts a string into a XML structure
441	 * of <char> elements.  With one <char> per letter.
442	 *
443	 * $string - The string to convert
444	 *
445	 * Writes multiple <char> elements to the result tree.
446	 */
447	template split-chars( $string ) {
448	    if( string-length( $string) > 0 ) {
449	        <char> substring( $string, 1, 1 );
450	         
451	        /* Go recursive if necessary */
452	        if( string-length( $string ) > 1 ) {
453	            call split-chars( $string = substring( $string, 2 ) );
454	        }
455	    }
456	}
457	 
458	 
459	var $ascii-rtf = {
460	<char> { <num> 32; <sym xml:space="preserve"> " "; }<char> { <num> 33; <sym> "!"; }<char> { <num> 34; <sym> "\""; }<char> { <num> 35; <sym> "#"; }<char> { <num> 36; <sym> "$"; }
461	<char> { <num> 37; <sym> "%"; }<char> { <num> 38; <sym> "&"; }<char> { <num> 39; <sym> "'"; }<char> { <num> 40; <sym> "("; }<char> { <num> 41; <sym> ")"; }
462	<char> { <num> 42; <sym> "*"; }<char> { <num> 43; <sym> "+"; }<char> { <num> 44; <sym> ","; }<char> { <num> 45; <sym> "-"; }<char> { <num> 46; <sym> "."; }
463	<char> { <num> 47; <sym> "/"; }<char> { <num> 48; <sym> "0"; }<char> { <num> 49; <sym> "1"; }<char> { <num> 50; <sym> "2"; }<char> { <num> 51; <sym> "3"; }
464	<char> { <num> 52; <sym> "4"; }<char> { <num> 53; <sym> "5"; }<char> { <num> 54; <sym> "6"; }<char> { <num> 55; <sym> "7"; }<char> { <num> 56; <sym> "8"; }
465	<char> { <num> 57; <sym> "9"; }<char> { <num> 58; <sym> ":"; }<char> { <num> 59; <sym> ";"; }<char> { <num> 60; <sym> "<"; }<char> { <num> 61; <sym> "="; } /* >" */
466	<char> { <num> 62; <sym> ">"; }<char> { <num> 63; <sym> "?"; }<char> { <num> 64; <sym> "@"; }<char> { <num> 65; <sym> "A"; }<char> { <num> 66; <sym> "B"; }
467	<char> { <num> 67; <sym> "C"; }<char> { <num> 68; <sym> "D"; }<char> { <num> 69; <sym> "E"; }<char> { <num> 70; <sym> "F"; }<char> { <num> 71; <sym> "G"; }
468	<char> { <num> 72; <sym> "H"; }<char> { <num> 73; <sym> "I"; }<char> { <num> 74; <sym> "J"; }<char> { <num> 75; <sym> "K"; }<char> { <num> 76; <sym> "L"; }
469	<char> { <num> 77; <sym> "M"; }<char> { <num> 78; <sym> "N"; }<char> { <num> 79; <sym> "O"; }<char> { <num> 80; <sym> "P"; }<char> { <num> 81; <sym> "Q"; }
470	<char> { <num> 82; <sym> "R"; }<char> { <num> 83; <sym> "S"; }<char> { <num> 84; <sym> "T"; }<char> { <num> 85; <sym> "U"; }<char> { <num> 86; <sym> "V"; }
471	<char> { <num> 87; <sym> "W"; }<char> { <num> 88; <sym> "X"; }<char> { <num> 89; <sym> "Y"; }<char> { <num> 90; <sym> "Z"; }<char> { <num> 91; <sym> "["; }
472	<char> { <num> 92; <sym> "\\"; }<char> { <num> 93; <sym> "]"; }<char> { <num> 94; <sym> "^"; }<char> { <num> 95; <sym> "_"; }<char> { <num> 96; <sym> "`"; }
473	<char> { <num> 97; <sym> "a"; }<char> { <num> 98; <sym> "b"; }<char> { <num> 99; <sym> "c"; }<char> { <num> 100; <sym> "d"; }<char> { <num> 101; <sym> "e"; }
474	<char> { <num> 102; <sym> "f"; }<char> { <num> 103; <sym> "g"; }<char> { <num> 104; <sym> "h"; }<char> { <num> 105; <sym> "i"; }<char> { <num> 106; <sym> "j"; }
475	<char> { <num> 107; <sym> "k"; }<char> { <num> 108; <sym> "l"; }<char> { <num> 109; <sym> "m"; }<char> { <num> 110; <sym> "n"; }<char> { <num> 111; <sym> "o"; }
476	<char> { <num> 112; <sym> "p"; }<char> { <num> 113; <sym> "q"; }<char> { <num> 114; <sym> "r"; }<char> { <num> 115; <sym> "s"; }<char> { <num> 116; <sym> "t"; }
477	<char> { <num> 117; <sym> "u"; }<char> { <num> 118; <sym> "v"; }<char> { <num> 119; <sym> "w"; }<char> { <num> 120; <sym> "x"; }<char> { <num> 121; <sym> "y"; }
478	<char> { <num> 122; <sym> "z"; }<char> { <num> 123; <sym> "{"; }<char> { <num> 124; <sym> "|"; }<char> { <num> 125; <sym> "}"; }<char> { <num> 126; <sym> "~"; }
479	}
480	/* old-school conversion */
481	var $ascii-set = ext:node-set( $ascii-rtf );

XML Script Contents


01	<?xml version="1.0"?>
02	<script>
03	  <title>utility-mib-helper.slax</title>
04	  <author>curtisstephencall</author>
05	  <synopsis>
06	    Simplifies working with the SNMP Utility MIB
07	  </synopsis>
08	  <coe>op</coe>
09	  <type>snmp</type>
10	 
11	  <description>
12	utility-mib-helper performs a handful of small but very useful tasks related to the Utility MIB.
13	 
14	Possible Actions:
15	 
16	walk-mib - This performs a MIB walk of all the Utility MIB values.  Each object can be displayed
17	in either oid or ascii format.
18	 
19	get-instance - Retrieves an instance from the Utility MIB.  The object-type must be specified.
20	 
21	set-instance - Sets an instance into the Utility MIB.  The object-type must be specified.
22	 
23	clear-instance - Removes an instance from the Utility MIB.  The object-type must be specified.
24	 
25	clear-instance-regex - Removes multiple instances from the Utility MIB, based on a regular
26	expression.  The object-types are ignored.
27	 
28	 
29	Minimum JUNOS Version: 8.4
30	Latest Script Version: 1.0
31	MD5 Checksum: 1ed1d48364f3cc62aa5feb87b9eb7e4a
32	SHA-256 Checksum: 51df53bcaea3ef2891122b185271c711c26e4ce6cc34570a65bce49c631bd2aa
33	 
34	  </description>
35	 
36	  <keyword>slax</keyword>
37	  <keyword>snmp</keyword>
38	  <keyword>utility-mib</keyword>
39	  <keyword>regex</keyword>
40	  <example>
41	    <title>Usage Example</title>
42	    <output>example-1.output</output>
43	  </example>
44	 
45	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
46	                src="../../../../../web/leaf.js"
47	            type="text/javascript"/>
48	</script>

#How-To
#SNMP
#ScriptingHow-To
#opscript
#Slax