SDK Script snmp-agent.are

snmp-agent.are
/* DESC: This script extends MIB entries of the SNMP agent
 * Copyright (C) 2014 NetModule AG, Switzerland
 */
 
template entry {
    name;
    ext;
    type;
    mode;
 
    int entry (string name, int ext, string type, string mode) {
        /* register entry */
        if (nb_snmp_register(name, ext, type, mode) < 0) {
            nb_syslog("unable to register %s", name);
            exit(1);
        } 
        this.name = name;
        this.ext = ext;
        this.type = type;
        this.mode = mode;
 
        return 0;
    }
};
 
cnt = 0;
entries[cnt++] = new entry("ETH1_MAC",   1, "s", "ro");
entries[cnt++] = new entry("ETH1_STATE", 2, "s", "rw");
entries[cnt++] = new entry("ETH1_SPEED", 3, "i", "ro");
 
entries[cnt++] = new entry("ETH2_MAC",   11, "s", "ro");
entries[cnt++] = new entry("ETH2_STATE", 12, "s", "rw");
entries[cnt++] = new entry("ETH2_SPEED", 13, "i", "ro");
 
if (nb_snmp_link() != 0) {
    nb_syslog("unable to link entries to agent");
    exit(1);
}
nb_syslog("linked entries to snmp agent");
 
while (1) {
    /* wait 5s for any commits on our read-write entries */
    e = nb_snmp_listen(5);
    if (is_struct(e)) {
        name = struct_get(e, "name");
        value = struct_get(e, "value");
 
        nb_syslog("%s has been set to %s", name, value);
    }
 
    /* update read-only entries */
    status = nb_status("eth");
    for (i = 0; i < cnt; i++) {
        e = entries[i];
        value = struct_get(status, e.name);
 
        if (nb_snmp_update(e.name, value) != 0) {
            nb_syslog("unable to update %s to %s", e.name, value);
        } else {
            nb_syslog("updated %s to %s", e.name, value);
        }
    }
 
    sleep(1);
}
 
nb_snmp_unlink();
nb_syslog("unlinked entries, terminating");
 
 
exit(0);