s4:auth/gensec: remove unused include of lib/socket/socket.h
[Samba.git] / ctdb / tools / ctdb.c
blob9b7fb11831eaf91372ecf8ca8b0d6be6a69cf474
1 /*
2 ctdb control tool
4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "system/network.h"
25 #include "system/locale.h"
26 #include "popt.h"
27 #include "cmdline.h"
28 #include "../include/ctdb_version.h"
29 #include "../include/ctdb_client.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
32 #include "lib/tdb_wrap/tdb_wrap.h"
33 #include "lib/util/dlinklist.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 static void usage(void);
41 static struct {
42 int timelimit;
43 uint32_t pnn;
44 uint32_t *nodes;
45 int machinereadable;
46 const char *machineseparator;
47 int verbose;
48 int maxruntime;
49 int printemptyrecords;
50 int printdatasize;
51 int printlmaster;
52 int printhash;
53 int printrecordflags;
54 } options;
56 #define LONGTIMEOUT options.timelimit*10
58 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
59 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
61 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
63 return (tv2->tv_sec - tv->tv_sec) +
64 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
67 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
69 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
70 return 0;
73 /* Like printf(3) but substitute for separator in format */
74 static int printm(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
75 static int printm(const char *format, ...)
77 va_list ap;
78 int ret;
79 size_t len = strlen(format);
80 char new_format[len+1];
82 strcpy(new_format, format);
84 if (options.machineseparator[0] != ':') {
85 all_string_sub(new_format,
86 ":", options.machineseparator, len + 1);
89 va_start(ap, format);
90 ret = vprintf(new_format, ap);
91 va_end(ap);
93 return ret;
96 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
97 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
98 "Out of memory in " __location__ )); \
99 abort(); \
100 }} while (0)
102 static uint32_t getpnn(struct ctdb_context *ctdb)
104 if ((options.pnn == CTDB_BROADCAST_ALL) ||
105 (options.pnn == CTDB_MULTICAST)) {
106 DEBUG(DEBUG_ERR,
107 ("Cannot get PNN for node %u\n", options.pnn));
108 exit(1);
111 if (options.pnn == CTDB_CURRENT_NODE) {
112 return ctdb_get_pnn(ctdb);
113 } else {
114 return options.pnn;
118 static void assert_single_node_only(void)
120 if ((options.pnn == CTDB_BROADCAST_ALL) ||
121 (options.pnn == CTDB_MULTICAST)) {
122 DEBUG(DEBUG_ERR,
123 ("This control can not be applied to multiple PNNs\n"));
124 exit(1);
128 static void assert_current_node_only(struct ctdb_context *ctdb)
130 if (options.pnn != ctdb_get_pnn(ctdb)) {
131 DEBUG(DEBUG_ERR,
132 ("This control can only be applied to the current node\n"));
133 exit(1);
137 /* Pretty print the flags to a static buffer in human-readable format.
138 * This never returns NULL!
140 static const char *pretty_print_flags(uint32_t flags)
142 int j;
143 static const struct {
144 uint32_t flag;
145 const char *name;
146 } flag_names[] = {
147 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
148 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
149 { NODE_FLAGS_BANNED, "BANNED" },
150 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
151 { NODE_FLAGS_DELETED, "DELETED" },
152 { NODE_FLAGS_STOPPED, "STOPPED" },
153 { NODE_FLAGS_INACTIVE, "INACTIVE" },
155 static char flags_str[512]; /* Big enough to contain all flag names */
157 flags_str[0] = '\0';
158 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
159 if (flags & flag_names[j].flag) {
160 if (flags_str[0] == '\0') {
161 (void) strcpy(flags_str, flag_names[j].name);
162 } else {
163 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
164 (void) strncat(flags_str, flag_names[j].name,
165 sizeof(flags_str)-1);
169 if (flags_str[0] == '\0') {
170 (void) strcpy(flags_str, "OK");
173 return flags_str;
176 static int h2i(char h)
178 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
179 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
180 return h - '0';
183 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
185 int i, len;
186 TDB_DATA key = {NULL, 0};
188 len = strlen(str);
189 if (len & 0x01) {
190 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
191 return key;
194 key.dsize = len>>1;
195 key.dptr = talloc_size(mem_ctx, key.dsize);
197 for (i=0; i < len/2; i++) {
198 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
200 return key;
203 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
204 * that are disconnected or deleted. If dd_ok is true those nodes are
205 * included in the output list of nodes. If dd_ok is false, those
206 * nodes are filtered from the "all" case and cause an error if
207 * explicitly specified.
209 static bool parse_nodestring(struct ctdb_context *ctdb,
210 TALLOC_CTX *mem_ctx,
211 const char * nodestring,
212 uint32_t current_pnn,
213 bool dd_ok,
214 uint32_t **nodes,
215 uint32_t *pnn_mode)
217 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
218 int n;
219 uint32_t i;
220 struct ctdb_node_map *nodemap;
221 int ret;
223 *nodes = NULL;
225 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
226 if (ret != 0) {
227 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
228 talloc_free(tmp_ctx);
229 exit(10);
232 if (nodestring != NULL) {
233 *nodes = talloc_array(mem_ctx, uint32_t, 0);
234 if (*nodes == NULL) {
235 goto failed;
238 n = 0;
240 if (strcmp(nodestring, "all") == 0) {
241 *pnn_mode = CTDB_BROADCAST_ALL;
243 /* all */
244 for (i = 0; i < nodemap->num; i++) {
245 if ((nodemap->nodes[i].flags &
246 (NODE_FLAGS_DISCONNECTED |
247 NODE_FLAGS_DELETED)) && !dd_ok) {
248 continue;
250 *nodes = talloc_realloc(mem_ctx, *nodes,
251 uint32_t, n+1);
252 if (*nodes == NULL) {
253 goto failed;
255 (*nodes)[n] = i;
256 n++;
258 } else {
259 /* x{,y...} */
260 char *ns, *tok;
262 ns = talloc_strdup(tmp_ctx, nodestring);
263 tok = strtok(ns, ",");
264 while (tok != NULL) {
265 uint32_t pnn;
266 char *endptr;
267 i = (uint32_t)strtoul(tok, &endptr, 0);
268 if (i == 0 && tok == endptr) {
269 DEBUG(DEBUG_ERR,
270 ("Invalid node %s\n", tok));
271 talloc_free(tmp_ctx);
272 exit(ERR_NONODE);
274 if (i >= nodemap->num) {
275 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
276 talloc_free(tmp_ctx);
277 exit(ERR_NONODE);
279 if ((nodemap->nodes[i].flags &
280 (NODE_FLAGS_DISCONNECTED |
281 NODE_FLAGS_DELETED)) && !dd_ok) {
282 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
283 talloc_free(tmp_ctx);
284 exit(ERR_DISNODE);
286 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
287 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
288 talloc_free(tmp_ctx);
289 exit(10);
292 *nodes = talloc_realloc(mem_ctx, *nodes,
293 uint32_t, n+1);
294 if (*nodes == NULL) {
295 goto failed;
298 (*nodes)[n] = i;
299 n++;
301 tok = strtok(NULL, ",");
303 talloc_free(ns);
305 if (n == 1) {
306 *pnn_mode = (*nodes)[0];
307 } else {
308 *pnn_mode = CTDB_MULTICAST;
311 } else {
312 /* default - no nodes specified */
313 *nodes = talloc_array(mem_ctx, uint32_t, 1);
314 if (*nodes == NULL) {
315 goto failed;
317 *pnn_mode = CTDB_CURRENT_NODE;
319 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
320 goto failed;
324 talloc_free(tmp_ctx);
325 return true;
327 failed:
328 talloc_free(tmp_ctx);
329 return false;
333 check if a database exists
335 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
336 uint32_t *dbid, const char **dbname, uint8_t *flags)
338 int i, ret;
339 struct ctdb_dbid_map *dbmap=NULL;
340 bool dbid_given = false, found = false;
341 uint32_t id;
342 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
343 const char *name = NULL;
345 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
346 if (ret != 0) {
347 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
348 goto fail;
351 if (strncmp(dbarg, "0x", 2) == 0) {
352 id = strtoul(dbarg, NULL, 0);
353 dbid_given = true;
356 for(i=0; i<dbmap->num; i++) {
357 if (dbid_given) {
358 if (id == dbmap->dbs[i].dbid) {
359 found = true;
360 break;
362 } else {
363 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
364 if (ret != 0) {
365 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
366 goto fail;
369 if (strcmp(name, dbarg) == 0) {
370 id = dbmap->dbs[i].dbid;
371 found = true;
372 break;
377 if (found && dbid_given && dbname != NULL) {
378 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
379 if (ret != 0) {
380 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
381 found = false;
382 goto fail;
386 if (found) {
387 if (dbid) *dbid = id;
388 if (dbname) *dbname = talloc_strdup(ctdb, name);
389 if (flags) *flags = dbmap->dbs[i].flags;
390 } else {
391 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
394 fail:
395 talloc_free(tmp_ctx);
396 return found;
400 see if a process exists
402 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
404 uint32_t pnn, pid;
405 int ret;
406 if (argc < 1) {
407 usage();
410 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
411 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
412 return -1;
415 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
416 if (ret == 0) {
417 printf("%u:%u exists\n", pnn, pid);
418 } else {
419 printf("%u:%u does not exist\n", pnn, pid);
421 return ret;
425 display statistics structure
427 static void show_statistics(struct ctdb_statistics *s, int show_header)
429 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
430 int i;
431 const char *prefix=NULL;
432 int preflen=0;
433 int tmp, days, hours, minutes, seconds;
434 const struct {
435 const char *name;
436 uint32_t offset;
437 } fields[] = {
438 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
439 STATISTICS_FIELD(num_clients),
440 STATISTICS_FIELD(frozen),
441 STATISTICS_FIELD(recovering),
442 STATISTICS_FIELD(num_recoveries),
443 STATISTICS_FIELD(client_packets_sent),
444 STATISTICS_FIELD(client_packets_recv),
445 STATISTICS_FIELD(node_packets_sent),
446 STATISTICS_FIELD(node_packets_recv),
447 STATISTICS_FIELD(keepalive_packets_sent),
448 STATISTICS_FIELD(keepalive_packets_recv),
449 STATISTICS_FIELD(node.req_call),
450 STATISTICS_FIELD(node.reply_call),
451 STATISTICS_FIELD(node.req_dmaster),
452 STATISTICS_FIELD(node.reply_dmaster),
453 STATISTICS_FIELD(node.reply_error),
454 STATISTICS_FIELD(node.req_message),
455 STATISTICS_FIELD(node.req_control),
456 STATISTICS_FIELD(node.reply_control),
457 STATISTICS_FIELD(client.req_call),
458 STATISTICS_FIELD(client.req_message),
459 STATISTICS_FIELD(client.req_control),
460 STATISTICS_FIELD(timeouts.call),
461 STATISTICS_FIELD(timeouts.control),
462 STATISTICS_FIELD(timeouts.traverse),
463 STATISTICS_FIELD(locks.num_calls),
464 STATISTICS_FIELD(locks.num_current),
465 STATISTICS_FIELD(locks.num_pending),
466 STATISTICS_FIELD(locks.num_failed),
467 STATISTICS_FIELD(total_calls),
468 STATISTICS_FIELD(pending_calls),
469 STATISTICS_FIELD(childwrite_calls),
470 STATISTICS_FIELD(pending_childwrite_calls),
471 STATISTICS_FIELD(memory_used),
472 STATISTICS_FIELD(max_hop_count),
473 STATISTICS_FIELD(total_ro_delegations),
474 STATISTICS_FIELD(total_ro_revokes),
477 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
478 seconds = tmp%60;
479 tmp /= 60;
480 minutes = tmp%60;
481 tmp /= 60;
482 hours = tmp%24;
483 tmp /= 24;
484 days = tmp;
486 if (options.machinereadable){
487 if (show_header) {
488 printm("CTDB version:");
489 printm("Current time of statistics:");
490 printm("Statistics collected since:");
491 for (i=0;i<ARRAY_SIZE(fields);i++) {
492 printm("%s:", fields[i].name);
494 printm("num_reclock_ctdbd_latency:");
495 printm("min_reclock_ctdbd_latency:");
496 printm("avg_reclock_ctdbd_latency:");
497 printm("max_reclock_ctdbd_latency:");
499 printm("num_reclock_recd_latency:");
500 printm("min_reclock_recd_latency:");
501 printm("avg_reclock_recd_latency:");
502 printm("max_reclock_recd_latency:");
504 printm("num_call_latency:");
505 printm("min_call_latency:");
506 printm("avg_call_latency:");
507 printm("max_call_latency:");
509 printm("num_lockwait_latency:");
510 printm("min_lockwait_latency:");
511 printm("avg_lockwait_latency:");
512 printm("max_lockwait_latency:");
514 printm("num_childwrite_latency:");
515 printm("min_childwrite_latency:");
516 printm("avg_childwrite_latency:");
517 printm("max_childwrite_latency:");
518 printm("\n");
520 printm("%d:", CTDB_PROTOCOL);
521 printm("%d:", (int)s->statistics_current_time.tv_sec);
522 printm("%d:", (int)s->statistics_start_time.tv_sec);
523 for (i=0;i<ARRAY_SIZE(fields);i++) {
524 printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
526 printm("%d:", s->reclock.ctdbd.num);
527 printm("%.6f:", s->reclock.ctdbd.min);
528 printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
529 printm("%.6f:", s->reclock.ctdbd.max);
531 printm("%d:", s->reclock.recd.num);
532 printm("%.6f:", s->reclock.recd.min);
533 printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
534 printm("%.6f:", s->reclock.recd.max);
536 printm("%d:", s->call_latency.num);
537 printm("%.6f:", s->call_latency.min);
538 printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
539 printm("%.6f:", s->call_latency.max);
541 printm("%d:", s->childwrite_latency.num);
542 printm("%.6f:", s->childwrite_latency.min);
543 printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
544 printm("%.6f:", s->childwrite_latency.max);
545 printm("\n");
546 } else {
547 printf("CTDB version %u\n", CTDB_PROTOCOL);
548 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
549 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
551 for (i=0;i<ARRAY_SIZE(fields);i++) {
552 if (strchr(fields[i].name, '.')) {
553 preflen = strcspn(fields[i].name, ".")+1;
554 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
555 prefix = fields[i].name;
556 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
558 } else {
559 preflen = 0;
561 printf(" %*s%-22s%*s%10u\n",
562 preflen?4:0, "",
563 fields[i].name+preflen,
564 preflen?0:4, "",
565 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
567 printf(" hop_count_buckets:");
568 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
569 printf(" %d", s->hop_count_bucket[i]);
571 printf("\n");
572 printf(" lock_buckets:");
573 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
574 printf(" %d", s->locks.buckets[i]);
576 printf("\n");
577 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "locks_latency MIN/AVG/MAX", s->locks.latency.min, s->locks.latency.num?s->locks.latency.total/s->locks.latency.num:0.0, s->locks.latency.max, s->locks.latency.num);
579 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
581 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
583 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "call_latency MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
584 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
587 talloc_free(tmp_ctx);
591 display remote ctdb statistics combined from all nodes
593 static int control_statistics_all(struct ctdb_context *ctdb)
595 int ret, i;
596 struct ctdb_statistics statistics;
597 uint32_t *nodes;
598 uint32_t num_nodes;
600 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
601 CTDB_NO_MEMORY(ctdb, nodes);
603 ZERO_STRUCT(statistics);
605 for (i=0;i<num_nodes;i++) {
606 struct ctdb_statistics s1;
607 int j;
608 uint32_t *v1 = (uint32_t *)&s1;
609 uint32_t *v2 = (uint32_t *)&statistics;
610 uint32_t num_ints =
611 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
612 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
613 if (ret != 0) {
614 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
615 return ret;
617 for (j=0;j<num_ints;j++) {
618 v2[j] += v1[j];
620 statistics.max_hop_count =
621 MAX(statistics.max_hop_count, s1.max_hop_count);
622 statistics.call_latency.max =
623 MAX(statistics.call_latency.max, s1.call_latency.max);
625 talloc_free(nodes);
626 printf("Gathered statistics for %u nodes\n", num_nodes);
627 show_statistics(&statistics, 1);
628 return 0;
632 display remote ctdb statistics
634 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
636 int ret;
637 struct ctdb_statistics statistics;
639 if (options.pnn == CTDB_BROADCAST_ALL) {
640 return control_statistics_all(ctdb);
643 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
644 if (ret != 0) {
645 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
646 return ret;
648 show_statistics(&statistics, 1);
649 return 0;
654 reset remote ctdb statistics
656 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
658 int ret;
660 ret = ctdb_statistics_reset(ctdb, options.pnn);
661 if (ret != 0) {
662 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
663 return ret;
665 return 0;
670 display remote ctdb rolling statistics
672 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
674 int ret;
675 struct ctdb_statistics_wire *stats;
676 int i, num_records = -1;
678 assert_single_node_only();
680 if (argc ==1) {
681 num_records = atoi(argv[0]) - 1;
684 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
685 if (ret != 0) {
686 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
687 return ret;
689 for (i=0;i<stats->num;i++) {
690 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
691 continue;
693 show_statistics(&stats->stats[i], i==0);
694 if (i == num_records) {
695 break;
698 return 0;
703 display remote ctdb db statistics
705 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
707 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
708 struct ctdb_db_statistics *dbstat;
709 int i;
710 uint32_t db_id;
711 int num_hot_keys;
712 int ret;
714 if (argc < 1) {
715 usage();
718 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
719 return -1;
722 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
723 if (ret != 0) {
724 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
725 talloc_free(tmp_ctx);
726 return -1;
729 printf("DB Statistics: %s\n", argv[0]);
730 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
731 dbstat->db_ro_delegations);
732 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
733 dbstat->db_ro_delegations);
734 printf(" %s\n", "locks");
735 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
736 dbstat->locks.num_calls);
737 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
738 dbstat->locks.num_failed);
739 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
740 dbstat->locks.num_current);
741 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
742 dbstat->locks.num_pending);
743 printf(" %s", "hop_count_buckets:");
744 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
745 printf(" %d", dbstat->hop_count_bucket[i]);
747 printf("\n");
748 printf(" %s", "lock_buckets:");
749 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
750 printf(" %d", dbstat->locks.buckets[i]);
752 printf("\n");
753 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
754 "locks_latency MIN/AVG/MAX",
755 dbstat->locks.latency.min,
756 (dbstat->locks.latency.num ?
757 dbstat->locks.latency.total /dbstat->locks.latency.num :
758 0.0),
759 dbstat->locks.latency.max,
760 dbstat->locks.latency.num);
761 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
762 "vacuum_latency MIN/AVG/MAX",
763 dbstat->vacuum.latency.min,
764 (dbstat->vacuum.latency.num ?
765 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
766 0.0),
767 dbstat->vacuum.latency.max,
768 dbstat->vacuum.latency.num);
769 num_hot_keys = 0;
770 for (i=0; i<dbstat->num_hot_keys; i++) {
771 if (dbstat->hot_keys[i].count > 0) {
772 num_hot_keys++;
775 dbstat->num_hot_keys = num_hot_keys;
777 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
778 for (i = 0; i < dbstat->num_hot_keys; i++) {
779 int j;
780 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
781 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
782 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
784 printf("\n");
787 talloc_free(tmp_ctx);
788 return 0;
792 display uptime of remote node
794 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
796 int ret;
797 struct ctdb_uptime *uptime = NULL;
798 int tmp, days, hours, minutes, seconds;
800 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
801 if (ret != 0) {
802 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
803 return ret;
806 if (options.machinereadable){
807 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
808 printm(":%u:%u:%u:%lf\n",
809 (unsigned int)uptime->current_time.tv_sec,
810 (unsigned int)uptime->ctdbd_start_time.tv_sec,
811 (unsigned int)uptime->last_recovery_finished.tv_sec,
812 timeval_delta(&uptime->last_recovery_finished,
813 &uptime->last_recovery_started)
815 return 0;
818 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
820 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
821 seconds = tmp%60;
822 tmp /= 60;
823 minutes = tmp%60;
824 tmp /= 60;
825 hours = tmp%24;
826 tmp /= 24;
827 days = tmp;
828 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
830 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
831 seconds = tmp%60;
832 tmp /= 60;
833 minutes = tmp%60;
834 tmp /= 60;
835 hours = tmp%24;
836 tmp /= 24;
837 days = tmp;
838 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
840 printf("Duration of last recovery/failover: %lf seconds\n",
841 timeval_delta(&uptime->last_recovery_finished,
842 &uptime->last_recovery_started));
844 return 0;
848 show the PNN of the current node
850 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
852 uint32_t mypnn;
854 mypnn = getpnn(ctdb);
856 printf("PNN:%d\n", mypnn);
857 return 0;
861 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx)
863 const char *nodes_list;
865 /* read the nodes file */
866 nodes_list = getenv("CTDB_NODES");
867 if (nodes_list == NULL) {
868 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
869 getenv("CTDB_BASE"));
870 if (nodes_list == NULL) {
871 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
872 exit(1);
876 return ctdb_read_nodes_file(mem_ctx, nodes_list);
880 show the PNN of the current node
881 discover the pnn by loading the nodes file and try to bind to all
882 addresses one at a time until the ip address is found.
884 static int find_node_xpnn(void)
886 TALLOC_CTX *mem_ctx = talloc_new(NULL);
887 struct ctdb_node_map *node_map;
888 int i, pnn;
890 node_map = read_nodes_file(mem_ctx);
891 if (node_map == NULL) {
892 talloc_free(mem_ctx);
893 return -1;
896 for (i = 0; i < node_map->num; i++) {
897 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
898 continue;
900 if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
901 pnn = node_map->nodes[i].pnn;
902 talloc_free(mem_ctx);
903 return pnn;
907 printf("Failed to detect which PNN this node is\n");
908 talloc_free(mem_ctx);
909 return -1;
912 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
914 uint32_t pnn;
916 assert_single_node_only();
918 pnn = find_node_xpnn();
919 if (pnn == -1) {
920 return -1;
923 printf("PNN:%d\n", pnn);
924 return 0;
927 /* Helpers for ctdb status
929 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
931 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
932 int j;
933 bool ret = false;
935 if (node->flags == 0) {
936 struct ctdb_control_get_ifaces *ifaces;
938 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
939 tmp_ctx, &ifaces) == 0) {
940 for (j=0; j < ifaces->num; j++) {
941 if (ifaces->ifaces[j].link_state != 0) {
942 continue;
944 ret = true;
945 break;
949 talloc_free(tmp_ctx);
951 return ret;
954 static void control_status_header_machine(void)
956 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
957 ":Inactive:PartiallyOnline:ThisNode:\n");
960 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
961 struct ctdb_node_and_flags *node)
963 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
964 ctdb_addr_to_str(&node->addr),
965 !!(node->flags&NODE_FLAGS_DISCONNECTED),
966 !!(node->flags&NODE_FLAGS_BANNED),
967 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
968 !!(node->flags&NODE_FLAGS_UNHEALTHY),
969 !!(node->flags&NODE_FLAGS_STOPPED),
970 !!(node->flags&NODE_FLAGS_INACTIVE),
971 is_partially_online(ctdb, node) ? 1 : 0,
972 (node->pnn == mypnn)?'Y':'N');
974 return node->flags;
977 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
978 struct ctdb_node_and_flags *node)
980 printf("pnn:%d %-16s %s%s\n", node->pnn,
981 ctdb_addr_to_str(&node->addr),
982 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
983 node->pnn == mypnn?" (THIS NODE)":"");
985 return node->flags;
989 display remote ctdb status
991 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
993 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
994 int i;
995 struct ctdb_vnn_map *vnnmap=NULL;
996 struct ctdb_node_map *nodemap=NULL;
997 uint32_t recmode, recmaster, mypnn;
998 int num_deleted_nodes = 0;
999 int ret;
1001 mypnn = getpnn(ctdb);
1003 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1004 if (ret != 0) {
1005 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1006 talloc_free(tmp_ctx);
1007 return -1;
1010 if (options.machinereadable) {
1011 control_status_header_machine();
1012 for (i=0;i<nodemap->num;i++) {
1013 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1014 continue;
1016 (void) control_status_1_machine(ctdb, mypnn,
1017 &nodemap->nodes[i]);
1019 talloc_free(tmp_ctx);
1020 return 0;
1023 for (i=0; i<nodemap->num; i++) {
1024 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1025 num_deleted_nodes++;
1028 if (num_deleted_nodes == 0) {
1029 printf("Number of nodes:%d\n", nodemap->num);
1030 } else {
1031 printf("Number of nodes:%d (including %d deleted nodes)\n",
1032 nodemap->num, num_deleted_nodes);
1034 for(i=0;i<nodemap->num;i++){
1035 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1036 continue;
1038 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1041 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1042 if (ret != 0) {
1043 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1044 talloc_free(tmp_ctx);
1045 return -1;
1047 if (vnnmap->generation == INVALID_GENERATION) {
1048 printf("Generation:INVALID\n");
1049 } else {
1050 printf("Generation:%d\n",vnnmap->generation);
1052 printf("Size:%d\n",vnnmap->size);
1053 for(i=0;i<vnnmap->size;i++){
1054 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1057 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1058 if (ret != 0) {
1059 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1060 talloc_free(tmp_ctx);
1061 return -1;
1063 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1065 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1066 if (ret != 0) {
1067 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1068 talloc_free(tmp_ctx);
1069 return -1;
1071 printf("Recovery master:%d\n",recmaster);
1073 talloc_free(tmp_ctx);
1074 return 0;
1077 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1079 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1080 int i, ret;
1081 struct ctdb_node_map *nodemap=NULL;
1082 uint32_t * nodes;
1083 uint32_t pnn_mode, mypnn;
1085 if (argc > 1) {
1086 usage();
1089 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1090 options.pnn, true, &nodes, &pnn_mode)) {
1091 return -1;
1094 if (options.machinereadable) {
1095 control_status_header_machine();
1096 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1097 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1100 mypnn = getpnn(ctdb);
1102 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1103 if (ret != 0) {
1104 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1105 talloc_free(tmp_ctx);
1106 return -1;
1109 ret = 0;
1111 for (i = 0; i < talloc_array_length(nodes); i++) {
1112 if (options.machinereadable) {
1113 ret |= control_status_1_machine(ctdb, mypnn,
1114 &nodemap->nodes[nodes[i]]);
1115 } else {
1116 ret |= control_status_1_human(ctdb, mypnn,
1117 &nodemap->nodes[nodes[i]]);
1121 talloc_free(tmp_ctx);
1122 return ret;
1125 static struct ctdb_node_map *read_natgw_nodes_file(struct ctdb_context *ctdb,
1126 TALLOC_CTX *mem_ctx)
1128 const char *natgw_list;
1129 struct ctdb_node_map *natgw_nodes = NULL;
1131 natgw_list = getenv("CTDB_NATGW_NODES");
1132 if (natgw_list == NULL) {
1133 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1134 getenv("CTDB_BASE"));
1135 if (natgw_list == NULL) {
1136 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1137 exit(1);
1140 /* The PNNs/flags will be junk but they're not used */
1141 natgw_nodes = ctdb_read_nodes_file(mem_ctx, natgw_list);
1142 if (natgw_nodes == NULL) {
1143 DEBUG(DEBUG_ERR,
1144 ("Failed to load natgw node list '%s'\n", natgw_list));
1146 return natgw_nodes;
1150 /* talloc off the existing nodemap... */
1151 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1153 return talloc_zero_size(nodemap,
1154 offsetof(struct ctdb_node_map, nodes) +
1155 nodemap->num * sizeof(struct ctdb_node_and_flags));
1158 static struct ctdb_node_map *
1159 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1160 struct ctdb_node_map *nodemap,
1161 struct ctdb_node_map *natgw_nodes)
1163 int i, j;
1164 struct ctdb_node_map *ret;
1166 ret = talloc_nodemap(nodemap);
1167 CTDB_NO_MEMORY_NULL(ctdb, ret);
1169 ret->num = 0;
1171 for (i = 0; i < nodemap->num; i++) {
1172 for(j = 0; j < natgw_nodes->num ; j++) {
1173 if (nodemap->nodes[j].flags & NODE_FLAGS_DELETED) {
1174 continue;
1176 if (ctdb_same_ip(&natgw_nodes->nodes[j].addr,
1177 &nodemap->nodes[i].addr)) {
1179 ret->nodes[ret->num] = nodemap->nodes[i];
1180 ret->num++;
1181 break;
1186 return ret;
1189 static struct ctdb_node_map *
1190 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1191 struct ctdb_node_map *nodemap,
1192 uint32_t required_capabilities,
1193 bool first_only)
1195 int i;
1196 uint32_t capabilities;
1197 struct ctdb_node_map *ret;
1199 ret = talloc_nodemap(nodemap);
1200 CTDB_NO_MEMORY_NULL(ctdb, ret);
1202 ret->num = 0;
1204 for (i = 0; i < nodemap->num; i++) {
1205 int res;
1207 /* Disconnected nodes have no capabilities! */
1208 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1209 continue;
1212 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1213 nodemap->nodes[i].pnn,
1214 &capabilities);
1215 if (res != 0) {
1216 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1217 nodemap->nodes[i].pnn));
1218 talloc_free(ret);
1219 return NULL;
1221 if (!(capabilities & required_capabilities)) {
1222 continue;
1225 ret->nodes[ret->num] = nodemap->nodes[i];
1226 ret->num++;
1227 if (first_only) {
1228 break;
1232 return ret;
1235 static struct ctdb_node_map *
1236 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1237 struct ctdb_node_map *nodemap,
1238 uint32_t flags_mask)
1240 int i;
1241 struct ctdb_node_map *ret;
1243 ret = talloc_nodemap(nodemap);
1244 CTDB_NO_MEMORY_NULL(ctdb, ret);
1246 ret->num = 0;
1248 for (i = 0; i < nodemap->num; i++) {
1249 if (nodemap->nodes[i].flags & flags_mask) {
1250 continue;
1253 ret->nodes[ret->num] = nodemap->nodes[i];
1254 ret->num++;
1257 return ret;
1261 display the list of nodes belonging to this natgw configuration
1263 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1265 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1266 int i, ret;
1267 struct ctdb_node_map *natgw_nodes = NULL;
1268 struct ctdb_node_map *orig_nodemap=NULL;
1269 struct ctdb_node_map *nodemap;
1270 uint32_t mypnn, pnn;
1271 const char *ip;
1273 /* When we have some nodes that could be the NATGW, make a
1274 * series of attempts to find the first node that doesn't have
1275 * certain status flags set.
1277 uint32_t exclude_flags[] = {
1278 /* Look for a nice healthy node */
1279 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1280 /* If not found, an UNHEALTHY/BANNED node will do */
1281 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1282 /* If not found, a STOPPED node will do */
1283 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1287 /* read the natgw nodes file into a linked list */
1288 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1289 if (natgw_nodes == NULL) {
1290 ret = -1;
1291 goto done;
1294 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1295 tmp_ctx, &orig_nodemap);
1296 if (ret != 0) {
1297 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1298 talloc_free(tmp_ctx);
1299 return -1;
1302 /* Get a nodemap that includes only the nodes in the NATGW
1303 * group */
1304 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1305 if (nodemap == NULL) {
1306 ret = -1;
1307 goto done;
1310 ret = 2; /* matches ENOENT */
1311 pnn = -1;
1312 ip = "0.0.0.0";
1313 /* For each flag mask... */
1314 for (i = 0; exclude_flags[i] != 0; i++) {
1315 /* ... get a nodemap that excludes nodes with with
1316 * masked flags... */
1317 struct ctdb_node_map *t =
1318 filter_nodemap_by_flags(ctdb, nodemap,
1319 exclude_flags[i]);
1320 if (t == NULL) {
1321 /* No memory */
1322 ret = -1;
1323 goto done;
1325 if (t->num > 0) {
1326 /* ... and find the first node with the NATGW
1327 * capability */
1328 struct ctdb_node_map *n;
1329 n = filter_nodemap_by_capabilities(ctdb, t,
1330 CTDB_CAP_NATGW,
1331 true);
1332 if (n == NULL) {
1333 /* No memory */
1334 ret = -1;
1335 goto done;
1337 if (n->num > 0) {
1338 ret = 0;
1339 pnn = n->nodes[0].pnn;
1340 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1341 break;
1344 talloc_free(t);
1347 if (options.machinereadable) {
1348 printm(":Node:IP:\n");
1349 printm(":%d:%s:\n", pnn, ip);
1350 } else {
1351 printf("%d %s\n", pnn, ip);
1354 /* print the pruned list of nodes belonging to this natgw list */
1355 mypnn = getpnn(ctdb);
1356 if (options.machinereadable) {
1357 control_status_header_machine();
1358 } else {
1359 printf("Number of nodes:%d\n", nodemap->num);
1361 for(i=0;i<nodemap->num;i++){
1362 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1363 continue;
1365 if (options.machinereadable) {
1366 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1367 } else {
1368 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1372 done:
1373 talloc_free(tmp_ctx);
1374 return ret;
1378 display the status of the scripts for monitoring (or other events)
1380 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1381 enum ctdb_eventscript_call type)
1383 struct ctdb_scripts_wire *script_status;
1384 int ret, i;
1386 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1387 if (ret != 0) {
1388 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1389 return ret;
1392 if (script_status == NULL) {
1393 if (!options.machinereadable) {
1394 printf("%s cycle never run\n",
1395 ctdb_eventscript_call_names[type]);
1397 return 0;
1400 if (!options.machinereadable) {
1401 int num_run = 0;
1402 for (i=0; i<script_status->num_scripts; i++) {
1403 if (script_status->scripts[i].status != -ENOEXEC) {
1404 num_run++;
1407 printf("%d scripts were executed last %s cycle\n",
1408 num_run,
1409 ctdb_eventscript_call_names[type]);
1411 for (i=0; i<script_status->num_scripts; i++) {
1412 const char *status = NULL;
1414 switch (script_status->scripts[i].status) {
1415 case -ETIME:
1416 status = "TIMEDOUT";
1417 break;
1418 case -ENOEXEC:
1419 status = "DISABLED";
1420 break;
1421 case 0:
1422 status = "OK";
1423 break;
1424 default:
1425 if (script_status->scripts[i].status > 0)
1426 status = "ERROR";
1427 break;
1429 if (options.machinereadable) {
1430 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1431 ctdb_eventscript_call_names[type],
1432 script_status->scripts[i].name,
1433 script_status->scripts[i].status,
1434 status,
1435 (long)script_status->scripts[i].start.tv_sec,
1436 (long)script_status->scripts[i].start.tv_usec,
1437 (long)script_status->scripts[i].finished.tv_sec,
1438 (long)script_status->scripts[i].finished.tv_usec,
1439 script_status->scripts[i].output);
1440 continue;
1442 if (status)
1443 printf("%-20s Status:%s ",
1444 script_status->scripts[i].name, status);
1445 else
1446 /* Some other error, eg from stat. */
1447 printf("%-20s Status:CANNOT RUN (%s)",
1448 script_status->scripts[i].name,
1449 strerror(-script_status->scripts[i].status));
1451 if (script_status->scripts[i].status >= 0) {
1452 printf("Duration:%.3lf ",
1453 timeval_delta(&script_status->scripts[i].finished,
1454 &script_status->scripts[i].start));
1456 if (script_status->scripts[i].status != -ENOEXEC) {
1457 printf("%s",
1458 ctime(&script_status->scripts[i].start.tv_sec));
1459 if (script_status->scripts[i].status != 0) {
1460 printf(" OUTPUT:%s\n",
1461 script_status->scripts[i].output);
1463 } else {
1464 printf("\n");
1467 return 0;
1471 static int control_scriptstatus(struct ctdb_context *ctdb,
1472 int argc, const char **argv)
1474 int ret;
1475 enum ctdb_eventscript_call type, min, max;
1476 const char *arg;
1478 if (argc > 1) {
1479 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1480 return -1;
1483 if (argc == 0)
1484 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1485 else
1486 arg = argv[0];
1488 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1489 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1490 min = type;
1491 max = type+1;
1492 break;
1495 if (type == CTDB_EVENT_MAX) {
1496 if (strcmp(arg, "all") == 0) {
1497 min = 0;
1498 max = CTDB_EVENT_MAX;
1499 } else {
1500 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1501 return -1;
1505 if (options.machinereadable) {
1506 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1509 for (type = min; type < max; type++) {
1510 ret = control_one_scriptstatus(ctdb, type);
1511 if (ret != 0) {
1512 return ret;
1516 return 0;
1520 enable an eventscript
1522 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1524 int ret;
1526 if (argc < 1) {
1527 usage();
1530 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1531 if (ret != 0) {
1532 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1533 return ret;
1536 return 0;
1540 disable an eventscript
1542 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1544 int ret;
1546 if (argc < 1) {
1547 usage();
1550 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1551 if (ret != 0) {
1552 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1553 return ret;
1556 return 0;
1560 display the pnn of the recovery master
1562 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1564 uint32_t recmaster;
1565 int ret;
1567 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1568 if (ret != 0) {
1569 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1570 return -1;
1572 printf("%d\n",recmaster);
1574 return 0;
1578 add a tickle to a public address
1580 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1582 struct ctdb_tcp_connection t;
1583 TDB_DATA data;
1584 int ret;
1586 assert_single_node_only();
1588 if (argc < 2) {
1589 usage();
1592 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1593 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1594 return -1;
1596 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1597 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1598 return -1;
1601 data.dptr = (uint8_t *)&t;
1602 data.dsize = sizeof(t);
1604 /* tell all nodes about this tcp connection */
1605 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1606 0, data, ctdb, NULL, NULL, NULL, NULL);
1607 if (ret != 0) {
1608 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1609 return -1;
1612 return 0;
1617 delete a tickle from a node
1619 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1621 struct ctdb_tcp_connection t;
1622 TDB_DATA data;
1623 int ret;
1625 assert_single_node_only();
1627 if (argc < 2) {
1628 usage();
1631 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1632 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1633 return -1;
1635 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1636 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1637 return -1;
1640 data.dptr = (uint8_t *)&t;
1641 data.dsize = sizeof(t);
1643 /* tell all nodes about this tcp connection */
1644 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1645 0, data, ctdb, NULL, NULL, NULL, NULL);
1646 if (ret != 0) {
1647 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1648 return -1;
1651 return 0;
1656 get a list of all tickles for this pnn
1658 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1660 struct ctdb_control_tcp_tickle_list *list;
1661 ctdb_sock_addr addr;
1662 int i, ret;
1663 unsigned port = 0;
1665 assert_single_node_only();
1667 if (argc < 1) {
1668 usage();
1671 if (argc == 2) {
1672 port = atoi(argv[1]);
1675 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1676 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1677 return -1;
1680 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1681 if (ret == -1) {
1682 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1683 return -1;
1686 if (options.machinereadable){
1687 printm(":source ip:port:destination ip:port:\n");
1688 for (i=0;i<list->tickles.num;i++) {
1689 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1690 continue;
1692 printm(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1693 printm(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1695 } else {
1696 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1697 printf("Num tickles:%u\n", list->tickles.num);
1698 for (i=0;i<list->tickles.num;i++) {
1699 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1700 continue;
1702 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1703 printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1707 talloc_free(list);
1709 return 0;
1713 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1715 struct ctdb_all_public_ips *ips;
1716 struct ctdb_public_ip ip;
1717 int i, ret;
1718 uint32_t *nodes;
1719 uint32_t disable_time;
1720 TDB_DATA data;
1721 struct ctdb_node_map *nodemap=NULL;
1722 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1724 disable_time = 30;
1725 data.dptr = (uint8_t*)&disable_time;
1726 data.dsize = sizeof(disable_time);
1727 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1728 if (ret != 0) {
1729 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1730 return -1;
1735 /* read the public ip list from the node */
1736 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1737 if (ret != 0) {
1738 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1739 talloc_free(tmp_ctx);
1740 return -1;
1743 for (i=0;i<ips->num;i++) {
1744 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1745 break;
1748 if (i==ips->num) {
1749 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1750 pnn, ctdb_addr_to_str(addr)));
1751 talloc_free(tmp_ctx);
1752 return -1;
1755 ip.pnn = pnn;
1756 ip.addr = *addr;
1758 data.dptr = (uint8_t *)&ip;
1759 data.dsize = sizeof(ip);
1761 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1762 if (ret != 0) {
1763 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1764 talloc_free(tmp_ctx);
1765 return ret;
1768 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1769 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1770 nodes, 0,
1771 LONGTIMELIMIT(),
1772 false, data,
1773 NULL, NULL,
1774 NULL);
1775 if (ret != 0) {
1776 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1777 talloc_free(tmp_ctx);
1778 return -1;
1781 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1782 if (ret != 0) {
1783 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1784 talloc_free(tmp_ctx);
1785 return -1;
1788 /* update the recovery daemon so it now knows to expect the new
1789 node assignment for this ip.
1791 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1792 if (ret != 0) {
1793 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1794 return -1;
1797 talloc_free(tmp_ctx);
1798 return 0;
1803 * scans all other nodes and returns a pnn for another node that can host this
1804 * ip address or -1
1806 static int
1807 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1809 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1810 struct ctdb_all_public_ips *ips;
1811 struct ctdb_node_map *nodemap=NULL;
1812 int i, j, ret;
1813 int pnn;
1815 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1816 if (ret != 0) {
1817 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1818 talloc_free(tmp_ctx);
1819 return ret;
1822 for(i=0;i<nodemap->num;i++){
1823 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1824 continue;
1826 if (nodemap->nodes[i].pnn == options.pnn) {
1827 continue;
1830 /* read the public ip list from this node */
1831 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1832 if (ret != 0) {
1833 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1834 return -1;
1837 for (j=0;j<ips->num;j++) {
1838 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1839 pnn = nodemap->nodes[i].pnn;
1840 talloc_free(tmp_ctx);
1841 return pnn;
1844 talloc_free(ips);
1847 talloc_free(tmp_ctx);
1848 return -1;
1851 /* If pnn is -1 then try to find a node to move IP to... */
1852 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1854 bool pnn_specified = (pnn == -1 ? false : true);
1855 int retries = 0;
1857 while (retries < 5) {
1858 if (!pnn_specified) {
1859 pnn = find_other_host_for_public_ip(ctdb, addr);
1860 if (pnn == -1) {
1861 return false;
1863 DEBUG(DEBUG_NOTICE,
1864 ("Trying to move public IP to node %u\n", pnn));
1867 if (move_ip(ctdb, addr, pnn) == 0) {
1868 return true;
1871 sleep(3);
1872 retries++;
1875 return false;
1880 move/failover an ip address to a specific node
1882 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1884 uint32_t pnn;
1885 ctdb_sock_addr addr;
1887 assert_single_node_only();
1889 if (argc < 2) {
1890 usage();
1891 return -1;
1894 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1895 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1896 return -1;
1900 if (sscanf(argv[1], "%u", &pnn) != 1) {
1901 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1902 return -1;
1905 if (!try_moveip(ctdb, &addr, pnn)) {
1906 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1907 return -1;
1910 return 0;
1913 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1915 TDB_DATA data;
1917 data.dptr = (uint8_t *)&pnn;
1918 data.dsize = sizeof(uint32_t);
1919 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1920 DEBUG(DEBUG_ERR,
1921 ("Failed to send message to force node %u to be a rebalancing target\n",
1922 pnn));
1923 return -1;
1926 return 0;
1931 rebalance a node by setting it to allow failback and triggering a
1932 takeover run
1934 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1936 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1937 uint32_t *nodes;
1938 uint32_t pnn_mode;
1939 int i, ret;
1941 assert_single_node_only();
1943 if (argc > 1) {
1944 usage();
1947 /* Determine the nodes where IPs need to be reloaded */
1948 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1949 options.pnn, true, &nodes, &pnn_mode)) {
1950 ret = -1;
1951 goto done;
1954 for (i = 0; i < talloc_array_length(nodes); i++) {
1955 if (!rebalance_node(ctdb, nodes[i])) {
1956 ret = -1;
1960 done:
1961 talloc_free(tmp_ctx);
1962 return ret;
1965 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1967 struct ctdb_public_ip ip;
1968 int ret;
1969 uint32_t *nodes;
1970 uint32_t disable_time;
1971 TDB_DATA data;
1972 struct ctdb_node_map *nodemap=NULL;
1973 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1975 disable_time = 30;
1976 data.dptr = (uint8_t*)&disable_time;
1977 data.dsize = sizeof(disable_time);
1978 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1979 if (ret != 0) {
1980 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1981 return -1;
1984 ip.pnn = -1;
1985 ip.addr = *addr;
1987 data.dptr = (uint8_t *)&ip;
1988 data.dsize = sizeof(ip);
1990 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1991 if (ret != 0) {
1992 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1993 talloc_free(tmp_ctx);
1994 return ret;
1997 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1998 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1999 nodes, 0,
2000 LONGTIMELIMIT(),
2001 false, data,
2002 NULL, NULL,
2003 NULL);
2004 if (ret != 0) {
2005 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2006 talloc_free(tmp_ctx);
2007 return -1;
2010 talloc_free(tmp_ctx);
2011 return 0;
2015 release an ip form all nodes and have it re-assigned by recd
2017 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2019 ctdb_sock_addr addr;
2021 assert_single_node_only();
2023 if (argc < 1) {
2024 usage();
2025 return -1;
2028 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2029 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2030 return -1;
2033 if (rebalance_ip(ctdb, &addr) != 0) {
2034 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2035 return -1;
2038 return 0;
2041 static int getips_store_callback(void *param, void *data)
2043 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2044 struct ctdb_all_public_ips *ips = param;
2045 int i;
2047 i = ips->num++;
2048 ips->ips[i].pnn = node_ip->pnn;
2049 ips->ips[i].addr = node_ip->addr;
2050 return 0;
2053 static int getips_count_callback(void *param, void *data)
2055 uint32_t *count = param;
2057 (*count)++;
2058 return 0;
2061 #define IP_KEYLEN 4
2062 static uint32_t *ip_key(ctdb_sock_addr *ip)
2064 static uint32_t key[IP_KEYLEN];
2066 bzero(key, sizeof(key));
2068 switch (ip->sa.sa_family) {
2069 case AF_INET:
2070 key[0] = ip->ip.sin_addr.s_addr;
2071 break;
2072 case AF_INET6: {
2073 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2074 key[0] = s6_a32[3];
2075 key[1] = s6_a32[2];
2076 key[2] = s6_a32[1];
2077 key[3] = s6_a32[0];
2078 break;
2080 default:
2081 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2082 return key;
2085 return key;
2088 static void *add_ip_callback(void *parm, void *data)
2090 return parm;
2093 static int
2094 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2096 struct ctdb_all_public_ips *tmp_ips;
2097 struct ctdb_node_map *nodemap=NULL;
2098 trbt_tree_t *ip_tree;
2099 int i, j, len, ret;
2100 uint32_t count;
2102 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2103 if (ret != 0) {
2104 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2105 return ret;
2108 ip_tree = trbt_create(tmp_ctx, 0);
2110 for(i=0;i<nodemap->num;i++){
2111 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2112 continue;
2114 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2115 continue;
2118 /* read the public ip list from this node */
2119 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2120 if (ret != 0) {
2121 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2122 return -1;
2125 for (j=0; j<tmp_ips->num;j++) {
2126 struct ctdb_public_ip *node_ip;
2128 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2129 node_ip->pnn = tmp_ips->ips[j].pnn;
2130 node_ip->addr = tmp_ips->ips[j].addr;
2132 trbt_insertarray32_callback(ip_tree,
2133 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2134 add_ip_callback,
2135 node_ip);
2137 talloc_free(tmp_ips);
2140 /* traverse */
2141 count = 0;
2142 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2144 len = offsetof(struct ctdb_all_public_ips, ips) +
2145 count*sizeof(struct ctdb_public_ip);
2146 tmp_ips = talloc_zero_size(tmp_ctx, len);
2147 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2149 *ips = tmp_ips;
2151 return 0;
2155 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2157 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2159 event_add_timed(ctdb->ev, ctdb,
2160 timeval_current_ofs(1, 0),
2161 ctdb_every_second, ctdb);
2164 struct srvid_reply_handler_data {
2165 bool done;
2166 bool wait_for_all;
2167 uint32_t *nodes;
2168 const char *srvid_str;
2171 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2172 uint64_t srvid,
2173 TDB_DATA data,
2174 void *private_data)
2176 struct srvid_reply_handler_data *d =
2177 (struct srvid_reply_handler_data *)private_data;
2178 int i;
2179 int32_t ret;
2181 if (data.dsize != sizeof(ret)) {
2182 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2183 return;
2186 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2187 ret = *(int32_t *)data.dptr;
2188 if (ret < 0) {
2189 DEBUG(DEBUG_ERR,
2190 ("%s failed with result %d\n", d->srvid_str, ret));
2191 return;
2194 if (!d->wait_for_all) {
2195 d->done = true;
2196 return;
2199 /* Wait for all replies */
2200 d->done = true;
2201 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2202 if (d->nodes[i] == ret) {
2203 DEBUG(DEBUG_DEBUG,
2204 ("%s reply received from node %u\n",
2205 d->srvid_str, ret));
2206 d->nodes[i] = -1;
2208 if (d->nodes[i] != -1) {
2209 /* Found a node that hasn't yet replied */
2210 d->done = false;
2215 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2216 * or replies from all connected nodes. arg is the data argument to
2217 * pass in the srvid_request structure - pass 0 if this isn't needed.
2219 static int srvid_broadcast(struct ctdb_context *ctdb,
2220 uint64_t srvid, uint32_t *arg,
2221 const char *srvid_str, bool wait_for_all)
2223 int ret;
2224 TDB_DATA data;
2225 uint32_t pnn;
2226 uint64_t reply_srvid;
2227 struct srvid_request request;
2228 struct srvid_request_data request_data;
2229 struct srvid_reply_handler_data reply_data;
2230 struct timeval tv;
2232 ZERO_STRUCT(request);
2234 /* Time ticks to enable timeouts to be processed */
2235 event_add_timed(ctdb->ev, ctdb,
2236 timeval_current_ofs(1, 0),
2237 ctdb_every_second, ctdb);
2239 pnn = ctdb_get_pnn(ctdb);
2240 reply_srvid = getpid();
2242 if (arg == NULL) {
2243 request.pnn = pnn;
2244 request.srvid = reply_srvid;
2246 data.dptr = (uint8_t *)&request;
2247 data.dsize = sizeof(request);
2248 } else {
2249 request_data.pnn = pnn;
2250 request_data.srvid = reply_srvid;
2251 request_data.data = *arg;
2253 data.dptr = (uint8_t *)&request_data;
2254 data.dsize = sizeof(request_data);
2257 /* Register message port for reply from recovery master */
2258 ctdb_client_set_message_handler(ctdb, reply_srvid,
2259 srvid_broadcast_reply_handler,
2260 &reply_data);
2262 reply_data.wait_for_all = wait_for_all;
2263 reply_data.nodes = NULL;
2264 reply_data.srvid_str = srvid_str;
2266 again:
2267 reply_data.done = false;
2269 if (wait_for_all) {
2270 struct ctdb_node_map *nodemap;
2272 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2273 CTDB_CURRENT_NODE, ctdb, &nodemap);
2274 if (ret != 0) {
2275 DEBUG(DEBUG_ERR,
2276 ("Unable to get nodemap from current node, try again\n"));
2277 sleep(1);
2278 goto again;
2281 if (reply_data.nodes != NULL) {
2282 talloc_free(reply_data.nodes);
2284 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2285 NULL, true);
2287 talloc_free(nodemap);
2290 /* Send to all connected nodes. Only recmaster replies */
2291 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2292 srvid, data);
2293 if (ret != 0) {
2294 /* This can only happen if the socket is closed and
2295 * there's no way to recover from that, so don't try
2296 * again.
2298 DEBUG(DEBUG_ERR,
2299 ("Failed to send %s request to connected nodes\n",
2300 srvid_str));
2301 return -1;
2304 tv = timeval_current();
2305 /* This loop terminates the reply is received */
2306 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2307 event_loop_once(ctdb->ev);
2310 if (!reply_data.done) {
2311 DEBUG(DEBUG_NOTICE,
2312 ("Still waiting for confirmation of %s\n", srvid_str));
2313 sleep(1);
2314 goto again;
2317 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2319 talloc_free(reply_data.nodes);
2321 return 0;
2324 static int ipreallocate(struct ctdb_context *ctdb)
2326 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2327 "IP reallocation", false);
2331 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2333 return ipreallocate(ctdb);
2337 add a public ip address to a node
2339 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2341 int i, ret;
2342 int len, retries = 0;
2343 unsigned mask;
2344 ctdb_sock_addr addr;
2345 struct ctdb_control_ip_iface *pub;
2346 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2347 struct ctdb_all_public_ips *ips;
2350 if (argc != 2) {
2351 talloc_free(tmp_ctx);
2352 usage();
2355 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2356 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2357 talloc_free(tmp_ctx);
2358 return -1;
2361 /* read the public ip list from the node */
2362 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2363 if (ret != 0) {
2364 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2365 talloc_free(tmp_ctx);
2366 return -1;
2368 for (i=0;i<ips->num;i++) {
2369 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2370 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2371 return 0;
2377 /* Dont timeout. This command waits for an ip reallocation
2378 which sometimes can take wuite a while if there has
2379 been a recent recovery
2381 alarm(0);
2383 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2384 pub = talloc_size(tmp_ctx, len);
2385 CTDB_NO_MEMORY(ctdb, pub);
2387 pub->addr = addr;
2388 pub->mask = mask;
2389 pub->len = strlen(argv[1])+1;
2390 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2392 do {
2393 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2394 if (ret != 0) {
2395 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2396 sleep(3);
2397 retries++;
2399 } while (retries < 5 && ret != 0);
2400 if (ret != 0) {
2401 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2402 talloc_free(tmp_ctx);
2403 return ret;
2406 if (rebalance_node(ctdb, options.pnn) != 0) {
2407 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2408 return ret;
2411 talloc_free(tmp_ctx);
2412 return 0;
2416 add a public ip address to a node
2418 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2420 ctdb_sock_addr addr;
2421 char *iface = NULL;
2423 if (argc != 1) {
2424 usage();
2427 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2428 printf("Badly formed ip : %s\n", argv[0]);
2429 return -1;
2432 iface = ctdb_sys_find_ifname(&addr);
2433 if (iface == NULL) {
2434 printf("Failed to get interface name for ip: %s", argv[0]);
2435 return -1;
2438 printf("IP on interface %s\n", iface);
2440 free(iface);
2442 return 0;
2445 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2447 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2449 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2450 struct ctdb_node_map *nodemap=NULL;
2451 struct ctdb_all_public_ips *ips;
2452 int ret, i, j;
2454 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2455 if (ret != 0) {
2456 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2457 return ret;
2460 /* remove it from the nodes that are not hosting the ip currently */
2461 for(i=0;i<nodemap->num;i++){
2462 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2463 continue;
2465 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2466 if (ret != 0) {
2467 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2468 continue;
2471 for (j=0;j<ips->num;j++) {
2472 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2473 break;
2476 if (j==ips->num) {
2477 continue;
2480 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2481 continue;
2484 options.pnn = nodemap->nodes[i].pnn;
2485 control_delip(ctdb, argc, argv);
2489 /* remove it from every node (also the one hosting it) */
2490 for(i=0;i<nodemap->num;i++){
2491 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2492 continue;
2494 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2495 if (ret != 0) {
2496 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2497 continue;
2500 for (j=0;j<ips->num;j++) {
2501 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2502 break;
2505 if (j==ips->num) {
2506 continue;
2509 options.pnn = nodemap->nodes[i].pnn;
2510 control_delip(ctdb, argc, argv);
2513 talloc_free(tmp_ctx);
2514 return 0;
2518 delete a public ip address from a node
2520 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2522 int i, ret;
2523 ctdb_sock_addr addr;
2524 struct ctdb_control_ip_iface pub;
2525 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2526 struct ctdb_all_public_ips *ips;
2528 if (argc != 1) {
2529 talloc_free(tmp_ctx);
2530 usage();
2533 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2534 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2535 return -1;
2538 if (options.pnn == CTDB_BROADCAST_ALL) {
2539 return control_delip_all(ctdb, argc, argv, &addr);
2542 pub.addr = addr;
2543 pub.mask = 0;
2544 pub.len = 0;
2546 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2547 if (ret != 0) {
2548 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2549 talloc_free(tmp_ctx);
2550 return ret;
2553 for (i=0;i<ips->num;i++) {
2554 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2555 break;
2559 if (i==ips->num) {
2560 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2561 ctdb_addr_to_str(&addr)));
2562 talloc_free(tmp_ctx);
2563 return -1;
2566 /* This is an optimisation. If this node is hosting the IP
2567 * then try to move it somewhere else without invoking a full
2568 * takeover run. We don't care if this doesn't work!
2570 if (ips->ips[i].pnn == options.pnn) {
2571 (void) try_moveip(ctdb, &addr, -1);
2574 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2575 if (ret != 0) {
2576 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2577 talloc_free(tmp_ctx);
2578 return ret;
2581 talloc_free(tmp_ctx);
2582 return 0;
2585 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2586 int argc, const char **argv)
2588 struct ctdb_control_killtcp *killtcp;
2589 int max_entries, current, i;
2590 struct timeval timeout;
2591 char line[128], src[128], dst[128];
2592 int linenum;
2593 TDB_DATA data;
2594 struct client_async_data *async_data;
2595 struct ctdb_client_control_state *state;
2597 if (argc != 0) {
2598 usage();
2601 linenum = 1;
2602 killtcp = NULL;
2603 max_entries = 0;
2604 current = 0;
2605 while (!feof(stdin)) {
2606 if (fgets(line, sizeof(line), stdin) == NULL) {
2607 continue;
2610 /* Silently skip empty lines */
2611 if (line[0] == '\n') {
2612 continue;
2615 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2616 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2617 linenum, line));
2618 talloc_free(killtcp);
2619 return -1;
2622 if (current >= max_entries) {
2623 max_entries += 1024;
2624 killtcp = talloc_realloc(ctdb, killtcp,
2625 struct ctdb_control_killtcp,
2626 max_entries);
2627 CTDB_NO_MEMORY(ctdb, killtcp);
2630 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2631 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2632 linenum, src));
2633 talloc_free(killtcp);
2634 return -1;
2637 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2638 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2639 linenum, dst));
2640 talloc_free(killtcp);
2641 return -1;
2644 current++;
2647 async_data = talloc_zero(ctdb, struct client_async_data);
2648 if (async_data == NULL) {
2649 talloc_free(killtcp);
2650 return -1;
2653 for (i = 0; i < current; i++) {
2655 data.dsize = sizeof(struct ctdb_control_killtcp);
2656 data.dptr = (unsigned char *)&killtcp[i];
2658 timeout = TIMELIMIT();
2659 state = ctdb_control_send(ctdb, options.pnn, 0,
2660 CTDB_CONTROL_KILL_TCP, 0, data,
2661 async_data, &timeout, NULL);
2663 if (state == NULL) {
2664 DEBUG(DEBUG_ERR,
2665 ("Failed to call async killtcp control to node %u\n",
2666 options.pnn));
2667 talloc_free(killtcp);
2668 return -1;
2671 ctdb_client_async_add(async_data, state);
2674 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2675 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2676 talloc_free(killtcp);
2677 return -1;
2680 talloc_free(killtcp);
2681 return 0;
2686 kill a tcp connection
2688 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2690 int ret;
2691 struct ctdb_control_killtcp killtcp;
2693 assert_single_node_only();
2695 if (argc == 0) {
2696 return kill_tcp_from_file(ctdb, argc, argv);
2699 if (argc < 2) {
2700 usage();
2703 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2704 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2705 return -1;
2708 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2709 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2710 return -1;
2713 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2714 if (ret != 0) {
2715 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2716 return ret;
2719 return 0;
2724 send a gratious arp
2726 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2728 int ret;
2729 ctdb_sock_addr addr;
2731 assert_single_node_only();
2733 if (argc < 2) {
2734 usage();
2737 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2738 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2739 return -1;
2742 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2743 if (ret != 0) {
2744 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2745 return ret;
2748 return 0;
2752 register a server id
2754 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2756 int ret;
2757 struct ctdb_server_id server_id;
2759 if (argc < 3) {
2760 usage();
2763 server_id.pnn = strtoul(argv[0], NULL, 0);
2764 server_id.type = strtoul(argv[1], NULL, 0);
2765 server_id.server_id = strtoul(argv[2], NULL, 0);
2767 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2768 if (ret != 0) {
2769 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2770 return ret;
2772 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2773 sleep(999);
2774 return -1;
2778 unregister a server id
2780 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2782 int ret;
2783 struct ctdb_server_id server_id;
2785 if (argc < 3) {
2786 usage();
2789 server_id.pnn = strtoul(argv[0], NULL, 0);
2790 server_id.type = strtoul(argv[1], NULL, 0);
2791 server_id.server_id = strtoul(argv[2], NULL, 0);
2793 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2794 if (ret != 0) {
2795 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2796 return ret;
2798 return -1;
2802 check if a server id exists
2804 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2806 uint32_t status = 0;
2807 int ret;
2808 struct ctdb_server_id server_id;
2810 if (argc < 3) {
2811 usage();
2814 server_id.pnn = strtoul(argv[0], NULL, 0);
2815 server_id.type = strtoul(argv[1], NULL, 0);
2816 server_id.server_id = strtoul(argv[2], NULL, 0);
2818 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2819 if (ret != 0) {
2820 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2821 return ret;
2824 if (status) {
2825 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2826 } else {
2827 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2829 return 0;
2833 get a list of all server ids that are registered on a node
2835 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2837 int i, ret;
2838 struct ctdb_server_id_list *server_ids;
2840 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2841 if (ret != 0) {
2842 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2843 return ret;
2846 for (i=0; i<server_ids->num; i++) {
2847 printf("Server id %d:%d:%d\n",
2848 server_ids->server_ids[i].pnn,
2849 server_ids->server_ids[i].type,
2850 server_ids->server_ids[i].server_id);
2853 return -1;
2857 check if a server id exists
2859 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2861 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2862 uint64_t *ids;
2863 uint8_t *result;
2864 int i;
2866 if (argc < 1) {
2867 talloc_free(tmp_ctx);
2868 usage();
2871 ids = talloc_array(tmp_ctx, uint64_t, argc);
2872 result = talloc_array(tmp_ctx, uint8_t, argc);
2874 for (i = 0; i < argc; i++) {
2875 ids[i] = strtoull(argv[i], NULL, 0);
2878 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2879 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2880 options.pnn));
2881 talloc_free(tmp_ctx);
2882 return -1;
2885 for (i=0; i < argc; i++) {
2886 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2887 result[i] ? "exists" : "does not exist");
2890 talloc_free(tmp_ctx);
2891 return 0;
2895 send a tcp tickle ack
2897 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2899 int ret;
2900 ctdb_sock_addr src, dst;
2902 if (argc < 2) {
2903 usage();
2906 if (!parse_ip_port(argv[0], &src)) {
2907 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2908 return -1;
2911 if (!parse_ip_port(argv[1], &dst)) {
2912 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2913 return -1;
2916 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2917 if (ret==0) {
2918 return 0;
2920 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2922 return -1;
2927 display public ip status
2929 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2931 int i, ret;
2932 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2933 struct ctdb_all_public_ips *ips;
2935 if (options.pnn == CTDB_BROADCAST_ALL) {
2936 /* read the list of public ips from all nodes */
2937 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2938 } else {
2939 /* read the public ip list from this node */
2940 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2942 if (ret != 0) {
2943 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2944 talloc_free(tmp_ctx);
2945 return ret;
2948 if (options.machinereadable){
2949 printm(":Public IP:Node:");
2950 if (options.verbose){
2951 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2953 printm("\n");
2954 } else {
2955 if (options.pnn == CTDB_BROADCAST_ALL) {
2956 printf("Public IPs on ALL nodes\n");
2957 } else {
2958 printf("Public IPs on node %u\n", options.pnn);
2962 for (i=1;i<=ips->num;i++) {
2963 struct ctdb_control_public_ip_info *info = NULL;
2964 int32_t pnn;
2965 char *aciface = NULL;
2966 char *avifaces = NULL;
2967 char *cifaces = NULL;
2969 if (options.pnn == CTDB_BROADCAST_ALL) {
2970 pnn = ips->ips[ips->num-i].pnn;
2971 } else {
2972 pnn = options.pnn;
2975 if (pnn != -1) {
2976 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2977 &ips->ips[ips->num-i].addr, &info);
2978 } else {
2979 ret = -1;
2982 if (ret == 0) {
2983 int j;
2984 for (j=0; j < info->num; j++) {
2985 if (cifaces == NULL) {
2986 cifaces = talloc_strdup(info,
2987 info->ifaces[j].name);
2988 } else {
2989 cifaces = talloc_asprintf_append(cifaces,
2990 ",%s",
2991 info->ifaces[j].name);
2994 if (info->active_idx == j) {
2995 aciface = info->ifaces[j].name;
2998 if (info->ifaces[j].link_state == 0) {
2999 continue;
3002 if (avifaces == NULL) {
3003 avifaces = talloc_strdup(info, info->ifaces[j].name);
3004 } else {
3005 avifaces = talloc_asprintf_append(avifaces,
3006 ",%s",
3007 info->ifaces[j].name);
3012 if (options.machinereadable){
3013 printm(":%s:%d:",
3014 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3015 ips->ips[ips->num-i].pnn);
3016 if (options.verbose){
3017 printm("%s:%s:%s:",
3018 aciface?aciface:"",
3019 avifaces?avifaces:"",
3020 cifaces?cifaces:"");
3022 printf("\n");
3023 } else {
3024 if (options.verbose) {
3025 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3026 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3027 ips->ips[ips->num-i].pnn,
3028 aciface?aciface:"",
3029 avifaces?avifaces:"",
3030 cifaces?cifaces:"");
3031 } else {
3032 printf("%s %d\n",
3033 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3034 ips->ips[ips->num-i].pnn);
3037 talloc_free(info);
3040 talloc_free(tmp_ctx);
3041 return 0;
3045 public ip info
3047 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3049 int i, ret;
3050 ctdb_sock_addr addr;
3051 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3052 struct ctdb_control_public_ip_info *info;
3054 if (argc != 1) {
3055 talloc_free(tmp_ctx);
3056 usage();
3059 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3060 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3061 return -1;
3064 /* read the public ip info from this node */
3065 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3066 tmp_ctx, &addr, &info);
3067 if (ret != 0) {
3068 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3069 argv[0], options.pnn));
3070 talloc_free(tmp_ctx);
3071 return ret;
3074 printf("Public IP[%s] info on node %u\n",
3075 ctdb_addr_to_str(&info->ip.addr),
3076 options.pnn);
3078 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3079 ctdb_addr_to_str(&info->ip.addr),
3080 info->ip.pnn, info->num);
3082 for (i=0; i<info->num; i++) {
3083 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3085 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3086 i+1, info->ifaces[i].name,
3087 info->ifaces[i].link_state?"up":"down",
3088 (unsigned int)info->ifaces[i].references,
3089 (i==info->active_idx)?" (active)":"");
3092 talloc_free(tmp_ctx);
3093 return 0;
3097 display interfaces status
3099 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3101 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3102 int i;
3103 struct ctdb_control_get_ifaces *ifaces;
3104 int ret;
3106 /* read the public ip list from this node */
3107 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3108 if (ret != 0) {
3109 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3110 options.pnn));
3111 talloc_free(tmp_ctx);
3112 return -1;
3115 if (options.machinereadable){
3116 printm(":Name:LinkStatus:References:\n");
3117 } else {
3118 printf("Interfaces on node %u\n", options.pnn);
3121 for (i=0; i<ifaces->num; i++) {
3122 if (options.machinereadable){
3123 printm(":%s:%s:%u:\n",
3124 ifaces->ifaces[i].name,
3125 ifaces->ifaces[i].link_state?"1":"0",
3126 (unsigned int)ifaces->ifaces[i].references);
3127 } else {
3128 printf("name:%s link:%s references:%u\n",
3129 ifaces->ifaces[i].name,
3130 ifaces->ifaces[i].link_state?"up":"down",
3131 (unsigned int)ifaces->ifaces[i].references);
3135 talloc_free(tmp_ctx);
3136 return 0;
3141 set link status of an interface
3143 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3145 int ret;
3146 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3147 struct ctdb_control_iface_info info;
3149 ZERO_STRUCT(info);
3151 if (argc != 2) {
3152 usage();
3155 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3156 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3157 argv[0]));
3158 talloc_free(tmp_ctx);
3159 return -1;
3161 strcpy(info.name, argv[0]);
3163 if (strcmp(argv[1], "up") == 0) {
3164 info.link_state = 1;
3165 } else if (strcmp(argv[1], "down") == 0) {
3166 info.link_state = 0;
3167 } else {
3168 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3169 argv[1]));
3170 talloc_free(tmp_ctx);
3171 return -1;
3174 /* read the public ip list from this node */
3175 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3176 tmp_ctx, &info);
3177 if (ret != 0) {
3178 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3179 argv[0], options.pnn));
3180 talloc_free(tmp_ctx);
3181 return ret;
3184 talloc_free(tmp_ctx);
3185 return 0;
3189 display pid of a ctdb daemon
3191 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3193 uint32_t pid;
3194 int ret;
3196 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3197 if (ret != 0) {
3198 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3199 return ret;
3201 printf("Pid:%d\n", pid);
3203 return 0;
3206 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3208 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3209 void *data,
3210 update_flags_handler_t handler,
3211 uint32_t flag,
3212 const char *desc,
3213 bool set_flag)
3215 struct ctdb_node_map *nodemap = NULL;
3216 bool flag_is_set;
3217 int ret;
3219 /* Check if the node is already in the desired state */
3220 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3221 if (ret != 0) {
3222 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3223 exit(10);
3225 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3226 if (set_flag == flag_is_set) {
3227 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3228 (set_flag ? "already" : "not"), desc));
3229 return 0;
3232 do {
3233 if (!handler(ctdb, data)) {
3234 DEBUG(DEBUG_WARNING,
3235 ("Failed to send control to set state %s on node %u, try again\n",
3236 desc, options.pnn));
3239 sleep(1);
3241 /* Read the nodemap and verify the change took effect.
3242 * Even if the above control/hanlder timed out then it
3243 * could still have worked!
3245 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3246 ctdb, &nodemap);
3247 if (ret != 0) {
3248 DEBUG(DEBUG_WARNING,
3249 ("Unable to get nodemap from local node, try again\n"));
3251 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3252 } while (nodemap == NULL || (set_flag != flag_is_set));
3254 return ipreallocate(ctdb);
3257 /* Administratively disable a node */
3258 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3260 int ret;
3262 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3263 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3264 return ret == 0;
3267 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3269 return update_flags_and_ipreallocate(ctdb, NULL,
3270 update_flags_disabled,
3271 NODE_FLAGS_PERMANENTLY_DISABLED,
3272 "disabled",
3273 true /* set_flag*/);
3276 /* Administratively re-enable a node */
3277 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3279 int ret;
3281 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3282 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3283 return ret == 0;
3286 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3288 return update_flags_and_ipreallocate(ctdb, NULL,
3289 update_flags_not_disabled,
3290 NODE_FLAGS_PERMANENTLY_DISABLED,
3291 "disabled",
3292 false /* set_flag*/);
3295 /* Stop a node */
3296 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3298 int ret;
3300 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3302 return ret == 0;
3305 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3307 return update_flags_and_ipreallocate(ctdb, NULL,
3308 update_flags_stopped,
3309 NODE_FLAGS_STOPPED,
3310 "stopped",
3311 true /* set_flag*/);
3314 /* Continue a stopped node */
3315 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3317 int ret;
3319 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3321 return ret == 0;
3324 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3326 return update_flags_and_ipreallocate(ctdb, NULL,
3327 update_flags_not_stopped,
3328 NODE_FLAGS_STOPPED,
3329 "stopped",
3330 false /* set_flag */);
3333 static uint32_t get_generation(struct ctdb_context *ctdb)
3335 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3336 struct ctdb_vnn_map *vnnmap=NULL;
3337 int ret;
3338 uint32_t generation;
3340 /* wait until the recmaster is not in recovery mode */
3341 while (1) {
3342 uint32_t recmode, recmaster;
3344 if (vnnmap != NULL) {
3345 talloc_free(vnnmap);
3346 vnnmap = NULL;
3349 /* get the recmaster */
3350 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3351 if (ret != 0) {
3352 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3353 talloc_free(tmp_ctx);
3354 exit(10);
3357 /* get recovery mode */
3358 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3359 if (ret != 0) {
3360 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3361 talloc_free(tmp_ctx);
3362 exit(10);
3365 /* get the current generation number */
3366 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3367 if (ret != 0) {
3368 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3369 talloc_free(tmp_ctx);
3370 exit(10);
3373 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3374 generation = vnnmap->generation;
3375 talloc_free(tmp_ctx);
3376 return generation;
3378 sleep(1);
3382 /* Ban a node */
3383 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3385 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3386 int ret;
3388 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3390 return ret == 0;
3393 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3395 struct ctdb_ban_time bantime;
3397 if (argc < 1) {
3398 usage();
3401 bantime.pnn = options.pnn;
3402 bantime.time = strtoul(argv[0], NULL, 0);
3404 if (bantime.time == 0) {
3405 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3406 return -1;
3409 return update_flags_and_ipreallocate(ctdb, &bantime,
3410 update_state_banned,
3411 NODE_FLAGS_BANNED,
3412 "banned",
3413 true /* set_flag*/);
3417 /* Unban a node */
3418 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3420 struct ctdb_ban_time bantime;
3422 bantime.pnn = options.pnn;
3423 bantime.time = 0;
3425 return update_flags_and_ipreallocate(ctdb, &bantime,
3426 update_state_banned,
3427 NODE_FLAGS_BANNED,
3428 "banned",
3429 false /* set_flag*/);
3433 show ban information for a node
3435 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3437 int ret;
3438 struct ctdb_node_map *nodemap=NULL;
3439 struct ctdb_ban_time *bantime;
3441 /* verify the node exists */
3442 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3443 if (ret != 0) {
3444 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3445 return ret;
3448 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3449 if (ret != 0) {
3450 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3451 return -1;
3454 if (bantime->time == 0) {
3455 printf("Node %u is not banned\n", bantime->pnn);
3456 } else {
3457 printf("Node %u is banned, %d seconds remaining\n",
3458 bantime->pnn, bantime->time);
3461 return 0;
3465 shutdown a daemon
3467 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3469 int ret;
3471 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3472 if (ret != 0) {
3473 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3474 return ret;
3477 return 0;
3481 trigger a recovery
3483 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3485 int ret;
3486 uint32_t generation, next_generation;
3488 /* record the current generation number */
3489 generation = get_generation(ctdb);
3491 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3492 if (ret != 0) {
3493 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3494 return ret;
3497 /* wait until we are in a new generation */
3498 while (1) {
3499 next_generation = get_generation(ctdb);
3500 if (next_generation != generation) {
3501 return 0;
3503 sleep(1);
3506 return 0;
3511 display monitoring mode of a remote node
3513 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3515 uint32_t monmode;
3516 int ret;
3518 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3519 if (ret != 0) {
3520 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3521 return ret;
3523 if (!options.machinereadable){
3524 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3525 } else {
3526 printm(":mode:\n");
3527 printm(":%d:\n",monmode);
3529 return 0;
3534 display capabilities of a remote node
3536 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3538 uint32_t capabilities;
3539 int ret;
3541 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3542 if (ret != 0) {
3543 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3544 return -1;
3547 if (!options.machinereadable){
3548 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3549 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3550 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3551 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3552 } else {
3553 printm(":RECMASTER:LMASTER:LVS:NATGW:\n");
3554 printm(":%d:%d:%d:%d:\n",
3555 !!(capabilities&CTDB_CAP_RECMASTER),
3556 !!(capabilities&CTDB_CAP_LMASTER),
3557 !!(capabilities&CTDB_CAP_LVS),
3558 !!(capabilities&CTDB_CAP_NATGW));
3560 return 0;
3564 display lvs configuration
3567 static uint32_t lvs_exclude_flags[] = {
3568 /* Look for a nice healthy node */
3569 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3570 /* If not found, an UNHEALTHY node will do */
3571 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3575 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3577 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3578 struct ctdb_node_map *orig_nodemap=NULL;
3579 struct ctdb_node_map *nodemap;
3580 int i, ret;
3582 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3583 tmp_ctx, &orig_nodemap);
3584 if (ret != 0) {
3585 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3586 talloc_free(tmp_ctx);
3587 return -1;
3590 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3591 CTDB_CAP_LVS, false);
3592 if (nodemap == NULL) {
3593 /* No memory */
3594 ret = -1;
3595 goto done;
3598 ret = 0;
3600 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3601 struct ctdb_node_map *t =
3602 filter_nodemap_by_flags(ctdb, nodemap,
3603 lvs_exclude_flags[i]);
3604 if (t == NULL) {
3605 /* No memory */
3606 ret = -1;
3607 goto done;
3609 if (t->num > 0) {
3610 /* At least 1 node without excluded flags */
3611 int j;
3612 for (j = 0; j < t->num; j++) {
3613 printf("%d:%s\n", t->nodes[j].pnn,
3614 ctdb_addr_to_str(&t->nodes[j].addr));
3616 goto done;
3618 talloc_free(t);
3620 done:
3621 talloc_free(tmp_ctx);
3622 return ret;
3626 display who is the lvs master
3628 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3630 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3631 struct ctdb_node_map *nodemap=NULL;
3632 int i, ret;
3634 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3635 tmp_ctx, &nodemap);
3636 if (ret != 0) {
3637 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3638 talloc_free(tmp_ctx);
3639 return -1;
3642 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3643 struct ctdb_node_map *t =
3644 filter_nodemap_by_flags(ctdb, nodemap,
3645 lvs_exclude_flags[i]);
3646 if (t == NULL) {
3647 /* No memory */
3648 ret = -1;
3649 goto done;
3651 if (t->num > 0) {
3652 struct ctdb_node_map *n;
3653 n = filter_nodemap_by_capabilities(ctdb,
3655 CTDB_CAP_LVS,
3656 true);
3657 if (n == NULL) {
3658 /* No memory */
3659 ret = -1;
3660 goto done;
3662 if (n->num > 0) {
3663 ret = 0;
3664 if (options.machinereadable) {
3665 printm("%d\n", n->nodes[0].pnn);
3666 } else {
3667 printf("Node %d is LVS master\n", n->nodes[0].pnn);
3669 goto done;
3672 talloc_free(t);
3675 printf("There is no LVS master\n");
3676 ret = 255;
3677 done:
3678 talloc_free(tmp_ctx);
3679 return ret;
3683 disable monitoring on a node
3685 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3688 int ret;
3690 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3691 if (ret != 0) {
3692 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3693 return ret;
3695 printf("Monitoring mode:%s\n","DISABLED");
3697 return 0;
3701 enable monitoring on a node
3703 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3706 int ret;
3708 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3709 if (ret != 0) {
3710 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3711 return ret;
3713 printf("Monitoring mode:%s\n","ACTIVE");
3715 return 0;
3719 display remote list of keys/data for a db
3721 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3723 const char *db_name;
3724 struct ctdb_db_context *ctdb_db;
3725 int ret;
3726 struct ctdb_dump_db_context c;
3727 uint8_t flags;
3729 if (argc < 1) {
3730 usage();
3733 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3734 return -1;
3737 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3738 if (ctdb_db == NULL) {
3739 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3740 return -1;
3743 if (options.printlmaster) {
3744 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3745 ctdb, &ctdb->vnn_map);
3746 if (ret != 0) {
3747 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3748 options.pnn));
3749 return ret;
3753 ZERO_STRUCT(c);
3754 c.f = stdout;
3755 c.printemptyrecords = (bool)options.printemptyrecords;
3756 c.printdatasize = (bool)options.printdatasize;
3757 c.printlmaster = (bool)options.printlmaster;
3758 c.printhash = (bool)options.printhash;
3759 c.printrecordflags = (bool)options.printrecordflags;
3761 /* traverse and dump the cluster tdb */
3762 ret = ctdb_dump_db(ctdb_db, &c);
3763 if (ret == -1) {
3764 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3765 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3766 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3767 db_name));
3768 return -1;
3770 talloc_free(ctdb_db);
3772 printf("Dumped %d records\n", ret);
3773 return 0;
3776 struct cattdb_data {
3777 struct ctdb_context *ctdb;
3778 uint32_t count;
3781 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3783 struct cattdb_data *d = private_data;
3784 struct ctdb_dump_db_context c;
3786 d->count++;
3788 ZERO_STRUCT(c);
3789 c.f = stdout;
3790 c.printemptyrecords = (bool)options.printemptyrecords;
3791 c.printdatasize = (bool)options.printdatasize;
3792 c.printlmaster = false;
3793 c.printhash = (bool)options.printhash;
3794 c.printrecordflags = true;
3796 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3800 cat the local tdb database using same format as catdb
3802 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3804 const char *db_name;
3805 struct ctdb_db_context *ctdb_db;
3806 struct cattdb_data d;
3807 uint8_t flags;
3809 if (argc < 1) {
3810 usage();
3813 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3814 return -1;
3817 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3818 if (ctdb_db == NULL) {
3819 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3820 return -1;
3823 /* traverse the local tdb */
3824 d.count = 0;
3825 d.ctdb = ctdb;
3826 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3827 printf("Failed to cattdb data\n");
3828 exit(10);
3830 talloc_free(ctdb_db);
3832 printf("Dumped %d records\n", d.count);
3833 return 0;
3837 display the content of a database key
3839 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3841 const char *db_name;
3842 struct ctdb_db_context *ctdb_db;
3843 struct ctdb_record_handle *h;
3844 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3845 TDB_DATA key, data;
3846 uint8_t flags;
3848 if (argc < 2) {
3849 usage();
3852 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3853 return -1;
3856 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3857 if (ctdb_db == NULL) {
3858 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3859 return -1;
3862 key.dptr = discard_const(argv[1]);
3863 key.dsize = strlen((char *)key.dptr);
3865 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3866 if (h == NULL) {
3867 printf("Failed to fetch record '%s' on node %d\n",
3868 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3869 talloc_free(tmp_ctx);
3870 exit(10);
3873 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3875 talloc_free(tmp_ctx);
3876 talloc_free(ctdb_db);
3877 return 0;
3881 display the content of a database key
3883 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3885 const char *db_name;
3886 struct ctdb_db_context *ctdb_db;
3887 struct ctdb_record_handle *h;
3888 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3889 TDB_DATA key, data;
3890 uint8_t flags;
3892 if (argc < 3) {
3893 usage();
3896 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3897 return -1;
3900 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3901 if (ctdb_db == NULL) {
3902 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3903 return -1;
3906 key.dptr = discard_const(argv[1]);
3907 key.dsize = strlen((char *)key.dptr);
3909 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3910 if (h == NULL) {
3911 printf("Failed to fetch record '%s' on node %d\n",
3912 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3913 talloc_free(tmp_ctx);
3914 exit(10);
3917 data.dptr = discard_const(argv[2]);
3918 data.dsize = strlen((char *)data.dptr);
3920 if (ctdb_record_store(h, data) != 0) {
3921 printf("Failed to store record\n");
3924 talloc_free(h);
3925 talloc_free(tmp_ctx);
3926 talloc_free(ctdb_db);
3927 return 0;
3931 fetch a record from a persistent database
3933 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3935 const char *db_name;
3936 struct ctdb_db_context *ctdb_db;
3937 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3938 struct ctdb_transaction_handle *h;
3939 TDB_DATA key, data;
3940 int fd, ret;
3941 bool persistent;
3942 uint8_t flags;
3944 if (argc < 2) {
3945 talloc_free(tmp_ctx);
3946 usage();
3949 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3950 talloc_free(tmp_ctx);
3951 return -1;
3954 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3955 if (!persistent) {
3956 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3957 talloc_free(tmp_ctx);
3958 return -1;
3961 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3962 if (ctdb_db == NULL) {
3963 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3964 talloc_free(tmp_ctx);
3965 return -1;
3968 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3969 if (h == NULL) {
3970 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3971 talloc_free(tmp_ctx);
3972 return -1;
3975 key.dptr = discard_const(argv[1]);
3976 key.dsize = strlen(argv[1]);
3977 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3978 if (ret != 0) {
3979 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3980 talloc_free(tmp_ctx);
3981 return -1;
3984 if (data.dsize == 0 || data.dptr == NULL) {
3985 DEBUG(DEBUG_ERR,("Record is empty\n"));
3986 talloc_free(tmp_ctx);
3987 return -1;
3990 if (argc == 3) {
3991 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3992 if (fd == -1) {
3993 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3994 talloc_free(tmp_ctx);
3995 return -1;
3997 sys_write(fd, data.dptr, data.dsize);
3998 close(fd);
3999 } else {
4000 sys_write(1, data.dptr, data.dsize);
4003 /* abort the transaction */
4004 talloc_free(h);
4007 talloc_free(tmp_ctx);
4008 return 0;
4012 fetch a record from a tdb-file
4014 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4016 const char *tdb_file;
4017 TDB_CONTEXT *tdb;
4018 TDB_DATA key, data;
4019 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4020 int fd;
4022 if (argc < 2) {
4023 usage();
4026 tdb_file = argv[0];
4028 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4029 if (tdb == NULL) {
4030 printf("Failed to open TDB file %s\n", tdb_file);
4031 return -1;
4034 if (!strncmp(argv[1], "0x", 2)) {
4035 key = hextodata(tmp_ctx, argv[1] + 2);
4036 if (key.dsize == 0) {
4037 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4038 return -1;
4040 } else {
4041 key.dptr = discard_const(argv[1]);
4042 key.dsize = strlen(argv[1]);
4045 data = tdb_fetch(tdb, key);
4046 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4047 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4048 tdb_close(tdb);
4049 return -1;
4052 tdb_close(tdb);
4054 if (argc == 3) {
4055 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4056 if (fd == -1) {
4057 printf("Failed to open output file %s\n", argv[2]);
4058 return -1;
4060 if (options.verbose){
4061 sys_write(fd, data.dptr, data.dsize);
4062 } else {
4063 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4065 close(fd);
4066 } else {
4067 if (options.verbose){
4068 sys_write(1, data.dptr, data.dsize);
4069 } else {
4070 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4074 talloc_free(tmp_ctx);
4075 return 0;
4079 store a record and header to a tdb-file
4081 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4083 const char *tdb_file;
4084 TDB_CONTEXT *tdb;
4085 TDB_DATA key, value, data;
4086 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4087 struct ctdb_ltdb_header header;
4089 if (argc < 3) {
4090 usage();
4093 tdb_file = argv[0];
4095 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4096 if (tdb == NULL) {
4097 printf("Failed to open TDB file %s\n", tdb_file);
4098 return -1;
4101 if (!strncmp(argv[1], "0x", 2)) {
4102 key = hextodata(tmp_ctx, argv[1] + 2);
4103 if (key.dsize == 0) {
4104 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4105 return -1;
4107 } else {
4108 key.dptr = discard_const(argv[1]);
4109 key.dsize = strlen(argv[1]);
4112 if (!strncmp(argv[2], "0x", 2)) {
4113 value = hextodata(tmp_ctx, argv[2] + 2);
4114 if (value.dsize == 0) {
4115 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4116 return -1;
4118 } else {
4119 value.dptr = discard_const(argv[2]);
4120 value.dsize = strlen(argv[2]);
4123 ZERO_STRUCT(header);
4124 if (argc > 3) {
4125 header.rsn = atoll(argv[3]);
4127 if (argc > 4) {
4128 header.dmaster = atoi(argv[4]);
4130 if (argc > 5) {
4131 header.flags = atoi(argv[5]);
4134 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4135 data.dptr = talloc_size(tmp_ctx, data.dsize);
4136 if (data.dptr == NULL) {
4137 printf("Failed to allocate header+value\n");
4138 return -1;
4141 *(struct ctdb_ltdb_header *)data.dptr = header;
4142 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4144 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4145 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4146 tdb_close(tdb);
4147 return -1;
4150 tdb_close(tdb);
4152 talloc_free(tmp_ctx);
4153 return 0;
4157 write a record to a persistent database
4159 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4161 const char *db_name;
4162 struct ctdb_db_context *ctdb_db;
4163 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4164 struct ctdb_transaction_handle *h;
4165 struct stat st;
4166 TDB_DATA key, data;
4167 int fd, ret;
4169 if (argc < 3) {
4170 talloc_free(tmp_ctx);
4171 usage();
4174 fd = open(argv[2], O_RDONLY);
4175 if (fd == -1) {
4176 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4177 talloc_free(tmp_ctx);
4178 return -1;
4181 ret = fstat(fd, &st);
4182 if (ret == -1) {
4183 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4184 close(fd);
4185 talloc_free(tmp_ctx);
4186 return -1;
4189 if (!S_ISREG(st.st_mode)) {
4190 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4191 close(fd);
4192 talloc_free(tmp_ctx);
4193 return -1;
4196 data.dsize = st.st_size;
4197 if (data.dsize == 0) {
4198 data.dptr = NULL;
4199 } else {
4200 data.dptr = talloc_size(tmp_ctx, data.dsize);
4201 if (data.dptr == NULL) {
4202 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4203 close(fd);
4204 talloc_free(tmp_ctx);
4205 return -1;
4207 ret = sys_read(fd, data.dptr, data.dsize);
4208 if (ret != data.dsize) {
4209 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4210 close(fd);
4211 talloc_free(tmp_ctx);
4212 return -1;
4215 close(fd);
4218 db_name = argv[0];
4220 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4221 if (ctdb_db == NULL) {
4222 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4223 talloc_free(tmp_ctx);
4224 return -1;
4227 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4228 if (h == NULL) {
4229 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4230 talloc_free(tmp_ctx);
4231 return -1;
4234 key.dptr = discard_const(argv[1]);
4235 key.dsize = strlen(argv[1]);
4236 ret = ctdb_transaction_store(h, key, data);
4237 if (ret != 0) {
4238 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4239 talloc_free(tmp_ctx);
4240 return -1;
4243 ret = ctdb_transaction_commit(h);
4244 if (ret != 0) {
4245 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4246 talloc_free(tmp_ctx);
4247 return -1;
4251 talloc_free(tmp_ctx);
4252 return 0;
4256 * delete a record from a persistent database
4258 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4260 const char *db_name;
4261 struct ctdb_db_context *ctdb_db;
4262 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4263 struct ctdb_transaction_handle *h;
4264 TDB_DATA key;
4265 int ret;
4266 bool persistent;
4267 uint8_t flags;
4269 if (argc < 2) {
4270 talloc_free(tmp_ctx);
4271 usage();
4274 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4275 talloc_free(tmp_ctx);
4276 return -1;
4279 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4280 if (!persistent) {
4281 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4282 talloc_free(tmp_ctx);
4283 return -1;
4286 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4287 if (ctdb_db == NULL) {
4288 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4289 talloc_free(tmp_ctx);
4290 return -1;
4293 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4294 if (h == NULL) {
4295 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4296 talloc_free(tmp_ctx);
4297 return -1;
4300 key.dptr = discard_const(argv[1]);
4301 key.dsize = strlen(argv[1]);
4302 ret = ctdb_transaction_store(h, key, tdb_null);
4303 if (ret != 0) {
4304 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4305 talloc_free(tmp_ctx);
4306 return -1;
4309 ret = ctdb_transaction_commit(h);
4310 if (ret != 0) {
4311 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4312 talloc_free(tmp_ctx);
4313 return -1;
4316 talloc_free(tmp_ctx);
4317 return 0;
4320 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4321 TDB_DATA *data)
4323 const char *t;
4324 size_t n;
4325 const char *ret; /* Next byte after successfully parsed value */
4327 /* Error, unless someone says otherwise */
4328 ret = NULL;
4329 /* Indicates no value to parse */
4330 *data = tdb_null;
4332 /* Skip whitespace */
4333 n = strspn(s, " \t");
4334 t = s + n;
4336 if (t[0] == '"') {
4337 /* Quoted ASCII string - no wide characters! */
4338 t++;
4339 n = strcspn(t, "\"");
4340 if (t[n] == '"') {
4341 if (n > 0) {
4342 data->dsize = n;
4343 data->dptr = talloc_memdup(mem_ctx, t, n);
4344 CTDB_NOMEM_ABORT(data->dptr);
4346 ret = t + n + 1;
4347 } else {
4348 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4350 } else {
4351 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4354 return ret;
4357 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4358 TDB_DATA *key, TDB_DATA *value)
4360 char line [1024]; /* FIXME: make this more flexible? */
4361 const char *t;
4362 char *ptr;
4364 ptr = fgets(line, sizeof(line), file);
4366 if (ptr == NULL) {
4367 return false;
4370 /* Get key */
4371 t = ptrans_parse_string(mem_ctx, line, key);
4372 if (t == NULL || key->dptr == NULL) {
4373 /* Line Ignored but not EOF */
4374 return true;
4377 /* Get value */
4378 t = ptrans_parse_string(mem_ctx, t, value);
4379 if (t == NULL) {
4380 /* Line Ignored but not EOF */
4381 talloc_free(key->dptr);
4382 *key = tdb_null;
4383 return true;
4386 return true;
4390 * Update a persistent database as per file/stdin
4392 static int control_ptrans(struct ctdb_context *ctdb,
4393 int argc, const char **argv)
4395 const char *db_name;
4396 struct ctdb_db_context *ctdb_db;
4397 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4398 struct ctdb_transaction_handle *h;
4399 TDB_DATA key, value;
4400 FILE *file;
4401 int ret;
4403 if (argc < 1) {
4404 talloc_free(tmp_ctx);
4405 usage();
4408 file = stdin;
4409 if (argc == 2) {
4410 file = fopen(argv[1], "r");
4411 if (file == NULL) {
4412 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4413 talloc_free(tmp_ctx);
4414 return -1;
4418 db_name = argv[0];
4420 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4421 if (ctdb_db == NULL) {
4422 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4423 goto error;
4426 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4427 if (h == NULL) {
4428 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4429 goto error;
4432 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4433 if (key.dsize != 0) {
4434 ret = ctdb_transaction_store(h, key, value);
4435 /* Minimise memory use */
4436 talloc_free(key.dptr);
4437 if (value.dptr != NULL) {
4438 talloc_free(value.dptr);
4440 if (ret != 0) {
4441 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4442 ctdb_transaction_cancel(h);
4443 goto error;
4448 ret = ctdb_transaction_commit(h);
4449 if (ret != 0) {
4450 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4451 goto error;
4454 if (file != stdin) {
4455 fclose(file);
4457 talloc_free(tmp_ctx);
4458 return 0;
4460 error:
4461 if (file != stdin) {
4462 fclose(file);
4465 talloc_free(tmp_ctx);
4466 return -1;
4470 check if a service is bound to a port or not
4472 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4474 int s, ret;
4475 int v;
4476 int port;
4477 struct sockaddr_in sin;
4479 if (argc != 1) {
4480 printf("Use: ctdb chktcport <port>\n");
4481 return EINVAL;
4484 port = atoi(argv[0]);
4486 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4487 if (s == -1) {
4488 printf("Failed to open local socket\n");
4489 return errno;
4492 v = fcntl(s, F_GETFL, 0);
4493 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4494 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4497 bzero(&sin, sizeof(sin));
4498 sin.sin_family = PF_INET;
4499 sin.sin_port = htons(port);
4500 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4501 close(s);
4502 if (ret == -1) {
4503 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4504 return errno;
4507 return 0;
4511 /* Reload public IPs on a specified nodes */
4512 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4514 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4515 uint32_t *nodes;
4516 uint32_t pnn_mode;
4517 uint32_t timeout;
4518 int ret;
4520 assert_single_node_only();
4522 if (argc > 1) {
4523 usage();
4526 /* Determine the nodes where IPs need to be reloaded */
4527 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4528 options.pnn, true, &nodes, &pnn_mode)) {
4529 ret = -1;
4530 goto done;
4533 again:
4534 /* Disable takeover runs on all connected nodes. A reply
4535 * indicating success is needed from each node so all nodes
4536 * will need to be active. This will retry until maxruntime
4537 * is exceeded, hence no error handling.
4539 * A check could be added to not allow reloading of IPs when
4540 * there are disconnected nodes. However, this should
4541 * probably be left up to the administrator.
4543 timeout = LONGTIMEOUT;
4544 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4545 "Disable takeover runs", true);
4547 /* Now tell all the desired nodes to reload their public IPs.
4548 * Keep trying this until it succeeds. This assumes all
4549 * failures are transient, which might not be true...
4551 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4552 nodes, 0, LONGTIMELIMIT(),
4553 false, tdb_null,
4554 NULL, NULL, NULL) != 0) {
4555 DEBUG(DEBUG_ERR,
4556 ("Unable to reload IPs on some nodes, try again.\n"));
4557 goto again;
4560 /* It isn't strictly necessary to wait until takeover runs are
4561 * re-enabled but doing so can't hurt.
4563 timeout = 0;
4564 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4565 "Enable takeover runs", true);
4567 ipreallocate(ctdb);
4569 ret = 0;
4570 done:
4571 talloc_free(tmp_ctx);
4572 return ret;
4576 display a list of the databases on a remote ctdb
4578 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4580 int i, ret;
4581 struct ctdb_dbid_map *dbmap=NULL;
4583 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4584 if (ret != 0) {
4585 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4586 return ret;
4589 if(options.machinereadable){
4590 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4591 for(i=0;i<dbmap->num;i++){
4592 const char *path = NULL;
4593 const char *name = NULL;
4594 const char *health = NULL;
4595 bool persistent;
4596 bool readonly;
4597 bool sticky;
4599 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4600 dbmap->dbs[i].dbid, ctdb, &path);
4601 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4602 dbmap->dbs[i].dbid, ctdb, &name);
4603 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4604 dbmap->dbs[i].dbid, ctdb, &health);
4605 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4606 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4607 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4608 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4609 dbmap->dbs[i].dbid, name, path,
4610 !!(persistent), !!(sticky),
4611 !!(health), !!(readonly));
4613 return 0;
4616 printf("Number of databases:%d\n", dbmap->num);
4617 for(i=0;i<dbmap->num;i++){
4618 const char *path = NULL;
4619 const char *name = NULL;
4620 const char *health = NULL;
4621 bool persistent;
4622 bool readonly;
4623 bool sticky;
4625 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4626 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4627 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4628 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4629 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4630 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4631 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4632 dbmap->dbs[i].dbid, name, path,
4633 persistent?" PERSISTENT":"",
4634 sticky?" STICKY":"",
4635 readonly?" READONLY":"",
4636 health?" UNHEALTHY":"");
4639 return 0;
4643 display the status of a database on a remote ctdb
4645 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4647 const char *db_name;
4648 uint32_t db_id;
4649 uint8_t flags;
4650 const char *path = NULL;
4651 const char *health = NULL;
4653 if (argc < 1) {
4654 usage();
4657 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4658 return -1;
4661 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4662 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4663 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4664 db_id, db_name, path,
4665 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4666 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4667 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4668 (health ? health : "OK"));
4670 return 0;
4674 check if the local node is recmaster or not
4675 it will return 1 if this node is the recmaster and 0 if it is not
4676 or if the local ctdb daemon could not be contacted
4678 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4680 uint32_t mypnn, recmaster;
4681 int ret;
4683 assert_single_node_only();
4685 mypnn = getpnn(ctdb);
4687 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4688 if (ret != 0) {
4689 printf("Failed to get the recmaster\n");
4690 return 1;
4693 if (recmaster != mypnn) {
4694 printf("this node is not the recmaster\n");
4695 return 1;
4698 printf("this node is the recmaster\n");
4699 return 0;
4703 ping a node
4705 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4707 int ret;
4708 struct timeval tv = timeval_current();
4709 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4710 if (ret == -1) {
4711 printf("Unable to get ping response from node %u\n", options.pnn);
4712 return -1;
4713 } else {
4714 printf("response from %u time=%.6f sec (%d clients)\n",
4715 options.pnn, timeval_elapsed(&tv), ret);
4717 return 0;
4722 get a node's runstate
4724 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4726 int ret;
4727 enum ctdb_runstate runstate;
4729 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4730 if (ret == -1) {
4731 printf("Unable to get runstate response from node %u\n",
4732 options.pnn);
4733 return -1;
4734 } else {
4735 bool found = true;
4736 enum ctdb_runstate t;
4737 int i;
4738 for (i=0; i<argc; i++) {
4739 found = false;
4740 t = runstate_from_string(argv[i]);
4741 if (t == CTDB_RUNSTATE_UNKNOWN) {
4742 printf("Invalid run state (%s)\n", argv[i]);
4743 return -1;
4746 if (t == runstate) {
4747 found = true;
4748 break;
4752 if (!found) {
4753 printf("CTDB not in required run state (got %s)\n",
4754 runstate_to_string((enum ctdb_runstate)runstate));
4755 return -1;
4759 printf("%s\n", runstate_to_string(runstate));
4760 return 0;
4765 get a tunable
4767 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4769 const char *name;
4770 uint32_t value;
4771 int ret;
4773 if (argc < 1) {
4774 usage();
4777 name = argv[0];
4778 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4779 if (ret != 0) {
4780 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4781 return -1;
4784 printf("%-23s = %u\n", name, value);
4785 return 0;
4789 set a tunable
4791 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4793 const char *name;
4794 uint32_t value;
4795 int ret;
4797 if (argc < 2) {
4798 usage();
4801 name = argv[0];
4802 value = strtoul(argv[1], NULL, 0);
4804 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4805 if (ret == -1) {
4806 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4807 return -1;
4809 if (ret == 1) {
4810 DEBUG(DEBUG_WARNING,
4811 ("Setting obsolete tunable variable '%s'\n",
4812 name));
4814 return 0;
4818 list all tunables
4820 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4822 uint32_t count;
4823 const char **list;
4824 int ret, i;
4826 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4827 if (ret == -1) {
4828 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4829 return -1;
4832 for (i=0;i<count;i++) {
4833 control_getvar(ctdb, 1, &list[i]);
4836 talloc_free(list);
4838 return 0;
4842 display debug level on a node
4844 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4846 int ret;
4847 int32_t level;
4849 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4850 if (ret != 0) {
4851 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4852 return ret;
4853 } else {
4854 const char *desc = get_debug_by_level(level);
4855 if (desc == NULL) {
4856 /* This should never happen */
4857 desc = "Unknown";
4859 if (options.machinereadable){
4860 printm(":Name:Level:\n");
4861 printm(":%s:%d:\n", desc, level);
4862 } else {
4863 printf("Node %u is at debug level %s (%d)\n",
4864 options.pnn, desc, level);
4867 return 0;
4871 display reclock file of a node
4873 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4875 int ret;
4876 const char *reclock;
4878 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4879 if (ret != 0) {
4880 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4881 return ret;
4882 } else {
4883 if (options.machinereadable){
4884 if (reclock != NULL) {
4885 printm("%s", reclock);
4887 } else {
4888 if (reclock == NULL) {
4889 printf("No reclock file used.\n");
4890 } else {
4891 printf("Reclock file:%s\n", reclock);
4895 return 0;
4899 set the reclock file of a node
4901 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4903 int ret;
4904 const char *reclock;
4906 if (argc == 0) {
4907 reclock = NULL;
4908 } else if (argc == 1) {
4909 reclock = argv[0];
4910 } else {
4911 usage();
4914 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4915 if (ret != 0) {
4916 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4917 return ret;
4919 return 0;
4923 set the natgw state on/off
4925 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4927 int ret;
4928 uint32_t natgwstate;
4930 if (argc == 0) {
4931 usage();
4934 if (!strcmp(argv[0], "on")) {
4935 natgwstate = 1;
4936 } else if (!strcmp(argv[0], "off")) {
4937 natgwstate = 0;
4938 } else {
4939 usage();
4942 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4943 if (ret != 0) {
4944 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4945 return ret;
4948 return 0;
4952 set the lmaster role on/off
4954 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4956 int ret;
4957 uint32_t lmasterrole;
4959 if (argc == 0) {
4960 usage();
4963 if (!strcmp(argv[0], "on")) {
4964 lmasterrole = 1;
4965 } else if (!strcmp(argv[0], "off")) {
4966 lmasterrole = 0;
4967 } else {
4968 usage();
4971 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4972 if (ret != 0) {
4973 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4974 return ret;
4977 return 0;
4981 set the recmaster role on/off
4983 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4985 int ret;
4986 uint32_t recmasterrole;
4988 if (argc == 0) {
4989 usage();
4992 if (!strcmp(argv[0], "on")) {
4993 recmasterrole = 1;
4994 } else if (!strcmp(argv[0], "off")) {
4995 recmasterrole = 0;
4996 } else {
4997 usage();
5000 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5001 if (ret != 0) {
5002 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5003 return ret;
5006 return 0;
5010 set debug level on a node or all nodes
5012 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5014 int ret;
5015 int32_t level;
5017 if (argc == 0) {
5018 printf("You must specify the debug level. Valid levels are:\n");
5019 print_debug_levels(stdout);
5020 return 0;
5023 if (!parse_debug(argv[0], &level)) {
5024 printf("Invalid debug level, must be one of\n");
5025 print_debug_levels(stdout);
5026 return -1;
5029 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5030 if (ret != 0) {
5031 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5033 return 0;
5038 thaw a node
5040 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5042 int ret;
5043 uint32_t priority;
5045 if (argc == 1) {
5046 priority = strtol(argv[0], NULL, 0);
5047 } else {
5048 priority = 0;
5050 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5052 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5053 if (ret != 0) {
5054 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5056 return 0;
5061 attach to a database
5063 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5065 const char *db_name;
5066 struct ctdb_db_context *ctdb_db;
5067 bool persistent = false;
5069 if (argc < 1) {
5070 usage();
5072 db_name = argv[0];
5073 if (argc > 2) {
5074 usage();
5076 if (argc == 2) {
5077 if (strcmp(argv[1], "persistent") != 0) {
5078 usage();
5080 persistent = true;
5083 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5084 if (ctdb_db == NULL) {
5085 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5086 return -1;
5089 return 0;
5093 * detach from a database
5095 static int control_detach(struct ctdb_context *ctdb, int argc,
5096 const char **argv)
5098 uint32_t db_id;
5099 uint8_t flags;
5100 int ret, i, status = 0;
5101 struct ctdb_node_map *nodemap = NULL;
5102 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5103 uint32_t recmode;
5105 if (argc < 1) {
5106 usage();
5109 assert_single_node_only();
5111 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5112 &recmode);
5113 if (ret != 0) {
5114 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5115 "when recovery is active\n"));
5116 talloc_free(tmp_ctx);
5117 return -1;
5120 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5121 &nodemap);
5122 if (ret != 0) {
5123 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5124 options.pnn));
5125 talloc_free(tmp_ctx);
5126 return -1;
5129 for (i=0; i<nodemap->num; i++) {
5130 uint32_t value;
5132 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5133 continue;
5136 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5137 continue;
5140 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5141 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5142 "inactive (stopped or banned) node "
5143 "%u\n", nodemap->nodes[i].pnn));
5144 talloc_free(tmp_ctx);
5145 return -1;
5148 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5149 nodemap->nodes[i].pnn,
5150 "AllowClientDBAttach",
5151 &value);
5152 if (ret != 0) {
5153 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5154 "AllowClientDBAttach from node %u\n",
5155 nodemap->nodes[i].pnn));
5156 talloc_free(tmp_ctx);
5157 return -1;
5160 if (value == 1) {
5161 DEBUG(DEBUG_ERR, ("Database access is still active on "
5162 "node %u. Set AllowClientDBAttach=0 "
5163 "on all nodes.\n",
5164 nodemap->nodes[i].pnn));
5165 talloc_free(tmp_ctx);
5166 return -1;
5170 talloc_free(tmp_ctx);
5172 for (i=0; i<argc; i++) {
5173 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5174 continue;
5177 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5178 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5179 "cannot be detached\n", argv[i]));
5180 status = -1;
5181 continue;
5184 ret = ctdb_detach(ctdb, db_id);
5185 if (ret != 0) {
5186 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5187 argv[i]));
5188 status = ret;
5192 return status;
5196 set db priority
5198 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5200 struct ctdb_db_priority db_prio;
5201 int ret;
5203 if (argc < 2) {
5204 usage();
5207 db_prio.db_id = strtoul(argv[0], NULL, 0);
5208 db_prio.priority = strtoul(argv[1], NULL, 0);
5210 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5211 if (ret != 0) {
5212 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5213 return -1;
5216 return 0;
5220 get db priority
5222 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5224 uint32_t db_id, priority;
5225 int ret;
5227 if (argc < 1) {
5228 usage();
5231 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5232 return -1;
5235 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5236 if (ret != 0) {
5237 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5238 return -1;
5241 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5243 return 0;
5247 set the sticky records capability for a database
5249 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5251 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5252 uint32_t db_id;
5253 int ret;
5255 if (argc < 1) {
5256 usage();
5259 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5260 return -1;
5263 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5264 if (ret != 0) {
5265 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5266 talloc_free(tmp_ctx);
5267 return -1;
5270 talloc_free(tmp_ctx);
5271 return 0;
5275 set the readonly capability for a database
5277 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5279 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5280 uint32_t db_id;
5281 int ret;
5283 if (argc < 1) {
5284 usage();
5287 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5288 return -1;
5291 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5292 if (ret != 0) {
5293 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5294 talloc_free(tmp_ctx);
5295 return -1;
5298 talloc_free(tmp_ctx);
5299 return 0;
5303 get db seqnum
5305 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5307 uint32_t db_id;
5308 uint64_t seqnum;
5309 int ret;
5311 if (argc < 1) {
5312 usage();
5315 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5316 return -1;
5319 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5320 if (ret != 0) {
5321 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5322 return -1;
5325 printf("Sequence number:%lld\n", (long long)seqnum);
5327 return 0;
5331 run an eventscript on a node
5333 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5335 TDB_DATA data;
5336 int ret;
5337 int32_t res;
5338 char *errmsg;
5339 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5341 if (argc != 1) {
5342 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5343 return -1;
5346 data.dptr = (unsigned char *)discard_const(argv[0]);
5347 data.dsize = strlen((char *)data.dptr) + 1;
5349 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5351 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5352 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5353 if (ret != 0 || res != 0) {
5354 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5355 talloc_free(tmp_ctx);
5356 return -1;
5358 talloc_free(tmp_ctx);
5359 return 0;
5362 #define DB_VERSION 1
5363 #define MAX_DB_NAME 64
5364 struct db_file_header {
5365 unsigned long version;
5366 time_t timestamp;
5367 unsigned long persistent;
5368 unsigned long size;
5369 const char name[MAX_DB_NAME];
5372 struct backup_data {
5373 struct ctdb_marshall_buffer *records;
5374 uint32_t len;
5375 uint32_t total;
5376 bool traverse_error;
5379 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5381 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5382 struct ctdb_rec_data *rec;
5384 /* add the record */
5385 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5386 if (rec == NULL) {
5387 bd->traverse_error = true;
5388 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5389 return -1;
5391 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5392 if (bd->records == NULL) {
5393 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5394 bd->traverse_error = true;
5395 return -1;
5397 bd->records->count++;
5398 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5399 bd->len += rec->length;
5400 talloc_free(rec);
5402 bd->total++;
5403 return 0;
5407 * backup a database to a file
5409 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5411 const char *db_name;
5412 int ret;
5413 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5414 struct db_file_header dbhdr;
5415 struct ctdb_db_context *ctdb_db;
5416 struct backup_data *bd;
5417 int fh = -1;
5418 int status = -1;
5419 const char *reason = NULL;
5420 uint32_t db_id;
5421 uint8_t flags;
5423 assert_single_node_only();
5425 if (argc != 2) {
5426 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5427 return -1;
5430 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5431 return -1;
5434 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5435 db_id, tmp_ctx, &reason);
5436 if (ret != 0) {
5437 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5438 argv[0]));
5439 talloc_free(tmp_ctx);
5440 return -1;
5442 if (reason) {
5443 uint32_t allow_unhealthy = 0;
5445 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5446 "AllowUnhealthyDBRead",
5447 &allow_unhealthy);
5449 if (allow_unhealthy != 1) {
5450 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5451 argv[0], reason));
5453 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5454 allow_unhealthy));
5455 talloc_free(tmp_ctx);
5456 return -1;
5459 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5460 argv[0], argv[0]));
5461 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5462 "tunnable AllowUnhealthyDBRead = %u\n",
5463 allow_unhealthy));
5466 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5467 if (ctdb_db == NULL) {
5468 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5469 talloc_free(tmp_ctx);
5470 return -1;
5474 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5475 if (ret == -1) {
5476 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5477 talloc_free(tmp_ctx);
5478 return -1;
5482 bd = talloc_zero(tmp_ctx, struct backup_data);
5483 if (bd == NULL) {
5484 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5485 talloc_free(tmp_ctx);
5486 return -1;
5489 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5490 if (bd->records == NULL) {
5491 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5492 talloc_free(tmp_ctx);
5493 return -1;
5496 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5497 bd->records->db_id = ctdb_db->db_id;
5498 /* traverse the database collecting all records */
5499 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5500 bd->traverse_error) {
5501 DEBUG(DEBUG_ERR,("Traverse error\n"));
5502 talloc_free(tmp_ctx);
5503 return -1;
5506 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5509 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5510 if (fh == -1) {
5511 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5512 talloc_free(tmp_ctx);
5513 return -1;
5516 ZERO_STRUCT(dbhdr);
5517 dbhdr.version = DB_VERSION;
5518 dbhdr.timestamp = time(NULL);
5519 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5520 dbhdr.size = bd->len;
5521 if (strlen(argv[0]) >= MAX_DB_NAME) {
5522 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5523 goto done;
5525 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5526 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5527 if (ret == -1) {
5528 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5529 goto done;
5531 ret = sys_write(fh, bd->records, bd->len);
5532 if (ret == -1) {
5533 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5534 goto done;
5537 status = 0;
5538 done:
5539 if (fh != -1) {
5540 ret = close(fh);
5541 if (ret == -1) {
5542 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5546 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5548 talloc_free(tmp_ctx);
5549 return status;
5553 * restore a database from a file
5555 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5557 int ret;
5558 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5559 TDB_DATA outdata;
5560 TDB_DATA data;
5561 struct db_file_header dbhdr;
5562 struct ctdb_db_context *ctdb_db;
5563 struct ctdb_node_map *nodemap=NULL;
5564 struct ctdb_vnn_map *vnnmap=NULL;
5565 int i, fh;
5566 struct ctdb_control_wipe_database w;
5567 uint32_t *nodes;
5568 uint32_t generation;
5569 struct tm *tm;
5570 char tbuf[100];
5571 char *dbname;
5573 assert_single_node_only();
5575 if (argc < 1 || argc > 2) {
5576 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5577 return -1;
5580 fh = open(argv[0], O_RDONLY);
5581 if (fh == -1) {
5582 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5583 talloc_free(tmp_ctx);
5584 return -1;
5587 sys_read(fh, &dbhdr, sizeof(dbhdr));
5588 if (dbhdr.version != DB_VERSION) {
5589 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5590 close(fh);
5591 talloc_free(tmp_ctx);
5592 return -1;
5595 dbname = discard_const(dbhdr.name);
5596 if (argc == 2) {
5597 dbname = discard_const(argv[1]);
5600 outdata.dsize = dbhdr.size;
5601 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5602 if (outdata.dptr == NULL) {
5603 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5604 close(fh);
5605 talloc_free(tmp_ctx);
5606 return -1;
5608 sys_read(fh, outdata.dptr, outdata.dsize);
5609 close(fh);
5611 tm = localtime(&dbhdr.timestamp);
5612 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5613 printf("Restoring database '%s' from backup @ %s\n",
5614 dbname, tbuf);
5617 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5618 if (ctdb_db == NULL) {
5619 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5620 talloc_free(tmp_ctx);
5621 return -1;
5624 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5625 if (ret != 0) {
5626 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5627 talloc_free(tmp_ctx);
5628 return ret;
5632 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5633 if (ret != 0) {
5634 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5635 talloc_free(tmp_ctx);
5636 return ret;
5639 /* freeze all nodes */
5640 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5641 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5642 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5643 nodes, i,
5644 TIMELIMIT(),
5645 false, tdb_null,
5646 NULL, NULL,
5647 NULL) != 0) {
5648 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5649 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5650 talloc_free(tmp_ctx);
5651 return -1;
5655 generation = vnnmap->generation;
5656 data.dptr = (void *)&generation;
5657 data.dsize = sizeof(generation);
5659 /* start a cluster wide transaction */
5660 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5661 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5662 nodes, 0,
5663 TIMELIMIT(), false, data,
5664 NULL, NULL,
5665 NULL) != 0) {
5666 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5667 return -1;
5671 w.db_id = ctdb_db->db_id;
5672 w.transaction_id = generation;
5674 data.dptr = (void *)&w;
5675 data.dsize = sizeof(w);
5677 /* wipe all the remote databases. */
5678 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5679 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5680 nodes, 0,
5681 TIMELIMIT(), false, data,
5682 NULL, NULL,
5683 NULL) != 0) {
5684 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5685 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5686 talloc_free(tmp_ctx);
5687 return -1;
5690 /* push the database */
5691 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5692 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5693 nodes, 0,
5694 TIMELIMIT(), false, outdata,
5695 NULL, NULL,
5696 NULL) != 0) {
5697 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5698 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5699 talloc_free(tmp_ctx);
5700 return -1;
5703 data.dptr = (void *)&ctdb_db->db_id;
5704 data.dsize = sizeof(ctdb_db->db_id);
5706 /* mark the database as healthy */
5707 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5708 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5709 nodes, 0,
5710 TIMELIMIT(), false, data,
5711 NULL, NULL,
5712 NULL) != 0) {
5713 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5714 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5715 talloc_free(tmp_ctx);
5716 return -1;
5719 data.dptr = (void *)&generation;
5720 data.dsize = sizeof(generation);
5722 /* commit all the changes */
5723 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5724 nodes, 0,
5725 TIMELIMIT(), false, data,
5726 NULL, NULL,
5727 NULL) != 0) {
5728 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5729 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5730 talloc_free(tmp_ctx);
5731 return -1;
5735 /* thaw all nodes */
5736 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5737 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5738 nodes, 0,
5739 TIMELIMIT(),
5740 false, tdb_null,
5741 NULL, NULL,
5742 NULL) != 0) {
5743 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5744 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5745 talloc_free(tmp_ctx);
5746 return -1;
5750 talloc_free(tmp_ctx);
5751 return 0;
5755 * dump a database backup from a file
5757 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5759 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5760 TDB_DATA outdata;
5761 struct db_file_header dbhdr;
5762 int i, fh;
5763 struct tm *tm;
5764 char tbuf[100];
5765 struct ctdb_rec_data *rec = NULL;
5766 struct ctdb_marshall_buffer *m;
5767 struct ctdb_dump_db_context c;
5769 assert_single_node_only();
5771 if (argc != 1) {
5772 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5773 return -1;
5776 fh = open(argv[0], O_RDONLY);
5777 if (fh == -1) {
5778 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5779 talloc_free(tmp_ctx);
5780 return -1;
5783 sys_read(fh, &dbhdr, sizeof(dbhdr));
5784 if (dbhdr.version != DB_VERSION) {
5785 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5786 close(fh);
5787 talloc_free(tmp_ctx);
5788 return -1;
5791 outdata.dsize = dbhdr.size;
5792 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5793 if (outdata.dptr == NULL) {
5794 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5795 close(fh);
5796 talloc_free(tmp_ctx);
5797 return -1;
5799 sys_read(fh, outdata.dptr, outdata.dsize);
5800 close(fh);
5801 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5803 tm = localtime(&dbhdr.timestamp);
5804 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5805 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5806 dbhdr.name, m->db_id, tbuf);
5808 ZERO_STRUCT(c);
5809 c.f = stdout;
5810 c.printemptyrecords = (bool)options.printemptyrecords;
5811 c.printdatasize = (bool)options.printdatasize;
5812 c.printlmaster = false;
5813 c.printhash = (bool)options.printhash;
5814 c.printrecordflags = (bool)options.printrecordflags;
5816 for (i=0; i < m->count; i++) {
5817 uint32_t reqid = 0;
5818 TDB_DATA key, data;
5820 /* we do not want the header splitted, so we pass NULL*/
5821 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5822 NULL, &key, &data);
5824 ctdb_dumpdb_record(ctdb, key, data, &c);
5827 printf("Dumped %d records\n", i);
5828 talloc_free(tmp_ctx);
5829 return 0;
5833 * wipe a database from a file
5835 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5836 const char **argv)
5838 const char *db_name;
5839 int ret;
5840 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5841 TDB_DATA data;
5842 struct ctdb_db_context *ctdb_db;
5843 struct ctdb_node_map *nodemap = NULL;
5844 struct ctdb_vnn_map *vnnmap = NULL;
5845 int i;
5846 struct ctdb_control_wipe_database w;
5847 uint32_t *nodes;
5848 uint32_t generation;
5849 uint8_t flags;
5851 assert_single_node_only();
5853 if (argc != 1) {
5854 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5855 return -1;
5858 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5859 return -1;
5862 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5863 if (ctdb_db == NULL) {
5864 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5865 argv[0]));
5866 talloc_free(tmp_ctx);
5867 return -1;
5870 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5871 &nodemap);
5872 if (ret != 0) {
5873 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5874 options.pnn));
5875 talloc_free(tmp_ctx);
5876 return ret;
5879 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5880 &vnnmap);
5881 if (ret != 0) {
5882 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5883 options.pnn));
5884 talloc_free(tmp_ctx);
5885 return ret;
5888 /* freeze all nodes */
5889 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5890 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5891 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5892 nodes, i,
5893 TIMELIMIT(),
5894 false, tdb_null,
5895 NULL, NULL,
5896 NULL);
5897 if (ret != 0) {
5898 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5899 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5900 CTDB_RECOVERY_ACTIVE);
5901 talloc_free(tmp_ctx);
5902 return -1;
5906 generation = vnnmap->generation;
5907 data.dptr = (void *)&generation;
5908 data.dsize = sizeof(generation);
5910 /* start a cluster wide transaction */
5911 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5912 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5913 nodes, 0,
5914 TIMELIMIT(), false, data,
5915 NULL, NULL,
5916 NULL);
5917 if (ret!= 0) {
5918 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5919 "transactions.\n"));
5920 return -1;
5923 w.db_id = ctdb_db->db_id;
5924 w.transaction_id = generation;
5926 data.dptr = (void *)&w;
5927 data.dsize = sizeof(w);
5929 /* wipe all the remote databases. */
5930 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5931 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5932 nodes, 0,
5933 TIMELIMIT(), false, data,
5934 NULL, NULL,
5935 NULL) != 0) {
5936 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5937 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5938 talloc_free(tmp_ctx);
5939 return -1;
5942 data.dptr = (void *)&ctdb_db->db_id;
5943 data.dsize = sizeof(ctdb_db->db_id);
5945 /* mark the database as healthy */
5946 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5947 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5948 nodes, 0,
5949 TIMELIMIT(), false, data,
5950 NULL, NULL,
5951 NULL) != 0) {
5952 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5953 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5954 talloc_free(tmp_ctx);
5955 return -1;
5958 data.dptr = (void *)&generation;
5959 data.dsize = sizeof(generation);
5961 /* commit all the changes */
5962 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5963 nodes, 0,
5964 TIMELIMIT(), false, data,
5965 NULL, NULL,
5966 NULL) != 0) {
5967 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5968 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5969 talloc_free(tmp_ctx);
5970 return -1;
5973 /* thaw all nodes */
5974 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5975 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5976 nodes, 0,
5977 TIMELIMIT(),
5978 false, tdb_null,
5979 NULL, NULL,
5980 NULL) != 0) {
5981 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5982 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5983 talloc_free(tmp_ctx);
5984 return -1;
5987 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5989 talloc_free(tmp_ctx);
5990 return 0;
5994 dump memory usage
5996 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5998 TDB_DATA data;
5999 int ret;
6000 int32_t res;
6001 char *errmsg;
6002 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6003 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6004 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6005 if (ret != 0 || res != 0) {
6006 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6007 talloc_free(tmp_ctx);
6008 return -1;
6010 sys_write(1, data.dptr, data.dsize);
6011 talloc_free(tmp_ctx);
6012 return 0;
6016 handler for memory dumps
6018 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6019 TDB_DATA data, void *private_data)
6021 sys_write(1, data.dptr, data.dsize);
6022 exit(0);
6026 dump memory usage on the recovery daemon
6028 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6030 int ret;
6031 TDB_DATA data;
6032 struct srvid_request rd;
6034 rd.pnn = ctdb_get_pnn(ctdb);
6035 rd.srvid = getpid();
6037 /* register a message port for receiveing the reply so that we
6038 can receive the reply
6040 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6043 data.dptr = (uint8_t *)&rd;
6044 data.dsize = sizeof(rd);
6046 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6047 if (ret != 0) {
6048 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6049 return -1;
6052 /* this loop will terminate when we have received the reply */
6053 while (1) {
6054 event_loop_once(ctdb->ev);
6057 return 0;
6061 send a message to a srvid
6063 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6065 unsigned long srvid;
6066 int ret;
6067 TDB_DATA data;
6069 if (argc < 2) {
6070 usage();
6073 srvid = strtoul(argv[0], NULL, 0);
6075 data.dptr = (uint8_t *)discard_const(argv[1]);
6076 data.dsize= strlen(argv[1]);
6078 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6079 if (ret != 0) {
6080 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6081 return -1;
6084 return 0;
6088 handler for msglisten
6090 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6091 TDB_DATA data, void *private_data)
6093 int i;
6095 printf("Message received: ");
6096 for (i=0;i<data.dsize;i++) {
6097 printf("%c", data.dptr[i]);
6099 printf("\n");
6103 listen for messages on a messageport
6105 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6107 uint64_t srvid;
6109 srvid = getpid();
6111 /* register a message port and listen for messages
6113 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6114 printf("Listening for messages on srvid:%d\n", (int)srvid);
6116 while (1) {
6117 event_loop_once(ctdb->ev);
6120 return 0;
6124 list all nodes in the cluster
6125 we parse the nodes file directly
6127 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6129 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6130 struct ctdb_node_map *node_map;
6131 int i;
6133 assert_single_node_only();
6135 node_map = read_nodes_file(mem_ctx);
6136 if (node_map == NULL) {
6137 talloc_free(mem_ctx);
6138 return -1;
6141 for (i = 0; i < node_map->num; i++) {
6142 const char *addr;
6144 if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
6145 continue;
6147 addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
6148 if (options.machinereadable){
6149 printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
6150 } else {
6151 printf("%s\n", addr);
6154 talloc_free(mem_ctx);
6156 return 0;
6159 /**********************************************************************/
6160 /* reload the nodes file on all nodes */
6162 static void get_nodes_files_callback(struct ctdb_context *ctdb,
6163 uint32_t node_pnn, int32_t res,
6164 TDB_DATA outdata, void *callback_data)
6166 struct ctdb_node_map **maps =
6167 talloc_get_type(callback_data, struct ctdb_node_map *);
6169 if (outdata.dsize < offsetof(struct ctdb_node_map, nodes) ||
6170 outdata.dptr == NULL) {
6171 DEBUG(DEBUG_ERR,
6172 (__location__ " Invalid return data: %u %p\n",
6173 (unsigned)outdata.dsize, outdata.dptr));
6174 return;
6177 if (node_pnn >= talloc_array_length(maps)) {
6178 DEBUG(DEBUG_ERR,
6179 (__location__ " unexpected PNN %u\n", node_pnn));
6180 return;
6183 maps[node_pnn] = talloc_memdup(maps, outdata.dptr, outdata.dsize);
6186 static void get_nodes_files_fail_callback(struct ctdb_context *ctdb,
6187 uint32_t node_pnn, int32_t res,
6188 TDB_DATA outdata, void *callback_data)
6190 DEBUG(DEBUG_ERR,
6191 ("ERROR: Failed to get nodes file from node %u\n", node_pnn));
6194 static struct ctdb_node_map **
6195 ctdb_get_nodes_files(struct ctdb_context *ctdb,
6196 TALLOC_CTX *mem_ctx,
6197 struct timeval timeout,
6198 struct ctdb_node_map *nodemap)
6200 uint32_t *nodes;
6201 int ret;
6202 struct ctdb_node_map **maps;
6204 maps = talloc_zero_array(mem_ctx, struct ctdb_node_map *, nodemap->num);
6205 CTDB_NO_MEMORY_NULL(ctdb, maps);
6207 nodes = list_of_connected_nodes(ctdb, nodemap, mem_ctx, true);
6209 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_NODES_FILE,
6210 nodes, 0, TIMELIMIT(),
6211 true, tdb_null,
6212 get_nodes_files_callback,
6213 get_nodes_files_fail_callback,
6214 maps);
6215 if (ret != 0) {
6216 talloc_free(maps);
6217 return NULL;
6220 return maps;
6223 static bool node_files_are_identical(struct ctdb_node_map *nm1,
6224 struct ctdb_node_map *nm2)
6226 int i;
6228 if (nm1->num != nm2->num) {
6229 return false;
6231 for (i = 0; i < nm1->num; i++) {
6232 if (memcmp(&nm1->nodes[i], &nm2->nodes[i],
6233 sizeof(struct ctdb_node_and_flags)) != 0) {
6234 return false;
6238 return true;
6241 static bool check_all_node_files_are_identical(struct ctdb_context *ctdb,
6242 TALLOC_CTX *mem_ctx,
6243 struct timeval timeout,
6244 struct ctdb_node_map *nodemap,
6245 struct ctdb_node_map *file_nodemap)
6247 static struct ctdb_node_map **maps;
6248 int i;
6249 bool ret = true;
6251 maps = ctdb_get_nodes_files(ctdb, mem_ctx, timeout, nodemap);
6252 if (maps == NULL) {
6253 return false;
6256 for (i = 0; i < talloc_array_length(maps); i++) {
6257 if (maps[i] == NULL) {
6258 continue;
6260 if (!node_files_are_identical(file_nodemap, maps[i])) {
6261 DEBUG(DEBUG_ERR,
6262 ("ERROR: Node file on node %u differs from current node (%u)\n",
6263 i, ctdb_get_pnn(ctdb)));
6264 ret = false;
6268 return ret;
6272 reload the nodes file on the local node
6274 static bool sanity_check_nodes_file_changes(TALLOC_CTX *mem_ctx,
6275 struct ctdb_node_map *nodemap,
6276 struct ctdb_node_map *file_nodemap)
6278 int i;
6279 bool should_abort = false;
6280 bool have_changes = false;
6282 for (i=0; i<nodemap->num; i++) {
6283 if (i >= file_nodemap->num) {
6284 DEBUG(DEBUG_ERR,
6285 ("ERROR: Node %u (%s) missing from nodes file\n",
6286 nodemap->nodes[i].pnn,
6287 ctdb_addr_to_str(&nodemap->nodes[i].addr)));
6288 should_abort = true;
6289 continue;
6291 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED &&
6292 file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6293 /* Node remains deleted */
6294 DEBUG(DEBUG_INFO,
6295 ("Node %u is unchanged (DELETED)\n",
6296 nodemap->nodes[i].pnn));
6297 } else if (!(nodemap->nodes[i].flags & NODE_FLAGS_DELETED) &&
6298 !(file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED)) {
6299 /* Node not newly nor previously deleted */
6300 if (!ctdb_same_ip(&nodemap->nodes[i].addr,
6301 &file_nodemap->nodes[i].addr)) {
6302 DEBUG(DEBUG_ERR,
6303 ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
6304 nodemap->nodes[i].pnn,
6305 /* ctdb_addr_to_str() returns a static */
6306 talloc_strdup(mem_ctx,
6307 ctdb_addr_to_str(&nodemap->nodes[i].addr)),
6308 ctdb_addr_to_str(&file_nodemap->nodes[i].addr)));
6309 should_abort = true;
6310 } else {
6311 DEBUG(DEBUG_INFO,
6312 ("Node %u is unchanged\n",
6313 nodemap->nodes[i].pnn));
6314 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
6315 DEBUG(DEBUG_WARNING,
6316 ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
6317 nodemap->nodes[i].pnn));
6320 } else if (file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6321 /* Node is being deleted */
6322 DEBUG(DEBUG_NOTICE,
6323 ("Node %u is DELETED\n",
6324 nodemap->nodes[i].pnn));
6325 have_changes = true;
6326 if (!(nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
6327 DEBUG(DEBUG_ERR,
6328 ("ERROR: Node %u is still connected\n",
6329 nodemap->nodes[i].pnn));
6330 should_abort = true;
6332 } else if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
6333 /* Node was previously deleted */
6334 DEBUG(DEBUG_NOTICE,
6335 ("Node %u is UNDELETED\n", nodemap->nodes[i].pnn));
6336 have_changes = true;
6340 if (should_abort) {
6341 DEBUG(DEBUG_ERR,
6342 ("ERROR: Nodes will not be reloaded due to previous error\n"));
6343 talloc_free(mem_ctx);
6344 exit(1);
6347 /* Leftover nodes in file are NEW */
6348 for (; i < file_nodemap->num; i++) {
6349 DEBUG(DEBUG_NOTICE, ("Node %u is NEW\n",
6350 file_nodemap->nodes[i].pnn));
6351 have_changes = true;
6354 return have_changes;
6357 static void reload_nodes_fail_callback(struct ctdb_context *ctdb,
6358 uint32_t node_pnn, int32_t res,
6359 TDB_DATA outdata, void *callback_data)
6361 DEBUG(DEBUG_WARNING,
6362 ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
6363 node_pnn));
6366 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6368 int i, ret;
6369 struct ctdb_node_map *nodemap=NULL;
6370 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
6371 struct ctdb_node_map *file_nodemap;
6372 uint32_t *conn;
6373 uint32_t timeout;
6375 assert_current_node_only(ctdb);
6377 /* Load both the current nodemap and the contents of the local
6378 * nodes file. Compare and sanity check them before doing
6379 * anything. */
6381 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6382 if (ret != 0) {
6383 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6384 return ret;
6387 file_nodemap = read_nodes_file(tmp_ctx);
6388 if (file_nodemap == NULL) {
6389 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6390 talloc_free(tmp_ctx);
6391 return -1;
6394 if (!check_all_node_files_are_identical(ctdb, tmp_ctx, TIMELIMIT(),
6395 nodemap, file_nodemap)) {
6396 return -1;
6399 if (!sanity_check_nodes_file_changes(tmp_ctx, nodemap, file_nodemap)) {
6400 DEBUG(DEBUG_NOTICE,
6401 ("No change in nodes file, skipping unnecessary reload\n"));
6402 talloc_free(tmp_ctx);
6403 return 0;
6406 /* Now make the changes */
6407 conn = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
6408 for (i = 0; i < talloc_array_length(conn); i++) {
6409 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n",
6410 conn[i]));
6413 /* Another timeout could be used, such as ReRecoveryTimeout or
6414 * a new one for this purpose. However, this is the simplest
6415 * option. */
6416 timeout = options.timelimit;
6417 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6418 "Disable recoveries", true);
6421 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_NODES_FILE,
6422 conn, 0, TIMELIMIT(),
6423 true, tdb_null,
6424 NULL, reload_nodes_fail_callback,
6425 NULL);
6427 timeout = 0;
6428 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
6429 "Enable recoveries", true);
6431 talloc_free(tmp_ctx);
6433 return 0;
6437 static const struct {
6438 const char *name;
6439 int (*fn)(struct ctdb_context *, int, const char **);
6440 bool auto_all;
6441 bool without_daemon; /* can be run without daemon running ? */
6442 const char *msg;
6443 const char *args;
6444 } ctdb_commands[] = {
6445 { "version", control_version, true, true, "show version of ctdb" },
6446 { "status", control_status, true, false, "show node status" },
6447 { "uptime", control_uptime, true, false, "show node uptime" },
6448 { "ping", control_ping, true, false, "ping all nodes" },
6449 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6450 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6451 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6452 { "listvars", control_listvars, true, false, "list tunable variables"},
6453 { "statistics", control_statistics, false, false, "show statistics" },
6454 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6455 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6456 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6457 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6458 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6459 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6460 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6461 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6462 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6463 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6464 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6465 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6466 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6467 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6468 { "lvs", control_lvs, true, false, "show lvs configuration" },
6469 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6470 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6471 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6472 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6473 { "getdebug", control_getdebug, true, false, "get debug level" },
6474 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6475 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6476 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6477 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6478 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6479 { "disable", control_disable, true, false, "disable a nodes public IP" },
6480 { "enable", control_enable, true, false, "enable a nodes public IP" },
6481 { "stop", control_stop, true, false, "stop a node" },
6482 { "continue", control_continue, true, false, "re-start a stopped node" },
6483 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6484 { "unban", control_unban, true, false, "unban a node" },
6485 { "showban", control_showban, true, false, "show ban information"},
6486 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6487 { "recover", control_recover, true, false, "force recovery" },
6488 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6489 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6490 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6491 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6492 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6493 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6494 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6495 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6496 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6498 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6500 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6501 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6502 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6503 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6504 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6505 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6506 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6507 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6508 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6509 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6510 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6511 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6512 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6513 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6514 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6515 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6516 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6517 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6518 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6519 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6520 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6521 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6522 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6523 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6524 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6525 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6526 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6527 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6528 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6529 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6530 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6531 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6532 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6533 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6534 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6535 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6536 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6537 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6538 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6539 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6540 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6541 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6542 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6543 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6544 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6545 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6546 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6547 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6548 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6552 show usage message
6554 static void usage(void)
6556 int i;
6557 printf(
6558 "Usage: ctdb [options] <control>\n" \
6559 "Options:\n" \
6560 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6561 " -Y generate machine readable output\n"
6562 " -x <char> specify delimiter for machine readable output\n"
6563 " -v generate verbose output\n"
6564 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6565 printf("Controls:\n");
6566 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6567 printf(" %-15s %-27s %s\n",
6568 ctdb_commands[i].name,
6569 ctdb_commands[i].args?ctdb_commands[i].args:"",
6570 ctdb_commands[i].msg);
6572 exit(1);
6576 static void ctdb_alarm(int sig)
6578 printf("Maximum runtime exceeded - exiting\n");
6579 _exit(ERR_TIMEOUT);
6583 main program
6585 int main(int argc, const char *argv[])
6587 struct ctdb_context *ctdb;
6588 char *nodestring = NULL;
6589 int machineparsable = 0;
6590 struct poptOption popt_options[] = {
6591 POPT_AUTOHELP
6592 POPT_CTDB_CMDLINE
6593 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6594 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6595 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
6596 { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
6597 { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
6598 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6599 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6600 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6601 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6602 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6603 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6604 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6605 POPT_TABLEEND
6607 int opt;
6608 const char **extra_argv;
6609 int extra_argc = 0;
6610 int ret=-1, i;
6611 poptContext pc;
6612 struct event_context *ev;
6613 const char *control;
6615 setlinebuf(stdout);
6617 /* set some defaults */
6618 options.maxruntime = 0;
6619 options.timelimit = 10;
6620 options.pnn = CTDB_CURRENT_NODE;
6622 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6624 while ((opt = poptGetNextOpt(pc)) != -1) {
6625 switch (opt) {
6626 default:
6627 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6628 poptBadOption(pc, 0), poptStrerror(opt)));
6629 exit(1);
6633 /* setup the remaining options for the main program to use */
6634 extra_argv = poptGetArgs(pc);
6635 if (extra_argv) {
6636 extra_argv++;
6637 while (extra_argv[extra_argc]) extra_argc++;
6640 if (extra_argc < 1) {
6641 usage();
6644 if (options.maxruntime == 0) {
6645 const char *ctdb_timeout;
6646 ctdb_timeout = getenv("CTDB_TIMEOUT");
6647 if (ctdb_timeout != NULL) {
6648 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6649 } else {
6650 /* default timeout is 120 seconds */
6651 options.maxruntime = 120;
6655 if (machineparsable) {
6656 options.machineseparator = "|";
6658 if (options.machineseparator != NULL) {
6659 if (strlen(options.machineseparator) != 1) {
6660 printf("Invalid separator \"%s\" - "
6661 "must be single character\n",
6662 options.machineseparator);
6663 exit(1);
6666 /* -x implies -Y */
6667 options.machinereadable = true;
6668 } else if (options.machinereadable) {
6669 options.machineseparator = ":";
6672 signal(SIGALRM, ctdb_alarm);
6673 alarm(options.maxruntime);
6675 control = extra_argv[0];
6677 /* Default value for CTDB_BASE - don't override */
6678 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6680 ev = event_context_init(NULL);
6681 if (!ev) {
6682 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6683 exit(1);
6686 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6687 if (strcmp(control, ctdb_commands[i].name) == 0) {
6688 break;
6692 if (i == ARRAY_SIZE(ctdb_commands)) {
6693 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6694 exit(1);
6697 if (ctdb_commands[i].without_daemon == true) {
6698 if (nodestring != NULL) {
6699 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6700 exit(1);
6702 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6705 /* initialise ctdb */
6706 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6708 if (ctdb == NULL) {
6709 uint32_t pnn;
6710 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6712 pnn = find_node_xpnn();
6713 if (pnn == -1) {
6714 DEBUG(DEBUG_ERR,
6715 ("Is this node part of a CTDB cluster?\n"));
6717 exit(1);
6720 /* setup the node number(s) to contact */
6721 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6722 &options.nodes, &options.pnn)) {
6723 usage();
6726 if (options.pnn == CTDB_CURRENT_NODE) {
6727 options.pnn = options.nodes[0];
6730 if (ctdb_commands[i].auto_all &&
6731 ((options.pnn == CTDB_BROADCAST_ALL) ||
6732 (options.pnn == CTDB_MULTICAST))) {
6733 int j;
6735 ret = 0;
6736 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6737 options.pnn = options.nodes[j];
6738 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6740 } else {
6741 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6744 talloc_free(ctdb);
6745 talloc_free(ev);
6746 (void)poptFreeContext(pc);
6748 return ret;