OSDN Git Service

maged dhc codes.
author6638678 <6638678@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Thu, 21 Oct 2010 10:29:18 +0000 (10:29 +0000)
committer6638678 <6638678@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Thu, 21 Oct 2010 10:29:18 +0000 (10:29 +0000)
git-svn-id: http://10.144.169.20/repos/um/branches/l7vsd-3.x-ramiel-epoll-cond@10396 1ed66053-1c2d-0410-8867-f7571e6e31d3

l7vsd/include/snmpfunc.h
l7vsd/src/snmpagent_impl.cpp
l7vsd/src/snmpfunc.cpp

index 191bd58..21c8fae 100644 (file)
@@ -36,7 +36,6 @@
 #include <net-snmp/agent/net-snmp-agent-includes.h>
 
 using namespace l7vs;
-
 /*
  * function declarations
  */
@@ -54,10 +53,10 @@ int        handle_get_vstable_count(netsnmp_mib_handler *handler,
  * handle for replication state.
  *
  */
-int     handle_get_rep_state(netsnmp_mib_handler *handler,
-                             netsnmp_handler_registration *reginfo,
-                             netsnmp_agent_request_info *reqinfo,
-                             netsnmp_request_info *requests);
+int        handle_get_rep_state(netsnmp_mib_handler *handler,
+                                netsnmp_handler_registration *reginfo,
+                                netsnmp_agent_request_info *reqinfo,
+                                netsnmp_request_info *requests);
 
 /*!
  * handle for virtual service table.
@@ -78,7 +77,7 @@ int        handle_get_rstable(netsnmp_mib_handler *handler,
                               netsnmp_request_info *requests);
 
 /*!
- * initialize snmp scalar and table data set handles.
+ * initialize snmp scaler and table data set handles.
  *
  * @param[in]      error_code& is trap error code
  * @retrun         void
@@ -86,6 +85,12 @@ int        handle_get_rstable(netsnmp_mib_handler *handler,
 void    init_snmp_handles(error_code& err);
 
 /*!
+ * unregister snmp handler.
+ *
+ */
+void    unregister_handler();
+
+/*!
  * initialize virtual service table data set.
  *
  * @param[in]      error_code& is trap error code
index 1f774f3..9e421e3 100644 (file)
@@ -113,8 +113,7 @@ namespace l7vs
 
                         if (it != trapfunc_map.end()) {
                                 //send trap message
-                                if (it->second(trapmsg.message) == SNMP_ERR_NOERROR)
-                                {
+                                if (it->second(trapmsg.message) == SNMP_ERR_NOERROR) {
                                         //set trap last date to current time
                                         trap_last_date = time(NULL);
 
@@ -236,7 +235,7 @@ namespace l7vs
                                 trapfunc_map[trapmessage::INFO_LOG]              = &trap_info;
                                 trapfunc_map[trapmessage::DEBUG_LOG]             = &trap_debug;
 
-                                //init member variables
+                                //init member varable
                                 mib_collect_last_time = boost::posix_time::ptime(boost::posix_time::min_date_time);
                                 start_date = 0;
                                 request_last_date = 0;
@@ -264,7 +263,44 @@ namespace l7vs
 
                                 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
 
-                                //set initialize flag true
+                                //socket startup
+                                SOCK_STARTUP;
+
+                                //init l7vsAgent
+                                int ret = init_agent("l7vsAgent");
+                                if (ret) {
+                                        std::string msg("init_agent failed.");
+                                        Logger::putLogFatal(LOG_CAT_L7VSD_SNMPAGENT, 3, msg, __FILE__, __LINE__);
+                                        //set error code
+                                        err.setter(true, msg);
+                                        return;
+                                }
+
+                                init_snmp("l7vsAgent");
+
+                                //here we initialize all the tables we're planning on supporting
+                                initialize_virtual_service_table(err);
+
+                                if (err) {
+                                        std::string msg("initialize_virtual_service_table failed.");
+                                        Logger::putLogFatal(LOG_CAT_L7VSD_SNMPAGENT, 4, msg, __FILE__, __LINE__);
+                                        //set error code
+                                        err.setter(true, msg);
+                                        return;
+                                }
+
+                                initialize_real_server_table(err);
+
+                                if (err) {
+                                        std::string msg("initialize_real_server_table failed.");
+                                        Logger::putLogFatal(LOG_CAT_L7VSD_SNMPAGENT, 5, msg, __FILE__, __LINE__);
+                                        //set error code
+                                        err.setter(true, msg);
+                                        return;
+                                }
+
+
+                                //set initializ flag true
                                 initialized = true;
 
                                 /*-------- DEBUG LOG --------*/
@@ -324,9 +360,8 @@ namespace l7vs
                                 {
                                         boost::mutex::scoped_lock lock(trap_msg_queue_condition_mutex);
 
-                                        //drop the message when the trap message queue is too large
-                                        if( trap_msg_queue.size() < trap_queue_max_size.get())
-                                        {
+                                        //drap the message when the trap message queue is too large
+                                        if ( trap_msg_queue.size() < trap_queue_max_size.get()) {
                                                 //push the message into trap message queue
                                                 trap_msg_queue.push_back(trapmessage);
                                                 /*-------- DEBUG LOG --------*/
@@ -338,8 +373,7 @@ namespace l7vs
                                                         Logger::putLogDebug(LOG_CAT_L7VSD_SNMPAGENT, 32, debugstr.str(), __FILE__, __LINE__);
                                                 }
                                                 /*------ DEBUG LOG END ------*/
-                                        }
-                                        else{
+                                        } else {
                                                 /*-------- DEBUG LOG --------*/
                                                 if (LOG_LV_DEBUG == Logger::getLogLevel(LOG_CAT_L7VSD_SNMPAGENT)) {
                                                         std::string  debugstr =  "function : snmpagent_impl::push_trapmessage : trap message queue is overflow.";
@@ -353,8 +387,7 @@ namespace l7vs
                                 trap_msg_queue_condition.notify_one();
 
                         }
-                }
-                catch (const std::exception &e) {
+                } catch (const std::exception &e) {
                         std::stringstream msg;
                         msg << "Push trap message failed : " << e.what() << ".";
 
@@ -406,21 +439,6 @@ namespace l7vs
                 }
 
                 try {
-                       //socket startup
-                        SOCK_STARTUP;
-
-                        //init l7vsAgent
-                        int ret = init_agent("l7vsAgent");
-                        if (ret) {
-                                std::string msg("init_agent failed.");
-                                Logger::putLogFatal(LOG_CAT_L7VSD_SNMPAGENT, 3, msg, __FILE__, __LINE__);
-                                //set error code
-                                err.setter(true, msg);
-                                return;
-                        }
-
-                        init_snmp("l7vsAgent");
-
                         //regist snmp get item' handle
                         init_snmp_handles(err);
 
@@ -475,22 +493,20 @@ namespace l7vs
                 enabled = FALSE;
 
                 if (!start_flag) {
-                        std::string  str =  "snmp function has already stopped.";
+                        std::string  str =  "snmp function has already stoped.";
                         Logger::putLogInfo(LOG_CAT_L7VSD_SNMPAGENT, 2, str, __FILE__, __LINE__);
                         return;
                 }
 
                 start_flag = false;
 
+                unregister_handler();
+
                 try {
                         trap_thread.interrupt();
                         process_mib_thread.join();
                         trap_thread.join();
 
-                        snmp_shutdown("l7vsAgent");
-                        shutdown_agent();
-                        SOCK_CLEANUP;
-
                         std::string  str =  "snmp function stop.";
                         Logger::putLogInfo(LOG_CAT_L7VSD_SNMPAGENT, 3, str, __FILE__, __LINE__);
 
@@ -506,7 +522,8 @@ namespace l7vs
          *
          * @retrun         void
          */
-        void snmpagent_impl::enable() {
+        void snmpagent_impl::enable()
+        {
                 Logger    logger(LOG_CAT_L7VSD_SNMPAGENT, 38, "snmpagent_impl::enable", __FILE__, __LINE__);
 
                 //set snmp function enable flag
@@ -520,8 +537,7 @@ namespace l7vs
                                 msg << "snmp function start failed.";
                                 Logger::putLogError(LOG_CAT_L7VSD_SNMPAGENT, 7, msg.str(), __FILE__, __LINE__);
                         }
-                }
-                else {
+                } else {
                         std::string  str =  "snmp function has already started.";
                         Logger::putLogInfo(LOG_CAT_L7VSD_SNMPAGENT, 4, str, __FILE__, __LINE__);
                 }
@@ -532,16 +548,16 @@ namespace l7vs
          *
          * @retrun         void
          */
-        void snmpagent_impl::disable() {
+        void snmpagent_impl::disable()
+        {
                 Logger    logger(LOG_CAT_L7VSD_SNMPAGENT, 39, "snmpagent_impl::disable", __FILE__, __LINE__);
 
                 //set snmp function enable flag
                 enabled = FALSE;
                 if (start_flag == true) {
                         stop();
-                }
-                else {
-                        std::string  str =  "snmp function has already stopped.";
+                } else {
+                        std::string  str =  "snmp function has already stoped.";
                         Logger::putLogInfo(LOG_CAT_L7VSD_SNMPAGENT, 5, str, __FILE__, __LINE__);
                 }
         }
@@ -550,7 +566,8 @@ namespace l7vs
          * set log trap enable
          * @retrun  void
          */
-        void snmpagent_impl::logtrap_enable() {
+        void snmpagent_impl::logtrap_enable()
+        {
                 Logger    logger(LOG_CAT_L7VSD_SNMPAGENT, 40, "snmpagent_impl::logtrap_enable", __FILE__, __LINE__);
 
                 logtrap_enabled = TRUE;
@@ -561,7 +578,8 @@ namespace l7vs
          * set log trap disable
          * @retrun  void
          */
-        void snmpagent_impl::logtrap_disable() {
+        void snmpagent_impl::logtrap_disable()
+        {
                 Logger    logger(LOG_CAT_L7VSD_SNMPAGENT, 41, "snmpagent_impl::logtrap_disable", __FILE__, __LINE__);
 
                 logtrap_enabled = FALSE;
@@ -725,7 +743,7 @@ namespace l7vs
                                 /*------ DEBUG LOG END ------*/
 
                                 http_stats &httpstats = static_cast<http_stats&>(base_stats);
-                                //clear http statistics
+                                //clear http statstics
                                 httpstats.http_requests = 0ULL;
                                 httpstats.http_get_requests = 0ULL;
                                 httpstats.http_post_requests = 0ULL;
@@ -735,7 +753,7 @@ namespace l7vs
         }
 
         /*!
-         *  set vsd information.
+         *  set vsd infomation.
          *
          * @param[in]      vsd is vsd pointer.
          * @retrun         void
@@ -748,9 +766,9 @@ namespace l7vs
         }
 
         /*!
-         *  get snmp information.
+         *  get snmp infomation.
          *
-         * @retrun         snmp_info is snmp information.
+         * @retrun         snmp_info is snmp infomation.
          */
         snmp_info snmpagent_impl::get_snmp_info()
         {
@@ -954,7 +972,12 @@ namespace l7vs
                 if (start_flag == true) {
                         stop();
                 }
-                
+
+                //shutdown l7vsAgent
+                snmp_shutdown("l7vsAgent");
+                shutdown_master_agent();
+                shutdown_agent();
+                SOCK_CLEANUP;
         }
 
         /*!
index 4ba0ad5..273670c 100644 (file)
 static const oid snmptrap_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
 netsnmp_table_data_set *vs_table;
 netsnmp_table_data_set *rs_table;
-
+netsnmp_handler_registration *vs_table_size_handler = NULL;
+netsnmp_handler_registration *vs_table_handler = NULL;
+netsnmp_handler_registration *rs_table_handler = NULL;
+netsnmp_handler_registration *replication_handler = NULL;
 
 /*!
- * initialize snmp scalar and table data set handles.
+ * register snmp scaler and table data set handler.
  *
  * @param[in]      error_code& is trap error code
  * @retrun         void
@@ -49,48 +52,92 @@ init_snmp_handles(error_code& err)
 
         oid       l7vsVsNumber_oid[] = { 1, 3, 6, 1, 4, 1, 32132, 1, 1, 1, 1 };
         oid       l7vsReplicationMode_oid[] = { 1, 3, 6, 1, 4, 1, 32132, 1, 1, 1, 4 };
+        oid       l7vsVsTable_oid[] = { 1, 3, 6, 1, 4, 1, 32132, 1, 1, 1, 2 };
+        oid       l7vsRsTable_oid[] = { 1, 3, 6, 1, 4, 1, 32132, 1, 1, 1, 3 };
 
         DEBUGMSGTL(("ultramonkey_l7", "init_snmp_handles\n"));
 
-        int ret = netsnmp_register_scalar(netsnmp_create_handler_registration
-                                          ("l7vsVsNumber", handle_get_vstable_count,
-                                           l7vsVsNumber_oid,
-                                           OID_LENGTH(l7vsVsNumber_oid),
-                                           HANDLER_CAN_RONLY));
+        vs_table_size_handler = netsnmp_create_handler_registration
+                                ("l7vsVsNumber", handle_get_vstable_count,
+                                 l7vsVsNumber_oid,
+                                 OID_LENGTH(l7vsVsNumber_oid),
+                                 HANDLER_CAN_RONLY);
+
+        int ret = netsnmp_register_scalar(vs_table_size_handler);
 
         if ( ret ) {
                 std::string msg("netsnmp_register_scalar failed.");
-                Logger::putLogFatal(LOG_CAT_L7VSD_SNMPAGENT, 4, msg, __FILE__, __LINE__);
+                Logger::putLogError(LOG_CAT_L7VSD_SNMPAGENT, 68, msg, __FILE__, __LINE__);
                 //set error code
                 err.setter(true, msg);
                 return;
         }
 
-        ret = netsnmp_register_scalar(netsnmp_create_handler_registration
-                                      ("l7vsReplicationMode",
-                                       handle_get_rep_state,
-                                       l7vsReplicationMode_oid,
-                                       OID_LENGTH(l7vsReplicationMode_oid),
-                                       HANDLER_CAN_RONLY));
+        replication_handler = netsnmp_create_handler_registration
+                              ("l7vsReplicationMode",
+                               handle_get_rep_state,
+                               l7vsReplicationMode_oid,
+                               OID_LENGTH(l7vsReplicationMode_oid),
+                               HANDLER_CAN_RONLY);
+
+        ret = netsnmp_register_scalar(replication_handler);
 
         if (ret) {
                 std::string msg("netsnmp_register_scalar failed.");
-                Logger::putLogFatal(LOG_CAT_L7VSD_SNMPAGENT, 5, msg, __FILE__, __LINE__);
+                Logger::putLogError(LOG_CAT_L7VSD_SNMPAGENT, 69, msg, __FILE__, __LINE__);
                 //set error code
                 err.setter(true, msg);
                 return;
         }
 
+
+        /*
+         * registering the table with the master agent
+         */
         /*
-         * here we initialize all the tables we're planning on supporting
+         * note: if you don't need a subhandler to deal with any aspects
+         * of the request, change handle_get_vstable to "NULL"
          */
-        initialize_virtual_service_table(err);
+        if (vs_table) {
+                vs_table_handler = netsnmp_create_handler_registration
+                                   ("l7vsVsTable", handle_get_vstable,
+                                    l7vsVsTable_oid,
+                                    OID_LENGTH(l7vsVsTable_oid),
+                                    HANDLER_CAN_RWRITE);
+                int ret = netsnmp_register_table_data_set(vs_table_handler, vs_table, NULL);
 
-        if (ret) return;
+                if (ret) {
+                        std::string msg("netsnmp create l7vsVsTable data set failed.");
+                        Logger::putLogError(LOG_CAT_L7VSD_SNMPAGENT, 24, msg, __FILE__, __LINE__);
+                        //set error code
+                        err.setter(true, msg);
+                        return;
+                }
+        }
 
-        initialize_real_server_table(err);
+        /*
+         * registering the table with the master agent
+         */
+        /*
+         * note: if you don't need a subhandler to deal with any aspects
+         * of the request, change handle_get_rstable to "NULL"
+         */
+        if (rs_table) {
+                rs_table_handler = netsnmp_create_handler_registration
+                                   ("l7vsRsTable", handle_get_rstable,
+                                    l7vsRsTable_oid,
+                                    OID_LENGTH(l7vsRsTable_oid),
+                                    HANDLER_CAN_RWRITE);
+                int ret = netsnmp_register_table_data_set(rs_table_handler, rs_table, NULL);
 
-        if (ret) return;
+                if (ret) {
+                        std::string msg("netsnmp create l7vsRsTable data set failed.");
+                        Logger::putLogError(LOG_CAT_L7VSD_SNMPAGENT, 26, msg, __FILE__, __LINE__);
+                        //set error code
+                        err.setter(true, msg);
+                        return;
+                }
+        }
 }
 
 /*!
@@ -117,7 +164,7 @@ handle_get_vstable_count(netsnmp_mib_handler *handler,
         switch (reqinfo->mode) {
 
         case MODE_GET: {
-                //get virtual service size
+                //get virtual serveice size
                 int table_size = l7vs::mibdata::get_instance().get_vs_table_size();
 
                 //set value
@@ -223,8 +270,6 @@ initialize_virtual_service_table(error_code &err)
 {
         Logger    logger(LOG_CAT_L7VSD_SNMPAGENT, 79, "snmpfunc::initialize_virtual_service_table", __FILE__, __LINE__);
 
-        oid       l7vsVsTable_oid[] = { 1, 3, 6, 1, 4, 1, 32132, 1, 1, 1, 2 };
-
         /*
          * create the table structure itself
          */
@@ -310,26 +355,6 @@ initialize_virtual_service_table(error_code &err)
                                                 COLUMN_L7VSVSRSNUMBER,
                                                 ASN_INTEGER, 0, NULL, 0, 0);
 
-        /*
-         * registering the table with the master agent
-         */
-        /*
-         * note: if you don't need a subhandler to deal with any aspects
-         * of the request, change handle_get_vstable to "NULL"
-         */
-        int ret = netsnmp_register_table_data_set(netsnmp_create_handler_registration
-                        ("l7vsVsTable", handle_get_vstable,
-                         l7vsVsTable_oid,
-                         OID_LENGTH(l7vsVsTable_oid),
-                         HANDLER_CAN_RWRITE), vs_table, NULL);
-
-        if (ret) {
-                std::string msg("netsnmp create l7vsVsTable data set failed.");
-                Logger::putLogError(LOG_CAT_L7VSD_SNMPAGENT, 24, msg, __FILE__, __LINE__);
-                //set error code
-                err.setter(true, msg);
-                return;
-        }
 }
 
 /*!
@@ -343,8 +368,6 @@ initialize_real_server_table(error_code &err)
 {
         Logger    logger(LOG_CAT_L7VSD_SNMPAGENT, 80, "snmpfunc::initialize_real_server_table", __FILE__, __LINE__);
 
-        oid       l7vsRsTable_oid[] = { 1, 3, 6, 1, 4, 1, 32132, 1, 1, 1, 3 };
-
         /*
          * create the table structure itself
          */
@@ -394,28 +417,38 @@ initialize_real_server_table(error_code &err)
                                                 ASN_INTEGER, 0, NULL, 0,
                                                 COLUMN_L7VSRSINACTIVECONN,
                                                 ASN_INTEGER, 0, NULL, 0, 0);
+}
 
-        /*
-         * registering the table with the master agent
-         */
-        /*
-         * note: if you don't need a subhandler to deal with any aspects
-         * of the request, change handle_get_rstable to "NULL"
-         */
-        int ret = netsnmp_register_table_data_set(netsnmp_create_handler_registration
-                        ("l7vsRsTable", handle_get_rstable,
-                         l7vsRsTable_oid,
-                         OID_LENGTH(l7vsRsTable_oid),
-                         HANDLER_CAN_RWRITE), rs_table, NULL);
+/*!
+ * unregister snmp handler.
+ *
+ */
+void unregister_handler()
+{
+        // unregister snmp handler
+        if (rs_table_handler && vs_table_handler && vs_table_size_handler && replication_handler) {
+                //fix memory leak for function netsnmp_register_table_data_set() start
+                //get table registration information
+                netsnmp_table_registration_info *rs_table_registration_info = netsnmp_find_table_registration_info(rs_table_handler);
+                netsnmp_table_registration_info *vs_table_registration_info = netsnmp_find_table_registration_info(vs_table_handler);
+
+                if (rs_table_registration_info) {
+                        SNMP_FREE(rs_table_registration_info->indexes);
+                        SNMP_FREE(rs_table_registration_info->valid_columns);
+                        SNMP_FREE(rs_table_registration_info);
+                }
+                if (vs_table_registration_info) {
+                        SNMP_FREE(vs_table_registration_info->indexes);
+                        SNMP_FREE(vs_table_registration_info->valid_columns);
+                        SNMP_FREE(vs_table_registration_info);
+                }
+                //fix memory leak for function netsnmp_register_table_data_set() end
 
-        if (ret) {
-                std::string msg("netsnmp create l7vsRsTable data set failed.");
-                Logger::putLogError(LOG_CAT_L7VSD_SNMPAGENT, 26, msg, __FILE__, __LINE__);
-                //set error code
-                err.setter(true, msg);
-                return;
+                netsnmp_unregister_handler(rs_table_handler);
+                netsnmp_unregister_handler(vs_table_handler);
+                netsnmp_unregister_handler(vs_table_size_handler);
+                netsnmp_unregister_handler(replication_handler);
         }
-
 }
 
 /*!
@@ -446,7 +479,7 @@ handle_get_vstable(netsnmp_mib_handler *handler,
         }
         break;
         case MODE_GETNEXT:
-        break;
+                break;
         default:
                 /*
                  * we should never get here, so this is a really bad error
@@ -489,7 +522,7 @@ handle_get_rstable(netsnmp_mib_handler *handler,
         }
         break;
         case MODE_GETNEXT:
-        break;
+                break;
         default:
                 /*
                  * we should never get here, so this is a really bad error