Esempio n. 1
0
int
main(int argc, char *argv[])
{
    netsnmp_session session, *ss;
    int            total_entries = 0;

    netsnmp_set_line_buffering(stdout);

    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, 
                           NETSNMP_DS_LIB_QUICK_PRINT, 1);

    /*
     * get the common command line arguments 
     */
    switch (snmp_parse_args(argc, argv, &session, "C:", optProc)) {
    case NETSNMP_PARSE_ARGS_ERROR:
        exit(1);
    case NETSNMP_PARSE_ARGS_SUCCESS_EXIT:
        exit(0);
    case NETSNMP_PARSE_ARGS_ERROR_USAGE:
        usage();
        exit(1);
    default:
        break;
    }

    /*
     * get the initial object and subtree 
     */
    /*
     * specified on the command line 
     */
    if (optind + 1 != argc) {
        fprintf(stderr, "Must have exactly one table name\n");
        usage();
        exit(1);
    }

    rootlen = MAX_OID_LEN;
    if (!snmp_parse_oid(argv[optind], root, &rootlen)) {
        snmp_perror(argv[optind]);
        exit(1);
    }
    localdebug = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
                                        NETSNMP_DS_LIB_DUMP_PACKET);

    get_field_names();
    reverse_fields();

    /*
     * open an SNMP session 
     */
    SOCK_STARTUP;
    ss = snmp_open(&session);
    if (ss == NULL) {
        /*
         * diagnose snmp_open errors with the input netsnmp_session pointer 
         */
        snmp_sess_perror("snmptable", &session);
        SOCK_CLEANUP;
        exit(1);
    }

#ifndef NETSNMP_DISABLE_SNMPV1
    if (ss->version == SNMP_VERSION_1)
        use_getbulk = 0;
#endif

    do {
        entries = 0;
        allocated = 0;
        if (!headers_only) {
            if (use_getbulk)
                getbulk_table_entries(ss);
            else
                get_table_entries(ss);
        }

        if (exitval) {
            snmp_close(ss);
            SOCK_CLEANUP;
            return exitval;
        }

        if (entries || headers_only)
            print_table();

        if (data) {
            free (data);
            data = NULL;
        }

        if (indices) {
            free (indices);
            indices = NULL;
        }

        total_entries += entries;

    } while (!end_of_table);

    snmp_close(ss);
    SOCK_CLEANUP;

    if (total_entries == 0)
        printf("%s: No entries\n", table_name);
    if (extra_columns)
	printf("%s: WARNING: More columns on agent than in MIB\n", table_name);

    return 0;
}
/********************
 * pl_fact_exists
 ********************/
static foreign_t
pl_fact_exists(term_t pl_name,
               term_t pl_fields, term_t pl_list, control_t handle)
{
    context_t  *ctx;
    char       *name, factname[64];
    fid_t       frame;
    term_t      pl_values;
    OhmFact    *f;
    
    switch (PL_foreign_control(handle)) {
    case PL_FIRST_CALL:
        if (!PL_is_list(pl_fields) || /*!PL_is_list(pl_list) ||*/
            !PL_get_chars(pl_name, &name, CVT_ALL))
            PL_fail;
        strncpy(factname, name, sizeof(factname));
        factname[sizeof(factname)-1] = '\0';

        if ((ctx = malloc(sizeof(*ctx))) == NULL)
            PL_fail;
        memset(ctx, 0, sizeof(*ctx));
        
        if (get_field_names(ctx, pl_fields) != 0) {
            free(ctx);
            PL_fail;
        }
        
        ctx->store = ohm_fact_store_get_fact_store();
        ctx->facts = ohm_fact_store_get_facts_by_name(ctx->store, factname);
        break;
        
    case PL_REDO:
        ctx = PL_foreign_context_address(handle);
        break;
        
    case PL_CUTTED:
        ctx = PL_foreign_context_address(handle);
        goto nomore;

    default:
        PL_fail;
    }


    /* XXX TODO: shouldn't we discard the frame here instead of closing them */

    frame = PL_open_foreign_frame();
    while (ctx->facts != NULL) {
        f = (OhmFact *)ctx->facts->data;
        ctx->facts = g_slist_next(ctx->facts);

        if (!fact_values(ctx, f, &pl_values) && PL_unify(pl_list, pl_values)) {
            PL_close_foreign_frame(frame); /* PL_discard_foreign_frame ??? */
            PL_retry_address(ctx);
        }
        
        PL_rewind_foreign_frame(frame);
    }
    PL_close_foreign_frame(frame);  /* PL_discard_foreign_frame ??? */
    
 nomore:
    if (ctx->fields)
        free(ctx->fields);
    free(ctx);
    PL_fail;
}