main.cpp 4.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

17
#include <ndb_global.h>
18
#include <ndb_opts.h>
19

20 21 22 23 24 25 26 27 28 29 30
// copied from mysql.cc to get readline
extern "C" {
#if defined( __WIN__) || defined(OS2)
#include <conio.h>
#elif !defined(__NETWARE__)
#include <readline/readline.h>
extern "C" int add_history(const char *command); /* From readline directory */
#define HAVE_READLINE
#endif
}

31 32
#include <NdbMain.h>
#include <NdbHost.h>
33 34
#include <BaseString.hpp>
#include <NdbOut.hpp>
35
#include <mgmapi.h>
36
#include <ndb_version.h>
37

38
#include "ndb_mgmclient.hpp"
39

40
const char *progname = "ndb_mgm";
41 42


43
static Ndb_mgmclient* com;
44 45 46

extern "C"
void 
47 48 49
handler(int sig)
{
  DBUG_ENTER("handler");
50 51 52 53 54 55 56 57 58
  switch(sig){
  case SIGPIPE:
    /**
     * Will happen when connection to mgmsrv is broken
     * Reset connected flag
     */
    com->disconnect();    
    break;
  }
59
  DBUG_VOID_RETURN;
60 61
}

62 63
NDB_STD_OPTS_VARS;

64
static const char default_prompt[]= "ndb_mgm> ";
65
static unsigned _try_reconnect;
66
static const char *prompt= default_prompt;
67
static char *opt_execute_str= 0;
68 69 70 71

static struct my_option my_long_options[] =
{
  NDB_STD_OPTS("ndb_mgm"),
72 73 74 75
  { "execute", 'e',
    "execute command and exit", 
    (gptr*) &opt_execute_str, (gptr*) &opt_execute_str, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
76
  { "try-reconnect", 't',
77
    "Specify number of tries for connecting to ndb_mgmd (0 = infinite)", 
78
    (gptr*) &_try_reconnect, (gptr*) &_try_reconnect, 0,
79
    GET_UINT, REQUIRED_ARG, 3, 0, 0, 0, 0, 0 },
80 81 82 83 84 85 86 87 88
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
static void short_usage_sub(void)
{
  printf("Usage: %s [OPTIONS] [hostname [port]]\n", my_progname);
}
static void usage()
{
  short_usage_sub();
89
  ndb_std_print_version();
90 91 92 93 94 95 96
  my_print_help(my_long_options);
  my_print_variables(my_long_options);
}
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
	       char *argument)
{
97 98
  return ndb_std_get_one_option(optid, opt, argument ? argument :
				"d:t:O,/tmp/ndb_mgm.trace");
99 100
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114
static int 
read_and_execute(int _try_reconnect) 
{
  static char *line_read = (char *)NULL;

  /* If the buffer has already been allocated, return the memory
     to the free pool. */
  if (line_read)
  {
    free (line_read);
    line_read = (char *)NULL;
  }
#ifdef HAVE_READLINE
  /* Get a line from the user. */
115
  line_read = readline (prompt);    
116 117 118 119 120
  /* If the line has any text in it, save it on the history. */
  if (line_read && *line_read)
    add_history (line_read);
#else
  static char linebuffer[254];
121
  fputs(prompt, stdout);
122 123 124 125 126 127 128 129 130 131 132 133
  linebuffer[sizeof(linebuffer)-1]=0;
  line_read = fgets(linebuffer, sizeof(linebuffer)-1, stdin);
  if (line_read == linebuffer) {
    char *q=linebuffer;
    while (*q > 31) q++;
    *q=0;
    line_read= strdup(linebuffer);
  }
#endif
  return com->execute(line_read,_try_reconnect);
}

134 135
int main(int argc, char** argv){
  NDB_INIT(argv[0]);
136 137
  const char *_host = 0;
  int _port = 0;
138
  const char *load_default_groups[]= { "mysql_cluster","ndb_mgm",0 };
139

140 141 142 143
  load_defaults("my",load_default_groups,&argc,&argv);
  int ho_error;
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
    exit(ho_error);
144 145

  char buf[MAXHOSTNAMELEN+10];
146 147 148 149 150 151 152
  if(argc == 1) {
    BaseString::snprintf(buf, sizeof(buf), "%s",  argv[0]);
    opt_connect_str= buf;
  } else if (argc >= 2) {
    BaseString::snprintf(buf, sizeof(buf), "%s:%s",  argv[0], argv[1]);
    opt_connect_str= buf;
  }
153

154
  if (!isatty(0) || opt_execute_str)
155 156 157 158
  {
    prompt= 0;
  }

159
  signal(SIGPIPE, handler);
160
  com = new Ndb_mgmclient(opt_connect_str,1);
161 162 163 164 165 166 167 168 169 170
  int ret= 0;
  if (!opt_execute_str)
  {
    ndbout << "-- NDB Cluster -- Management Client --" << endl;
    while(read_and_execute(_try_reconnect));
  }
  else
  {
    com->execute(opt_execute_str,_try_reconnect, &ret);
  }
171
  delete com;
172 173

  ndb_end(opt_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);
174
  return ret;
175 176
}