1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| //---------------------------------------------------------------------------
//-----------------------------GETMIBINT-------------------------------------
//---------------------------------------------------------------------------
int _stdcall getMibINT(char* strOid, char* addressIP, char* context,
char* SecurityName, bool SecurityLevel,
char* AuthPassword, char* PrivPassword,
unsigned short port)
{
Snmp::socket_startup(); // Initialize socket subsystem
//---------[ make a GenAddress and Oid object to retrieve ]---------------
UdpAddress address(addressIP); // make a SNMP++ Generic address
if ( !address.valid()) { // check validity of address
return -10;
}
Oid oid(strOid);
if(!oid.valid())
{ // check validity of user oid
return -20;
}
//---------[ determine options to use ]-----------------------------------
snmp_version version=version3; // default is v3
int retries=1; // default retries is 1
int timeout=100; // default is 1 second
OctetStr privPassword(PrivPassword);
OctetStr authPassword(AuthPassword);
OctetStr securityName(SecurityName);
int securityModel = SecurityModel_USM;
long authProtocol;
long privProtocol;
int securityLevel;
if(SecurityLevel==true)
{
//securityLevel = SNMP_SECURITY_LEVEL_AUTH_PRIV;
securityLevel = SNMP_SECURITY_LEVEL_AUTH_NOPRIV;
authProtocol = SNMP_AUTHPROTOCOL_HMACMD5;
privProtocol = SNMP_PRIVPROTOCOL_NONE;
//privProtocol = SNMP_PRIVPROTOCOL_DES;
}
else
{
securityLevel=SecurityLevel_noAuthNoPriv;
authProtocol = SNMPv3_usmNoAuthProtocol;
privProtocol = SNMPv3_usmNoPrivProtocol;
}
OctetStr contextName(context);
OctetStr contextEngineID("");
v3MP *v3_MP;
//----------[ create a SNMP++ session ]-----------------------------------
int status;
// bind to any port and use IPv6 if needed
Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));
if(status != SNMP_CLASS_SUCCESS)
{
return -30;
}
//---------[ init SnmpV3 ]--------------------------------------------
char *engineId = "snmpGet";
char *filename = "snmpv3_boot_counter";
unsigned int snmpEngineBoots = 0;
status = getBootCounter(filename, engineId, snmpEngineBoots);
if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
{
return -40;
}
snmpEngineBoots++;
status = saveBootCounter(filename, engineId, snmpEngineBoots);
if (status != SNMPv3_OK)
{
return -50;
}
int construct_status;
v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);
USM *usm = v3_MP->get_usm();
status=usm->add_usm_user(securityName,authProtocol, privProtocol, authPassword, privPassword);
if(status != SNMPv3_USM_OK )
{
return -55;
}
//--------[ build up SNMP++ object needed ]-------------------------------
Pdu pdu; // construct a Pdu object
Vb vb; // construct a Vb object
vb.set_oid(strOid); // set the Oid portion of the Vb
pdu += vb; // add the vb to the Pdu
address.set_port(port);
UTarget utarget(address); // make a target using the address
utarget.set_version(version); // set the SNMP version SNMPV1 or V2 or V3
utarget.set_retry(retries); // set the number of auto retries
utarget.set_timeout(timeout); // set timeout
utarget.set_security_model(securityModel);
utarget.set_security_name(securityName);
pdu.set_security_level(securityLevel);
pdu.set_context_name(contextName);
pdu.set_context_engine_id(contextEngineID);
//-------[ issue the request, blocked mode ]-----------------------------
SnmpTarget *target;
target = &utarget;//SNMP V3
status = snmp.get(pdu, *target);
if (status == SNMP_CLASS_SUCCESS)
{
pdu.get_vb( vb,0);
if (pdu.get_type() == REPORT_MSG)
{
return -60;
}
if ((vb.get_syntax() == sNMP_SYNTAX_ENDOFMIBVIEW) ||
(vb.get_syntax() == sNMP_SYNTAX_NOSUCHINSTANCE) ||
(vb.get_syntax() == sNMP_SYNTAX_NOSUCHOBJECT))
{
return -70;
}
int value=0;
if(vb.get_value(value)==SNMP_CLASS_INVALID)
{
return -80;
}
else
{
/*char buffer[10];
sprintf(buffer,"Result Value: %d",value);
MessageBox(NULL,buffer,"Result",MB_OK);*/
return value;
}
}
else
{
return status;
}
Snmp::socket_cleanup(); // Shut down socket subsystem
}
//--------------------------------------------------------------------------- |
Partager