Required Boilerplate for SNMP Scripts
Junos OS SNMP scripts can be written in Extensible Stylesheet Language Transformations (XSLT), Stylesheet Language Alternative syntaX (SLAX), or Python. SNMP scripts must include the necessary boilerplate required for that script language for both basic script functionality, as well as any optional functionality used within the script such as the Junos OS extension functions and named templates. This topic provides standard boilerplate that can be used in XSLT, SLAX, and Python SNMP scripts.
SLAX Boilerplate for SNMP Scripts
The SLAX SNMP script boilerplate is as follows:
1 version 1.2; 2 ns junos = "http://xml.juniper.net/junos/*/junos"; 3 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; 4 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; 5 ns dyn = "http://exslt.org/dynamic"; 6 ns snmp extension = "http://exslt.org/functions"; 7 match / { 8 <snmp-script-results> { 9 var $snmp-action = snmp-script-input/snmp-action; 10 var $snmp-oid = snmp-script-input/snmp-oid; <!-- ... insert your code here ... --> 11 <snmp-oid> $snmp-oid; 12 <snmp-type> $snmp-type; 13 <snmp-value> $snmp-value; } }
SLAX and XSLT SNMP scripts are based on Junos XML and Junos XML protocol tag elements. Like all XML elements, angle brackets enclose the name of a Junos XML or Junos XML protocol tag element in its opening and closing tags. This is an XML convention, and the brackets are a required part of the complete tag element name. They are not to be confused with the angle brackets used in the documentation to indicate optional parts of Junos OS CLI command strings.
Line 1 specifies the SLAX version as 1.2.
1 version 1.2;
Lines 2 through 6 list all the namespace mappings commonly used in SNMP scripts. Not all of these prefixes are used in this example. Listing all namespace mappings prevents errors if the mappings are used in later versions of the script. These namespace mappings enable you to use extension functions and named templates in your scripts. These extension functions and named templates are discussed in Understanding Extension Functions in Junos OS Automation Scripts and Understanding Named Templates in Junos OS Automation Scripts.
Line 5 and line 6 have EXSLT namespace mappings. SNMP extension
functions are defined in the namespace with the associated URI http://exslt.org/functions.
Line 6 registers the snmp
extension namespace
with the EXSLT functions namespace, allowing you to define customized
functions using snmp
as a prefix within
your SLAX script. For more information about the EXSLT namespace,
see http://exslt.org/func/index.html.
2 ns junos = "http://xml.juniper.net/junos/*/junos"; 3 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; 4 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; 5 ns dyn = "http://exslt.org/dynamic"; 6 ns snmp extension = "http://exslt.org/functions";
Line 7 defines an unnamed template, match
/
, that represents the top level of the configuration
hierarchy. All XML Path Language (XPath)
expressions in the script must start at the top-level element in the
configuration hierarchy. This allows the script to access all possible
Junos XML and Junos XML protocol remote procedure calls (RPCs). For
more information, see XPath Overview.
7 match / {
After the match /
tag element,
the <snmp-script-results>
container
tag must be the top-level child tag, as shown in Line 8. The value
of this container is returned to the OID requester.
8 <snmp-script-results> {
Lines 9 and 10 define variables based on the corresponding elements that you can use in your code to determine whether the action is get or get-next, and the value of the OID.
9 var $snmp-action = snmp-script-input/snmp-action; 10 var $snmp-oid = snmp-script-input/snmp-oid;
Between Line 10 and Line 11, you can define additional code
that includes XSLT templates that are called from within the match /
template.
Lines 11 through 13 define the values returned by the
SNMP script to the OID requester. The value of <snmp-oid>
is taken from the input value of snmp-script-input/snmp-oid
. For the SNMP script feature, the following object identifier types
for <snmp-type>
are supported:
Counter32
Counter64
Integer32
Unsigned32
Octet String
You set the <snmp-value>
to the return value from
the script.
11 <snmp-oid> $snmp-oid; 12 <snmp-type> $snmp-type; 13 <snmp-value> $snmp-value;
XSLT Boilerplate for SNMP Scripts
The XSLT boilerplate is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:junos="http://xml.juniper.net/junos/*/junos" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0" xmlns:dyn="http://exslt.org/dynamic" xmlns:snmp="http://exslt.org/functions" version="1.0" extension-element-prefixes="snmp"> <xsl:template match="/"> <snmp-script-results> <xsl:variable name="snmp-action" select="snmp-script-input/snmp-action"/> <xsl:variable name="snmp-oid" select="snmp-script-input/snmp-oid"/> <!-- Insert your code here --> <snmp-oid> <xsl:value-of select="$snmp-oid"/> </snmp-oid> <snmp-type> <xsl:value-of select="$snmp-type"/> </snmp-type> <snmp-value> <xsl:value-of select="$snmp-value"/> </snmp-value> </snmp-script-results> </xsl:template> </xsl:stylesheet>
Python Boilerplate for SNMP Scripts
Python SNMP scripts do not have a required boilerplate,
but they must import any objects that are used in the script. Python
SNMP scripts must include the import jcs
statement in order to use the get_snmp_action()
, get_snmp_oid()
, and emit_snmp_attributes()
functions that retrieve the action and OID values passed to the
script and return the data for the requested MIB object.
import jcs if __name__ == '__main__':
Python automation scripts do not need to include an interpreter
directive line (#!/usr/bin/env python
)
at the start of the script. However, the program will still execute
correctly if one is present.