Exemplo n.º 1
0
netsnmp_log_handler *
netsnmp_register_stdio_loghandler(int is_stdout, int priority, int priority_max,
                                  const char *tok)
{
    netsnmp_log_handler *logh =
        netsnmp_register_loghandler(NETSNMP_LOGHANDLER_STDERR, priority);
    if (NULL == logh) {
        return NULL;
    }
    if (is_stdout) {
        netsnmp_set_line_buffering(stdout);
        logh->imagic = 1; /* stdout, not stderr */
    } else
        netsnmp_set_line_buffering(stderr);

    logh->pri_max = priority_max;
    if (tok)
        logh->token   = strdup(tok);
    return logh;
}
Exemplo n.º 2
0
void
snmp_log_string(int priority, const char *str)
{
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
    static int stderr_enabled = 0;
    static netsnmp_log_handler lh = { 1, 0, 0, 0, "stderr",
                                      log_handler_stdouterr, 0, NULL,  NULL,
                                      NULL };
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
    netsnmp_log_handler *logh;

    /*
     * We've got to be able to log messages *somewhere*!
     * If you don't want stderr logging, then enable something else.
     */
    if (0 == logh_enabled) {
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
        if (!stderr_enabled) {
            ++stderr_enabled;
            netsnmp_set_line_buffering(stderr);
        }
        log_handler_stdouterr( &lh, priority, str );
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */

        return;
    }
#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
    else if (stderr_enabled) {
        stderr_enabled = 0;
        log_handler_stdouterr( &lh, LOG_INFO,
                               "Log handling defined - disabling stderr\n" );
    }
#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
        

    /*
     * Start at the given priority, and work "upwards"....
     */
    if (priority > LOG_DEBUG)
        priority = LOG_DEBUG;
    logh = logh_priorities[priority];
    for ( ; logh; logh = logh->next ) {
        /*
         * ... but skipping any handlers with a "maximum priority"
         *     that we have already exceeded. And don't forget to
         *     ensure this logging is turned on (see snmp_disable_stderrlog
         *     and its cohorts).
         */
        if (logh->enabled && (priority >= logh->pri_max))
            logh->handler( logh, priority, str );
    }
}
Exemplo n.º 3
0
void
netsnmp_enable_filelog(netsnmp_log_handler *logh, int dont_zero_log)
{
    FILE *logfile;

    if (!logh)
        return;

    if (!logh->magic) {
        logfile = fopen(logh->token, dont_zero_log ? "a" : "w");
        if (!logfile) {
	    snmp_log_perror(logh->token);
            return;
	}
        logh->magic = (void*)logfile;
        netsnmp_set_line_buffering(logfile);
    }
    netsnmp_enable_this_loghandler(logh);
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
0
int
snmp_log_options(char *optarg, int argc, char *const *argv)
{
    char           *cp = optarg;
        /*
	 * Hmmm... this doesn't seem to work.
	 * The main agent 'getopt' handling assumes
	 *   that the -L option takes an argument,
	 *   and objects if this is missing.
	 * Trying to differentiate between
	 *   new-style "-Lx", and old-style "-L xx"
	 *   is likely to be a major headache.
	 */
    char            missing_opt = 'e';	/* old -L is new -Le */
    int             priority = LOG_DEBUG;
    int             pri_max  = LOG_EMERG;
    int             inc_optind = 0;
    netsnmp_log_handler *logh;

    DEBUGMSGT(("logging:options", "optarg: '%s', argc %d, argv '%s'\n",
               optarg, argc, argv ? argv[0] : "NULL"));
    optarg++;
    if (!*cp)
        cp = &missing_opt;

    /*
     * Support '... -Lx=value ....' syntax
     */
    if (*optarg == '=') {
        optarg++;
    }
    /*
     * and '.... "-Lx value" ....'  (*with* the quotes)
     */
    while (*optarg && isspace((unsigned char)(*optarg))) {
        optarg++;
    }
    /*
     * Finally, handle ".... -Lx value ...." syntax
     *   (*without* surrounding quotes)
     */
    if ((!*optarg) && (NULL != argv)) {
        /*
         * We've run off the end of the argument
         *  so move on to the next.
         * But we might not actually need it, so don't
	 *  increment optind just yet!
         */
        optarg = argv[optind];
        inc_optind = 1;
    }

    DEBUGMSGT(("logging:options", "*cp: '%c'\n", *cp));
    switch (*cp) {

    /*
     * Log to Standard Error
     */
    case 'E':
        priority = decode_priority( &optarg, &pri_max );
        if (priority == -1)  return -1;
        if (inc_optind)
            optind++;
        /* Fallthrough */
    case 'e':
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_STDERR, priority);
        if (logh) {
            netsnmp_set_line_buffering(stderr);
            logh->pri_max = pri_max;
            logh->token   = strdup("stderr");
	}
        break;

    /*
     * Log to Standard Output
     */
    case 'O':
        priority = decode_priority( &optarg, &pri_max );
        if (priority == -1)  return -1;
        if (inc_optind)
            optind++;
        /* Fallthrough */
    case 'o':
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_STDERR, priority);
        if (logh) {
            netsnmp_set_line_buffering(stdout);
            logh->pri_max = pri_max;
            logh->token   = strdup("stdout");
            logh->imagic  = 1;	    /* stdout, not stderr */
	}
        break;

    /*
     * Log to a named file
     */
    case 'F':
        priority = decode_priority( &optarg, &pri_max );
        if (priority == -1 || !argv)  return -1;
        optarg = argv[++optind];
        /* Fallthrough */
    case 'f':
        if (inc_optind)
            optind++;
        if (!optarg) {
            fprintf(stderr, "Missing log file\n");
            return -1;
        }
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_FILE, priority);
        if (logh) {
            logh->pri_max = pri_max;
            logh->token   = strdup(optarg);
            netsnmp_enable_filelog(logh,
                                   netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
                                                          NETSNMP_DS_LIB_APPEND_LOGFILES));
	}
        break;

    /*
     * Log to syslog
     */
    case 'S':
        priority = decode_priority( &optarg, &pri_max );
        if (priority == -1 || !argv)  return -1;
        if (!optarg[0]) {
            /* The command line argument with priority does not contain log
             * facility. The facility must be in next argument then. */
            optind++;
            if (optind < argc)
                optarg = argv[optind];
        }
        /* Fallthrough */
    case 's':
        if (inc_optind)
            optind++;
        if (!optarg) {
            fprintf(stderr, "Missing syslog facility\n");
            return -1;
        }
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_SYSLOG, priority);
        if (logh) {
            int facility = decode_facility(optarg);
            if (facility == -1)  return -1;
            logh->pri_max = pri_max;
            logh->token   = strdup(snmp_log_syslogname(NULL));
            logh->magic   = (void *)(intptr_t)facility;
	    snmp_enable_syslog_ident(snmp_log_syslogname(NULL), facility);
	}
        break;

    /*
     * Don't log 
     */
    case 'N':
        priority = decode_priority( &optarg, &pri_max );
        if (priority == -1)  return -1;
        if (inc_optind)
            optind++;
        /* Fallthrough */
    case 'n':
        /*
         * disable all logs to clean them up (close files, etc),
         * remove all log handlers, then register a null handler.
         */
        snmp_disable_log();
        while(NULL != logh_head)
            netsnmp_remove_loghandler( logh_head );
        logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE, priority);
        if (logh) {
            logh->pri_max = pri_max;
	}
        break;

    default:
        fprintf(stderr, "Unknown logging option passed to -L: %c.\n", *cp);
        return -1;
    }
    return 0;
}