有没有巨牛,这个东西有些冷门,资料不多

bsnry 2012-06-08 02:54:49

描述:需要做一个snmp agent, 于是在codproject 找到了范例, 按照作者的意思, 搞成个dll,然后注册到 代理计算机上

,然后 网络管理员在 服务端 使用snmptil.exe即可查询到一些信息。


问题: 由于 我要做一个agent,然后给注册到机子上,给网络管理员使用,让管理员查询一些信息。

这是代码,dll的。

我不明白,代理机子是怎么发送信息给 网管的, 仅仅是一个dll而已。

还有,我如何修改呢,比如:我需要获取cpu的温度 什么的??


第三个问题: 代码中的三个函数 SnmpExtensionInit SnmpExtensionQuery SnmpExtensionTrap 居然已经在库snmpapi.lib中有。但是作者还是自己实现了。

我如果在作者的基础上修改代码,应该如何修改呢???


http://www.codeproject.com/Articles/9024/How-to-develop-a-SNMP-extension-agent-DLL


代码在 下一楼

...全文
163 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bsnry 2012-06-08
  • 打赏
  • 举报
回复
// MyAgent.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <snmp.h>
#include <malloc.h>

#define OID_SIZEOF( Oid ) ( sizeof Oid / sizeof(UINT) )

// template MIB entry
struct MIB_ENTRY
{
AsnObjectIdentifier asnOid;
void * pStorageValue;
CHAR * szStorageName;
BYTE chType;
UINT unAccess;
MIB_ENTRY* pMibNext;
};

// this is the our branch starting point (clled prefix)
UINT g_unMyOIDPrefix[] = {1, 3, 6, 1, 4, 1, 15};

// this is the trap OID to send to client
UINT g_TrapOid[] = {1, 3, 6, 1, 4, 1, 15, 0};

AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(g_unMyOIDPrefix), g_unMyOIDPrefix};

DWORD g_dwStartTime = 0;
HANDLE g_hSimulateTrap = NULL;
HANDLE g_hTrapGenThread = NULL;
unsigned long __stdcall TrapGenThread(void *lpVoid);

int GetRequest(SnmpVarBind *pVarBind);
int GetNextRequest(SnmpVarBind *pVarBind);
int SetRequest(SnmpVarBind *pVarBind);

UINT GetStoreVar(MIB_ENTRY* pMIB, AsnAny *pasnValue);
UINT SetStoreVar(MIB_ENTRY* pMIB, AsnAny asnValue);


/////////MIB Table ////////////////////////////////////////////////
UINT g_unAboutOid[] = {0,0,1};
UINT g_unNameOid[] = {0,0,2};
UINT g_unAgeOid[] = {0,0,3};

char *g_szAbout = NULL;
char *g_szName = NULL;
AsnInteger g_asnIntAge = 0;

/// This is the MIB table and its related variable store
// here evry thing is hard-coded to demonstration perpose
// Actualy it should be loaded from the registry or from some file
MIB_ENTRY g_MyMibTable[] = {
{
{OID_SIZEOF(g_unAboutOid),g_unAboutOid},
&g_szAbout,
"About",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_ONLY,
&g_MyMibTable[1]
},
{
{OID_SIZEOF(g_unNameOid),g_unNameOid},
&g_szName,
"Name",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_WRITE,
&g_MyMibTable[2]
},
{
{OID_SIZEOF(g_unAgeOid),g_unAgeOid},
&g_asnIntAge,
"Age",
ASN_INTEGER,
SNMP_ACCESS_READ_WRITE,
NULL
}
};

UINT g_unMyMibCount = (sizeof(g_MyMibTable) / sizeof(MIB_ENTRY));
///////////////////////////////////////////////////////////////////

BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
TerminateThread(g_hTrapGenThread,0);
CloseHandle(g_hTrapGenThread);
CloseHandle(g_hSimulateTrap);
break;
}
return TRUE;
}

// When exported funtion will be called during DLL loading and initialization
BOOL SNMP_FUNC_TYPE SnmpExtensionInit(DWORD dwUptimeReference,HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion)
{
g_hSimulateTrap = CreateEvent(NULL, FALSE, FALSE, NULL); // creaet this event for the trap

*pFirstSupportedRegion = MIB_OidPrefix;
*phSubagentTrapEvent = g_hSimulateTrap; // by assigning it pass it to the SNMP service
// So when ever you set this event service will call
// SnmpExtensionTrap exported function

// on loading the our SNMP DLL create the thread
g_hTrapGenThread = CreateThread(NULL,0,TrapGenThread,NULL,0,NULL);

// hard coded initialization
g_szAbout = (char*)malloc(sizeof(char)*64);
strcpy(g_szAbout,"Author : Ramanan.T");
g_szName = (char*)malloc(sizeof(char)*64);
strcpy(g_szName,"Your Name");
g_asnIntAge = 0;

g_dwStartTime = GetTickCount();

return SNMPAPI_NOERROR;
}

