本文整理汇总了C++中snmp_parse_oid函数的典型用法代码示例。如果您正苦于以下问题:C++ snmp_parse_oid函数的具体用法?C++ snmp_parse_oid怎么用?C++ snmp_parse_oid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snmp_parse_oid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: view_oid
void view_oid (oid * it, size_t * len, const char *viewName, char *viewSubtree)
{
int i;
oid c_oid[SPRINT_MAX_LEN];
size_t c_oid_length = SPRINT_MAX_LEN;
int itIndex = VIEW_OID_LEN;
if (!snmp_parse_oid (viewSubtree, c_oid, &c_oid_length))
{
printf ("Error parsing subtree (%s)\n", viewSubtree);
exit (1);
}
*len = itIndex + 2 + strlen (viewName) + c_oid_length;
it[itIndex++] = strlen (viewName);
for (i = 0; i < (int) strlen (viewName); i++)
it[itIndex++] = viewName[i];
it[itIndex++] = c_oid_length;
for (i = 0; i < (int) c_oid_length; i++)
it[itIndex++] = c_oid[i];
/*
* sprint_objid(c_oid, it, *len);
*/
}
示例2: add
int
add(netsnmp_pdu *pdu, const char *mibnodename,
oid * index, size_t indexlen)
{
oid base[MAX_OID_LEN];
size_t base_length = MAX_OID_LEN;
memset(base, 0, MAX_OID_LEN * sizeof(oid));
if (!snmp_parse_oid(mibnodename, base, &base_length)) {
snmp_perror(mibnodename);
fprintf(stderr, "couldn't find mib node %s, giving up\n",
mibnodename);
#if HAVE_CURSES_H
endwin();
#endif
exit(1);
}
if (index && indexlen) {
memcpy(&(base[base_length]), index, indexlen * sizeof(oid));
base_length += indexlen;
}
DEBUGMSGTL(("add", "created: "));
DEBUGMSGOID(("add", base, base_length));
DEBUGMSG(("add", "\n"));
snmp_add_null_var(pdu, base, base_length);
return base_length;
}
示例3: snmp_set
/**
* The 'raw' SNMP set function. It should not be used directly in external
* files. Rather, a non-static wrapper function should be created here that
* makes the appropriate call in order to preserve source code readability.
*
* @return 1 if successful, 0 if not
*/
static int snmp_set(netsnmp_session *s, char *oid_str, char type, const char *val)
{
netsnmp_pdu *pdu;
oid the_oid[MAX_OID_LEN];
size_t oid_len;
pdu = snmp_pdu_create(SNMP_MSG_SET);
oid_len = MAX_OID_LEN;
// Parse the OID
if (snmp_parse_oid(oid_str, the_oid, &oid_len) == 0) {
snmp_perror(oid_str);
return 0;
}
// Build the packet to be sent
if (snmp_add_var(pdu, the_oid, oid_len, type, val) != 0) {
printf("type: %c, val: %s, oid_str: %s\n", type, val, oid_str);
snmp_perror("SNMP: Could not add var!");
return 0;
}
// Send the request
if (snmp_send(s, pdu) == 0) {
snmp_perror("SNMP: Error while sending!");
snmp_free_pdu(pdu);
return 0;
}
return 1;
}
示例4: simpleSNMPsend
void
simpleSNMPsend(struct snmp_session *session,
oid *name,
size_t name_length)
{
struct snmp_pdu *pdu;
oid uptime[MAX_OID_LEN];
size_t uptime_length;
/*
* Create PDU for GET request and add object names to request.
*/
pdu = snmp_pdu_create(SNMP_MSG_GET);
/*
* First insert uptime request into PDU.
*/
uptime_length = MAX_OID_LEN;
if (!snmp_parse_oid("system.sysUpTime.0",
uptime, &uptime_length)) {
printf("error parsing oid: system.sysUpTime.0\n");
}
snmp_add_null_var(pdu, uptime, uptime_length);
snmp_add_null_var(pdu, name, name_length);
/*
* Perform the request.
*/
snmp_send(session, pdu);
}
示例5: add_field
static inline void add_field (
netsnmp_pdu *trap_pdu,
u_char asn_type,
const char *prefix,
void *value,
size_t value_size)
{
oid _oid[MAX_OID_LEN];
size_t _oid_len = MAX_OID_LEN;
if (snmp_parse_oid(prefix, _oid, &_oid_len)) {
snmp_pdu_add_variable (trap_pdu, _oid, _oid_len, asn_type, (u_char *) value, value_size);
}
}
示例6: csnmp_config_add_data_values
static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
{
int i;
if (ci->values_num < 1)
{
WARNING ("snmp plugin: `Values' needs at least one argument.");
return (-1);
}
for (i = 0; i < ci->values_num; i++)
if (ci->values[i].type != OCONFIG_TYPE_STRING)
{
WARNING ("snmp plugin: `Values' needs only string argument.");
return (-1);
}
sfree (dd->values);
dd->values_len = 0;
dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
if (dd->values == NULL)
return (-1);
dd->values_len = (size_t) ci->values_num;
for (i = 0; i < ci->values_num; i++)
{
dd->values[i].oid_len = MAX_OID_LEN;
if (NULL == snmp_parse_oid (ci->values[i].value.string,
dd->values[i].oid, &dd->values[i].oid_len))
{
ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
ci->values[i].value.string);
free (dd->values);
dd->values = NULL;
dd->values_len = 0;
return (-1);
}
}
return (0);
} /* int csnmp_config_add_data_instance */
示例7: add_pdu_var
static void add_pdu_var(netsnmp_pdu *pdu, const char *mib_name, int id, const char *value)
{
oid oid_name[MAX_OID_LEN];
size_t name_length;
char buf[4096];
buf[4095] = '\0';
snprintf(buf, sizeof(buf)-1, "%s.%d", mib_name, id);
name_length = MAX_OID_LEN;
if (snmp_parse_oid(buf, oid_name, &name_length) == NULL) {
snmp_perror(buf);
return;
}
if (snmp_add_var(pdu, oid_name, name_length, '=', value)) {
snmp_perror(buf);
return;
}
}
示例8: main
int main(int argc, char ** argv)
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
oid anOID[MAX_OID_LEN];
size_t anOID_len;
netsnmp_variable_list *vars;
int status;
int count=1;
/*
* Initialize the SNMP library
*/
init_snmp("snmpdemoapp");
/*
* Initialize a "session" that defines who we're going to talk to
*/
snmp_sess_init( &session ); /* set up defaults */
session.peername = strdup("test.net-snmp.org");
/* set up the authentication parameters for talking to the server */
#ifdef DEMO_USE_SNMP_VERSION_3
/* Use SNMPv3 to talk to the experimental server */
/* set the SNMP version number */
session.version=SNMP_VERSION_3;
/* set the SNMPv3 user name */
session.securityName = strdup("MD5User");
session.securityNameLen = strlen(session.securityName);
/* set the security level to authenticated, but not encrypted */
session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
/* set the authentication method to MD5 */
session.securityAuthProto = usmHMACMD5AuthProtocol;
session.securityAuthProtoLen = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
session.securityAuthKeyLen = USM_AUTH_KU_LEN;
/* set the authentication key to a MD5 hashed version of our
passphrase "The Net-SNMP Demo Password" (which must be at least 8
characters long) */
if (generate_Ku(session.securityAuthProto,
session.securityAuthProtoLen,
(u_char *) our_v3_passphrase, strlen(our_v3_passphrase),
session.securityAuthKey,
&session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
snmp_perror(argv[0]);
snmp_log(LOG_ERR,
"Error generating Ku from authentication pass phrase. \n");
exit(1);
}
#else /* we'll use the insecure (but simplier) SNMPv1 */
/* set the SNMP version number */
session.version = SNMP_VERSION_1;
/* set the SNMPv1 community name used for authentication */
session.community = "demopublic";
session.community_len = strlen(session.community);
#endif /* SNMPv1 */
/*
* Open the session
*/
SOCK_STARTUP;
ss = snmp_open(&session); /* establish the session */
if (!ss) {
snmp_sess_perror("ack", &session);
SOCK_CLEANUP;
exit(1);
}
/*
* Create the PDU for the data for our request.
* 1) We're going to GET the system.sysDescr.0 node.
*/
pdu = snmp_pdu_create(SNMP_MSG_GET);
anOID_len = MAX_OID_LEN;
if (!snmp_parse_oid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len)) {
snmp_perror(".1.3.6.1.2.1.1.1.0");
SOCK_CLEANUP;
exit(1);
}
#if OTHER_METHODS
/*
* These are alternatives to the 'snmp_parse_oid' call above,
* e.g. specifying the OID by name rather than numerically.
*/
read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);
get_node("sysDescr.0", anOID, &anOID_len);
//.........这里部分代码省略.........
示例9: my_snmp_bulkwalk
/**
* Bulk walk snmp value to the structure my_oid_result array.
* Author : lining 15810423651 [email protected]
* @param peername : the remote host ip address.
* @param oid_argv : the char *oid pointer.
* @param my_oid_result oid_results : the return values array.
* @param my_oid_result oid_results_nums : the values array numbers.
* @return 0 on success, or others on failure.
* @return 1 on failure, the argument error.
* @return 2 on failure, snmp_open return error.
* @return 3 on failure, snmp_parse_oid return error.
* @return 4 on failure, snmp_synch_response return status time out.
* @return 5 on failure, snmp_synch_response return status others.
* @return 6 on failure, response->errstat end of mib.
* @return 7 on failure, response->errstat others.
*/
int my_snmp_bulkwalk(const char *peername,
const char *community,
const char *oid_argv,
my_oid_result *oid_results,
unsigned int *oid_results_nums) {
if(peername == NULL) {
printf("[ERROR] my_snmp_walk: the peername pointer is null\n");
return 1;
}
if(community == NULL) {
printf("[ERROR] my_snmp_walk: the community pointer is null\n");
return 1;
}
if(oid_argv == NULL) {
printf("[ERROR] my_snmp_walk: the oid_argv pointer is null\n");
return 1;
}
netsnmp_session session,*ss;
netsnmp_pdu *pdu, *response;
netsnmp_variable_list *vars;
int arg;
oid name[MAX_OID_LEN];
size_t name_length;
oid root[MAX_OID_LEN];
size_t rootlen;
int count;
int running;
int status;
int check;
int numprinted = 0;
int reps = 10, non_reps = 0;
netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "includeRequested", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_INCLUDE_REQUESTED);
netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "printStatistics", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_PRINT_STATISTICS);
netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "dontCheckOrdering", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
init_snmp("snmpapp");
snmp_sess_init(&session);
session.version = SNMP_VERSION_2c;
session.community = (char*)community;
session.community_len = strlen(session.community);
session.peername = (char*)peername;
session.timeout = 1000000;
session.retries = 1;
rootlen = MAX_OID_LEN;
if(snmp_parse_oid(oid_argv, root, &rootlen) == NULL) {
printf("[ERROR] my_snmp_bulkwalk: call snmp_parse_oid function failed\n");
snmp_close(ss);
SOCK_CLEANUP;
return 2;
}
SOCK_STARTUP;
ss = snmp_open(&session);
if(ss == NULL) {
printf("[ERROR] my_snmp_bulkwalk: cakk snnp_open function failed\n");
snmp_close(ss);
SOCK_CLEANUP;
return 3;
}
memmove(name, root, rootlen * sizeof(oid));
name_length = rootlen;
running = 1;
check = !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
while(running) {
pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
pdu->non_repeaters = non_reps;
pdu->max_repetitions = reps;
snmp_add_null_var(pdu, name, name_length);
status = snmp_synch_response(ss, pdu, &response);
if(status == STAT_SUCCESS) {
//.........这里部分代码省略.........
示例10: netsnmp_config_parse_table_set
/** @internal */
void
netsnmp_config_parse_table_set(const char *token, char *line)
{
oid name[MAX_OID_LEN], table_name[MAX_OID_LEN];
size_t name_length = MAX_OID_LEN, table_name_length =
MAX_OID_LEN;
struct tree *tp, *indexnode;
netsnmp_table_data_set *table_set;
data_set_tables *tables;
struct index_list *index;
unsigned int mincol = 0xffffff, maxcol = 0;
u_char type;
char *pos;
/*
* instatiate a fake table based on MIB information
*/
DEBUGMSGTL(("9:table_set_add_table", "processing '%s'\n", line));
if (NULL != (pos = strchr(line,' '))) {
config_pwarn("ignoring extra tokens on line");
snmp_log(LOG_WARNING," ignoring '%s'\n", pos);
*pos = '\0';
}
/*
* check for duplicate table
*/
tables = (data_set_tables *) netsnmp_get_list_data(auto_tables, line);
if (NULL != tables) {
config_pwarn("duplicate table definition");
return;
}
/*
* parse oid and find tree structure
*/
if (!snmp_parse_oid(line, table_name, &table_name_length)) {
config_pwarn
("can't instatiate table since I can't parse the table name");
return;
}
if(NULL == (tp = get_tree(table_name, table_name_length,
get_tree_head()))) {
config_pwarn("can't instatiate table since "
"I can't find mib information about it");
return;
}
if (NULL == (tp = tp->child_list) || NULL == tp->child_list) {
config_pwarn("can't instatiate table since it doesn't appear to be "
"a proper table (no children)");
return;
}
/*
* check for augments indexes
*/
if (NULL != tp->augments) {
if (!snmp_parse_oid(tp->augments, table_name, &table_name_length)) {
config_pwarn("I can't parse the augment tabel name");
snmp_log(LOG_WARNING, " can't parse %s\n", tp->augments);
return;
}
if(NULL == (tp = get_tree(table_name, table_name_length,
get_tree_head()))) {
config_pwarn("can't instatiate table since "
"I can't find mib information about augment table");
snmp_log(LOG_WARNING, " table %s not found in tree\n",
tp->augments);
return;
}
table_set = netsnmp_create_table_data_set(line);
/*
* loop through indexes and add types
*/
for (index = tp->indexes; index; index = index->next) {
if (!snmp_parse_oid(index->ilabel, name, &name_length) ||
(NULL ==
(indexnode = get_tree(name, name_length, get_tree_head())))) {
config_pwarn("can't instatiate table since "
"I don't know anything about one index");
snmp_log(LOG_WARNING, " index %s not found in tree\n",
index->ilabel);
return; /* xxx mem leak */
}
type = mib_to_asn_type(indexnode->type);
if (type == (u_char) - 1) {
config_pwarn("unknown index type");
return; /* xxx mem leak */
}
if (index->isimplied) /* if implied, mark it as such */
type |= ASN_PRIVATE;
DEBUGMSGTL(("table_set_add_row",
"adding default index of type %d\n", type));
netsnmp_table_dataset_add_index(table_set, type);
//.........这里部分代码省略.........
示例11: main
int
main(int argc, char *argv[])
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
netsnmp_variable_list *vars;
int arg;
int count;
int current_name = 0;
char *names[128];
oid name[MAX_OID_LEN];
size_t name_length;
int status;
int exitval = 0;
/*
* get the common command line arguments
*/
switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
case -2:
exit(0);
case -1:
usage();
exit(1);
default:
break;
}
if (arg >= argc) {
fprintf(stderr, "Missing object name\n");
usage();
exit(1);
}
/*
* get the object names
*/
for (; arg < argc; arg++)
names[current_name++] = argv[arg];
SOCK_STARTUP;
/*
* Open an SNMP session.
*/
ss = snmp_open(&session);
if (ss == NULL) {
/*
* diagnose snmp_open errors with the input netsnmp_session pointer
*/
snmp_sess_perror("snmpget", &session);
SOCK_CLEANUP;
exit(1);
}
/*
* Create PDU for GET request and add object names to request.
*/
pdu = snmp_pdu_create(SNMP_MSG_GET);
for (count = 0; count < current_name; count++) {
name_length = MAX_OID_LEN;
if (!snmp_parse_oid(names[count], name, &name_length)) {
snmp_perror(names[count]);
failures++;
} else
snmp_add_null_var(pdu, name, name_length);
}
if (failures) {
SOCK_CLEANUP;
exit(1);
}
/*
* Perform the request.
*
* If the Get Request fails, note the OID that caused the error,
* "fix" the PDU (removing the error-prone OID) and retry.
*/
retry:
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS) {
if (response->errstat == SNMP_ERR_NOERROR) {
for (vars = response->variables; vars;
vars = vars->next_variable)
print_variable(vars->name, vars->name_length, vars);
} else {
fprintf(stderr, "Error in packet\nReason: %s\n",
snmp_errstring(response->errstat));
if (response->errindex != 0) {
fprintf(stderr, "Failed object: ");
for (count = 1, vars = response->variables;
vars && count != response->errindex;
vars = vars->next_variable, count++)
/*EMPTY*/;
//.........这里部分代码省略.........
示例12: apply_plugin_config
static void
apply_plugin_config()
{
Reader *reader, *nreader;
gchar *name;
gint row;
if (!list_modified)
return;
for (reader = readers; reader; reader = readers) {
readers = reader->next;
destroy_reader(reader);
}
for (row = 0; row < GTK_CLIST(reader_clist)->rows; ++row)
{
gint i;
i = 0;
reader = g_new0(Reader, 1);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
gkrellm_dup_string(&reader->label, name);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
gkrellm_dup_string(&reader->peer, name);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
reader->port = atoi(name);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
gkrellm_dup_string(&reader->community, name);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
gkrellm_dup_string(&reader->oid_str, name);
reader->objid_length = MAX_OID_LEN;
if (!snmp_parse_oid(reader->oid_str,
reader->objid, &reader->objid_length)) {
//FIXME:
printf("error parsing oid: %s\n", reader->oid_str);
}
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
gkrellm_dup_string(&reader->unit, name);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
reader->delay = atoi(name);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
reader->divisor = atoi(name);
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
reader->delta = (strcmp(name, "yes") == 0) ? TRUE : FALSE;
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
reader->scale = (strcmp(name, "yes") == 0) ? TRUE : FALSE;
gtk_clist_get_text(GTK_CLIST(reader_clist), row, i++, &name);
reader->active = (strcmp(name, "yes") == 0) ? TRUE : FALSE;
if (!readers)
readers = reader;
else {
for (nreader = readers; nreader->next ; nreader = nreader->next);
nreader->next = reader;
}
create_reader(main_vbox, reader, 1);
}
list_modified = 0;
}
示例13: netsnmp_parse_override
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);
//.........这里部分代码省略.........
示例14: main
int main (int argc, char *argv[])
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
netsnmp_variable_list *vars;
int arg;
int count;
int status;
int exitval = 0;
/*
* get the common command line arguments
*/
switch (arg = 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;
}
names = argc - arg;
if (names < non_repeaters)
{
fprintf (stderr, "snmpbulkget: need more objects than <nonrep>\n");
exit (1);
}
namep = name = (struct nameStruct *) calloc (names, sizeof (*name));
while (arg < argc)
{
namep->name_len = MAX_OID_LEN;
if (snmp_parse_oid (argv[arg], namep->name, &namep->name_len) == NULL)
{
snmp_perror (argv[arg]);
exit (1);
}
arg++;
namep++;
}
SOCK_STARTUP;
/*
* open an SNMP session
*/
ss = snmp_open (&session);
if (ss == NULL)
{
/*
* diagnose snmp_open errors with the input netsnmp_session pointer
*/
snmp_sess_perror ("snmpbulkget", &session);
SOCK_CLEANUP;
exit (1);
}
/*
* create PDU for GETBULK request and add object name to request
*/
pdu = snmp_pdu_create (SNMP_MSG_GETBULK);
pdu->non_repeaters = non_repeaters;
pdu->max_repetitions = max_repetitions; /* fill the packet */
for (arg = 0; arg < names; arg++)
snmp_add_null_var (pdu, name[arg].name, name[arg].name_len);
/*
* do the request
*/
status = snmp_synch_response (ss, pdu, &response);
if (status == STAT_SUCCESS)
{
if (response->errstat == SNMP_ERR_NOERROR)
{
/*
* check resulting variables
*/
for (vars = response->variables; vars; vars = vars->next_variable)
print_variable (vars->name, vars->name_length, vars);
}
else
{
/*
* error in response, print it
*/
if (response->errstat == SNMP_ERR_NOSUCHNAME)
{
//.........这里部分代码省略.........
示例15: my_snmp_get
/**
* Get snmp value to the structure my_oid_result array.
* Author : lining 15810423651 [email protected]
* @param peername : the remote host ip address.
* @param oid_argvs : the oids pointer array.
* @param my_oid_result oid_result : the return values.
* @return 0 on success, or others on failure.
* @return 1 on failure, the argument value error.
* @return 2 on failure, snmp_open return error.
* @return 3 on failure, snmp_parse_oid return error.
* @return 4 on failure, snmp_synch_response return status time out.
* @return 5 on failure, snmp_synch_response return status others.
*/
int my_snmp_get(const char *peername,
const char *community,
const char *oid_argv,
my_oid_result *oid_result) {
netsnmp_session session;
netsnmp_session *sess_handle;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
netsnmp_variable_list *vars;
oid oids[MAX_OID_LEN];
size_t oids_length;
int status;
int failures = 0;
oids_length = 0;
if (peername == NULL) {
printf("[ERROR] my_snmp_get: the peername pointer is null\n");
return 1;
}
if (community == NULL) {
printf("[ERROR] my_snmp_get: the community pointer is null\n");
return 1;
}
if (oid_argv == NULL) {
printf("[ERROR] my_snmp_get: the oid_argv pointer is null\n");
return 1;
}
init_snmp("snmpget");
snmp_sess_init(&session);
session.version = SNMP_VERSION_2c;
session.community = (char*)community;
session.community_len = strlen(session.community);
session.peername = (char*)peername;
session.timeout = 3000000;
session.retries = 1;
SOCK_STARTUP;
sess_handle = snmp_open(&session);
if (sess_handle == NULL) {
SOCK_CLEANUP;
printf("[ERROR] my_snmp_get: call snmp_open function failed, return sess_handle is null\n");
return 2;
}
pdu = snmp_pdu_create(SNMP_MSG_GET);
oids_length = MAX_OID_LEN;
if (!snmp_parse_oid(oid_argv, oids, &oids_length)) {
snmp_perror(oid_argv);
failures++;
} else {
snmp_add_null_var(pdu, oids, oids_length);
}
if (failures) {
SOCK_CLEANUP;
printf("[ERROR] my_snmp_get: call snmp_parse_oid function failed\n");
return 3;
}
status = snmp_synch_response(sess_handle, pdu, &response);
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
for (vars = response->variables; vars; vars = vars->next_variable) {
get_oid_value(vars, &oid_result);
}
} else if (status == STAT_TIMEOUT) {
printf("[ERROR] my_snmp_get: call snmp_synch_response function failed, return status time out\n");
return 4;
} else {
printf("[ERROR] my_snmp_get: call snmp_synch_response function failed, return status others\n");
return 5;
}
if (response) {
snmp_free_pdu(response);
}
snmp_close(sess_handle);
SOCK_CLEANUP;
//.........这里部分代码省略.........