ctdb-vacuum: Track time for vacuuming in database statistics
[Samba.git] / ctdb / tools / ctdb.c
blobf0c87d91e5b6b7a163e4f60b92bc15a5c48c1971
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 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
729 "vacuum_latency MIN/AVG/MAX",
730 dbstat->vacuum.latency.min,
731 (dbstat->vacuum.latency.num ?
732 dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
733 0.0),
734 dbstat->vacuum.latency.max,
735 dbstat->vacuum.latency.num);
736 num_hot_keys = 0;
737 for (i=0; i<dbstat->num_hot_keys; i++) {
738 if (dbstat->hot_keys[i].count > 0) {
739 num_hot_keys++;
742 dbstat->num_hot_keys = num_hot_keys;
744 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
745 for (i = 0; i < dbstat->num_hot_keys; i++) {
746 int j;
747 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
748 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
749 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
751 printf("\n");
754 talloc_free(tmp_ctx);
755 return 0;
759 display uptime of remote node
761 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
763 int ret;
764 struct ctdb_uptime *uptime = NULL;
765 int tmp, days, hours, minutes, seconds;
767 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
768 if (ret != 0) {
769 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
770 return ret;
773 if (options.machinereadable){
774 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
775 printf(":%u:%u:%u:%lf\n",
776 (unsigned int)uptime->current_time.tv_sec,
777 (unsigned int)uptime->ctdbd_start_time.tv_sec,
778 (unsigned int)uptime->last_recovery_finished.tv_sec,
779 timeval_delta(&uptime->last_recovery_finished,
780 &uptime->last_recovery_started)
782 return 0;
785 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
787 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
788 seconds = tmp%60;
789 tmp /= 60;
790 minutes = tmp%60;
791 tmp /= 60;
792 hours = tmp%24;
793 tmp /= 24;
794 days = tmp;
795 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
797 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
798 seconds = tmp%60;
799 tmp /= 60;
800 minutes = tmp%60;
801 tmp /= 60;
802 hours = tmp%24;
803 tmp /= 24;
804 days = tmp;
805 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
807 printf("Duration of last recovery/failover: %lf seconds\n",
808 timeval_delta(&uptime->last_recovery_finished,
809 &uptime->last_recovery_started));
811 return 0;
815 show the PNN of the current node
817 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
819 uint32_t mypnn;
821 mypnn = getpnn(ctdb);
823 printf("PNN:%d\n", mypnn);
824 return 0;
828 struct pnn_node {
829 struct pnn_node *next, *prev;
830 ctdb_sock_addr addr;
831 int pnn;
834 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
835 const char *file)
837 int nlines;
838 char **lines;
839 int i, pnn;
840 struct pnn_node *pnn_nodes = NULL;
841 struct pnn_node *pnn_node;
843 lines = file_lines_load(file, &nlines, 0, mem_ctx);
844 if (lines == NULL) {
845 return NULL;
847 for (i=0, pnn=0; i<nlines; i++) {
848 char *node;
850 node = lines[i];
851 /* strip leading spaces */
852 while((*node == ' ') || (*node == '\t')) {
853 node++;
855 if (*node == '#') {
856 pnn++;
857 continue;
859 if (strcmp(node, "") == 0) {
860 continue;
862 pnn_node = talloc(mem_ctx, struct pnn_node);
863 pnn_node->pnn = pnn++;
865 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
866 DEBUG(DEBUG_ERR,
867 ("Invalid IP address '%s' in file %s\n",
868 node, file));
869 /* Caller will free mem_ctx */
870 return NULL;
873 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
876 return pnn_nodes;
879 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
881 const char *nodes_list;
883 /* read the nodes file */
884 nodes_list = getenv("CTDB_NODES");
885 if (nodes_list == NULL) {
886 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
887 getenv("CTDB_BASE"));
888 if (nodes_list == NULL) {
889 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
890 exit(1);
894 return read_pnn_node_file(mem_ctx, nodes_list);
898 show the PNN of the current node
899 discover the pnn by loading the nodes file and try to bind to all
900 addresses one at a time until the ip address is found.
902 static int find_node_xpnn(void)
904 TALLOC_CTX *mem_ctx = talloc_new(NULL);
905 struct pnn_node *pnn_nodes;
906 struct pnn_node *pnn_node;
907 int pnn;
909 pnn_nodes = read_nodes_file(mem_ctx);
910 if (pnn_nodes == NULL) {
911 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
912 talloc_free(mem_ctx);
913 return -1;
916 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
917 if (ctdb_sys_have_ip(&pnn_node->addr)) {
918 pnn = pnn_node->pnn;
919 talloc_free(mem_ctx);
920 return pnn;
924 printf("Failed to detect which PNN this node is\n");
925 talloc_free(mem_ctx);
926 return -1;
929 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
931 uint32_t pnn;
933 assert_single_node_only();
935 pnn = find_node_xpnn();
936 if (pnn == -1) {
937 return -1;
940 printf("PNN:%d\n", pnn);
941 return 0;
944 /* Helpers for ctdb status
946 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
948 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
949 int j;
950 bool ret = false;
952 if (node->flags == 0) {
953 struct ctdb_control_get_ifaces *ifaces;
955 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
956 tmp_ctx, &ifaces) == 0) {
957 for (j=0; j < ifaces->num; j++) {
958 if (ifaces->ifaces[j].link_state != 0) {
959 continue;
961 ret = true;
962 break;
966 talloc_free(tmp_ctx);
968 return ret;
971 static void control_status_header_machine(void)
973 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
974 ":Inactive:PartiallyOnline:ThisNode:\n");
977 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
978 struct ctdb_node_and_flags *node)
980 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
981 ctdb_addr_to_str(&node->addr),
982 !!(node->flags&NODE_FLAGS_DISCONNECTED),
983 !!(node->flags&NODE_FLAGS_BANNED),
984 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
985 !!(node->flags&NODE_FLAGS_UNHEALTHY),
986 !!(node->flags&NODE_FLAGS_STOPPED),
987 !!(node->flags&NODE_FLAGS_INACTIVE),
988 is_partially_online(ctdb, node) ? 1 : 0,
989 (node->pnn == mypnn)?'Y':'N');
991 return node->flags;
994 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
995 struct ctdb_node_and_flags *node)
997 printf("pnn:%d %-16s %s%s\n", node->pnn,
998 ctdb_addr_to_str(&node->addr),
999 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
1000 node->pnn == mypnn?" (THIS NODE)":"");
1002 return node->flags;
1006 display remote ctdb status
1008 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
1010 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1011 int i;
1012 struct ctdb_vnn_map *vnnmap=NULL;
1013 struct ctdb_node_map *nodemap=NULL;
1014 uint32_t recmode, recmaster, mypnn;
1015 int num_deleted_nodes = 0;
1016 int ret;
1018 mypnn = getpnn(ctdb);
1020 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1021 if (ret != 0) {
1022 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1023 talloc_free(tmp_ctx);
1024 return -1;
1027 if (options.machinereadable) {
1028 control_status_header_machine();
1029 for (i=0;i<nodemap->num;i++) {
1030 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1031 continue;
1033 (void) control_status_1_machine(ctdb, mypnn,
1034 &nodemap->nodes[i]);
1036 talloc_free(tmp_ctx);
1037 return 0;
1040 for (i=0; i<nodemap->num; i++) {
1041 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1042 num_deleted_nodes++;
1045 if (num_deleted_nodes == 0) {
1046 printf("Number of nodes:%d\n", nodemap->num);
1047 } else {
1048 printf("Number of nodes:%d (including %d deleted nodes)\n",
1049 nodemap->num, num_deleted_nodes);
1051 for(i=0;i<nodemap->num;i++){
1052 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1053 continue;
1055 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1058 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1059 if (ret != 0) {
1060 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1061 talloc_free(tmp_ctx);
1062 return -1;
1064 if (vnnmap->generation == INVALID_GENERATION) {
1065 printf("Generation:INVALID\n");
1066 } else {
1067 printf("Generation:%d\n",vnnmap->generation);
1069 printf("Size:%d\n",vnnmap->size);
1070 for(i=0;i<vnnmap->size;i++){
1071 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1074 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1075 if (ret != 0) {
1076 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1077 talloc_free(tmp_ctx);
1078 return -1;
1080 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1082 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1083 if (ret != 0) {
1084 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1085 talloc_free(tmp_ctx);
1086 return -1;
1088 printf("Recovery master:%d\n",recmaster);
1090 talloc_free(tmp_ctx);
1091 return 0;
1094 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1096 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1097 int i, ret;
1098 struct ctdb_node_map *nodemap=NULL;
1099 uint32_t * nodes;
1100 uint32_t pnn_mode, mypnn;
1102 if (argc > 1) {
1103 usage();
1106 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1107 options.pnn, true, &nodes, &pnn_mode)) {
1108 return -1;
1111 if (options.machinereadable) {
1112 control_status_header_machine();
1113 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1114 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1117 mypnn = getpnn(ctdb);
1119 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1120 if (ret != 0) {
1121 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1122 talloc_free(tmp_ctx);
1123 return -1;
1126 ret = 0;
1128 for (i = 0; i < talloc_array_length(nodes); i++) {
1129 if (options.machinereadable) {
1130 ret |= control_status_1_machine(ctdb, mypnn,
1131 &nodemap->nodes[nodes[i]]);
1132 } else {
1133 ret |= control_status_1_human(ctdb, mypnn,
1134 &nodemap->nodes[nodes[i]]);
1138 talloc_free(tmp_ctx);
1139 return ret;
1142 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1143 TALLOC_CTX *mem_ctx)
1145 const char *natgw_list;
1146 struct pnn_node *natgw_nodes = NULL;
1148 natgw_list = getenv("CTDB_NATGW_NODES");
1149 if (natgw_list == NULL) {
1150 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1151 getenv("CTDB_BASE"));
1152 if (natgw_list == NULL) {
1153 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1154 exit(1);
1157 /* The PNNs will be junk but they're not used */
1158 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1159 if (natgw_nodes == NULL) {
1160 DEBUG(DEBUG_ERR,
1161 ("Failed to load natgw node list '%s'\n", natgw_list));
1163 return natgw_nodes;
1167 /* talloc off the existing nodemap... */
1168 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1170 return talloc_zero_size(nodemap,
1171 offsetof(struct ctdb_node_map, nodes) +
1172 nodemap->num * sizeof(struct ctdb_node_and_flags));
1175 static struct ctdb_node_map *
1176 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1177 struct ctdb_node_map *nodemap,
1178 struct pnn_node *nodes)
1180 int i;
1181 struct pnn_node *n;
1182 struct ctdb_node_map *ret;
1184 ret = talloc_nodemap(nodemap);
1185 CTDB_NO_MEMORY_NULL(ctdb, ret);
1187 ret->num = 0;
1189 for (i = 0; i < nodemap->num; i++) {
1190 for(n = nodes; n != NULL ; n = n->next) {
1191 if (ctdb_same_ip(&n->addr,
1192 &nodemap->nodes[i].addr)) {
1193 break;
1196 if (n == NULL) {
1197 continue;
1200 ret->nodes[ret->num] = nodemap->nodes[i];
1201 ret->num++;
1204 return ret;
1207 static struct ctdb_node_map *
1208 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1209 struct ctdb_node_map *nodemap,
1210 uint32_t required_capabilities,
1211 bool first_only)
1213 int i;
1214 uint32_t capabilities;
1215 struct ctdb_node_map *ret;
1217 ret = talloc_nodemap(nodemap);
1218 CTDB_NO_MEMORY_NULL(ctdb, ret);
1220 ret->num = 0;
1222 for (i = 0; i < nodemap->num; i++) {
1223 int res;
1225 /* Disconnected nodes have no capabilities! */
1226 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1227 continue;
1230 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1231 nodemap->nodes[i].pnn,
1232 &capabilities);
1233 if (res != 0) {
1234 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1235 nodemap->nodes[i].pnn));
1236 talloc_free(ret);
1237 return NULL;
1239 if (!(capabilities & required_capabilities)) {
1240 continue;
1243 ret->nodes[ret->num] = nodemap->nodes[i];
1244 ret->num++;
1245 if (first_only) {
1246 break;
1250 return ret;
1253 static struct ctdb_node_map *
1254 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1255 struct ctdb_node_map *nodemap,
1256 uint32_t flags_mask)
1258 int i;
1259 struct ctdb_node_map *ret;
1261 ret = talloc_nodemap(nodemap);
1262 CTDB_NO_MEMORY_NULL(ctdb, ret);
1264 ret->num = 0;
1266 for (i = 0; i < nodemap->num; i++) {
1267 if (nodemap->nodes[i].flags & flags_mask) {
1268 continue;
1271 ret->nodes[ret->num] = nodemap->nodes[i];
1272 ret->num++;
1275 return ret;
1279 display the list of nodes belonging to this natgw configuration
1281 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1283 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1284 int i, ret;
1285 struct pnn_node *natgw_nodes = NULL;
1286 struct ctdb_node_map *orig_nodemap=NULL;
1287 struct ctdb_node_map *nodemap;
1288 uint32_t mypnn, pnn;
1289 const char *ip;
1291 /* When we have some nodes that could be the NATGW, make a
1292 * series of attempts to find the first node that doesn't have
1293 * certain status flags set.
1295 uint32_t exclude_flags[] = {
1296 /* Look for a nice healthy node */
1297 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1298 /* If not found, an UNHEALTHY/BANNED node will do */
1299 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1300 /* If not found, a STOPPED node will do */
1301 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1305 /* read the natgw nodes file into a linked list */
1306 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1307 if (natgw_nodes == NULL) {
1308 ret = -1;
1309 goto done;
1312 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1313 tmp_ctx, &orig_nodemap);
1314 if (ret != 0) {
1315 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1316 talloc_free(tmp_ctx);
1317 return -1;
1320 /* Get a nodemap that includes only the nodes in the NATGW
1321 * group */
1322 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1323 if (nodemap == NULL) {
1324 ret = -1;
1325 goto done;
1328 ret = 2; /* matches ENOENT */
1329 pnn = -1;
1330 ip = "0.0.0.0";
1331 /* For each flag mask... */
1332 for (i = 0; exclude_flags[i] != 0; i++) {
1333 /* ... get a nodemap that excludes nodes with with
1334 * masked flags... */
1335 struct ctdb_node_map *t =
1336 filter_nodemap_by_flags(ctdb, nodemap,
1337 exclude_flags[i]);
1338 if (t == NULL) {
1339 /* No memory */
1340 ret = -1;
1341 goto done;
1343 if (t->num > 0) {
1344 /* ... and find the first node with the NATGW
1345 * capability */
1346 struct ctdb_node_map *n;
1347 n = filter_nodemap_by_capabilities(ctdb, t,
1348 CTDB_CAP_NATGW,
1349 true);
1350 if (n == NULL) {
1351 /* No memory */
1352 ret = -1;
1353 goto done;
1355 if (n->num > 0) {
1356 ret = 0;
1357 pnn = n->nodes[0].pnn;
1358 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1359 break;
1362 talloc_free(t);
1365 if (options.machinereadable) {
1366 printf(":Node:IP:\n");
1367 printf(":%d:%s:\n", pnn, ip);
1368 } else {
1369 printf("%d %s\n", pnn, ip);
1372 /* print the pruned list of nodes belonging to this natgw list */
1373 mypnn = getpnn(ctdb);
1374 if (options.machinereadable) {
1375 control_status_header_machine();
1376 } else {
1377 printf("Number of nodes:%d\n", nodemap->num);
1379 for(i=0;i<nodemap->num;i++){
1380 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1381 continue;
1383 if (options.machinereadable) {
1384 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1385 } else {
1386 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1390 done:
1391 talloc_free(tmp_ctx);
1392 return ret;
1396 display the status of the scripts for monitoring (or other events)
1398 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1399 enum ctdb_eventscript_call type)
1401 struct ctdb_scripts_wire *script_status;
1402 int ret, i;
1404 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1405 if (ret != 0) {
1406 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1407 return ret;
1410 if (script_status == NULL) {
1411 if (!options.machinereadable) {
1412 printf("%s cycle never run\n",
1413 ctdb_eventscript_call_names[type]);
1415 return 0;
1418 if (!options.machinereadable) {
1419 int num_run = 0;
1420 for (i=0; i<script_status->num_scripts; i++) {
1421 if (script_status->scripts[i].status != -ENOEXEC) {
1422 num_run++;
1425 printf("%d scripts were executed last %s cycle\n",
1426 num_run,
1427 ctdb_eventscript_call_names[type]);
1429 for (i=0; i<script_status->num_scripts; i++) {
1430 const char *status = NULL;
1432 switch (script_status->scripts[i].status) {
1433 case -ETIME:
1434 status = "TIMEDOUT";
1435 break;
1436 case -ENOEXEC:
1437 status = "DISABLED";
1438 break;
1439 case 0:
1440 status = "OK";
1441 break;
1442 default:
1443 if (script_status->scripts[i].status > 0)
1444 status = "ERROR";
1445 break;
1447 if (options.machinereadable) {
1448 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1449 ctdb_eventscript_call_names[type],
1450 script_status->scripts[i].name,
1451 script_status->scripts[i].status,
1452 status,
1453 (long)script_status->scripts[i].start.tv_sec,
1454 (long)script_status->scripts[i].start.tv_usec,
1455 (long)script_status->scripts[i].finished.tv_sec,
1456 (long)script_status->scripts[i].finished.tv_usec,
1457 script_status->scripts[i].output);
1458 continue;
1460 if (status)
1461 printf("%-20s Status:%s ",
1462 script_status->scripts[i].name, status);
1463 else
1464 /* Some other error, eg from stat. */
1465 printf("%-20s Status:CANNOT RUN (%s)",
1466 script_status->scripts[i].name,
1467 strerror(-script_status->scripts[i].status));
1469 if (script_status->scripts[i].status >= 0) {
1470 printf("Duration:%.3lf ",
1471 timeval_delta(&script_status->scripts[i].finished,
1472 &script_status->scripts[i].start));
1474 if (script_status->scripts[i].status != -ENOEXEC) {
1475 printf("%s",
1476 ctime(&script_status->scripts[i].start.tv_sec));
1477 if (script_status->scripts[i].status != 0) {
1478 printf(" OUTPUT:%s\n",
1479 script_status->scripts[i].output);
1481 } else {
1482 printf("\n");
1485 return 0;
1489 static int control_scriptstatus(struct ctdb_context *ctdb,
1490 int argc, const char **argv)
1492 int ret;
1493 enum ctdb_eventscript_call type, min, max;
1494 const char *arg;
1496 if (argc > 1) {
1497 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1498 return -1;
1501 if (argc == 0)
1502 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1503 else
1504 arg = argv[0];
1506 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1507 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1508 min = type;
1509 max = type+1;
1510 break;
1513 if (type == CTDB_EVENT_MAX) {
1514 if (strcmp(arg, "all") == 0) {
1515 min = 0;
1516 max = CTDB_EVENT_MAX;
1517 } else {
1518 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1519 return -1;
1523 if (options.machinereadable) {
1524 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1527 for (type = min; type < max; type++) {
1528 ret = control_one_scriptstatus(ctdb, type);
1529 if (ret != 0) {
1530 return ret;
1534 return 0;
1538 enable an eventscript
1540 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1542 int ret;
1544 if (argc < 1) {
1545 usage();
1548 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1549 if (ret != 0) {
1550 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1551 return ret;
1554 return 0;
1558 disable an eventscript
1560 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1562 int ret;
1564 if (argc < 1) {
1565 usage();
1568 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1569 if (ret != 0) {
1570 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1571 return ret;
1574 return 0;
1578 display the pnn of the recovery master
1580 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1582 uint32_t recmaster;
1583 int ret;
1585 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1586 if (ret != 0) {
1587 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1588 return -1;
1590 printf("%d\n",recmaster);
1592 return 0;
1596 add a tickle to a public address
1598 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1600 struct ctdb_tcp_connection t;
1601 TDB_DATA data;
1602 int ret;
1604 assert_single_node_only();
1606 if (argc < 2) {
1607 usage();
1610 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1611 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1612 return -1;
1614 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1615 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1616 return -1;
1619 data.dptr = (uint8_t *)&t;
1620 data.dsize = sizeof(t);
1622 /* tell all nodes about this tcp connection */
1623 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1624 0, data, ctdb, NULL, NULL, NULL, NULL);
1625 if (ret != 0) {
1626 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1627 return -1;
1630 return 0;
1635 delete a tickle from a node
1637 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1639 struct ctdb_tcp_connection t;
1640 TDB_DATA data;
1641 int ret;
1643 assert_single_node_only();
1645 if (argc < 2) {
1646 usage();
1649 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1650 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1651 return -1;
1653 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1654 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1655 return -1;
1658 data.dptr = (uint8_t *)&t;
1659 data.dsize = sizeof(t);
1661 /* tell all nodes about this tcp connection */
1662 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1663 0, data, ctdb, NULL, NULL, NULL, NULL);
1664 if (ret != 0) {
1665 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1666 return -1;
1669 return 0;
1674 get a list of all tickles for this pnn
1676 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1678 struct ctdb_control_tcp_tickle_list *list;
1679 ctdb_sock_addr addr;
1680 int i, ret;
1681 unsigned port = 0;
1683 assert_single_node_only();
1685 if (argc < 1) {
1686 usage();
1689 if (argc == 2) {
1690 port = atoi(argv[1]);
1693 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1694 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1695 return -1;
1698 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1699 if (ret == -1) {
1700 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1701 return -1;
1704 if (options.machinereadable){
1705 printf(":source ip:port:destination ip:port:\n");
1706 for (i=0;i<list->tickles.num;i++) {
1707 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1708 continue;
1710 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1711 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1713 } else {
1714 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1715 printf("Num tickles:%u\n", list->tickles.num);
1716 for (i=0;i<list->tickles.num;i++) {
1717 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1718 continue;
1720 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1721 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));
1725 talloc_free(list);
1727 return 0;
1731 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1733 struct ctdb_all_public_ips *ips;
1734 struct ctdb_public_ip ip;
1735 int i, ret;
1736 uint32_t *nodes;
1737 uint32_t disable_time;
1738 TDB_DATA data;
1739 struct ctdb_node_map *nodemap=NULL;
1740 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1742 disable_time = 30;
1743 data.dptr = (uint8_t*)&disable_time;
1744 data.dsize = sizeof(disable_time);
1745 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1746 if (ret != 0) {
1747 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1748 return -1;
1753 /* read the public ip list from the node */
1754 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1755 if (ret != 0) {
1756 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1757 talloc_free(tmp_ctx);
1758 return -1;
1761 for (i=0;i<ips->num;i++) {
1762 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1763 break;
1766 if (i==ips->num) {
1767 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1768 pnn, ctdb_addr_to_str(addr)));
1769 talloc_free(tmp_ctx);
1770 return -1;
1773 ip.pnn = pnn;
1774 ip.addr = *addr;
1776 data.dptr = (uint8_t *)&ip;
1777 data.dsize = sizeof(ip);
1779 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1780 if (ret != 0) {
1781 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1782 talloc_free(tmp_ctx);
1783 return ret;
1786 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1787 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1788 nodes, 0,
1789 LONGTIMELIMIT(),
1790 false, data,
1791 NULL, NULL,
1792 NULL);
1793 if (ret != 0) {
1794 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1795 talloc_free(tmp_ctx);
1796 return -1;
1799 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1800 if (ret != 0) {
1801 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1802 talloc_free(tmp_ctx);
1803 return -1;
1806 /* update the recovery daemon so it now knows to expect the new
1807 node assignment for this ip.
1809 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1810 if (ret != 0) {
1811 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1812 return -1;
1815 talloc_free(tmp_ctx);
1816 return 0;
1821 * scans all other nodes and returns a pnn for another node that can host this
1822 * ip address or -1
1824 static int
1825 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1827 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1828 struct ctdb_all_public_ips *ips;
1829 struct ctdb_node_map *nodemap=NULL;
1830 int i, j, ret;
1831 int pnn;
1833 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1834 if (ret != 0) {
1835 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1836 talloc_free(tmp_ctx);
1837 return ret;
1840 for(i=0;i<nodemap->num;i++){
1841 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1842 continue;
1844 if (nodemap->nodes[i].pnn == options.pnn) {
1845 continue;
1848 /* read the public ip list from this node */
1849 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1850 if (ret != 0) {
1851 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1852 return -1;
1855 for (j=0;j<ips->num;j++) {
1856 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1857 pnn = nodemap->nodes[i].pnn;
1858 talloc_free(tmp_ctx);
1859 return pnn;
1862 talloc_free(ips);
1865 talloc_free(tmp_ctx);
1866 return -1;
1869 /* If pnn is -1 then try to find a node to move IP to... */
1870 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1872 bool pnn_specified = (pnn == -1 ? false : true);
1873 int retries = 0;
1875 while (retries < 5) {
1876 if (!pnn_specified) {
1877 pnn = find_other_host_for_public_ip(ctdb, addr);
1878 if (pnn == -1) {
1879 return false;
1881 DEBUG(DEBUG_NOTICE,
1882 ("Trying to move public IP to node %u\n", pnn));
1885 if (move_ip(ctdb, addr, pnn) == 0) {
1886 return true;
1889 sleep(3);
1890 retries++;
1893 return false;
1898 move/failover an ip address to a specific node
1900 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1902 uint32_t pnn;
1903 ctdb_sock_addr addr;
1905 assert_single_node_only();
1907 if (argc < 2) {
1908 usage();
1909 return -1;
1912 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1913 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1914 return -1;
1918 if (sscanf(argv[1], "%u", &pnn) != 1) {
1919 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1920 return -1;
1923 if (!try_moveip(ctdb, &addr, pnn)) {
1924 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1925 return -1;
1928 return 0;
1931 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1933 TDB_DATA data;
1935 data.dptr = (uint8_t *)&pnn;
1936 data.dsize = sizeof(uint32_t);
1937 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1938 DEBUG(DEBUG_ERR,
1939 ("Failed to send message to force node %u to be a rebalancing target\n",
1940 pnn));
1941 return -1;
1944 return 0;
1949 rebalance a node by setting it to allow failback and triggering a
1950 takeover run
1952 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1954 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1955 uint32_t *nodes;
1956 uint32_t pnn_mode;
1957 int i, ret;
1959 assert_single_node_only();
1961 if (argc > 1) {
1962 usage();
1965 /* Determine the nodes where IPs need to be reloaded */
1966 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1967 options.pnn, true, &nodes, &pnn_mode)) {
1968 ret = -1;
1969 goto done;
1972 for (i = 0; i < talloc_array_length(nodes); i++) {
1973 if (!rebalance_node(ctdb, nodes[i])) {
1974 ret = -1;
1978 done:
1979 talloc_free(tmp_ctx);
1980 return ret;
1983 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1985 struct ctdb_public_ip ip;
1986 int ret;
1987 uint32_t *nodes;
1988 uint32_t disable_time;
1989 TDB_DATA data;
1990 struct ctdb_node_map *nodemap=NULL;
1991 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1993 disable_time = 30;
1994 data.dptr = (uint8_t*)&disable_time;
1995 data.dsize = sizeof(disable_time);
1996 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1997 if (ret != 0) {
1998 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1999 return -1;
2002 ip.pnn = -1;
2003 ip.addr = *addr;
2005 data.dptr = (uint8_t *)&ip;
2006 data.dsize = sizeof(ip);
2008 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
2009 if (ret != 0) {
2010 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2011 talloc_free(tmp_ctx);
2012 return ret;
2015 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2016 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
2017 nodes, 0,
2018 LONGTIMELIMIT(),
2019 false, data,
2020 NULL, NULL,
2021 NULL);
2022 if (ret != 0) {
2023 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
2024 talloc_free(tmp_ctx);
2025 return -1;
2028 talloc_free(tmp_ctx);
2029 return 0;
2033 release an ip form all nodes and have it re-assigned by recd
2035 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2037 ctdb_sock_addr addr;
2039 assert_single_node_only();
2041 if (argc < 1) {
2042 usage();
2043 return -1;
2046 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2047 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2048 return -1;
2051 if (rebalance_ip(ctdb, &addr) != 0) {
2052 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2053 return -1;
2056 return 0;
2059 static int getips_store_callback(void *param, void *data)
2061 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2062 struct ctdb_all_public_ips *ips = param;
2063 int i;
2065 i = ips->num++;
2066 ips->ips[i].pnn = node_ip->pnn;
2067 ips->ips[i].addr = node_ip->addr;
2068 return 0;
2071 static int getips_count_callback(void *param, void *data)
2073 uint32_t *count = param;
2075 (*count)++;
2076 return 0;
2079 #define IP_KEYLEN 4
2080 static uint32_t *ip_key(ctdb_sock_addr *ip)
2082 static uint32_t key[IP_KEYLEN];
2084 bzero(key, sizeof(key));
2086 switch (ip->sa.sa_family) {
2087 case AF_INET:
2088 key[0] = ip->ip.sin_addr.s_addr;
2089 break;
2090 case AF_INET6: {
2091 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2092 key[0] = s6_a32[3];
2093 key[1] = s6_a32[2];
2094 key[2] = s6_a32[1];
2095 key[3] = s6_a32[0];
2096 break;
2098 default:
2099 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2100 return key;
2103 return key;
2106 static void *add_ip_callback(void *parm, void *data)
2108 return parm;
2111 static int
2112 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2114 struct ctdb_all_public_ips *tmp_ips;
2115 struct ctdb_node_map *nodemap=NULL;
2116 trbt_tree_t *ip_tree;
2117 int i, j, len, ret;
2118 uint32_t count;
2120 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2121 if (ret != 0) {
2122 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2123 return ret;
2126 ip_tree = trbt_create(tmp_ctx, 0);
2128 for(i=0;i<nodemap->num;i++){
2129 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2130 continue;
2132 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2133 continue;
2136 /* read the public ip list from this node */
2137 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2138 if (ret != 0) {
2139 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2140 return -1;
2143 for (j=0; j<tmp_ips->num;j++) {
2144 struct ctdb_public_ip *node_ip;
2146 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2147 node_ip->pnn = tmp_ips->ips[j].pnn;
2148 node_ip->addr = tmp_ips->ips[j].addr;
2150 trbt_insertarray32_callback(ip_tree,
2151 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2152 add_ip_callback,
2153 node_ip);
2155 talloc_free(tmp_ips);
2158 /* traverse */
2159 count = 0;
2160 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2162 len = offsetof(struct ctdb_all_public_ips, ips) +
2163 count*sizeof(struct ctdb_public_ip);
2164 tmp_ips = talloc_zero_size(tmp_ctx, len);
2165 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2167 *ips = tmp_ips;
2169 return 0;
2173 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2175 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2177 event_add_timed(ctdb->ev, ctdb,
2178 timeval_current_ofs(1, 0),
2179 ctdb_every_second, ctdb);
2182 struct srvid_reply_handler_data {
2183 bool done;
2184 bool wait_for_all;
2185 uint32_t *nodes;
2186 const char *srvid_str;
2189 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2190 uint64_t srvid,
2191 TDB_DATA data,
2192 void *private_data)
2194 struct srvid_reply_handler_data *d =
2195 (struct srvid_reply_handler_data *)private_data;
2196 int i;
2197 int32_t ret;
2199 if (data.dsize != sizeof(ret)) {
2200 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2201 return;
2204 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2205 ret = *(int32_t *)data.dptr;
2206 if (ret < 0) {
2207 DEBUG(DEBUG_ERR,
2208 ("%s failed with result %d\n", d->srvid_str, ret));
2209 return;
2212 if (!d->wait_for_all) {
2213 d->done = true;
2214 return;
2217 /* Wait for all replies */
2218 d->done = true;
2219 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2220 if (d->nodes[i] == ret) {
2221 DEBUG(DEBUG_INFO,
2222 ("%s reply received from node %u\n",
2223 d->srvid_str, ret));
2224 d->nodes[i] = -1;
2226 if (d->nodes[i] != -1) {
2227 /* Found a node that hasn't yet replied */
2228 d->done = false;
2233 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2234 * or replies from all connected nodes. arg is the data argument to
2235 * pass in the srvid_request structure - pass 0 if this isn't needed.
2237 static int srvid_broadcast(struct ctdb_context *ctdb,
2238 uint64_t srvid, uint32_t *arg,
2239 const char *srvid_str, bool wait_for_all)
2241 int ret;
2242 TDB_DATA data;
2243 uint32_t pnn;
2244 uint64_t reply_srvid;
2245 struct srvid_request request;
2246 struct srvid_request_data request_data;
2247 struct srvid_reply_handler_data reply_data;
2248 struct timeval tv;
2250 ZERO_STRUCT(request);
2252 /* Time ticks to enable timeouts to be processed */
2253 event_add_timed(ctdb->ev, ctdb,
2254 timeval_current_ofs(1, 0),
2255 ctdb_every_second, ctdb);
2257 pnn = ctdb_get_pnn(ctdb);
2258 reply_srvid = getpid();
2260 if (arg == NULL) {
2261 request.pnn = pnn;
2262 request.srvid = reply_srvid;
2264 data.dptr = (uint8_t *)&request;
2265 data.dsize = sizeof(request);
2266 } else {
2267 request_data.pnn = pnn;
2268 request_data.srvid = reply_srvid;
2269 request_data.data = *arg;
2271 data.dptr = (uint8_t *)&request_data;
2272 data.dsize = sizeof(request_data);
2275 /* Register message port for reply from recovery master */
2276 ctdb_client_set_message_handler(ctdb, reply_srvid,
2277 srvid_broadcast_reply_handler,
2278 &reply_data);
2280 reply_data.wait_for_all = wait_for_all;
2281 reply_data.nodes = NULL;
2282 reply_data.srvid_str = srvid_str;
2284 again:
2285 reply_data.done = false;
2287 if (wait_for_all) {
2288 struct ctdb_node_map *nodemap;
2290 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2291 CTDB_CURRENT_NODE, ctdb, &nodemap);
2292 if (ret != 0) {
2293 DEBUG(DEBUG_ERR,
2294 ("Unable to get nodemap from current node, try again\n"));
2295 sleep(1);
2296 goto again;
2299 if (reply_data.nodes != NULL) {
2300 talloc_free(reply_data.nodes);
2302 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2303 NULL, true);
2305 talloc_free(nodemap);
2308 /* Send to all connected nodes. Only recmaster replies */
2309 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2310 srvid, data);
2311 if (ret != 0) {
2312 /* This can only happen if the socket is closed and
2313 * there's no way to recover from that, so don't try
2314 * again.
2316 DEBUG(DEBUG_ERR,
2317 ("Failed to send %s request to connected nodes\n",
2318 srvid_str));
2319 return -1;
2322 tv = timeval_current();
2323 /* This loop terminates the reply is received */
2324 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2325 event_loop_once(ctdb->ev);
2328 if (!reply_data.done) {
2329 DEBUG(DEBUG_NOTICE,
2330 ("Still waiting for confirmation of %s\n", srvid_str));
2331 sleep(1);
2332 goto again;
2335 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2337 talloc_free(reply_data.nodes);
2339 return 0;
2342 static int ipreallocate(struct ctdb_context *ctdb)
2344 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2345 "IP reallocation", false);
2349 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2351 return ipreallocate(ctdb);
2355 add a public ip address to a node
2357 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2359 int i, ret;
2360 int len, retries = 0;
2361 unsigned mask;
2362 ctdb_sock_addr addr;
2363 struct ctdb_control_ip_iface *pub;
2364 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2365 struct ctdb_all_public_ips *ips;
2368 if (argc != 2) {
2369 talloc_free(tmp_ctx);
2370 usage();
2373 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2374 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2375 talloc_free(tmp_ctx);
2376 return -1;
2379 /* read the public ip list from the node */
2380 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2381 if (ret != 0) {
2382 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2383 talloc_free(tmp_ctx);
2384 return -1;
2386 for (i=0;i<ips->num;i++) {
2387 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2388 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2389 return 0;
2395 /* Dont timeout. This command waits for an ip reallocation
2396 which sometimes can take wuite a while if there has
2397 been a recent recovery
2399 alarm(0);
2401 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2402 pub = talloc_size(tmp_ctx, len);
2403 CTDB_NO_MEMORY(ctdb, pub);
2405 pub->addr = addr;
2406 pub->mask = mask;
2407 pub->len = strlen(argv[1])+1;
2408 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2410 do {
2411 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2412 if (ret != 0) {
2413 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2414 sleep(3);
2415 retries++;
2417 } while (retries < 5 && ret != 0);
2418 if (ret != 0) {
2419 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2420 talloc_free(tmp_ctx);
2421 return ret;
2424 if (rebalance_node(ctdb, options.pnn) != 0) {
2425 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2426 return ret;
2429 talloc_free(tmp_ctx);
2430 return 0;
2434 add a public ip address to a node
2436 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2438 ctdb_sock_addr addr;
2440 if (argc != 1) {
2441 usage();
2444 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2445 printf("Badly formed ip : %s\n", argv[0]);
2446 return -1;
2449 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2451 return 0;
2454 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2456 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2458 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2459 struct ctdb_node_map *nodemap=NULL;
2460 struct ctdb_all_public_ips *ips;
2461 int ret, i, j;
2463 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2464 if (ret != 0) {
2465 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2466 return ret;
2469 /* remove it from the nodes that are not hosting the ip currently */
2470 for(i=0;i<nodemap->num;i++){
2471 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2472 continue;
2474 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2475 if (ret != 0) {
2476 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2477 continue;
2480 for (j=0;j<ips->num;j++) {
2481 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2482 break;
2485 if (j==ips->num) {
2486 continue;
2489 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2490 continue;
2493 options.pnn = nodemap->nodes[i].pnn;
2494 control_delip(ctdb, argc, argv);
2498 /* remove it from every node (also the one hosting it) */
2499 for(i=0;i<nodemap->num;i++){
2500 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2501 continue;
2503 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2504 if (ret != 0) {
2505 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2506 continue;
2509 for (j=0;j<ips->num;j++) {
2510 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2511 break;
2514 if (j==ips->num) {
2515 continue;
2518 options.pnn = nodemap->nodes[i].pnn;
2519 control_delip(ctdb, argc, argv);
2522 talloc_free(tmp_ctx);
2523 return 0;
2527 delete a public ip address from a node
2529 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2531 int i, ret;
2532 ctdb_sock_addr addr;
2533 struct ctdb_control_ip_iface pub;
2534 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2535 struct ctdb_all_public_ips *ips;
2537 if (argc != 1) {
2538 talloc_free(tmp_ctx);
2539 usage();
2542 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2543 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2544 return -1;
2547 if (options.pnn == CTDB_BROADCAST_ALL) {
2548 return control_delip_all(ctdb, argc, argv, &addr);
2551 pub.addr = addr;
2552 pub.mask = 0;
2553 pub.len = 0;
2555 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2556 if (ret != 0) {
2557 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2558 talloc_free(tmp_ctx);
2559 return ret;
2562 for (i=0;i<ips->num;i++) {
2563 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2564 break;
2568 if (i==ips->num) {
2569 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2570 ctdb_addr_to_str(&addr)));
2571 talloc_free(tmp_ctx);
2572 return -1;
2575 /* This is an optimisation. If this node is hosting the IP
2576 * then try to move it somewhere else without invoking a full
2577 * takeover run. We don't care if this doesn't work!
2579 if (ips->ips[i].pnn == options.pnn) {
2580 (void) try_moveip(ctdb, &addr, -1);
2583 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2584 if (ret != 0) {
2585 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2586 talloc_free(tmp_ctx);
2587 return ret;
2590 talloc_free(tmp_ctx);
2591 return 0;
2594 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2595 int argc, const char **argv)
2597 struct ctdb_control_killtcp *killtcp;
2598 int max_entries, current, i;
2599 struct timeval timeout;
2600 char line[128], src[128], dst[128];
2601 int linenum;
2602 TDB_DATA data;
2603 struct client_async_data *async_data;
2604 struct ctdb_client_control_state *state;
2606 if (argc != 0) {
2607 usage();
2610 linenum = 1;
2611 killtcp = NULL;
2612 max_entries = 0;
2613 current = 0;
2614 while (!feof(stdin)) {
2615 if (fgets(line, sizeof(line), stdin) == NULL) {
2616 continue;
2619 /* Silently skip empty lines */
2620 if (line[0] == '\n') {
2621 continue;
2624 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2625 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2626 linenum, line));
2627 talloc_free(killtcp);
2628 return -1;
2631 if (current >= max_entries) {
2632 max_entries += 1024;
2633 killtcp = talloc_realloc(ctdb, killtcp,
2634 struct ctdb_control_killtcp,
2635 max_entries);
2636 CTDB_NO_MEMORY(ctdb, killtcp);
2639 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2640 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2641 linenum, src));
2642 talloc_free(killtcp);
2643 return -1;
2646 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2647 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2648 linenum, dst));
2649 talloc_free(killtcp);
2650 return -1;
2653 current++;
2656 async_data = talloc_zero(ctdb, struct client_async_data);
2657 if (async_data == NULL) {
2658 talloc_free(killtcp);
2659 return -1;
2662 for (i = 0; i < current; i++) {
2664 data.dsize = sizeof(struct ctdb_control_killtcp);
2665 data.dptr = (unsigned char *)&killtcp[i];
2667 timeout = TIMELIMIT();
2668 state = ctdb_control_send(ctdb, options.pnn, 0,
2669 CTDB_CONTROL_KILL_TCP, 0, data,
2670 async_data, &timeout, NULL);
2672 if (state == NULL) {
2673 DEBUG(DEBUG_ERR,
2674 ("Failed to call async killtcp control to node %u\n",
2675 options.pnn));
2676 talloc_free(killtcp);
2677 return -1;
2680 ctdb_client_async_add(async_data, state);
2683 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2684 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2685 talloc_free(killtcp);
2686 return -1;
2689 talloc_free(killtcp);
2690 return 0;
2695 kill a tcp connection
2697 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2699 int ret;
2700 struct ctdb_control_killtcp killtcp;
2702 assert_single_node_only();
2704 if (argc == 0) {
2705 return kill_tcp_from_file(ctdb, argc, argv);
2708 if (argc < 2) {
2709 usage();
2712 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2713 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2714 return -1;
2717 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2718 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2719 return -1;
2722 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2723 if (ret != 0) {
2724 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2725 return ret;
2728 return 0;
2733 send a gratious arp
2735 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2737 int ret;
2738 ctdb_sock_addr addr;
2740 assert_single_node_only();
2742 if (argc < 2) {
2743 usage();
2746 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2747 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2748 return -1;
2751 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2752 if (ret != 0) {
2753 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2754 return ret;
2757 return 0;
2761 register a server id
2763 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2765 int ret;
2766 struct ctdb_server_id server_id;
2768 if (argc < 3) {
2769 usage();
2772 server_id.pnn = strtoul(argv[0], NULL, 0);
2773 server_id.type = strtoul(argv[1], NULL, 0);
2774 server_id.server_id = strtoul(argv[2], NULL, 0);
2776 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2777 if (ret != 0) {
2778 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2779 return ret;
2781 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2782 sleep(999);
2783 return -1;
2787 unregister a server id
2789 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2791 int ret;
2792 struct ctdb_server_id server_id;
2794 if (argc < 3) {
2795 usage();
2798 server_id.pnn = strtoul(argv[0], NULL, 0);
2799 server_id.type = strtoul(argv[1], NULL, 0);
2800 server_id.server_id = strtoul(argv[2], NULL, 0);
2802 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2803 if (ret != 0) {
2804 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2805 return ret;
2807 return -1;
2811 check if a server id exists
2813 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2815 uint32_t status;
2816 int ret;
2817 struct ctdb_server_id server_id;
2819 if (argc < 3) {
2820 usage();
2823 server_id.pnn = strtoul(argv[0], NULL, 0);
2824 server_id.type = strtoul(argv[1], NULL, 0);
2825 server_id.server_id = strtoul(argv[2], NULL, 0);
2827 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2828 if (ret != 0) {
2829 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2830 return ret;
2833 if (status) {
2834 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2835 } else {
2836 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2838 return 0;
2842 get a list of all server ids that are registered on a node
2844 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2846 int i, ret;
2847 struct ctdb_server_id_list *server_ids;
2849 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2850 if (ret != 0) {
2851 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2852 return ret;
2855 for (i=0; i<server_ids->num; i++) {
2856 printf("Server id %d:%d:%d\n",
2857 server_ids->server_ids[i].pnn,
2858 server_ids->server_ids[i].type,
2859 server_ids->server_ids[i].server_id);
2862 return -1;
2866 check if a server id exists
2868 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2870 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2871 uint64_t *ids;
2872 uint8_t *result;
2873 int i;
2875 if (argc < 1) {
2876 talloc_free(tmp_ctx);
2877 usage();
2880 ids = talloc_array(tmp_ctx, uint64_t, argc);
2881 result = talloc_array(tmp_ctx, uint8_t, argc);
2883 for (i = 0; i < argc; i++) {
2884 ids[i] = strtoull(argv[i], NULL, 0);
2887 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2888 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2889 options.pnn));
2890 talloc_free(tmp_ctx);
2891 return -1;
2894 for (i=0; i < argc; i++) {
2895 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2896 result[i] ? "exists" : "does not exist");
2899 talloc_free(tmp_ctx);
2900 return 0;
2904 send a tcp tickle ack
2906 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2908 int ret;
2909 ctdb_sock_addr src, dst;
2911 if (argc < 2) {
2912 usage();
2915 if (!parse_ip_port(argv[0], &src)) {
2916 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2917 return -1;
2920 if (!parse_ip_port(argv[1], &dst)) {
2921 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2922 return -1;
2925 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2926 if (ret==0) {
2927 return 0;
2929 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2931 return -1;
2936 display public ip status
2938 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2940 int i, ret;
2941 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2942 struct ctdb_all_public_ips *ips;
2944 if (options.pnn == CTDB_BROADCAST_ALL) {
2945 /* read the list of public ips from all nodes */
2946 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2947 } else {
2948 /* read the public ip list from this node */
2949 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2951 if (ret != 0) {
2952 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2953 talloc_free(tmp_ctx);
2954 return ret;
2957 if (options.machinereadable){
2958 printf(":Public IP:Node:");
2959 if (options.verbose){
2960 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2962 printf("\n");
2963 } else {
2964 if (options.pnn == CTDB_BROADCAST_ALL) {
2965 printf("Public IPs on ALL nodes\n");
2966 } else {
2967 printf("Public IPs on node %u\n", options.pnn);
2971 for (i=1;i<=ips->num;i++) {
2972 struct ctdb_control_public_ip_info *info = NULL;
2973 int32_t pnn;
2974 char *aciface = NULL;
2975 char *avifaces = NULL;
2976 char *cifaces = NULL;
2978 if (options.pnn == CTDB_BROADCAST_ALL) {
2979 pnn = ips->ips[ips->num-i].pnn;
2980 } else {
2981 pnn = options.pnn;
2984 if (pnn != -1) {
2985 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2986 &ips->ips[ips->num-i].addr, &info);
2987 } else {
2988 ret = -1;
2991 if (ret == 0) {
2992 int j;
2993 for (j=0; j < info->num; j++) {
2994 if (cifaces == NULL) {
2995 cifaces = talloc_strdup(info,
2996 info->ifaces[j].name);
2997 } else {
2998 cifaces = talloc_asprintf_append(cifaces,
2999 ",%s",
3000 info->ifaces[j].name);
3003 if (info->active_idx == j) {
3004 aciface = info->ifaces[j].name;
3007 if (info->ifaces[j].link_state == 0) {
3008 continue;
3011 if (avifaces == NULL) {
3012 avifaces = talloc_strdup(info, info->ifaces[j].name);
3013 } else {
3014 avifaces = talloc_asprintf_append(avifaces,
3015 ",%s",
3016 info->ifaces[j].name);
3021 if (options.machinereadable){
3022 printf(":%s:%d:",
3023 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3024 ips->ips[ips->num-i].pnn);
3025 if (options.verbose){
3026 printf("%s:%s:%s:",
3027 aciface?aciface:"",
3028 avifaces?avifaces:"",
3029 cifaces?cifaces:"");
3031 printf("\n");
3032 } else {
3033 if (options.verbose) {
3034 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3035 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3036 ips->ips[ips->num-i].pnn,
3037 aciface?aciface:"",
3038 avifaces?avifaces:"",
3039 cifaces?cifaces:"");
3040 } else {
3041 printf("%s %d\n",
3042 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3043 ips->ips[ips->num-i].pnn);
3046 talloc_free(info);
3049 talloc_free(tmp_ctx);
3050 return 0;
3054 public ip info
3056 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3058 int i, ret;
3059 ctdb_sock_addr addr;
3060 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3061 struct ctdb_control_public_ip_info *info;
3063 if (argc != 1) {
3064 talloc_free(tmp_ctx);
3065 usage();
3068 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3069 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3070 return -1;
3073 /* read the public ip info from this node */
3074 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3075 tmp_ctx, &addr, &info);
3076 if (ret != 0) {
3077 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3078 argv[0], options.pnn));
3079 talloc_free(tmp_ctx);
3080 return ret;
3083 printf("Public IP[%s] info on node %u\n",
3084 ctdb_addr_to_str(&info->ip.addr),
3085 options.pnn);
3087 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3088 ctdb_addr_to_str(&info->ip.addr),
3089 info->ip.pnn, info->num);
3091 for (i=0; i<info->num; i++) {
3092 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3094 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3095 i+1, info->ifaces[i].name,
3096 info->ifaces[i].link_state?"up":"down",
3097 (unsigned int)info->ifaces[i].references,
3098 (i==info->active_idx)?" (active)":"");
3101 talloc_free(tmp_ctx);
3102 return 0;
3106 display interfaces status
3108 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3110 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3111 int i;
3112 struct ctdb_control_get_ifaces *ifaces;
3113 int ret;
3115 /* read the public ip list from this node */
3116 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3117 if (ret != 0) {
3118 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3119 options.pnn));
3120 talloc_free(tmp_ctx);
3121 return -1;
3124 if (options.machinereadable){
3125 printf(":Name:LinkStatus:References:\n");
3126 } else {
3127 printf("Interfaces on node %u\n", options.pnn);
3130 for (i=0; i<ifaces->num; i++) {
3131 if (options.machinereadable){
3132 printf(":%s:%s:%u\n",
3133 ifaces->ifaces[i].name,
3134 ifaces->ifaces[i].link_state?"1":"0",
3135 (unsigned int)ifaces->ifaces[i].references);
3136 } else {
3137 printf("name:%s link:%s references:%u\n",
3138 ifaces->ifaces[i].name,
3139 ifaces->ifaces[i].link_state?"up":"down",
3140 (unsigned int)ifaces->ifaces[i].references);
3144 talloc_free(tmp_ctx);
3145 return 0;
3150 set link status of an interface
3152 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3154 int ret;
3155 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3156 struct ctdb_control_iface_info info;
3158 ZERO_STRUCT(info);
3160 if (argc != 2) {
3161 usage();
3164 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3165 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3166 argv[0]));
3167 talloc_free(tmp_ctx);
3168 return -1;
3170 strcpy(info.name, argv[0]);
3172 if (strcmp(argv[1], "up") == 0) {
3173 info.link_state = 1;
3174 } else if (strcmp(argv[1], "down") == 0) {
3175 info.link_state = 0;
3176 } else {
3177 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3178 argv[1]));
3179 talloc_free(tmp_ctx);
3180 return -1;
3183 /* read the public ip list from this node */
3184 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3185 tmp_ctx, &info);
3186 if (ret != 0) {
3187 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3188 argv[0], options.pnn));
3189 talloc_free(tmp_ctx);
3190 return ret;
3193 talloc_free(tmp_ctx);
3194 return 0;
3198 display pid of a ctdb daemon
3200 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3202 uint32_t pid;
3203 int ret;
3205 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3206 if (ret != 0) {
3207 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3208 return ret;
3210 printf("Pid:%d\n", pid);
3212 return 0;
3215 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3217 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3218 void *data,
3219 update_flags_handler_t handler,
3220 uint32_t flag,
3221 const char *desc,
3222 bool set_flag)
3224 struct ctdb_node_map *nodemap = NULL;
3225 bool flag_is_set;
3226 int ret;
3228 /* Check if the node is already in the desired state */
3229 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3230 if (ret != 0) {
3231 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3232 exit(10);
3234 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3235 if (set_flag == flag_is_set) {
3236 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3237 (set_flag ? "already" : "not"), desc));
3238 return 0;
3241 do {
3242 if (!handler(ctdb, data)) {
3243 DEBUG(DEBUG_WARNING,
3244 ("Failed to send control to set state %s on node %u, try again\n",
3245 desc, options.pnn));
3248 sleep(1);
3250 /* Read the nodemap and verify the change took effect.
3251 * Even if the above control/hanlder timed out then it
3252 * could still have worked!
3254 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3255 ctdb, &nodemap);
3256 if (ret != 0) {
3257 DEBUG(DEBUG_WARNING,
3258 ("Unable to get nodemap from local node, try again\n"));
3260 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3261 } while (nodemap == NULL || (set_flag != flag_is_set));
3263 return ipreallocate(ctdb);
3266 /* Administratively disable a node */
3267 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3269 int ret;
3271 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3272 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3273 return ret == 0;
3276 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3278 return update_flags_and_ipreallocate(ctdb, NULL,
3279 update_flags_disabled,
3280 NODE_FLAGS_PERMANENTLY_DISABLED,
3281 "disabled",
3282 true /* set_flag*/);
3285 /* Administratively re-enable a node */
3286 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3288 int ret;
3290 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3291 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3292 return ret == 0;
3295 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3297 return update_flags_and_ipreallocate(ctdb, NULL,
3298 update_flags_not_disabled,
3299 NODE_FLAGS_PERMANENTLY_DISABLED,
3300 "disabled",
3301 false /* set_flag*/);
3304 /* Stop a node */
3305 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3307 int ret;
3309 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3311 return ret == 0;
3314 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3316 return update_flags_and_ipreallocate(ctdb, NULL,
3317 update_flags_stopped,
3318 NODE_FLAGS_STOPPED,
3319 "stopped",
3320 true /* set_flag*/);
3323 /* Continue a stopped node */
3324 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3326 int ret;
3328 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3330 return ret == 0;
3333 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3335 return update_flags_and_ipreallocate(ctdb, NULL,
3336 update_flags_not_stopped,
3337 NODE_FLAGS_STOPPED,
3338 "stopped",
3339 false /* set_flag */);
3342 static uint32_t get_generation(struct ctdb_context *ctdb)
3344 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3345 struct ctdb_vnn_map *vnnmap=NULL;
3346 int ret;
3347 uint32_t generation;
3349 /* wait until the recmaster is not in recovery mode */
3350 while (1) {
3351 uint32_t recmode, recmaster;
3353 if (vnnmap != NULL) {
3354 talloc_free(vnnmap);
3355 vnnmap = NULL;
3358 /* get the recmaster */
3359 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3360 if (ret != 0) {
3361 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3362 talloc_free(tmp_ctx);
3363 exit(10);
3366 /* get recovery mode */
3367 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3368 if (ret != 0) {
3369 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3370 talloc_free(tmp_ctx);
3371 exit(10);
3374 /* get the current generation number */
3375 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3376 if (ret != 0) {
3377 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3378 talloc_free(tmp_ctx);
3379 exit(10);
3382 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3383 generation = vnnmap->generation;
3384 talloc_free(tmp_ctx);
3385 return generation;
3387 sleep(1);
3391 /* Ban a node */
3392 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3394 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3395 int ret;
3397 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3399 return ret == 0;
3402 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3404 struct ctdb_ban_time bantime;
3406 if (argc < 1) {
3407 usage();
3410 bantime.pnn = options.pnn;
3411 bantime.time = strtoul(argv[0], NULL, 0);
3413 if (bantime.time == 0) {
3414 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3415 return -1;
3418 return update_flags_and_ipreallocate(ctdb, &bantime,
3419 update_state_banned,
3420 NODE_FLAGS_BANNED,
3421 "banned",
3422 true /* set_flag*/);
3426 /* Unban a node */
3427 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3429 struct ctdb_ban_time bantime;
3431 bantime.pnn = options.pnn;
3432 bantime.time = 0;
3434 return update_flags_and_ipreallocate(ctdb, &bantime,
3435 update_state_banned,
3436 NODE_FLAGS_BANNED,
3437 "banned",
3438 false /* set_flag*/);
3442 show ban information for a node
3444 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3446 int ret;
3447 struct ctdb_node_map *nodemap=NULL;
3448 struct ctdb_ban_time *bantime;
3450 /* verify the node exists */
3451 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3452 if (ret != 0) {
3453 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3454 return ret;
3457 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3458 if (ret != 0) {
3459 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3460 return -1;
3463 if (bantime->time == 0) {
3464 printf("Node %u is not banned\n", bantime->pnn);
3465 } else {
3466 printf("Node %u is banned, %d seconds remaining\n",
3467 bantime->pnn, bantime->time);
3470 return 0;
3474 shutdown a daemon
3476 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3478 int ret;
3480 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3481 if (ret != 0) {
3482 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3483 return ret;
3486 return 0;
3490 trigger a recovery
3492 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3494 int ret;
3495 uint32_t generation, next_generation;
3497 /* record the current generation number */
3498 generation = get_generation(ctdb);
3500 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3501 if (ret != 0) {
3502 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3503 return ret;
3506 /* wait until we are in a new generation */
3507 while (1) {
3508 next_generation = get_generation(ctdb);
3509 if (next_generation != generation) {
3510 return 0;
3512 sleep(1);
3515 return 0;
3520 display monitoring mode of a remote node
3522 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3524 uint32_t monmode;
3525 int ret;
3527 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3528 if (ret != 0) {
3529 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3530 return ret;
3532 if (!options.machinereadable){
3533 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3534 } else {
3535 printf(":mode:\n");
3536 printf(":%d:\n",monmode);
3538 return 0;
3543 display capabilities of a remote node
3545 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3547 uint32_t capabilities;
3548 int ret;
3550 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3551 if (ret != 0) {
3552 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3553 return -1;
3556 if (!options.machinereadable){
3557 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3558 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3559 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3560 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3561 } else {
3562 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3563 printf(":%d:%d:%d:%d:\n",
3564 !!(capabilities&CTDB_CAP_RECMASTER),
3565 !!(capabilities&CTDB_CAP_LMASTER),
3566 !!(capabilities&CTDB_CAP_LVS),
3567 !!(capabilities&CTDB_CAP_NATGW));
3569 return 0;
3573 display lvs configuration
3576 static uint32_t lvs_exclude_flags[] = {
3577 /* Look for a nice healthy node */
3578 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3579 /* If not found, an UNHEALTHY node will do */
3580 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3584 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3586 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3587 struct ctdb_node_map *orig_nodemap=NULL;
3588 struct ctdb_node_map *nodemap;
3589 int i, ret;
3591 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3592 tmp_ctx, &orig_nodemap);
3593 if (ret != 0) {
3594 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3595 talloc_free(tmp_ctx);
3596 return -1;
3599 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3600 CTDB_CAP_LVS, false);
3601 if (nodemap == NULL) {
3602 /* No memory */
3603 ret = -1;
3604 goto done;
3607 ret = 0;
3609 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3610 struct ctdb_node_map *t =
3611 filter_nodemap_by_flags(ctdb, nodemap,
3612 lvs_exclude_flags[i]);
3613 if (t == NULL) {
3614 /* No memory */
3615 ret = -1;
3616 goto done;
3618 if (t->num > 0) {
3619 /* At least 1 node without excluded flags */
3620 int j;
3621 for (j = 0; j < t->num; j++) {
3622 printf("%d:%s\n", t->nodes[j].pnn,
3623 ctdb_addr_to_str(&t->nodes[j].addr));
3625 goto done;
3627 talloc_free(t);
3629 done:
3630 talloc_free(tmp_ctx);
3631 return ret;
3635 display who is the lvs master
3637 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3639 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3640 struct ctdb_node_map *nodemap=NULL;
3641 int i, ret;
3643 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3644 tmp_ctx, &nodemap);
3645 if (ret != 0) {
3646 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3647 talloc_free(tmp_ctx);
3648 return -1;
3651 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3652 struct ctdb_node_map *t =
3653 filter_nodemap_by_flags(ctdb, nodemap,
3654 lvs_exclude_flags[i]);
3655 if (t == NULL) {
3656 /* No memory */
3657 ret = -1;
3658 goto done;
3660 if (t->num > 0) {
3661 struct ctdb_node_map *n;
3662 n = filter_nodemap_by_capabilities(ctdb,
3664 CTDB_CAP_LVS,
3665 true);
3666 if (n == NULL) {
3667 /* No memory */
3668 ret = -1;
3669 goto done;
3671 if (n->num > 0) {
3672 ret = 0;
3673 printf(options.machinereadable ?
3674 "%d\n" : "Node %d is LVS master\n",
3675 n->nodes[0].pnn);
3676 goto done;
3679 talloc_free(t);
3682 printf("There is no LVS master\n");
3683 ret = 255;
3684 done:
3685 talloc_free(tmp_ctx);
3686 return ret;
3690 disable monitoring on a node
3692 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3695 int ret;
3697 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3698 if (ret != 0) {
3699 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3700 return ret;
3702 printf("Monitoring mode:%s\n","DISABLED");
3704 return 0;
3708 enable monitoring on a node
3710 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3713 int ret;
3715 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3716 if (ret != 0) {
3717 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3718 return ret;
3720 printf("Monitoring mode:%s\n","ACTIVE");
3722 return 0;
3726 display remote list of keys/data for a db
3728 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3730 const char *db_name;
3731 struct ctdb_db_context *ctdb_db;
3732 int ret;
3733 struct ctdb_dump_db_context c;
3734 uint8_t flags;
3736 if (argc < 1) {
3737 usage();
3740 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3741 return -1;
3744 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3745 if (ctdb_db == NULL) {
3746 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3747 return -1;
3750 if (options.printlmaster) {
3751 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3752 ctdb, &ctdb->vnn_map);
3753 if (ret != 0) {
3754 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3755 options.pnn));
3756 return ret;
3760 ZERO_STRUCT(c);
3761 c.f = stdout;
3762 c.printemptyrecords = (bool)options.printemptyrecords;
3763 c.printdatasize = (bool)options.printdatasize;
3764 c.printlmaster = (bool)options.printlmaster;
3765 c.printhash = (bool)options.printhash;
3766 c.printrecordflags = (bool)options.printrecordflags;
3768 /* traverse and dump the cluster tdb */
3769 ret = ctdb_dump_db(ctdb_db, &c);
3770 if (ret == -1) {
3771 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3772 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3773 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3774 db_name));
3775 return -1;
3777 talloc_free(ctdb_db);
3779 printf("Dumped %d records\n", ret);
3780 return 0;
3783 struct cattdb_data {
3784 struct ctdb_context *ctdb;
3785 uint32_t count;
3788 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3790 struct cattdb_data *d = private_data;
3791 struct ctdb_dump_db_context c;
3793 d->count++;
3795 ZERO_STRUCT(c);
3796 c.f = stdout;
3797 c.printemptyrecords = (bool)options.printemptyrecords;
3798 c.printdatasize = (bool)options.printdatasize;
3799 c.printlmaster = false;
3800 c.printhash = (bool)options.printhash;
3801 c.printrecordflags = true;
3803 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3807 cat the local tdb database using same format as catdb
3809 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3811 const char *db_name;
3812 struct ctdb_db_context *ctdb_db;
3813 struct cattdb_data d;
3814 uint8_t flags;
3816 if (argc < 1) {
3817 usage();
3820 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3821 return -1;
3824 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3825 if (ctdb_db == NULL) {
3826 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3827 return -1;
3830 /* traverse the local tdb */
3831 d.count = 0;
3832 d.ctdb = ctdb;
3833 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3834 printf("Failed to cattdb data\n");
3835 exit(10);
3837 talloc_free(ctdb_db);
3839 printf("Dumped %d records\n", d.count);
3840 return 0;
3844 display the content of a database key
3846 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3848 const char *db_name;
3849 struct ctdb_db_context *ctdb_db;
3850 struct ctdb_record_handle *h;
3851 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3852 TDB_DATA key, data;
3853 uint8_t flags;
3855 if (argc < 2) {
3856 usage();
3859 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3860 return -1;
3863 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3864 if (ctdb_db == NULL) {
3865 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3866 return -1;
3869 key.dptr = discard_const(argv[1]);
3870 key.dsize = strlen((char *)key.dptr);
3872 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3873 if (h == NULL) {
3874 printf("Failed to fetch record '%s' on node %d\n",
3875 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3876 talloc_free(tmp_ctx);
3877 exit(10);
3880 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3882 talloc_free(tmp_ctx);
3883 talloc_free(ctdb_db);
3884 return 0;
3888 display the content of a database key
3890 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3892 const char *db_name;
3893 struct ctdb_db_context *ctdb_db;
3894 struct ctdb_record_handle *h;
3895 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3896 TDB_DATA key, data;
3897 uint8_t flags;
3899 if (argc < 3) {
3900 usage();
3903 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3904 return -1;
3907 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3908 if (ctdb_db == NULL) {
3909 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3910 return -1;
3913 key.dptr = discard_const(argv[1]);
3914 key.dsize = strlen((char *)key.dptr);
3916 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3917 if (h == NULL) {
3918 printf("Failed to fetch record '%s' on node %d\n",
3919 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3920 talloc_free(tmp_ctx);
3921 exit(10);
3924 data.dptr = discard_const(argv[2]);
3925 data.dsize = strlen((char *)data.dptr);
3927 if (ctdb_record_store(h, data) != 0) {
3928 printf("Failed to store record\n");
3931 talloc_free(h);
3932 talloc_free(tmp_ctx);
3933 talloc_free(ctdb_db);
3934 return 0;
3938 fetch a record from a persistent database
3940 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3942 const char *db_name;
3943 struct ctdb_db_context *ctdb_db;
3944 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3945 struct ctdb_transaction_handle *h;
3946 TDB_DATA key, data;
3947 int fd, ret;
3948 bool persistent;
3949 uint8_t flags;
3951 if (argc < 2) {
3952 talloc_free(tmp_ctx);
3953 usage();
3956 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3957 talloc_free(tmp_ctx);
3958 return -1;
3961 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3962 if (!persistent) {
3963 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3964 talloc_free(tmp_ctx);
3965 return -1;
3968 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3969 if (ctdb_db == NULL) {
3970 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3971 talloc_free(tmp_ctx);
3972 return -1;
3975 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3976 if (h == NULL) {
3977 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3978 talloc_free(tmp_ctx);
3979 return -1;
3982 key.dptr = discard_const(argv[1]);
3983 key.dsize = strlen(argv[1]);
3984 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3985 if (ret != 0) {
3986 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3987 talloc_free(tmp_ctx);
3988 return -1;
3991 if (data.dsize == 0 || data.dptr == NULL) {
3992 DEBUG(DEBUG_ERR,("Record is empty\n"));
3993 talloc_free(tmp_ctx);
3994 return -1;
3997 if (argc == 3) {
3998 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3999 if (fd == -1) {
4000 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
4001 talloc_free(tmp_ctx);
4002 return -1;
4004 sys_write(fd, data.dptr, data.dsize);
4005 close(fd);
4006 } else {
4007 sys_write(1, data.dptr, data.dsize);
4010 /* abort the transaction */
4011 talloc_free(h);
4014 talloc_free(tmp_ctx);
4015 return 0;
4019 fetch a record from a tdb-file
4021 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
4023 const char *tdb_file;
4024 TDB_CONTEXT *tdb;
4025 TDB_DATA key, data;
4026 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4027 int fd;
4029 if (argc < 2) {
4030 usage();
4033 tdb_file = argv[0];
4035 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4036 if (tdb == NULL) {
4037 printf("Failed to open TDB file %s\n", tdb_file);
4038 return -1;
4041 if (!strncmp(argv[1], "0x", 2)) {
4042 key = hextodata(tmp_ctx, argv[1] + 2);
4043 if (key.dsize == 0) {
4044 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4045 return -1;
4047 } else {
4048 key.dptr = discard_const(argv[1]);
4049 key.dsize = strlen(argv[1]);
4052 data = tdb_fetch(tdb, key);
4053 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4054 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4055 tdb_close(tdb);
4056 return -1;
4059 tdb_close(tdb);
4061 if (argc == 3) {
4062 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4063 if (fd == -1) {
4064 printf("Failed to open output file %s\n", argv[2]);
4065 return -1;
4067 if (options.verbose){
4068 sys_write(fd, data.dptr, data.dsize);
4069 } else {
4070 sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4072 close(fd);
4073 } else {
4074 if (options.verbose){
4075 sys_write(1, data.dptr, data.dsize);
4076 } else {
4077 sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4081 talloc_free(tmp_ctx);
4082 return 0;
4086 store a record and header to a tdb-file
4088 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4090 const char *tdb_file;
4091 TDB_CONTEXT *tdb;
4092 TDB_DATA key, value, data;
4093 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4094 struct ctdb_ltdb_header header;
4096 if (argc < 3) {
4097 usage();
4100 tdb_file = argv[0];
4102 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4103 if (tdb == NULL) {
4104 printf("Failed to open TDB file %s\n", tdb_file);
4105 return -1;
4108 if (!strncmp(argv[1], "0x", 2)) {
4109 key = hextodata(tmp_ctx, argv[1] + 2);
4110 if (key.dsize == 0) {
4111 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4112 return -1;
4114 } else {
4115 key.dptr = discard_const(argv[1]);
4116 key.dsize = strlen(argv[1]);
4119 if (!strncmp(argv[2], "0x", 2)) {
4120 value = hextodata(tmp_ctx, argv[2] + 2);
4121 if (value.dsize == 0) {
4122 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4123 return -1;
4125 } else {
4126 value.dptr = discard_const(argv[2]);
4127 value.dsize = strlen(argv[2]);
4130 ZERO_STRUCT(header);
4131 if (argc > 3) {
4132 header.rsn = atoll(argv[3]);
4134 if (argc > 4) {
4135 header.dmaster = atoi(argv[4]);
4137 if (argc > 5) {
4138 header.flags = atoi(argv[5]);
4141 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4142 data.dptr = talloc_size(tmp_ctx, data.dsize);
4143 if (data.dptr == NULL) {
4144 printf("Failed to allocate header+value\n");
4145 return -1;
4148 *(struct ctdb_ltdb_header *)data.dptr = header;
4149 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4151 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4152 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4153 tdb_close(tdb);
4154 return -1;
4157 tdb_close(tdb);
4159 talloc_free(tmp_ctx);
4160 return 0;
4164 write a record to a persistent database
4166 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4168 const char *db_name;
4169 struct ctdb_db_context *ctdb_db;
4170 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4171 struct ctdb_transaction_handle *h;
4172 struct stat st;
4173 TDB_DATA key, data;
4174 int fd, ret;
4176 if (argc < 3) {
4177 talloc_free(tmp_ctx);
4178 usage();
4181 fd = open(argv[2], O_RDONLY);
4182 if (fd == -1) {
4183 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4184 talloc_free(tmp_ctx);
4185 return -1;
4188 ret = fstat(fd, &st);
4189 if (ret == -1) {
4190 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4191 close(fd);
4192 talloc_free(tmp_ctx);
4193 return -1;
4196 if (!S_ISREG(st.st_mode)) {
4197 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4198 close(fd);
4199 talloc_free(tmp_ctx);
4200 return -1;
4203 data.dsize = st.st_size;
4204 if (data.dsize == 0) {
4205 data.dptr = NULL;
4206 } else {
4207 data.dptr = talloc_size(tmp_ctx, data.dsize);
4208 if (data.dptr == NULL) {
4209 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4210 close(fd);
4211 talloc_free(tmp_ctx);
4212 return -1;
4214 ret = sys_read(fd, data.dptr, data.dsize);
4215 if (ret != data.dsize) {
4216 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4217 close(fd);
4218 talloc_free(tmp_ctx);
4219 return -1;
4222 close(fd);
4225 db_name = argv[0];
4227 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4228 if (ctdb_db == NULL) {
4229 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4230 talloc_free(tmp_ctx);
4231 return -1;
4234 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4235 if (h == NULL) {
4236 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4237 talloc_free(tmp_ctx);
4238 return -1;
4241 key.dptr = discard_const(argv[1]);
4242 key.dsize = strlen(argv[1]);
4243 ret = ctdb_transaction_store(h, key, data);
4244 if (ret != 0) {
4245 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4246 talloc_free(tmp_ctx);
4247 return -1;
4250 ret = ctdb_transaction_commit(h);
4251 if (ret != 0) {
4252 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4253 talloc_free(tmp_ctx);
4254 return -1;
4258 talloc_free(tmp_ctx);
4259 return 0;
4263 * delete a record from a persistent database
4265 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4267 const char *db_name;
4268 struct ctdb_db_context *ctdb_db;
4269 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4270 struct ctdb_transaction_handle *h;
4271 TDB_DATA key;
4272 int ret;
4273 bool persistent;
4274 uint8_t flags;
4276 if (argc < 2) {
4277 talloc_free(tmp_ctx);
4278 usage();
4281 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4282 talloc_free(tmp_ctx);
4283 return -1;
4286 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4287 if (!persistent) {
4288 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4289 talloc_free(tmp_ctx);
4290 return -1;
4293 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4294 if (ctdb_db == NULL) {
4295 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4296 talloc_free(tmp_ctx);
4297 return -1;
4300 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4301 if (h == NULL) {
4302 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4303 talloc_free(tmp_ctx);
4304 return -1;
4307 key.dptr = discard_const(argv[1]);
4308 key.dsize = strlen(argv[1]);
4309 ret = ctdb_transaction_store(h, key, tdb_null);
4310 if (ret != 0) {
4311 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4312 talloc_free(tmp_ctx);
4313 return -1;
4316 ret = ctdb_transaction_commit(h);
4317 if (ret != 0) {
4318 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4319 talloc_free(tmp_ctx);
4320 return -1;
4323 talloc_free(tmp_ctx);
4324 return 0;
4327 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4328 TDB_DATA *data)
4330 const char *t;
4331 size_t n;
4332 const char *ret; /* Next byte after successfully parsed value */
4334 /* Error, unless someone says otherwise */
4335 ret = NULL;
4336 /* Indicates no value to parse */
4337 *data = tdb_null;
4339 /* Skip whitespace */
4340 n = strspn(s, " \t");
4341 t = s + n;
4343 if (t[0] == '"') {
4344 /* Quoted ASCII string - no wide characters! */
4345 t++;
4346 n = strcspn(t, "\"");
4347 if (t[n] == '"') {
4348 if (n > 0) {
4349 data->dsize = n;
4350 data->dptr = talloc_memdup(mem_ctx, t, n);
4351 CTDB_NOMEM_ABORT(data->dptr);
4353 ret = t + n + 1;
4354 } else {
4355 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4357 } else {
4358 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4361 return ret;
4364 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4365 TDB_DATA *key, TDB_DATA *value)
4367 char line [1024]; /* FIXME: make this more flexible? */
4368 const char *t;
4369 char *ptr;
4371 ptr = fgets(line, sizeof(line), file);
4373 if (ptr == NULL) {
4374 return false;
4377 /* Get key */
4378 t = ptrans_parse_string(mem_ctx, line, key);
4379 if (t == NULL || key->dptr == NULL) {
4380 /* Line Ignored but not EOF */
4381 return true;
4384 /* Get value */
4385 t = ptrans_parse_string(mem_ctx, t, value);
4386 if (t == NULL) {
4387 /* Line Ignored but not EOF */
4388 talloc_free(key->dptr);
4389 *key = tdb_null;
4390 return true;
4393 return true;
4397 * Update a persistent database as per file/stdin
4399 static int control_ptrans(struct ctdb_context *ctdb,
4400 int argc, const char **argv)
4402 const char *db_name;
4403 struct ctdb_db_context *ctdb_db;
4404 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4405 struct ctdb_transaction_handle *h;
4406 TDB_DATA key, value;
4407 FILE *file;
4408 int ret;
4410 if (argc < 1) {
4411 talloc_free(tmp_ctx);
4412 usage();
4415 file = stdin;
4416 if (argc == 2) {
4417 file = fopen(argv[1], "r");
4418 if (file == NULL) {
4419 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4420 talloc_free(tmp_ctx);
4421 return -1;
4425 db_name = argv[0];
4427 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4428 if (ctdb_db == NULL) {
4429 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4430 goto error;
4433 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4434 if (h == NULL) {
4435 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4436 goto error;
4439 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4440 if (key.dsize != 0) {
4441 ret = ctdb_transaction_store(h, key, value);
4442 /* Minimise memory use */
4443 talloc_free(key.dptr);
4444 if (value.dptr != NULL) {
4445 talloc_free(value.dptr);
4447 if (ret != 0) {
4448 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4449 ctdb_transaction_cancel(h);
4450 goto error;
4455 ret = ctdb_transaction_commit(h);
4456 if (ret != 0) {
4457 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4458 goto error;
4461 if (file != stdin) {
4462 fclose(file);
4464 talloc_free(tmp_ctx);
4465 return 0;
4467 error:
4468 if (file != stdin) {
4469 fclose(file);
4472 talloc_free(tmp_ctx);
4473 return -1;
4477 check if a service is bound to a port or not
4479 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4481 int s, ret;
4482 int v;
4483 int port;
4484 struct sockaddr_in sin;
4486 if (argc != 1) {
4487 printf("Use: ctdb chktcport <port>\n");
4488 return EINVAL;
4491 port = atoi(argv[0]);
4493 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4494 if (s == -1) {
4495 printf("Failed to open local socket\n");
4496 return errno;
4499 v = fcntl(s, F_GETFL, 0);
4500 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4501 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4504 bzero(&sin, sizeof(sin));
4505 sin.sin_family = PF_INET;
4506 sin.sin_port = htons(port);
4507 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4508 close(s);
4509 if (ret == -1) {
4510 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4511 return errno;
4514 return 0;
4518 /* Reload public IPs on a specified nodes */
4519 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4521 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4522 uint32_t *nodes;
4523 uint32_t pnn_mode;
4524 uint32_t timeout;
4525 int ret;
4527 assert_single_node_only();
4529 if (argc > 1) {
4530 usage();
4533 /* Determine the nodes where IPs need to be reloaded */
4534 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4535 options.pnn, true, &nodes, &pnn_mode)) {
4536 ret = -1;
4537 goto done;
4540 again:
4541 /* Disable takeover runs on all connected nodes. A reply
4542 * indicating success is needed from each node so all nodes
4543 * will need to be active. This will retry until maxruntime
4544 * is exceeded, hence no error handling.
4546 * A check could be added to not allow reloading of IPs when
4547 * there are disconnected nodes. However, this should
4548 * probably be left up to the administrator.
4550 timeout = LONGTIMEOUT;
4551 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4552 "Disable takeover runs", true);
4554 /* Now tell all the desired nodes to reload their public IPs.
4555 * Keep trying this until it succeeds. This assumes all
4556 * failures are transient, which might not be true...
4558 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4559 nodes, 0, LONGTIMELIMIT(),
4560 false, tdb_null,
4561 NULL, NULL, NULL) != 0) {
4562 DEBUG(DEBUG_ERR,
4563 ("Unable to reload IPs on some nodes, try again.\n"));
4564 goto again;
4567 /* It isn't strictly necessary to wait until takeover runs are
4568 * re-enabled but doing so can't hurt.
4570 timeout = 0;
4571 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4572 "Enable takeover runs", true);
4574 ipreallocate(ctdb);
4576 ret = 0;
4577 done:
4578 talloc_free(tmp_ctx);
4579 return ret;
4583 display a list of the databases on a remote ctdb
4585 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4587 int i, ret;
4588 struct ctdb_dbid_map *dbmap=NULL;
4590 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4591 if (ret != 0) {
4592 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4593 return ret;
4596 if(options.machinereadable){
4597 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4598 for(i=0;i<dbmap->num;i++){
4599 const char *path;
4600 const char *name;
4601 const char *health;
4602 bool persistent;
4603 bool readonly;
4604 bool sticky;
4606 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4607 dbmap->dbs[i].dbid, ctdb, &path);
4608 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4609 dbmap->dbs[i].dbid, ctdb, &name);
4610 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4611 dbmap->dbs[i].dbid, ctdb, &health);
4612 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4613 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4614 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4615 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4616 dbmap->dbs[i].dbid, name, path,
4617 !!(persistent), !!(sticky),
4618 !!(health), !!(readonly));
4620 return 0;
4623 printf("Number of databases:%d\n", dbmap->num);
4624 for(i=0;i<dbmap->num;i++){
4625 const char *path;
4626 const char *name;
4627 const char *health;
4628 bool persistent;
4629 bool readonly;
4630 bool sticky;
4632 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4633 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4634 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4635 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4636 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4637 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4638 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4639 dbmap->dbs[i].dbid, name, path,
4640 persistent?" PERSISTENT":"",
4641 sticky?" STICKY":"",
4642 readonly?" READONLY":"",
4643 health?" UNHEALTHY":"");
4646 return 0;
4650 display the status of a database on a remote ctdb
4652 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4654 const char *db_name;
4655 uint32_t db_id;
4656 uint8_t flags;
4657 const char *path;
4658 const char *health;
4660 if (argc < 1) {
4661 usage();
4664 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4665 return -1;
4668 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4669 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4670 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4671 db_id, db_name, path,
4672 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4673 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4674 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4675 (health ? health : "OK"));
4677 return 0;
4681 check if the local node is recmaster or not
4682 it will return 1 if this node is the recmaster and 0 if it is not
4683 or if the local ctdb daemon could not be contacted
4685 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4687 uint32_t mypnn, recmaster;
4688 int ret;
4690 assert_single_node_only();
4692 mypnn = getpnn(ctdb);
4694 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4695 if (ret != 0) {
4696 printf("Failed to get the recmaster\n");
4697 return 1;
4700 if (recmaster != mypnn) {
4701 printf("this node is not the recmaster\n");
4702 return 1;
4705 printf("this node is the recmaster\n");
4706 return 0;
4710 ping a node
4712 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4714 int ret;
4715 struct timeval tv = timeval_current();
4716 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4717 if (ret == -1) {
4718 printf("Unable to get ping response from node %u\n", options.pnn);
4719 return -1;
4720 } else {
4721 printf("response from %u time=%.6f sec (%d clients)\n",
4722 options.pnn, timeval_elapsed(&tv), ret);
4724 return 0;
4729 get a node's runstate
4731 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4733 int ret;
4734 enum ctdb_runstate runstate;
4736 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4737 if (ret == -1) {
4738 printf("Unable to get runstate response from node %u\n",
4739 options.pnn);
4740 return -1;
4741 } else {
4742 bool found = true;
4743 enum ctdb_runstate t;
4744 int i;
4745 for (i=0; i<argc; i++) {
4746 found = false;
4747 t = runstate_from_string(argv[i]);
4748 if (t == CTDB_RUNSTATE_UNKNOWN) {
4749 printf("Invalid run state (%s)\n", argv[i]);
4750 return -1;
4753 if (t == runstate) {
4754 found = true;
4755 break;
4759 if (!found) {
4760 printf("CTDB not in required run state (got %s)\n",
4761 runstate_to_string((enum ctdb_runstate)runstate));
4762 return -1;
4766 printf("%s\n", runstate_to_string(runstate));
4767 return 0;
4772 get a tunable
4774 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4776 const char *name;
4777 uint32_t value;
4778 int ret;
4780 if (argc < 1) {
4781 usage();
4784 name = argv[0];
4785 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4786 if (ret != 0) {
4787 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4788 return -1;
4791 printf("%-23s = %u\n", name, value);
4792 return 0;
4796 set a tunable
4798 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4800 const char *name;
4801 uint32_t value;
4802 int ret;
4804 if (argc < 2) {
4805 usage();
4808 name = argv[0];
4809 value = strtoul(argv[1], NULL, 0);
4811 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4812 if (ret == -1) {
4813 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4814 return -1;
4816 return 0;
4820 list all tunables
4822 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4824 uint32_t count;
4825 const char **list;
4826 int ret, i;
4828 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4829 if (ret == -1) {
4830 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4831 return -1;
4834 for (i=0;i<count;i++) {
4835 control_getvar(ctdb, 1, &list[i]);
4838 talloc_free(list);
4840 return 0;
4844 display debug level on a node
4846 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4848 int ret;
4849 int32_t level;
4851 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4852 if (ret != 0) {
4853 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4854 return ret;
4855 } else {
4856 const char *desc = get_debug_by_level(level);
4857 if (desc == NULL) {
4858 /* This should never happen */
4859 desc = "Unknown";
4861 if (options.machinereadable){
4862 printf(":Name:Level:\n");
4863 printf(":%s:%d:\n", desc, level);
4864 } else {
4865 printf("Node %u is at debug level %s (%d)\n",
4866 options.pnn, desc, level);
4869 return 0;
4873 display reclock file of a node
4875 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4877 int ret;
4878 const char *reclock;
4880 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4881 if (ret != 0) {
4882 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4883 return ret;
4884 } else {
4885 if (options.machinereadable){
4886 if (reclock != NULL) {
4887 printf("%s", reclock);
4889 } else {
4890 if (reclock == NULL) {
4891 printf("No reclock file used.\n");
4892 } else {
4893 printf("Reclock file:%s\n", reclock);
4897 return 0;
4901 set the reclock file of a node
4903 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4905 int ret;
4906 const char *reclock;
4908 if (argc == 0) {
4909 reclock = NULL;
4910 } else if (argc == 1) {
4911 reclock = argv[0];
4912 } else {
4913 usage();
4916 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4917 if (ret != 0) {
4918 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4919 return ret;
4921 return 0;
4925 set the natgw state on/off
4927 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4929 int ret;
4930 uint32_t natgwstate;
4932 if (argc == 0) {
4933 usage();
4936 if (!strcmp(argv[0], "on")) {
4937 natgwstate = 1;
4938 } else if (!strcmp(argv[0], "off")) {
4939 natgwstate = 0;
4940 } else {
4941 usage();
4944 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4945 if (ret != 0) {
4946 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4947 return ret;
4950 return 0;
4954 set the lmaster role on/off
4956 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4958 int ret;
4959 uint32_t lmasterrole;
4961 if (argc == 0) {
4962 usage();
4965 if (!strcmp(argv[0], "on")) {
4966 lmasterrole = 1;
4967 } else if (!strcmp(argv[0], "off")) {
4968 lmasterrole = 0;
4969 } else {
4970 usage();
4973 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4974 if (ret != 0) {
4975 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4976 return ret;
4979 return 0;
4983 set the recmaster role on/off
4985 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4987 int ret;
4988 uint32_t recmasterrole;
4990 if (argc == 0) {
4991 usage();
4994 if (!strcmp(argv[0], "on")) {
4995 recmasterrole = 1;
4996 } else if (!strcmp(argv[0], "off")) {
4997 recmasterrole = 0;
4998 } else {
4999 usage();
5002 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5003 if (ret != 0) {
5004 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5005 return ret;
5008 return 0;
5012 set debug level on a node or all nodes
5014 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5016 int ret;
5017 int32_t level;
5019 if (argc == 0) {
5020 printf("You must specify the debug level. Valid levels are:\n");
5021 print_debug_levels(stdout);
5022 return 0;
5025 if (!parse_debug(argv[0], &level)) {
5026 printf("Invalid debug level, must be one of\n");
5027 print_debug_levels(stdout);
5028 return -1;
5031 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5032 if (ret != 0) {
5033 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5035 return 0;
5040 thaw a node
5042 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5044 int ret;
5045 uint32_t priority;
5047 if (argc == 1) {
5048 priority = strtol(argv[0], NULL, 0);
5049 } else {
5050 priority = 0;
5052 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5054 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5055 if (ret != 0) {
5056 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5058 return 0;
5063 attach to a database
5065 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5067 const char *db_name;
5068 struct ctdb_db_context *ctdb_db;
5069 bool persistent = false;
5071 if (argc < 1) {
5072 usage();
5074 db_name = argv[0];
5075 if (argc > 2) {
5076 usage();
5078 if (argc == 2) {
5079 if (strcmp(argv[1], "persistent") != 0) {
5080 usage();
5082 persistent = true;
5085 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5086 if (ctdb_db == NULL) {
5087 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5088 return -1;
5091 return 0;
5095 * detach from a database
5097 static int control_detach(struct ctdb_context *ctdb, int argc,
5098 const char **argv)
5100 uint32_t db_id;
5101 uint8_t flags;
5102 int ret, i, status = 0;
5103 struct ctdb_node_map *nodemap = NULL;
5104 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5105 uint32_t recmode;
5107 if (argc < 1) {
5108 usage();
5111 assert_single_node_only();
5113 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5114 &recmode);
5115 if (ret != 0) {
5116 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5117 "when recovery is active\n"));
5118 talloc_free(tmp_ctx);
5119 return -1;
5122 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5123 &nodemap);
5124 if (ret != 0) {
5125 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5126 options.pnn));
5127 talloc_free(tmp_ctx);
5128 return -1;
5131 for (i=0; i<nodemap->num; i++) {
5132 uint32_t value;
5134 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5135 continue;
5138 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5139 continue;
5142 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5143 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5144 "inactive (stopped or banned) node "
5145 "%u\n", nodemap->nodes[i].pnn));
5146 talloc_free(tmp_ctx);
5147 return -1;
5150 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5151 nodemap->nodes[i].pnn,
5152 "AllowClientDBAttach",
5153 &value);
5154 if (ret != 0) {
5155 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5156 "AllowClientDBAttach from node %u\n",
5157 nodemap->nodes[i].pnn));
5158 talloc_free(tmp_ctx);
5159 return -1;
5162 if (value == 1) {
5163 DEBUG(DEBUG_ERR, ("Database access is still active on "
5164 "node %u. Set AllowClientDBAttach=0 "
5165 "on all nodes.\n",
5166 nodemap->nodes[i].pnn));
5167 talloc_free(tmp_ctx);
5168 return -1;
5172 talloc_free(tmp_ctx);
5174 for (i=0; i<argc; i++) {
5175 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5176 continue;
5179 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5180 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5181 "cannot be detached\n", argv[i]));
5182 status = -1;
5183 continue;
5186 ret = ctdb_detach(ctdb, db_id);
5187 if (ret != 0) {
5188 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5189 argv[i]));
5190 status = ret;
5194 return status;
5198 set db priority
5200 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5202 struct ctdb_db_priority db_prio;
5203 int ret;
5205 if (argc < 2) {
5206 usage();
5209 db_prio.db_id = strtoul(argv[0], NULL, 0);
5210 db_prio.priority = strtoul(argv[1], NULL, 0);
5212 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5213 if (ret != 0) {
5214 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5215 return -1;
5218 return 0;
5222 get db priority
5224 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5226 uint32_t db_id, priority;
5227 int ret;
5229 if (argc < 1) {
5230 usage();
5233 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5234 return -1;
5237 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5238 if (ret != 0) {
5239 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5240 return -1;
5243 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5245 return 0;
5249 set the sticky records capability for a database
5251 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5253 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5254 uint32_t db_id;
5255 int ret;
5257 if (argc < 1) {
5258 usage();
5261 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5262 return -1;
5265 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5266 if (ret != 0) {
5267 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5268 talloc_free(tmp_ctx);
5269 return -1;
5272 talloc_free(tmp_ctx);
5273 return 0;
5277 set the readonly capability for a database
5279 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5281 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5282 uint32_t db_id;
5283 int ret;
5285 if (argc < 1) {
5286 usage();
5289 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5290 return -1;
5293 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5294 if (ret != 0) {
5295 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5296 talloc_free(tmp_ctx);
5297 return -1;
5300 talloc_free(tmp_ctx);
5301 return 0;
5305 get db seqnum
5307 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5309 uint32_t db_id;
5310 uint64_t seqnum;
5311 int ret;
5313 if (argc < 1) {
5314 usage();
5317 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5318 return -1;
5321 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5322 if (ret != 0) {
5323 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5324 return -1;
5327 printf("Sequence number:%lld\n", (long long)seqnum);
5329 return 0;
5333 run an eventscript on a node
5335 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5337 TDB_DATA data;
5338 int ret;
5339 int32_t res;
5340 char *errmsg;
5341 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5343 if (argc != 1) {
5344 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5345 return -1;
5348 data.dptr = (unsigned char *)discard_const(argv[0]);
5349 data.dsize = strlen((char *)data.dptr) + 1;
5351 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5353 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5354 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5355 if (ret != 0 || res != 0) {
5356 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5357 talloc_free(tmp_ctx);
5358 return -1;
5360 talloc_free(tmp_ctx);
5361 return 0;
5364 #define DB_VERSION 1
5365 #define MAX_DB_NAME 64
5366 struct db_file_header {
5367 unsigned long version;
5368 time_t timestamp;
5369 unsigned long persistent;
5370 unsigned long size;
5371 const char name[MAX_DB_NAME];
5374 struct backup_data {
5375 struct ctdb_marshall_buffer *records;
5376 uint32_t len;
5377 uint32_t total;
5378 bool traverse_error;
5381 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5383 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5384 struct ctdb_rec_data *rec;
5386 /* add the record */
5387 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5388 if (rec == NULL) {
5389 bd->traverse_error = true;
5390 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5391 return -1;
5393 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5394 if (bd->records == NULL) {
5395 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5396 bd->traverse_error = true;
5397 return -1;
5399 bd->records->count++;
5400 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5401 bd->len += rec->length;
5402 talloc_free(rec);
5404 bd->total++;
5405 return 0;
5409 * backup a database to a file
5411 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5413 const char *db_name;
5414 int ret;
5415 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5416 struct db_file_header dbhdr;
5417 struct ctdb_db_context *ctdb_db;
5418 struct backup_data *bd;
5419 int fh = -1;
5420 int status = -1;
5421 const char *reason = NULL;
5422 uint32_t db_id;
5423 uint8_t flags;
5425 assert_single_node_only();
5427 if (argc != 2) {
5428 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5429 return -1;
5432 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5433 return -1;
5436 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5437 db_id, tmp_ctx, &reason);
5438 if (ret != 0) {
5439 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5440 argv[0]));
5441 talloc_free(tmp_ctx);
5442 return -1;
5444 if (reason) {
5445 uint32_t allow_unhealthy = 0;
5447 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5448 "AllowUnhealthyDBRead",
5449 &allow_unhealthy);
5451 if (allow_unhealthy != 1) {
5452 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5453 argv[0], reason));
5455 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5456 allow_unhealthy));
5457 talloc_free(tmp_ctx);
5458 return -1;
5461 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5462 argv[0], argv[0]));
5463 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5464 "tunnable AllowUnhealthyDBRead = %u\n",
5465 allow_unhealthy));
5468 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5469 if (ctdb_db == NULL) {
5470 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5471 talloc_free(tmp_ctx);
5472 return -1;
5476 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5477 if (ret == -1) {
5478 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5479 talloc_free(tmp_ctx);
5480 return -1;
5484 bd = talloc_zero(tmp_ctx, struct backup_data);
5485 if (bd == NULL) {
5486 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5487 talloc_free(tmp_ctx);
5488 return -1;
5491 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5492 if (bd->records == NULL) {
5493 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5494 talloc_free(tmp_ctx);
5495 return -1;
5498 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5499 bd->records->db_id = ctdb_db->db_id;
5500 /* traverse the database collecting all records */
5501 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5502 bd->traverse_error) {
5503 DEBUG(DEBUG_ERR,("Traverse error\n"));
5504 talloc_free(tmp_ctx);
5505 return -1;
5508 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5511 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5512 if (fh == -1) {
5513 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5514 talloc_free(tmp_ctx);
5515 return -1;
5518 ZERO_STRUCT(dbhdr);
5519 dbhdr.version = DB_VERSION;
5520 dbhdr.timestamp = time(NULL);
5521 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5522 dbhdr.size = bd->len;
5523 if (strlen(argv[0]) >= MAX_DB_NAME) {
5524 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5525 goto done;
5527 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5528 ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
5529 if (ret == -1) {
5530 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5531 goto done;
5533 ret = sys_write(fh, bd->records, bd->len);
5534 if (ret == -1) {
5535 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5536 goto done;
5539 status = 0;
5540 done:
5541 if (fh != -1) {
5542 ret = close(fh);
5543 if (ret == -1) {
5544 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5548 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5550 talloc_free(tmp_ctx);
5551 return status;
5555 * restore a database from a file
5557 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5559 int ret;
5560 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5561 TDB_DATA outdata;
5562 TDB_DATA data;
5563 struct db_file_header dbhdr;
5564 struct ctdb_db_context *ctdb_db;
5565 struct ctdb_node_map *nodemap=NULL;
5566 struct ctdb_vnn_map *vnnmap=NULL;
5567 int i, fh;
5568 struct ctdb_control_wipe_database w;
5569 uint32_t *nodes;
5570 uint32_t generation;
5571 struct tm *tm;
5572 char tbuf[100];
5573 char *dbname;
5575 assert_single_node_only();
5577 if (argc < 1 || argc > 2) {
5578 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5579 return -1;
5582 fh = open(argv[0], O_RDONLY);
5583 if (fh == -1) {
5584 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5585 talloc_free(tmp_ctx);
5586 return -1;
5589 sys_read(fh, &dbhdr, sizeof(dbhdr));
5590 if (dbhdr.version != DB_VERSION) {
5591 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5592 close(fh);
5593 talloc_free(tmp_ctx);
5594 return -1;
5597 dbname = discard_const(dbhdr.name);
5598 if (argc == 2) {
5599 dbname = discard_const(argv[1]);
5602 outdata.dsize = dbhdr.size;
5603 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5604 if (outdata.dptr == NULL) {
5605 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5606 close(fh);
5607 talloc_free(tmp_ctx);
5608 return -1;
5610 sys_read(fh, outdata.dptr, outdata.dsize);
5611 close(fh);
5613 tm = localtime(&dbhdr.timestamp);
5614 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5615 printf("Restoring database '%s' from backup @ %s\n",
5616 dbname, tbuf);
5619 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5620 if (ctdb_db == NULL) {
5621 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5622 talloc_free(tmp_ctx);
5623 return -1;
5626 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5627 if (ret != 0) {
5628 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5629 talloc_free(tmp_ctx);
5630 return ret;
5634 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5635 if (ret != 0) {
5636 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5637 talloc_free(tmp_ctx);
5638 return ret;
5641 /* freeze all nodes */
5642 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5643 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5644 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5645 nodes, i,
5646 TIMELIMIT(),
5647 false, tdb_null,
5648 NULL, NULL,
5649 NULL) != 0) {
5650 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5651 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5652 talloc_free(tmp_ctx);
5653 return -1;
5657 generation = vnnmap->generation;
5658 data.dptr = (void *)&generation;
5659 data.dsize = sizeof(generation);
5661 /* start a cluster wide transaction */
5662 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5663 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5664 nodes, 0,
5665 TIMELIMIT(), false, data,
5666 NULL, NULL,
5667 NULL) != 0) {
5668 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5669 return -1;
5673 w.db_id = ctdb_db->db_id;
5674 w.transaction_id = generation;
5676 data.dptr = (void *)&w;
5677 data.dsize = sizeof(w);
5679 /* wipe all the remote databases. */
5680 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5681 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5682 nodes, 0,
5683 TIMELIMIT(), false, data,
5684 NULL, NULL,
5685 NULL) != 0) {
5686 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5687 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5688 talloc_free(tmp_ctx);
5689 return -1;
5692 /* push the database */
5693 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5694 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5695 nodes, 0,
5696 TIMELIMIT(), false, outdata,
5697 NULL, NULL,
5698 NULL) != 0) {
5699 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5700 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5701 talloc_free(tmp_ctx);
5702 return -1;
5705 data.dptr = (void *)&ctdb_db->db_id;
5706 data.dsize = sizeof(ctdb_db->db_id);
5708 /* mark the database as healthy */
5709 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5710 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5711 nodes, 0,
5712 TIMELIMIT(), false, data,
5713 NULL, NULL,
5714 NULL) != 0) {
5715 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5716 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5717 talloc_free(tmp_ctx);
5718 return -1;
5721 data.dptr = (void *)&generation;
5722 data.dsize = sizeof(generation);
5724 /* commit all the changes */
5725 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5726 nodes, 0,
5727 TIMELIMIT(), false, data,
5728 NULL, NULL,
5729 NULL) != 0) {
5730 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5731 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5732 talloc_free(tmp_ctx);
5733 return -1;
5737 /* thaw all nodes */
5738 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5739 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5740 nodes, 0,
5741 TIMELIMIT(),
5742 false, tdb_null,
5743 NULL, NULL,
5744 NULL) != 0) {
5745 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5746 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5747 talloc_free(tmp_ctx);
5748 return -1;
5752 talloc_free(tmp_ctx);
5753 return 0;
5757 * dump a database backup from a file
5759 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5761 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5762 TDB_DATA outdata;
5763 struct db_file_header dbhdr;
5764 int i, fh;
5765 struct tm *tm;
5766 char tbuf[100];
5767 struct ctdb_rec_data *rec = NULL;
5768 struct ctdb_marshall_buffer *m;
5769 struct ctdb_dump_db_context c;
5771 assert_single_node_only();
5773 if (argc != 1) {
5774 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5775 return -1;
5778 fh = open(argv[0], O_RDONLY);
5779 if (fh == -1) {
5780 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5781 talloc_free(tmp_ctx);
5782 return -1;
5785 sys_read(fh, &dbhdr, sizeof(dbhdr));
5786 if (dbhdr.version != DB_VERSION) {
5787 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5788 close(fh);
5789 talloc_free(tmp_ctx);
5790 return -1;
5793 outdata.dsize = dbhdr.size;
5794 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5795 if (outdata.dptr == NULL) {
5796 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5797 close(fh);
5798 talloc_free(tmp_ctx);
5799 return -1;
5801 sys_read(fh, outdata.dptr, outdata.dsize);
5802 close(fh);
5803 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5805 tm = localtime(&dbhdr.timestamp);
5806 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5807 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5808 dbhdr.name, m->db_id, tbuf);
5810 ZERO_STRUCT(c);
5811 c.f = stdout;
5812 c.printemptyrecords = (bool)options.printemptyrecords;
5813 c.printdatasize = (bool)options.printdatasize;
5814 c.printlmaster = false;
5815 c.printhash = (bool)options.printhash;
5816 c.printrecordflags = (bool)options.printrecordflags;
5818 for (i=0; i < m->count; i++) {
5819 uint32_t reqid = 0;
5820 TDB_DATA key, data;
5822 /* we do not want the header splitted, so we pass NULL*/
5823 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5824 NULL, &key, &data);
5826 ctdb_dumpdb_record(ctdb, key, data, &c);
5829 printf("Dumped %d records\n", i);
5830 talloc_free(tmp_ctx);
5831 return 0;
5835 * wipe a database from a file
5837 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5838 const char **argv)
5840 const char *db_name;
5841 int ret;
5842 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5843 TDB_DATA data;
5844 struct ctdb_db_context *ctdb_db;
5845 struct ctdb_node_map *nodemap = NULL;
5846 struct ctdb_vnn_map *vnnmap = NULL;
5847 int i;
5848 struct ctdb_control_wipe_database w;
5849 uint32_t *nodes;
5850 uint32_t generation;
5851 uint8_t flags;
5853 assert_single_node_only();
5855 if (argc != 1) {
5856 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5857 return -1;
5860 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5861 return -1;
5864 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5865 if (ctdb_db == NULL) {
5866 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5867 argv[0]));
5868 talloc_free(tmp_ctx);
5869 return -1;
5872 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5873 &nodemap);
5874 if (ret != 0) {
5875 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5876 options.pnn));
5877 talloc_free(tmp_ctx);
5878 return ret;
5881 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5882 &vnnmap);
5883 if (ret != 0) {
5884 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5885 options.pnn));
5886 talloc_free(tmp_ctx);
5887 return ret;
5890 /* freeze all nodes */
5891 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5892 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5893 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5894 nodes, i,
5895 TIMELIMIT(),
5896 false, tdb_null,
5897 NULL, NULL,
5898 NULL);
5899 if (ret != 0) {
5900 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5901 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5902 CTDB_RECOVERY_ACTIVE);
5903 talloc_free(tmp_ctx);
5904 return -1;
5908 generation = vnnmap->generation;
5909 data.dptr = (void *)&generation;
5910 data.dsize = sizeof(generation);
5912 /* start a cluster wide transaction */
5913 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5914 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5915 nodes, 0,
5916 TIMELIMIT(), false, data,
5917 NULL, NULL,
5918 NULL);
5919 if (ret!= 0) {
5920 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5921 "transactions.\n"));
5922 return -1;
5925 w.db_id = ctdb_db->db_id;
5926 w.transaction_id = generation;
5928 data.dptr = (void *)&w;
5929 data.dsize = sizeof(w);
5931 /* wipe all the remote databases. */
5932 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5933 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5934 nodes, 0,
5935 TIMELIMIT(), false, data,
5936 NULL, NULL,
5937 NULL) != 0) {
5938 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5939 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5940 talloc_free(tmp_ctx);
5941 return -1;
5944 data.dptr = (void *)&ctdb_db->db_id;
5945 data.dsize = sizeof(ctdb_db->db_id);
5947 /* mark the database as healthy */
5948 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5949 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5950 nodes, 0,
5951 TIMELIMIT(), false, data,
5952 NULL, NULL,
5953 NULL) != 0) {
5954 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5955 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5956 talloc_free(tmp_ctx);
5957 return -1;
5960 data.dptr = (void *)&generation;
5961 data.dsize = sizeof(generation);
5963 /* commit all the changes */
5964 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5965 nodes, 0,
5966 TIMELIMIT(), false, data,
5967 NULL, NULL,
5968 NULL) != 0) {
5969 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5970 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5971 talloc_free(tmp_ctx);
5972 return -1;
5975 /* thaw all nodes */
5976 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5977 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5978 nodes, 0,
5979 TIMELIMIT(),
5980 false, tdb_null,
5981 NULL, NULL,
5982 NULL) != 0) {
5983 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5984 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5985 talloc_free(tmp_ctx);
5986 return -1;
5989 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5991 talloc_free(tmp_ctx);
5992 return 0;
5996 dump memory usage
5998 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6000 TDB_DATA data;
6001 int ret;
6002 int32_t res;
6003 char *errmsg;
6004 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6005 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6006 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6007 if (ret != 0 || res != 0) {
6008 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6009 talloc_free(tmp_ctx);
6010 return -1;
6012 sys_write(1, data.dptr, data.dsize);
6013 talloc_free(tmp_ctx);
6014 return 0;
6018 handler for memory dumps
6020 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6021 TDB_DATA data, void *private_data)
6023 sys_write(1, data.dptr, data.dsize);
6024 exit(0);
6028 dump memory usage on the recovery daemon
6030 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6032 int ret;
6033 TDB_DATA data;
6034 struct srvid_request rd;
6036 rd.pnn = ctdb_get_pnn(ctdb);
6037 rd.srvid = getpid();
6039 /* register a message port for receiveing the reply so that we
6040 can receive the reply
6042 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6045 data.dptr = (uint8_t *)&rd;
6046 data.dsize = sizeof(rd);
6048 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6049 if (ret != 0) {
6050 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6051 return -1;
6054 /* this loop will terminate when we have received the reply */
6055 while (1) {
6056 event_loop_once(ctdb->ev);
6059 return 0;
6063 send a message to a srvid
6065 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6067 unsigned long srvid;
6068 int ret;
6069 TDB_DATA data;
6071 if (argc < 2) {
6072 usage();
6075 srvid = strtoul(argv[0], NULL, 0);
6077 data.dptr = (uint8_t *)discard_const(argv[1]);
6078 data.dsize= strlen(argv[1]);
6080 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6081 if (ret != 0) {
6082 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6083 return -1;
6086 return 0;
6090 handler for msglisten
6092 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6093 TDB_DATA data, void *private_data)
6095 int i;
6097 printf("Message received: ");
6098 for (i=0;i<data.dsize;i++) {
6099 printf("%c", data.dptr[i]);
6101 printf("\n");
6105 listen for messages on a messageport
6107 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6109 uint64_t srvid;
6111 srvid = getpid();
6113 /* register a message port and listen for messages
6115 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6116 printf("Listening for messages on srvid:%d\n", (int)srvid);
6118 while (1) {
6119 event_loop_once(ctdb->ev);
6122 return 0;
6126 list all nodes in the cluster
6127 we parse the nodes file directly
6129 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6131 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6132 struct pnn_node *pnn_nodes;
6133 struct pnn_node *pnn_node;
6135 assert_single_node_only();
6137 pnn_nodes = read_nodes_file(mem_ctx);
6138 if (pnn_nodes == NULL) {
6139 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6140 talloc_free(mem_ctx);
6141 return -1;
6144 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6145 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6146 if (options.machinereadable){
6147 printf(":%d:%s:\n", pnn_node->pnn, addr);
6148 } else {
6149 printf("%s\n", addr);
6152 talloc_free(mem_ctx);
6154 return 0;
6158 reload the nodes file on the local node
6160 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6162 int i, ret;
6163 int mypnn;
6164 struct ctdb_node_map *nodemap=NULL;
6166 assert_single_node_only();
6168 mypnn = ctdb_get_pnn(ctdb);
6170 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6171 if (ret != 0) {
6172 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6173 return ret;
6176 /* reload the nodes file on all remote nodes */
6177 for (i=0;i<nodemap->num;i++) {
6178 if (nodemap->nodes[i].pnn == mypnn) {
6179 continue;
6181 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6182 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6183 nodemap->nodes[i].pnn);
6184 if (ret != 0) {
6185 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6189 /* reload the nodes file on the local node */
6190 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6191 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6192 if (ret != 0) {
6193 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6196 /* initiate a recovery */
6197 control_recover(ctdb, argc, argv);
6199 return 0;
6203 static const struct {
6204 const char *name;
6205 int (*fn)(struct ctdb_context *, int, const char **);
6206 bool auto_all;
6207 bool without_daemon; /* can be run without daemon running ? */
6208 const char *msg;
6209 const char *args;
6210 } ctdb_commands[] = {
6211 { "version", control_version, true, true, "show version of ctdb" },
6212 { "status", control_status, true, false, "show node status" },
6213 { "uptime", control_uptime, true, false, "show node uptime" },
6214 { "ping", control_ping, true, false, "ping all nodes" },
6215 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6216 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6217 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6218 { "listvars", control_listvars, true, false, "list tunable variables"},
6219 { "statistics", control_statistics, false, false, "show statistics" },
6220 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6221 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6222 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6223 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6224 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6225 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6226 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6227 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6228 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6229 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6230 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6231 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6232 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6233 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6234 { "lvs", control_lvs, true, false, "show lvs configuration" },
6235 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6236 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6237 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6238 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6239 { "getdebug", control_getdebug, true, false, "get debug level" },
6240 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6241 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6242 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6243 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6244 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6245 { "disable", control_disable, true, false, "disable a nodes public IP" },
6246 { "enable", control_enable, true, false, "enable a nodes public IP" },
6247 { "stop", control_stop, true, false, "stop a node" },
6248 { "continue", control_continue, true, false, "re-start a stopped node" },
6249 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6250 { "unban", control_unban, true, false, "unban a node" },
6251 { "showban", control_showban, true, false, "show ban information"},
6252 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6253 { "recover", control_recover, true, false, "force recovery" },
6254 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6255 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6256 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6257 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6258 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6259 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6260 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6261 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6262 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6264 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6266 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6267 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6268 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6269 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6270 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6271 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6272 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6273 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6274 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6275 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6276 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6277 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6278 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6279 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6280 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6281 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6282 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6283 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6284 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6285 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6286 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6287 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6288 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6289 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6290 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6291 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6292 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6293 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6294 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6295 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6296 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6297 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6298 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6299 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6300 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6301 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6302 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6303 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6304 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6305 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6306 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6307 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6308 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6309 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6310 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6311 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6312 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6313 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6314 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6318 show usage message
6320 static void usage(void)
6322 int i;
6323 printf(
6324 "Usage: ctdb [options] <control>\n" \
6325 "Options:\n" \
6326 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6327 " -Y generate machinereadable output\n"
6328 " -v generate verbose output\n"
6329 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6330 printf("Controls:\n");
6331 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6332 printf(" %-15s %-27s %s\n",
6333 ctdb_commands[i].name,
6334 ctdb_commands[i].args?ctdb_commands[i].args:"",
6335 ctdb_commands[i].msg);
6337 exit(1);
6341 static void ctdb_alarm(int sig)
6343 printf("Maximum runtime exceeded - exiting\n");
6344 _exit(ERR_TIMEOUT);
6348 main program
6350 int main(int argc, const char *argv[])
6352 struct ctdb_context *ctdb;
6353 char *nodestring = NULL;
6354 struct poptOption popt_options[] = {
6355 POPT_AUTOHELP
6356 POPT_CTDB_CMDLINE
6357 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6358 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6359 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6360 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6361 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6362 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6363 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6364 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6365 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6366 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6367 POPT_TABLEEND
6369 int opt;
6370 const char **extra_argv;
6371 int extra_argc = 0;
6372 int ret=-1, i;
6373 poptContext pc;
6374 struct event_context *ev;
6375 const char *control;
6377 setlinebuf(stdout);
6379 /* set some defaults */
6380 options.maxruntime = 0;
6381 options.timelimit = 10;
6382 options.pnn = CTDB_CURRENT_NODE;
6384 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6386 while ((opt = poptGetNextOpt(pc)) != -1) {
6387 switch (opt) {
6388 default:
6389 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6390 poptBadOption(pc, 0), poptStrerror(opt)));
6391 exit(1);
6395 /* setup the remaining options for the main program to use */
6396 extra_argv = poptGetArgs(pc);
6397 if (extra_argv) {
6398 extra_argv++;
6399 while (extra_argv[extra_argc]) extra_argc++;
6402 if (extra_argc < 1) {
6403 usage();
6406 if (options.maxruntime == 0) {
6407 const char *ctdb_timeout;
6408 ctdb_timeout = getenv("CTDB_TIMEOUT");
6409 if (ctdb_timeout != NULL) {
6410 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6411 } else {
6412 /* default timeout is 120 seconds */
6413 options.maxruntime = 120;
6417 signal(SIGALRM, ctdb_alarm);
6418 alarm(options.maxruntime);
6420 control = extra_argv[0];
6422 /* Default value for CTDB_BASE - don't override */
6423 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6425 ev = event_context_init(NULL);
6426 if (!ev) {
6427 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6428 exit(1);
6431 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6432 if (strcmp(control, ctdb_commands[i].name) == 0) {
6433 break;
6437 if (i == ARRAY_SIZE(ctdb_commands)) {
6438 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6439 exit(1);
6442 if (ctdb_commands[i].without_daemon == true) {
6443 if (nodestring != NULL) {
6444 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6445 exit(1);
6447 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6450 /* initialise ctdb */
6451 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6453 if (ctdb == NULL) {
6454 uint32_t pnn;
6455 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6457 pnn = find_node_xpnn();
6458 if (pnn == -1) {
6459 DEBUG(DEBUG_ERR,
6460 ("Is this node part of a CTDB cluster?\n"));
6462 exit(1);
6465 /* setup the node number(s) to contact */
6466 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6467 &options.nodes, &options.pnn)) {
6468 usage();
6471 if (options.pnn == CTDB_CURRENT_NODE) {
6472 options.pnn = options.nodes[0];
6475 if (ctdb_commands[i].auto_all &&
6476 ((options.pnn == CTDB_BROADCAST_ALL) ||
6477 (options.pnn == CTDB_MULTICAST))) {
6478 int j;
6480 ret = 0;
6481 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6482 options.pnn = options.nodes[j];
6483 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6485 } else {
6486 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6489 talloc_free(ctdb);
6490 talloc_free(ev);
6491 (void)poptFreeContext(pc);
6493 return ret;