2 CTDB event daemon utility code
4 Copyright (C) Amitay Isaacs 2018
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/filesys.h"
22 #include "system/time.h"
28 #include "lib/util/debug.h"
30 #include "common/cmdline.h"
31 #include "common/logging.h"
32 #include "common/path.h"
34 #include "event/event_protocol_api.h"
35 #include "event/event.h"
36 #include "event/event_tool.h"
38 struct event_tool_context {
39 struct cmdline_context *cmdline;
40 struct tevent_context *ev;
41 struct ctdb_event_context *eclient;
44 static int compact_args(TALLOC_CTX *mem_ctx,
58 arg_str = talloc_strdup(mem_ctx, argv[from]);
59 if (arg_str == NULL) {
63 for (i = from+1; i < argc; i++) {
64 arg_str = talloc_asprintf_append(arg_str, " %s", argv[i]);
65 if (arg_str == NULL) {
74 static int event_command_run(TALLOC_CTX *mem_ctx,
79 struct event_tool_context *ctx = talloc_get_type_abort(
80 private_data, struct event_tool_context);
81 struct tevent_req *req;
82 struct ctdb_event_request_run request_run;
83 const char *arg_str = NULL;
85 int timeout, ret = 0, result = 0;
89 cmdline_usage(ctx->cmdline, "run");
93 ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient);
95 D_ERR("Failed to initialize event client, ret=%d\n", ret);
99 timeout = atoi(argv[0]);
104 ret = compact_args(mem_ctx, argv, argc, 3, &arg_str);
106 D_ERR("Memory allocation error\n");
110 request_run.component = argv[1];
111 request_run.event = argv[2];
112 request_run.args = arg_str;
113 request_run.timeout = timeout;
114 request_run.flags = 0;
116 t = getenv("CTDB_TEST_MODE");
118 t = getenv("CTDB_EVENT_RUN_ALL");
120 request_run.flags = CTDB_EVENT_RUN_ALL;
124 req = ctdb_event_run_send(mem_ctx,
129 D_ERR("Memory allocation error\n");
133 tevent_req_poll(req, ctx->ev);
135 ok = ctdb_event_run_recv(req, &ret, &result);
137 D_ERR("Failed to run event %s in %s, ret=%d\n",
144 D_NOTICE("Command run finished with result=%d\n", result);
146 if (result == ENOENT) {
147 printf("Event dir for %s does not exist\n", argv[1]);
148 } else if (result == ETIME) {
149 printf("Event %s in %s timed out\n", argv[2], argv[1]);
150 } else if (result == ECANCELED) {
151 printf("Event %s in %s got cancelled\n", argv[2], argv[1]);
152 } else if (result == ENOEXEC) {
153 printf("Event %s in %s failed\n", argv[2], argv[1]);
154 } else if (result != 0) {
155 printf("Failed to run event %s in %s, result=%d\n",
161 ret = (result < 0) ? -result : result;
165 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
167 return (tv2->tv_sec - tv->tv_sec) +
168 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
171 static void print_status_one(struct ctdb_event_script *script)
173 if (script->result == -ETIME) {
174 printf("%-20s %-10s %s",
177 ctime(&script->begin.tv_sec));
178 } else if (script->result == -ENOEXEC) {
179 printf("%-20s %-10s\n", script->name, "DISABLED");
180 } else if (script->result < 0) {
181 printf("%-20s %-10s (%s)\n",
184 strerror(-script->result));
185 } else if (script->result == 0) {
186 printf("%-20s %-10s %.3lf %s",
189 timeval_delta(&script->end, &script->begin),
190 ctime(&script->begin.tv_sec));
192 printf("%-20s %-10s %.3lf %s",
195 timeval_delta(&script->end, &script->begin),
196 ctime(&script->begin.tv_sec));
199 if (script->result != 0 && script->result != -ENOEXEC) {
200 printf(" OUTPUT: %s\n",
201 script->output == NULL ? "" : script->output);
205 static void print_status(const char *component,
208 struct ctdb_event_reply_status *status)
213 if (result == ENOENT) {
214 printf("Event dir for %s does not exist\n", component);
215 } else if (result == EINVAL) {
216 printf("Event %s has never run in %s\n",
220 printf("Unknown error (%d) for event %s in %s\n",
228 for (i=0; i<status->script_list->num_scripts; i++) {
229 print_status_one(&status->script_list->script[i]);
233 static int event_command_status(TALLOC_CTX *mem_ctx,
238 struct event_tool_context *ctx = talloc_get_type_abort(
239 private_data, struct event_tool_context);
240 struct tevent_req *req;
241 struct ctdb_event_request_status request_status;
242 struct ctdb_event_reply_status *reply_status;
243 int ret = 0, result = 0;
247 cmdline_usage(ctx->cmdline, "run");
251 ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient);
253 D_ERR("Failed to initialize event client, ret=%d\n", ret);
257 request_status.component = argv[0];
258 request_status.event = argv[1];
260 req = ctdb_event_status_send(mem_ctx,
265 D_ERR("Memory allocation error\n");
269 tevent_req_poll(req, ctx->ev);
271 ok = ctdb_event_status_recv(req,
277 D_ERR("Failed to get status for event %s in %s, ret=%d\n",
284 D_NOTICE("Command status finished with result=%d\n", result);
286 print_status(argv[0], argv[1], result, reply_status);
288 if (reply_status == NULL) {
291 ret = reply_status->summary;
292 ret = (ret < 0) ? -ret : ret;
297 static int event_command_script(TALLOC_CTX *mem_ctx,
298 struct event_tool_context *ctx,
299 const char *component,
301 enum ctdb_event_script_action action)
303 struct tevent_req *req;
304 struct ctdb_event_request_script request_script;
305 int ret = 0, result = 0;
308 ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient);
310 D_ERR("Failed to initialize event client, ret=%d\n", ret);
314 request_script.component = component;
315 request_script.script = script;
316 request_script.action = action;
318 req = ctdb_event_script_send(mem_ctx,
323 D_ERR("Memory allocation error\n");
327 tevent_req_poll(req, ctx->ev);
329 ok = ctdb_event_script_recv(req, &ret, &result);
331 D_ERR("Failed to %s script, ret=%d\n",
332 (action == CTDB_EVENT_SCRIPT_DISABLE ?
339 D_NOTICE("Command script finished with result=%d\n", result);
341 if (result == EINVAL) {
342 printf("Script %s is invalid in %s\n", script, component);
343 } else if (result == ENOENT) {
344 printf("Script %s does not exist in %s\n", script, component);
350 static int event_command_script_enable(TALLOC_CTX *mem_ctx,
355 struct event_tool_context *ctx = talloc_get_type_abort(
356 private_data, struct event_tool_context);
358 char *script, *etc_script, *data_script;
362 cmdline_usage(ctx->cmdline, "script enable");
366 script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]);
367 if (script == NULL) {
371 etc_script = path_etcdir_append(mem_ctx, script);
372 if (etc_script == NULL) {
376 data_script = path_datadir_append(mem_ctx, script);
377 if (data_script == NULL) {
381 ret = lstat(etc_script, &statbuf);
383 if (S_ISLNK(statbuf.st_mode)) {
384 /* Link already exists */
386 } else if (S_ISREG(statbuf.st_mode)) {
387 return event_command_script(mem_ctx,
391 CTDB_EVENT_SCRIPT_ENABLE);
394 printf("Script %s is not a file or a link\n", etc_script);
397 if (errno == ENOENT) {
398 ret = stat(data_script, &statbuf);
400 printf("Script %s does not exist in %s\n",
405 ret = symlink(data_script, etc_script);
407 printf("Failed to create symlink %s\n",
415 printf("Script %s does not exist\n", etc_script);
420 static int event_command_script_disable(TALLOC_CTX *mem_ctx,
425 struct event_tool_context *ctx = talloc_get_type_abort(
426 private_data, struct event_tool_context);
428 char *script, *etc_script;
433 cmdline_usage(ctx->cmdline, "script disable");
437 script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]);
438 if (script == NULL) {
442 etc_script = path_etcdir_append(mem_ctx, script);
443 if (etc_script == NULL) {
447 ret = lstat(etc_script, &statbuf);
449 if (S_ISLNK(statbuf.st_mode)) {
451 ret = unlink(etc_script);
453 printf("Failed to remove symlink %s\n",
459 } else if (S_ISREG(statbuf.st_mode)) {
460 return event_command_script(mem_ctx,
464 CTDB_EVENT_SCRIPT_DISABLE);
467 printf("Script %s is not a file or a link\n", etc_script);
474 struct cmdline_command event_commands[] = {
475 { "run", event_command_run,
476 "Run an event", "<timeout> <component> <event> <args>" },
477 { "status", event_command_status,
478 "Get status of an event", "<component> <event>" },
479 { "script enable", event_command_script_enable,
480 "Enable an event script", "<component> <script>" },
481 { "script disable", event_command_script_disable,
482 "Disable an event script", "<component> <script>" },
486 int event_tool_init(TALLOC_CTX *mem_ctx,
488 struct poptOption *options,
492 struct event_tool_context **result)
494 struct event_tool_context *ctx;
497 ctx = talloc_zero(mem_ctx, struct event_tool_context);
499 D_ERR("Memory allocation error\n");
503 ret = cmdline_init(mem_ctx,
509 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
514 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
516 cmdline_usage(ctx->cmdline, NULL);
525 int event_tool_run(struct event_tool_context *ctx, int *result)
529 ctx->ev = tevent_context_init(ctx);
530 if (ctx->ev == NULL) {
531 D_ERR("Failed to initialize tevent\n");
535 ret = cmdline_run(ctx->cmdline, ctx, result);
539 #ifdef CTDB_EVENT_TOOL
547 struct poptOption event_options[] = {
548 { "debug", 'd', POPT_ARG_STRING, &event_data.debug, 0,
549 "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
553 int main(int argc, const char **argv)
556 struct event_tool_context *ctx;
560 mem_ctx = talloc_new(NULL);
561 if (mem_ctx == NULL) {
562 fprintf(stderr, "Memory allocation error\n");
566 ret = event_tool_init(mem_ctx,
574 talloc_free(mem_ctx);
578 setup_logging("ctdb-event", DEBUG_STDERR);
579 ok = debug_level_parse(event_data.debug, &DEBUGLEVEL);
581 DEBUGLEVEL = DEBUG_ERR;
584 ret = event_tool_run(ctx, &result);
589 talloc_free(mem_ctx);
593 #endif /* CTDB_EVENT_TOOL */