Example: Process Unsupported OIDs with an SNMP Script
This sample SNMP script shows how to process object identifiers (OIDs) that are not supported on devices running Junos OS.
Requirements
Junos OS Release 15.1 or later when using SLAX SNMP scripts.
Junos OS Release 16.1R3 or later when using Python SNMP scripts on QFX Series switches or MX Series, PTX Series, or T Series routers.
Junos OS Release 17.1R1 or later when using Python SNMP scripts on EX Series switches.
Junos OS Release 17.3R1 or later when using Python SNMP scripts on SRX Series Firewalls.
SNMP is configured on the device.
Overview and SNMP Script
In this example, two equivalent SNMP scripts are presented in
SLAX and Python that match and process several unsupported OIDs. The
script returns the value for the requested object by using the <snmp-script-results>
element in the SLAX script
and the jcs.emit_snmp_attributes()
function
in the equivalent Python script. The syslog()
extension
function is called to log the requested SNMP action and OID in the
system log file. For more information about the syslog()
function, see syslog()
Function (Python, SLAX, and XSLT).
SLAX Syntax
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"; ns dyn = "http://exslt.org/dynamic"; ns snmp extension = "http://exslt.org/functions"; match / { var $snmp-action = snmp-script-input/snmp-action; var $snmp-oid = snmp-script-input/snmp-oid; expr jcs:syslog(8, "snmp-action = ", $snmp-action, " snmp-oid = ", $snmp-oid); if ($snmp-action == 'get') { if($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.1') { <snmp-script-results> { <snmp-oid> $snmp-oid; <snmp-type> "Integer32"; <snmp-value> "211"; } } else if($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.2') { <snmp-script-results> { <snmp-oid> $snmp-oid; <snmp-type> "Integer32"; <snmp-value> "429"; } } } else if ($snmp-action == 'get-next') { if($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1') { <snmp-script-results> { <snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.9.1.1.1"; <snmp-type> "Integer32"; <snmp-value> "211"; } } else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.1') { <snmp-script-results> { <snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.9.1.1.2"; <snmp-type> "Integer32"; <snmp-value> "429"; } } } }
Python Syntax
import jcs def main(): snmp_action = jcs.get_snmp_action() snmp_oid = jcs.get_snmp_oid() jcs.syslog("8", "snmp_action = ", snmp_action, " snmp_oid = ", snmp_oid) if snmp_action == 'get': if snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.1': jcs.emit_snmp_attributes(snmp_oid, "Integer32", "211") elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.2': jcs.emit_snmp_attributes(snmp_oid, "Integer32", "429") elif snmp_action == 'get-next': if snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1': jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.9.1.1.1", "Integer32", "211") elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.1': jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.9.1.1.2", "Integer32", "429") if __name__ == '__main__': main()
Configuration
Procedure
Step-by-Step Procedure
To download and enable the script:
You can create SNMP scripts in Python, SLAX, or XSLT. You can use the request system scripts convert command to convert between SLAX and XSLT.
Copy the script into a text file, name the file sample_snmp.slax or sample_snmp.py, as appropriate, and download it to the /var/db/scripts/snmp directory on the device.
Note:Unsigned Python scripts must be owned by either root or a user in the Junos OS
super-user
login class, and only the file owner can have write permission for the file.Enable the SNMP script and configure the OID.
In configuration mode, configure the
file filename
statement with the appropriate filename and extension for your script language at the[edit system scripts snmp]
hierarchy level, and configure the OID that will trigger the script.[edit system scripts] user@host# set snmp file sample_snmp.slax oid .1.3.6.1.4.1.2636.13.61.1.9.1.1
-
If the script is written in Python, configure the
language python
orlanguage python3
statement as appropriate.[edit system scripts] user@host# set language python3
If the script is written in Python, configure the user under whose access privileges the script executes.
[edit system scripts] user@host# set snmp file sample_snmp.py python-script-user username
Note:If you do not configure the
python-script-user
statement, then by default, Junos OS executes Python SNMP scripts under the access privileges of the user and groupnobody
.Issue the
commit
command to commit the configuration.[edit system scripts] user@host# commit
Results
From configuration mode, confirm your configuration
by entering the show system scripts snmp
command.
[edit] user@host# show system scripts snmp file sample_snmp.slax { oid .1.3.6.1.4.1.2636.13.61.1.9.1.1; }
If the output does not display the intended configuration, repeat the configuration instructions in this example to correct it.
To ensure that the enabled files are on the device, list the
contents of the /var/run/scripts/snmp directory using the file list /var/run/scripts/snmp
operational
mode command.
Verification
Verifying the Script Execution
Purpose
Verify that the SNMP script functions as expected.
Action
Issue the show snmp mib get
, show snmp
mib get-next
, or show snmp mib walk
command to generate
an SNMP request.
user@host> show snmp mib get .1.3.6.1.4.1.2636.13.61.1.9.1.1.1 juniperMIB.13.61.1.9.1.1.1 = 211
user@host> show snmp mib get .1.3.6.1.4.1.2636.13.61.1.9.1.1.2 juniperMIB.13.61.1.9.1.1.2 = 429
user@host> show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.9.1.1.1 juniperMIB.13.61.1.9.1.1.2 = 429
user@host> show snmp mib walk .1.3.6.1.4.1.2636.13.61.1.9.1.1 juniperMIB.13.61.1.9.1.1.1 = 211 juniperMIB.13.61.1.9.1.1.2 = 429
The system log file contains the following messages after script execution:
Jul 3 10:07:48 host cscript: snmp-action = get snmp-oid = .1.3.6.1.4.1.2636.13.61.1.9.1.1.1 Jul 3 10:07:51 host cscript: snmp-action = get snmp-oid = .1.3.6.1.4.1.2636.13.61.1.9.1.1.2 Jul 3 10:08:05 host cscript: snmp-action = get-next snmp-oid = .1.3.6.1.4.1.2636.13.61.1.9.1.1.1 Jul 3 10:08:24 host cscript: snmp-action = get-next snmp-oid = .1.3.6.1.4.1.2636.13.61.1.9.1.1 Jul 3 10:08:24 host cscript: snmp-action = get-next snmp-oid = .1.3.6.1.4.1.2636.13.61.1.9.1.1.1 Jul 3 10:08:24 host cscript: snmp-action = get-next snmp-oid = .1.3.6.1.4.1.2636.13.61.1.9.1.1.2