snmp 질문좀 드립니다. 답변 부탁드립니다 ㅠㅠ

mulgo79의 이미지


net-snmp를 이용하여 snmp-agent를 제작하였습니다.
동작잘하고 트랩 잘되고 뭐 일단은 정상적으로 동작은 합니다
헌데 데이터를 get하거나 walk를 하게되면 agent메모리가
계속 증가합니다. 제가 보기에는 테이블쪽에서 인덱스를
생성하면서 메모리를 할당하는데 이것때문인거 같습니다.
net-snmp에서 메모리를 컨트롤 하기위해서 어떤 식으로
접근해야하는지 잘 모르겠습니다. 답변 부탁드립니다.
소스는 밑에 붙여 넣겠습니다. 조금 길면 메모장에 붙여서 보시면 될듯합니다.

인덱스는 octet-string입니다.

소스 첨부 시작

/**
* @(#)AAAHostListTable.c
*
* Copyright (C) 2008 Park Sung Sik
* Date : 2008/12/19 FRI, Version 0.1
*
**/

#include
#include
#include

#include "qam_datas.h"
#include "qam_daemon.h"
#include "AAAHostListTable.h"
#include "queueList.h"

//#define ROW_STATUS_INSERT 1
//#define ROW_STATUS_UPDATE 2
//#define ROW_STATUS_DELETE 3
//#define ROW_STATUS_SELECT 4

static oid AAAHostListTable_oid[] = { 1, 3, 6, 1, 4, 1, 17560, 1000, 5, 4, 2 };

/**
* index : ASN_OCTET_STR
**/
typedef struct my_loop_info_s {
char hostItem[OCTETSTRING_LENGTH];
} my_loop_info;

void
initialize_table_AAAHostListTable(void)
{
netsnmp_table_registration_info *table_info;
netsnmp_handler_registration *my_handler;
netsnmp_iterator_info *iinfo;

table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);

my_handler = netsnmp_create_handler_registration(
"AAAHostListTable",
handler_AAAHostListTable,
AAAHostListTable_oid,
OID_LENGTH(
AAAHostListTable_oid),
HANDLER_CAN_RWRITE);

if(!my_handler || !table_info || !iinfo) {
snmp_log(LOG_ERR, "malloc failed in initialize_table_AAAHostListTable");
return; /* Error Return */
}

netsnmp_table_helper_add_indexes(table_info, ASN_OCTET_STR, 0); /* Index : fiAAAHostNumber */

table_info->min_column = 1;
table_info->max_column = 8;

iinfo->get_first_data_point = AAAHostListTable_get_first_data_point;
iinfo->get_next_data_point = AAAHostListTable_get_next_data_point;

iinfo->table_reginfo = table_info;

DEBUGMSGTL(("initialize_table_AAAHostListTable", "Registering table AAAHostListTable as a table iterator\n"));
netsnmp_register_table_iterator(my_handler, iinfo);
}

void
init_AAAHostListTable(void)
{
initialize_table_AAAHostListTable();
}

netsnmp_variable_list
*AAAHostListTable_get_first_data_point(void **my_loop_context, void **my_data_context,
netsnmp_variable_list *put_index_data,
netsnmp_iterator_info *mydata)
{
#if 1
netsnmp_variable_list *vptr = put_index_data;
struct AAAHostIndex_ListEntry *index_item = NULL;
if(TAILQ_EMPTY(&AAAHostIndex_ListHead)) {
return NULL;
}
int index = 1;
index_item = TAILQ_FIRST(&AAAHostIndex_ListHead);

my_loop_info *loopctx = SNMP_MALLOC_TYPEDEF(my_loop_info);
strcpy(loopctx->hostItem, index_item->HostItem);
*my_loop_context = (void *) loopctx;
*my_data_context = (void *) loopctx->hostItem;

snmp_set_var_value(vptr, (u_char *) &loopctx->hostItem, strlen(loopctx->hostItem));
vptr = vptr->next_variable;
#else
netsnmp_variable_list *vptr = put_index_data;
struct AAAHostIndex_ListEntry *index_item = NULL;
if(TAILQ_EMPTY(&AAAHostIndex_ListHead)) {
return NULL;
}
int index = 1;
index_item = TAILQ_FIRST(&AAAHostIndex_ListHead);

//my_loop_info *loopctx = SNMP_MALLOC_TYPEDEF(my_loop_info);
my_loop_info loopctx;
strcpy(loopctx.hostItem, index_item->HostItem);
*my_loop_context = (void *) &loopctx;
*my_data_context = (void *) &loopctx.hostItem;

snmp_set_var_value(vptr, (u_char *) &loopctx.hostItem, strlen(loopctx.hostItem));
vptr = vptr->next_variable;
#endif

return put_index_data;
}

