s3: leases: libsmbsharemodes no longer works with SMB2 leases inside our locking...
[Samba.git] / ctdb / tools / ctdb.c
blob5d5ce3cc9ea7653790f0d4b2d4a113a410699b22
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 int verbose;
47 int maxruntime;
48 int printemptyrecords;
49 int printdatasize;
50 int printlmaster;
51 int printhash;
52 int printrecordflags;
53 } options;
55 #define LONGTIMEOUT options.timelimit*10
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
60 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
62 return (tv2->tv_sec - tv->tv_sec) +
63 (tv2->tv_usec - tv->tv_usec)*1.0e-6;
66 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
68 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
69 return 0;
72 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
73 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
74 "Out of memory in " __location__ )); \
75 abort(); \
76 }} while (0)
78 static uint32_t getpnn(struct ctdb_context *ctdb)
80 if ((options.pnn == CTDB_BROADCAST_ALL) ||
81 (options.pnn == CTDB_MULTICAST)) {
82 DEBUG(DEBUG_ERR,
83 ("Cannot get PNN for node %u\n", options.pnn));
84 exit(1);
87 if (options.pnn == CTDB_CURRENT_NODE) {
88 return ctdb_get_pnn(ctdb);
89 } else {
90 return options.pnn;
94 static void assert_single_node_only(void)
96 if ((options.pnn == CTDB_BROADCAST_ALL) ||
97 (options.pnn == CTDB_MULTICAST)) {
98 DEBUG(DEBUG_ERR,
99 ("This control can not be applied to multiple PNNs\n"));
100 exit(1);
104 /* Pretty print the flags to a static buffer in human-readable format.
105 * This never returns NULL!
107 static const char *pretty_print_flags(uint32_t flags)
109 int j;
110 static const struct {
111 uint32_t flag;
112 const char *name;
113 } flag_names[] = {
114 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
115 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
116 { NODE_FLAGS_BANNED, "BANNED" },
117 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
118 { NODE_FLAGS_DELETED, "DELETED" },
119 { NODE_FLAGS_STOPPED, "STOPPED" },
120 { NODE_FLAGS_INACTIVE, "INACTIVE" },
122 static char flags_str[512]; /* Big enough to contain all flag names */
124 flags_str[0] = '\0';
125 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
126 if (flags & flag_names[j].flag) {
127 if (flags_str[0] == '\0') {
128 (void) strcpy(flags_str, flag_names[j].name);
129 } else {
130 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
131 (void) strncat(flags_str, flag_names[j].name,
132 sizeof(flags_str)-1);
136 if (flags_str[0] == '\0') {
137 (void) strcpy(flags_str, "OK");
140 return flags_str;
143 static int h2i(char h)
145 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
146 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
147 return h - '0';
150 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
152 int i, len;
153 TDB_DATA key = {NULL, 0};
155 len = strlen(str);
156 if (len & 0x01) {
157 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
158 return key;
161 key.dsize = len>>1;
162 key.dptr = talloc_size(mem_ctx, key.dsize);
164 for (i=0; i < len/2; i++) {
165 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
167 return key;
170 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
171 * that are disconnected or deleted. If dd_ok is true those nodes are
172 * included in the output list of nodes. If dd_ok is false, those
173 * nodes are filtered from the "all" case and cause an error if
174 * explicitly specified.
176 static bool parse_nodestring(struct ctdb_context *ctdb,
177 TALLOC_CTX *mem_ctx,
178 const char * nodestring,
179 uint32_t current_pnn,
180 bool dd_ok,
181 uint32_t **nodes,
182 uint32_t *pnn_mode)
184 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
185 int n;
186 uint32_t i;
187 struct ctdb_node_map *nodemap;
188 int ret;
190 *nodes = NULL;
192 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
193 if (ret != 0) {
194 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
195 talloc_free(tmp_ctx);
196 exit(10);
199 if (nodestring != NULL) {
200 *nodes = talloc_array(mem_ctx, uint32_t, 0);
201 if (*nodes == NULL) {
202 goto failed;
205 n = 0;
207 if (strcmp(nodestring, "all") == 0) {
208 *pnn_mode = CTDB_BROADCAST_ALL;
210 /* all */
211 for (i = 0; i < nodemap->num; i++) {
212 if ((nodemap->nodes[i].flags &
213 (NODE_FLAGS_DISCONNECTED |
214 NODE_FLAGS_DELETED)) && !dd_ok) {
215 continue;
217 *nodes = talloc_realloc(mem_ctx, *nodes,
218 uint32_t, n+1);
219 if (*nodes == NULL) {
220 goto failed;
222 (*nodes)[n] = i;
223 n++;
225 } else {
226 /* x{,y...} */
227 char *ns, *tok;
229 ns = talloc_strdup(tmp_ctx, nodestring);
230 tok = strtok(ns, ",");
231 while (tok != NULL) {
232 uint32_t pnn;
233 char *endptr;
234 i = (uint32_t)strtoul(tok, &endptr, 0);
235 if (i == 0 && tok == endptr) {
236 DEBUG(DEBUG_ERR,
237 ("Invalid node %s\n", tok));
238 talloc_free(tmp_ctx);
239 exit(ERR_NONODE);
241 if (i >= nodemap->num) {
242 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
243 talloc_free(tmp_ctx);
244 exit(ERR_NONODE);
246 if ((nodemap->nodes[i].flags &
247 (NODE_FLAGS_DISCONNECTED |
248 NODE_FLAGS_DELETED)) && !dd_ok) {
249 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
250 talloc_free(tmp_ctx);
251 exit(ERR_DISNODE);
253 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
254 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
255 talloc_free(tmp_ctx);
256 exit(10);
259 *nodes = talloc_realloc(mem_ctx, *nodes,
260 uint32_t, n+1);
261 if (*nodes == NULL) {
262 goto failed;
265 (*nodes)[n] = i;
266 n++;
268 tok = strtok(NULL, ",");
270 talloc_free(ns);
272 if (n == 1) {
273 *pnn_mode = (*nodes)[0];
274 } else {
275 *pnn_mode = CTDB_MULTICAST;
278 } else {
279 /* default - no nodes specified */
280 *nodes = talloc_array(mem_ctx, uint32_t, 1);
281 if (*nodes == NULL) {
282 goto failed;
284 *pnn_mode = CTDB_CURRENT_NODE;
286 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
287 goto failed;
291 talloc_free(tmp_ctx);
292 return true;
294 failed:
295 talloc_free(tmp_ctx);
296 return false;
300 check if a database exists
302 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
303 uint32_t *dbid, const char **dbname, uint8_t *flags)
305 int i, ret;
306 struct ctdb_dbid_map *dbmap=NULL;
307 bool dbid_given = false, found = false;
308 uint32_t id;
309 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
310 const char *name;
312 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
313 if (ret != 0) {
314 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
315 goto fail;
318 if (strncmp(dbarg, "0x", 2) == 0) {
319 id = strtoul(dbarg, NULL, 0);
320 dbid_given = true;
323 for(i=0; i<dbmap->num; i++) {
324 if (dbid_given) {
325 if (id == dbmap->dbs[i].dbid) {
326 found = true;
327 break;
329 } else {
330 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
331 if (ret != 0) {
332 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
333 goto fail;
336 if (strcmp(name, dbarg) == 0) {
337 id = dbmap->dbs[i].dbid;
338 found = true;
339 break;
344 if (found && dbid_given && dbname != NULL) {
345 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
346 if (ret != 0) {
347 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
348 found = false;
349 goto fail;
353 if (found) {
354 if (dbid) *dbid = id;
355 if (dbname) *dbname = talloc_strdup(ctdb, name);
356 if (flags) *flags = dbmap->dbs[i].flags;
357 } else {
358 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
361 fail:
362 talloc_free(tmp_ctx);
363 return found;
367 see if a process exists
369 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
371 uint32_t pnn, pid;
372 int ret;
373 if (argc < 1) {
374 usage();
377 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
378 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
379 return -1;
382 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
383 if (ret == 0) {
384 printf("%u:%u exists\n", pnn, pid);
385 } else {
386 printf("%u:%u does not exist\n", pnn, pid);
388 return ret;
392 display statistics structure
394 static void show_statistics(struct ctdb_statistics *s, int show_header)
396 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
397 int i;
398 const char *prefix=NULL;
399 int preflen=0;
400 int tmp, days, hours, minutes, seconds;
401 const struct {
402 const char *name;
403 uint32_t offset;
404 } fields[] = {
405 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
406 STATISTICS_FIELD(num_clients),
407 STATISTICS_FIELD(frozen),
408 STATISTICS_FIELD(recovering),
409 STATISTICS_FIELD(num_recoveries),
410 STATISTICS_FIELD(client_packets_sent),
411 STATISTICS_FIELD(client_packets_recv),
412 STATISTICS_FIELD(node_packets_sent),
413 STATISTICS_FIELD(node_packets_recv),
414 STATISTICS_FIELD(keepalive_packets_sent),
415 STATISTICS_FIELD(keepalive_packets_recv),
416 STATISTICS_FIELD(node.req_call),
417 STATISTICS_FIELD(node.reply_call),
418 STATISTICS_FIELD(node.req_dmaster),
419 STATISTICS_FIELD(node.reply_dmaster),
420 STATISTICS_FIELD(node.reply_error),
421 STATISTICS_FIELD(node.req_message),
422 STATISTICS_FIELD(node.req_control),
423 STATISTICS_FIELD(node.reply_control),
424 STATISTICS_FIELD(client.req_call),
425 STATISTICS_FIELD(client.req_message),
426 STATISTICS_FIELD(client.req_control),
427 STATISTICS_FIELD(timeouts.call),
428 STATISTICS_FIELD(timeouts.control),
429 STATISTICS_FIELD(timeouts.traverse),
430 STATISTICS_FIELD(locks.num_calls),
431 STATISTICS_FIELD(locks.num_current),
432 STATISTICS_FIELD(locks.num_pending),
433 STATISTICS_FIELD(locks.num_failed),
434 STATISTICS_FIELD(total_calls),
435 STATISTICS_FIELD(pending_calls),
436 STATISTICS_FIELD(childwrite_calls),
437 STATISTICS_FIELD(pending_childwrite_calls),
438 STATISTICS_FIELD(memory_used),
439 STATISTICS_FIELD(max_hop_count),
440 STATISTICS_FIELD(total_ro_delegations),
441 STATISTICS_FIELD(total_ro_revokes),
444 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
445 seconds = tmp%60;
446 tmp /= 60;
447 minutes = tmp%60;
448 tmp /= 60;
449 hours = tmp%24;
450 tmp /= 24;
451 days = tmp;
453 if (options.machinereadable){
454 if (show_header) {
455 printf("CTDB version:");
456 printf("Current time of statistics:");
457 printf("Statistics collected since:");
458 for (i=0;i<ARRAY_SIZE(fields);i++) {
459 printf("%s:", fields[i].name);
461 printf("num_reclock_ctdbd_latency:");
462 printf("min_reclock_ctdbd_latency:");
463 printf("avg_reclock_ctdbd_latency:");
464 printf("max_reclock_ctdbd_latency:");
466 printf("num_reclock_recd_latency:");
467 printf("min_reclock_recd_latency:");
468 printf("avg_reclock_recd_latency:");
469 printf("max_reclock_recd_latency:");
471 printf("num_call_latency:");
472 printf("min_call_latency:");
473 printf("avg_call_latency:");
474 printf("max_call_latency:");
476 printf("num_lockwait_latency:");
477 printf("min_lockwait_latency:");
478 printf("avg_lockwait_latency:");
479 printf("max_lockwait_latency:");
481 printf("num_childwrite_latency:");
482 printf("min_childwrite_latency:");
483 printf("avg_childwrite_latency:");
484 printf("max_childwrite_latency:");
485 printf("\n");
487 printf("%d:", CTDB_PROTOCOL);
488 printf("%d:", (int)s->statistics_current_time.tv_sec);
489 printf("%d:", (int)s->statistics_start_time.tv_sec);
490 for (i=0;i<ARRAY_SIZE(fields);i++) {
491 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
493 printf("%d:", s->reclock.ctdbd.num);
494 printf("%.6f:", s->reclock.ctdbd.min);
495 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
496 printf("%.6f:", s->reclock.ctdbd.max);
498 printf("%d:", s->reclock.recd.num);
499 printf("%.6f:", s->reclock.recd.min);
500 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
501 printf("%.6f:", s->reclock.recd.max);
503 printf("%d:", s->call_latency.num);
504 printf("%.6f:", s->call_latency.min);
505 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
506 printf("%.6f:", s->call_latency.max);
508 printf("%d:", s->childwrite_latency.num);
509 printf("%.6f:", s->childwrite_latency.min);
510 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
511 printf("%.6f:", s->childwrite_latency.max);
512 printf("\n");
513 } else {
514 printf("CTDB version %u\n", CTDB_PROTOCOL);
515 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
516 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
518 for (i=0;i<ARRAY_SIZE(fields);i++) {
519 if (strchr(fields[i].name, '.')) {
520 preflen = strcspn(fields[i].name, ".")+1;
521 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
522 prefix = fields[i].name;
523 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
525 } else {
526 preflen = 0;
528 printf(" %*s%-22s%*s%10u\n",
529 preflen?4:0, "",
530 fields[i].name+preflen,
531 preflen?0:4, "",
532 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
534 printf(" hop_count_buckets:");
535 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
536 printf(" %d", s->hop_count_bucket[i]);
538 printf("\n");
539 printf(" lock_buckets:");
540 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
541 printf(" %d", s->locks.buckets[i]);
543 printf("\n");
544 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);
546 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);
548 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);
550 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);
551 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);
554 talloc_free(tmp_ctx);
558 display remote ctdb statistics combined from all nodes
560 static int control_statistics_all(struct ctdb_context *ctdb)
562 int ret, i;
563 struct ctdb_statistics statistics;
564 uint32_t *nodes;
565 uint32_t num_nodes;
567 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
568 CTDB_NO_MEMORY(ctdb, nodes);
570 ZERO_STRUCT(statistics);
572 for (i=0;i<num_nodes;i++) {
573 struct ctdb_statistics s1;
574 int j;
575 uint32_t *v1 = (uint32_t *)&s1;
576 uint32_t *v2 = (uint32_t *)&statistics;
577 uint32_t num_ints =
578 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
579 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
580 if (ret != 0) {
581 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
582 return ret;
584 for (j=0;j<num_ints;j++) {
585 v2[j] += v1[j];
587 statistics.max_hop_count =
588 MAX(statistics.max_hop_count, s1.max_hop_count);
589 statistics.call_latency.max =
590 MAX(statistics.call_latency.max, s1.call_latency.max);
592 talloc_free(nodes);
593 printf("Gathered statistics for %u nodes\n", num_nodes);
594 show_statistics(&statistics, 1);
595 return 0;
599 display remote ctdb statistics
601 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
603 int ret;
604 struct ctdb_statistics statistics;
606 if (options.pnn == CTDB_BROADCAST_ALL) {
607 return control_statistics_all(ctdb);
610 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
611 if (ret != 0) {
612 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
613 return ret;
615 show_statistics(&statistics, 1);
616 return 0;
621 reset remote ctdb statistics
623 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
625 int ret;
627 ret = ctdb_statistics_reset(ctdb, options.pnn);
628 if (ret != 0) {
629 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
630 return ret;
632 return 0;
637 display remote ctdb rolling statistics
639 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
641 int ret;
642 struct ctdb_statistics_wire *stats;
643 int i, num_records = -1;
645 assert_single_node_only();
647 if (argc ==1) {
648 num_records = atoi(argv[0]) - 1;
651 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
652 if (ret != 0) {
653 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
654 return ret;
656 for (i=0;i<stats->num;i++) {
657 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
658 continue;
660 show_statistics(&stats->stats[i], i==0);
661 if (i == num_records) {
662 break;
665 return 0;
670 display remote ctdb db statistics
672 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
674 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
675 struct ctdb_db_statistics *dbstat;
676 int i;
677 uint32_t db_id;
678 int num_hot_keys;
679 int ret;
681 if (argc < 1) {
682 usage();
685 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
686 return -1;
689 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
690 if (ret != 0) {
691 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
692 talloc_free(tmp_ctx);
693 return -1;
696 printf("DB Statistics: %s\n", argv[0]);
697 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
698 dbstat->db_ro_delegations);
699 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
700 dbstat->db_ro_delegations);
701 printf(" %s\n", "locks");
702 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
703 dbstat->locks.num_calls);
704 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
705 dbstat->locks.num_failed);
706 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
707 dbstat->locks.num_current);
708 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
709 dbstat->locks.num_pending);
710 printf(" %s", "hop_count_buckets:");
711 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
712 printf(" %d", dbstat->hop_count_bucket[i]);
714 printf("\n");
715 printf(" %s", "lock_buckets:");
716 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
717 printf(" %d", dbstat->locks.buckets[i]);
719 printf("\n");
720 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
721 "locks_latency MIN/AVG/MAX",
722 dbstat->locks.latency.min,
723 (dbstat->locks.latency.num ?
724 dbstat->locks.latency.total /dbstat->locks.latency.num :
725 0.0),
726 dbstat->locks.latency.max,
727 dbstat->locks.latency.num);
728 num_hot_keys = 0;
729 for (i=0; i<dbstat->num_hot_keys; i++) {
730 if (dbstat->hot_keys[i].count > 0) {
731 num_hot_keys++;
734 dbstat->num_hot_keys = num_hot_keys;
736 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
737 for (i = 0; i < dbstat->num_hot_keys; i++) {
738 int j;
739 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
740 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
741 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
743 printf("\n");
746 talloc_free(tmp_ctx);
747 return 0;
751 display uptime of remote node
753 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
755 int ret;
756 struct ctdb_uptime *uptime = NULL;
757 int tmp, days, hours, minutes, seconds;
759 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
760 if (ret != 0) {
761 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
762 return ret;
765 if (options.machinereadable){
766 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
767 printf(":%u:%u:%u:%lf\n",
768 (unsigned int)uptime->current_time.tv_sec,
769 (unsigned int)uptime->ctdbd_start_time.tv_sec,
770 (unsigned int)uptime->last_recovery_finished.tv_sec,
771 timeval_delta(&uptime->last_recovery_finished,
772 &uptime->last_recovery_started)
774 return 0;
777 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
779 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
780 seconds = tmp%60;
781 tmp /= 60;
782 minutes = tmp%60;
783 tmp /= 60;
784 hours = tmp%24;
785 tmp /= 24;
786 days = tmp;
787 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
789 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
790 seconds = tmp%60;
791 tmp /= 60;
792 minutes = tmp%60;
793 tmp /= 60;
794 hours = tmp%24;
795 tmp /= 24;
796 days = tmp;
797 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
799 printf("Duration of last recovery/failover: %lf seconds\n",
800 timeval_delta(&uptime->last_recovery_finished,
801 &uptime->last_recovery_started));
803 return 0;
807 show the PNN of the current node
809 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
811 uint32_t mypnn;
813 mypnn = getpnn(ctdb);
815 printf("PNN:%d\n", mypnn);
816 return 0;
820 struct pnn_node {
821 struct pnn_node *next, *prev;
822 ctdb_sock_addr addr;
823 int pnn;
826 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
827 const char *file)
829 int nlines;
830 char **lines;
831 int i, pnn;
832 struct pnn_node *pnn_nodes = NULL;
833 struct pnn_node *pnn_node;
835 lines = file_lines_load(file, &nlines, 0, mem_ctx);
836 if (lines == NULL) {
837 return NULL;
839 for (i=0, pnn=0; i<nlines; i++) {
840 char *node;
842 node = lines[i];
843 /* strip leading spaces */
844 while((*node == ' ') || (*node == '\t')) {
845 node++;
847 if (*node == '#') {
848 pnn++;
849 continue;
851 if (strcmp(node, "") == 0) {
852 continue;
854 pnn_node = talloc(mem_ctx, struct pnn_node);
855 pnn_node->pnn = pnn++;
857 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
858 DEBUG(DEBUG_ERR,
859 ("Invalid IP address '%s' in file %s\n",
860 node, file));
861 /* Caller will free mem_ctx */
862 return NULL;
865 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
868 return pnn_nodes;
871 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
873 const char *nodes_list;
875 /* read the nodes file */
876 nodes_list = getenv("CTDB_NODES");
877 if (nodes_list == NULL) {
878 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
879 getenv("CTDB_BASE"));
880 if (nodes_list == NULL) {
881 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
882 exit(1);
886 return read_pnn_node_file(mem_ctx, nodes_list);
890 show the PNN of the current node
891 discover the pnn by loading the nodes file and try to bind to all
892 addresses one at a time until the ip address is found.
894 static int find_node_xpnn(void)
896 TALLOC_CTX *mem_ctx = talloc_new(NULL);
897 struct pnn_node *pnn_nodes;
898 struct pnn_node *pnn_node;
899 int pnn;
901 pnn_nodes = read_nodes_file(mem_ctx);
902 if (pnn_nodes == NULL) {
903 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
904 talloc_free(mem_ctx);
905 return -1;
908 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
909 if (ctdb_sys_have_ip(&pnn_node->addr)) {
910 pnn = pnn_node->pnn;
911 talloc_free(mem_ctx);
912 return pnn;
916 printf("Failed to detect which PNN this node is\n");
917 talloc_free(mem_ctx);
918 return -1;
921 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
923 uint32_t pnn;
925 assert_single_node_only();
927 pnn = find_node_xpnn();
928 if (pnn == -1) {
929 return -1;
932 printf("PNN:%d\n", pnn);
933 return 0;
936 /* Helpers for ctdb status
938 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
940 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
941 int j;
942 bool ret = false;
944 if (node->flags == 0) {
945 struct ctdb_control_get_ifaces *ifaces;
947 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
948 tmp_ctx, &ifaces) == 0) {
949 for (j=0; j < ifaces->num; j++) {
950 if (ifaces->ifaces[j].link_state != 0) {
951 continue;
953 ret = true;
954 break;
958 talloc_free(tmp_ctx);
960 return ret;
963 static void control_status_header_machine(void)
965 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
966 ":Inactive:PartiallyOnline:ThisNode:\n");
969 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
970 struct ctdb_node_and_flags *node)
972 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
973 ctdb_addr_to_str(&node->addr),
974 !!(node->flags&NODE_FLAGS_DISCONNECTED),
975 !!(node->flags&NODE_FLAGS_BANNED),
976 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
977 !!(node->flags&NODE_FLAGS_UNHEALTHY),
978 !!(node->flags&NODE_FLAGS_STOPPED),
979 !!(node->flags&NODE_FLAGS_INACTIVE),
980 is_partially_online(ctdb, node) ? 1 : 0,
981 (node->pnn == mypnn)?'Y':'N');
983 return node->flags;
986 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
987 struct ctdb_node_and_flags *node)
989 printf("pnn:%d %-16s %s%s\n", node->pnn,
990 ctdb_addr_to_str(&node->addr),
991 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
992 node->pnn == mypnn?" (THIS NODE)":"");
994 return node->flags;
998 display remote ctdb status
1000 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1002 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1003 int i;
1004 struct ctdb_vnn_map *vnnmap=NULL;
1005 struct ctdb_node_map *nodemap=NULL;
1006 uint32_t recmode, recmaster, mypnn;
1007 int num_deleted_nodes = 0;
1008 int ret;
1010 mypnn = getpnn(ctdb);
1012 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1013 if (ret != 0) {
1014 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1015 talloc_free(tmp_ctx);
1016 return -1;
1019 if (options.machinereadable) {
1020 control_status_header_machine();
1021 for (i=0;i<nodemap->num;i++) {
1022 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1023 continue;
1025 (void) control_status_1_machine(ctdb, mypnn,
1026 &nodemap->nodes[i]);
1028 talloc_free(tmp_ctx);
1029 return 0;
1032 for (i=0; i<nodemap->num; i++) {
1033 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1034 num_deleted_nodes++;
1037 if (num_deleted_nodes == 0) {
1038 printf("Number of nodes:%d\n", nodemap->num);
1039 } else {
1040 printf("Number of nodes:%d (including %d deleted nodes)\n",
1041 nodemap->num, num_deleted_nodes);
1043 for(i=0;i<nodemap->num;i++){
1044 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1045 continue;
1047 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1050 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1051 if (ret != 0) {
1052 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1053 talloc_free(tmp_ctx);
1054 return -1;
1056 if (vnnmap->generation == INVALID_GENERATION) {
1057 printf("Generation:INVALID\n");
1058 } else {
1059 printf("Generation:%d\n",vnnmap->generation);
1061 printf("Size:%d\n",vnnmap->size);
1062 for(i=0;i<vnnmap->size;i++){
1063 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1066 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1067 if (ret != 0) {
1068 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1069 talloc_free(tmp_ctx);
1070 return -1;
1072 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1074 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1075 if (ret != 0) {
1076 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1077 talloc_free(tmp_ctx);
1078 return -1;
1080 printf("Recovery master:%d\n",recmaster);
1082 talloc_free(tmp_ctx);
1083 return 0;
1086 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1088 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1089 int i, ret;
1090 struct ctdb_node_map *nodemap=NULL;
1091 uint32_t * nodes;
1092 uint32_t pnn_mode, mypnn;
1094 if (argc > 1) {
1095 usage();
1098 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1099 options.pnn, true, &nodes, &pnn_mode)) {
1100 return -1;
1103 if (options.machinereadable) {
1104 control_status_header_machine();
1105 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1106 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1109 mypnn = getpnn(ctdb);
1111 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1112 if (ret != 0) {
1113 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1114 talloc_free(tmp_ctx);
1115 return -1;
1118 ret = 0;
1120 for (i = 0; i < talloc_array_length(nodes); i++) {
1121 if (options.machinereadable) {
1122 ret |= control_status_1_machine(ctdb, mypnn,
1123 &nodemap->nodes[nodes[i]]);
1124 } else {
1125 ret |= control_status_1_human(ctdb, mypnn,
1126 &nodemap->nodes[nodes[i]]);
1130 talloc_free(tmp_ctx);
1131 return ret;
1134 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1135 TALLOC_CTX *mem_ctx)
1137 const char *natgw_list;
1138 struct pnn_node *natgw_nodes = NULL;
1140 natgw_list = getenv("CTDB_NATGW_NODES");
1141 if (natgw_list == NULL) {
1142 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1143 getenv("CTDB_BASE"));
1144 if (natgw_list == NULL) {
1145 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1146 exit(1);
1149 /* The PNNs will be junk but they're not used */
1150 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1151 if (natgw_nodes == NULL) {
1152 DEBUG(DEBUG_ERR,
1153 ("Failed to load natgw node list '%s'\n", natgw_list));
1155 return natgw_nodes;
1159 /* talloc off the existing nodemap... */
1160 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1162 return talloc_zero_size(nodemap,
1163 offsetof(struct ctdb_node_map, nodes) +
1164 nodemap->num * sizeof(struct ctdb_node_and_flags));
1167 static struct ctdb_node_map *
1168 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1169 struct ctdb_node_map *nodemap,
1170 struct pnn_node *nodes)
1172 int i;
1173 struct pnn_node *n;
1174 struct ctdb_node_map *ret;
1176 ret = talloc_nodemap(nodemap);
1177 CTDB_NO_MEMORY_NULL(ctdb, ret);
1179 ret->num = 0;
1181 for (i = 0; i < nodemap->num; i++) {
1182 for(n = nodes; n != NULL ; n = n->next) {
1183 if (ctdb_same_ip(&n->addr,
1184 &nodemap->nodes[i].addr)) {
1185 break;
1188 if (n == NULL) {
1189 continue;
1192 ret->nodes[ret->num] = nodemap->nodes[i];
1193 ret->num++;
1196 return ret;
1199 static struct ctdb_node_map *
1200 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1201 struct ctdb_node_map *nodemap,
1202 uint32_t required_capabilities,
1203 bool first_only)
1205 int i;
1206 uint32_t capabilities;
1207 struct ctdb_node_map *ret;
1209 ret = talloc_nodemap(nodemap);
1210 CTDB_NO_MEMORY_NULL(ctdb, ret);
1212 ret->num = 0;
1214 for (i = 0; i < nodemap->num; i++) {
1215 int res;
1217 /* Disconnected nodes have no capabilities! */
1218 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1219 continue;
1222 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1223 nodemap->nodes[i].pnn,
1224 &capabilities);
1225 if (res != 0) {
1226 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1227 nodemap->nodes[i].pnn));
1228 talloc_free(ret);
1229 return NULL;
1231 if (!(capabilities & required_capabilities)) {
1232 continue;
1235 ret->nodes[ret->num] = nodemap->nodes[i];
1236 ret->num++;
1237 if (first_only) {
1238 break;
1242 return ret;
1245 static struct ctdb_node_map *
1246 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1247 struct ctdb_node_map *nodemap,
1248 uint32_t flags_mask)
1250 int i;
1251 struct ctdb_node_map *ret;
1253 ret = talloc_nodemap(nodemap);
1254 CTDB_NO_MEMORY_NULL(ctdb, ret);
1256 ret->num = 0;
1258 for (i = 0; i < nodemap->num; i++) {
1259 if (nodemap->nodes[i].flags & flags_mask) {
1260 continue;
1263 ret->nodes[ret->num] = nodemap->nodes[i];
1264 ret->num++;
1267 return ret;
1271 display the list of nodes belonging to this natgw configuration
1273 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1275 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1276 int i, ret;
1277 struct pnn_node *natgw_nodes = NULL;
1278 struct ctdb_node_map *orig_nodemap=NULL;
1279 struct ctdb_node_map *nodemap;
1280 uint32_t mypnn, pnn;
1281 const char *ip;
1283 /* When we have some nodes that could be the NATGW, make a
1284 * series of attempts to find the first node that doesn't have
1285 * certain status flags set.
1287 uint32_t exclude_flags[] = {
1288 /* Look for a nice healthy node */
1289 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1290 /* If not found, an UNHEALTHY/BANNED node will do */
1291 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1292 /* If not found, a STOPPED node will do */
1293 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1297 /* read the natgw nodes file into a linked list */
1298 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1299 if (natgw_nodes == NULL) {
1300 ret = -1;
1301 goto done;
1304 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1305 tmp_ctx, &orig_nodemap);
1306 if (ret != 0) {
1307 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1308 talloc_free(tmp_ctx);
1309 return -1;
1312 /* Get a nodemap that includes only the nodes in the NATGW
1313 * group */
1314 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1315 if (nodemap == NULL) {
1316 ret = -1;
1317 goto done;
1320 ret = 2; /* matches ENOENT */
1321 pnn = -1;
1322 ip = "0.0.0.0";
1323 /* For each flag mask... */
1324 for (i = 0; exclude_flags[i] != 0; i++) {
1325 /* ... get a nodemap that excludes nodes with with
1326 * masked flags... */
1327 struct ctdb_node_map *t =
1328 filter_nodemap_by_flags(ctdb, nodemap,
1329 exclude_flags[i]);
1330 if (t == NULL) {
1331 /* No memory */
1332 ret = -1;
1333 goto done;
1335 if (t->num > 0) {
1336 /* ... and find the first node with the NATGW
1337 * capability */
1338 struct ctdb_node_map *n;
1339 n = filter_nodemap_by_capabilities(ctdb, t,
1340 CTDB_CAP_NATGW,
1341 true);
1342 if (n == NULL) {
1343 /* No memory */
1344 ret = -1;
1345 goto done;
1347 if (n->num > 0) {
1348 ret = 0;
1349 pnn = n->nodes[0].pnn;
1350 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1351 break;
1354 talloc_free(t);
1357 if (options.machinereadable) {
1358 printf(":Node:IP:\n");
1359 printf(":%d:%s:\n", pnn, ip);
1360 } else {
1361 printf("%d %s\n", pnn, ip);
1364 /* print the pruned list of nodes belonging to this natgw list */
1365 mypnn = getpnn(ctdb);
1366 if (options.machinereadable) {
1367 control_status_header_machine();
1368 } else {
1369 printf("Number of nodes:%d\n", nodemap->num);
1371 for(i=0;i<nodemap->num;i++){
1372 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1373 continue;
1375 if (options.machinereadable) {
1376 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1377 } else {
1378 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1382 done:
1383 talloc_free(tmp_ctx);
1384 return ret;
1388 display the status of the scripts for monitoring (or other events)
1390 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1391 enum ctdb_eventscript_call type)
1393 struct ctdb_scripts_wire *script_status;
1394 int ret, i;
1396 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1397 if (ret != 0) {
1398 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1399 return ret;
1402 if (script_status == NULL) {
1403 if (!options.machinereadable) {
1404 printf("%s cycle never run\n",
1405 ctdb_eventscript_call_names[type]);
1407 return 0;
1410 if (!options.machinereadable) {
1411 int num_run = 0;
1412 for (i=0; i<script_status->num_scripts; i++) {
1413 if (script_status->scripts[i].status != -ENOEXEC) {
1414 num_run++;
1417 printf("%d scripts were executed last %s cycle\n",
1418 num_run,
1419 ctdb_eventscript_call_names[type]);
1421 for (i=0; i<script_status->num_scripts; i++) {
1422 const char *status = NULL;
1424 switch (script_status->scripts[i].status) {
1425 case -ETIME:
1426 status = "TIMEDOUT";
1427 break;
1428 case -ENOEXEC:
1429 status = "DISABLED";
1430 break;
1431 case 0:
1432 status = "OK";
1433 break;
1434 default:
1435 if (script_status->scripts[i].status > 0)
1436 status = "ERROR";
1437 break;
1439 if (options.machinereadable) {
1440 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1441 ctdb_eventscript_call_names[type],
1442 script_status->scripts[i].name,
1443 script_status->scripts[i].status,
1444 status,
1445 (long)script_status->scripts[i].start.tv_sec,
1446 (long)script_status->scripts[i].start.tv_usec,
1447 (long)script_status->scripts[i].finished.tv_sec,
1448 (long)script_status->scripts[i].finished.tv_usec,
1449 script_status->scripts[i].output);
1450 continue;
1452 if (status)
1453 printf("%-20s Status:%s ",
1454 script_status->scripts[i].name, status);
1455 else
1456 /* Some other error, eg from stat. */
1457 printf("%-20s Status:CANNOT RUN (%s)",
1458 script_status->scripts[i].name,
1459 strerror(-script_status->scripts[i].status));
1461 if (script_status->scripts[i].status >= 0) {
1462 printf("Duration:%.3lf ",
1463 timeval_delta(&script_status->scripts[i].finished,
1464 &script_status->scripts[i].start));
1466 if (script_status->scripts[i].status != -ENOEXEC) {
1467 printf("%s",
1468 ctime(&script_status->scripts[i].start.tv_sec));
1469 if (script_status->scripts[i].status != 0) {
1470 printf(" OUTPUT:%s\n",
1471 script_status->scripts[i].output);
1473 } else {
1474 printf("\n");
1477 return 0;
1481 static int control_scriptstatus(struct ctdb_context *ctdb,
1482 int argc, const char **argv)
1484 int ret;
1485 enum ctdb_eventscript_call type, min, max;
1486 const char *arg;
1488 if (argc > 1) {
1489 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1490 return -1;
1493 if (argc == 0)
1494 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1495 else
1496 arg = argv[0];
1498 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1499 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1500 min = type;
1501 max = type+1;
1502 break;
1505 if (type == CTDB_EVENT_MAX) {
1506 if (strcmp(arg, "all") == 0) {
1507 min = 0;
1508 max = CTDB_EVENT_MAX;
1509 } else {
1510 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1511 return -1;
1515 if (options.machinereadable) {
1516 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1519 for (type = min; type < max; type++) {
1520 ret = control_one_scriptstatus(ctdb, type);
1521 if (ret != 0) {
1522 return ret;
1526 return 0;
1530 enable an eventscript
1532 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1534 int ret;
1536 if (argc < 1) {
1537 usage();
1540 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1541 if (ret != 0) {
1542 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1543 return ret;
1546 return 0;
1550 disable an eventscript
1552 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1554 int ret;
1556 if (argc < 1) {
1557 usage();
1560 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1561 if (ret != 0) {
1562 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1563 return ret;
1566 return 0;
1570 display the pnn of the recovery master
1572 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1574 uint32_t recmaster;
1575 int ret;
1577 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1578 if (ret != 0) {
1579 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1580 return -1;
1582 printf("%d\n",recmaster);
1584 return 0;
1588 add a tickle to a public address
1590 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1592 struct ctdb_tcp_connection t;
1593 TDB_DATA data;
1594 int ret;
1596 assert_single_node_only();
1598 if (argc < 2) {
1599 usage();
1602 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1603 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1604 return -1;
1606 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1607 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1608 return -1;
1611 data.dptr = (uint8_t *)&t;
1612 data.dsize = sizeof(t);
1614 /* tell all nodes about this tcp connection */
1615 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1616 0, data, ctdb, NULL, NULL, NULL, NULL);
1617 if (ret != 0) {
1618 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1619 return -1;
1622 return 0;
1627 delete a tickle from a node
1629 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1631 struct ctdb_tcp_connection t;
1632 TDB_DATA data;
1633 int ret;
1635 assert_single_node_only();
1637 if (argc < 2) {
1638 usage();
1641 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1642 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1643 return -1;
1645 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1646 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1647 return -1;
1650 data.dptr = (uint8_t *)&t;
1651 data.dsize = sizeof(t);
1653 /* tell all nodes about this tcp connection */
1654 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1655 0, data, ctdb, NULL, NULL, NULL, NULL);
1656 if (ret != 0) {
1657 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1658 return -1;
1661 return 0;
1666 get a list of all tickles for this pnn
1668 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1670 struct ctdb_control_tcp_tickle_list *list;
1671 ctdb_sock_addr addr;
1672 int i, ret;
1673 unsigned port = 0;
1675 assert_single_node_only();
1677 if (argc < 1) {
1678 usage();
1681 if (argc == 2) {
1682 port = atoi(argv[1]);
1685 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1686 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1687 return -1;
1690 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1691 if (ret == -1) {
1692 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1693 return -1;
1696 if (options.machinereadable){
1697 printf(":source ip:port:destination ip:port:\n");
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(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1703 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1705 } else {
1706 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1707 printf("Num tickles:%u\n", list->tickles.num);
1708 for (i=0;i<list->tickles.num;i++) {
1709 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1710 continue;
1712 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1713 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));
1717 talloc_free(list);
1719 return 0;
1723 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1725 struct ctdb_all_public_ips *ips;
1726 struct ctdb_public_ip ip;
1727 int i, ret;
1728 uint32_t *nodes;
1729 uint32_t disable_time;
1730 TDB_DATA data;
1731 struct ctdb_node_map *nodemap=NULL;
1732 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1734 disable_time = 30;
1735 data.dptr = (uint8_t*)&disable_time;
1736 data.dsize = sizeof(disable_time);
1737 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1738 if (ret != 0) {
1739 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1740 return -1;
1745 /* read the public ip list from the node */
1746 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1747 if (ret != 0) {
1748 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1749 talloc_free(tmp_ctx);
1750 return -1;
1753 for (i=0;i<ips->num;i++) {
1754 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1755 break;
1758 if (i==ips->num) {
1759 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1760 pnn, ctdb_addr_to_str(addr)));
1761 talloc_free(tmp_ctx);
1762 return -1;
1765 ip.pnn = pnn;
1766 ip.addr = *addr;
1768 data.dptr = (uint8_t *)&ip;
1769 data.dsize = sizeof(ip);
1771 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1772 if (ret != 0) {
1773 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1774 talloc_free(tmp_ctx);
1775 return ret;
1778 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1779 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1780 nodes, 0,
1781 LONGTIMELIMIT(),
1782 false, data,
1783 NULL, NULL,
1784 NULL);
1785 if (ret != 0) {
1786 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1787 talloc_free(tmp_ctx);
1788 return -1;
1791 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1792 if (ret != 0) {
1793 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1794 talloc_free(tmp_ctx);
1795 return -1;
1798 /* update the recovery daemon so it now knows to expect the new
1799 node assignment for this ip.
1801 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1802 if (ret != 0) {
1803 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1804 return -1;
1807 talloc_free(tmp_ctx);
1808 return 0;
1813 * scans all other nodes and returns a pnn for another node that can host this
1814 * ip address or -1
1816 static int
1817 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1819 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1820 struct ctdb_all_public_ips *ips;
1821 struct ctdb_node_map *nodemap=NULL;
1822 int i, j, ret;
1823 int pnn;
1825 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1826 if (ret != 0) {
1827 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1828 talloc_free(tmp_ctx);
1829 return ret;
1832 for(i=0;i<nodemap->num;i++){
1833 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1834 continue;
1836 if (nodemap->nodes[i].pnn == options.pnn) {
1837 continue;
1840 /* read the public ip list from this node */
1841 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1842 if (ret != 0) {
1843 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1844 return -1;
1847 for (j=0;j<ips->num;j++) {
1848 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1849 pnn = nodemap->nodes[i].pnn;
1850 talloc_free(tmp_ctx);
1851 return pnn;
1854 talloc_free(ips);
1857 talloc_free(tmp_ctx);
1858 return -1;
1861 /* If pnn is -1 then try to find a node to move IP to... */
1862 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1864 bool pnn_specified = (pnn == -1 ? false : true);
1865 int retries = 0;
1867 while (retries < 5) {
1868 if (!pnn_specified) {
1869 pnn = find_other_host_for_public_ip(ctdb, addr);
1870 if (pnn == -1) {
1871 return false;
1873 DEBUG(DEBUG_NOTICE,
1874 ("Trying to move public IP to node %u\n", pnn));
1877 if (move_ip(ctdb, addr, pnn) == 0) {
1878 return true;
1881 sleep(3);
1882 retries++;
1885 return false;
1890 move/failover an ip address to a specific node
1892 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1894 uint32_t pnn;
1895 ctdb_sock_addr addr;
1897 assert_single_node_only();
1899 if (argc < 2) {
1900 usage();
1901 return -1;
1904 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1905 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1906 return -1;
1910 if (sscanf(argv[1], "%u", &pnn) != 1) {
1911 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1912 return -1;
1915 if (!try_moveip(ctdb, &addr, pnn)) {
1916 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1917 return -1;
1920 return 0;
1923 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1925 TDB_DATA data;
1927 data.dptr = (uint8_t *)&pnn;
1928 data.dsize = sizeof(uint32_t);
1929 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1930 DEBUG(DEBUG_ERR,
1931 ("Failed to send message to force node %u to be a rebalancing target\n",
1932 pnn));
1933 return -1;
1936 return 0;
1941 rebalance a node by setting it to allow failback and triggering a
1942 takeover run
1944 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1946 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1947 uint32_t *nodes;
1948 uint32_t pnn_mode;
1949 int i, ret;
1951 assert_single_node_only();
1953 if (argc > 1) {
1954 usage();
1957 /* Determine the nodes where IPs need to be reloaded */
1958 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1959 options.pnn, true, &nodes, &pnn_mode)) {
1960 ret = -1;
1961 goto done;
1964 for (i = 0; i < talloc_array_length(nodes); i++) {
1965 if (!rebalance_node(ctdb, nodes[i])) {
1966 ret = -1;
1970 done:
1971 talloc_free(tmp_ctx);
1972 return ret;
1975 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1977 struct ctdb_public_ip ip;
1978 int ret;
1979 uint32_t *nodes;
1980 uint32_t disable_time;
1981 TDB_DATA data;
1982 struct ctdb_node_map *nodemap=NULL;
1983 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1985 disable_time = 30;
1986 data.dptr = (uint8_t*)&disable_time;
1987 data.dsize = sizeof(disable_time);
1988 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1989 if (ret != 0) {
1990 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1991 return -1;
1994 ip.pnn = -1;
1995 ip.addr = *addr;
1997 data.dptr = (uint8_t *)&ip;
1998 data.dsize = sizeof(ip);
2000 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
2001 if (ret != 0) {
2002 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2003 talloc_free(tmp_ctx);
2004 return ret;
2007 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2008 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
2009 nodes, 0,
2010 LONGTIMELIMIT(),
2011 false, data,
2012 NULL, NULL,
2013 NULL);
2014 if (ret != 0) {
2015 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2016 talloc_free(tmp_ctx);
2017 return -1;
2020 talloc_free(tmp_ctx);
2021 return 0;
2025 release an ip form all nodes and have it re-assigned by recd
2027 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2029 ctdb_sock_addr addr;
2031 assert_single_node_only();
2033 if (argc < 1) {
2034 usage();
2035 return -1;
2038 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2039 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2040 return -1;
2043 if (rebalance_ip(ctdb, &addr) != 0) {
2044 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2045 return -1;
2048 return 0;
2051 static int getips_store_callback(void *param, void *data)
2053 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2054 struct ctdb_all_public_ips *ips = param;
2055 int i;
2057 i = ips->num++;
2058 ips->ips[i].pnn = node_ip->pnn;
2059 ips->ips[i].addr = node_ip->addr;
2060 return 0;
2063 static int getips_count_callback(void *param, void *data)
2065 uint32_t *count = param;
2067 (*count)++;
2068 return 0;
2071 #define IP_KEYLEN 4
2072 static uint32_t *ip_key(ctdb_sock_addr *ip)
2074 static uint32_t key[IP_KEYLEN];
2076 bzero(key, sizeof(key));
2078 switch (ip->sa.sa_family) {
2079 case AF_INET:
2080 key[0] = ip->ip.sin_addr.s_addr;
2081 break;
2082 case AF_INET6: {
2083 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2084 key[0] = s6_a32[3];
2085 key[1] = s6_a32[2];
2086 key[2] = s6_a32[1];
2087 key[3] = s6_a32[0];
2088 break;
2090 default:
2091 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2092 return key;
2095 return key;
2098 static void *add_ip_callback(void *parm, void *data)
2100 return parm;
2103 static int
2104 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2106 struct ctdb_all_public_ips *tmp_ips;
2107 struct ctdb_node_map *nodemap=NULL;
2108 trbt_tree_t *ip_tree;
2109 int i, j, len, ret;
2110 uint32_t count;
2112 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2113 if (ret != 0) {
2114 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2115 return ret;
2118 ip_tree = trbt_create(tmp_ctx, 0);
2120 for(i=0;i<nodemap->num;i++){
2121 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2122 continue;
2124 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2125 continue;
2128 /* read the public ip list from this node */
2129 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2130 if (ret != 0) {
2131 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2132 return -1;
2135 for (j=0; j<tmp_ips->num;j++) {
2136 struct ctdb_public_ip *node_ip;
2138 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2139 node_ip->pnn = tmp_ips->ips[j].pnn;
2140 node_ip->addr = tmp_ips->ips[j].addr;
2142 trbt_insertarray32_callback(ip_tree,
2143 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2144 add_ip_callback,
2145 node_ip);
2147 talloc_free(tmp_ips);
2150 /* traverse */
2151 count = 0;
2152 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2154 len = offsetof(struct ctdb_all_public_ips, ips) +
2155 count*sizeof(struct ctdb_public_ip);
2156 tmp_ips = talloc_zero_size(tmp_ctx, len);
2157 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2159 *ips = tmp_ips;
2161 return 0;
2165 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2167 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2169 event_add_timed(ctdb->ev, ctdb,
2170 timeval_current_ofs(1, 0),
2171 ctdb_every_second, ctdb);
2174 struct srvid_reply_handler_data {
2175 bool done;
2176 bool wait_for_all;
2177 uint32_t *nodes;
2178 const char *srvid_str;
2181 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2182 uint64_t srvid,
2183 TDB_DATA data,
2184 void *private_data)
2186 struct srvid_reply_handler_data *d =
2187 (struct srvid_reply_handler_data *)private_data;
2188 int i;
2189 int32_t ret;
2191 if (data.dsize != sizeof(ret)) {
2192 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2193 return;
2196 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2197 ret = *(int32_t *)data.dptr;
2198 if (ret < 0) {
2199 DEBUG(DEBUG_ERR,
2200 ("%s failed with result %d\n", d->srvid_str, ret));
2201 return;
2204 if (!d->wait_for_all) {
2205 d->done = true;
2206 return;
2209 /* Wait for all replies */
2210 d->done = true;
2211 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2212 if (d->nodes[i] == ret) {
2213 DEBUG(DEBUG_INFO,
2214 ("%s reply received from node %u\n",
2215 d->srvid_str, ret));
2216 d->nodes[i] = -1;
2218 if (d->nodes[i] != -1) {
2219 /* Found a node that hasn't yet replied */
2220 d->done = false;
2225 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2226 * or replies from all connected nodes. arg is the data argument to
2227 * pass in the srvid_request structure - pass 0 if this isn't needed.
2229 static int srvid_broadcast(struct ctdb_context *ctdb,
2230 uint64_t srvid, uint32_t *arg,
2231 const char *srvid_str, bool wait_for_all)
2233 int ret;
2234 TDB_DATA data;
2235 uint32_t pnn;
2236 uint64_t reply_srvid;
2237 struct srvid_request request;
2238 struct srvid_request_data request_data;
2239 struct srvid_reply_handler_data reply_data;
2240 struct timeval tv;
2242 ZERO_STRUCT(request);
2244 /* Time ticks to enable timeouts to be processed */
2245 event_add_timed(ctdb->ev, ctdb,
2246 timeval_current_ofs(1, 0),
2247 ctdb_every_second, ctdb);
2249 pnn = ctdb_get_pnn(ctdb);
2250 reply_srvid = getpid();
2252 if (arg == NULL) {
2253 request.pnn = pnn;
2254 request.srvid = reply_srvid;
2256 data.dptr = (uint8_t *)&request;
2257 data.dsize = sizeof(request);
2258 } else {
2259 request_data.pnn = pnn;
2260 request_data.srvid = reply_srvid;
2261 request_data.data = *arg;
2263 data.dptr = (uint8_t *)&request_data;
2264 data.dsize = sizeof(request_data);
2267 /* Register message port for reply from recovery master */
2268 ctdb_client_set_message_handler(ctdb, reply_srvid,
2269 srvid_broadcast_reply_handler,
2270 &reply_data);
2272 reply_data.wait_for_all = wait_for_all;
2273 reply_data.nodes = NULL;
2274 reply_data.srvid_str = srvid_str;
2276 again:
2277 reply_data.done = false;
2279 if (wait_for_all) {
2280 struct ctdb_node_map *nodemap;
2282 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2283 CTDB_CURRENT_NODE, ctdb, &nodemap);
2284 if (ret != 0) {
2285 DEBUG(DEBUG_ERR,
2286 ("Unable to get nodemap from current node, try again\n"));
2287 sleep(1);
2288 goto again;
2291 if (reply_data.nodes != NULL) {
2292 talloc_free(reply_data.nodes);
2294 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2295 NULL, true);
2297 talloc_free(nodemap);
2300 /* Send to all connected nodes. Only recmaster replies */
2301 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2302 srvid, data);
2303 if (ret != 0) {
2304 /* This can only happen if the socket is closed and
2305 * there's no way to recover from that, so don't try
2306 * again.
2308 DEBUG(DEBUG_ERR,
2309 ("Failed to send %s request to connected nodes\n",
2310 srvid_str));
2311 return -1;
2314 tv = timeval_current();
2315 /* This loop terminates the reply is received */
2316 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2317 event_loop_once(ctdb->ev);
2320 if (!reply_data.done) {
2321 DEBUG(DEBUG_NOTICE,
2322 ("Still waiting for confirmation of %s\n", srvid_str));
2323 sleep(1);
2324 goto again;
2327 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2329 talloc_free(reply_data.nodes);
2331 return 0;
2334 static int ipreallocate(struct ctdb_context *ctdb)
2336 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2337 "IP reallocation", false);
2341 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2343 return ipreallocate(ctdb);
2347 add a public ip address to a node
2349 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2351 int i, ret;
2352 int len, retries = 0;
2353 unsigned mask;
2354 ctdb_sock_addr addr;
2355 struct ctdb_control_ip_iface *pub;
2356 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2357 struct ctdb_all_public_ips *ips;
2360 if (argc != 2) {
2361 talloc_free(tmp_ctx);
2362 usage();
2365 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2366 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2367 talloc_free(tmp_ctx);
2368 return -1;
2371 /* read the public ip list from the node */
2372 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2373 if (ret != 0) {
2374 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2375 talloc_free(tmp_ctx);
2376 return -1;
2378 for (i=0;i<ips->num;i++) {
2379 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2380 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2381 return 0;
2387 /* Dont timeout. This command waits for an ip reallocation
2388 which sometimes can take wuite a while if there has
2389 been a recent recovery
2391 alarm(0);
2393 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2394 pub = talloc_size(tmp_ctx, len);
2395 CTDB_NO_MEMORY(ctdb, pub);
2397 pub->addr = addr;
2398 pub->mask = mask;
2399 pub->len = strlen(argv[1])+1;
2400 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2402 do {
2403 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2404 if (ret != 0) {
2405 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2406 sleep(3);
2407 retries++;
2409 } while (retries < 5 && ret != 0);
2410 if (ret != 0) {
2411 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2412 talloc_free(tmp_ctx);
2413 return ret;
2416 if (rebalance_node(ctdb, options.pnn) != 0) {
2417 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2418 return ret;
2421 talloc_free(tmp_ctx);
2422 return 0;
2426 add a public ip address to a node
2428 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2430 ctdb_sock_addr addr;
2432 if (argc != 1) {
2433 usage();
2436 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2437 printf("Badly formed ip : %s\n", argv[0]);
2438 return -1;
2441 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2443 return 0;
2446 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2448 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2450 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2451 struct ctdb_node_map *nodemap=NULL;
2452 struct ctdb_all_public_ips *ips;
2453 int ret, i, j;
2455 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2456 if (ret != 0) {
2457 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2458 return ret;
2461 /* remove it from the nodes that are not hosting the ip currently */
2462 for(i=0;i<nodemap->num;i++){
2463 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2464 continue;
2466 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2467 if (ret != 0) {
2468 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2469 continue;
2472 for (j=0;j<ips->num;j++) {
2473 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2474 break;
2477 if (j==ips->num) {
2478 continue;
2481 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2482 continue;
2485 options.pnn = nodemap->nodes[i].pnn;
2486 control_delip(ctdb, argc, argv);
2490 /* remove it from every node (also the one hosting it) */
2491 for(i=0;i<nodemap->num;i++){
2492 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2493 continue;
2495 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2496 if (ret != 0) {
2497 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2498 continue;
2501 for (j=0;j<ips->num;j++) {
2502 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2503 break;
2506 if (j==ips->num) {
2507 continue;
2510 options.pnn = nodemap->nodes[i].pnn;
2511 control_delip(ctdb, argc, argv);
2514 talloc_free(tmp_ctx);
2515 return 0;
2519 delete a public ip address from a node
2521 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2523 int i, ret;
2524 ctdb_sock_addr addr;
2525 struct ctdb_control_ip_iface pub;
2526 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2527 struct ctdb_all_public_ips *ips;
2529 if (argc != 1) {
2530 talloc_free(tmp_ctx);
2531 usage();
2534 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2535 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2536 return -1;
2539 if (options.pnn == CTDB_BROADCAST_ALL) {
2540 return control_delip_all(ctdb, argc, argv, &addr);
2543 pub.addr = addr;
2544 pub.mask = 0;
2545 pub.len = 0;
2547 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2548 if (ret != 0) {
2549 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2550 talloc_free(tmp_ctx);
2551 return ret;
2554 for (i=0;i<ips->num;i++) {
2555 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2556 break;
2560 if (i==ips->num) {
2561 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2562 ctdb_addr_to_str(&addr)));
2563 talloc_free(tmp_ctx);
2564 return -1;
2567 /* This is an optimisation. If this node is hosting the IP
2568 * then try to move it somewhere else without invoking a full
2569 * takeover run. We don't care if this doesn't work!
2571 if (ips->ips[i].pnn == options.pnn) {
2572 (void) try_moveip(ctdb, &addr, -1);
2575 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2576 if (ret != 0) {
2577 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2578 talloc_free(tmp_ctx);
2579 return ret;
2582 talloc_free(tmp_ctx);
2583 return 0;
2586 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2587 int argc, const char **argv)
2589 struct ctdb_control_killtcp *killtcp;
2590 int max_entries, current, i;
2591 struct timeval timeout;
2592 char line[128], src[128], dst[128];
2593 int linenum;
2594 TDB_DATA data;
2595 struct client_async_data *async_data;
2596 struct ctdb_client_control_state *state;
2598 if (argc != 0) {
2599 usage();
2602 linenum = 1;
2603 killtcp = NULL;
2604 max_entries = 0;
2605 current = 0;
2606 while (!feof(stdin)) {
2607 if (fgets(line, sizeof(line), stdin) == NULL) {
2608 continue;
2611 /* Silently skip empty lines */
2612 if (line[0] == '\n') {
2613 continue;
2616 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2617 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2618 linenum, line));
2619 talloc_free(killtcp);
2620 return -1;
2623 if (current >= max_entries) {
2624 max_entries += 1024;
2625 killtcp = talloc_realloc(ctdb, killtcp,
2626 struct ctdb_control_killtcp,
2627 max_entries);
2628 CTDB_NO_MEMORY(ctdb, killtcp);
2631 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2632 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2633 linenum, src));
2634 talloc_free(killtcp);
2635 return -1;
2638 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2639 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2640 linenum, dst));
2641 talloc_free(killtcp);
2642 return -1;
2645 current++;
2648 async_data = talloc_zero(ctdb, struct client_async_data);
2649 if (async_data == NULL) {
2650 talloc_free(killtcp);
2651 return -1;
2654 for (i = 0; i < current; i++) {
2656 data.dsize = sizeof(struct ctdb_control_killtcp);
2657 data.dptr = (unsigned char *)&killtcp[i];
2659 timeout = TIMELIMIT();
2660 state = ctdb_control_send(ctdb, options.pnn, 0,
2661 CTDB_CONTROL_KILL_TCP, 0, data,
2662 async_data, &timeout, NULL);
2664 if (state == NULL) {
2665 DEBUG(DEBUG_ERR,
2666 ("Failed to call async killtcp control to node %u\n",
2667 options.pnn));
2668 talloc_free(killtcp);
2669 return -1;
2672 ctdb_client_async_add(async_data, state);
2675 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2676 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2677 talloc_free(killtcp);
2678 return -1;
2681 talloc_free(killtcp);
2682 return 0;
2687 kill a tcp connection
2689 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2691 int ret;
2692 struct ctdb_control_killtcp killtcp;
2694 assert_single_node_only();
2696 if (argc == 0) {
2697 return kill_tcp_from_file(ctdb, argc, argv);
2700 if (argc < 2) {
2701 usage();
2704 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2705 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2706 return -1;
2709 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2710 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2711 return -1;
2714 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2715 if (ret != 0) {
2716 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2717 return ret;
2720 return 0;
2725 send a gratious arp
2727 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2729 int ret;
2730 ctdb_sock_addr addr;
2732 assert_single_node_only();
2734 if (argc < 2) {
2735 usage();
2738 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2739 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2740 return -1;
2743 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2744 if (ret != 0) {
2745 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2746 return ret;
2749 return 0;
2753 register a server id
2755 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2757 int ret;
2758 struct ctdb_server_id server_id;
2760 if (argc < 3) {
2761 usage();
2764 server_id.pnn = strtoul(argv[0], NULL, 0);
2765 server_id.type = strtoul(argv[1], NULL, 0);
2766 server_id.server_id = strtoul(argv[2], NULL, 0);
2768 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2769 if (ret != 0) {
2770 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2771 return ret;
2773 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2774 sleep(999);
2775 return -1;
2779 unregister a server id
2781 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2783 int ret;
2784 struct ctdb_server_id server_id;
2786 if (argc < 3) {
2787 usage();
2790 server_id.pnn = strtoul(argv[0], NULL, 0);
2791 server_id.type = strtoul(argv[1], NULL, 0);
2792 server_id.server_id = strtoul(argv[2], NULL, 0);
2794 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2795 if (ret != 0) {
2796 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2797 return ret;
2799 return -1;
2803 check if a server id exists
2805 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2807 uint32_t status;
2808 int ret;
2809 struct ctdb_server_id server_id;
2811 if (argc < 3) {
2812 usage();
2815 server_id.pnn = strtoul(argv[0], NULL, 0);
2816 server_id.type = strtoul(argv[1], NULL, 0);
2817 server_id.server_id = strtoul(argv[2], NULL, 0);
2819 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2820 if (ret != 0) {
2821 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2822 return ret;
2825 if (status) {
2826 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2827 } else {
2828 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2830 return 0;
2834 get a list of all server ids that are registered on a node
2836 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2838 int i, ret;
2839 struct ctdb_server_id_list *server_ids;
2841 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2842 if (ret != 0) {
2843 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2844 return ret;
2847 for (i=0; i<server_ids->num; i++) {
2848 printf("Server id %d:%d:%d\n",
2849 server_ids->server_ids[i].pnn,
2850 server_ids->server_ids[i].type,
2851 server_ids->server_ids[i].server_id);
2854 return -1;
2858 check if a server id exists
2860 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2862 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2863 uint64_t *ids;
2864 uint8_t *result;
2865 int i;
2867 if (argc < 1) {
2868 talloc_free(tmp_ctx);
2869 usage();
2872 ids = talloc_array(tmp_ctx, uint64_t, argc);
2873 result = talloc_array(tmp_ctx, uint8_t, argc);
2875 for (i = 0; i < argc; i++) {
2876 ids[i] = strtoull(argv[i], NULL, 0);
2879 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2880 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2881 options.pnn));
2882 talloc_free(tmp_ctx);
2883 return -1;
2886 for (i=0; i < argc; i++) {
2887 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2888 result[i] ? "exists" : "does not exist");
2891 talloc_free(tmp_ctx);
2892 return 0;
2896 send a tcp tickle ack
2898 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2900 int ret;
2901 ctdb_sock_addr src, dst;
2903 if (argc < 2) {
2904 usage();
2907 if (!parse_ip_port(argv[0], &src)) {
2908 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2909 return -1;
2912 if (!parse_ip_port(argv[1], &dst)) {
2913 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2914 return -1;
2917 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2918 if (ret==0) {
2919 return 0;
2921 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2923 return -1;
2928 display public ip status
2930 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2932 int i, ret;
2933 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2934 struct ctdb_all_public_ips *ips;
2936 if (options.pnn == CTDB_BROADCAST_ALL) {
2937 /* read the list of public ips from all nodes */
2938 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2939 } else {
2940 /* read the public ip list from this node */
2941 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2943 if (ret != 0) {
2944 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2945 talloc_free(tmp_ctx);
2946 return ret;
2949 if (options.machinereadable){
2950 printf(":Public IP:Node:");
2951 if (options.verbose){
2952 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2954 printf("\n");
2955 } else {
2956 if (options.pnn == CTDB_BROADCAST_ALL) {
2957 printf("Public IPs on ALL nodes\n");
2958 } else {
2959 printf("Public IPs on node %u\n", options.pnn);
2963 for (i=1;i<=ips->num;i++) {
2964 struct ctdb_control_public_ip_info *info = NULL;
2965 int32_t pnn;
2966 char *aciface = NULL;
2967 char *avifaces = NULL;
2968 char *cifaces = NULL;
2970 if (options.pnn == CTDB_BROADCAST_ALL) {
2971 pnn = ips->ips[ips->num-i].pnn;
2972 } else {
2973 pnn = options.pnn;
2976 if (pnn != -1) {
2977 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2978 &ips->ips[ips->num-i].addr, &info);
2979 } else {
2980 ret = -1;
2983 if (ret == 0) {
2984 int j;
2985 for (j=0; j < info->num; j++) {
2986 if (cifaces == NULL) {
2987 cifaces = talloc_strdup(info,
2988 info->ifaces[j].name);
2989 } else {
2990 cifaces = talloc_asprintf_append(cifaces,
2991 ",%s",
2992 info->ifaces[j].name);
2995 if (info->active_idx == j) {
2996 aciface = info->ifaces[j].name;
2999 if (info->ifaces[j].link_state == 0) {
3000 continue;
3003 if (avifaces == NULL) {
3004 avifaces = talloc_strdup(info, info->ifaces[j].name);
3005 } else {
3006 avifaces = talloc_asprintf_append(avifaces,
3007 ",%s",
3008 info->ifaces[j].name);
3013 if (options.machinereadable){
3014 printf(":%s:%d:",
3015 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3016 ips->ips[ips->num-i].pnn);
3017 if (options.verbose){
3018 printf("%s:%s:%s:",
3019 aciface?aciface:"",
3020 avifaces?avifaces:"",
3021 cifaces?cifaces:"");
3023 printf("\n");
3024 } else {
3025 if (options.verbose) {
3026 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3027 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3028 ips->ips[ips->num-i].pnn,
3029 aciface?aciface:"",
3030 avifaces?avifaces:"",
3031 cifaces?cifaces:"");
3032 } else {
3033 printf("%s %d\n",
3034 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3035 ips->ips[ips->num-i].pnn);
3038 talloc_free(info);
3041 talloc_free(tmp_ctx);
3042 return 0;
3046 public ip info
3048 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3050 int i, ret;
3051 ctdb_sock_addr addr;
3052 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3053 struct ctdb_control_public_ip_info *info;
3055 if (argc != 1) {
3056 talloc_free(tmp_ctx);
3057 usage();
3060 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3061 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3062 return -1;
3065 /* read the public ip info from this node */
3066 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3067 tmp_ctx, &addr, &info);
3068 if (ret != 0) {
3069 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3070 argv[0], options.pnn));
3071 talloc_free(tmp_ctx);
3072 return ret;
3075 printf("Public IP[%s] info on node %u\n",
3076 ctdb_addr_to_str(&info->ip.addr),
3077 options.pnn);
3079 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3080 ctdb_addr_to_str(&info->ip.addr),
3081 info->ip.pnn, info->num);
3083 for (i=0; i<info->num; i++) {
3084 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3086 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3087 i+1, info->ifaces[i].name,
3088 info->ifaces[i].link_state?"up":"down",
3089 (unsigned int)info->ifaces[i].references,
3090 (i==info->active_idx)?" (active)":"");
3093 talloc_free(tmp_ctx);
3094 return 0;
3098 display interfaces status
3100 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3102 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3103 int i;
3104 struct ctdb_control_get_ifaces *ifaces;
3105 int ret;
3107 /* read the public ip list from this node */
3108 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3109 if (ret != 0) {
3110 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3111 options.pnn));
3112 talloc_free(tmp_ctx);
3113 return -1;
3116 if (options.machinereadable){
3117 printf(":Name:LinkStatus:References:\n");
3118 } else {
3119 printf("Interfaces on node %u\n", options.pnn);
3122 for (i=0; i<ifaces->num; i++) {
3123 if (options.machinereadable){
3124 printf(":%s:%s:%u\n",
3125 ifaces->ifaces[i].name,
3126 ifaces->ifaces[i].link_state?"1":"0",
3127 (unsigned int)ifaces->ifaces[i].references);
3128 } else {
3129 printf("name:%s link:%s references:%u\n",
3130 ifaces->ifaces[i].name,
3131 ifaces->ifaces[i].link_state?"up":"down",
3132 (unsigned int)ifaces->ifaces[i].references);
3136 talloc_free(tmp_ctx);
3137 return 0;
3142 set link status of an interface
3144 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3146 int ret;
3147 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3148 struct ctdb_control_iface_info info;
3150 ZERO_STRUCT(info);
3152 if (argc != 2) {
3153 usage();
3156 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3157 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3158 argv[0]));
3159 talloc_free(tmp_ctx);
3160 return -1;
3162 strcpy(info.name, argv[0]);
3164 if (strcmp(argv[1], "up") == 0) {
3165 info.link_state = 1;
3166 } else if (strcmp(argv[1], "down") == 0) {
3167 info.link_state = 0;
3168 } else {
3169 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3170 argv[1]));
3171 talloc_free(tmp_ctx);
3172 return -1;
3175 /* read the public ip list from this node */
3176 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3177 tmp_ctx, &info);
3178 if (ret != 0) {
3179 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3180 argv[0], options.pnn));
3181 talloc_free(tmp_ctx);
3182 return ret;
3185 talloc_free(tmp_ctx);
3186 return 0;
3190 display pid of a ctdb daemon
3192 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3194 uint32_t pid;
3195 int ret;
3197 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3198 if (ret != 0) {
3199 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3200 return ret;
3202 printf("Pid:%d\n", pid);
3204 return 0;
3207 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3209 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3210 void *data,
3211 update_flags_handler_t handler,
3212 uint32_t flag,
3213 const char *desc,
3214 bool set_flag)
3216 struct ctdb_node_map *nodemap = NULL;
3217 bool flag_is_set;
3218 int ret;
3220 /* Check if the node is already in the desired state */
3221 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3222 if (ret != 0) {
3223 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3224 exit(10);
3226 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3227 if (set_flag == flag_is_set) {
3228 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3229 (set_flag ? "already" : "not"), desc));
3230 return 0;
3233 do {
3234 if (!handler(ctdb, data)) {
3235 DEBUG(DEBUG_WARNING,
3236 ("Failed to send control to set state %s on node %u, try again\n",
3237 desc, options.pnn));
3240 sleep(1);
3242 /* Read the nodemap and verify the change took effect.
3243 * Even if the above control/hanlder timed out then it
3244 * could still have worked!
3246 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3247 ctdb, &nodemap);
3248 if (ret != 0) {
3249 DEBUG(DEBUG_WARNING,
3250 ("Unable to get nodemap from local node, try again\n"));
3252 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3253 } while (nodemap == NULL || (set_flag != flag_is_set));
3255 return ipreallocate(ctdb);
3258 /* Administratively disable a node */
3259 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3261 int ret;
3263 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3264 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3265 return ret == 0;
3268 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3270 return update_flags_and_ipreallocate(ctdb, NULL,
3271 update_flags_disabled,
3272 NODE_FLAGS_PERMANENTLY_DISABLED,
3273 "disabled",
3274 true /* set_flag*/);
3277 /* Administratively re-enable a node */
3278 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3280 int ret;
3282 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3283 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3284 return ret == 0;
3287 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3289 return update_flags_and_ipreallocate(ctdb, NULL,
3290 update_flags_not_disabled,
3291 NODE_FLAGS_PERMANENTLY_DISABLED,
3292 "disabled",
3293 false /* set_flag*/);
3296 /* Stop a node */
3297 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3299 int ret;
3301 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3303 return ret == 0;
3306 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3308 return update_flags_and_ipreallocate(ctdb, NULL,
3309 update_flags_stopped,
3310 NODE_FLAGS_STOPPED,
3311 "stopped",
3312 true /* set_flag*/);
3315 /* Continue a stopped node */
3316 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3318 int ret;
3320 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3322 return ret == 0;
3325 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3327 return update_flags_and_ipreallocate(ctdb, NULL,
3328 update_flags_not_stopped,
3329 NODE_FLAGS_STOPPED,
3330 "stopped",
3331 false /* set_flag */);
3334 static uint32_t get_generation(struct ctdb_context *ctdb)
3336 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3337 struct ctdb_vnn_map *vnnmap=NULL;
3338 int ret;
3339 uint32_t generation;
3341 /* wait until the recmaster is not in recovery mode */
3342 while (1) {
3343 uint32_t recmode, recmaster;
3345 if (vnnmap != NULL) {
3346 talloc_free(vnnmap);
3347 vnnmap = NULL;
3350 /* get the recmaster */
3351 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3352 if (ret != 0) {
3353 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3354 talloc_free(tmp_ctx);
3355 exit(10);
3358 /* get recovery mode */
3359 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3360 if (ret != 0) {
3361 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3362 talloc_free(tmp_ctx);
3363 exit(10);
3366 /* get the current generation number */
3367 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3368 if (ret != 0) {
3369 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3370 talloc_free(tmp_ctx);
3371 exit(10);
3374 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3375 generation = vnnmap->generation;
3376 talloc_free(tmp_ctx);
3377 return generation;
3379 sleep(1);
3383 /* Ban a node */
3384 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3386 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3387 int ret;
3389 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3391 return ret == 0;
3394 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3396 struct ctdb_ban_time bantime;
3398 if (argc < 1) {
3399 usage();
3402 bantime.pnn = options.pnn;
3403 bantime.time = strtoul(argv[0], NULL, 0);
3405 if (bantime.time == 0) {
3406 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3407 return -1;
3410 return update_flags_and_ipreallocate(ctdb, &bantime,
3411 update_state_banned,
3412 NODE_FLAGS_BANNED,
3413 "banned",
3414 true /* set_flag*/);
3418 /* Unban a node */
3419 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3421 struct ctdb_ban_time bantime;
3423 bantime.pnn = options.pnn;
3424 bantime.time = 0;
3426 return update_flags_and_ipreallocate(ctdb, &bantime,
3427 update_state_banned,
3428 NODE_FLAGS_BANNED,
3429 "banned",
3430 false /* set_flag*/);
3434 show ban information for a node
3436 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3438 int ret;
3439 struct ctdb_node_map *nodemap=NULL;
3440 struct ctdb_ban_time *bantime;
3442 /* verify the node exists */
3443 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3444 if (ret != 0) {
3445 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3446 return ret;
3449 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3450 if (ret != 0) {
3451 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3452 return -1;
3455 if (bantime->time == 0) {
3456 printf("Node %u is not banned\n", bantime->pnn);
3457 } else {
3458 printf("Node %u is banned, %d seconds remaining\n",
3459 bantime->pnn, bantime->time);
3462 return 0;
3466 shutdown a daemon
3468 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3470 int ret;
3472 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3473 if (ret != 0) {
3474 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3475 return ret;
3478 return 0;
3482 trigger a recovery
3484 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3486 int ret;
3487 uint32_t generation, next_generation;
3489 /* record the current generation number */
3490 generation = get_generation(ctdb);
3492 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3493 if (ret != 0) {
3494 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3495 return ret;
3498 /* wait until we are in a new generation */
3499 while (1) {
3500 next_generation = get_generation(ctdb);
3501 if (next_generation != generation) {
3502 return 0;
3504 sleep(1);
3507 return 0;
3512 display monitoring mode of a remote node
3514 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3516 uint32_t monmode;
3517 int ret;
3519 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3520 if (ret != 0) {
3521 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3522 return ret;
3524 if (!options.machinereadable){
3525 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3526 } else {
3527 printf(":mode:\n");
3528 printf(":%d:\n",monmode);
3530 return 0;
3535 display capabilities of a remote node
3537 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3539 uint32_t capabilities;
3540 int ret;
3542 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3543 if (ret != 0) {
3544 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3545 return -1;
3548 if (!options.machinereadable){
3549 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3550 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3551 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3552 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3553 } else {
3554 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3555 printf(":%d:%d:%d:%d:\n",
3556 !!(capabilities&CTDB_CAP_RECMASTER),
3557 !!(capabilities&CTDB_CAP_LMASTER),
3558 !!(capabilities&CTDB_CAP_LVS),
3559 !!(capabilities&CTDB_CAP_NATGW));
3561 return 0;
3565 display lvs configuration
3568 static uint32_t lvs_exclude_flags[] = {
3569 /* Look for a nice healthy node */
3570 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3571 /* If not found, an UNHEALTHY node will do */
3572 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3576 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3578 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3579 struct ctdb_node_map *orig_nodemap=NULL;
3580 struct ctdb_node_map *nodemap;
3581 int i, ret;
3583 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3584 tmp_ctx, &orig_nodemap);
3585 if (ret != 0) {
3586 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3587 talloc_free(tmp_ctx);
3588 return -1;
3591 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3592 CTDB_CAP_LVS, false);
3593 if (nodemap == NULL) {
3594 /* No memory */
3595 ret = -1;
3596 goto done;
3599 ret = 0;
3601 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3602 struct ctdb_node_map *t =
3603 filter_nodemap_by_flags(ctdb, nodemap,
3604 lvs_exclude_flags[i]);
3605 if (t == NULL) {
3606 /* No memory */
3607 ret = -1;
3608 goto done;
3610 if (t->num > 0) {
3611 /* At least 1 node without excluded flags */
3612 int j;
3613 for (j = 0; j < t->num; j++) {
3614 printf("%d:%s\n", t->nodes[j].pnn,
3615 ctdb_addr_to_str(&t->nodes[j].addr));
3617 goto done;
3619 talloc_free(t);
3621 done:
3622 talloc_free(tmp_ctx);
3623 return ret;
3627 display who is the lvs master
3629 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3631 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3632 struct ctdb_node_map *nodemap=NULL;
3633 int i, ret;
3635 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3636 tmp_ctx, &nodemap);
3637 if (ret != 0) {
3638 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3639 talloc_free(tmp_ctx);
3640 return -1;
3643 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3644 struct ctdb_node_map *t =
3645 filter_nodemap_by_flags(ctdb, nodemap,
3646 lvs_exclude_flags[i]);
3647 if (t == NULL) {
3648 /* No memory */
3649 ret = -1;
3650 goto done;
3652 if (t->num > 0) {
3653 struct ctdb_node_map *n;
3654 n = filter_nodemap_by_capabilities(ctdb,
3656 CTDB_CAP_LVS,
3657 true);
3658 if (n == NULL) {
3659 /* No memory */
3660 ret = -1;
3661 goto done;
3663 if (n->num > 0) {
3664 ret = 0;
3665 printf(options.machinereadable ?
3666 "%d\n" : "Node %d is LVS master\n",
3667 n->nodes[0].pnn);
3668 goto done;
3671 talloc_free(t);
3674 printf("There is no LVS master\n");
3675 ret = 255;
3676 done:
3677 talloc_free(tmp_ctx);
3678 return ret;
3682 disable monitoring on a node
3684 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3687 int ret;
3689 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3690 if (ret != 0) {
3691 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3692 return ret;
3694 printf("Monitoring mode:%s\n","DISABLED");
3696 return 0;
3700 enable monitoring on a node
3702 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3705 int ret;
3707 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3708 if (ret != 0) {
3709 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3710 return ret;
3712 printf("Monitoring mode:%s\n","ACTIVE");
3714 return 0;
3718 display remote list of keys/data for a db
3720 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3722 const char *db_name;
3723 struct ctdb_db_context *ctdb_db;
3724 int ret;
3725 struct ctdb_dump_db_context c;
3726 uint8_t flags;
3728 if (argc < 1) {
3729 usage();
3732 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3733 return -1;
3736 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3737 if (ctdb_db == NULL) {
3738 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3739 return -1;
3742 if (options.printlmaster) {
3743 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3744 ctdb, &ctdb->vnn_map);
3745 if (ret != 0) {
3746 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3747 options.pnn));
3748 return ret;
3752 ZERO_STRUCT(c);
3753 c.f = stdout;
3754 c.printemptyrecords = (bool)options.printemptyrecords;
3755 c.printdatasize = (bool)options.printdatasize;
3756 c.printlmaster = (bool)options.printlmaster;
3757 c.printhash = (bool)options.printhash;
3758 c.printrecordflags = (bool)options.printrecordflags;
3760 /* traverse and dump the cluster tdb */
3761 ret = ctdb_dump_db(ctdb_db, &c);
3762 if (ret == -1) {
3763 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3764 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3765 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3766 db_name));
3767 return -1;
3769 talloc_free(ctdb_db);
3771 printf("Dumped %d records\n", ret);
3772 return 0;
3775 struct cattdb_data {
3776 struct ctdb_context *ctdb;
3777 uint32_t count;
3780 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3782 struct cattdb_data *d = private_data;
3783 struct ctdb_dump_db_context c;
3785 d->count++;
3787 ZERO_STRUCT(c);
3788 c.f = stdout;
3789 c.printemptyrecords = (bool)options.printemptyrecords;
3790 c.printdatasize = (bool)options.printdatasize;
3791 c.printlmaster = false;
3792 c.printhash = (bool)options.printhash;
3793 c.printrecordflags = true;
3795 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3799 cat the local tdb database using same format as catdb
3801 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3803 const char *db_name;
3804 struct ctdb_db_context *ctdb_db;
3805 struct cattdb_data d;
3806 uint8_t flags;
3808 if (argc < 1) {
3809 usage();
3812 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3813 return -1;
3816 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3817 if (ctdb_db == NULL) {
3818 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3819 return -1;
3822 /* traverse the local tdb */
3823 d.count = 0;
3824 d.ctdb = ctdb;
3825 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3826 printf("Failed to cattdb data\n");
3827 exit(10);
3829 talloc_free(ctdb_db);
3831 printf("Dumped %d records\n", d.count);
3832 return 0;
3836 display the content of a database key
3838 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3840 const char *db_name;
3841 struct ctdb_db_context *ctdb_db;
3842 struct ctdb_record_handle *h;
3843 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3844 TDB_DATA key, data;
3845 uint8_t flags;
3847 if (argc < 2) {
3848 usage();
3851 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3852 return -1;
3855 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3856 if (ctdb_db == NULL) {
3857 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3858 return -1;
3861 key.dptr = discard_const(argv[1]);
3862 key.dsize = strlen((char *)key.dptr);
3864 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3865 if (h == NULL) {
3866 printf("Failed to fetch record '%s' on node %d\n",
3867 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3868 talloc_free(tmp_ctx);
3869 exit(10);
3872 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3874 talloc_free(tmp_ctx);
3875 talloc_free(ctdb_db);
3876 return 0;
3880 display the content of a database key
3882 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3884 const char *db_name;
3885 struct ctdb_db_context *ctdb_db;
3886 struct ctdb_record_handle *h;
3887 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3888 TDB_DATA key, data;
3889 uint8_t flags;
3891 if (argc < 3) {
3892 usage();
3895 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3896 return -1;
3899 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3900 if (ctdb_db == NULL) {
3901 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3902 return -1;
3905 key.dptr = discard_const(argv[1]);
3906 key.dsize = strlen((char *)key.dptr);
3908 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3909 if (h == NULL) {
3910 printf("Failed to fetch record '%s' on node %d\n",
3911 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3912 talloc_free(tmp_ctx);
3913 exit(10);
3916 data.dptr = discard_const(argv[2]);
3917 data.dsize = strlen((char *)data.dptr);
3919 if (ctdb_record_store(h, data) != 0) {
3920 printf("Failed to store record\n");
3923 talloc_free(h);
3924 talloc_free(tmp_ctx);
3925 talloc_free(ctdb_db);
3926 return 0;
3930 fetch a record from a persistent database
3932 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3934 const char *db_name;
3935 struct ctdb_db_context *ctdb_db;
3936 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3937 struct ctdb_transaction_handle *h;
3938 TDB_DATA key, data;
3939 int fd, ret;
3940 bool persistent;
3941 uint8_t flags;
3943 if (argc < 2) {
3944 talloc_free(tmp_ctx);
3945 usage();
3948 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3949 talloc_free(tmp_ctx);
3950 return -1;
3953 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3954 if (!persistent) {
3955 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3956 talloc_free(tmp_ctx);
3957 return -1;
3960 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3961 if (ctdb_db == NULL) {
3962 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3963 talloc_free(tmp_ctx);
3964 return -1;
3967 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3968 if (h == NULL) {
3969 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3970 talloc_free(tmp_ctx);
3971 return -1;
3974 key.dptr = discard_const(argv[1]);
3975 key.dsize = strlen(argv[1]);
3976 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3977 if (ret != 0) {
3978 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3979 talloc_free(tmp_ctx);
3980 return -1;
3983 if (data.dsize == 0 || data.dptr == NULL) {
3984 DEBUG(DEBUG_ERR,("Record is empty\n"));
3985 talloc_free(tmp_ctx);
3986 return -1;
3989 if (argc == 3) {
3990 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3991 if (fd == -1) {
3992 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3993 talloc_free(tmp_ctx);
3994 return -1;
3996 sys_write(fd, data.dptr, data.dsize);
3997 close(fd);
3998 } else {
3999 sys_write(1, data.dptr, data.dsize);
4002 /* abort the transaction */
4003 talloc_free(h);
4006 talloc_free(tmp_ctx);
4007 return 0;
4011 fetch a record from a tdb-file
4013 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4015 const char *tdb_file;
4016 TDB_CONTEXT *tdb;
4017 TDB_DATA key, data;
4018 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4019 int fd;
4021 if (argc < 2) {
4022 usage();
4025 tdb_file = argv[0];
4027 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4028 if (tdb == NULL) {
4029 printf("Failed to open TDB file %s\n", tdb_file);
4030 return -1;
4033 if (!strncmp(argv[1], "0x", 2)) {
4034 key = hextodata(tmp_ctx, argv[1] + 2);
4035 if (key.dsize == 0) {
4036 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4037 return -1;
4039 } else {
4040 key.dptr = discard_const(argv[1]);
4041 key.dsize = strlen(argv[1]);
4044 data = tdb_fetch(tdb, key);
4045 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4046 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4047 tdb_close(tdb);
4048 return -1;
4051 tdb_close(tdb);
4053 if (argc == 3) {
4054 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4055 if (fd == -1) {
4056 printf("Failed to open output file %s\n", argv[2]);
4057 return -1;
4059 if (options.verbose){
4060 sys_write(fd, data.dptr, data.dsize);
4061 } else {
4062 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4064 close(fd);
4065 } else {
4066 if (options.verbose){
4067 sys_write(1, data.dptr, data.dsize);
4068 } else {
4069 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4073 talloc_free(tmp_ctx);
4074 return 0;
4078 store a record and header to a tdb-file
4080 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4082 const char *tdb_file;
4083 TDB_CONTEXT *tdb;
4084 TDB_DATA key, value, data;
4085 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4086 struct ctdb_ltdb_header header;
4088 if (argc < 3) {
4089 usage();
4092 tdb_file = argv[0];
4094 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4095 if (tdb == NULL) {
4096 printf("Failed to open TDB file %s\n", tdb_file);
4097 return -1;
4100 if (!strncmp(argv[1], "0x", 2)) {
4101 key = hextodata(tmp_ctx, argv[1] + 2);
4102 if (key.dsize == 0) {
4103 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4104 return -1;
4106 } else {
4107 key.dptr = discard_const(argv[1]);
4108 key.dsize = strlen(argv[1]);
4111 if (!strncmp(argv[2], "0x", 2)) {
4112 value = hextodata(tmp_ctx, argv[2] + 2);
4113 if (value.dsize == 0) {
4114 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4115 return -1;
4117 } else {
4118 value.dptr = discard_const(argv[2]);
4119 value.dsize = strlen(argv[2]);
4122 ZERO_STRUCT(header);
4123 if (argc > 3) {
4124 header.rsn = atoll(argv[3]);
4126 if (argc > 4) {
4127 header.dmaster = atoi(argv[4]);
4129 if (argc > 5) {
4130 header.flags = atoi(argv[5]);
4133 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4134 data.dptr = talloc_size(tmp_ctx, data.dsize);
4135 if (data.dptr == NULL) {
4136 printf("Failed to allocate header+value\n");
4137 return -1;
4140 *(struct ctdb_ltdb_header *)data.dptr = header;
4141 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4143 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4144 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4145 tdb_close(tdb);
4146 return -1;
4149 tdb_close(tdb);
4151 talloc_free(tmp_ctx);
4152 return 0;
4156 write a record to a persistent database
4158 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4160 const char *db_name;
4161 struct ctdb_db_context *ctdb_db;
4162 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4163 struct ctdb_transaction_handle *h;
4164 struct stat st;
4165 TDB_DATA key, data;
4166 int fd, ret;
4168 if (argc < 3) {
4169 talloc_free(tmp_ctx);
4170 usage();
4173 fd = open(argv[2], O_RDONLY);
4174 if (fd == -1) {
4175 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4176 talloc_free(tmp_ctx);
4177 return -1;
4180 ret = fstat(fd, &st);
4181 if (ret == -1) {
4182 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4183 close(fd);
4184 talloc_free(tmp_ctx);
4185 return -1;
4188 if (!S_ISREG(st.st_mode)) {
4189 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4190 close(fd);
4191 talloc_free(tmp_ctx);
4192 return -1;
4195 data.dsize = st.st_size;
4196 if (data.dsize == 0) {
4197 data.dptr = NULL;
4198 } else {
4199 data.dptr = talloc_size(tmp_ctx, data.dsize);
4200 if (data.dptr == NULL) {
4201 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4202 close(fd);
4203 talloc_free(tmp_ctx);
4204 return -1;
4206 ret = sys_read(fd, data.dptr, data.dsize);
4207 if (ret != data.dsize) {
4208 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4209 close(fd);
4210 talloc_free(tmp_ctx);
4211 return -1;
4214 close(fd);
4217 db_name = argv[0];
4219 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4220 if (ctdb_db == NULL) {
4221 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4222 talloc_free(tmp_ctx);
4223 return -1;
4226 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4227 if (h == NULL) {
4228 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4229 talloc_free(tmp_ctx);
4230 return -1;
4233 key.dptr = discard_const(argv[1]);
4234 key.dsize = strlen(argv[1]);
4235 ret = ctdb_transaction_store(h, key, data);
4236 if (ret != 0) {
4237 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4238 talloc_free(tmp_ctx);
4239 return -1;
4242 ret = ctdb_transaction_commit(h);
4243 if (ret != 0) {
4244 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4245 talloc_free(tmp_ctx);
4246 return -1;
4250 talloc_free(tmp_ctx);
4251 return 0;
4255 * delete a record from a persistent database
4257 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4259 const char *db_name;
4260 struct ctdb_db_context *ctdb_db;
4261 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4262 struct ctdb_transaction_handle *h;
4263 TDB_DATA key;
4264 int ret;
4265 bool persistent;
4266 uint8_t flags;
4268 if (argc < 2) {
4269 talloc_free(tmp_ctx);
4270 usage();
4273 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4274 talloc_free(tmp_ctx);
4275 return -1;
4278 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4279 if (!persistent) {
4280 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4281 talloc_free(tmp_ctx);
4282 return -1;
4285 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4286 if (ctdb_db == NULL) {
4287 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4288 talloc_free(tmp_ctx);
4289 return -1;
4292 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4293 if (h == NULL) {
4294 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4295 talloc_free(tmp_ctx);
4296 return -1;
4299 key.dptr = discard_const(argv[1]);
4300 key.dsize = strlen(argv[1]);
4301 ret = ctdb_transaction_store(h, key, tdb_null);
4302 if (ret != 0) {
4303 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4304 talloc_free(tmp_ctx);
4305 return -1;
4308 ret = ctdb_transaction_commit(h);
4309 if (ret != 0) {
4310 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4311 talloc_free(tmp_ctx);
4312 return -1;
4315 talloc_free(tmp_ctx);
4316 return 0;
4319 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4320 TDB_DATA *data)
4322 const char *t;
4323 size_t n;
4324 const char *ret; /* Next byte after successfully parsed value */
4326 /* Error, unless someone says otherwise */
4327 ret = NULL;
4328 /* Indicates no value to parse */
4329 *data = tdb_null;
4331 /* Skip whitespace */
4332 n = strspn(s, " \t");
4333 t = s + n;
4335 if (t[0] == '"') {
4336 /* Quoted ASCII string - no wide characters! */
4337 t++;
4338 n = strcspn(t, "\"");
4339 if (t[n] == '"') {
4340 if (n > 0) {
4341 data->dsize = n;
4342 data->dptr = talloc_memdup(mem_ctx, t, n);
4343 CTDB_NOMEM_ABORT(data->dptr);
4345 ret = t + n + 1;
4346 } else {
4347 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4349 } else {
4350 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4353 return ret;
4356 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4357 TDB_DATA *key, TDB_DATA *value)
4359 char line [1024]; /* FIXME: make this more flexible? */
4360 const char *t;
4361 char *ptr;
4363 ptr = fgets(line, sizeof(line), file);
4365 if (ptr == NULL) {
4366 return false;
4369 /* Get key */
4370 t = ptrans_parse_string(mem_ctx, line, key);
4371 if (t == NULL || key->dptr == NULL) {
4372 /* Line Ignored but not EOF */
4373 return true;
4376 /* Get value */
4377 t = ptrans_parse_string(mem_ctx, t, value);
4378 if (t == NULL) {
4379 /* Line Ignored but not EOF */
4380 talloc_free(key->dptr);
4381 *key = tdb_null;
4382 return true;
4385 return true;
4389 * Update a persistent database as per file/stdin
4391 static int control_ptrans(struct ctdb_context *ctdb,
4392 int argc, const char **argv)
4394 const char *db_name;
4395 struct ctdb_db_context *ctdb_db;
4396 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4397 struct ctdb_transaction_handle *h;
4398 TDB_DATA key, value;
4399 FILE *file;
4400 int ret;
4402 if (argc < 1) {
4403 talloc_free(tmp_ctx);
4404 usage();
4407 file = stdin;
4408 if (argc == 2) {
4409 file = fopen(argv[1], "r");
4410 if (file == NULL) {
4411 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4412 talloc_free(tmp_ctx);
4413 return -1;
4417 db_name = argv[0];
4419 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4420 if (ctdb_db == NULL) {
4421 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4422 goto error;
4425 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4426 if (h == NULL) {
4427 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4428 goto error;
4431 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4432 if (key.dsize != 0) {
4433 ret = ctdb_transaction_store(h, key, value);
4434 /* Minimise memory use */
4435 talloc_free(key.dptr);
4436 if (value.dptr != NULL) {
4437 talloc_free(value.dptr);
4439 if (ret != 0) {
4440 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4441 ctdb_transaction_cancel(h);
4442 goto error;
4447 ret = ctdb_transaction_commit(h);
4448 if (ret != 0) {
4449 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4450 goto error;
4453 if (file != stdin) {
4454 fclose(file);
4456 talloc_free(tmp_ctx);
4457 return 0;
4459 error:
4460 if (file != stdin) {
4461 fclose(file);
4464 talloc_free(tmp_ctx);
4465 return -1;
4469 check if a service is bound to a port or not
4471 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4473 int s, ret;
4474 int v;
4475 int port;
4476 struct sockaddr_in sin;
4478 if (argc != 1) {
4479 printf("Use: ctdb chktcport <port>\n");
4480 return EINVAL;
4483 port = atoi(argv[0]);
4485 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4486 if (s == -1) {
4487 printf("Failed to open local socket\n");
4488 return errno;
4491 v = fcntl(s, F_GETFL, 0);
4492 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4493 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4496 bzero(&sin, sizeof(sin));
4497 sin.sin_family = PF_INET;
4498 sin.sin_port = htons(port);
4499 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4500 close(s);
4501 if (ret == -1) {
4502 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4503 return errno;
4506 return 0;
4510 /* Reload public IPs on a specified nodes */
4511 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4513 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4514 uint32_t *nodes;
4515 uint32_t pnn_mode;
4516 uint32_t timeout;
4517 int ret;
4519 assert_single_node_only();
4521 if (argc > 1) {
4522 usage();
4525 /* Determine the nodes where IPs need to be reloaded */
4526 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4527 options.pnn, true, &nodes, &pnn_mode)) {
4528 ret = -1;
4529 goto done;
4532 again:
4533 /* Disable takeover runs on all connected nodes. A reply
4534 * indicating success is needed from each node so all nodes
4535 * will need to be active. This will retry until maxruntime
4536 * is exceeded, hence no error handling.
4538 * A check could be added to not allow reloading of IPs when
4539 * there are disconnected nodes. However, this should
4540 * probably be left up to the administrator.
4542 timeout = LONGTIMEOUT;
4543 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4544 "Disable takeover runs", true);
4546 /* Now tell all the desired nodes to reload their public IPs.
4547 * Keep trying this until it succeeds. This assumes all
4548 * failures are transient, which might not be true...
4550 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4551 nodes, 0, LONGTIMELIMIT(),
4552 false, tdb_null,
4553 NULL, NULL, NULL) != 0) {
4554 DEBUG(DEBUG_ERR,
4555 ("Unable to reload IPs on some nodes, try again.\n"));
4556 goto again;
4559 /* It isn't strictly necessary to wait until takeover runs are
4560 * re-enabled but doing so can't hurt.
4562 timeout = 0;
4563 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4564 "Enable takeover runs", true);
4566 ipreallocate(ctdb);
4568 ret = 0;
4569 done:
4570 talloc_free(tmp_ctx);
4571 return ret;
4575 display a list of the databases on a remote ctdb
4577 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4579 int i, ret;
4580 struct ctdb_dbid_map *dbmap=NULL;
4582 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4583 if (ret != 0) {
4584 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4585 return ret;
4588 if(options.machinereadable){
4589 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4590 for(i=0;i<dbmap->num;i++){
4591 const char *path;
4592 const char *name;
4593 const char *health;
4594 bool persistent;
4595 bool readonly;
4596 bool sticky;
4598 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4599 dbmap->dbs[i].dbid, ctdb, &path);
4600 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4601 dbmap->dbs[i].dbid, ctdb, &name);
4602 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4603 dbmap->dbs[i].dbid, ctdb, &health);
4604 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4605 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4606 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4607 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4608 dbmap->dbs[i].dbid, name, path,
4609 !!(persistent), !!(sticky),
4610 !!(health), !!(readonly));
4612 return 0;
4615 printf("Number of databases:%d\n", dbmap->num);
4616 for(i=0;i<dbmap->num;i++){
4617 const char *path;
4618 const char *name;
4619 const char *health;
4620 bool persistent;
4621 bool readonly;
4622 bool sticky;
4624 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4625 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4626 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4627 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4628 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4629 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4630 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4631 dbmap->dbs[i].dbid, name, path,
4632 persistent?" PERSISTENT":"",
4633 sticky?" STICKY":"",
4634 readonly?" READONLY":"",
4635 health?" UNHEALTHY":"");
4638 return 0;
4642 display the status of a database on a remote ctdb
4644 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4646 const char *db_name;
4647 uint32_t db_id;
4648 uint8_t flags;
4649 const char *path;
4650 const char *health;
4652 if (argc < 1) {
4653 usage();
4656 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4657 return -1;
4660 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4661 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4662 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4663 db_id, db_name, path,
4664 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4665 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4666 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4667 (health ? health : "OK"));
4669 return 0;
4673 check if the local node is recmaster or not
4674 it will return 1 if this node is the recmaster and 0 if it is not
4675 or if the local ctdb daemon could not be contacted
4677 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4679 uint32_t mypnn, recmaster;
4680 int ret;
4682 assert_single_node_only();
4684 mypnn = getpnn(ctdb);
4686 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4687 if (ret != 0) {
4688 printf("Failed to get the recmaster\n");
4689 return 1;
4692 if (recmaster != mypnn) {
4693 printf("this node is not the recmaster\n");
4694 return 1;
4697 printf("this node is the recmaster\n");
4698 return 0;
4702 ping a node
4704 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4706 int ret;
4707 struct timeval tv = timeval_current();
4708 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4709 if (ret == -1) {
4710 printf("Unable to get ping response from node %u\n", options.pnn);
4711 return -1;
4712 } else {
4713 printf("response from %u time=%.6f sec (%d clients)\n",
4714 options.pnn, timeval_elapsed(&tv), ret);
4716 return 0;
4721 get a node's runstate
4723 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4725 int ret;
4726 enum ctdb_runstate runstate;
4728 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4729 if (ret == -1) {
4730 printf("Unable to get runstate response from node %u\n",
4731 options.pnn);
4732 return -1;
4733 } else {
4734 bool found = true;
4735 enum ctdb_runstate t;
4736 int i;
4737 for (i=0; i<argc; i++) {
4738 found = false;
4739 t = runstate_from_string(argv[i]);
4740 if (t == CTDB_RUNSTATE_UNKNOWN) {
4741 printf("Invalid run state (%s)\n", argv[i]);
4742 return -1;
4745 if (t == runstate) {
4746 found = true;
4747 break;
4751 if (!found) {
4752 printf("CTDB not in required run state (got %s)\n",
4753 runstate_to_string((enum ctdb_runstate)runstate));
4754 return -1;
4758 printf("%s\n", runstate_to_string(runstate));
4759 return 0;
4764 get a tunable
4766 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4768 const char *name;
4769 uint32_t value;
4770 int ret;
4772 if (argc < 1) {
4773 usage();
4776 name = argv[0];
4777 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4778 if (ret != 0) {
4779 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4780 return -1;
4783 printf("%-23s = %u\n", name, value);
4784 return 0;
4788 set a tunable
4790 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4792 const char *name;
4793 uint32_t value;
4794 int ret;
4796 if (argc < 2) {
4797 usage();
4800 name = argv[0];
4801 value = strtoul(argv[1], NULL, 0);
4803 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4804 if (ret == -1) {
4805 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4806 return -1;
4808 return 0;
4812 list all tunables
4814 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4816 uint32_t count;
4817 const char **list;
4818 int ret, i;
4820 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4821 if (ret == -1) {
4822 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4823 return -1;
4826 for (i=0;i<count;i++) {
4827 control_getvar(ctdb, 1, &list[i]);
4830 talloc_free(list);
4832 return 0;
4836 display debug level on a node
4838 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4840 int ret;
4841 int32_t level;
4843 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4844 if (ret != 0) {
4845 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4846 return ret;
4847 } else {
4848 const char *desc = get_debug_by_level(level);
4849 if (desc == NULL) {
4850 /* This should never happen */
4851 desc = "Unknown";
4853 if (options.machinereadable){
4854 printf(":Name:Level:\n");
4855 printf(":%s:%d:\n", desc, level);
4856 } else {
4857 printf("Node %u is at debug level %s (%d)\n",
4858 options.pnn, desc, level);
4861 return 0;
4865 display reclock file of a node
4867 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4869 int ret;
4870 const char *reclock;
4872 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4873 if (ret != 0) {
4874 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4875 return ret;
4876 } else {
4877 if (options.machinereadable){
4878 if (reclock != NULL) {
4879 printf("%s", reclock);
4881 } else {
4882 if (reclock == NULL) {
4883 printf("No reclock file used.\n");
4884 } else {
4885 printf("Reclock file:%s\n", reclock);
4889 return 0;
4893 set the reclock file of a node
4895 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4897 int ret;
4898 const char *reclock;
4900 if (argc == 0) {
4901 reclock = NULL;
4902 } else if (argc == 1) {
4903 reclock = argv[0];
4904 } else {
4905 usage();
4908 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4909 if (ret != 0) {
4910 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4911 return ret;
4913 return 0;
4917 set the natgw state on/off
4919 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4921 int ret;
4922 uint32_t natgwstate;
4924 if (argc == 0) {
4925 usage();
4928 if (!strcmp(argv[0], "on")) {
4929 natgwstate = 1;
4930 } else if (!strcmp(argv[0], "off")) {
4931 natgwstate = 0;
4932 } else {
4933 usage();
4936 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4937 if (ret != 0) {
4938 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4939 return ret;
4942 return 0;
4946 set the lmaster role on/off
4948 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4950 int ret;
4951 uint32_t lmasterrole;
4953 if (argc == 0) {
4954 usage();
4957 if (!strcmp(argv[0], "on")) {
4958 lmasterrole = 1;
4959 } else if (!strcmp(argv[0], "off")) {
4960 lmasterrole = 0;
4961 } else {
4962 usage();
4965 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4966 if (ret != 0) {
4967 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4968 return ret;
4971 return 0;
4975 set the recmaster role on/off
4977 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4979 int ret;
4980 uint32_t recmasterrole;
4982 if (argc == 0) {
4983 usage();
4986 if (!strcmp(argv[0], "on")) {
4987 recmasterrole = 1;
4988 } else if (!strcmp(argv[0], "off")) {
4989 recmasterrole = 0;
4990 } else {
4991 usage();
4994 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4995 if (ret != 0) {
4996 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4997 return ret;
5000 return 0;
5004 set debug level on a node or all nodes
5006 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5008 int ret;
5009 int32_t level;
5011 if (argc == 0) {
5012 printf("You must specify the debug level. Valid levels are:\n");
5013 print_debug_levels(stdout);
5014 return 0;
5017 if (!parse_debug(argv[0], &level)) {
5018 printf("Invalid debug level, must be one of\n");
5019 print_debug_levels(stdout);
5020 return -1;
5023 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5024 if (ret != 0) {
5025 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5027 return 0;
5032 thaw a node
5034 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5036 int ret;
5037 uint32_t priority;
5039 if (argc == 1) {
5040 priority = strtol(argv[0], NULL, 0);
5041 } else {
5042 priority = 0;
5044 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5046 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5047 if (ret != 0) {
5048 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5050 return 0;
5055 attach to a database
5057 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5059 const char *db_name;
5060 struct ctdb_db_context *ctdb_db;
5061 bool persistent = false;
5063 if (argc < 1) {
5064 usage();
5066 db_name = argv[0];
5067 if (argc > 2) {
5068 usage();
5070 if (argc == 2) {
5071 if (strcmp(argv[1], "persistent") != 0) {
5072 usage();
5074 persistent = true;
5077 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5078 if (ctdb_db == NULL) {
5079 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5080 return -1;
5083 return 0;
5087 * detach from a database
5089 static int control_detach(struct ctdb_context *ctdb, int argc,
5090 const char **argv)
5092 uint32_t db_id;
5093 uint8_t flags;
5094 int ret, i, status = 0;
5095 struct ctdb_node_map *nodemap = NULL;
5096 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5097 uint32_t recmode;
5099 if (argc < 1) {
5100 usage();
5103 assert_single_node_only();
5105 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5106 &recmode);
5107 if (ret != 0) {
5108 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5109 "when recovery is active\n"));
5110 talloc_free(tmp_ctx);
5111 return -1;
5114 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5115 &nodemap);
5116 if (ret != 0) {
5117 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5118 options.pnn));
5119 talloc_free(tmp_ctx);
5120 return -1;
5123 for (i=0; i<nodemap->num; i++) {
5124 uint32_t value;
5126 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5127 continue;
5130 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5131 continue;
5134 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5135 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5136 "inactive (stopped or banned) node "
5137 "%u\n", nodemap->nodes[i].pnn));
5138 talloc_free(tmp_ctx);
5139 return -1;
5142 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5143 nodemap->nodes[i].pnn,
5144 "AllowClientDBAttach",
5145 &value);
5146 if (ret != 0) {
5147 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5148 "AllowClientDBAttach from node %u\n",
5149 nodemap->nodes[i].pnn));
5150 talloc_free(tmp_ctx);
5151 return -1;
5154 if (value == 1) {
5155 DEBUG(DEBUG_ERR, ("Database access is still active on "
5156 "node %u. Set AllowClientDBAttach=0 "
5157 "on all nodes.\n",
5158 nodemap->nodes[i].pnn));
5159 talloc_free(tmp_ctx);
5160 return -1;
5164 talloc_free(tmp_ctx);
5166 for (i=0; i<argc; i++) {
5167 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5168 continue;
5171 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5172 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5173 "cannot be detached\n", argv[i]));
5174 status = -1;
5175 continue;
5178 ret = ctdb_detach(ctdb, db_id);
5179 if (ret != 0) {
5180 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5181 argv[i]));
5182 status = ret;
5186 return status;
5190 set db priority
5192 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5194 struct ctdb_db_priority db_prio;
5195 int ret;
5197 if (argc < 2) {
5198 usage();
5201 db_prio.db_id = strtoul(argv[0], NULL, 0);
5202 db_prio.priority = strtoul(argv[1], NULL, 0);
5204 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5205 if (ret != 0) {
5206 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5207 return -1;
5210 return 0;
5214 get db priority
5216 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5218 uint32_t db_id, priority;
5219 int ret;
5221 if (argc < 1) {
5222 usage();
5225 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5226 return -1;
5229 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5230 if (ret != 0) {
5231 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5232 return -1;
5235 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5237 return 0;
5241 set the sticky records capability for a database
5243 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5245 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5246 uint32_t db_id;
5247 int ret;
5249 if (argc < 1) {
5250 usage();
5253 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5254 return -1;
5257 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5258 if (ret != 0) {
5259 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5260 talloc_free(tmp_ctx);
5261 return -1;
5264 talloc_free(tmp_ctx);
5265 return 0;
5269 set the readonly capability for a database
5271 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5273 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5274 uint32_t db_id;
5275 int ret;
5277 if (argc < 1) {
5278 usage();
5281 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5282 return -1;
5285 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5286 if (ret != 0) {
5287 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5288 talloc_free(tmp_ctx);
5289 return -1;
5292 talloc_free(tmp_ctx);
5293 return 0;
5297 get db seqnum
5299 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5301 uint32_t db_id;
5302 uint64_t seqnum;
5303 int ret;
5305 if (argc < 1) {
5306 usage();
5309 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5310 return -1;
5313 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5314 if (ret != 0) {
5315 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5316 return -1;
5319 printf("Sequence number:%lld\n", (long long)seqnum);
5321 return 0;
5325 run an eventscript on a node
5327 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5329 TDB_DATA data;
5330 int ret;
5331 int32_t res;
5332 char *errmsg;
5333 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5335 if (argc != 1) {
5336 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5337 return -1;
5340 data.dptr = (unsigned char *)discard_const(argv[0]);
5341 data.dsize = strlen((char *)data.dptr) + 1;
5343 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5345 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5346 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5347 if (ret != 0 || res != 0) {
5348 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5349 talloc_free(tmp_ctx);
5350 return -1;
5352 talloc_free(tmp_ctx);
5353 return 0;
5356 #define DB_VERSION 1
5357 #define MAX_DB_NAME 64
5358 struct db_file_header {
5359 unsigned long version;
5360 time_t timestamp;
5361 unsigned long persistent;
5362 unsigned long size;
5363 const char name[MAX_DB_NAME];
5366 struct backup_data {
5367 struct ctdb_marshall_buffer *records;
5368 uint32_t len;
5369 uint32_t total;
5370 bool traverse_error;
5373 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5375 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5376 struct ctdb_rec_data *rec;
5378 /* add the record */
5379 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5380 if (rec == NULL) {
5381 bd->traverse_error = true;
5382 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5383 return -1;
5385 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5386 if (bd->records == NULL) {
5387 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5388 bd->traverse_error = true;
5389 return -1;
5391 bd->records->count++;
5392 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5393 bd->len += rec->length;
5394 talloc_free(rec);
5396 bd->total++;
5397 return 0;
5401 * backup a database to a file
5403 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5405 const char *db_name;
5406 int ret;
5407 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5408 struct db_file_header dbhdr;
5409 struct ctdb_db_context *ctdb_db;
5410 struct backup_data *bd;
5411 int fh = -1;
5412 int status = -1;
5413 const char *reason = NULL;
5414 uint32_t db_id;
5415 uint8_t flags;
5417 assert_single_node_only();
5419 if (argc != 2) {
5420 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5421 return -1;
5424 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5425 return -1;
5428 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5429 db_id, tmp_ctx, &reason);
5430 if (ret != 0) {
5431 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5432 argv[0]));
5433 talloc_free(tmp_ctx);
5434 return -1;
5436 if (reason) {
5437 uint32_t allow_unhealthy = 0;
5439 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5440 "AllowUnhealthyDBRead",
5441 &allow_unhealthy);
5443 if (allow_unhealthy != 1) {
5444 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5445 argv[0], reason));
5447 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5448 allow_unhealthy));
5449 talloc_free(tmp_ctx);
5450 return -1;
5453 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5454 argv[0], argv[0]));
5455 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5456 "tunnable AllowUnhealthyDBRead = %u\n",
5457 allow_unhealthy));
5460 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5461 if (ctdb_db == NULL) {
5462 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5463 talloc_free(tmp_ctx);
5464 return -1;
5468 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5469 if (ret == -1) {
5470 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5471 talloc_free(tmp_ctx);
5472 return -1;
5476 bd = talloc_zero(tmp_ctx, struct backup_data);
5477 if (bd == NULL) {
5478 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5479 talloc_free(tmp_ctx);
5480 return -1;
5483 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5484 if (bd->records == NULL) {
5485 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5486 talloc_free(tmp_ctx);
5487 return -1;
5490 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5491 bd->records->db_id = ctdb_db->db_id;
5492 /* traverse the database collecting all records */
5493 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5494 bd->traverse_error) {
5495 DEBUG(DEBUG_ERR,("Traverse error\n"));
5496 talloc_free(tmp_ctx);
5497 return -1;
5500 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5503 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5504 if (fh == -1) {
5505 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5506 talloc_free(tmp_ctx);
5507 return -1;
5510 ZERO_STRUCT(dbhdr);
5511 dbhdr.version = DB_VERSION;
5512 dbhdr.timestamp = time(NULL);
5513 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5514 dbhdr.size = bd->len;
5515 if (strlen(argv[0]) >= MAX_DB_NAME) {
5516 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5517 goto done;
5519 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5520 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5521 if (ret == -1) {
5522 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5523 goto done;
5525 ret = sys_write(fh, bd->records, bd->len);
5526 if (ret == -1) {
5527 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5528 goto done;
5531 status = 0;
5532 done:
5533 if (fh != -1) {
5534 ret = close(fh);
5535 if (ret == -1) {
5536 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5540 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5542 talloc_free(tmp_ctx);
5543 return status;
5547 * restore a database from a file
5549 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5551 int ret;
5552 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5553 TDB_DATA outdata;
5554 TDB_DATA data;
5555 struct db_file_header dbhdr;
5556 struct ctdb_db_context *ctdb_db;
5557 struct ctdb_node_map *nodemap=NULL;
5558 struct ctdb_vnn_map *vnnmap=NULL;
5559 int i, fh;
5560 struct ctdb_control_wipe_database w;
5561 uint32_t *nodes;
5562 uint32_t generation;
5563 struct tm *tm;
5564 char tbuf[100];
5565 char *dbname;
5567 assert_single_node_only();
5569 if (argc < 1 || argc > 2) {
5570 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5571 return -1;
5574 fh = open(argv[0], O_RDONLY);
5575 if (fh == -1) {
5576 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5577 talloc_free(tmp_ctx);
5578 return -1;
5581 sys_read(fh, &dbhdr, sizeof(dbhdr));
5582 if (dbhdr.version != DB_VERSION) {
5583 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5584 close(fh);
5585 talloc_free(tmp_ctx);
5586 return -1;
5589 dbname = discard_const(dbhdr.name);
5590 if (argc == 2) {
5591 dbname = discard_const(argv[1]);
5594 outdata.dsize = dbhdr.size;
5595 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5596 if (outdata.dptr == NULL) {
5597 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5598 close(fh);
5599 talloc_free(tmp_ctx);
5600 return -1;
5602 sys_read(fh, outdata.dptr, outdata.dsize);
5603 close(fh);
5605 tm = localtime(&dbhdr.timestamp);
5606 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5607 printf("Restoring database '%s' from backup @ %s\n",
5608 dbname, tbuf);
5611 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5612 if (ctdb_db == NULL) {
5613 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5614 talloc_free(tmp_ctx);
5615 return -1;
5618 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5619 if (ret != 0) {
5620 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5621 talloc_free(tmp_ctx);
5622 return ret;
5626 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5627 if (ret != 0) {
5628 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5629 talloc_free(tmp_ctx);
5630 return ret;
5633 /* freeze all nodes */
5634 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5635 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5636 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5637 nodes, i,
5638 TIMELIMIT(),
5639 false, tdb_null,
5640 NULL, NULL,
5641 NULL) != 0) {
5642 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5643 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5644 talloc_free(tmp_ctx);
5645 return -1;
5649 generation = vnnmap->generation;
5650 data.dptr = (void *)&generation;
5651 data.dsize = sizeof(generation);
5653 /* start a cluster wide transaction */
5654 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5655 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5656 nodes, 0,
5657 TIMELIMIT(), false, data,
5658 NULL, NULL,
5659 NULL) != 0) {
5660 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5661 return -1;
5665 w.db_id = ctdb_db->db_id;
5666 w.transaction_id = generation;
5668 data.dptr = (void *)&w;
5669 data.dsize = sizeof(w);
5671 /* wipe all the remote databases. */
5672 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5673 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5674 nodes, 0,
5675 TIMELIMIT(), false, data,
5676 NULL, NULL,
5677 NULL) != 0) {
5678 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5679 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5680 talloc_free(tmp_ctx);
5681 return -1;
5684 /* push the database */
5685 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5686 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5687 nodes, 0,
5688 TIMELIMIT(), false, outdata,
5689 NULL, NULL,
5690 NULL) != 0) {
5691 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5692 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5693 talloc_free(tmp_ctx);
5694 return -1;
5697 data.dptr = (void *)&ctdb_db->db_id;
5698 data.dsize = sizeof(ctdb_db->db_id);
5700 /* mark the database as healthy */
5701 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5702 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5703 nodes, 0,
5704 TIMELIMIT(), false, data,
5705 NULL, NULL,
5706 NULL) != 0) {
5707 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5708 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5709 talloc_free(tmp_ctx);
5710 return -1;
5713 data.dptr = (void *)&generation;
5714 data.dsize = sizeof(generation);
5716 /* commit all the changes */
5717 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5718 nodes, 0,
5719 TIMELIMIT(), false, data,
5720 NULL, NULL,
5721 NULL) != 0) {
5722 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5723 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5724 talloc_free(tmp_ctx);
5725 return -1;
5729 /* thaw all nodes */
5730 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5731 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5732 nodes, 0,
5733 TIMELIMIT(),
5734 false, tdb_null,
5735 NULL, NULL,
5736 NULL) != 0) {
5737 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5738 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5739 talloc_free(tmp_ctx);
5740 return -1;
5744 talloc_free(tmp_ctx);
5745 return 0;
5749 * dump a database backup from a file
5751 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5753 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5754 TDB_DATA outdata;
5755 struct db_file_header dbhdr;
5756 int i, fh;
5757 struct tm *tm;
5758 char tbuf[100];
5759 struct ctdb_rec_data *rec = NULL;
5760 struct ctdb_marshall_buffer *m;
5761 struct ctdb_dump_db_context c;
5763 assert_single_node_only();
5765 if (argc != 1) {
5766 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5767 return -1;
5770 fh = open(argv[0], O_RDONLY);
5771 if (fh == -1) {
5772 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5773 talloc_free(tmp_ctx);
5774 return -1;
5777 sys_read(fh, &dbhdr, sizeof(dbhdr));
5778 if (dbhdr.version != DB_VERSION) {
5779 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5780 close(fh);
5781 talloc_free(tmp_ctx);
5782 return -1;
5785 outdata.dsize = dbhdr.size;
5786 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5787 if (outdata.dptr == NULL) {
5788 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5789 close(fh);
5790 talloc_free(tmp_ctx);
5791 return -1;
5793 sys_read(fh, outdata.dptr, outdata.dsize);
5794 close(fh);
5795 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5797 tm = localtime(&dbhdr.timestamp);
5798 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5799 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5800 dbhdr.name, m->db_id, tbuf);
5802 ZERO_STRUCT(c);
5803 c.f = stdout;
5804 c.printemptyrecords = (bool)options.printemptyrecords;
5805 c.printdatasize = (bool)options.printdatasize;
5806 c.printlmaster = false;
5807 c.printhash = (bool)options.printhash;
5808 c.printrecordflags = (bool)options.printrecordflags;
5810 for (i=0; i < m->count; i++) {
5811 uint32_t reqid = 0;
5812 TDB_DATA key, data;
5814 /* we do not want the header splitted, so we pass NULL*/
5815 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5816 NULL, &key, &data);
5818 ctdb_dumpdb_record(ctdb, key, data, &c);
5821 printf("Dumped %d records\n", i);
5822 talloc_free(tmp_ctx);
5823 return 0;
5827 * wipe a database from a file
5829 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5830 const char **argv)
5832 const char *db_name;
5833 int ret;
5834 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5835 TDB_DATA data;
5836 struct ctdb_db_context *ctdb_db;
5837 struct ctdb_node_map *nodemap = NULL;
5838 struct ctdb_vnn_map *vnnmap = NULL;
5839 int i;
5840 struct ctdb_control_wipe_database w;
5841 uint32_t *nodes;
5842 uint32_t generation;
5843 uint8_t flags;
5845 assert_single_node_only();
5847 if (argc != 1) {
5848 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5849 return -1;
5852 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5853 return -1;
5856 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5857 if (ctdb_db == NULL) {
5858 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5859 argv[0]));
5860 talloc_free(tmp_ctx);
5861 return -1;
5864 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5865 &nodemap);
5866 if (ret != 0) {
5867 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5868 options.pnn));
5869 talloc_free(tmp_ctx);
5870 return ret;
5873 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5874 &vnnmap);
5875 if (ret != 0) {
5876 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5877 options.pnn));
5878 talloc_free(tmp_ctx);
5879 return ret;
5882 /* freeze all nodes */
5883 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5884 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5885 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5886 nodes, i,
5887 TIMELIMIT(),
5888 false, tdb_null,
5889 NULL, NULL,
5890 NULL);
5891 if (ret != 0) {
5892 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5893 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5894 CTDB_RECOVERY_ACTIVE);
5895 talloc_free(tmp_ctx);
5896 return -1;
5900 generation = vnnmap->generation;
5901 data.dptr = (void *)&generation;
5902 data.dsize = sizeof(generation);
5904 /* start a cluster wide transaction */
5905 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5906 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5907 nodes, 0,
5908 TIMELIMIT(), false, data,
5909 NULL, NULL,
5910 NULL);
5911 if (ret!= 0) {
5912 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5913 "transactions.\n"));
5914 return -1;
5917 w.db_id = ctdb_db->db_id;
5918 w.transaction_id = generation;
5920 data.dptr = (void *)&w;
5921 data.dsize = sizeof(w);
5923 /* wipe all the remote databases. */
5924 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5925 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5926 nodes, 0,
5927 TIMELIMIT(), false, data,
5928 NULL, NULL,
5929 NULL) != 0) {
5930 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5931 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5932 talloc_free(tmp_ctx);
5933 return -1;
5936 data.dptr = (void *)&ctdb_db->db_id;
5937 data.dsize = sizeof(ctdb_db->db_id);
5939 /* mark the database as healthy */
5940 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5941 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5942 nodes, 0,
5943 TIMELIMIT(), false, data,
5944 NULL, NULL,
5945 NULL) != 0) {
5946 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5947 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5948 talloc_free(tmp_ctx);
5949 return -1;
5952 data.dptr = (void *)&generation;
5953 data.dsize = sizeof(generation);
5955 /* commit all the changes */
5956 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5957 nodes, 0,
5958 TIMELIMIT(), false, data,
5959 NULL, NULL,
5960 NULL) != 0) {
5961 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5962 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5963 talloc_free(tmp_ctx);
5964 return -1;
5967 /* thaw all nodes */
5968 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5969 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5970 nodes, 0,
5971 TIMELIMIT(),
5972 false, tdb_null,
5973 NULL, NULL,
5974 NULL) != 0) {
5975 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5976 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5977 talloc_free(tmp_ctx);
5978 return -1;
5981 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5983 talloc_free(tmp_ctx);
5984 return 0;
5988 dump memory usage
5990 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5992 TDB_DATA data;
5993 int ret;
5994 int32_t res;
5995 char *errmsg;
5996 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5997 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5998 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5999 if (ret != 0 || res != 0) {
6000 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6001 talloc_free(tmp_ctx);
6002 return -1;
6004 sys_write(1, data.dptr, data.dsize);
6005 talloc_free(tmp_ctx);
6006 return 0;
6010 handler for memory dumps
6012 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6013 TDB_DATA data, void *private_data)
6015 sys_write(1, data.dptr, data.dsize);
6016 exit(0);
6020 dump memory usage on the recovery daemon
6022 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6024 int ret;
6025 TDB_DATA data;
6026 struct srvid_request rd;
6028 rd.pnn = ctdb_get_pnn(ctdb);
6029 rd.srvid = getpid();
6031 /* register a message port for receiveing the reply so that we
6032 can receive the reply
6034 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6037 data.dptr = (uint8_t *)&rd;
6038 data.dsize = sizeof(rd);
6040 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6041 if (ret != 0) {
6042 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6043 return -1;
6046 /* this loop will terminate when we have received the reply */
6047 while (1) {
6048 event_loop_once(ctdb->ev);
6051 return 0;
6055 send a message to a srvid
6057 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6059 unsigned long srvid;
6060 int ret;
6061 TDB_DATA data;
6063 if (argc < 2) {
6064 usage();
6067 srvid = strtoul(argv[0], NULL, 0);
6069 data.dptr = (uint8_t *)discard_const(argv[1]);
6070 data.dsize= strlen(argv[1]);
6072 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6073 if (ret != 0) {
6074 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6075 return -1;
6078 return 0;
6082 handler for msglisten
6084 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6085 TDB_DATA data, void *private_data)
6087 int i;
6089 printf("Message received: ");
6090 for (i=0;i<data.dsize;i++) {
6091 printf("%c", data.dptr[i]);
6093 printf("\n");
6097 listen for messages on a messageport
6099 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6101 uint64_t srvid;
6103 srvid = getpid();
6105 /* register a message port and listen for messages
6107 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6108 printf("Listening for messages on srvid:%d\n", (int)srvid);
6110 while (1) {
6111 event_loop_once(ctdb->ev);
6114 return 0;
6118 list all nodes in the cluster
6119 we parse the nodes file directly
6121 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6123 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6124 struct pnn_node *pnn_nodes;
6125 struct pnn_node *pnn_node;
6127 assert_single_node_only();
6129 pnn_nodes = read_nodes_file(mem_ctx);
6130 if (pnn_nodes == NULL) {
6131 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6132 talloc_free(mem_ctx);
6133 return -1;
6136 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6137 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6138 if (options.machinereadable){
6139 printf(":%d:%s:\n", pnn_node->pnn, addr);
6140 } else {
6141 printf("%s\n", addr);
6144 talloc_free(mem_ctx);
6146 return 0;
6150 reload the nodes file on the local node
6152 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6154 int i, ret;
6155 int mypnn;
6156 struct ctdb_node_map *nodemap=NULL;
6158 assert_single_node_only();
6160 mypnn = ctdb_get_pnn(ctdb);
6162 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6163 if (ret != 0) {
6164 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6165 return ret;
6168 /* reload the nodes file on all remote nodes */
6169 for (i=0;i<nodemap->num;i++) {
6170 if (nodemap->nodes[i].pnn == mypnn) {
6171 continue;
6173 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6174 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6175 nodemap->nodes[i].pnn);
6176 if (ret != 0) {
6177 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6181 /* reload the nodes file on the local node */
6182 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6183 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6184 if (ret != 0) {
6185 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6188 /* initiate a recovery */
6189 control_recover(ctdb, argc, argv);
6191 return 0;
6195 static const struct {
6196 const char *name;
6197 int (*fn)(struct ctdb_context *, int, const char **);
6198 bool auto_all;
6199 bool without_daemon; /* can be run without daemon running ? */
6200 const char *msg;
6201 const char *args;
6202 } ctdb_commands[] = {
6203 { "version", control_version, true, true, "show version of ctdb" },
6204 { "status", control_status, true, false, "show node status" },
6205 { "uptime", control_uptime, true, false, "show node uptime" },
6206 { "ping", control_ping, true, false, "ping all nodes" },
6207 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6208 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6209 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6210 { "listvars", control_listvars, true, false, "list tunable variables"},
6211 { "statistics", control_statistics, false, false, "show statistics" },
6212 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6213 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6214 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6215 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6216 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6217 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6218 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6219 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6220 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6221 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6222 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6223 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6224 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6225 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6226 { "lvs", control_lvs, true, false, "show lvs configuration" },
6227 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6228 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6229 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6230 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6231 { "getdebug", control_getdebug, true, false, "get debug level" },
6232 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6233 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6234 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6235 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6236 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6237 { "disable", control_disable, true, false, "disable a nodes public IP" },
6238 { "enable", control_enable, true, false, "enable a nodes public IP" },
6239 { "stop", control_stop, true, false, "stop a node" },
6240 { "continue", control_continue, true, false, "re-start a stopped node" },
6241 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6242 { "unban", control_unban, true, false, "unban a node" },
6243 { "showban", control_showban, true, false, "show ban information"},
6244 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6245 { "recover", control_recover, true, false, "force recovery" },
6246 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6247 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6248 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6249 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6250 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6251 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6252 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6253 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6254 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6256 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6258 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6259 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6260 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6261 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6262 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6263 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6264 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6265 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6266 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6267 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6268 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6269 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6270 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6271 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6272 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6273 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6274 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6275 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6276 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6277 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6278 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6279 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6280 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6281 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6282 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6283 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6284 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6285 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6286 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6287 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6288 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6289 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6290 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6291 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6292 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6293 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6294 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6295 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6296 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6297 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6298 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6299 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6300 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6301 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6302 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6303 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6304 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6305 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6306 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6310 show usage message
6312 static void usage(void)
6314 int i;
6315 printf(
6316 "Usage: ctdb [options] <control>\n" \
6317 "Options:\n" \
6318 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6319 " -Y generate machinereadable output\n"
6320 " -v generate verbose output\n"
6321 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6322 printf("Controls:\n");
6323 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6324 printf(" %-15s %-27s %s\n",
6325 ctdb_commands[i].name,
6326 ctdb_commands[i].args?ctdb_commands[i].args:"",
6327 ctdb_commands[i].msg);
6329 exit(1);
6333 static void ctdb_alarm(int sig)
6335 printf("Maximum runtime exceeded - exiting\n");
6336 _exit(ERR_TIMEOUT);
6340 main program
6342 int main(int argc, const char *argv[])
6344 struct ctdb_context *ctdb;
6345 char *nodestring = NULL;
6346 struct poptOption popt_options[] = {
6347 POPT_AUTOHELP
6348 POPT_CTDB_CMDLINE
6349 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6350 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6351 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6352 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6353 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6354 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6355 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6356 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6357 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6358 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6359 POPT_TABLEEND
6361 int opt;
6362 const char **extra_argv;
6363 int extra_argc = 0;
6364 int ret=-1, i;
6365 poptContext pc;
6366 struct event_context *ev;
6367 const char *control;
6369 setlinebuf(stdout);
6371 /* set some defaults */
6372 options.maxruntime = 0;
6373 options.timelimit = 10;
6374 options.pnn = CTDB_CURRENT_NODE;
6376 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6378 while ((opt = poptGetNextOpt(pc)) != -1) {
6379 switch (opt) {
6380 default:
6381 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6382 poptBadOption(pc, 0), poptStrerror(opt)));
6383 exit(1);
6387 /* setup the remaining options for the main program to use */
6388 extra_argv = poptGetArgs(pc);
6389 if (extra_argv) {
6390 extra_argv++;
6391 while (extra_argv[extra_argc]) extra_argc++;
6394 if (extra_argc < 1) {
6395 usage();
6398 if (options.maxruntime == 0) {
6399 const char *ctdb_timeout;
6400 ctdb_timeout = getenv("CTDB_TIMEOUT");
6401 if (ctdb_timeout != NULL) {
6402 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6403 } else {
6404 /* default timeout is 120 seconds */
6405 options.maxruntime = 120;
6409 signal(SIGALRM, ctdb_alarm);
6410 alarm(options.maxruntime);
6412 control = extra_argv[0];
6414 /* Default value for CTDB_BASE - don't override */
6415 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6417 ev = event_context_init(NULL);
6418 if (!ev) {
6419 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6420 exit(1);
6423 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6424 if (strcmp(control, ctdb_commands[i].name) == 0) {
6425 break;
6429 if (i == ARRAY_SIZE(ctdb_commands)) {
6430 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6431 exit(1);
6434 if (ctdb_commands[i].without_daemon == true) {
6435 if (nodestring != NULL) {
6436 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6437 exit(1);
6439 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6442 /* initialise ctdb */
6443 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6445 if (ctdb == NULL) {
6446 uint32_t pnn;
6447 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6449 pnn = find_node_xpnn();
6450 if (pnn == -1) {
6451 DEBUG(DEBUG_ERR,
6452 ("Is this node part of a CTDB cluster?\n"));
6454 exit(1);
6457 /* setup the node number(s) to contact */
6458 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6459 &options.nodes, &options.pnn)) {
6460 usage();
6463 if (options.pnn == CTDB_CURRENT_NODE) {
6464 options.pnn = options.nodes[0];
6467 if (ctdb_commands[i].auto_all &&
6468 ((options.pnn == CTDB_BROADCAST_ALL) ||
6469 (options.pnn == CTDB_MULTICAST))) {
6470 int j;
6472 ret = 0;
6473 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6474 options.pnn = options.nodes[j];
6475 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6477 } else {
6478 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6481 talloc_free(ctdb);
6482 talloc_free(ev);
6483 (void)poptFreeContext(pc);
6485 return ret;