CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Trap SNMP

Threaded View

  1. #1
    Join Date
    Jan 2006
    Posts
    6

    Trap SNMP

    Hi,
    I want to send trap to my snmp program.
    I have find this code in internet that send a trap, but it doesn't run correctly bacause my SNMP don't catch any trap!!!

    Code:
    #include "stdafx.h" 
    
    #using <mscorlib.dll> 
    #include <Winsnmp.h> 
    #include <windows.h> 
    
    using namespace System; 
    
    SNMPAPI_STATUS CALLBACK cbFunc 
    (HSNMP_SESSION hSession, HWND hWnd, UINT wMsg, 
    WPARAM wParam, LPARAM lParam, LPVOID lpClientData) 
    { // This is a noop function for this app, but must be present. 
    return (SNMPAPI_SUCCESS); 
    } 
    
    
    int _tmain(){ 
    
    DWORD nTime1=0, nTime2=0; 
    HSNMP_SESSION hSession; 
    HSNMP_ENTITY hDst; 
    HSNMP_CONTEXT hContext; 
    HSNMP_VBL hVbl; 
    HSNMP_PDU hPdu; 
    SNMPAPI_CALLBACK cB = &cbFunc; 
    smiOCTETS dContext = {6, (unsigned char *)"CIAOOO"}; 
    
    smiUINT32 lStat; 
    smiUINT32 sysUpTime[] = {1,3,6,1,2,1,1,3,0}; 
    smiUINT32 snmpTrapOid[] = {1,3,6,1,6,3,1 ,1,4,1,0}; 
    smiUINT32 trapValue[] = {1,3,6,1,4,1,12,1,1,1,210}; 
    
    smiOID dSysUpTimeName = {9, sysUpTime}; 
    smiOID dTrapName = {11, snmpTrapOid}; 
    smiOID dTrapName2 = {11, trapValue}; 
    
    smiVALUE valSysUpTime; 
    smiVALUE valTrap; 
    smiVALUE valTrap2; 
    
    lStat = SnmpStartup (&lStat, &lStat, &lStat, &lStat, &lStat); 
    if (lStat == SNMPAPI_FAILURE) 
    return (0); // exit 
    lStat = SnmpSetRetransmitMode(SNMPAPI_ON); 
    hSession = SnmpCreateSession(NULL, 0, cB, NULL); 
    
    if (hSession == SNMPAPI_FAILURE) 
    { 
    printf ("SnmpCreateSession() failed!\n"); 
    lStat = SnmpClose (hSession); 
    lStat = SnmpCleanup (); 
    return (0); 
    } 
    
    lStat = SnmpSetTranslateMode (SNMPAPI_TRANSLATED); //SNMPAPI_UNTRANSLATED_V1 
    
    //THIS IS THE IPADRESS OF SERVER WHERE IS INSTALLER SNMP PROGRAM
    hDst = SnmpStrToEntity (hSession, "192.168.1.10"); 
    lStat = SnmpSetPort (hDst, 162); 
    
    hContext = SnmpStrToContext(hSession, &dContext); 
    hVbl = SnmpCreateVbl (hSession, NULL, NULL); 
    
    valSysUpTime.syntax = SNMP_SYNTAX_TIMETICKS; 
    valSysUpTime.value.uNumber = GetTickCount() / 10; 
    lStat = SnmpSetVb (hVbl, 0, &dSysUpTimeName, &valSysUpTime); 
    
    valTrap.syntax = SNMP_SYNTAX_OID; 
    valTrap.value.oid.len = 11; 
    valTrap.value.oid.ptr = trapValue; 
    lStat = SnmpSetVb (hVbl, 0, &dTrapName, &valTrap); 
    
    valTrap2.syntax = SNMP_SYNTAX_INT; 
    valTrap2.value.sNumber = 210; 
    lStat = SnmpSetVb (hVbl, 0, &dTrapName2, &valTrap2); 
    
    hPdu = SnmpCreatePdu (hSession, SNMP_PDU_TRAP, 1, 0, 0, hVbl); 
    
    nTime1 = GetTickCount(); 
    lStat = SnmpSendMsg (hSession, NULL, hDst, hContext, hPdu); 
    if (lStat != SNMPAPI_SUCCESS) 
    { 
    printf ("SnmpSendMsg() failed:\n" ); 
    } 
    
    nTime2 = GetTickCount(); 
    printf ("Elapsed time (in milliseconds): %u \n", nTime2 - nTime1); 
    lStat = SnmpFreeContext(hContext); 
    lStat = SnmpClose (hSession); 
    lStat = SnmpCleanup (); 
    
    return (0); 
    
    }
    Why my SNMP don't catch trap send by this code??
    It's possibile to set priprity of trap??
    Where I set the string message of trap??
    I hope in you help!!
    Thanks

    Bye
    Sara
    Last edited by saraf; January 18th, 2006 at 08:27 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured