Dear all,
I am new in this list, I am learning make a simple MIB for host that
connect to my host.
All IP address is listed in file /etc/HostIndex with format "index
Ip_address". This is the example of /etc/HostIndex :
# cat /etc/HostIndex
0 192.168.1.3
1 192.168.2.2

that file updated sometimes, so I dont need realtime update of that
value on get request.

I am trying build new MIB, with consist one table. it named MY-MIB.txt :
MY-PRIV-MIB DEFINITIONS ::= BEGIN

IMPORTS
    MODULE-IDENTITY, OBJECT-TYPE, Integer32, Unsigned32,
    Gauge32, Counter32, Counter64, IpAddress, mib-2
                                       FROM SNMPv2-SMI
    MODULE-COMPLIANCE, OBJECT-GROUP    FROM SNMPv2-CONF
    ucdExperimental                    FROM UCD-SNMP-MIB
    InetAddress, InetAddressType,
    InetPortNumber                     FROM INET-ADDRESS-MIB;


myPrivMIB MODULE-IDENTITY
    LAST-UPDATED "200803310000Z"
    ORGANIZATION "Mine"
    CONTACT-INFO "Bandung
                [EMAIL PROTECTED]
                "
    DESCRIPTION  "Private MIB for listing any host who ever connect to this host
                 "
    ::= { ucdExperimental 18 }

myPriv     OBJECT IDENTIFIER ::= { myPrivMIB 1 }


myHostTable OBJECT-TYPE
    SYNTAX      SEQUENCE OF WiltHostEntry
    MAX-ACCESS  not-accessible
    STATUS      current
    DESCRIPTION
        "This is an object that simply supports a writable integer
         when compiled into the agent.  See
         implementation details."
    ::= { myPriv 1 }

myHostEntry OBJECT-TYPE
    SYNTAX      myHostEntry
    MAX-ACCESS  not-accessible
    STATUS      current
    DESCRIPTION
        "This is an object that simply supports a writable integer
         when compiled into the agent.  See
         implementation details."
    INDEX { myHostId }
    ::= { myHostTable 1 }

myHostEntry ::= SEQUENCE {
        myHostId                Integer32,
        myHostAddress           InetAddress}

myHostId OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-only
    STATUS      current
    DESCRIPTION
            "Define as host Index, from file HostIndex"
    ::= { myHostEntry 1 }

myHostAddress OBJECT-TYPE
    SYNTAX      InetAddress
    MAX-ACCESS  read-only
    STATUS      current
    DESCRIPTION
            "IP Address of host listed in HostIndex"
    ::= { myHostEntry 2 }

END

after that, I make that mib to c code using mib2c command :
#  env MIBS="All" mib2c -c  mib2c.iterate.conf  myPrivMIB
that command will generate myPrivMIB.c and myPrivMIB.h

I edit myPrivMIB.c, I added procedure DataToLinkedList() so it will
load data from file /etc/HostIndex automatically every this agent
start :
/*
 * Note: this file originally auto-generated by mib2c using
 *  : mib2c.iterate.conf,v 5.19 2006/09/07 16:17:35 dts12 Exp $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "myPrivMIB.h"

#define INDEX_FILE "/etc/HostIndex"

void DataToLinkedList();

/** Initializes the myPrivMIB module */
void
init_myPrivMIB(void)
{
    /*
     * here we initialize all the tables we're planning on supporting
     */
        DataToLinkedList();
    initialize_table_myHostTable();
}

//# Determine the first/last column names

/** Initialize the myHostTable table by defining its contents and how
it's structured */
void
initialize_table_myHostTable(void)
{
    static oid      myHostTable_oid[] =
        { 1, 3, 6, 1, 4, 1, 2021, 13, 18, 1, 1 };
    size_t          myHostTable_oid_len = OID_LENGTH(myHostTable_oid);
    netsnmp_handler_registration *reg;
    netsnmp_iterator_info *iinfo;
    netsnmp_table_registration_info *table_info;

    reg =
        netsnmp_create_handler_registration("myHostTable",
                                            myHostTable_handler,
                                            myHostTable_oid,
                                            myHostTable_oid_len,
                                            HANDLER_CAN_RONLY);

    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
    netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER,   /*
index: myHostId */
                                     0);
    table_info->min_column = COLUMN_MYHOSTID;
    table_info->max_column = COLUMN_MYHOSTADDRESS;

    iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
    iinfo->get_first_data_point = myHostTable_get_first_data_point;
    iinfo->get_next_data_point = myHostTable_get_next_data_point;
    iinfo->table_reginfo = table_info;

    netsnmp_register_table_iterator(reg, iinfo);

    /*
     * Initialise the contents of the table here
     */
}

    /*
     * Typical data structure for a row entry
     */
struct myHostTable_entry {
    /*
     * Index values
     */
//    long            myHostId;

    /*
     * Column values
     */
    long            myHostId;
    char            myHostAddress[1024];
    size_t          myHostAddress_len;

    /*
     * Illustrate using a simple linked list
     */
    int             valid;
    struct myHostTable_entry *next;
};

struct myHostTable_entry *myHostTable_head;

/*
 * create a new row in the (unsorted) table
 */