// this export is to query the MIB table and fields
BOOL SNMP_FUNC_TYPE SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
{
int nRet = 0;
AsnObjectName;

*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
*pErrorIndex = 0;

for(UINT i=0;i<pVarBindList->len;i++)
{
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;

// what type of request we are getting?
switch(bPduType)
{
case SNMP_PDU_GET:// // gets the variable value passed variable in pVarBindList
*pErrorStatus = GetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_GETNEXT: // gets the next variable related to the passed variable in pVarBindList
*pErrorStatus = GetNextRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_SET: // sets a variable
*pErrorStatus = SetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
default:
*pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
*pErrorIndex++;
};
}

return SNMPAPI_NOERROR;
}

// this function just simulate traps
// Traps just a 2 variables value from MIB
// Trap is kind of event from server to client
/// When ever the event is set service will call this function and gets the parameters filled.
// After filling the parameters service willsend the trap to all the client connected
BOOL SNMP_FUNC_TYPE SnmpExtensionTrap(AsnObjectIdentifier *pEnterpriseOid, AsnInteger32 *pGenericTrapId, AsnInteger32 *pSpecificTrapId, AsnTimeticks *pTimeStamp, SnmpVarBindList *pVarBindList)
{
static int nNoOfTraps = 1; // just ignore this, I introduced this to send meny traps at once
// any way below we are generating one trap with two values

if(nNoOfTraps) // if it is zero don't send traps
{
pEnterpriseOid->idLength = sizeof(g_TrapOid);
pEnterpriseOid->ids = g_TrapOid;

*pGenericTrapId = SNMP_GENERICTRAP_ENTERSPECIFIC;
*pSpecificTrapId = 1; // ToasterControl Up trap.
*pTimeStamp = GetTickCount() - g_dwStartTime;

// Allocate space for the Variable Bindings.
pVarBindList->list = (SnmpVarBind*)SnmpUtilMemAlloc(2*sizeof(SnmpVarBind));

SnmpUtilOidCpy(&pVarBindList->list[0].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[0].name,&g_MyMibTable[1].asnOid);
pVarBindList->list[0].value.asnType = ASN_OCTETSTRING;
pVarBindList->list[0].value.asnValue.string.dynamic = TRUE;
pVarBindList->list[0].value.asnValue.string.length = strlen(*(LPSTR*)g_MyMibTable[1].pStorageValue);
pVarBindList->list[0].value.asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc(pVarBindList->list[0].value.asnValue.string.length * sizeof(char));
memcpy(pVarBindList->list[0].value.asnValue.string.stream,*(LPSTR*)g_MyMibTable[1].pStorageValue,pVarBindList->list[0].value.asnValue.string.length);

SnmpUtilOidCpy(&pVarBindList->list[1].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[1].name,&g_MyMibTable[2].asnOid);
pVarBindList->list[1].value.asnType = ASN_INTEGER;
pVarBindList->list[1].value.asnValue.number = *((AsnInteger32*)g_MyMibTable[2].pStorageValue);

pVarBindList->len = 2;

nNoOfTraps--;
// Indicate that valid trap data exists in the parameters.
return TRUE;
}
nNoOfTraps = 1;
// Indicate that no more traps are available, and parameters do not refer to any valid data
return FALSE;
}
bsnry 2012-06-08
  • 打赏
  • 举报
回复


// When exported funtion will be called during DLL loading and initialization
BOOL SNMP_FUNC_TYPE SnmpExtensionInit(DWORD dwUptimeReference,HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion)
{
g_hSimulateTrap = CreateEvent(NULL, FALSE, FALSE, NULL); // creaet this event for the trap

*pFirstSupportedRegion = MIB_OidPrefix;
*phSubagentTrapEvent = g_hSimulateTrap; // by assigning it pass it to the SNMP service
// So when ever you set this event service will call
// SnmpExtensionTrap exported function

// on loading the our SNMP DLL create the thread
g_hTrapGenThread = CreateThread(NULL,0,TrapGenThread,NULL,0,NULL);

// hard coded initialization
g_szAbout = (char*)malloc(sizeof(char)*64);
strcpy(g_szAbout,"Author : Ramanan.T");
g_szName = (char*)malloc(sizeof(char)*64);
strcpy(g_szName,"Your Name");
g_asnIntAge = 0;

g_dwStartTime = GetTickCount();

return SNMPAPI_NOERROR;
}

// this export is to query the MIB table and fields
BOOL SNMP_FUNC_TYPE SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
{
int nRet = 0;
AsnObjectName;

*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
*pErrorIndex = 0;

for(UINT i=0;i<pVarBindList->len;i++)
{
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;

// what type of request we are getting?
switch(bPduType)
{
case SNMP_PDU_GET:// // gets the variable value passed variable in pVarBindList
*pErrorStatus = GetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_GETNEXT: // gets the next variable related to the passed variable in pVarBindList
*pErrorStatus = GetNextRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_SET: // sets a variable
*pErrorStatus = SetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
default:
*pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
*pErrorIndex++;
};
}

return SNMPAPI_NOERROR;
}

