Understanding Supported Data Types in SNMP Scripts
Understand the data types supported by SNMP scripts, which return information for custom OIDs on Junos devices.
Data Types Overview
SNMP scripts are triggered automatically when an SNMP manager requests information
from the SNMP agent for an object identifier (OID) that is mapped to the SNMP
script. SLAX and XSLT scripts return the information for the requested object in the
<snmp-script-results> element. Python scripts return the
information by executing the jcs.emit_snmp_attributes()
function.
When the SNMP script returns the requested object, it provides the OID, a type, and a value. SNMP scripts support standard SNMP data types. Table 1 outlines the supported data types.
| Data Type | Description | Example |
|---|---|---|
|
Counter32 |
32-bit unsigned integer. Range: 0 through 4,294,967,295 |
4294967295 |
|
Counter64 |
64-bit unsigned integer. Range: 0 through 18,446,744,073,709,551,615 |
18446744073709551615 |
|
INTEGER |
32-bit signed integer. Range: -2,147,483,647 through 2,147,483,647 |
-1000 |
|
OCTET STRING |
A sequence of bytes that can represent text or binary data. Specify the hexadecimal representation of each ASCII character in the output string. Use spaces to separate each hex value. |
64 65 76 69 63 65 20 72 65 62 6f 6f 74 65 64 |
|
Unsigned32 |
32-bit unsigned integer. Range: 0 through 4,294,967,295 |
896 |
Example: Emitting Objects with Different Data Types in SNMP Scripts
This example shows an SNMP script that emits information for objects with different data types.
Requirements
-
Device running Junos OS or device running Junos OS Evolved.
-
SNMP is configured on the device.
Overview and SNMP Script
This example presents an SNMP script that matches and processes several OIDs. The script returns the information for the requested object, which includes its OID, data type, and value. The script processes OIDs for objects that use different data types.
The example provides the SNMP script in SLAX and Python. The SLAX SNMP script
returns the information for the requested object by using the
<snmp-script-results> element. The Python SNMP script
returns the information for the requested object by executing the
jcs.emit_snmp_attributes() function.
SLAX Syntax
version 1.2;
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.1.1.8') {
<snmp-script-results> {
<snmp-oid> $snmp-oid;
<snmp-type> "Counter32";
<snmp-value> "4294967295";
}
}
else if($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.1.9') {
<snmp-script-results> {
<snmp-oid> $snmp-oid;
<snmp-type> "Counter64";
<snmp-value> "18446744073709551615";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.2') {
<snmp-script-results> {
<snmp-oid> $snmp-oid;
<snmp-type> "INTEGER";
<snmp-value> "-1000";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.3') {
<snmp-script-results> {
<snmp-oid> $snmp-oid;
<snmp-type> "Integer32";
<snmp-value> "3000";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.3.5') {
<snmp-script-results> {
<snmp-oid> $snmp-oid;
<snmp-type> "Unsigned32";
<snmp-value> "999";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.8.8') {
<snmp-script-results> {
<snmp-oid> $snmp-oid;
<snmp-type> "OCTET STRING";
<snmp-value> "64 65 76 69 63 65 20 72 65 62 6f 6f 74 65 64";
}
}
}
else if ($snmp-action == 'get-next') {
if($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.1.8') {
<snmp-script-results> {
<snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.1.1.8.1";
<snmp-type> "Counter32";
<snmp-value> "4294967260";
}
}
else if($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.1.9') {
<snmp-script-results> {
<snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.1.1.9.1";
<snmp-type> "Counter64";
<snmp-value> "18446744073709551613";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.2') {
<snmp-script-results> {
<snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.1.2.2.1";
<snmp-type> "Integer";
<snmp-value> "-2000";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.3') {
<snmp-script-results> {
<snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.1.2.3.1";
<snmp-type> "Integer32";
<snmp-value> "4000";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.3.5') {
<snmp-script-results> {
<snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.1.3.5.1";
<snmp-type> "Unsigned32";
<snmp-value> "2040";
}
}
else if ($snmp-oid == '.1.3.6.1.4.1.2636.13.61.1.1.8.8') {
<snmp-script-results> {
<snmp-oid> ".1.3.6.1.4.1.2636.13.61.1.1.8.8.1";
<snmp-type> "OCTET STRING";
<snmp-value> "64 65 76 69 63 65 20 72 65 62 6f 6f 74 65 64";
}
}
}
}
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.1.1.8':
jcs.emit_snmp_attributes(snmp_oid, "Counter32", "4294967295")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.1.9':
jcs.emit_snmp_attributes(snmp_oid, "Counter64", "18446744073709551615")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.2':
jcs.emit_snmp_attributes(snmp_oid, "INTEGER", "-1000")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.3':
jcs.emit_snmp_attributes(snmp_oid, "Integer32", "3000")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.3.5':
jcs.emit_snmp_attributes(snmp_oid, "Unsigned32", "999")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.8.8':
jcs.emit_snmp_attributes(snmp_oid, "OCTET STRING", "64 65 76 69 63 65 20 72 65 62 6f 6f 74 65 64")
elif snmp_action == 'get-next':
if snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.1.8':
jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.1.1.8.1", "Counter32", "4294967260")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.1.9':
jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.1.1.9.1", "Counter64", "18446744073709551613")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.2':
jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.1.2.2.1", "INTEGER", "-2000")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.2.3':
jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.1.2.3.1", "Integer32", "4000")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.3.5':
jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.1.3.5.1", "Unsigned32", "2040")
elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.1.8.8':
jcs.emit_snmp_attributes(".1.3.6.1.4.1.2636.13.61.1.1.8.8.1", "OCTET STRING", "64 65 76 69 63 65 20 72 65 62 6f 6f 74 65 64")
if __name__ == '__main__':
main()
Configuration
Procedure
To download and enable the script:
Copy the script into a text file and name the file snmp-script-data-types.slax or snmp-script-data-types.py, as appropriate.
Download the script 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-userlogin class, and only the file owner can have write permission for the file.-
If the script is written in Python, configure the
language pythonorlanguage python3statement as appropriate.[edit system scripts] user@host# set language python3
Configure the SNMP script with the appropriate filename and extension for your script language at the
[edit system scripts snmp]hierarchy level.[edit system scripts] user@host# edit snmp file snmp-script-data-types.py
Under the SNMP script's hierarchy, configure the OIDs for which the script is returning information.
[edit system scripts snmp file snmp-script-data-types.py] user@host# set oid .1.3.6.1.4.1.2636.13.61.1.1.1.8 user@host# set oid .1.3.6.1.4.1.2636.13.61.1.1.1.9 user@host# set oid .1.3.6.1.4.1.2636.13.61.1.1.2.2 user@host# set oid .1.3.6.1.4.1.2636.13.61.1.1.2.3 user@host# set oid .1.3.6.1.4.1.2636.13.61.1.1.3.5 user@host# set oid .1.3.6.1.4.1.2636.13.61.1.1.8.8
If the script is written in Python, configure the user under whose access privileges the script executes.
[edit system scripts snmp file snmp-script-data-types.py] user@host# set python-script-user username
For example:
[edit system scripts snmp file snmp-script-data-types.py] user@host# set python-script-user admin
Note:If you do not configure the
python-script-userstatement, then by default, Junos OS executes the Python SNMP script under the access privileges of the user and groupnobody.Issue the
commitcommand to commit the configuration.[edit system scripts] user@host# commit
Results
From configuration mode, confirm your configuration by entering the
show system scripts command. The configuration
statements will vary depending on your script language.
[edit]
user@host# show system scripts
language python3;
snmp {
file snmp-script-data-types.py {
oid .1.3.6.1.4.1.2636.13.61.1.1.1.8;
oid .1.3.6.1.4.1.2636.13.61.1.1.1.9;
oid .1.3.6.1.4.1.2636.13.61.1.1.2.2;
oid .1.3.6.1.4.1.2636.13.61.1.1.2.3;
oid .1.3.6.1.4.1.2636.13.61.1.1.3.5;
oid .1.3.6.1.4.1.2636.13.61.1.1.8.8;
python-script-user admin;
}
}
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.1.1.8 juniperMIB.13.61.1.1.1.8 = 4294967295 user@host> show snmp mib get .1.3.6.1.4.1.2636.13.61.1.1.1.9 juniperMIB.13.61.1.1.1.9 = 18446744073709551615 user@host> show snmp mib get .1.3.6.1.4.1.2636.13.61.1.1.2.2 juniperMIB.13.61.1.1.2.2 = -1000 user@host> show snmp mib get .1.3.6.1.4.1.2636.13.61.1.1.2.3 juniperMIB.13.61.1.1.2.3 = 3000 user@host> show snmp mib get .1.3.6.1.4.1.2636.13.61.1.1.3.5 juniperMIB.13.61.1.1.3.5 = 999 user@host> show snmp mib get .1.3.6.1.4.1.2636.13.61.1.1.8.8 juniperMIB.13.61.1.1.8.8 = device rebooted user@host> show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.1.1.8 juniperMIB.13.61.1.1.1.8.1 = 4294967260 user@host> show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.1.1.9 juniperMIB.13.61.1.1.1.9.1 = 18446744073709551613 user@host> show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.1.2.2 juniperMIB.13.61.1.1.2.2.1 = -2000 user@host> show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.1.2.3 juniperMIB.13.61.1.1.2.3.1 = 4000 user@host> show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.1.3.5 juniperMIB.13.61.1.1.3.5.1 = 2040 user@host> show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.1.8.8 juniperMIB.13.61.1.1.8.8.1 = device rebooted