ctdb-daemon: Remove ctdb_event_helper
[samba.git] / ctdb / tools / ctdb_event.c
1 /*
2    CTDB event daemon control tool
3
4    Copyright (C) Amitay Isaacs  2016
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/time.h"
23
24 #include <talloc.h>
25 #include <tevent.h>
26
27 #include "lib/util/debug.h"
28
29 #include "protocol/protocol_api.h"
30 #include "client/client.h"
31 #include "common/logging.h"
32
33 struct tool_context {
34         struct tevent_context *ev;
35         struct ctdb_event_context *eclient;
36 };
37
38 static void usage(void);
39
40 static char *compact_args(const char **argv, int argc, int from)
41 {
42         char *arg_str = NULL;
43         int i;
44
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");
49                         exit(1);
50                 }
51         }
52
53         return arg_str;
54 }
55
56 static int command_run(TALLOC_CTX *mem_ctx, struct tool_context *tctx,
57                        int argc, const char **argv)
58 {
59         struct tevent_req *req;
60         enum ctdb_event event;
61         const char *arg_str;
62         int timeout;
63         int eargs;
64         int ret, result;
65         bool status;
66
67         if (argc < 2) {
68                 usage();
69         }
70
71         event = ctdb_event_from_string(argv[0]);
72         if (event == CTDB_EVENT_MAX) {
73                 fprintf(stderr, "Invalid event '%s'\n", argv[0]);
74                 return 1;
75         }
76
77         timeout = atoi(argv[1]);
78         if (timeout < 0) {
79                 timeout = 0;
80         }
81
82         switch (event) {
83                 case CTDB_EVENT_INIT:
84                 case CTDB_EVENT_SETUP:
85                 case CTDB_EVENT_STARTUP:
86                 case CTDB_EVENT_MONITOR:
87                 case CTDB_EVENT_IPREALLOCATED:
88                         eargs = 0;
89                         break;
90
91                 case CTDB_EVENT_TAKE_IP:
92                 case CTDB_EVENT_RELEASE_IP:
93                         eargs = 3;
94                         break;
95
96                 case CTDB_EVENT_UPDATE_IP:
97                         eargs = 4;
98                         break;
99
100                 default:
101                         eargs = -1;
102                         break;
103         }
104
105         if (eargs < 0) {
106                 fprintf(stderr, "Cannot run event %s\n", argv[0]);
107                 return 1;
108         }
109
110         if (argc != 2 + eargs) {
111                 fprintf(stderr, "Insufficient arguments for event %s\n",
112                         argv[0]);
113                 return 1;
114         }
115
116         arg_str = compact_args(argv, argc, 2);
117
118         req = ctdb_event_run_send(mem_ctx, tctx->ev, tctx->eclient,
119                                   event, timeout, arg_str);
120         if (req == NULL) {
121                 return ENOMEM;
122         }
123
124         tevent_req_poll(req, tctx->ev);
125
126         status = ctdb_event_run_recv(req, &ret, &result);
127         talloc_free(req);
128         if (! status) {
129                 fprintf(stderr, "Failed to run event %s, ret=%d\n",
130                         argv[0], ret);
131                 return ret;
132         }
133
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",
140                         argv[0], result);
141         }
142
143         ret = (result < 0) ? -result : result;
144         return ret;
145 }
146
147 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
148 {
149         return (tv2->tv_sec - tv->tv_sec) +
150                (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
151 }
152
153 static void print_status_one(struct ctdb_script *script)
154 {
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));
167         } else {
168                 printf("%-20s %-10s %.3lf %s", script->name, "ERROR",
169                        timeval_delta(&script->finished, &script->start),
170                        ctime(&script->start.tv_sec));
171         }
172
173         if (script->status != 0 && script->status != -ENOEXEC) {
174                 printf("  OUTPUT: %s\n", script->output);
175         }
176 }
177
178 static int command_status(TALLOC_CTX *mem_ctx, struct tool_context *tctx,
179                           int argc, const char **argv)
180 {
181         struct tevent_req *req;
182         struct ctdb_script_list *script_list;
183         enum ctdb_event event;
184         uint32_t state;
185         int ret, result, event_status, i;
186         bool status;
187
188         if (argc < 1) {
189                 event = CTDB_EVENT_MONITOR;
190         } else {
191                 event = ctdb_event_from_string(argv[0]);
192                 if (event == CTDB_EVENT_MAX) {
193                         fprintf(stderr, "Invalid event '%s'\n", argv[0]);
194                         return EINVAL;
195                 }
196         }
197
198         if (argc < 2) {
199                 state = CTDB_EVENT_LAST_RUN;
200         } else {
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;
207                 } else {
208                         fprintf(stderr, "Invalid state %s\n", argv[1]);
209                         return EINVAL;
210                 }
211         }
212
213         req = ctdb_event_status_send(mem_ctx, tctx->ev, tctx->eclient,
214                                      event, state);
215         if (req == NULL) {
216                 return ENOMEM;
217         }
218
219         tevent_req_poll(req, tctx->ev);
220
221         status = ctdb_event_status_recv(req, &ret, &result, &event_status,
222                                         mem_ctx, &script_list);
223         talloc_free(req);
224         if (! status) {
225                 fprintf(stderr, "Failed to get event %s status, ret=%d\n",
226                         argv[0], ret);
227                 return ret;
228         }
229
230         if (result != 0) {
231                 fprintf(stderr, "Failed to get event %s status, result=%d\n",
232                         argv[0], result);
233                 return result;
234         }
235
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]);
243                 }
244         } else {
245                 for (i=0; i<script_list->num_scripts; i++) {
246                         print_status_one(&script_list->script[i]);
247                 }
248                 talloc_free(script_list);
249         }
250
251         return event_status;
252 }
253
254 static int command_script_list(TALLOC_CTX *mem_ctx, struct tool_context *tctx,
255                                int argc, const char **argv)
256 {
257         struct tevent_req *req;
258         struct ctdb_script_list *script_list = NULL;
259         int ret, result, i;
260         bool status;
261
262         if (argc != 0) {
263                 usage();
264         }
265
266         req = ctdb_event_script_list_send(mem_ctx, tctx->ev, tctx->eclient);
267         if (req == NULL) {
268                 return ENOMEM;
269         }
270
271         tevent_req_poll(req, tctx->ev);
272
273         status = ctdb_event_script_list_recv(req, &ret, &result,
274                                              mem_ctx, &script_list);
275         talloc_free(req);
276         if (! status) {
277                 fprintf(stderr, "Failed to get script list, ret=%d\n", ret);
278                 return ret;
279         }
280
281         if (result != 0) {
282                 fprintf(stderr, "Failed to get script list, result=%d\n",
283                         result);
284                 return result;
285         }
286
287         if (script_list == NULL || script_list->num_scripts == 0) {
288                 printf("No event scripts found\n");
289         } else {
290                 for (i=0; i<script_list->num_scripts; i++) {
291                         struct ctdb_script *s;
292
293                         s = &script_list->script[i];
294
295                         if (s->status == -ENOEXEC) {
296                                 printf("%-20s DISABLED\n", s->name);
297                         } else {
298                                 printf("%-20s\n", s->name);
299                         }
300                 }
301                 talloc_free(script_list);
302         }
303
304         return 0;
305 }
306
307 static int command_script_enable(TALLOC_CTX *mem_ctx,
308                                  struct tool_context *tctx,
309                                  int argc, const char **argv)
310 {
311         struct tevent_req *req;
312         int ret, result;
313         bool status;
314
315         if (argc != 1) {
316                 usage();
317         }
318
319         req = ctdb_event_script_enable_send(mem_ctx, tctx->ev, tctx->eclient,
320                                             argv[0]);
321         if (req == NULL) {
322                 return ENOMEM;
323         }
324
325         tevent_req_poll(req, tctx->ev);
326
327         status = ctdb_event_script_enable_recv(req, &ret, &result);
328         talloc_free(req);
329         if (! status) {
330                 fprintf(stderr, "Failed to enable script %s, ret=%d\n",
331                         argv[0], ret);
332                 return ret;
333         }
334
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",
341                         argv[0], result);
342         }
343
344         ret = (result < 0) ? -result : result;
345         return ret;
346 }
347
348 static int command_script_disable(TALLOC_CTX *mem_ctx,
349                                   struct tool_context *tctx,
350                                   int argc, const char **argv)
351 {
352         struct tevent_req *req;
353         int ret, result;
354         bool status;
355
356         if (argc != 1) {
357                 usage();
358         }
359
360         req = ctdb_event_script_disable_send(mem_ctx, tctx->ev, tctx->eclient,
361                                              argv[0]);
362         if (req == NULL) {
363                 return ENOMEM;
364         }
365
366         tevent_req_poll(req, tctx->ev);
367
368         status = ctdb_event_script_disable_recv(req, &ret, &result);
369         talloc_free(req);
370         if (! status) {
371                 fprintf(stderr, "Failed to disable script %s, ret=%d\n",
372                         argv[0], ret);
373                 return ret;
374         }
375
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",
382                         argv[0], result);
383         }
384
385         ret = (result < 0) ? -result : result;
386         return ret;
387 }
388
389 static const struct ctdb_event_cmd {
390         const char *name;
391         const char *str1;
392         const char *str2;
393         int (*fn)(TALLOC_CTX *, struct tool_context *, int, const char **);
394         const char *msg;
395         const char *args;
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>" },
408 };
409
410 static void usage(void)
411 {
412         const struct ctdb_event_cmd *cmd;
413         int i;
414
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];
419
420                 printf("  %-15s %-37s  %s\n",
421                        cmd->name, cmd->args ? cmd->args : "", cmd->msg);
422         }
423
424         exit(1);
425 }
426
427 static const struct ctdb_event_cmd *match_command(const char *str1,
428                                                   const char *str2)
429 {
430         const struct ctdb_event_cmd *cmd;
431         bool match = false;
432         int i;
433
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) {
438                         match = true;
439                 }
440                 if (cmd->str2 != NULL) {
441                         if (str2 == NULL) {
442                                 match = false;
443                         } else {
444                                 if (strcmp(str2, cmd->str2) == 0) {
445                                         match = true;
446                                 } else {
447                                         match = false;
448                                 }
449                         }
450                 }
451
452                 if (match) {
453                         return cmd;
454                 }
455         }
456
457         return NULL;
458 }
459
460 static int process_command(const char *sockpath,
461                            const struct ctdb_event_cmd *cmd,
462                            int argc, const char **argv)
463 {
464         TALLOC_CTX *tmp_ctx;
465         struct tool_context *tctx;
466         int ret;
467
468         tmp_ctx = talloc_new(NULL);
469         if (tmp_ctx == NULL) {
470                 fprintf(stderr, "Memory allocation error\n");
471                 goto fail;
472         }
473
474         tctx = talloc_zero(tmp_ctx, struct tool_context);
475         if (tctx == NULL) {
476                 fprintf(stderr, "Memory allocation error\n");
477                 goto fail;
478         }
479
480         tctx->ev = tevent_context_init(tctx);
481         if (tctx->ev == NULL) {
482                 fprintf(stderr, "Failed to initialize tevent\n");
483                 goto fail;
484         }
485
486         ret = ctdb_event_init(tmp_ctx, tctx->ev, sockpath, &tctx->eclient);
487         if (ret != 0) {
488                 fprintf(stderr, "ctdb_event_init() failed, ret=%d\n", ret);
489                 goto fail;
490         }
491
492         if (cmd->str2 == NULL) {
493                 ret = cmd->fn(tmp_ctx, tctx, argc-1, &argv[1]);
494         } else {
495                 ret = cmd->fn(tmp_ctx, tctx, argc-2, &argv[2]);
496         }
497
498         talloc_free(tmp_ctx);
499         return ret;
500
501 fail:
502         TALLOC_FREE(tmp_ctx);
503         return 1;
504 }
505
506 int main(int argc, const char **argv)
507 {
508         const struct ctdb_event_cmd *cmd;
509         const char *eventd_socket;
510         const char *t;
511
512         if (argc < 3) {
513                 usage();
514         }
515
516         eventd_socket = argv[1];
517
518         cmd = match_command(argv[2], argv[3]);
519         if (cmd == NULL) {
520                 fprintf(stderr, "Unknown command '%s %s'", argv[2], argv[3]);
521                 exit(1);
522         }
523
524         /* Enable logging */
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;
529         }
530
531         return process_command(eventd_socket, cmd, argc-2, &argv[2]);
532 }