OSDN Git Service

Add SNMP code
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / snmpagent_impl.h
1 /*!
2  * @file  snmpagent_impl.h
3  * @brief snmpagent implement module.
4  *
5  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6  * Copyright (C) 2010  NTT COMWARE Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  **********************************************************************/
24 #ifndef __SNMPAGENT_IMPL_H__
25 #define __SNMPAGENT_IMPL_H__
26
27 #include "l7vsd.h"
28 #include "snmp_info.h"
29 #include <boost/thread.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <map>
32 #include <list>
33 #include <time.h>
34 #include <deque>
35 #include "error_code.h"
36 #include "boost/date_time/posix_time/posix_time.hpp"
37
38 namespace l7vs
39 {
40
41 //! @class    snmpagent_impl
42 //! @brief    this class is snmpagent implement class.
43         class snmpagent_impl : private boost::noncopyable
44         {
45
46         public:
47
48                 //! trap function type
49                 typedef int trapfunc(const std::string&);
50
51                 //!trap function map type
52                 typedef std::map<trapmessage::TRAP_TYPE_TAG, trapfunc*> trapfunc_type;
53
54                 //!string log level map type
55                 typedef std::map<std::string, LOG_LEVEL_TAG> string_loglevel_map_type;
56         protected:
57                 bool                        initialized;
58                 bool                        start_flag;
59                 l7vsd                       *vsd;
60                 trapfunc_type               trapfunc_map;
61                 atomic<unsigned int>        trap_polling_interval;
62                 atomic<unsigned int>        trap_queue_max_size;
63                 atomic<unsigned int>        enabled;
64                 unsigned int                logtrap_enabled;
65                 LOG_LEVEL_TAG               logtrap_level;
66                 atomic<unsigned int>        interval;
67                 atomic<time_t>              start_date;
68                 atomic<time_t>              request_last_date;
69                 atomic<time_t>              trap_last_date;
70                 atomic<unsigned long long>  snmp_get_requests;
71                 atomic<unsigned long long>  snmp_set_requests;
72                 atomic<unsigned long long>  snmp_trap_count;
73                 std::deque<trapmessage>     trap_msg_queue;
74                 boost::condition            trap_msg_queue_condition;
75                 boost::mutex                trap_msg_queue_condition_mutex;
76                 string_loglevel_map_type    string_loglevel_map;
77                 boost::posix_time::ptime    mib_collect_last_time;
78                 boost::thread               trap_thread;
79                 boost::thread               process_mib_thread;
80
81                 snmpagent_impl() {};
82                 ~snmpagent_impl() {};
83                 snmpagent_impl(const snmpagent_impl&);
84                 snmpagent_impl& operator=(const snmpagent_impl &);
85
86                 /*!
87                  * process mib get item thread function.
88                  *
89                  * @retrun  void
90                  */
91                 void    process_mib_run();
92
93                 /*!
94                  * process trap thread function.
95                  *
96                  * @retrun  void
97                  */
98                 void    trap_run();
99
100                 /*!
101                  * check mib data timeout.
102                  *
103                  * @retrun  void
104                  */
105                 bool    check_timeout();
106
107                 /*!
108                  * load configure file setting.
109                  *
110                  * @retrun  void
111                  */
112                 void    load_config();
113
114         public:
115
116                 /*!
117                  * create and return a snmpagent_impl instance.
118                  *
119                  * @retrun  snmpagent_impl & snmpagent_impl' instance
120                  */
121                 static  snmpagent_impl &get_instance();
122
123                 /*!
124                  * snmpagent_impl class initialize.
125                  *
126                  * @param   error_code & error code
127                  * @retrun  void
128                  */
129                 void    init(error_code &);
130
131                 /*!
132                  *  push the trap message into trap message queue.
133                  *
134                  * @param[in]      in_trapmessage is trap message
135                  * @param[in/out]  err is error code
136                  * @retrun         void
137                  */
138                 void    push_trapmessage(trapmessage &in_trapmessage, error_code& err);
139
140                 /*!
141                  * snmpagent function start.
142                  *
143                  * @param[in/out]  error_code is error code
144                  * @retrun  void
145                  */
146                 void    start(error_code&);
147
148                 /*!
149                  * snmpagent function stop.
150                  *
151                  * @retrun  void
152                  */
153                 void    stop();
154
155                 /*!
156                  *  set snmp function enable
157                  *
158                  * @retrun         void
159                  */
160                 void    enable();
161
162                 /*!
163                  *  set snmp function disable
164                  *
165                  * @retrun         void
166                  */
167                 void    disable();
168
169                 /*!
170                  * set log trap enable
171                  * @retrun  void
172                  */
173                 void    logtrap_enable();
174
175                 /*!
176                  * set log trap disable
177                  * @retrun  void
178                  */
179                 void    logtrap_disable();
180
181                 /*!
182                  *  set log trap level.
183                  *
184                  * @param[in]      in_level is log trap level
185                  * @retrun         void
186                  */
187                 void    set_logtrap_level(const LOG_LEVEL_TAG in_level);
188
189                 /*!
190                  *  set mib cache update interval.
191                  *
192                  * @param[in]      in_interval is cache update interval
193                  * @retrun         void
194                  */
195                 void    set_interval(const unsigned int in_interval);
196
197                 /*!
198                  *  refresh all the statistics.
199                  *
200                  * @return         void
201                  */
202                 void    refresh_all_statistics();
203
204                 /*!
205                  *  refresh a special virtual service's statistics.
206                  *
207                  * @param[in]      vs_endpoint is virtual service's endpoint
208                  * @param[in]      protocol is virtual service's protocol
209                  * @return         void
210                  */
211                 void    refresh_statistics(const boost::asio::ip::tcp::endpoint &vs_endpoint, const std::string &protocol);
212
213                 /*!
214                  *  set vsd infomation.
215                  *
216                  * @param[in]      vsd is vsd pointer.
217                  * @retrun         void
218                  */
219                 void    set_vsd_info(l7vsd *vsd);
220
221                 /*!
222                  *  get snmp infomation.
223                  *
224                  * @retrun         snmp_info is snmp infomation.
225                  */
226                 snmp_info get_snmp_info();
227
228                 /*!
229                  *  collect mib data.
230                  *
231                  * @param[in]      error_code is err code.
232                  * @retrun         void
233                  */
234                 void    collect_mibdata(error_code &);
235
236                 /*!
237                  *  finalize.
238                  *
239                  * @retrun         void
240                  */
241                 void    finalize();
242
243                 /*!
244                  *  increment get request count.
245                  *
246                  * @retrun         void
247                  */
248                 void    increment_getrequest_count();
249
250                 /*!
251                  *  increment set request count.
252                  *
253                  * @retrun         void
254                  */
255                 void    increment_setrequest_count();
256         };
257
258 }
259
260 #endif //__SNMPAGENT_IMPL_H__
261