Dear Mr. /Ms., Hi:
     I have a problem (SNMP Get), to seek help, the problem described as 
follows:
     The main body of the program 
(Referencehttp://www.net-snmp.org/tutorial/tutorial-5/toolkit/demoapp/snmpdemoapp.c),
     I added a while loop, do SNMPDEMO about 50 times the memory will increase 
about 4K, then the memory will continue to increase

     External environment:
      CentOS5 NET-SNMP 5.5

     View memory usage of the command:
   # ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' |grep snmpdemo|grep 
-v grep 
    
     thanks.
    
     source Code:
    
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <string.h>

int main(int argc, char ** argv)
{
    netsnmp_session session, *ss;
    netsnmp_pdu *pdu;
    netsnmp_pdu *response;

    oid anOID[MAX_OID_LEN];
    size_t anOID_len;

    netsnmp_variable_list *vars;
    int status;
    int count=1;

    /*
     * Initialize the SNMP library
     */
    init_snmp("snmpdemoapp");

    while (1){ // //new add
     /*
      * Initialize a "session" that defines who we're going to talk to
      */
     snmp_sess_init( &session );                   /* set up defaults */
     session.peername = strdup("10.10.11.104");  //change
     
     /* set up the authentication parameters for talking to the server */
     
     
     /* set the SNMP version number */
     session.version = SNMP_VERSION_1;
     
     /* set the SNMPv1 community name used for authentication */
     session.community = "public";       //change
     session.community_len = strlen(session.community);
     
     
     /*
      * Open the session
      */
     SOCK_STARTUP;
     ss = snmp_open(&session);                     /* establish the session */
     
     if (!ss) {
       snmp_sess_perror("ack", &session);
       SOCK_CLEANUP;
       exit(1);
     }
     
     /*
      * Create the PDU for the data for our request.
      *   1) We're going to GET the system.sysDescr.0 node.
      */
     pdu = snmp_pdu_create(SNMP_MSG_GET);
     anOID_len = MAX_OID_LEN;
     if (!snmp_parse_oid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len)) {
       snmp_perror(".1.3.6.1.2.1.1.1.0");
       SOCK_CLEANUP;
       exit(1);
     }
     
     snmp_add_null_var(pdu, anOID, anOID_len);
     
     /*
      * Send the Request out.
      */
     status = snmp_synch_response(ss, pdu, &response);
     
     /*
      * Process the response.
      */
     if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
       /*
        * SUCCESS: Print the result variables
        */
     
       for(vars = response->variables; vars; vars = vars->next_variable)
         print_variable(vars->name, vars->name_length, vars);
     
       /* manipuate the information ourselves */
       for(vars = response->variables; vars; vars = vars->next_variable) {
         if (vars->type == ASN_OCTET_STR) {
    char *sp = (char *)malloc(1 + vars->val_len);
    memcpy(sp, vars->val.string, vars->val_len);
    sp[vars->val_len] = '\0';
           printf("value #%d is a string: %s\n", count++, sp);
    free(sp);
  }
         else
           printf("value #%d is NOT a string! Ack!\n", count++);
       }
     } else {
       /*
        * FAILURE: print what went wrong!
        */
     
       if (status == STAT_SUCCESS)
         fprintf(stderr, "Error in packet\nReason: %s\n",
                 snmp_errstring(response->errstat));
       else if (status == STAT_TIMEOUT)
         fprintf(stderr, "Timeout: No response from %s.\n",
                 session.peername);
       else
         snmp_sess_perror("snmpdemoapp", ss);
     
     }
     
     /*
      * Clean up:
      *  1) free the response.
      *  2) close the session.
      */
     if (response)
       snmp_free_pdu(response);
     snmp_close(ss);
     usleep(1000);  //new add
 }
    SOCK_CLEANUP;
    return (0);
} /* main() */

        
------------------------------------------------------------------------------
Free Software Download: Index, Search & Analyze Logs and other IT data in 
Real-Time with Splunk. Collect, index and harness all the fast moving IT data 
generated by your applications, servers and devices whether physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new business 
insights. http://p.sf.net/sfu/splunk-dev2dev 
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to