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

Thread: Trap SNMP

  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.

  2. #2
    Join Date
    Aug 2004
    Posts
    184

    Re: Trap SNMP

    Have you installed the MS trap provider? This has to be installed before you can receive traps.

    If not, go to add and remove programs --> windows components --> network --> snmp ( not 100% sure on this path ). Once done, this should install two services, one that allows you to receive traps the other will send traps from your machine.

    The service that sends traps can be configured to automatically send authentication traps to machines of your choice.

    HTH

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Trap SNMP

    [ moved thread ]

    Please use the code tags when posting code.
    &#91;code&#93;Your code&#91;/code&#93;
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    May 2005
    Posts
    399

    Re: Trap SNMP

    f1shrman was partly right, you have to install snmp service on your machine and then start the service (to send snmp messages) and the trap service (to receive traps)

    snmp-simple network management protocol, as the name stands, is a management tool (and not 'exactly' a network tool )
    So, to install it you must follow this path:
    Control Panel -> Add/Remove Programs -> Windows Components -> Management and Monitoring Tools -> SNMP
    after installing, dont' forget to start the service (Start -> Programs -> Administrative Tools -> Services)

  5. #5
    Join Date
    Jan 2006
    Posts
    6

    Re: Trap SNMP

    Hi,
    thank you so much for you reply!!
    I have installed the SNMP and I have started the SNMP service and Trap SNMP service.

    But in the server (ipaddress = 192.168.1.10) there is installed the Kiwi Syslog (receiver trap SNMP).
    I would send trap SNMP by my machine to 192.168.1.10.
    It's however necessary to install SNMP in my machine for send trap message, correctly??

    Now I launch my code, but how I can verify it works correctly??
    In my SNMP in the server I don't see any trap message.

    Thanks a lot!!
    Bye
    Sara

  6. #6
    Join Date
    May 2005
    Posts
    399

    Re: Trap SNMP

    Quote Originally Posted by saraf
    It's however necessary to install SNMP in my machine for send trap message, correctly??
    If I can re-collect correctly, you may have to have SNMP installed on both, the sender as well as receiver machines. I would advice you to install on both and see if it helps.

    Quote Originally Posted by saraf
    Now I launch my code, but how I can verify it works correctly??
    In my SNMP in the server I don't see any trap message.
    I cannot verify this b'coz I don't have SNMP on my machine right now, but I think in the Services 'options' you have the provision to set the type of output you want when a trap is received. It can be set to popup a dialog box or start an application or something like that.

  7. #7
    Join Date
    Jan 2006
    Posts
    6

    Re: Trap SNMP

    Ok, I have set property of SNMP Service. Now my code function correctly.
    Thank you so much, there are 3 days that I search info about SNMP.

    Thanks.
    Bye, Sara

  8. #8
    Join Date
    Apr 2007
    Posts
    1

    Re: Trap SNMP

    Hi, would you mind sharing your working SNMP Trap sender code? I am trying to develop one but am having some issues. Did you figure out where to put the message string in the SNMPSENDMSG?

    ANy help is sincerely appreciated.

    JOA

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