OSDN Git Service

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