// this function just simulate traps
// Traps just a 2 variables value from MIB
// Trap is kind of event from server to client
/// When ever the event is set service will call this function and gets the parameters filled.
// After filling the parameters service willsend the trap to all the client connected
BOOL SNMP_FUNC_TYPE SnmpExtensionTrap(AsnObjectIdentifier *pEnterpriseOid, AsnInteger32 *pGenericTrapId, AsnInteger32 *pSpecificTrapId, AsnTimeticks *pTimeStamp, SnmpVarBindList *pVarBindList)
{
static int nNoOfTraps = 1; // just ignore this, I introduced this to send meny traps at once
// any way below we are generating one trap with two values

if(nNoOfTraps) // if it is zero don't send traps
{
pEnterpriseOid->idLength = sizeof(g_TrapOid);
pEnterpriseOid->ids = g_TrapOid;

*pGenericTrapId = SNMP_GENERICTRAP_ENTERSPECIFIC;
*pSpecificTrapId = 1; // ToasterControl Up trap.
*pTimeStamp = GetTickCount() - g_dwStartTime;

// Allocate space for the Variable Bindings.
pVarBindList->list = (SnmpVarBind*)SnmpUtilMemAlloc(2*sizeof(SnmpVarBind));

SnmpUtilOidCpy(&pVarBindList->list[0].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[0].name,&g_MyMibTable[1].asnOid);
pVarBindList->list[0].value.asnType = ASN_OCTETSTRING;
pVarBindList->list[0].value.asnValue.string.dynamic = TRUE;
pVarBindList->list[0].value.asnValue.string.length = strlen(*(LPSTR*)g_MyMibTable[1].pStorageValue);
pVarBindList->list[0].value.asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc(pVarBindList->list[0].value.asnValue.string.length * sizeof(char));
memcpy(pVarBindList->list[0].value.asnValue.string.stream,*(LPSTR*)g_MyMibTable[1].pStorageValue,pVarBindList->list[0].value.asnValue.string.length);

SnmpUtilOidCpy(&pVarBindList->list[1].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[1].name,&g_MyMibTable[2].asnOid);
pVarBindList->list[1].value.asnType = ASN_INTEGER;
pVarBindList->list[1].value.asnValue.number = *((AsnInteger32*)g_MyMibTable[2].pStorageValue);

pVarBindList->len = 2;

nNoOfTraps--;
// Indicate that valid trap data exists in the parameters.
return TRUE;
}
nNoOfTraps = 1;
// Indicate that no more traps are available, and parameters do not refer to any valid data
return FALSE;
}
bsnry 2012-06-08
  • 打赏
  • 举报
回复
// MyAgent.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <snmp.h>
#include <malloc.h>

#define OID_SIZEOF( Oid ) ( sizeof Oid / sizeof(UINT) )

// template MIB entry
struct MIB_ENTRY
{
AsnObjectIdentifier asnOid;
void * pStorageValue;
CHAR * szStorageName;
BYTE chType;
UINT unAccess;
MIB_ENTRY* pMibNext;
};

// this is the our branch starting point (clled prefix)
UINT g_unMyOIDPrefix[] = {1, 3, 6, 1, 4, 1, 15};

// this is the trap OID to send to client
UINT g_TrapOid[] = {1, 3, 6, 1, 4, 1, 15, 0};

AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(g_unMyOIDPrefix), g_unMyOIDPrefix};

DWORD g_dwStartTime = 0;
HANDLE g_hSimulateTrap = NULL;
HANDLE g_hTrapGenThread = NULL;
unsigned long __stdcall TrapGenThread(void *lpVoid);

int GetRequest(SnmpVarBind *pVarBind);
int GetNextRequest(SnmpVarBind *pVarBind);
int SetRequest(SnmpVarBind *pVarBind);

UINT GetStoreVar(MIB_ENTRY* pMIB, AsnAny *pasnValue);
UINT SetStoreVar(MIB_ENTRY* pMIB, AsnAny asnValue);


/////////MIB Table ////////////////////////////////////////////////
UINT g_unAboutOid[] = {0,0,1};
UINT g_unNameOid[] = {0,0,2};
UINT g_unAgeOid[] = {0,0,3};

char *g_szAbout = NULL;
char *g_szName = NULL;
AsnInteger g_asnIntAge = 0;

/// This is the MIB table and its related variable store
// here evry thing is hard-coded to demonstration perpose
// Actualy it should be loaded from the registry or from some file
MIB_ENTRY g_MyMibTable[] = {
{
{OID_SIZEOF(g_unAboutOid),g_unAboutOid},
&g_szAbout,
"About",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_ONLY,
&g_MyMibTable[1]
},
{
{OID_SIZEOF(g_unNameOid),g_unNameOid},
&g_szName,
"Name",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_WRITE,
&g_MyMibTable[2]
},
{
{OID_SIZEOF(g_unAgeOid),g_unAgeOid},
&g_asnIntAge,
"Age",
ASN_INTEGER,
SNMP_ACCESS_READ_WRITE,
NULL
}
};

UINT g_unMyMibCount = (sizeof(g_MyMibTable) / sizeof(MIB_ENTRY));
///////////////////////////////////////////////////////////////////

BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
TerminateThread(g_hTrapGenThread,0);
CloseHandle(g_hTrapGenThread);
CloseHandle(g_hSimulateTrap);
break;
}
return TRUE;
}

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