OSDN Git Service

ae40e6f7b5ccc1256369f2879a480f5a5846add4
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / l7vsadm.h
1 /*!
2  *    @file    l7vsadm.h
3  *    @brief    l7vsd control application.
4  *
5  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6  * Copyright (C) 2009  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
25 #ifndef    L7VSADM_H
26 #define    L7VSADM_H
27 #include <map>
28 #include <boost/function.hpp>
29 #include <boost/asio.hpp>
30 #include <boost/regex.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <sstream>
33 #include "error_code.h"
34 #include "l7vs_command.h"
35 #include "tcp_socket_option.h"
36
37 #ifndef     L7VS_MODULE_PATH
38 #define L7VS_MODULE_PATH    "./"
39 #endif
40
41 #ifndef     COMMAND_BUFFER_SIZE
42 #define COMMAND_BUFFER_SIZE (65535)
43 #endif
44
45 #ifndef     L7VS_CONFIG_SOCK_PATH
46 #define L7VS_CONFIG_SOCK_PATH   "/var/run/l7vs"
47 #endif
48 #define L7VS_CONFIG_SOCKNAME    L7VS_CONFIG_SOCK_PATH   "/l7vs"
49
50 #define L7VSADM_DEFAULT_SCHEDULER       "rr"        //!< Default scheduler name
51 #define L7VSADM_DEFAULT_WAIT_INTERVAL   (1)         //!< Default wait interval
52 #define L7VSADM_DEFAULT_WAIT_COUNT      (10)        //!< Default wait count
53 #define L7VSADM_MAX_WAIT                (60)        //!< Max wait value
54 #define L7VS_MODNAME_LEN                (16)        //!< Module name length
55 #define L7VS_FILENAME_LEN               (256)       //!< File name length
56
57 namespace l7vs
58 {
59
60 //! endpoint string parse function
61 //! @param[in]      endpoint string
62 //! @param[out]     error_code
63 //! @return         endpoint
64 template < class T >
65 typename T::endpoint string_to_endpoint(std::string &str, error_code &err)
66 {
67         std::string::size_type pos = str.rfind(":");
68         std::string hostname = str.substr(0, pos);
69         std::string portname = str.substr(pos + 1, str.length());
70         if (0 == hostname.length()) {
71                 err.setter(1, "hostname is not specified:");
72                 return typename T::endpoint();
73         }
74         boost::regex re("\\d+");
75         if (boost::regex_match(portname.c_str(), re)) {
76                 try {
77                         boost::lexical_cast<unsigned short>(portname);
78                 } catch (boost::bad_lexical_cast &) {
79                         err.setter(1, "invalid port number:");
80                         return typename T::endpoint();
81                 }
82         }
83         //remove "[","]"
84         boost::algorithm::erase_first(hostname, "[");
85         boost::algorithm::erase_last(hostname, "]");
86
87         boost::asio::io_service         io_service;
88         typename T::resolver            resolver(io_service);
89         typename T::resolver::query     query(hostname, portname);
90         typename T::resolver::iterator  end;
91         boost::system::error_code       ec;
92         typename T::resolver::iterator  itr = resolver.resolve(query, ec);
93         if (ec) {
94                 std::stringstream buf;
95                 buf << "invalid endpoint:" << ec.message() << ":";
96                 err.setter(1, buf.str());
97                 return typename T::endpoint();
98         }
99         if (itr == end) return typename T::endpoint();
100         return *itr;
101 }
102
103 //! check endpoint function
104 //! @param[in]      endpoint
105 //! @param[in]      allow any_address or not
106 //! @param[out]     error_code
107 template < class T >
108 void    check_endpoint(typename T::endpoint &ep, bool allow_any_address, error_code &err)
109 {
110         if (!allow_any_address) {
111                 if ((ep.address().is_v4() &&
112                      ep.address().to_v4() == boost::asio::ip::address_v4::any()) ||
113                     (ep.address().is_v6() &&
114                      ep.address().to_v6() == boost::asio::ip::address_v6::any())) {
115                         err.setter(1, "invalid address (INADDR_ANY):");
116                         return;
117                 }
118         }
119         if (ep.port() == 0) {
120                 err.setter(1, "invalid port number (0):");
121                 return;
122         }
123 }
124
125 //! endpoint to string function
126 //! @param[in]      endpoint
127 //! @param[in]      return numeric expression or not
128 //! @return         endpoint string
129 template < class T >
130 std::string    endpoint_to_string(typename T::endpoint ep, bool numeric_flag)
131 {
132         std::stringstream    buf;
133         if (!numeric_flag) {
134                 boost::asio::io_service         io_service;
135                 typename T::resolver            resolver(io_service);
136                 boost::system::error_code       ec;
137                 typename T::resolver::iterator  itr = resolver.resolve(ep, ec);
138                 if (!ec) {
139                         if (itr->host_name() == "::")
140                                 buf << "[::]:" << itr->service_name();
141                         else
142                                 buf << itr->host_name() << ":" << itr->service_name();
143                         return buf.str();
144                 }
145         }
146         if (ep.address().is_v6())
147                 buf << "[" << ep.address().to_string() << "]:" << ep.port();
148         else
149                 buf << ep.address().to_string() << ":" << ep.port();
150         return buf.str();
151 }
152
153 class    l7vsadm
154 {
155 protected:
156         //! @class    file_lock
157         //! @brief    l7vsadm execute file lock class
158         class    file_lock
159         {
160         protected:
161                 int            fd;
162                 int            lock;
163         public:
164                 //! constructor
165                 file_lock(const std::string &path, error_code &err)
166                         :    fd(-1),
167                              lock(-1) {
168                         fd = open(path.c_str(), O_RDONLY);
169                         if (fd == -1) {
170                                 std::stringstream buf;
171                                 buf << boost::format("L7vsadm execute file open error. file:%s") % path;
172                                 err.setter(true, buf.str());
173                         }
174                 }
175
176                 //! destructor
177                 ~file_lock() {
178                         if (lock != -1) {
179                                 // fd unlock.
180                                 flock(fd, LOCK_UN);
181                         }
182                         if (fd != -1) {
183                                 // fd close.
184                                 close(fd);
185                         }
186                 }
187
188                 //! try lock function
189                 //! @return    lock succeed(true) / lock failed(false)
190                 bool    try_lock() {
191                         lock = flock(fd, LOCK_EX | LOCK_NB);
192                         if (lock == 0) {
193                                 // l7vsadm file lock OK.
194                                 return true;
195                         }
196                         return false;
197                 }
198         };
199
200         //
201         //    command parse functions.
202         //
203         //! list command parse function
204         bool    parse_list_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
205         //! virtualservice command parse function
206         bool    parse_vs_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
207         //! realserver_command parse function
208         bool    parse_rs_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
209         //! replication command parse function
210         bool    parse_replication_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
211         //! log command parse function
212         bool    parse_log_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
213         //! snmpagent command parse function
214         bool    parse_snmp_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
215         //! parameter parse function
216         bool    parse_parameter_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
217         //! help command parse func
218         bool    parse_help_func(l7vsadm_request::COMMAND_CODE_TAG, int, char*[]);
219
220         //
221         //    option parse functions.
222         //
223         //
224         // list option functions.
225         //! list numeric flag check.
226         bool    parse_opt_list_numeric_func(int &, int, char*[]);
227         //
228         // virtualservice option functions.
229         //! target option check
230         bool    parse_opt_vs_target_func(int &, int, char*[]);
231         //! module option check
232         bool    parse_opt_vs_module_func(int &, int, char*[]);
233         //! scheduler option check.
234         bool    parse_opt_vs_scheduler_func(int &, int, char*[]);
235         //! upper flag check
236         bool    parse_opt_vs_upper_func(int &, int, char*[]);
237         //! bypass(SorryServer) option check
238         bool    parse_opt_vs_bypass_func(int &, int, char*[]);
239         //! virtualservice option flag function
240         bool    parse_opt_vs_flag_func(int &, int, char*[]);
241         //! virtualservice option qosupstream function
242         bool    parse_opt_vs_qosup_func(int &, int, char*[]);
243         //! virtualservice option qosdownstream functipn
244         bool    parse_opt_vs_qosdown_func(int &, int, char*[]);
245         //! virtualservice option udp func.
246         bool    parse_opt_vs_udp_func(int &, int, char*[]);
247         //! virtualservice option ssl file function
248         bool    parse_opt_vs_ssl_file_func(int &, int, char*[]);
249         //! virtualservice option access log function
250         bool    parse_opt_vs_access_log_func(int &, int, char*[]);
251         //! virtualservice option access log logrotate function
252         bool    parse_opt_vs_access_log_logrotate_func(int &, int, char*[]);
253         //! virtualservice option socket option function
254         bool    parse_opt_vs_socket_func(int &, int, char*[]);
255         // realserver option function
256         //! realserver weight set
257         bool    parse_opt_rs_weight_func(int &, int, char*[]);
258         //! realserver target set
259         bool    parse_opt_rs_realserver_func(int &, int, char*[]);
260         // replication option function
261         //! replication switch function
262         bool    parse_opt_replication_switch_func(int &, int, char*[]);
263         //! replication start function
264         bool    parse_opt_replication_start_func(int &, int, char*[]);
265         //! replication stop function
266         bool    parse_opt_replication_stop_func(int &, int, char*[]);
267         //! replication force function
268         bool    parse_opt_replication_force_func(int &, int, char*[]);
269         //! replication dump function
270         bool    parse_opt_replication_dump_func(int &, int, char*[]);
271         // log option function
272         //! log category set function
273         bool    parse_opt_log_category_func(int &, int, char*[]);
274         //! log level set function
275         bool    parse_opt_log_level_func(int &, int, char*[]);
276         // snmp option function
277         //! snmp log category set function
278         bool    parse_opt_snmp_log_category_func(int &, int, char*[]);
279         //! snmp log level set function
280         bool    parse_opt_snmp_log_level_func(int &, int, char*[]);
281         // parameter option function
282         //! parameter reload component parsing
283         bool    parse_opt_parameter_reload_func(int &, int, char*[]);
284
285         //!    disp_list function
286         void    disp_list();
287         //!    disp_list_key function
288         void    disp_list_key();
289         //!    disp_list_verbose function
290         void    disp_list_verbose();
291
292         //! Get l7vsadm parameter data
293         void    set_parameter();
294
295         //! argument dump for debug
296         std::string    argument_debug_dump(int, char*[]);
297
298         //! command parse function object.type.
299         typedef    boost::function< bool (int, char*[]) >
300         parse_cmd_func_type;
301         //! command string - parse function object map type.
302         typedef    std::map< std::string, parse_cmd_func_type >
303         parse_cmd_map_type;
304         //! command function map dictionary.
305         parse_cmd_map_type    command_dic;
306
307         //! option parse function object type.
308         typedef    boost::function< bool (int &, int, char*[]) >
309         parse_opt_func_type;
310         //! option string - parse function object map type
311         typedef    std::map< std::string, parse_opt_func_type >
312         parse_opt_map_type;
313         //! list option function map dictionary.
314         parse_opt_map_type    list_option_dic;
315         //! virtualservice option function map dictionary
316         parse_opt_map_type    vs_option_dic;
317         //! realserver option function map dictionary
318         parse_opt_map_type    rs_option_dic;
319         //! replication option function map dictionary
320         parse_opt_map_type    replication_option_dic;
321         //! replication switch option function map dictionary
322         parse_opt_map_type    replication_switch_option_dic;
323         //! log option function map dictionary
324         parse_opt_map_type    log_option_dic;
325         //! snmp option_function map dictionary
326         parse_opt_map_type    snmp_option_dic;
327         //! parameter option function map dictionary
328         parse_opt_map_type    parameter_option_dic;
329
330         //! log category string -> log category enum convert map type.
331         typedef    std::map< std::string, LOG_CATEGORY_TAG >    string_logcategory_map_type;
332         //! log category string to log category enum dictionary.
333         string_logcategory_map_type    string_logcategory_dic;
334         //! snmp log category string to snmp log category enum dictionary.
335         string_logcategory_map_type    string_snmp_logcategory_dic;
336
337         //! log category enum -> log category string convert map type.
338         typedef    std::map< LOG_CATEGORY_TAG, std::string >    logcategory_string_map_type;
339         //! log category enum to log category string dictionary.
340         logcategory_string_map_type    logcategory_string_dic;
341         //! snmp log category enum to snmp log category string dictionary.
342         logcategory_string_map_type    snmp_logcategory_string_dic;
343
344         //! log level string -> log level enum convert map type
345         typedef    std::map< std::string, LOG_LEVEL_TAG >        string_loglevel_map_type;
346         //! log level string to log level enum dictionary
347         string_loglevel_map_type    string_loglevel_dic;
348
349         //! log level enum convert map -> log level string type
350         typedef    std::map< LOG_LEVEL_TAG, std::string >        loglevel_string_map_type;
351         //! log level enum to log level string dictionary
352         loglevel_string_map_type    loglevel_string_dic;
353
354         //! parameter category string -> parameter category enum convert map type
355         typedef    std::map< std::string, PARAMETER_COMPONENT_TAG >    string_parameter_map_type;
356         string_parameter_map_type    string_parameter_dic;
357         //! COMMAND_RESPONSE_CODE -> message convert map type
358         typedef    std::map< l7vsd_response::COMMAND_RESPONSE_CODE, std::string >    response_error_message_map_type;
359         response_error_message_map_type    response_error_message_dic;
360
361         //! disp result function object type.
362         typedef    boost::function< void () >
363         disp_result_func_type;
364         //! command - disp result function object map type
365         typedef    std::map< l7vsadm_request::COMMAND_CODE_TAG, disp_result_func_type >
366         disp_result_map_type;
367         //! disp result function map dictionary.
368         disp_result_map_type    disp_result_dic;
369
370         //! replication mode enum -> replication mode string convert map type
371         typedef    std::map< replication::REPLICATION_MODE_TAG, std::string >    replication_mode_string_map_type;
372         //! replication mode enum to replication mode string dictionary
373         replication_mode_string_map_type    replication_mode_string_dic;
374
375         //! usage function
376         std::string    usage();
377
378         //
379         // l7vsd request data
380         l7vsadm_request    request;                //!< send_request
381         l7vsd_response    response;                //!< recv_response
382
383         //
384         // l7vsadm using datas.
385         bool                    numeric_flag;    //!< numeric flag
386         boost::asio::io_service    io_service;        //!< io_service
387
388         error_code    l7vsadm_err;
389
390         //! Interval of l7vsadm command conflict check.
391         int command_wait_interval;
392         //! Number of times of l7vsadm command conflict check.
393         int command_wait_count;
394         //! Interval of connected check to l7vsd.
395         int connect_wait_interval;
396         //! Number of times of connected check to l7vsd.
397         int connect_wait_count;
398
399 public:
400         //! constractor
401         l7vsadm();
402
403         //! execute function
404         bool    execute(int, char*[]);
405 };
406
407 }    //namespace l7vs
408
409
410 #endif    //L7VSADM_H