struct myHostTable_entry *
myHostTable_createEntry(long myHostId)
{
    struct myHostTable_entry *entry;

    entry = SNMP_MALLOC_TYPEDEF(struct myHostTable_entry);
    if (!entry)
        return NULL;

    entry->myHostId = myHostId;
    entry->next = myHostTable_head;
    myHostTable_head = entry;
    return entry;
}

/*
 * remove a row from the table
 */
void
myHostTable_removeEntry(struct myHostTable_entry *entry)
{
    struct myHostTable_entry *ptr, *prev;

    if (!entry)
        return;                 /* Nothing to remove */

    for (ptr = myHostTable_head, prev = NULL;
         ptr != NULL; prev = ptr, ptr = ptr->next) {
        if (ptr == entry)
            break;
    }
    if (!ptr)
        return;                 /* Can't find it */

    if (prev == NULL)
        myHostTable_head = ptr->next;
    else
        prev->next = ptr->next;

    SNMP_FREE(entry);           /* XXX - release any other internal resources */
}


void DataToLinkedList() {
        char hostidx[32], ipaddr[64];
        char cbuff[255];
        bzero(cbuff, sizeof(cbuff));
        FILE *fidx;
        fidx = fopen(INDEX_FILE, "r");

        while (fgets(cbuff, sizeof(cbuff), fidx) != NULL) {
                sscanf(cbuff, "%s %s", hostidx, ipaddr);
                struct myHostTable_entry *row = malloc(sizeof(struct
myHostTable_entry));
                row->myHostId = atoi(hostidx);
                strcpy(row->myHostAddress, ipaddr);
                row->myHostAddress_len = strlen(ipaddr);

                if (myHostTable_head == NULL) {
                        myHostTable_head = row;
                } else {
                        row->next = myHostTable_head;
                        myHostTable_head = row;
                }
        }
        fclose(fidx);
}


/*
 * Example iterator hook routines - using 'get_next' to do most of the work
 */
netsnmp_variable_list *
myHostTable_get_first_data_point(void **my_loop_context,
                                 void **my_data_context,
                                 netsnmp_variable_list * put_index_data,
                                 netsnmp_iterator_info *mydata)
{
    *my_loop_context = myHostTable_head;
    return myHostTable_get_next_data_point(my_loop_context,
                                           my_data_context, put_index_data,
                                           mydata);
}

netsnmp_variable_list *
myHostTable_get_next_data_point(void **my_loop_context,
                                void **my_data_context,
                                netsnmp_variable_list * put_index_data,
                                netsnmp_iterator_info *mydata)
{
    struct myHostTable_entry *entry =
        (struct myHostTable_entry *) *my_loop_context;
    netsnmp_variable_list *idx = put_index_data;

    if (entry) {
        snmp_set_var_typed_integer(idx, ASN_INTEGER, entry->myHostId);
        idx = idx->next_variable;
        *my_data_context = (void *) entry;
        *my_loop_context = (void *) entry->next;
        return put_index_data;
    } else {
        return NULL;
    }
}


/** handles requests for the myHostTable table */
int
myHostTable_handler(netsnmp_mib_handler *handler,
                    netsnmp_handler_registration *reginfo,
                    netsnmp_agent_request_info *reqinfo,
                    netsnmp_request_info *requests)
{

    netsnmp_request_info *request;
    netsnmp_table_request_info *table_info;
    struct myHostTable_entry *table_entry;

    switch (reqinfo->mode) {
        /*
         * Read-support (also covers GetNext requests)
         */
    case MODE_GET:
        for (request = requests; request; request = request->next) {
            table_entry = (struct myHostTable_entry *)
                netsnmp_extract_iterator_context(request);
            table_info = netsnmp_extract_table_info(request);

            switch (table_info->colnum) {
            case COLUMN_MYHOSTID:
                snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
                                           table_entry->myHostId);
                break;
            case COLUMN_MYHOSTADDRESS:
                snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
                                         (u_char *) table_entry->
                                         myHostAddress,
                                         table_entry->myHostAddress_len);
                break;
            default:
                /*
                 * An unsupported/unreadable column (if applicable)
                 */
                snmp_set_var_typed_value(request->requestvb,
                                         SNMP_NOSUCHOBJECT, NULL, 0);
            }
        }
        break;

    }
    return SNMP_ERR_NOERROR;
}

Then, I compile that code to agent with this command :
#  net-snmp-config --compile-subagent mysubagent myPrivMIB.c

Next, I am trying that code :
# ./mysubagent -Le -f
NET-SNMP version 5.4 AgentX subagent connected


trying snmpwalk to that oid :
# snmpwalk -c public localhost .1.3.6.1.4.1.2021.13.18
UCD-SNMP-MIB::ucdExperimental.18.1.1.1.1.0 = INTEGER: 0
UCD-SNMP-MIB::ucdExperimental.18.1.1.1.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdExperimental.18.1.1.1.2.0 = STRING: "192.168.1.3"
UCD-SNMP-MIB::ucdExperimental.18.1.1.1.2.1 = STRING: "192.168.2.2"
Error in packet.
Reason: (genError) A general failure occured
Failed object: UCD-SNMP-MIB::ucdExperimental.18.1.1.1.2.1
#

and mysubagent exit with error :
# ./mysubagent -Le -f
NET-SNMP version 5.4 AgentX subagent connected
Segmentation fault

Please help me, is there something I passed?
Thanks for your helps.

Regards,
Denny

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

Reply via email to