OSDN Git Service

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