2 CTDB event daemon control tool
4 Copyright (C) Amitay Isaacs 2016
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
22 #include "system/time.h"
27 #include "lib/util/debug.h"
29 #include "protocol/protocol_util.h"
30 #include "client/client_event.h"
31 #include "common/logging.h"
34 struct tevent_context *ev;
35 struct ctdb_event_context *eclient;
38 static void usage(void);
40 static char *compact_args(const char **argv, int argc, int from)
45 for (i = from; i < argc; i++) {
46 arg_str = talloc_asprintf_append(arg_str, "%s ", argv[i]);
47 if (arg_str == NULL) {
48 fprintf(stderr, "talloc_asprintf_append() failed\n");
56 static int command_run(TALLOC_CTX *mem_ctx, struct tool_context *tctx,
57 int argc, const char **argv)
59 struct tevent_req *req;
60 enum ctdb_event event;
71 event = ctdb_event_from_string(argv[0]);
72 if (event == CTDB_EVENT_MAX) {
73 fprintf(stderr, "Invalid event '%s'\n", argv[0]);
77 timeout = atoi(argv[1]);
84 case CTDB_EVENT_SETUP:
85 case CTDB_EVENT_STARTUP:
86 case CTDB_EVENT_MONITOR:
87 case CTDB_EVENT_IPREALLOCATED:
91 case CTDB_EVENT_TAKE_IP:
92 case CTDB_EVENT_RELEASE_IP:
96 case CTDB_EVENT_UPDATE_IP:
106 fprintf(stderr, "Cannot run event %s\n", argv[0]);
110 if (argc != 2 + eargs) {
111 fprintf(stderr, "Insufficient arguments for event %s\n",
116 arg_str = compact_args(argv, argc, 2);
118 req = ctdb_event_run_send(mem_ctx, tctx->ev, tctx->eclient,
119 event, timeout, arg_str);
124 tevent_req_poll(req, tctx->ev);
126 status = ctdb_event_run_recv(req, &ret, &result);
129 fprintf(stderr, "Failed to run event %s, ret=%d\n",
134 if (result == -ETIME) {
135 fprintf(stderr, "Event %s timed out\n", argv[0]);
136 } else if (result == -ECANCELED) {
137 fprintf(stderr, "Event %s got cancelled\n", argv[0]);
138 } else if (result != 0) {
139 fprintf(stderr, "Failed to run event %s, result=%d\n",
143 ret = (result < 0) ? -result : result;
147 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
149 return (tv2->tv_sec - tv->tv_sec) +
150 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
153 static void print_status_one(struct ctdb_script *script)
155 if (script->status == -ETIME) {
156 printf("%-20s %-10s %s", script->name, "TIMEDOUT",
157 ctime(&script->start.tv_sec));
158 } else if (script->status == -ENOEXEC) {
159 printf("%-20s %-10s\n", script->name, "DISABLED");
160 } else if (script->status < 0) {
161 printf("%-20s %-10s (%s)\n", script->name, "CANNOT RUN",
162 strerror(-script->status));
163 } else if (script->status == 0) {
164 printf("%-20s %-10s %.3lf %s", script->name, "OK",
165 timeval_delta(&script->finished, &script->start),
166 ctime(&script->start.tv_sec));
168 printf("%-20s %-10s %.3lf %s", script->name, "ERROR",
169 timeval_delta(&script->finished, &script->start),
170 ctime(&script->start.tv_sec));
173 if (script->status != 0 && script->status != -ENOEXEC) {
174 printf(" OUTPUT: %s\n", script->output);
178 static int command_status(TALLOC_CTX *mem_ctx, struct tool_context *tctx,
179 int argc, const char **argv)
181 struct tevent_req *req;
182 struct ctdb_script_list *script_list;
183 enum ctdb_event event;
185 int ret, result, event_status, i;
189 event = CTDB_EVENT_MONITOR;
191 event = ctdb_event_from_string(argv[0]);
192 if (event == CTDB_EVENT_MAX) {
193 fprintf(stderr, "Invalid event '%s'\n", argv[0]);
199 state = CTDB_EVENT_LAST_RUN;
201 if (strcmp(argv[1], "lastrun") == 0) {
202 state = CTDB_EVENT_LAST_RUN;
203 } else if (strcmp(argv[1], "lastpass") == 0) {
204 state = CTDB_EVENT_LAST_PASS;
205 } else if (strcmp(argv[1], "lastfail") == 0) {
206 state = CTDB_EVENT_LAST_FAIL;
208 fprintf(stderr, "Invalid state %s\n", argv[1]);
213 req = ctdb_event_status_send(mem_ctx, tctx->ev, tctx->eclient,
219 tevent_req_poll(req, tctx->ev);
221 status = ctdb_event_status_recv(req, &ret, &result, &event_status,
222 mem_ctx, &script_list);
225 fprintf(stderr, "Failed to get event %s status, ret=%d\n",
226 ctdb_event_to_string(event), ret);
231 fprintf(stderr, "Failed to get event %s status, result=%d\n",
232 ctdb_event_to_string(event), result);
236 if (script_list == NULL) {
237 if (state == CTDB_EVENT_LAST_RUN) {
238 printf("Event %s has never run\n",
239 ctdb_event_to_string(event));
240 } else if (state == CTDB_EVENT_LAST_PASS) {
241 printf("Event %s has never passed\n",
242 ctdb_event_to_string(event));
243 } else if (state == CTDB_EVENT_LAST_FAIL) {
244 printf("Event %s has never failed\n",
245 ctdb_event_to_string(event));
248 for (i=0; i<script_list->num_scripts; i++) {
249 print_status_one(&script_list->script[i]);
251 talloc_free(script_list);
254 ret = (event_status < 0) ? -event_status : event_status;
258 static int command_script_list(TALLOC_CTX *mem_ctx, struct tool_context *tctx,
259 int argc, const char **argv)
261 struct tevent_req *req;
262 struct ctdb_script_list *script_list = NULL;
270 req = ctdb_event_script_list_send(mem_ctx, tctx->ev, tctx->eclient);
275 tevent_req_poll(req, tctx->ev);
277 status = ctdb_event_script_list_recv(req, &ret, &result,
278 mem_ctx, &script_list);
281 fprintf(stderr, "Failed to get script list, ret=%d\n", ret);
286 fprintf(stderr, "Failed to get script list, result=%d\n",
291 if (script_list == NULL || script_list->num_scripts == 0) {
292 printf("No event scripts found\n");
294 for (i=0; i<script_list->num_scripts; i++) {
295 struct ctdb_script *s;
297 s = &script_list->script[i];
299 if (s->status == -ENOEXEC) {
300 printf("%-20s DISABLED\n", s->name);
302 printf("%-20s\n", s->name);
305 talloc_free(script_list);
311 static int command_script_enable(TALLOC_CTX *mem_ctx,
312 struct tool_context *tctx,
313 int argc, const char **argv)
315 struct tevent_req *req;
323 req = ctdb_event_script_enable_send(mem_ctx, tctx->ev, tctx->eclient,
329 tevent_req_poll(req, tctx->ev);
331 status = ctdb_event_script_enable_recv(req, &ret, &result);
334 fprintf(stderr, "Failed to enable script %s, ret=%d\n",
339 if (result == -ENOENT) {
340 fprintf(stderr, "Script %s does not exist\n", argv[0]);
341 } else if (result == -EINVAL) {
342 fprintf(stderr, "Script name %s is invalid\n", argv[0]);
343 } else if (result != 0) {
344 fprintf(stderr, "Failed to enable script %s, result=%d\n",
348 ret = (result < 0) ? -result : result;
352 static int command_script_disable(TALLOC_CTX *mem_ctx,
353 struct tool_context *tctx,
354 int argc, const char **argv)
356 struct tevent_req *req;
364 req = ctdb_event_script_disable_send(mem_ctx, tctx->ev, tctx->eclient,
370 tevent_req_poll(req, tctx->ev);
372 status = ctdb_event_script_disable_recv(req, &ret, &result);
375 fprintf(stderr, "Failed to disable script %s, ret=%d\n",
380 if (result == -ENOENT) {
381 fprintf(stderr, "Script %s does not exist\n", argv[0]);
382 } else if (result == -EINVAL) {
383 fprintf(stderr, "Script name %s is invalid\n", argv[0]);
384 } else if (result != 0) {
385 fprintf(stderr, "Failed to disable script %s, result=%d\n",
389 ret = (result < 0) ? -result : result;
393 static const struct ctdb_event_cmd {
397 int (*fn)(TALLOC_CTX *, struct tool_context *, int, const char **);
400 } ctdb_event_commands[] = {
401 { "run", "run", NULL, command_run,
402 "Run an event", "<event> <timeout> <args>" },
403 { "status", "status", NULL, command_status,
404 "Get last status of an event",
405 "[<event>] [lastrun|lastpass|lastfail]" },
406 { "script list", "script", "list", command_script_list,
407 "Get list of event scripts", NULL },
408 { "script enable", "script", "enable", command_script_enable,
409 "Enable an event script", "<script>" },
410 { "script disable", "script", "disable", command_script_disable,
411 "Disable an event script", "<script>" },
414 static void usage(void)
416 const struct ctdb_event_cmd *cmd;
419 printf("Usage: ctdb_event <command> <args>\n");
420 printf("Commands:\n");
421 for (i=0; i<ARRAY_SIZE(ctdb_event_commands); i++) {
422 cmd = &ctdb_event_commands[i];
424 printf(" %-15s %-37s %s\n",
425 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
431 static const struct ctdb_event_cmd *match_command(const char *str1,
434 const struct ctdb_event_cmd *cmd;
438 for (i=0; i<ARRAY_SIZE(ctdb_event_commands); i++) {
439 cmd = &ctdb_event_commands[i];
440 if (strlen(str1) == strlen(cmd->str1) &&
441 strcmp(str1, cmd->str1) == 0) {
444 if (cmd->str2 != NULL) {
448 if (strcmp(str2, cmd->str2) == 0) {
464 static int process_command(const char *sockpath,
465 const struct ctdb_event_cmd *cmd,
466 int argc, const char **argv)
469 struct tool_context *tctx;
472 tmp_ctx = talloc_new(NULL);
473 if (tmp_ctx == NULL) {
474 fprintf(stderr, "Memory allocation error\n");
478 tctx = talloc_zero(tmp_ctx, struct tool_context);
480 fprintf(stderr, "Memory allocation error\n");
484 tctx->ev = tevent_context_init(tctx);
485 if (tctx->ev == NULL) {
486 fprintf(stderr, "Failed to initialize tevent\n");
490 ret = ctdb_event_init(tmp_ctx, tctx->ev, sockpath, &tctx->eclient);
492 fprintf(stderr, "ctdb_event_init() failed, ret=%d\n", ret);
496 if (cmd->str2 == NULL) {
497 ret = cmd->fn(tmp_ctx, tctx, argc-1, &argv[1]);
499 ret = cmd->fn(tmp_ctx, tctx, argc-2, &argv[2]);
502 talloc_free(tmp_ctx);
506 TALLOC_FREE(tmp_ctx);
510 int main(int argc, const char **argv)
512 const struct ctdb_event_cmd *cmd;
513 const char *eventd_socket;
520 eventd_socket = argv[1];
522 cmd = match_command(argv[2], argv[3]);
524 fprintf(stderr, "Unknown command '%s %s'", argv[2], argv[3]);
529 setup_logging("ctdb_event", DEBUG_STDERR);
530 t = getenv("CTDB_DEBUGLEVEL");
531 if (t == NULL || ! debug_level_parse(t, &DEBUGLEVEL)) {
532 DEBUGLEVEL = DEBUG_ERR;
535 return process_command(eventd_socket, cmd, argc-2, &argv[2]);