void snmp_enable_filelog(const char *logfilename, int dont_zero_log) { netsnmp_log_handler *logh; /* * don't disable ALL filelogs whenever a new one is enabled. * this prevents '-Lf file' from working in snmpd, as the * call to set up /var/log/snmpd.log will disable the previous * log setup. * snmp_disable_filelog(); */ if (logfilename) { logh = netsnmp_find_loghandler( logfilename ); if (!logh) logh = netsnmp_register_filelog_handler( logfilename, LOG_DEBUG, 0, dont_zero_log ); else netsnmp_enable_filelog(logh, dont_zero_log); } else { for (logh = logh_head; logh; logh = logh->next) if (logh->type == NETSNMP_LOGHANDLER_FILE) netsnmp_enable_filelog(logh, dont_zero_log); } }
/* * close and reopen all file based logs, to allow logfile * rotation. */ void netsnmp_logging_restart(void) { netsnmp_log_handler *logh; #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG int doneone = 0; #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */ for (logh = logh_head; logh; logh = logh->next) { if (0 == logh->enabled) continue; #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG if (logh->type == NETSNMP_LOGHANDLER_SYSLOG) { snmp_disable_syslog_entry(logh); snmp_enable_syslog_ident(logh->token,(int)(intptr_t)logh->magic); doneone = 1; } #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */ #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE if (logh->type == NETSNMP_LOGHANDLER_FILE && !doneone) { snmp_disable_filelog_entry(logh); /** hmm, don't zero status isn't saved.. i think it's * safer not to overwrite, in case a hup is just to * re-read config files... */ netsnmp_enable_filelog(logh, 1); } #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */ } }
netsnmp_log_handler * netsnmp_register_filelog_handler(const char* logfilename, int priority, int priority_max, int dont_zero_log) { netsnmp_log_handler *logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_FILE, priority ); if (NULL == logh) return NULL; logh->pri_max = priority_max; logh->token = strdup(logfilename); if (-1 == dont_zero_log) dont_zero_log = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPEND_LOGFILES); netsnmp_enable_filelog(logh, dont_zero_log); return logh; }
/* * close and reopen all file based logs, to allow logfile * rotation. */ void netsnmp_logging_restart(void) { netsnmp_log_handler *logh; for (logh = logh_head; logh; logh = logh->next) { if (0 == logh->enabled) continue; if (logh->type == NETSNMP_LOGHANDLER_SYSLOG) { snmp_disable_syslog_entry(logh); snmp_enable_syslog_ident(logh->token,(int)(intptr_t)logh->magic); } else if (logh->type == NETSNMP_LOGHANDLER_FILE) { snmp_disable_filelog_entry(logh); /** hmm, don't zero status isn't saved.. i think it's * safer not to overwrite, in case a hup is just to * re-read config files... */ netsnmp_enable_filelog(logh, 1); } } }
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; optarg++; if (!*cp) cp = &missing_opt; /* * Support '... -Lx=value ....' syntax */ if (*optarg == '=') { optarg++; } /* * and '.... "-Lx value" ....' (*with* the quotes) */ while (*optarg && isspace(*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; } 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) { 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) { 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(0)); logh->magic = (void *)(intptr_t)facility; snmp_enable_syslog_ident(snmp_log_syslogname(0), 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; }