Hello all,

I've a problem to communicate with my agentx.
I've set the MIBS environment variable by typing 'export MIBS=ALL'

First, I write a small mib containing 2 objects (counter1 and counters2).
Thanks to the mib2c tool, i've generated the C code and update the data
needed.
I follow all the steps needed in order to configure the master agent, make
and make install.
All these steps succeeded and through another console I was able to send
'snmpget' command and get an answer.

Following this, I've created a daemon by following the tutorial on site
'Writing a subagent'.
My daemon is working, I've compiled it and see that the reading of the
shared memory is done every time a value is updated in the shared memory.

To launch the snmpd, I use the following command line:
snmpd -Dagentx -f -Lo -C -c /usr/local/share/snmpd.conf
--agentXSocket=tcp:localhost:1705 udp:1161

registered debug token agentx, 1
Log handling defined - disabling stderr
agentx_register_app_config_handler: registering .conf token for
"agentxsocket"
agentx_register_app_config_handler: registering .conf token for
"agentxperms"
agentx_register_app_config_handler: registering .conf token for
"agentxRetries"
agentx_register_app_config_handler: registering .conf token for
"agentxTimeout"
Turning on AgentX master support.
agentx/config: port spec: /var/agentx/master
agentx/config: port permissions: 777 777
agentx/config: socket permissions: 777 (511)
agentx/config: directory permissions: 777 (511)
agentx/config: port spec: tcp:localhost:1705
agentx/master: initializing...
agentx/master: initializing...   DONE
NET-SNMP version 5.

See below the content of the snmpd.conf located in
/usr/local/share/snmpd.conf

master agentx
agentxsocket /var/agentx/master
agentxperms 777 777

rocommunity MyComRO
rwcommunity MyComRW
syscontact surn...@company.com
syslocation 1st Floor / Town / Country

com2sec readonly default public
com2sec readwrite default private
rocommunity public
rwcommunity private

I've checked, I haven't a snmpd.conf file in /etc/snmp.

I'm quite lost, it looks that snmpd is not correctly configured.
Moreover, I don't really understand, why when I'm typing the following
command, snmpget -v 2c -c MyComRO localhost system.sysLocation.0
I've also a "Timeout:no response from localhost".

I've also checked that the Shared Memory is correctly created and available.

Do you have any clue in order to debug it or to understand what is not
correctly configured ?

By advance thanks for your help.
-- 
Best Regards.
Phil
TEST-MIB DEFINITIONS ::= BEGIN

-- MIB of the TEST-MIB

IMPORTS
        OBJECT-TYPE,
        Integer32,
        enterprises,
        IpAddress,
        MODULE-IDENTITY                       FROM SNMPv2-SMI
        MODULE-COMPLIANCE, OBJECT-GROUP       FROM SNMPv2-CONF;

--
-- A brief description and update information about this mib.
--
testmib MODULE-IDENTITY
    LAST-UPDATED "3011190000Z"            -- 01 Apr 2001, midnight
    ORGANIZATION "PHIL"
    CONTACT-INFO "None
                 "
    DESCRIPTION  "MIB for remote control by SNMP
                 "

    REVISION "0104010000Z"
    DESCRIPTION "Modification"
    ::= { enterprises 9369 }

counters           OBJECT IDENTIFIER ::= { testmib 1 }
reserved           OBJECT IDENTIFIER ::= { testmib 2 }

-- Define the sections of the mib them selves:

--
-- First Counter of the Counters
--
counter1        OBJECT-TYPE
    SYNTAX      Integer32 (0..100)
    MAX-ACCESS  read-only
    STATUS      current
    DESCRIPTION
        "First Counter"
    DEFVAL { 0 }
    ::= { counters 1 }

--
-- Second Counter of the Counters
--
counter2        OBJECT-TYPE
    SYNTAX      Integer32 (0..100)
    MAX-ACCESS  read-only
    STATUS      current
    DESCRIPTION
        "Second Counter"
    DEFVAL { 0 }
    ::= { counters 2 }

END
/*
 * Note: this file originally auto-generated by mib2c
 * using mib2c.scalar.conf
 */

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

unsigned int MyCounter1 = 234;
unsigned int MyCounter2 = 800;

/** Initializes the testmib module */
void init_testmib(void)
{
    const oid counter1_oid[] = { 1,3,6,1,4,1,9369,1,1 };
    const oid counter2_oid[] = { 1,3,6,1,4,1,9369,1,2 };

  DEBUGMSGTL(("testmib", "Initializing\n"));

    netsnmp_register_scalar(
        netsnmp_create_handler_registration("counter1", handle_counter1,
                               counter1_oid, OID_LENGTH(counter1_oid),
                               HANDLER_CAN_RONLY
        ));
    netsnmp_register_scalar(
        netsnmp_create_handler_registration("counter2", handle_counter2,
                               counter2_oid, OID_LENGTH(counter2_oid),
                               HANDLER_CAN_RONLY
        ));
}

