int swrun_count_processes_by_regex(pcre *regexp) { netsnmp_swrun_entry *entry; netsnmp_iterator *it; int i = 0; int found_ndx[30]; int found; char fullCommand[64 + 128 + 128 + 3]; netsnmp_cache *swrun_cache = netsnmp_swrun_cache(); netsnmp_container *swrun_container = netsnmp_swrun_container(); if ( !swrun_container || !regexp ) return 0; /* or -1 */ it = CONTAINER_ITERATOR( swrun_container ); while ((entry = (netsnmp_swrun_entry*)ITERATOR_NEXT( it )) != NULL) { // need to assemble full command back so regexps can get full picture sprintf(fullCommand, "%s %s", entry->hrSWRunPath, entry->hrSWRunParameters); found = pcre_exec(regexp, NULL, fullCommand, strlen(fullCommand), 0, 0, found_ndx, 30); if (found > 0) { i++; } } ITERATOR_RELEASE( it ); return i; }
/** * initialization */ void init_swrun(void) { DEBUGMSGTL(("swrun:access", "init\n")); if (1 == _swrun_init) return; _swrun_init = 1; (void)netsnmp_swrun_container(); netsnmp_arch_swrun_init(); (void) netsnmp_swrun_cache(); }
/** Initialize the hrSWRunTable table by defining its contents and how it's structured */ void initialize_table_hrSWRunTable(void) { netsnmp_handler_registration *reg; netsnmp_mib_handler *handler = NULL; #ifndef NETSNMP_NO_WRITE_SUPPORT #ifdef NETSNMP_INCLUDE_HRSWRUN_WRITE_SUPPORT # define SWRUN_ACCESS_LEVEL HANDLER_CAN_RWRITE #else # define SWRUN_ACCESS_LEVEL HANDLER_CAN_RONLY #endif #else /* !NETSNMP_NO_WRITE_SUPPORT */ # define SWRUN_ACCESS_LEVEL HANDLER_CAN_RONLY #endif /* !NETSNMP_NO_WRITE_SUPPORT */ reg = netsnmp_create_handler_registration(MYTABLE, hrSWRunTable_handler, hrSWRunTable_oid, hrSWRunTable_oid_len, SWRUN_ACCESS_LEVEL); if (NULL == reg) { snmp_log(LOG_ERR,"error creating handler registration for " MYTABLE "\n"); goto bail; } reg->modes |= HANDLER_CAN_NOT_CREATE; table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); if (NULL == table_info) { snmp_log(LOG_ERR,"error allocating table registration for " MYTABLE "\n"); goto bail; } netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, /* index: hrSWRunIndex */ 0); table_info->min_column = COLUMN_HRSWRUNINDEX; table_info->max_column = COLUMN_HRSWRUNSTATUS; /************************************************* * * inject container_table helper */ handler = netsnmp_container_table_handler_get(table_info, netsnmp_swrun_container(), TABLE_CONTAINER_KEY_NETSNMP_INDEX); if (NULL == handler) { snmp_log(LOG_ERR,"error allocating table registration for " MYTABLE "\n"); goto bail; } if (SNMPERR_SUCCESS != netsnmp_inject_handler(reg, handler)) { snmp_log(LOG_ERR,"error injecting container_table handler for " MYTABLE "\n"); goto bail; } handler = NULL; /* reg has it, will reuse below */ /************************************************* * * inject cache helper */ handler = netsnmp_cache_handler_get(netsnmp_swrun_cache()); if (NULL == handler) { snmp_log(LOG_ERR, "error creating cache handler for " MYTABLE "\n"); goto bail; } if (SNMPERR_SUCCESS != netsnmp_inject_handler(reg, handler)) { snmp_log(LOG_ERR,"error injecting cache handler for " MYTABLE "\n"); goto bail; } handler = NULL; /* reg has it*/ if (SNMPERR_SUCCESS != netsnmp_register_table(reg, table_info)) { snmp_log(LOG_ERR,"error registering table handler for " MYTABLE "\n"); reg = NULL; /* it was freed inside netsnmp_register_table */ goto bail; } return; /* ok */ bail: /* not ok */ if (handler) netsnmp_handler_free(handler); if (table_info) netsnmp_table_registration_info_free(table_info); if (reg) netsnmp_handler_registration_free(reg); }