sam
sam

Reputation: 1401

how to use snmpwalk in c++

i m using net-snmp in my code . for snmpget i use this code and it is 100 % correct .

 string oiids =  ".1.3.6.1.4.1.30429.1.3.2.1.0" ;  
 struct snmp_session session , *ss ;
 struct snmp_pdu *pdu;
 struct snmp_pdu *response;
 oid anOID[MAX_OID_LEN];
 size_t anOID_len = MAX_OID_LEN;
 int status;
 init_snmp("APC Check");
 snmp_sess_init( &session );
 ss = snmp_open(&session);
 session.peername = "192.168.17.74";
 session.community = (u_char *) "public";
 session.community_len = strlen("public");
 session.version = SNMP_VERSION_2c;
 ss = snmp_open(&session);
 pdu = snmp_pdu_create(SNMP_MSG_GET);
 read_objid(oiids.c_str(), anOID, &anOID_len);
 snmp_add_null_var(pdu, anOID, anOID_len);
 status = snmp_synch_response(ss, pdu, &response);
 for(variable_list * vars = response->variables; vars; vars = vars->next_variable)
         print_variable(vars->name, vars->name_length, vars);

but this code doesn't work with the OID which have multi row's answer. by changing this line pdu = snmp_pdu_create(SNMP_MSG_GET) to this pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); this code return only first row .

thats the problem , how could i get all rows not just first one

Upvotes: 2

Views: 3361

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272367

GETNEXT will return the one value after the oid you've specified. So you need to iterate across the dataset using repeated GETNEXT calls.

GETBULK is perhaps what you want. This will perform the iteration for you and return as much as it can. You'll still have to cater for collecting the full set of data yourself.

SNMPv2 defines the get-bulk operation, which allows a management application to retrieve a large section of a table at once. The standard get operation can attempt to retrieve more than one MIB object at once, but message sizes are limited by the agent's capabilities. If the agent can't return all the requested responses, it returns an error message with no data. The get-bulk operation, on the other hand, tells the agent to send as much of the response back as it can. This means that incomplete responses are possible.

Upvotes: 1

Related Questions