int handle_counter1(netsnmp_mib_handler *handler,
                    netsnmp_handler_registration *reginfo,
                    netsnmp_agent_request_info   *reqinfo,
                    netsnmp_request_info         *requests)
{
    DEBUGMSGTL(("testmib", "Accessing counter 1\n"));

    switch(reqinfo->mode)
    {
        case MODE_GET:
        {
            DEBUGMSGTL(("testmib", "Mode GET\n"));
            snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
                                     &MyCounter1,
                                     sizeof(MyCounter1));
        }
        break;

        default:
        {
            DEBUGMSGTL(("testmib", "ERROR\n"));
            /* we should never get here, so this is a really bad error */
            snmp_log(LOG_ERR, "unknown mode (%d) in handle_counter1\n", reqinfo->mode );
            return SNMP_ERR_GENERR;
        }
    }

    return SNMP_ERR_NOERROR;
}

int handle_counter2(netsnmp_mib_handler *handler,
                    netsnmp_handler_registration *reginfo,
                    netsnmp_agent_request_info   *reqinfo,
                    netsnmp_request_info         *requests)
{
    DEBUGMSGTL(("testmib", "Accessing counter 2\n"));

    switch(reqinfo->mode)
    {
        case MODE_GET:
        {
            snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
                                     &MyCounter2,
                                     sizeof(MyCounter2));
        }
        break;

        default:
        {
            /* we should never get here, so this is a really bad error */
            snmp_log(LOG_ERR, "unknown mode (%d) in handle_counter2\n", reqinfo->mode );
            return SNMP_ERR_GENERR;
        }
    }

    return SNMP_ERR_NOERROR;
}

/*
 * Note: this file originally auto-generated by mib2c
 * using mib2c.scalar.conf
 */
#ifndef TESTMIB_H
#define TESTMIB_H

/* function declarations */
void init_testmib(void);
Netsnmp_Node_Handler handle_counter1;
Netsnmp_Node_Handler handle_counter2;

#endif /* TESTMIB_H */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <signal.h>

#include "stdio.h"
#include "stdlib.h"

#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "testmib.h"
#include "TestShm.h"

const char ShmName[10] = { "/MyShm_1" };

extern unsigned int MyCounter1;
extern unsigned int MyCounter2;

static int keep_running;

RETSIGTYPE stop_server(int a)
{
    keep_running = 0;
}

int main (int argc, char **argv) 
{
    int ShmFd;
    MyShm_Tp addr;

    int agentx_subagent=1;  /* change this if you want to be a SNMP master agent */
    int background = 0;     /* change this if you want to run in the background */
    int syslog = 0;         /* change this if you want to use syslog */

    /* print log errors to syslog or stderr */
    if (syslog)
        snmp_enable_calllog();
    else
        snmp_enable_stderrlog();

    /* we're an agentx subagent? */
    if (agentx_subagent)
    {
        /* make us a agentx client. */
        netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
    }

    /* run in background, if requested */
    if (background && netsnmp_daemonize(1, !syslog))
        exit(1);

    /* initialize tcpip, if necessary */
    SOCK_STARTUP;

    /* initialize the agent library */
    init_agent("example-demon");

    /****************************/
    /* initialize mib code here */
    /****************************/
   
        /* Create a Shared Memory in Read only */
        ShmFd = shm_open(ShmName, O_RDONLY, S_IRUSR);

        /* Check if the Shared Memory is successfully created */
        if (ShmFd == -1)
        {
            printf("The creation of the Shared Memory failed\n");
            return EXIT_FAILURE;
        }

        /* Map the Shared Memory */
        addr = mmap(NULL, sizeof(MyShm_T), PROT_READ, MAP_SHARED, ShmFd, 0);

        if (addr == MAP_FAILED)
        {
            printf("The mapping of the Shared Memory failed\n");
            return EXIT_FAILURE;
        }

    /* mib code: init_nstAgentSubagentObject from nstAgentSubagentObject.C */
    init_testmib();  

    /* initialize vacm/usm access control  */
    if (!agentx_subagent)
    {
        init_vacm_vars();
        init_usmUser();
    }

    /* example-demon will be used to read example-demon.conf files. */
    //init_snmp("example-demon");

    /* If we're going to be a snmp master agent, initial the ports */
    if (!agentx_subagent)
        init_master_agent();    /* open the port to listen on (defaults to udp:161) */

    /* In case we receive a request to stop (kill -TERM or kill -INT) */
    keep_running = 1;
    signal(SIGTERM, stop_server);
    signal(SIGINT, stop_server);

    snmp_log(LOG_INFO,"mydaemon is up and running.\n");

    /* your main loop here... */
    while(keep_running)
    {
        /* Read the Shared Memory */
        if (MyCounter1 != addr->Counter1)
        {
            snmp_log(LOG_INFO,"MyCounter1 update in progress\n");
            MyCounter1 = addr->Counter1;
        }

        if (MyCounter1 != addr->Counter1)
        {
            snmp_log(LOG_INFO,"MyCounter1 update in progress\n");
            MyCounter2 = addr->Counter2;
        }

        /* if you use select(), see snmp_select_info() in snmp_api(3) */
        /*     --- OR ---  */
        agent_check_and_process(0); /* 0 == don't block */
    }

    /* at shutdown time */
    snmp_shutdown("example-demon");
    SOCK_CLEANUP;

    return 0;
}


typedef struct
{
    unsigned int Counter1;
    unsigned int Counter2;
} MyShm_T, *MyShm_Tp;
  
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to