netsnmp_variable_list
*AAAHostListTable_get_next_data_point(void **my_loop_context, void **my_data_context,
netsnmp_variable_list * put_index_data,
netsnmp_iterator_info *mydata)
{
#if 1
my_loop_info *loopctx;// = SNMP_MALLOC_TYPEDEF(my_loop_info);
loopctx = *my_loop_context;
if(!loopctx) {
return NULL;
}
struct AAAHostIndex_ListEntry *index_item = NULL;
int flag = 0;
for(index_item = TAILQ_FIRST(&AAAHostIndex_ListHead); index_item;
index_item = TAILQ_NEXT(index_item, entries)) {
if(index_item && strncmp(index_item->HostItem, loopctx->hostItem, strlen(loopctx->hostItem)) == 0) {
index_item = TAILQ_NEXT(index_item, entries);
flag++;
break;
}
}
if(flag == 0 || index_item == NULL) {
return NULL;
}
netsnmp_variable_list *vptr = put_index_data;
my_loop_info *next_loopctx = SNMP_MALLOC_TYPEDEF(my_loop_info);
strcpy(next_loopctx->hostItem, index_item->HostItem);

*my_loop_context = (void *) next_loopctx;
*my_data_context = (void *) next_loopctx->hostItem;

snmp_set_var_value(vptr, (u_char *) &next_loopctx->hostItem, strlen(next_loopctx->hostItem));
vptr = vptr->next_variable;
#else
my_loop_info *loopctx;// = SNMP_MALLOC_TYPEDEF(my_loop_info);
loopctx = *my_loop_context;
if(!loopctx) {
return NULL;
}
struct AAAHostIndex_ListEntry *index_item = NULL;
int flag = 0;
for(index_item = TAILQ_FIRST(&AAAHostIndex_ListHead); index_item;
index_item = TAILQ_NEXT(index_item, entries)) {
if(index_item && strncmp(index_item->HostItem, loopctx->hostItem, strlen(loopctx->hostItem)) == 0) {
index_item = TAILQ_NEXT(index_item, entries);
flag++;
break;
}
}
if(flag == 0 || index_item == NULL) {
return NULL;
}
netsnmp_variable_list *vptr = put_index_data;
//my_loop_info *next_loopctx = SNMP_MALLOC_TYPEDEF(my_loop_info);
my_loop_info next_loopctx;
strcpy(next_loopctx.hostItem, index_item->HostItem);

*my_loop_context = (void *) &next_loopctx;
*my_data_context = (void *) &next_loopctx.hostItem;

snmp_set_var_value(vptr, (u_char *) &next_loopctx.hostItem, strlen(next_loopctx.hostItem));
vptr = vptr->next_variable;
#endif

return put_index_data;
}

int
handler_AAAHostListTable(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
{
//FILE *log = NULL;
//log = fopen("/home/snmp/futureinfo_snmp_agent_ver_0_1/daemon/log/AAAHostListTable.log", "a+");
//fprintf(log, "handler\n");
//fclose(log);
struct AAAHostIndex_ListEntry *item = NULL;

netsnmp_request_info *request;
netsnmp_table_request_info *table_info;
netsnmp_variable_list *var;

int index, aaaHostNumber ;
char hostItem[OCTETSTRING_LENGTH];
my_loop_info *loopctx;
//my_loop_info *loopctx = SNMP_MALLOC_TYPEDEF(my_loop_info);
void *data_context = NULL;
struct AAAHostList_ListEntry *aaaHostListItem = NULL;

for(request = requests; request; request = request->next) { /* for loop */

var = request->requestvb;
if(request->processed != 0) {
continue;
}

loopctx = (void *) netsnmp_extract_iterator_context(request);
if(loopctx == NULL) {
if(reqinfo->mode == MODE_GET) {
netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE);
continue;
}
else if(reqinfo->mode == MODE_SET_RESERVE1) {
memset(hostItem, 0x00, sizeof(hostItem));
strncpy(hostItem, (char *) requests->requestvb->val.string,
requests->requestvb->val_len);
int flag = 0;
struct AAAHostIndex_ListEntry *index_item = NULL;
for(index_item=TAILQ_FIRST(&AAAHostIndex_ListHead); index_item;
index_item=TAILQ_NEXT(index_item, entries)) {
if(index_item && strcmp(index_item->HostItem, hostItem) == 0) {
flag++;
break;
}
}
if(flag == 0) {
index_item = malloc(sizeof(struct AAAHostIndex_ListEntry));
index_item->index = AAAHostNumber+1;
strcpy(index_item->HostItem, hostItem);
TAILQ_INSERT_TAIL(&AAAHostIndex_ListHead, index_item, entries);
AAAHostNumber++;
aaaHostListItem = malloc(sizeof(struct AAAHostList_ListEntry));
strcpy(aaaHostListItem->HostItem, hostItem);
TAILQ_INSERT_TAIL(&AAAHostList_ListHead, aaaHostListItem, entries);
}
}
else {
continue;
}
}

int flag = 0;
if(reqinfo->mode == MODE_SET_RESERVE1) {
if(loopctx == NULL) {
continue;
}
if((table_info = netsnmp_extract_table_info(request)) == NULL) {
//SNMP_FREE(request);
continue;
}
char set_val[OCTETSTRING_LENGTH];
strncpy(set_val, (char *) requests->requestvb->val.string,
requests->requestvb->val_len);
if(strcmp(loopctx->hostItem, set_val) != 0 &&
table_info->colnum == COLUMN_HOSTITEM) {
AAAHostIndex_Update(loopctx->hostItem, set_val);
}
for(aaaHostListItem=TAILQ_FIRST(&AAAHostList_ListHead); aaaHostListItem;
aaaHostListItem=TAILQ_NEXT(aaaHostListItem, entries)) {
if(aaaHostListItem && strcmp(aaaHostListItem->HostItem, loopctx->hostItem) == 0) {
flag++;
break;
}
}
if(flag == 0) {
//SNMP_FREE(request);
continue;
}
}
else {
strcpy(hostItem, loopctx->hostItem);
for(aaaHostListItem=TAILQ_FIRST(&AAAHostList_ListHead); aaaHostListItem;
aaaHostListItem=TAILQ_NEXT(aaaHostListItem, entries)) {
if(strcmp(aaaHostListItem->HostItem, loopctx->hostItem) == 0) {
flag++;
break;
}
}
if((table_info = netsnmp_extract_table_info(request)) == NULL) {
//SNMP_FREE(request);
continue;
}
}
if(!flag || aaaHostListItem == NULL) {
//SNMP_FREE(request);
continue;
}

int rowStatus = ROW_STATUS_SELECT;
switch(reqinfo->mode) { /* Switch */
case MODE_GET:
case MODE_GETNEXT:
switch(table_info->colnum) { /* tableinfo->colnum Switch */
case COLUMN_HOSTITEM:
snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) &aaaHostListItem->HostItem,
strlen(aaaHostListItem->HostItem));
break;

case COLUNM_HOSTNAME:
snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) &aaaHostListItem->HostName,
strlen(aaaHostListItem->HostName));
break;

