Topic: tech juniper jaut prev next

tech juniper jaut > Module 10: Junos SNMP Scripts

Module 10: Junos SNMP Scripts

Supported in Junos 15.1 and later. Scripts written in Python are supported in Junos 16.1 and later.

Junos maps unused SNMP OID’s to SNMP scripts. An SNMP scripts receives the SNMP OID and SNMP action as parameters. SLAX scripts obtain this information through the <snmp-script-input> element. Python scripts can use the jcs.get_snmp_oid() and jcs.get_snmp_action() functions. SLAX scripts requrn data by writing to the output XML document. Python scripts use the jcs.emit_snmp_attributes() function. The supported SNMP data types are Counter32, Counter64, Integer32, Unsigned32, Octet String.

SLAX Script Boilerplate

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";

match / {
    <snmp-script-results> {
        var $snmp-action = snmp-script-input/snmp-action;
        var $snmp-oid = snmp-script-input/snmp-oid;

        /* ... */

        <snmp-oid> $snmp-oid;
        <snmp-type> $snmp-type;
        <snmp-value> $snmp-value;
    }
}

Python Script Boilerplate

import jcs

def main():
    snmp_action = jcs.get_snmp_action()
    snmp_oid = jcs.get_snmp_oid()

    # ...

    jcs_emit_snmp_attributes(snmp_oid, snmp_type, snmp_value)

if __name__ == '__main__':
    main()

Executing Scripts

Copy the script file to the appropriate directory; /var/db/scripts/snmp or /config/scripts/snmp (the latter if system scripts load-scripts-from-flash is enabled. Enable the individual script and map to an SNMP OID in system scripts snmp.

system {
    scripts {
        snmp {
            set file sample.slax oid .1.3.6.1.4.1.2636.13.61.1.9.1.1;
        }
    }
}

To execute Python scripts, enable language python.

SLAX Complete Example

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";

match / {
    <snmp-script-results> {
        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> "111";
                }
            }
            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> "112";
                }
            }
        }
        else if ($snmp-action == 'get-next') {
            /* Note this is the PARENT OID */
            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> "111";
                }
            }
            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> "112";
                }
            }
        }
    }
}

Test the script by generating SNMP requests;

show snmp mib get .1.3.6.1.4.1.2636.13.61.1.9.1.1
show snmp mib get .1.3.6.1.4.1.2636.13.61.1.9.1.1.1
show snmp mib get .1.3.6.1.4.1.2636.13.61.1.9.1.1.2
show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.9.1.1
show snmp mib get-next .1.3.6.1.4.1.2636.13.61.1.9.1.2
show snmp mib walk .1.3.6.1.4.1.2636.13.61.1.9.1.1
show log messages | match snmp_oid

Python Complete Example

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", "111")
        elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.2':
            jcs_emit_snmp_attributes(snmp_oid, "Integer32", "112")
    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", "111")
        elif snmp_oid == '.1.3.6.1.4.1.2636.13.61.1.9.1.1.2':
            jcs_emit_snmp_attributes('.1.3.6.1.4.1.2636.13.61.1.9.1.1.2', "Integer32", "112")

if __name__ == '__main__':
    main()