Fix to MXS-438: https://mariadb.atlassian.net/browse/MXS-438
Maxinfo now supports the shutdown command which shuts down a service, monitor or MaxScale itself and the restart command which restarts a stopped monitor or service.
This commit is contained in:
@ -57,6 +57,8 @@ static int maxinfo_pattern_match(char *pattern, char *str);
|
||||
static void exec_flush(DCB *dcb, MAXINFO_TREE *tree);
|
||||
static void exec_set(DCB *dcb, MAXINFO_TREE *tree);
|
||||
static void exec_clear(DCB *dcb, MAXINFO_TREE *tree);
|
||||
static void exec_shutdown(DCB *dcb, MAXINFO_TREE *tree);
|
||||
static void exec_restart(DCB *dcb, MAXINFO_TREE *tree);
|
||||
void maxinfo_send_ok(DCB *dcb);
|
||||
/**
|
||||
* Execute a parse tree and return the result set or runtime error
|
||||
@ -85,6 +87,12 @@ maxinfo_execute(DCB *dcb, MAXINFO_TREE *tree)
|
||||
case MAXOP_CLEAR:
|
||||
exec_clear(dcb, tree);
|
||||
break;
|
||||
case MAXOP_SHUTDOWN:
|
||||
exec_shutdown(dcb, tree);
|
||||
break;
|
||||
case MAXOP_RESTART:
|
||||
exec_restart(dcb, tree);
|
||||
break;
|
||||
|
||||
case MAXOP_TABLE:
|
||||
case MAXOP_COLUMNS:
|
||||
@ -503,6 +511,236 @@ exec_clear(DCB *dcb, MAXINFO_TREE *tree)
|
||||
skygw_log_write(LE, errmsg);
|
||||
}
|
||||
|
||||
extern void shutdown_server();
|
||||
|
||||
/**
|
||||
* MaxScale shutdown
|
||||
* @param dcb Client DCB
|
||||
* @param tree Parse tree
|
||||
*/
|
||||
void exec_shutdown_maxscale(DCB *dcb, MAXINFO_TREE *tree)
|
||||
{
|
||||
shutdown_server();
|
||||
maxinfo_send_ok(dcb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop a monitor
|
||||
* @param dcb Client DCB
|
||||
* @param tree Parse tree
|
||||
*/
|
||||
void exec_shutdown_monitor(DCB *dcb, MAXINFO_TREE *tree)
|
||||
{
|
||||
char errmsg[120];
|
||||
if (tree && tree->value)
|
||||
{
|
||||
MONITOR* monitor = monitor_find(tree->value);
|
||||
if (monitor)
|
||||
{
|
||||
monitorStop(monitor);
|
||||
maxinfo_send_ok(dcb);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (strlen(tree->value) > 80) // Prevent buffer overrun
|
||||
{
|
||||
tree->value[80] = 0;
|
||||
}
|
||||
sprintf(errmsg, "Invalid argument '%s'", tree->value);
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(errmsg, "Missing argument for 'SHUTDOWN MONITOR'");
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop a service
|
||||
* @param dcb Client DCB
|
||||
* @param tree Parse tree
|
||||
*/
|
||||
void exec_shutdown_service(DCB *dcb, MAXINFO_TREE *tree)
|
||||
{
|
||||
char errmsg[120];
|
||||
if (tree && tree->value)
|
||||
{
|
||||
SERVICE* service = service_find(tree->value);
|
||||
if (service)
|
||||
{
|
||||
serviceStop(service);
|
||||
maxinfo_send_ok(dcb);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (strlen(tree->value) > 80) // Prevent buffer overrun
|
||||
{
|
||||
tree->value[80] = 0;
|
||||
}
|
||||
sprintf(errmsg, "Invalid argument '%s'", tree->value);
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(errmsg, "Missing argument for 'SHUTDOWN SERVICE'");
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The table of shutdown commands that are supported
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
char *name;
|
||||
void (*func)(DCB *, MAXINFO_TREE *);
|
||||
} shutdown_commands[] = {
|
||||
{ "maxscale", exec_shutdown_maxscale},
|
||||
{ "monitor", exec_shutdown_monitor},
|
||||
{ "service", exec_shutdown_service},
|
||||
{ NULL, NULL}
|
||||
};
|
||||
|
||||
/**
|
||||
* Execute a shutdown command parse tree and return OK or runtime error
|
||||
*
|
||||
* @param dcb The DCB that connects to the client
|
||||
* @param tree The parse tree for the query
|
||||
*/
|
||||
static void
|
||||
exec_shutdown(DCB *dcb, MAXINFO_TREE *tree)
|
||||
{
|
||||
int i;
|
||||
char errmsg[120];
|
||||
|
||||
for (i = 0; shutdown_commands[i].name; i++)
|
||||
{
|
||||
if (strcasecmp(shutdown_commands[i].name, tree->value) == 0)
|
||||
{
|
||||
(*shutdown_commands[i].func)(dcb, tree->right);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (strlen(tree->value) > 80) // Prevent buffer overrun
|
||||
{
|
||||
tree->value[80] = 0;
|
||||
}
|
||||
sprintf(errmsg, "Unsupported shutdown command '%s'", tree->value);
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
skygw_log_write(LE, errmsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart a monitor
|
||||
* @param dcb Client DCB
|
||||
* @param tree Parse tree
|
||||
*/
|
||||
void exec_restart_monitor(DCB *dcb, MAXINFO_TREE *tree)
|
||||
{
|
||||
char errmsg[120];
|
||||
if (tree && tree->value)
|
||||
{
|
||||
MONITOR* monitor = monitor_find(tree->value);
|
||||
if (monitor)
|
||||
{
|
||||
monitorStart(monitor, NULL);
|
||||
maxinfo_send_ok(dcb);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (strlen(tree->value) > 80) // Prevent buffer overrun
|
||||
{
|
||||
tree->value[80] = 0;
|
||||
}
|
||||
sprintf(errmsg, "Invalid argument '%s'", tree->value);
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(errmsg, "Missing argument for 'RESTART MONITOR'");
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart a service
|
||||
* @param dcb Client DCB
|
||||
* @param tree Parse tree
|
||||
*/
|
||||
void exec_restart_service(DCB *dcb, MAXINFO_TREE *tree)
|
||||
{
|
||||
char errmsg[120];
|
||||
if (tree && tree->value)
|
||||
{
|
||||
SERVICE* service = service_find(tree->value);
|
||||
if (service)
|
||||
{
|
||||
serviceRestart(service);
|
||||
maxinfo_send_ok(dcb);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (strlen(tree->value) > 80) // Prevent buffer overrun
|
||||
{
|
||||
tree->value[80] = 0;
|
||||
}
|
||||
sprintf(errmsg, "Invalid argument '%s'", tree->value);
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(errmsg, "Missing argument for 'RESTART SERVICE'");
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The table of restart commands that are supported
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
char *name;
|
||||
void (*func)(DCB *, MAXINFO_TREE *);
|
||||
} restart_commands[] = {
|
||||
{ "monitor", exec_restart_monitor},
|
||||
{ "service", exec_restart_service},
|
||||
{ NULL, NULL}
|
||||
};
|
||||
|
||||
/**
|
||||
* Execute a restart command parse tree and return OK or runtime error
|
||||
*
|
||||
* @param dcb The DCB that connects to the client
|
||||
* @param tree The parse tree for the query
|
||||
*/
|
||||
static void
|
||||
exec_restart(DCB *dcb, MAXINFO_TREE *tree)
|
||||
{
|
||||
int i;
|
||||
char errmsg[120];
|
||||
|
||||
for (i = 0; restart_commands[i].name; i++)
|
||||
{
|
||||
if (strcasecmp(restart_commands[i].name, tree->value) == 0)
|
||||
{
|
||||
(*restart_commands[i].func)(dcb, tree->right);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (strlen(tree->value) > 80) // Prevent buffer overrun
|
||||
{
|
||||
tree->value[80] = 0;
|
||||
}
|
||||
sprintf(errmsg, "Unsupported restart command '%s'", tree->value);
|
||||
maxinfo_send_error(dcb, 0, errmsg);
|
||||
skygw_log_write(LE, errmsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current MaxScale version
|
||||
*
|
||||
|
Reference in New Issue
Block a user