case COLUMN_HOSTPORT:
snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &aaaHostListItem->HostPort,
sizeof(aaaHostListItem->HostPort));
break;

case COLUMN_HOSTFAMILY:
snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &aaaHostListItem->HostFamily,
sizeof(aaaHostListItem->HostFamily));
break;

case COLUMN_HOSTBACKLOG:
snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &aaaHostListItem->HostBacklog,
sizeof(aaaHostListItem->HostBacklog));
break;

case COLUMN_HOSTHOST:
snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) &aaaHostListItem->HostHost,
strlen(aaaHostListItem->HostHost));
break;

case COLUMN_HOSTREALM:
snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) &aaaHostListItem->HostRealm,
strlen(aaaHostListItem->HostRealm));
break;

case COLUMN_HOSTROWSTATUS:
snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &(rowStatus),
sizeof(rowStatus));
break;
default:
snmp_log(LOG_ERR, "Problem in handler_AAAHostListTable : Unknown Column");
} /* tableinfo->colnum Switch */
break;

case MODE_SET_RESERVE1:
switch(table_info->colnum) { /* tableinfo->colnum Switch */
case COLUMN_HOSTITEM:
{
char set_val[OCTETSTRING_LENGTH];
strncpy(set_val, (char *) requests->requestvb->val.string, requests->requestvb->val_len);
set_AAAConfigHostList_HostItem(aaaHostListItem->HostItem, set_val);
strncpy(aaaHostListItem->HostItem, (char *) requests->requestvb->val.string,
requests->requestvb->val_len);
//strcpy(aaaHostListItem->HostItem, hostItem);
}
break;

case COLUNM_HOSTNAME:
memset(aaaHostListItem->HostName, 0x00, strlen(aaaHostListItem->HostName));
strncpy(aaaHostListItem->HostName, (char *) requests->requestvb->val.string,
requests->requestvb->val_len);
break;

case COLUMN_HOSTPORT:
aaaHostListItem->HostPort = (int) *(requests->requestvb->val.integer);
break;

case COLUMN_HOSTFAMILY:
aaaHostListItem->HostPort = (int) *(requests->requestvb->val.integer);
break;

case COLUMN_HOSTBACKLOG:
aaaHostListItem->HostPort = (int) *(requests->requestvb->val.integer);
break;

case COLUMN_HOSTHOST:
strncpy(aaaHostListItem->HostHost, (char *) requests->requestvb->val.string,
requests->requestvb->val_len);
break;

case COLUMN_HOSTREALM:
strncpy(aaaHostListItem->HostRealm, (char *) requests->requestvb->val.string,
requests->requestvb->val_len);
break;

case COLUMN_HOSTROWSTATUS:
rowStatus = (int)*(requests->requestvb->val.integer);
set_AAAConfigHostList_RowStatus(rowStatus, loopctx->hostItem);
break;

default:
snmp_log(LOG_ERR, "Problem in handler_AAAHostListTable : Unknown Column");
return SNMP_ERR_NOERROR;
} /* tableinfo->colnum Switch */
break;

case MODE_SET_RESERVE2:
case MODE_SET_FREE:
case MODE_SET_ACTION:
case MODE_SET_COMMIT:
case MODE_SET_UNDO:
break;

default:
snmp_log(LOG_ERR, "Problem in handler_AAAHostListTable : Unknown Column");

} /* Switch */
//SNMP_FREE(request);
}

return SNMP_ERR_NOERROR;
}

소스 첨부 끝

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.