static int register_value_handlers(void) { int i; for (i = 0; i < value_list.num_values; i++) { int result; netsnmp_handler_registration *value_reg; snmp_value_t *value = &(value_list.values[i]); value->val_oid_len = MAX_OID_LEN; if ((result = convert_string_to_oid(value->oid_str, value->val_oid, &value->val_oid_len)) != 0) { return -1; } DEBUG("Registering handle for stat name %s, ", value->stat_name); DEBUG_OID(value->val_oid, value->val_oid_len); value_reg = netsnmp_create_handler_registration(value->stat_name, value_handler, value->val_oid, value->val_oid_len, 0); netsnmp_register_instance(value_reg); } return 0; }
/** * \brief Initializes the deviceUserDesc module */ void init_deviceUserDesc(void) { const oid deviceUserDesc_oid[] = { 1,3,6,1,4,1,35990,3,1,1,10 }; g_debug("deviceUserDesc initializing"); netsnmp_register_instance( netsnmp_create_handler_registration("deviceUserDesc", handle_deviceUserDesc, deviceUserDesc_oid, OID_LENGTH(deviceUserDesc_oid), HANDLER_CAN_RWRITE )); }
int netsnmp_register_watched_instance(netsnmp_handler_registration *reginfo, netsnmp_watcher_info *watchinfo) { netsnmp_mib_handler *whandler; whandler = netsnmp_get_watcher_handler(); whandler->myvoid = (void *)watchinfo; netsnmp_inject_handler(reginfo, whandler); return netsnmp_register_instance(reginfo); }
/* Register OID handler for a sensor type */ static int reg_snmp_sensor_helper__(int sensor_type, oid *reg_oid, size_t oid_len, int dev_idx) { int ret = MIB_REGISTRATION_FAILED; Netsnmp_Node_Handler *handler = onlp_snmp_sensor_handler__; onlp_snmp_sensor_ctrl_t *ss_type = get_sensor_ctrl__(sensor_type); uint32_t col_cnt = ss_type->handler_cnt; char *table_name = ss_type->name; /* Use this to increase the column index in oid */ u_long *oid_col = ®_oid[oid_len - OID_SENSOR_COL_INDEX]; /* Use this to increase the dev index of oid */ reg_oid[oid_len - OID_SENSOR_DEV_INDEX] = dev_idx; snmp_log(LOG_DEBUG, "oid registrations: %s for dev_idx=%d", table_name, dev_idx); /* * Caller makes sure that this loop is run * since *oid_col starts as 1st col */ for (; *oid_col < col_cnt; (*oid_col)++) { netsnmp_handler_registration *reg; if (!ss_type->handlers[*oid_col]) continue; reg = netsnmp_create_handler_registration(table_name, handler, reg_oid, oid_len, HANDLER_CAN_RONLY); /* Ofad enables verbose/trace to see this */ snmp_log(LOG_INFO, "registering handler for %s column %ld, index %d", table_name, *oid_col, dev_idx); /* If reg is null, this returns error */ ret = netsnmp_register_instance(reg); if(ret) { break; } } return ret; }
/** Initializes the hypervisor module */ void init_vmInfo(void) { const oid vmNumber_oid[] = { 1,3,6,1,2,1,VM_MIB_OID,1,2 }; const oid vmTableLastChange_oid[] = { 1,3,6,1,2,1,VM_MIB_OID,1,3 }; DEBUGMSGTL(("vmInfo", "Initializing\n")); netsnmp_register_instance( netsnmp_create_handler_registration("vmNumber", handle_vmNumber, vmNumber_oid, OID_LENGTH(vmNumber_oid), HANDLER_CAN_RONLY )); netsnmp_register_instance( netsnmp_create_handler_registration("vmTableLastChange", handle_vmTableLastChange, vmTableLastChange_oid, OID_LENGTH(vmTableLastChange_oid), HANDLER_CAN_RONLY )); }
void init_delayed_instance(void) { static oid my_delayed_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 1, 2, 0 }; /* * delayed handler test */ netsnmp_handler_registration *my_test; my_test = netsnmp_create_handler_registration("delayed_instance_example", delayed_instance_handler, my_delayed_oid, OID_LENGTH(my_delayed_oid), HANDLER_CAN_RWRITE); netsnmp_register_instance(my_test); }
int netsnmp_register_num_file_instance(const char *name, const oid * reg_oid, size_t reg_oid_len, const char *file_name, int asn_type, int mode, Netsnmp_Node_Handler * subhandler, const char *contextName) { netsnmp_handler_registration *myreg; netsnmp_num_file_instance *nfi; if ((NULL == name) || (NULL == reg_oid) || (NULL == file_name)) { snmp_log(LOG_ERR, "bad parameter to netsnmp_register_num_file_instance\n"); return MIB_REGISTRATION_FAILED; } nfi = SNMP_MALLOC_TYPEDEF(netsnmp_num_file_instance); if ((NULL == nfi) || (NULL == (nfi->file_name = strdup(file_name)))) { snmp_log(LOG_ERR, "could not not allocate memory\n"); if (NULL != nfi) free(nfi); /* SNMP_FREE overkill on local var */ return MIB_REGISTRATION_FAILED; } nfi->refcnt = 1; myreg = get_reg(name, "file_num_handler", reg_oid, reg_oid_len, nfi, mode, netsnmp_instance_num_file_handler, subhandler, contextName); if (NULL == myreg) { netsnmp_num_file_instance_deref(nfi); return MIB_REGISTRATION_FAILED; } nfi->type = asn_type; if (HANDLER_CAN_RONLY == mode) return netsnmp_register_read_only_instance(myreg); return netsnmp_register_instance(myreg); }
void netsnmp_parse_override(const char *token, char *line) { char *cp; char buf[SNMP_MAXBUF], namebuf[SNMP_MAXBUF]; int readwrite = 0; oid oidbuf[MAX_OID_LEN]; size_t oidbuf_len = MAX_OID_LEN; int type; override_data *thedata; netsnmp_handler_registration *the_reg; cp = copy_nword(line, namebuf, sizeof(namebuf) - 1); if (strcmp(namebuf, "-rw") == 0) { readwrite = 1; cp = copy_nword(cp, namebuf, sizeof(namebuf) - 1); } if (!cp) { config_perror("no oid specified"); return; } if (!snmp_parse_oid(namebuf, oidbuf, &oidbuf_len)) { config_perror("illegal oid"); return; } cp = copy_nword(cp, buf, sizeof(buf) - 1); if (!cp && strcmp(buf, "null") != 0) { config_perror("no variable value specified"); return; } { struct { const char* key; int value; } const strings[] = { { "counter", ASN_COUNTER }, { "counter64", ASN_COUNTER64 }, { "integer", ASN_INTEGER }, { "ipaddress", ASN_IPADDRESS }, { "nsap", ASN_NSAP }, { "null", ASN_NULL }, { "object_id", ASN_OBJECT_ID }, { "octet_str", ASN_OCTET_STR }, { "opaque", ASN_OPAQUE }, #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES { "opaque_counter64", ASN_OPAQUE_COUNTER64 }, { "opaque_double", ASN_OPAQUE_DOUBLE }, { "opaque_float", ASN_OPAQUE_FLOAT }, { "opaque_i64", ASN_OPAQUE_I64 }, { "opaque_u64", ASN_OPAQUE_U64 }, #endif { "timeticks", ASN_TIMETICKS }, { "uinteger", ASN_GAUGE }, { "unsigned", ASN_UNSIGNED }, { NULL, 0 } }, * run; for(run = strings; run->key && strcasecmp(run->key, buf) < 0; ++run); if(run->key && strcasecmp(run->key, buf) == 0) type = run->value; else { config_perror("unknown type specified"); return; } } if (cp) copy_nword(cp, buf, sizeof(buf) - 1); else buf[0] = 0; thedata = SNMP_MALLOC_TYPEDEF(override_data); if (!thedata) { config_perror("memory allocation failure"); return; } thedata->type = type; switch (type) { case ASN_INTEGER: MALLOC_OR_DIE(sizeof(long)); *((long *) thedata->value) = strtol(buf, NULL, 0); break; case ASN_COUNTER: case ASN_TIMETICKS: case ASN_UNSIGNED: MALLOC_OR_DIE(sizeof(u_long)); *((u_long *) thedata->value) = strtoul(buf, NULL, 0); break; case ASN_OCTET_STR: case ASN_BIT_STR: if (buf[0] == '0' && buf[1] == 'x') { /* * hex */ thedata->value_len = hex_to_binary2((u_char *)(buf + 2), strlen(buf) - 2, (char **) &thedata->value); } else { thedata->value = strdup(buf); thedata->value_len = strlen(buf); } break; case ASN_OBJECT_ID: read_config_read_objid(buf, (oid **) & thedata->value, &thedata->value_len); break; case ASN_NULL: thedata->value_len = 0; break; default: SNMP_FREE(thedata); config_perror("illegal/unsupported type specified"); return; } if (!thedata->value && thedata->type != ASN_NULL) { config_perror("memory allocation failure"); free(thedata); return; } the_reg = SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration); if (!the_reg) { config_perror("memory allocation failure"); free(thedata); return; } the_reg->handlerName = strdup(namebuf); the_reg->priority = 255; the_reg->modes = (readwrite) ? HANDLER_CAN_RWRITE : HANDLER_CAN_RONLY; the_reg->handler = netsnmp_create_handler("override", override_handler); the_reg->rootoid = snmp_duplicate_objid(oidbuf, oidbuf_len); the_reg->rootoid_len = oidbuf_len; if (!the_reg->rootoid || !the_reg->handler || !the_reg->handlerName) { if (the_reg->handler) SNMP_FREE(the_reg->handler->handler_name); SNMP_FREE(the_reg->handler); SNMP_FREE(the_reg->handlerName); SNMP_FREE(the_reg); config_perror("memory allocation failure"); free(thedata); return; } the_reg->handler->myvoid = thedata; if (netsnmp_register_instance(the_reg)) { config_perror("oid registration failed within the agent"); SNMP_FREE(thedata->value); free(thedata); return; } }
void init_testhandler(void) { /* * we're registering at .1.2.3.4 */ netsnmp_handler_registration *my_test; netsnmp_table_registration_info *table_info; u_long ind1; netsnmp_table_data *table; netsnmp_table_data_set *table_set; netsnmp_table_row *row; DEBUGMSGTL(("testhandler", "initializing\n")); /* * basic handler test */ netsnmp_register_handler(netsnmp_create_handler_registration ("myTest", my_test_handler, my_test_oid, 4, HANDLER_CAN_RONLY)); /* * instance handler test */ netsnmp_register_instance(netsnmp_create_handler_registration ("myInstance", my_test_instance_handler, my_instance_oid, 5, HANDLER_CAN_RWRITE)); netsnmp_register_ulong_instance("myulong", my_data_ulong_instance, 4, &my_ulong, NULL); /* * table helper test */ my_test = netsnmp_create_handler_registration("myTable", my_test_table_handler, my_table_oid, 4, HANDLER_CAN_RONLY); if (!my_test) return; table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); if (table_info == NULL) return; netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, ASN_INTEGER, 0); table_info->min_column = 3; table_info->max_column = 3; netsnmp_register_table(my_test, table_info); /* * data table helper test */ /* * we'll construct a simple table here with two indexes: an * integer and a string (why not). It'll contain only one * column so the data pointer is merely the data in that * column. */ table = netsnmp_create_table_data("data_table_test"); netsnmp_table_data_add_index(table, ASN_INTEGER); netsnmp_table_data_add_index(table, ASN_OCTET_STR); /* * 1 partridge in a pear tree */ row = netsnmp_create_table_data_row(); ind1 = 1; netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1)); netsnmp_table_row_add_index(row, ASN_OCTET_STR, "partridge", strlen("partridge")); row->data = (void *) "pear tree"; netsnmp_table_data_add_row(table, row); /* * 2 turtle doves */ row = netsnmp_create_table_data_row(); ind1 = 2; netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1)); netsnmp_table_row_add_index(row, ASN_OCTET_STR, "turtle", strlen("turtle")); row->data = (void *) "doves"; netsnmp_table_data_add_row(table, row); /* * we're going to register it as a normal table too, so we get the * automatically parsed column and index information */ table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); if (table_info == NULL) return; netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, ASN_OCTET_STR, 0); table_info->min_column = 3; table_info->max_column = 3; netsnmp_register_read_only_table_data (netsnmp_create_handler_registration ("12days", my_data_table_handler, my_data_table_oid, 4, HANDLER_CAN_RONLY), table, table_info); }
void m_parse (char *filename) { /* Open File */ int fd = open(filename, O_RDONLY); if (fd == -1) { snmp_log(LOG_ERR, "m_parse failed to open %s: %s\n", filename, strerror(errno)); return; } size_t filesize = lseek(fd, 0, SEEK_END); void *source = mmap(0, filesize, PROT_READ, MAP_SHARED, fd, 0); if (source == MAP_FAILED) { snmp_log(LOG_ERR, "m_parse failed to mmap %s: %s\n", filename, strerror(errno)); return; } /* Compile the line-matching RegExp. * Matches lines like: * * .1.3.6.1.3.94.1.6.1.5.16.0.0.5.30.54.117.16.0.0.0.0.0.0.0.0 = INTEGER: 2 * * Captures OID String, Type and Value */ const char *error; int erroffset; pcre *re = pcre_compile("^((?:\\.[0-9]+)+) =(?: ([\\w-]+)\\:)? \"?(.*)$", PCRE_MULTILINE, &error, &erroffset, NULL); if (!re) { snmp_log(LOG_ERR, "m_parse failed to compile line-matching RE\n"); return; } /* Parse the mmap'd input file */ int ovector[OVECCOUNT]; ovector[0] = 0; ovector[1] = 0; while (1) { int options = 0; int start_offset = ovector[1]; int rc = pcre_exec( re, /* the compiled pattern */ NULL, /* no extra data - we didn't study the pattern */ source, /* the subject string */ filesize, /* the length of the subject */ start_offset, /* starting offset in the subject */ options, /* options */ ovector, /* output vector for substring information */ OVECCOUNT); /* number of elements in the output vector */ if (rc == PCRE_ERROR_NOMATCH) { if (options == 0) break; ovector[1] = start_offset + 1; continue; /* Go round the loop again */ } /* Other matching errors are not recoverable. */ if (rc > 0) { /* Matched a walk line * 0=FullString 1(2)=OID 2(4)=Type 3(6)=Value */ char *oid_str = string_at_vector(source, ovector, 2); char *type_str = string_at_vector(source, ovector, 4); char *value_str = string_at_vector(source, ovector, 6); // snmp_log(LOG_INFO, "OID='%s' TYPE='%s' VALUE='%s'\n", oid_str, type_str, value_str); /* Extract the oid from the string */ size_t oid_len; oid *oid_array = oid_from_string(oid_str, &oid_len); /* Create a value struct from the strings */ m_value *val = value_from_string(type_str, value_str); if (val) { /* Add the val to the values hashtable */ i_hashtable_key *key = m_value_key(oid_str); i_hashtable_put(m_value_ht(), key, val); i_hashtable_key_free(key); /* Add SNMP Handler for the OID */ //printf ("Registering value %s\n", oid_str); netsnmp_register_instance( netsnmp_create_handler_registration(oid_str, m_value_snmp_handler, oid_array, oid_len, HANDLER_CAN_RONLY)); } } else { /* Other matching error occurred */ snmp_log(LOG_ERR, "m_parse other matching error occurred\n"); pcre_free(re); return; } } /* Clean up */ pcre_free(re); /* Clean up */ munmap(source, filesize); close(fd); }
void init_demo_module_10(void) { /* * the OID at which to register the exampleData variable. */ static oid longRunScalar_oid[] = {1, 3, 6, 1, 4, 1, 42, 2, 2, 4, 4, 10, 1, 1, 0}; /* * the OID at which to register the status variable. */ static oid status_oid[] = {1, 3, 6, 1, 4, 1, 42, 2, 2, 4, 4, 10, 1, 2, 0}; /* * the OID at which to register the refreshTime variable. */ static oid refreshTime_oid[] = {1, 3, 6, 1, 4, 1, 42, 2, 2, 4, 4, 10, 1, 3, 0}; netsnmp_handler_registration *myreg; /* * A debugging statement. Run the agent with -Ddemo_module_10 to see * the output of this debugging statement in /var/log/snmpd.log, by * default. Use the -L option to write debugging output to the * screen. */ DEBUGMSGTL(("demo_module_10", "Initializing\n")); /* * Creates a read-only registration handler named longRunScalar, * which calls the get_longRunScalar function to service snmp * requests for the longRunScalar_oid object. The OID_LENGTH * argument calculates the length of the longRunScalar_oid. */ myreg = netsnmp_create_handler_registration ("longRunScalar", get_longRunScalar, longRunScalar_oid, OID_LENGTH(longRunScalar_oid), HANDLER_CAN_RONLY); /* * Register the instance and handler. * */ netsnmp_register_read_only_instance(myreg); /* * Creates a read/write registration handler named getStatus, * which calls the get_status function to service snmp * requests for the status_oid object. The OID_LENGTH * argument calculates the length of the status_oid. */ myreg = netsnmp_create_handler_registration ("delayed_instance_handler", delayed_instance_handler, status_oid, OID_LENGTH(status_oid), HANDLER_CAN_RWRITE); /* * Register the instance and handler. * */ netsnmp_register_instance(myreg); /* * Creates a read-only registration handler named getTimestamp, * which calls the get_timestamp function to service snmp * get requests for the timestamp_oid object. The OID_LENGTH * argument calculates the length of the timestamp_oid. */ myreg = netsnmp_create_handler_registration ("getTimestamp", get_timestamp, refreshTime_oid, OID_LENGTH(refreshTime_oid), HANDLER_CAN_RONLY); /* * Register the instance and handler. * */ netsnmp_register_instance(myreg); }
void init_demo_module_7(void) { char *filexcon = "fileX"; char *fileycon = "fileY"; netsnmp_handler_registration *myreg1; int status; /* Create a read-only registration handler named filesize, which calls the get_filesize function to service snmp requests for the me1filesize_oid object. The OID_LENGTH argument calculates the length of the me1filesize_oid. */ DEBUGMSGTL(("demo_module_7", "Initializing\n")); myreg1 = netsnmp_create_handler_registration ("filesize", get_filesize, me1filesize_oid, OID_LENGTH(me1filesize_oid), HANDLER_CAN_RONLY); myreg1->contextName = filexcon; status = netsnmp_register_read_only_instance(myreg1); DEBUGMSGTL(("demo_module_7", "init reg1 status %d:\n", status)); myreg1 = netsnmp_create_handler_registration ("filesize", get_filesize, me1filesize_oid, OID_LENGTH(me1filesize_oid), HANDLER_CAN_RONLY); myreg1->contextName = fileycon; status = netsnmp_register_read_only_instance(myreg1); DEBUGMSGTL(("demo_module_7", "init reg2 status %d:\n", status)); /* Create a read-write registration handler named filesize, which calls the set_createContext function to service snmp requests for the me1createContext_oid object. The OID_LENGTH argument calculates the length of the me1createContext_oid. */ myreg1 = netsnmp_create_handler_registration ("filesize", set_createContext, me1createContext_oid, OID_LENGTH(me1createContext_oid), HANDLER_CAN_RWRITE); status = netsnmp_register_instance(myreg1); DEBUGMSGTL(("filesize", "init reg3 status %d:\n", status)); /* Create a read-write registration handler named filesize, which calls the set_removeContext function to service snmp requests for the me1removeContext_oid object. The OID_LENGTH argument calculates the length of the me1removeContext_oid. */ myreg1 = netsnmp_create_handler_registration ("filesize", set_removeContext, me1removeContext_oid, OID_LENGTH(me1removeContext_oid), HANDLER_CAN_RWRITE); status = netsnmp_register_instance(myreg1); DEBUGMSGTL(("demo_module_7", "init reg4 status %d:\n", status)); }