// example2 — SNMP WALK
// Usage: ./example2 [host] [community]
module example2;

import snmp;
import std.stdio : writefln, stderr;

void main(string[] args)
{
    init_snmp("example2");

    string host      = args.length > 1 ? args[1] : "udp:localhost:161";
    string community = args.length > 2 ? args[2] : "public";

    auto sess = SNMPSession.open(host, community, SNMPVersion.v2c);
    if (!sess.isOpen)
    {
        snmp_perror("snmp_open");
        return;
    }

    auto subtree = OID.fromString("1.3.6.1.2.1.1");
    auto current  = OID.fromString("1.3.6.1.2.1.1");

    if (!subtree.isValid || !current.isValid)
    {
        stderr.writefln("Failed to parse base OID");
        return;
    }

    int count;
    while (true)
    {
        auto resp = sess.getNext(current);
        if (!resp.ok)
            break;

        auto v = resp.variables;
        if (!v.isValid)
            break;

        auto returned = v.toOID();
        if (!subtree.isPrefixOf(returned))
            break;

        writefln("%s = %s", v.oidString, v.valueString);
        current = returned;
        ++count;
    }

    writefln("\n%d object(s) found.", count);
}