64 #include <ndb_global.h>
68 #include <my_getopt.h>
69 #include <mysql_version.h>
73 #include "../src/mgmapi/mgmapi_configuration.hpp"
74 #include "../src/mgmsrv/ConfigInfo.hpp"
75 #include <NdbAutoPtr.hpp>
78 static int g_verbose = 0;
79 static int try_reconnect = 3;
81 static int g_nodes, g_connections, g_system, g_section;
82 static const char * g_query = 0;
84 static int g_nodeid = 0;
85 static const char * g_type = 0;
86 static const char * g_host = 0;
87 static const char * g_field_delimiter=
",";
88 static const char * g_row_delimiter=
" ";
89 static const char * g_config_file = 0;
90 static int g_mycnf = 0;
91 static int g_configinfo = 0;
93 static int g_config_from_node = 0;
95 const char *load_default_groups[]= {
"mysql_cluster",0 };
99 static struct my_option my_long_options[] =
101 NDB_STD_OPTS(
"ndb_config"),
102 {
"nodes", NDB_OPT_NOSHORT,
"Print nodes",
103 (uchar**) &g_nodes, (uchar**) &g_nodes,
104 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
105 {
"connections", NDB_OPT_NOSHORT,
"Print connections",
106 (uchar**) &g_connections, (uchar**) &g_connections,
107 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
108 {
"system", NDB_OPT_NOSHORT,
"Print system",
109 (uchar**) &g_system, (uchar**) &g_system,
110 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
111 {
"query",
'q',
"Query option(s)",
112 (uchar**) &g_query, (uchar**) &g_query,
113 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
114 {
"host", NDB_OPT_NOSHORT,
"Host",
115 (uchar**) &g_host, (uchar**) &g_host,
116 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
117 {
"type", NDB_OPT_NOSHORT,
"Type of node/connection",
118 (uchar**) &g_type, (uchar**) &g_type,
119 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
120 {
"id", NDB_OPT_NOSHORT,
"Nodeid",
121 (uchar**) &g_nodeid, (uchar**) &g_nodeid,
122 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
123 {
"nodeid", NDB_OPT_NOSHORT,
"Nodeid",
124 (uchar**) &g_nodeid, (uchar**) &g_nodeid,
125 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
126 {
"fields",
'f',
"Field separator",
127 (uchar**) &g_field_delimiter, (uchar**) &g_field_delimiter,
128 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
129 {
"rows",
'r',
"Row separator",
130 (uchar**) &g_row_delimiter, (uchar**) &g_row_delimiter,
131 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
132 {
"config-file", NDB_OPT_NOSHORT,
"Path to config.ini",
133 (uchar**) &g_config_file, (uchar**) &g_config_file,
134 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
135 {
"mycnf", NDB_OPT_NOSHORT,
"Read config from my.cnf",
136 (uchar**) &g_mycnf, (uchar**) &g_mycnf,
137 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
138 {
"configinfo", NDB_OPT_NOSHORT,
"Print configinfo",
139 (uchar**) &g_configinfo, (uchar**) &g_configinfo,
140 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
141 {
"xml", NDB_OPT_NOSHORT,
"Print configinfo in xml format",
142 (uchar**) &g_xml, (uchar**) &g_xml,
143 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
144 {
"config_from_node", NDB_OPT_NOSHORT,
"Use current config from node with given nodeid",
145 (uchar**) &g_config_from_node, (uchar**) &g_config_from_node,
146 0, GET_INT, REQUIRED_ARG, INT_MIN, INT_MIN, 0, 0, 0, 0},
147 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
150 static void short_usage_sub(
void)
152 ndb_short_usage_sub(NULL);
158 "This program will retreive config options for a ndb cluster\n";
160 ndb_usage(short_usage_sub, load_default_groups, my_long_options);
171 virtual int eval(
const Iter&);
178 virtual int eval(
const Iter&);
184 Apply(
int val) { m_key = val;}
186 virtual int apply(
const Iter&);
193 virtual int apply(
const Iter&);
199 virtual int apply(
const Iter&);
211 main(
int argc,
char** argv){
213 ndb_opt_set_usage_funcs(short_usage_sub, usage);
214 load_defaults(
"my",load_default_groups,&argc,&argv);
216 if ((ho_error=handle_options(&argc, &argv, my_long_options,
217 ndb_std_get_one_option)))
230 if ((g_nodes && g_connections) ||
231 (g_system && (g_nodes || g_connections)))
234 "Error: Only one of the section-options: --nodes, --connections, --system is allowed.\n");
243 if ((g_config_file && g_mycnf) ||
244 ((g_config_from_node != INT_MIN) && (g_config_file || g_mycnf)))
247 "Error: Config should be retrieved from only one of the following sources:\n");
249 "\tconfig stored at mgmd (default),\n");
251 "\tconfig stored at a data node (--config_from_node=<nodeid>), \n");
253 "\tmy.cnf(--mycnf=<my.cnf file>),\n");
255 "\tconfig.file (--config_file=<config file>).\n");
259 g_section = CFG_SECTION_NODE;
261 g_section = CFG_SECTION_CONNECTION;
263 g_section = CFG_SECTION_SYSTEM;
267 if (g_config_file || g_mycnf)
268 conf = load_configuration();
270 conf = fetch_configuration(g_config_from_node);
280 if(strcmp(g_row_delimiter,
"\\n") == 0)
281 g_row_delimiter =
"\n";
282 if(strcmp(g_field_delimiter,
"\\n") == 0)
283 g_field_delimiter =
"\n";
285 if(parse_query(select_list, argc, argv))
290 if(parse_where(where_clause, argc, argv))
295 Iter iter(* conf, g_section);
298 for(iter.first(); iter.valid(); iter.next())
300 if(eval(iter, where_clause))
303 printf(
"%s", g_row_delimiter);
305 apply(iter, select_list);
321 for(
unsigned i = 0;
i<list.size();
i++)
323 const char * str= list[
i].c_str();
324 if(g_section == CFG_SECTION_NODE)
326 if(strcasecmp(str,
"id") == 0 || strcasecmp(str,
"nodeid") == 0)
328 select.push_back(
new Apply(CFG_NODE_ID));
331 else if(strncasecmp(str,
"host", 4) == 0)
333 select.push_back(
new Apply(CFG_NODE_HOST));
336 else if(strcasecmp(str,
"type") == 0)
342 else if (g_section == CFG_SECTION_CONNECTION)
344 if(strcasecmp(str,
"type") == 0)
352 for(
int p = 0; p<ConfigInfo::m_NoOfParams; p++)
354 if(0)ndbout_c(
"%s %s",
357 if((g_section == CFG_SECTION_CONNECTION &&
362 (g_section == CFG_SECTION_NODE &&
367 (g_section == CFG_SECTION_SYSTEM))
379 fprintf(stderr,
"Unknown query option: %s\n", str);
396 tmp->m_key = CFG_NODE_HOST;
397 tmp->m_value.
assfmt(
"%s", g_host);
398 where.push_back(tmp);
403 m.m_key = CFG_TYPE_OF_SECTION;
405 where.push_back(
new Match(m));
410 m.m_key = CFG_NODE_ID;
411 m.m_value.
assfmt(
"%d", g_nodeid);
412 where.push_back(
new Match(m));
424 for(
unsigned i = 0;
i<where.size();
i++)
426 if(where[
i]->eval(iter) == 0)
437 for(
unsigned i = 0;
i<list.size();
i++)
439 list[
i]->apply(iter);
440 if(
i + 1 != list.size())
441 printf(
"%s", g_field_delimiter);
447 Match::eval(
const Iter& iter)
452 if (iter.get(m_key, &val32) == 0)
454 if(atoi(m_value.
c_str()) != (
int)val32)
457 else if(iter.get(m_key, &val64) == 0)
459 if(strtoll(m_value.
c_str(), (
char **)NULL, 10) != (
long long)val64)
462 else if(iter.get(m_key, &valc) == 0)
464 if(strcmp(m_value.
c_str(), valc) != 0)
475 HostMatch::eval(
const Iter& iter)
479 if(iter.get(m_key, &valc) == 0)
481 struct hostent *h1, *h2, copy1;
484 h1 = gethostbyname(m_value.
c_str());
491 memcpy(©1, h1,
sizeof(
struct hostent));
492 addr1 = (
char *)malloc(copy1.h_length);
494 memcpy(addr1, h1->h_addr, copy1.h_length);
496 h2 = gethostbyname(valc);
501 if (copy1.h_addrtype != h2->h_addrtype) {
505 if (copy1.h_length != h2->h_length)
510 return 0 == memcmp(addr1, h2->h_addr, copy1.h_length);
517 Apply::apply(
const Iter& iter)
522 if (iter.get(m_key, &val32) == 0)
526 else if(iter.get(m_key, &val64) == 0)
528 printf(
"%llu", val64);
530 else if(iter.get(m_key, &valc) == 0)
538 NodeTypeApply::apply(
const Iter& iter)
541 if (iter.get(CFG_TYPE_OF_SECTION, &val32) == 0)
549 ConnectionTypeApply::apply(
const Iter& iter)
552 if (iter.get(CFG_TYPE_OF_SECTION, &val32) == 0)
556 case CONNECTION_TYPE_TCP:
559 case CONNECTION_TYPE_SCI:
562 case CONNECTION_TYPE_SHM:
574 fetch_configuration(
int from_node)
579 fprintf(stderr,
"Cannot create handle to management server.\n");
587 fprintf(stderr,
"* %5d: %s\n",
597 fprintf(stderr,
"Connect failed");
598 fprintf(stderr,
" code: %d, msg: %s\n",
605 fprintf(stderr,
"Connected to %s:%d\n",
610 if (from_node == INT_MIN)
616 else if (from_node < 1)
618 fprintf(stderr,
"Invalid node number %d is given for --config_from_node.\n", from_node);
629 fprintf(stderr,
"Could not get configuration, ");
630 fprintf(stderr,
"error code: %d, error msg: %s\n",
636 fprintf(stderr,
"Fetched configuration\n");
646 #include "../src/mgmsrv/Config.hpp"
647 #include <EventLogger.hpp>
661 fprintf(stderr,
"Using config.ini : %s\n", g_config_file);
665 return conf->m_configValues;
670 fprintf(stderr,
"Using my.cnf\n");
674 return conf->m_configValues;