I am trying to attach a MIB Module into my C++ program. Making it an AgentX 
subagent. Any help/suggestion is appreciated. 



I followed the MIB Module Tutorial on Net-SNMP, created myself a MIB module 
with 3 scalars. 
Then running the command net-snmp-config --compile-subagent mysnmp mysnmp.c 
Executing the following snmpget localhost -v2c -c public 
1.3.6.1.4.1.40463.1.1.0 
And it output a test string I hard coded into mysnmp MIB module. Worked fine. 



snmpget localhost -v2c -c public 1.3.6.1.4.1.40463.1.1.0 
SNMPv2-SMI::enterprises.40463.1.1.0 = STRING: "mysnmp" 


I have baked Net-SNMP into my C++ program (trying to make my C++ program an 
AgentX subagent). To check if it worked like an AgentX, I just used the same 
MIB module from my mysnmp I made following the tutorial. Move the code from 
mysnmp.c into myclient.cpp. Compiled fine, ran fine, but did not get any result 
back. 

snmpget localhost -v2c -c public 1.3.6.1.4.1.40463.1.1.0 
SNMPv2-SMI::enterprises.40463.1.1.0 = No Such Object available on this agent at 
this OID 

I have put the following Net-SNMP initialization code in my main: 
main.cpp 
int background = 0; // change this if you want to run in the background. 
int syslog = 0; // change this if you want to use syslog. 

// print log errors to syslog or stderr. 
if (syslog) 
snmp_enable_calllog(); 
else 
snmp_enable_stderrlog(); 

// make us a agentx client. 
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1); 

// run in background, if requested. 
if (background && netsnmp_daemonize(1, !syslog)) { exit(1); } 

// initialize tcpip, if necessary. 
SOCK_STARTUP; 

// initialize the agent library. 
init_agent("my-snmp-deamon"); 

myclient client; 
client.run(); 

myclient.cpp 
extern "C" { 
#include <net-snmp/net-snmp-config.h> 
#include <net-snmp/net-snmp-includes.h> 
#include <net-snmp/agent/net-snmp-agent-includes.h> 

void init_vacm_vars(); 
void init_usmUser(); 

Netsnmp_Node_Handler handle_snmp; 
} 

const oid mysnmp_oid[] = { 1,3,6,1,4,1,40463,1 }; 
const oid mysnmpInfo_oid[] = { 1,3,6,1,4,1,40463,1,1 }; 
const oid mysnmpPlacementCountry_oid[] = { 1,3,6,1,4,1,40463,1,2 }; 
con st oid mysnmpCounter_oid[] = { 1,3,6,1,4,1,40463,1,3 }; 



myclient::myclient() : 
_mainloop(Glib::MainLoop::create()) 

{ 

netsnmp_register_scalar_group( 
netsnmp_create_handler_registration("mysnmp", 
handle_snmp, 
group_oid, 
OID_LENGTH(group_oid), 
HANDLER_CAN_RONLY 
), 1, 3); 



//Runs the Main Event Loop 
Glib::signal_idle().connect( 
sigc::bind_return(sigc::mem_fun(*this, &myclient::mainloop), true) 
); 
} 

void myclient::run() { 
_mainloop->run(); //Running Main Event Loop 
} 


myclient::mainloop() { 
if (_snmp_ready) { 
agent_check_and_process(1); /* 0 == don't block */ 
} 
} 

int 
handle_snmp(netsnmp_mib_handler *handler, 
netsnmp_handler_registration *reginfo, 
netsnmp_agent_request_info *reqinfo, 
netsnmp_request_info *requests) { 
void* datapointer; 
char buffer[200]; 
int dataint; 
int length = 0; 
u_char type; 

const oid* request_oid = (*reginfo).rootoid; 
if (netsnmp_oid_equals(request_oid, 9, mysnmpInfo_oid, 9) == 0) { //OID_LENGTH( 
mysnmpInfo_oid ) 
strcpy(buffer, "mysnmp"); 
datapointer = buffer; 
length = strlen(buffer); 
type = ASN_OCTET_STR; 
} else if (netsnmp_oid_equals(request_oid, 9, mysnmpPlacementCountry_oid, 9) == 
0) { 
strcpy(buffer, "mysnmp2"); 
datapointer = buffer; 
length = strlen(buffer); 
type = ASN_OCTET_STR; 
} else if (netsnmp_oid_equals(request_oid, 9, mysnmpCounter_oid, 9) == 0) { 
dataint = 1234; 
datapointer = &dataint; 
length = sizeof(dataint); 
type = ASN_INTEGER; 
} else { 
strcpy(buffer, "UNKNOWN_OID"); 
datapointer = buffer; 
length = strlen(buffer); 
type = ASN_OCTET_STR; 
} 

switch(reqinfo->mode) { 
case MODE_GET: 
snmp_set_var_typed_value(requests->requestvb, 
type, 
datapointer, 
length); 
break; 

default: 
/* we should never get here, so this is a really bad error */ 
snmp_log(LOG_ERR, "unknown mode (%d) in handle_snmp\n", reqinfo->mode ); 
return SNMP_ERR_GENERR; 
} 

return SNMP_ERR_NOERROR; 
} 

CONFIDENTIALITY
This e-mail and any attachment contain KONGSBERG information which may be 
proprietary, confidential or subject to export regulations, and is only meant 
for the intended recipient(s). Any disclosure, copying, distribution or use is 
prohibited, if not otherwise explicitly agreed with KONGSBERG. If received in 
error, please delete it immediately from your system and notify the sender 
properly.
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
Net-snmp-users mailing list
Net-snmp-users@lists.sourceforge.net
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

Reply via email to