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_api.h"
30 #include "client/client.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",
231 fprintf(stderr, "Failed to get event %s status, result=%d\n",
236 if (script_list == NULL) {
237 if (state == CTDB_EVENT_LAST_RUN) {
238 printf("Event %s has never run\n", argv[0]);
239 } else if (state == CTDB_EVENT_LAST_PASS) {
240 printf("Event %s has never passed\n", argv[0]);
241 } else if (state == CTDB_EVENT_LAST_FAIL) {
242 printf("Event %s has never failed\n", argv[0]);
245 for (i=0; i<script_list->num_scripts; i++) {
246 print_status_one(&script_list->script[i]);
248 talloc_free(script_list);
254 static int command_script_list(TALLOC_CTX *mem_ctx, struct tool_context *tctx,
255 int argc, const char **argv)
257 struct tevent_req *req;
258 struct ctdb_script_list *script_list = NULL;
266 req = ctdb_event_script_list_send(mem_ctx, tctx->ev, tctx->eclient);
271 tevent_req_poll(req, tctx->ev);
273 status = ctdb_event_script_list_recv(req, &ret, &result,
274 mem_ctx, &script_list);
277 fprintf(stderr, "Failed to get script list, ret=%d\n", ret);
282 fprintf(stderr, "Failed to get script list, result=%d\n",
287 if (script_list == NULL || script_list->num_scripts == 0) {
288 printf("No event scripts found\n");
290 for (i=0; i<script_list->num_scripts; i++) {
291 struct ctdb_script *s;
293 s = &script_list->script[i];
295 if (s->status == -ENOEXEC) {
296 printf("%-20s DISABLED\n", s->name);
298 printf("%-20s\n", s->name);
301 talloc_free(script_list);
307 static int command_script_enable(TALLOC_CTX *mem_ctx,
308 struct tool_context *tctx,
309 int argc, const char **argv)
311 struct tevent_req *req;
319 req = ctdb_event_script_enable_send(mem_ctx, tctx->ev, tctx->eclient,
325 tevent_req_poll(req, tctx->ev);
327 status = ctdb_event_script_enable_recv(req, &ret, &result);
330 fprintf(stderr, "Failed to enable script %s, ret=%d\n",
335 if (result == -ENOENT) {
336 fprintf(stderr, "Script %s does not exist\n", argv[0]);
337 } else if (result == -EINVAL) {
338 fprintf(stderr, "Script name %s is invalid\n", argv[0]);
339 } else if (result != 0) {
340 fprintf(stderr, "Failed to enable script %s, result=%d\n",
344 ret = (result < 0) ? -result : result;
348 static int command_script_disable(TALLOC_CTX *mem_ctx,
349 struct tool_context *tctx,
350 int argc, const char **argv)
352 struct tevent_req *req;
360 req = ctdb_event_script_disable_send(mem_ctx, tctx->ev, tctx->eclient,
366 tevent_req_poll(req, tctx->ev);
368 status = ctdb_event_script_disable_recv(req, &ret, &result);
371 fprintf(stderr, "Failed to disable script %s, ret=%d\n",
376 if (result == -ENOENT) {
377 fprintf(stderr, "Script %s does not exist\n", argv[0]);
378 } else if (result == -EINVAL) {
379 fprintf(stderr, "Script name %s is invalid\n", argv[0]);
380 } else if (result != 0) {
381 fprintf(stderr, "Failed to disable script %s, result=%d\n",
385 ret = (result < 0) ? -result : result;
389 static const struct ctdb_event_cmd {
393 int (*fn)(TALLOC_CTX *, struct tool_context *, int, const char **);
396 } ctdb_event_commands[] = {
397 { "run", "run", NULL, command_run,
398 "Run an event", "<event> <timeout> <args>" },
399 { "status", "status", NULL, command_status,
400 "Get last status of an event",
401 "[<event>] [lastrun|lastpass|lastfail]" },
402 { "script list", "script", "list", command_script_list,
403 "Get list of event scripts", NULL },
404 { "script enable", "script", "enable", command_script_enable,
405 "Enable an event script", "<script>" },
406 { "script disable", "script", "disable", command_script_disable,
407 "Disable an event script", "<script>" },
410 static void usage(void)
412 const struct ctdb_event_cmd *cmd;
415 printf("Usage: ctdb_event <command> <args>\n");
416 printf("Commands:\n");
417 for (i=0; i<ARRAY_SIZE(ctdb_event_commands); i++) {
418 cmd = &ctdb_event_commands[i];
420 printf(" %-15s %-37s %s\n",
421 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
427 static const struct ctdb_event_cmd *match_command(const char *str1,
430 const struct ctdb_event_cmd *cmd;
434 for (i=0; i<ARRAY_SIZE(ctdb_event_commands); i++) {
435 cmd = &ctdb_event_commands[i];
436 if (strlen(str1) == strlen(cmd->str1) &&
437 strcmp(str1, cmd->str1) == 0) {
440 if (cmd->str2 != NULL) {
444 if (strcmp(str2, cmd->str2) == 0) {
460 static int process_command(const char *sockpath,
461 const struct ctdb_event_cmd *cmd,
462 int argc, const char **argv)
465 struct tool_context *tctx;
468 tmp_ctx = talloc_new(NULL);
469 if (tmp_ctx == NULL) {
470 fprintf(stderr, "Memory allocation error\n");
474 tctx = talloc_zero(tmp_ctx, struct tool_context);
476 fprintf(stderr, "Memory allocation error\n");
480 tctx->ev = tevent_context_init(tctx);
481 if (tctx->ev == NULL) {
482 fprintf(stderr, "Failed to initialize tevent\n");
486 ret = ctdb_event_init(tmp_ctx, tctx->ev, sockpath, &tctx->eclient);
488 fprintf(stderr, "ctdb_event_init() failed, ret=%d\n", ret);
492 if (cmd->str2 == NULL) {
493 ret = cmd->fn(tmp_ctx, tctx, argc-1, &argv[1]);
495 ret = cmd->fn(tmp_ctx, tctx, argc-2, &argv[2]);
498 talloc_free(tmp_ctx);
502 TALLOC_FREE(tmp_ctx);
506 int main(int argc, const char **argv)
508 const struct ctdb_event_cmd *cmd;
509 const char *eventd_socket;
516 eventd_socket = argv[1];
518 cmd = match_command(argv[2], argv[3]);
520 fprintf(stderr, "Unknown command '%s %s'", argv[2], argv[3]);
525 setup_logging("ctdb_event", DEBUG_STDERR);
526 t = getenv("CTDB_DEBUGLEVEL");
527 if (t == NULL || ! debug_level_parse(t, &DEBUGLEVEL)) {
528 DEBUGLEVEL = DEBUG_ERR;
531 return process_command(eventd_socket, cmd, argc-2, &argv[2]);