I am using the following function modified from the main function of 'snmpget' program.
The SNMP library version is 5.4.1.
But, it has a memory leak problem.
When I call the functions infinitely,
the memory size of the program grows.
How can I solve the problem?
Please, let me know.
Thanks,
int snmp_get(int argc, char *argv[], char * response_str, int response_str_len)
{
netsnmp_session session, *ss;
netsnmp_pdu *pdu;
netsnmp_pdu *response;
netsnmp_variable_list *vars;
int arg;
int count;
int current_name = 0;
char *names[SNMP_MAX_CMDLINE_OIDS];
oid name[MAX_OID_LEN];
size_t name_length;
int status;
int failures = 0;
int exitval = 0;
/*
* get the common command line arguments
*/
arg = snmp_parse_args(argc, argv, &session, "C:", optProc);
switch (arg) {
case -2:
case -1:
pthread_mutex_unlock(&snmp_mutex);
return -1;
default:
break;
}
if (arg >= argc) {
fprintf(stderr, "Missing object name\n");
return -1;
}
if ((argc - arg) > SNMP_MAX_CMDLINE_OIDS) {
fprintf(stderr, "Too many object identifiers specified. ");
fprintf(stderr, "Only %d allowed in one request.\n", SNMP_MAX_CMDLINE_OIDS);
return -1;
}
/*
* get the object names
*/
for (; arg < argc; arg++)
names[current_name++] = argv[arg];
SOCK_STARTUP;
/*
* Open an SNMP session.
*/
ss = snmp_open(&session);
if (ss == NULL) {
/*
* diagnose snmp_open errors with the input netsnmp_session pointer
*/
snmp_sess_perror("snmpget", &session);
pthread_mutex_unlock(&snmp_mutex);
SOCK_CLEANUP;
return 1;
}
/*
* Create PDU for GET request and add object names to request.
*/
pdu = snmp_pdu_create(SNMP_MSG_GET);
for (count = 0; count < current_name; count++) {
name_length = MAX_OID_LEN;
if (!snmp_parse_oid(names[count], name, &name_length)) {
snmp_perror(names[count]);
failures++;
} else
snmp_add_null_var(pdu, name, name_length);
}
if (failures) {
snmp_close(ss);
SOCK_CLEANUP;
return 1;
}
/*
* Perform the request.
*
* If the Get Request fails, note the OID that caused the error,
* "fix" the PDU (removing the error-prone OID) and retry.
*/
retry:
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS) {
if (response->errstat == SNMP_ERR_NOERROR) {
int index = 0;
for (vars = response->variables; vars;
vars = vars->next_variable)
{
char buf[256];
int ret = snprint_variable(buf,256,vars->name, vars->name_length, vars);
if (ret == -1)
break;
index += sprintf(response_str+index,"%s\n",buf);
}
} else {
fprintf(stderr, "Error in packet\nReason: %s\n",
snmp_errstring(response->errstat));
if (response->errindex != 0) {
fprintf(stderr, "Failed object: ");
for (count = 1, vars = response->variables;
vars && count != response->errindex;
vars = vars->next_variable, count++)
/*EMPTY*/;
if (vars) {
fprint_objid(stderr, vars->name, vars->name_length);
}
//fprintf(stderr, "\n");
}
exitval = 2;
/*
* retry if the errored variable was successfully removed
*/
if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
NETSNMP_DS_APP_DONT_FIX_PDUS)) {
pdu = snmp_fix_pdu(response, SNMP_MSG_GET);
snmp_free_pdu(response);
response = NULL;
if (pdu != NULL) {
goto retry;
}
}
} /* endif -- SNMP_ERR_NOERROR */
} else if (status == STAT_TIMEOUT) {
exitval = 1;
} else { /* status == STAT_ERROR */
exitval = 1;
} /* endif -- STAT_SUCCESS */
if (response)
{
snmp_free_pdu(response);
}
snmp_close(ss);
SOCK_CLEANUP;
return exitval;
}
[나의 라이브러리, 한메일] 2008,새로운 한메일을 만나보세요. |
|
------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php
_______________________________________________ Net-snmp-coders mailing list Net-snmp-coders@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/net-snmp-coders