hi
I modified snmptrapd file, add a global trap handler by myself, and I delete some instructuions of standard snmptrapd(I think it maybe useless), and It runs ok, but I find a problem, when sending trap messages after my snmptrapd program started for one minute, my deamon couldn't receive the trap messages or couldn't invoke my global trap handler..I don't konw why? did I delete crucial code of original snmptrapd??

following is my thin snmptraps code:

start_trapd like main funtion!

int start_trapd()
{
   netsnmp_session *sess_list = NULL, *ss = NULL;
   netsnmp_transport *transport = NULL;
   int             i = 0, depmsg = 0;
   int             count, numfds, block;
   fd_set          fdset;
   struct timeval  timeout, *tvp;
   int             dofork = 1;
   char           *cp, *listen_ports = NULL;
   char           *trap1_fmt_str_remember = NULL;

//#ifdef SIGTERM
//    signal(SIGTERM, term_handler);
//#endif
//#ifdef SIGHUP
//    signal(SIGHUP, hup_handler);
//#endif
//#ifdef SIGINT
//    signal(SIGINT, term_handler);
//#endif
//#ifdef SIGPIPE
//    signal(SIGPIPE, SIG_IGN);   /* 'Inline' failure of wayward readers */
// #endif

   /*
* we need to be called back later */
   snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
                          usm_store_users, NULL);

#ifdef WIN32
   setvbuf(stdout, NULL, _IONBF, BUFSIZ);
#else
   setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
#endif

        /*
* don't fail if we can't do agentx (ie, socket not there, or not root)
    */
netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_NO_ROOT_ACCESS);
   /*
    * ignore any warning messages.
    */
netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_NO_CONNECTION_WARNINGS);

   /*
* initialize the agent library */
   init_agent("snmptrapd");

/*
        add trap message handler
        --anders
*/
netsnmp_add_global_traphandler(NETSNMPTRAPD_POST_HANDLER, trap_message_handler);

   /*
* Initialize the world. Create initial user */
//    init_snmp("snmptrapd");
//    if (trap1_fmt_str_remember) {
//        free_trap1_fmt();
//        free_trap2_fmt();
//        print_format1 = strdup(trap1_fmt_str_remember);
//        print_format2 = strdup(trap1_fmt_str_remember);
//    }

if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_QUIT_IMMEDIATELY)) {
       /*
        * just starting up to process specific configuration and then
* shutting down immediately. */
       running = 0;
   }

    SOCK_STARTUP;

   if (listen_ports)
       cp = listen_ports;
   else
       cp = default_port;

   while (cp != NULL) {
       char *sep = strchr(cp, ',');
       char  listen_name[128];
       char *cp2 = strchr(cp, ':');

       if (sep != NULL) {
           *sep = 0;
       }

          /*
           * Make sure this defaults to listening on port 162
           */
       if (!cp2) {
           snprintf(listen_name, sizeof(listen_name), "%s:162", cp);
           cp2 = listen_name;
       } else {
           cp2 = cp;
       }
       transport = netsnmp_tdomain_transport(cp2, 1, "udp");
       if (transport == NULL) {
//            snmp_log(LOG_ERR, "couldn't open %s -- errno %d (\"%s\")\n",
//                     cp2, errno, strerror(errno));
           snmptrapd_close_sessions(sess_list);
           SOCK_CLEANUP;
           exit(1);
       } else {
           ss = snmptrapd_add_session(transport);
           if (ss == NULL) {
               /*
                * Shouldn't happen?  We have already opened the transport
* successfully so what could have gone wrong? */
               snmptrapd_close_sessions(sess_list);
               netsnmp_transport_free(transport);
//                if (Syslog) {
//                    snmp_log(LOG_ERR, "couldn't open snmp - %m");
//                }
               SOCK_CLEANUP;
               exit(1);
           } else {
               ss->next = sess_list;
               sess_list = ss;
           }
       }

       /*
* Process next listen address, if there is one. */

       if (sep != NULL) {
           *sep = ',';
           cp = sep + 1;
       } else {
           cp = NULL;
       }
   }

   /*
    * ignore early sighup during startup
    */
   reconfig = 0;

//#ifdef WIN32SERVICE
//    trapd_status = SNMPTRAPD_RUNNING;
//#endif
   while (running) {
       if (reconfig) {
            reconfig = 0;
       }
       numfds = 0;
       FD_ZERO(&fdset);
       block = 0;
       tvp = &timeout;
       timerclear(tvp);
       tvp->tv_sec = 5;
       snmp_select_info(&numfds, &fdset, tvp, &block);
       if (block == 1)
           tvp = NULL;         /* block without timeout */
       count = select(numfds, &fdset, 0, 0, tvp);
       gettimeofday(&Now, 0);
       if (count > 0) {
           snmp_read(&fdset);
       } else
           switch (count) {
           case 0:
               snmp_timeout();
               break;
           case -1:
               if (errno == EINTR)
                   continue;
//                 snmp_log_perror("select");
               running = 0;
               break;
           default:
//                 fprintf(stderr, "select returned %d\n", count);
               running = 0;
           }
        run_alarms();
   }

   if (Print || Log) {
       struct tm      *tm;
       time_t          timer;
       time(&timer);
       tm = localtime(&timer);
   }


   snmptrapd_close_sessions(sess_list);
   snmp_shutdown("snmptrapd");
//#ifdef WIN32SERVICE
//    trapd_status = SNMPTRAPD_STOPPED;
//#endif
   snmp_disable_log();
   SOCK_CLEANUP;
   return 0;
}

int trap_message_handler(netsnmp_pdu         *pdu,
                      netsnmp_transport     *transport,
                      netsnmp_trapd_handler *handler)
{
        char *strIpAddress;
        struct in_addr sInAddr;
        char oidBuf[200];
        struct variable_list *vars;

        memset(oidBuf, 200, 0);

        //parse pdu->enterprise variable to string
        snprint_objid(oidBuf, 200, pdu->enterprise, pdu->enterprise_length);
        
        //covert agent_addr to string ip address
        sInAddr.S_un.S_un_b.s_b1 = pdu->agent_addr[0];
        sInAddr.S_un.S_un_b.s_b2 = pdu->agent_addr[1];
        sInAddr.S_un.S_un_b.s_b3 = pdu->agent_addr[2];
        sInAddr.S_un.S_un_b.s_b4 = pdu->agent_addr[3];
        strIpAddress = inet_ntoa(sInAddr);

        //print basic information
        printf("Agent address  : %s\n", strIpAddress);
        printf("Enterprise     : %s\n", oidBuf);
        printf("Generic trap   : %d\n", pdu->trap_type);
        printf("Sepecific trap : %d\n", pdu->specific_type);
        printf("Time stamp     : %d\n", pdu->time);

        //print variable
        printf("Bind variable  :\n");
        for (vars = pdu->variables; vars; vars = vars->next_variable)
        {
                char buf[1000];
                memset (buf, 1000, 0);
                //snprint_variable (buf, 1000, vars->name, vars->name_length, 
vars);
                print_variable(vars->name, vars->name_length, vars);
        }
        printf("--------------------------\n");
        return 1;
}


Best Regards
Anders

_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
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