smbd/nmbd: Remove HAVE_LONGLONG
[Samba.git] / ctdb / tools / ctdb.c
blob3b158fd7cdb72c37c0f3d9f933d1de96675ac858
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 "db_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 int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
62 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
63 return 0;
66 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
67 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
68 "Out of memory in " __location__ )); \
69 abort(); \
70 }} while (0)
72 static uint32_t getpnn(struct ctdb_context *ctdb)
74 if ((options.pnn == CTDB_BROADCAST_ALL) ||
75 (options.pnn == CTDB_MULTICAST)) {
76 DEBUG(DEBUG_ERR,
77 ("Cannot get PNN for node %u\n", options.pnn));
78 exit(1);
81 if (options.pnn == CTDB_CURRENT_NODE) {
82 return ctdb_get_pnn(ctdb);
83 } else {
84 return options.pnn;
88 static void assert_single_node_only(void)
90 if ((options.pnn == CTDB_BROADCAST_ALL) ||
91 (options.pnn == CTDB_MULTICAST)) {
92 DEBUG(DEBUG_ERR,
93 ("This control can not be applied to multiple PNNs\n"));
94 exit(1);
98 /* Pretty print the flags to a static buffer in human-readable format.
99 * This never returns NULL!
101 static const char *pretty_print_flags(uint32_t flags)
103 int j;
104 static const struct {
105 uint32_t flag;
106 const char *name;
107 } flag_names[] = {
108 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
109 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
110 { NODE_FLAGS_BANNED, "BANNED" },
111 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
112 { NODE_FLAGS_DELETED, "DELETED" },
113 { NODE_FLAGS_STOPPED, "STOPPED" },
114 { NODE_FLAGS_INACTIVE, "INACTIVE" },
116 static char flags_str[512]; /* Big enough to contain all flag names */
118 flags_str[0] = '\0';
119 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
120 if (flags & flag_names[j].flag) {
121 if (flags_str[0] == '\0') {
122 (void) strcpy(flags_str, flag_names[j].name);
123 } else {
124 (void) strncat(flags_str, "|", sizeof(flags_str)-1);
125 (void) strncat(flags_str, flag_names[j].name,
126 sizeof(flags_str)-1);
130 if (flags_str[0] == '\0') {
131 (void) strcpy(flags_str, "OK");
134 return flags_str;
137 static int h2i(char h)
139 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
140 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
141 return h - '0';
144 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
146 int i, len;
147 TDB_DATA key = {NULL, 0};
149 len = strlen(str);
150 if (len & 0x01) {
151 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
152 return key;
155 key.dsize = len>>1;
156 key.dptr = talloc_size(mem_ctx, key.dsize);
158 for (i=0; i < len/2; i++) {
159 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
161 return key;
164 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
165 * that are disconnected or deleted. If dd_ok is true those nodes are
166 * included in the output list of nodes. If dd_ok is false, those
167 * nodes are filtered from the "all" case and cause an error if
168 * explicitly specified.
170 static bool parse_nodestring(struct ctdb_context *ctdb,
171 TALLOC_CTX *mem_ctx,
172 const char * nodestring,
173 uint32_t current_pnn,
174 bool dd_ok,
175 uint32_t **nodes,
176 uint32_t *pnn_mode)
178 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
179 int n;
180 uint32_t i;
181 struct ctdb_node_map *nodemap;
182 int ret;
184 *nodes = NULL;
186 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
187 if (ret != 0) {
188 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
189 talloc_free(tmp_ctx);
190 exit(10);
193 if (nodestring != NULL) {
194 *nodes = talloc_array(mem_ctx, uint32_t, 0);
195 if (*nodes == NULL) {
196 goto failed;
199 n = 0;
201 if (strcmp(nodestring, "all") == 0) {
202 *pnn_mode = CTDB_BROADCAST_ALL;
204 /* all */
205 for (i = 0; i < nodemap->num; i++) {
206 if ((nodemap->nodes[i].flags &
207 (NODE_FLAGS_DISCONNECTED |
208 NODE_FLAGS_DELETED)) && !dd_ok) {
209 continue;
211 *nodes = talloc_realloc(mem_ctx, *nodes,
212 uint32_t, n+1);
213 if (*nodes == NULL) {
214 goto failed;
216 (*nodes)[n] = i;
217 n++;
219 } else {
220 /* x{,y...} */
221 char *ns, *tok;
223 ns = talloc_strdup(tmp_ctx, nodestring);
224 tok = strtok(ns, ",");
225 while (tok != NULL) {
226 uint32_t pnn;
227 char *endptr;
228 i = (uint32_t)strtoul(tok, &endptr, 0);
229 if (i == 0 && tok == endptr) {
230 DEBUG(DEBUG_ERR,
231 ("Invalid node %s\n", tok));
232 talloc_free(tmp_ctx);
233 exit(ERR_NONODE);
235 if (i >= nodemap->num) {
236 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
237 talloc_free(tmp_ctx);
238 exit(ERR_NONODE);
240 if ((nodemap->nodes[i].flags &
241 (NODE_FLAGS_DISCONNECTED |
242 NODE_FLAGS_DELETED)) && !dd_ok) {
243 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
244 talloc_free(tmp_ctx);
245 exit(ERR_DISNODE);
247 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
248 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
249 talloc_free(tmp_ctx);
250 exit(10);
253 *nodes = talloc_realloc(mem_ctx, *nodes,
254 uint32_t, n+1);
255 if (*nodes == NULL) {
256 goto failed;
259 (*nodes)[n] = i;
260 n++;
262 tok = strtok(NULL, ",");
264 talloc_free(ns);
266 if (n == 1) {
267 *pnn_mode = (*nodes)[0];
268 } else {
269 *pnn_mode = CTDB_MULTICAST;
272 } else {
273 /* default - no nodes specified */
274 *nodes = talloc_array(mem_ctx, uint32_t, 1);
275 if (*nodes == NULL) {
276 goto failed;
278 *pnn_mode = CTDB_CURRENT_NODE;
280 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
281 goto failed;
285 talloc_free(tmp_ctx);
286 return true;
288 failed:
289 talloc_free(tmp_ctx);
290 return false;
294 check if a database exists
296 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
297 uint32_t *dbid, const char **dbname, uint8_t *flags)
299 int i, ret;
300 struct ctdb_dbid_map *dbmap=NULL;
301 bool dbid_given = false, found = false;
302 uint32_t id;
303 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
304 const char *name;
306 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
307 if (ret != 0) {
308 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
309 goto fail;
312 if (strncmp(dbarg, "0x", 2) == 0) {
313 id = strtoul(dbarg, NULL, 0);
314 dbid_given = true;
317 for(i=0; i<dbmap->num; i++) {
318 if (dbid_given) {
319 if (id == dbmap->dbs[i].dbid) {
320 found = true;
321 break;
323 } else {
324 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
325 if (ret != 0) {
326 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
327 goto fail;
330 if (strcmp(name, dbarg) == 0) {
331 id = dbmap->dbs[i].dbid;
332 found = true;
333 break;
338 if (found && dbid_given && dbname != NULL) {
339 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
340 if (ret != 0) {
341 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
342 found = false;
343 goto fail;
347 if (found) {
348 if (dbid) *dbid = id;
349 if (dbname) *dbname = talloc_strdup(ctdb, name);
350 if (flags) *flags = dbmap->dbs[i].flags;
351 } else {
352 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
355 fail:
356 talloc_free(tmp_ctx);
357 return found;
361 see if a process exists
363 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
365 uint32_t pnn, pid;
366 int ret;
367 if (argc < 1) {
368 usage();
371 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
372 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
373 return -1;
376 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
377 if (ret == 0) {
378 printf("%u:%u exists\n", pnn, pid);
379 } else {
380 printf("%u:%u does not exist\n", pnn, pid);
382 return ret;
386 display statistics structure
388 static void show_statistics(struct ctdb_statistics *s, int show_header)
390 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
391 int i;
392 const char *prefix=NULL;
393 int preflen=0;
394 int tmp, days, hours, minutes, seconds;
395 const struct {
396 const char *name;
397 uint32_t offset;
398 } fields[] = {
399 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
400 STATISTICS_FIELD(num_clients),
401 STATISTICS_FIELD(frozen),
402 STATISTICS_FIELD(recovering),
403 STATISTICS_FIELD(num_recoveries),
404 STATISTICS_FIELD(client_packets_sent),
405 STATISTICS_FIELD(client_packets_recv),
406 STATISTICS_FIELD(node_packets_sent),
407 STATISTICS_FIELD(node_packets_recv),
408 STATISTICS_FIELD(keepalive_packets_sent),
409 STATISTICS_FIELD(keepalive_packets_recv),
410 STATISTICS_FIELD(node.req_call),
411 STATISTICS_FIELD(node.reply_call),
412 STATISTICS_FIELD(node.req_dmaster),
413 STATISTICS_FIELD(node.reply_dmaster),
414 STATISTICS_FIELD(node.reply_error),
415 STATISTICS_FIELD(node.req_message),
416 STATISTICS_FIELD(node.req_control),
417 STATISTICS_FIELD(node.reply_control),
418 STATISTICS_FIELD(client.req_call),
419 STATISTICS_FIELD(client.req_message),
420 STATISTICS_FIELD(client.req_control),
421 STATISTICS_FIELD(timeouts.call),
422 STATISTICS_FIELD(timeouts.control),
423 STATISTICS_FIELD(timeouts.traverse),
424 STATISTICS_FIELD(locks.num_calls),
425 STATISTICS_FIELD(locks.num_current),
426 STATISTICS_FIELD(locks.num_pending),
427 STATISTICS_FIELD(locks.num_failed),
428 STATISTICS_FIELD(total_calls),
429 STATISTICS_FIELD(pending_calls),
430 STATISTICS_FIELD(childwrite_calls),
431 STATISTICS_FIELD(pending_childwrite_calls),
432 STATISTICS_FIELD(memory_used),
433 STATISTICS_FIELD(max_hop_count),
434 STATISTICS_FIELD(total_ro_delegations),
435 STATISTICS_FIELD(total_ro_revokes),
438 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
439 seconds = tmp%60;
440 tmp /= 60;
441 minutes = tmp%60;
442 tmp /= 60;
443 hours = tmp%24;
444 tmp /= 24;
445 days = tmp;
447 if (options.machinereadable){
448 if (show_header) {
449 printf("CTDB version:");
450 printf("Current time of statistics:");
451 printf("Statistics collected since:");
452 for (i=0;i<ARRAY_SIZE(fields);i++) {
453 printf("%s:", fields[i].name);
455 printf("num_reclock_ctdbd_latency:");
456 printf("min_reclock_ctdbd_latency:");
457 printf("avg_reclock_ctdbd_latency:");
458 printf("max_reclock_ctdbd_latency:");
460 printf("num_reclock_recd_latency:");
461 printf("min_reclock_recd_latency:");
462 printf("avg_reclock_recd_latency:");
463 printf("max_reclock_recd_latency:");
465 printf("num_call_latency:");
466 printf("min_call_latency:");
467 printf("avg_call_latency:");
468 printf("max_call_latency:");
470 printf("num_lockwait_latency:");
471 printf("min_lockwait_latency:");
472 printf("avg_lockwait_latency:");
473 printf("max_lockwait_latency:");
475 printf("num_childwrite_latency:");
476 printf("min_childwrite_latency:");
477 printf("avg_childwrite_latency:");
478 printf("max_childwrite_latency:");
479 printf("\n");
481 printf("%d:", CTDB_VERSION);
482 printf("%d:", (int)s->statistics_current_time.tv_sec);
483 printf("%d:", (int)s->statistics_start_time.tv_sec);
484 for (i=0;i<ARRAY_SIZE(fields);i++) {
485 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
487 printf("%d:", s->reclock.ctdbd.num);
488 printf("%.6f:", s->reclock.ctdbd.min);
489 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
490 printf("%.6f:", s->reclock.ctdbd.max);
492 printf("%d:", s->reclock.recd.num);
493 printf("%.6f:", s->reclock.recd.min);
494 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
495 printf("%.6f:", s->reclock.recd.max);
497 printf("%d:", s->call_latency.num);
498 printf("%.6f:", s->call_latency.min);
499 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
500 printf("%.6f:", s->call_latency.max);
502 printf("%d:", s->childwrite_latency.num);
503 printf("%.6f:", s->childwrite_latency.min);
504 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
505 printf("%.6f:", s->childwrite_latency.max);
506 printf("\n");
507 } else {
508 printf("CTDB version %u\n", CTDB_VERSION);
509 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
510 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
512 for (i=0;i<ARRAY_SIZE(fields);i++) {
513 if (strchr(fields[i].name, '.')) {
514 preflen = strcspn(fields[i].name, ".")+1;
515 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
516 prefix = fields[i].name;
517 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
519 } else {
520 preflen = 0;
522 printf(" %*s%-22s%*s%10u\n",
523 preflen?4:0, "",
524 fields[i].name+preflen,
525 preflen?0:4, "",
526 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
528 printf(" hop_count_buckets:");
529 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
530 printf(" %d", s->hop_count_bucket[i]);
532 printf("\n");
533 printf(" lock_buckets:");
534 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
535 printf(" %d", s->locks.buckets[i]);
537 printf("\n");
538 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);
540 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);
542 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);
544 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);
545 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);
548 talloc_free(tmp_ctx);
552 display remote ctdb statistics combined from all nodes
554 static int control_statistics_all(struct ctdb_context *ctdb)
556 int ret, i;
557 struct ctdb_statistics statistics;
558 uint32_t *nodes;
559 uint32_t num_nodes;
561 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
562 CTDB_NO_MEMORY(ctdb, nodes);
564 ZERO_STRUCT(statistics);
566 for (i=0;i<num_nodes;i++) {
567 struct ctdb_statistics s1;
568 int j;
569 uint32_t *v1 = (uint32_t *)&s1;
570 uint32_t *v2 = (uint32_t *)&statistics;
571 uint32_t num_ints =
572 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
573 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
574 if (ret != 0) {
575 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
576 return ret;
578 for (j=0;j<num_ints;j++) {
579 v2[j] += v1[j];
581 statistics.max_hop_count =
582 MAX(statistics.max_hop_count, s1.max_hop_count);
583 statistics.call_latency.max =
584 MAX(statistics.call_latency.max, s1.call_latency.max);
586 talloc_free(nodes);
587 printf("Gathered statistics for %u nodes\n", num_nodes);
588 show_statistics(&statistics, 1);
589 return 0;
593 display remote ctdb statistics
595 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
597 int ret;
598 struct ctdb_statistics statistics;
600 if (options.pnn == CTDB_BROADCAST_ALL) {
601 return control_statistics_all(ctdb);
604 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
605 if (ret != 0) {
606 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
607 return ret;
609 show_statistics(&statistics, 1);
610 return 0;
615 reset remote ctdb statistics
617 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
619 int ret;
621 ret = ctdb_statistics_reset(ctdb, options.pnn);
622 if (ret != 0) {
623 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
624 return ret;
626 return 0;
631 display remote ctdb rolling statistics
633 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
635 int ret;
636 struct ctdb_statistics_wire *stats;
637 int i, num_records = -1;
639 assert_single_node_only();
641 if (argc ==1) {
642 num_records = atoi(argv[0]) - 1;
645 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
646 if (ret != 0) {
647 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
648 return ret;
650 for (i=0;i<stats->num;i++) {
651 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
652 continue;
654 show_statistics(&stats->stats[i], i==0);
655 if (i == num_records) {
656 break;
659 return 0;
664 display remote ctdb db statistics
666 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
668 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
669 struct ctdb_db_statistics *dbstat;
670 int i;
671 uint32_t db_id;
672 int num_hot_keys;
673 int ret;
675 if (argc < 1) {
676 usage();
679 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
680 return -1;
683 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
684 if (ret != 0) {
685 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
686 talloc_free(tmp_ctx);
687 return -1;
690 printf("DB Statistics: %s\n", argv[0]);
691 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
692 dbstat->db_ro_delegations);
693 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
694 dbstat->db_ro_delegations);
695 printf(" %s\n", "locks");
696 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
697 dbstat->locks.num_calls);
698 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
699 dbstat->locks.num_failed);
700 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
701 dbstat->locks.num_current);
702 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
703 dbstat->locks.num_pending);
704 printf(" %s", "hop_count_buckets:");
705 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
706 printf(" %d", dbstat->hop_count_bucket[i]);
708 printf("\n");
709 printf(" %s", "lock_buckets:");
710 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
711 printf(" %d", dbstat->locks.buckets[i]);
713 printf("\n");
714 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
715 "locks_latency MIN/AVG/MAX",
716 dbstat->locks.latency.min,
717 (dbstat->locks.latency.num ?
718 dbstat->locks.latency.total /dbstat->locks.latency.num :
719 0.0),
720 dbstat->locks.latency.max,
721 dbstat->locks.latency.num);
722 num_hot_keys = 0;
723 for (i=0; i<dbstat->num_hot_keys; i++) {
724 if (dbstat->hot_keys[i].count > 0) {
725 num_hot_keys++;
728 dbstat->num_hot_keys = num_hot_keys;
730 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
731 for (i = 0; i < dbstat->num_hot_keys; i++) {
732 int j;
733 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
734 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
735 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
737 printf("\n");
740 talloc_free(tmp_ctx);
741 return 0;
745 display uptime of remote node
747 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
749 int ret;
750 struct ctdb_uptime *uptime = NULL;
751 int tmp, days, hours, minutes, seconds;
753 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
754 if (ret != 0) {
755 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
756 return ret;
759 if (options.machinereadable){
760 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
761 printf(":%u:%u:%u:%lf\n",
762 (unsigned int)uptime->current_time.tv_sec,
763 (unsigned int)uptime->ctdbd_start_time.tv_sec,
764 (unsigned int)uptime->last_recovery_finished.tv_sec,
765 timeval_delta(&uptime->last_recovery_finished,
766 &uptime->last_recovery_started)
768 return 0;
771 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
773 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
774 seconds = tmp%60;
775 tmp /= 60;
776 minutes = tmp%60;
777 tmp /= 60;
778 hours = tmp%24;
779 tmp /= 24;
780 days = tmp;
781 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
783 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
784 seconds = tmp%60;
785 tmp /= 60;
786 minutes = tmp%60;
787 tmp /= 60;
788 hours = tmp%24;
789 tmp /= 24;
790 days = tmp;
791 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
793 printf("Duration of last recovery/failover: %lf seconds\n",
794 timeval_delta(&uptime->last_recovery_finished,
795 &uptime->last_recovery_started));
797 return 0;
801 show the PNN of the current node
803 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
805 uint32_t mypnn;
807 mypnn = getpnn(ctdb);
809 printf("PNN:%d\n", mypnn);
810 return 0;
814 struct pnn_node {
815 struct pnn_node *next, *prev;
816 ctdb_sock_addr addr;
817 int pnn;
820 static struct pnn_node *read_pnn_node_file(TALLOC_CTX *mem_ctx,
821 const char *file)
823 int nlines;
824 char **lines;
825 int i, pnn;
826 struct pnn_node *pnn_nodes = NULL;
827 struct pnn_node *pnn_node;
829 lines = file_lines_load(file, &nlines, mem_ctx);
830 if (lines == NULL) {
831 return NULL;
833 for (i=0, pnn=0; i<nlines; i++) {
834 char *node;
836 node = lines[i];
837 /* strip leading spaces */
838 while((*node == ' ') || (*node == '\t')) {
839 node++;
841 if (*node == '#') {
842 pnn++;
843 continue;
845 if (strcmp(node, "") == 0) {
846 continue;
848 pnn_node = talloc(mem_ctx, struct pnn_node);
849 pnn_node->pnn = pnn++;
851 if (!parse_ip(node, NULL, 0, &pnn_node->addr)) {
852 DEBUG(DEBUG_ERR,
853 ("Invalid IP address '%s' in file %s\n",
854 node, file));
855 /* Caller will free mem_ctx */
856 return NULL;
859 DLIST_ADD_END(pnn_nodes, pnn_node, NULL);
862 return pnn_nodes;
865 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
867 const char *nodes_list;
869 /* read the nodes file */
870 nodes_list = getenv("CTDB_NODES");
871 if (nodes_list == NULL) {
872 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
873 getenv("CTDB_BASE"));
874 if (nodes_list == NULL) {
875 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
876 exit(1);
880 return read_pnn_node_file(mem_ctx, nodes_list);
884 show the PNN of the current node
885 discover the pnn by loading the nodes file and try to bind to all
886 addresses one at a time until the ip address is found.
888 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
890 TALLOC_CTX *mem_ctx = talloc_new(NULL);
891 struct pnn_node *pnn_nodes;
892 struct pnn_node *pnn_node;
894 assert_single_node_only();
896 pnn_nodes = read_nodes_file(mem_ctx);
897 if (pnn_nodes == NULL) {
898 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
899 talloc_free(mem_ctx);
900 return -1;
903 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
904 if (ctdb_sys_have_ip(&pnn_node->addr)) {
905 printf("PNN:%d\n", pnn_node->pnn);
906 talloc_free(mem_ctx);
907 return 0;
911 printf("Failed to detect which PNN this node is\n");
912 talloc_free(mem_ctx);
913 return -1;
916 /* Helpers for ctdb status
918 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
920 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
921 int j;
922 bool ret = false;
924 if (node->flags == 0) {
925 struct ctdb_control_get_ifaces *ifaces;
927 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
928 tmp_ctx, &ifaces) == 0) {
929 for (j=0; j < ifaces->num; j++) {
930 if (ifaces->ifaces[j].link_state != 0) {
931 continue;
933 ret = true;
934 break;
938 talloc_free(tmp_ctx);
940 return ret;
943 static void control_status_header_machine(void)
945 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
946 ":Inactive:PartiallyOnline:ThisNode:\n");
949 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
950 struct ctdb_node_and_flags *node)
952 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
953 ctdb_addr_to_str(&node->addr),
954 !!(node->flags&NODE_FLAGS_DISCONNECTED),
955 !!(node->flags&NODE_FLAGS_BANNED),
956 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
957 !!(node->flags&NODE_FLAGS_UNHEALTHY),
958 !!(node->flags&NODE_FLAGS_STOPPED),
959 !!(node->flags&NODE_FLAGS_INACTIVE),
960 is_partially_online(ctdb, node) ? 1 : 0,
961 (node->pnn == mypnn)?'Y':'N');
963 return node->flags;
966 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
967 struct ctdb_node_and_flags *node)
969 printf("pnn:%d %-16s %s%s\n", node->pnn,
970 ctdb_addr_to_str(&node->addr),
971 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
972 node->pnn == mypnn?" (THIS NODE)":"");
974 return node->flags;
978 display remote ctdb status
980 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
982 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
983 int i;
984 struct ctdb_vnn_map *vnnmap=NULL;
985 struct ctdb_node_map *nodemap=NULL;
986 uint32_t recmode, recmaster, mypnn;
987 int num_deleted_nodes = 0;
988 int ret;
990 mypnn = getpnn(ctdb);
992 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
993 if (ret != 0) {
994 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
995 talloc_free(tmp_ctx);
996 return -1;
999 if (options.machinereadable) {
1000 control_status_header_machine();
1001 for (i=0;i<nodemap->num;i++) {
1002 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1003 continue;
1005 (void) control_status_1_machine(ctdb, mypnn,
1006 &nodemap->nodes[i]);
1008 talloc_free(tmp_ctx);
1009 return 0;
1012 for (i=0; i<nodemap->num; i++) {
1013 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1014 num_deleted_nodes++;
1017 if (num_deleted_nodes == 0) {
1018 printf("Number of nodes:%d\n", nodemap->num);
1019 } else {
1020 printf("Number of nodes:%d (including %d deleted nodes)\n",
1021 nodemap->num, num_deleted_nodes);
1023 for(i=0;i<nodemap->num;i++){
1024 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1025 continue;
1027 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1030 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1031 if (ret != 0) {
1032 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1033 talloc_free(tmp_ctx);
1034 return -1;
1036 if (vnnmap->generation == INVALID_GENERATION) {
1037 printf("Generation:INVALID\n");
1038 } else {
1039 printf("Generation:%d\n",vnnmap->generation);
1041 printf("Size:%d\n",vnnmap->size);
1042 for(i=0;i<vnnmap->size;i++){
1043 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1046 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1047 if (ret != 0) {
1048 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1049 talloc_free(tmp_ctx);
1050 return -1;
1052 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1054 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1055 if (ret != 0) {
1056 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1057 talloc_free(tmp_ctx);
1058 return -1;
1060 printf("Recovery master:%d\n",recmaster);
1062 talloc_free(tmp_ctx);
1063 return 0;
1066 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1068 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1069 int i, ret;
1070 struct ctdb_node_map *nodemap=NULL;
1071 uint32_t * nodes;
1072 uint32_t pnn_mode, mypnn;
1074 if (argc > 1) {
1075 usage();
1078 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1079 options.pnn, true, &nodes, &pnn_mode)) {
1080 return -1;
1083 if (options.machinereadable) {
1084 control_status_header_machine();
1085 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1086 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1089 mypnn = getpnn(ctdb);
1091 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1092 if (ret != 0) {
1093 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1094 talloc_free(tmp_ctx);
1095 return -1;
1098 ret = 0;
1100 for (i = 0; i < talloc_array_length(nodes); i++) {
1101 if (options.machinereadable) {
1102 ret |= control_status_1_machine(ctdb, mypnn,
1103 &nodemap->nodes[nodes[i]]);
1104 } else {
1105 ret |= control_status_1_human(ctdb, mypnn,
1106 &nodemap->nodes[nodes[i]]);
1110 talloc_free(tmp_ctx);
1111 return ret;
1114 static struct pnn_node *read_natgw_nodes_file(struct ctdb_context *ctdb,
1115 TALLOC_CTX *mem_ctx)
1117 const char *natgw_list;
1118 struct pnn_node *natgw_nodes = NULL;
1120 natgw_list = getenv("CTDB_NATGW_NODES");
1121 if (natgw_list == NULL) {
1122 natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
1123 getenv("CTDB_BASE"));
1124 if (natgw_list == NULL) {
1125 DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1126 exit(1);
1129 /* The PNNs will be junk but they're not used */
1130 natgw_nodes = read_pnn_node_file(mem_ctx, natgw_list);
1131 if (natgw_nodes == NULL) {
1132 DEBUG(DEBUG_ERR,
1133 ("Failed to load natgw node list '%s'\n", natgw_list));
1135 return natgw_nodes;
1139 /* talloc off the existing nodemap... */
1140 static struct ctdb_node_map *talloc_nodemap(struct ctdb_node_map *nodemap)
1142 return talloc_zero_size(nodemap,
1143 offsetof(struct ctdb_node_map, nodes) +
1144 nodemap->num * sizeof(struct ctdb_node_and_flags));
1147 static struct ctdb_node_map *
1148 filter_nodemap_by_addrs(struct ctdb_context *ctdb,
1149 struct ctdb_node_map *nodemap,
1150 struct pnn_node *nodes)
1152 int i;
1153 struct pnn_node *n;
1154 struct ctdb_node_map *ret;
1156 ret = talloc_nodemap(nodemap);
1157 CTDB_NO_MEMORY_NULL(ctdb, ret);
1159 ret->num = 0;
1161 for (i = 0; i < nodemap->num; i++) {
1162 for(n = nodes; n != NULL ; n = n->next) {
1163 if (ctdb_same_ip(&n->addr,
1164 &nodemap->nodes[i].addr)) {
1165 break;
1168 if (n == NULL) {
1169 continue;
1172 ret->nodes[ret->num] = nodemap->nodes[i];
1173 ret->num++;
1176 return ret;
1179 static struct ctdb_node_map *
1180 filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
1181 struct ctdb_node_map *nodemap,
1182 uint32_t required_capabilities,
1183 bool first_only)
1185 int i;
1186 uint32_t capabilities;
1187 struct ctdb_node_map *ret;
1189 ret = talloc_nodemap(nodemap);
1190 CTDB_NO_MEMORY_NULL(ctdb, ret);
1192 ret->num = 0;
1194 for (i = 0; i < nodemap->num; i++) {
1195 int res;
1197 /* Disconnected nodes have no capabilities! */
1198 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1199 continue;
1202 res = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1203 nodemap->nodes[i].pnn,
1204 &capabilities);
1205 if (res != 0) {
1206 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1207 nodemap->nodes[i].pnn));
1208 talloc_free(ret);
1209 return NULL;
1211 if (!(capabilities & required_capabilities)) {
1212 continue;
1215 ret->nodes[ret->num] = nodemap->nodes[i];
1216 ret->num++;
1217 if (first_only) {
1218 break;
1222 return ret;
1225 static struct ctdb_node_map *
1226 filter_nodemap_by_flags(struct ctdb_context *ctdb,
1227 struct ctdb_node_map *nodemap,
1228 uint32_t flags_mask)
1230 int i;
1231 struct ctdb_node_map *ret;
1233 ret = talloc_nodemap(nodemap);
1234 CTDB_NO_MEMORY_NULL(ctdb, ret);
1236 ret->num = 0;
1238 for (i = 0; i < nodemap->num; i++) {
1239 if (nodemap->nodes[i].flags & flags_mask) {
1240 continue;
1243 ret->nodes[ret->num] = nodemap->nodes[i];
1244 ret->num++;
1247 return ret;
1251 display the list of nodes belonging to this natgw configuration
1253 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1255 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1256 int i, ret;
1257 struct pnn_node *natgw_nodes = NULL;
1258 struct ctdb_node_map *orig_nodemap=NULL;
1259 struct ctdb_node_map *nodemap;
1260 uint32_t mypnn, pnn;
1261 const char *ip;
1263 /* When we have some nodes that could be the NATGW, make a
1264 * series of attempts to find the first node that doesn't have
1265 * certain status flags set.
1267 uint32_t exclude_flags[] = {
1268 /* Look for a nice healthy node */
1269 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1270 /* If not found, an UNHEALTHY/BANNED node will do */
1271 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1272 /* If not found, a STOPPED node will do */
1273 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1277 /* read the natgw nodes file into a linked list */
1278 natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
1279 if (natgw_nodes == NULL) {
1280 ret = -1;
1281 goto done;
1284 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
1285 tmp_ctx, &orig_nodemap);
1286 if (ret != 0) {
1287 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1288 talloc_free(tmp_ctx);
1289 return -1;
1292 /* Get a nodemap that includes only the nodes in the NATGW
1293 * group */
1294 nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
1295 if (nodemap == NULL) {
1296 ret = -1;
1297 goto done;
1300 ret = 2; /* matches ENOENT */
1301 pnn = -1;
1302 ip = "0.0.0.0";
1303 /* For each flag mask... */
1304 for (i = 0; exclude_flags[i] != 0; i++) {
1305 /* ... get a nodemap that excludes nodes with with
1306 * masked flags... */
1307 struct ctdb_node_map *t =
1308 filter_nodemap_by_flags(ctdb, nodemap,
1309 exclude_flags[i]);
1310 if (t == NULL) {
1311 /* No memory */
1312 ret = -1;
1313 goto done;
1315 if (t->num > 0) {
1316 /* ... and find the first node with the NATGW
1317 * capability */
1318 struct ctdb_node_map *n;
1319 n = filter_nodemap_by_capabilities(ctdb, t,
1320 CTDB_CAP_NATGW,
1321 true);
1322 if (n == NULL) {
1323 /* No memory */
1324 ret = -1;
1325 goto done;
1327 if (n->num > 0) {
1328 ret = 0;
1329 pnn = n->nodes[0].pnn;
1330 ip = ctdb_addr_to_str(&n->nodes[0].addr);
1331 break;
1334 talloc_free(t);
1337 if (options.machinereadable) {
1338 printf(":Node:IP:\n");
1339 printf(":%d:%s:\n", pnn, ip);
1340 } else {
1341 printf("%d %s\n", pnn, ip);
1344 /* print the pruned list of nodes belonging to this natgw list */
1345 mypnn = getpnn(ctdb);
1346 if (options.machinereadable) {
1347 control_status_header_machine();
1348 } else {
1349 printf("Number of nodes:%d\n", nodemap->num);
1351 for(i=0;i<nodemap->num;i++){
1352 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1353 continue;
1355 if (options.machinereadable) {
1356 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1357 } else {
1358 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1362 done:
1363 talloc_free(tmp_ctx);
1364 return ret;
1368 display the status of the scripts for monitoring (or other events)
1370 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1371 enum ctdb_eventscript_call type)
1373 struct ctdb_scripts_wire *script_status;
1374 int ret, i;
1376 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1377 if (ret != 0) {
1378 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1379 return ret;
1382 if (script_status == NULL) {
1383 if (!options.machinereadable) {
1384 printf("%s cycle never run\n",
1385 ctdb_eventscript_call_names[type]);
1387 return 0;
1390 if (!options.machinereadable) {
1391 int num_run = 0;
1392 for (i=0; i<script_status->num_scripts; i++) {
1393 if (script_status->scripts[i].status != -ENOEXEC) {
1394 num_run++;
1397 printf("%d scripts were executed last %s cycle\n",
1398 num_run,
1399 ctdb_eventscript_call_names[type]);
1401 for (i=0; i<script_status->num_scripts; i++) {
1402 const char *status = NULL;
1404 switch (script_status->scripts[i].status) {
1405 case -ETIME:
1406 status = "TIMEDOUT";
1407 break;
1408 case -ENOEXEC:
1409 status = "DISABLED";
1410 break;
1411 case 0:
1412 status = "OK";
1413 break;
1414 default:
1415 if (script_status->scripts[i].status > 0)
1416 status = "ERROR";
1417 break;
1419 if (options.machinereadable) {
1420 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1421 ctdb_eventscript_call_names[type],
1422 script_status->scripts[i].name,
1423 script_status->scripts[i].status,
1424 status,
1425 (long)script_status->scripts[i].start.tv_sec,
1426 (long)script_status->scripts[i].start.tv_usec,
1427 (long)script_status->scripts[i].finished.tv_sec,
1428 (long)script_status->scripts[i].finished.tv_usec,
1429 script_status->scripts[i].output);
1430 continue;
1432 if (status)
1433 printf("%-20s Status:%s ",
1434 script_status->scripts[i].name, status);
1435 else
1436 /* Some other error, eg from stat. */
1437 printf("%-20s Status:CANNOT RUN (%s)",
1438 script_status->scripts[i].name,
1439 strerror(-script_status->scripts[i].status));
1441 if (script_status->scripts[i].status >= 0) {
1442 printf("Duration:%.3lf ",
1443 timeval_delta(&script_status->scripts[i].finished,
1444 &script_status->scripts[i].start));
1446 if (script_status->scripts[i].status != -ENOEXEC) {
1447 printf("%s",
1448 ctime(&script_status->scripts[i].start.tv_sec));
1449 if (script_status->scripts[i].status != 0) {
1450 printf(" OUTPUT:%s\n",
1451 script_status->scripts[i].output);
1453 } else {
1454 printf("\n");
1457 return 0;
1461 static int control_scriptstatus(struct ctdb_context *ctdb,
1462 int argc, const char **argv)
1464 int ret;
1465 enum ctdb_eventscript_call type, min, max;
1466 const char *arg;
1468 if (argc > 1) {
1469 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1470 return -1;
1473 if (argc == 0)
1474 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1475 else
1476 arg = argv[0];
1478 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1479 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1480 min = type;
1481 max = type+1;
1482 break;
1485 if (type == CTDB_EVENT_MAX) {
1486 if (strcmp(arg, "all") == 0) {
1487 min = 0;
1488 max = CTDB_EVENT_MAX;
1489 } else {
1490 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1491 return -1;
1495 if (options.machinereadable) {
1496 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1499 for (type = min; type < max; type++) {
1500 ret = control_one_scriptstatus(ctdb, type);
1501 if (ret != 0) {
1502 return ret;
1506 return 0;
1510 enable an eventscript
1512 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1514 int ret;
1516 if (argc < 1) {
1517 usage();
1520 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1521 if (ret != 0) {
1522 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1523 return ret;
1526 return 0;
1530 disable an eventscript
1532 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1534 int ret;
1536 if (argc < 1) {
1537 usage();
1540 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1541 if (ret != 0) {
1542 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1543 return ret;
1546 return 0;
1550 display the pnn of the recovery master
1552 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1554 uint32_t recmaster;
1555 int ret;
1557 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1558 if (ret != 0) {
1559 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1560 return -1;
1562 printf("%d\n",recmaster);
1564 return 0;
1568 add a tickle to a public address
1570 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1572 struct ctdb_tcp_connection t;
1573 TDB_DATA data;
1574 int ret;
1576 assert_single_node_only();
1578 if (argc < 2) {
1579 usage();
1582 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1583 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1584 return -1;
1586 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1587 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1588 return -1;
1591 data.dptr = (uint8_t *)&t;
1592 data.dsize = sizeof(t);
1594 /* tell all nodes about this tcp connection */
1595 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1596 0, data, ctdb, NULL, NULL, NULL, NULL);
1597 if (ret != 0) {
1598 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1599 return -1;
1602 return 0;
1607 delete a tickle from a node
1609 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1611 struct ctdb_tcp_connection t;
1612 TDB_DATA data;
1613 int ret;
1615 assert_single_node_only();
1617 if (argc < 2) {
1618 usage();
1621 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1622 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1623 return -1;
1625 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1626 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1627 return -1;
1630 data.dptr = (uint8_t *)&t;
1631 data.dsize = sizeof(t);
1633 /* tell all nodes about this tcp connection */
1634 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1635 0, data, ctdb, NULL, NULL, NULL, NULL);
1636 if (ret != 0) {
1637 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1638 return -1;
1641 return 0;
1646 get a list of all tickles for this pnn
1648 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1650 struct ctdb_control_tcp_tickle_list *list;
1651 ctdb_sock_addr addr;
1652 int i, ret;
1653 unsigned port = 0;
1655 assert_single_node_only();
1657 if (argc < 1) {
1658 usage();
1661 if (argc == 2) {
1662 port = atoi(argv[1]);
1665 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1666 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1667 return -1;
1670 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1671 if (ret == -1) {
1672 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1673 return -1;
1676 if (options.machinereadable){
1677 printf(":source ip:port:destination ip:port:\n");
1678 for (i=0;i<list->tickles.num;i++) {
1679 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1680 continue;
1682 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1683 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1685 } else {
1686 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1687 printf("Num tickles:%u\n", list->tickles.num);
1688 for (i=0;i<list->tickles.num;i++) {
1689 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1690 continue;
1692 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1693 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));
1697 talloc_free(list);
1699 return 0;
1703 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1705 struct ctdb_all_public_ips *ips;
1706 struct ctdb_public_ip ip;
1707 int i, ret;
1708 uint32_t *nodes;
1709 uint32_t disable_time;
1710 TDB_DATA data;
1711 struct ctdb_node_map *nodemap=NULL;
1712 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1714 disable_time = 30;
1715 data.dptr = (uint8_t*)&disable_time;
1716 data.dsize = sizeof(disable_time);
1717 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1718 if (ret != 0) {
1719 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1720 return -1;
1725 /* read the public ip list from the node */
1726 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1727 if (ret != 0) {
1728 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1729 talloc_free(tmp_ctx);
1730 return -1;
1733 for (i=0;i<ips->num;i++) {
1734 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1735 break;
1738 if (i==ips->num) {
1739 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1740 pnn, ctdb_addr_to_str(addr)));
1741 talloc_free(tmp_ctx);
1742 return -1;
1745 ip.pnn = pnn;
1746 ip.addr = *addr;
1748 data.dptr = (uint8_t *)&ip;
1749 data.dsize = sizeof(ip);
1751 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1752 if (ret != 0) {
1753 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1754 talloc_free(tmp_ctx);
1755 return ret;
1758 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1759 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1760 nodes, 0,
1761 LONGTIMELIMIT(),
1762 false, data,
1763 NULL, NULL,
1764 NULL);
1765 if (ret != 0) {
1766 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1767 talloc_free(tmp_ctx);
1768 return -1;
1771 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1772 if (ret != 0) {
1773 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1774 talloc_free(tmp_ctx);
1775 return -1;
1778 /* update the recovery daemon so it now knows to expect the new
1779 node assignment for this ip.
1781 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1782 if (ret != 0) {
1783 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1784 return -1;
1787 talloc_free(tmp_ctx);
1788 return 0;
1793 * scans all other nodes and returns a pnn for another node that can host this
1794 * ip address or -1
1796 static int
1797 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1799 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1800 struct ctdb_all_public_ips *ips;
1801 struct ctdb_node_map *nodemap=NULL;
1802 int i, j, ret;
1804 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1805 if (ret != 0) {
1806 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1807 talloc_free(tmp_ctx);
1808 return ret;
1811 for(i=0;i<nodemap->num;i++){
1812 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1813 continue;
1815 if (nodemap->nodes[i].pnn == options.pnn) {
1816 continue;
1819 /* read the public ip list from this node */
1820 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1821 if (ret != 0) {
1822 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1823 return -1;
1826 for (j=0;j<ips->num;j++) {
1827 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1828 talloc_free(tmp_ctx);
1829 return nodemap->nodes[i].pnn;
1832 talloc_free(ips);
1835 talloc_free(tmp_ctx);
1836 return -1;
1839 /* If pnn is -1 then try to find a node to move IP to... */
1840 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1842 bool pnn_specified = (pnn == -1 ? false : true);
1843 int retries = 0;
1845 while (retries < 5) {
1846 if (!pnn_specified) {
1847 pnn = find_other_host_for_public_ip(ctdb, addr);
1848 if (pnn == -1) {
1849 return false;
1851 DEBUG(DEBUG_NOTICE,
1852 ("Trying to move public IP to node %u\n", pnn));
1855 if (move_ip(ctdb, addr, pnn) == 0) {
1856 return true;
1859 sleep(3);
1860 retries++;
1863 return false;
1868 move/failover an ip address to a specific node
1870 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1872 uint32_t pnn;
1873 ctdb_sock_addr addr;
1875 assert_single_node_only();
1877 if (argc < 2) {
1878 usage();
1879 return -1;
1882 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1883 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1884 return -1;
1888 if (sscanf(argv[1], "%u", &pnn) != 1) {
1889 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1890 return -1;
1893 if (!try_moveip(ctdb, &addr, pnn)) {
1894 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1895 return -1;
1898 return 0;
1901 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1903 TDB_DATA data;
1905 data.dptr = (uint8_t *)&pnn;
1906 data.dsize = sizeof(uint32_t);
1907 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1908 DEBUG(DEBUG_ERR,
1909 ("Failed to send message to force node %u to be a rebalancing target\n",
1910 pnn));
1911 return -1;
1914 return 0;
1919 rebalance a node by setting it to allow failback and triggering a
1920 takeover run
1922 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1924 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1925 uint32_t *nodes;
1926 uint32_t pnn_mode;
1927 int i, ret;
1929 assert_single_node_only();
1931 if (argc > 1) {
1932 usage();
1935 /* Determine the nodes where IPs need to be reloaded */
1936 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1937 options.pnn, true, &nodes, &pnn_mode)) {
1938 ret = -1;
1939 goto done;
1942 for (i = 0; i < talloc_array_length(nodes); i++) {
1943 if (!rebalance_node(ctdb, nodes[i])) {
1944 ret = -1;
1948 done:
1949 talloc_free(tmp_ctx);
1950 return ret;
1953 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1955 struct ctdb_public_ip ip;
1956 int ret;
1957 uint32_t *nodes;
1958 uint32_t disable_time;
1959 TDB_DATA data;
1960 struct ctdb_node_map *nodemap=NULL;
1961 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1963 disable_time = 30;
1964 data.dptr = (uint8_t*)&disable_time;
1965 data.dsize = sizeof(disable_time);
1966 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1967 if (ret != 0) {
1968 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1969 return -1;
1972 ip.pnn = -1;
1973 ip.addr = *addr;
1975 data.dptr = (uint8_t *)&ip;
1976 data.dsize = sizeof(ip);
1978 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1979 if (ret != 0) {
1980 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1981 talloc_free(tmp_ctx);
1982 return ret;
1985 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1986 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1987 nodes, 0,
1988 LONGTIMELIMIT(),
1989 false, data,
1990 NULL, NULL,
1991 NULL);
1992 if (ret != 0) {
1993 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1994 talloc_free(tmp_ctx);
1995 return -1;
1998 talloc_free(tmp_ctx);
1999 return 0;
2003 release an ip form all nodes and have it re-assigned by recd
2005 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
2007 ctdb_sock_addr addr;
2009 assert_single_node_only();
2011 if (argc < 1) {
2012 usage();
2013 return -1;
2016 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2017 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2018 return -1;
2021 if (rebalance_ip(ctdb, &addr) != 0) {
2022 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
2023 return -1;
2026 return 0;
2029 static int getips_store_callback(void *param, void *data)
2031 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
2032 struct ctdb_all_public_ips *ips = param;
2033 int i;
2035 i = ips->num++;
2036 ips->ips[i].pnn = node_ip->pnn;
2037 ips->ips[i].addr = node_ip->addr;
2038 return 0;
2041 static int getips_count_callback(void *param, void *data)
2043 uint32_t *count = param;
2045 (*count)++;
2046 return 0;
2049 #define IP_KEYLEN 4
2050 static uint32_t *ip_key(ctdb_sock_addr *ip)
2052 static uint32_t key[IP_KEYLEN];
2054 bzero(key, sizeof(key));
2056 switch (ip->sa.sa_family) {
2057 case AF_INET:
2058 key[0] = ip->ip.sin_addr.s_addr;
2059 break;
2060 case AF_INET6: {
2061 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
2062 key[0] = s6_a32[3];
2063 key[1] = s6_a32[2];
2064 key[2] = s6_a32[1];
2065 key[3] = s6_a32[0];
2066 break;
2068 default:
2069 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
2070 return key;
2073 return key;
2076 static void *add_ip_callback(void *parm, void *data)
2078 return parm;
2081 static int
2082 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
2084 struct ctdb_all_public_ips *tmp_ips;
2085 struct ctdb_node_map *nodemap=NULL;
2086 trbt_tree_t *ip_tree;
2087 int i, j, len, ret;
2088 uint32_t count;
2090 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2091 if (ret != 0) {
2092 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2093 return ret;
2096 ip_tree = trbt_create(tmp_ctx, 0);
2098 for(i=0;i<nodemap->num;i++){
2099 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2100 continue;
2102 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2103 continue;
2106 /* read the public ip list from this node */
2107 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2108 if (ret != 0) {
2109 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2110 return -1;
2113 for (j=0; j<tmp_ips->num;j++) {
2114 struct ctdb_public_ip *node_ip;
2116 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2117 node_ip->pnn = tmp_ips->ips[j].pnn;
2118 node_ip->addr = tmp_ips->ips[j].addr;
2120 trbt_insertarray32_callback(ip_tree,
2121 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2122 add_ip_callback,
2123 node_ip);
2125 talloc_free(tmp_ips);
2128 /* traverse */
2129 count = 0;
2130 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2132 len = offsetof(struct ctdb_all_public_ips, ips) +
2133 count*sizeof(struct ctdb_public_ip);
2134 tmp_ips = talloc_zero_size(tmp_ctx, len);
2135 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2137 *ips = tmp_ips;
2139 return 0;
2143 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2145 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2147 event_add_timed(ctdb->ev, ctdb,
2148 timeval_current_ofs(1, 0),
2149 ctdb_every_second, ctdb);
2152 struct srvid_reply_handler_data {
2153 bool done;
2154 bool wait_for_all;
2155 uint32_t *nodes;
2156 const char *srvid_str;
2159 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2160 uint64_t srvid,
2161 TDB_DATA data,
2162 void *private_data)
2164 struct srvid_reply_handler_data *d =
2165 (struct srvid_reply_handler_data *)private_data;
2166 int i;
2167 int32_t ret;
2169 if (data.dsize != sizeof(ret)) {
2170 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2171 return;
2174 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2175 ret = *(int32_t *)data.dptr;
2176 if (ret < 0) {
2177 DEBUG(DEBUG_ERR,
2178 ("%s failed with result %d\n", d->srvid_str, ret));
2179 return;
2182 if (!d->wait_for_all) {
2183 d->done = true;
2184 return;
2187 /* Wait for all replies */
2188 d->done = true;
2189 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2190 if (d->nodes[i] == ret) {
2191 DEBUG(DEBUG_INFO,
2192 ("%s reply received from node %u\n",
2193 d->srvid_str, ret));
2194 d->nodes[i] = -1;
2196 if (d->nodes[i] != -1) {
2197 /* Found a node that hasn't yet replied */
2198 d->done = false;
2203 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2204 * or replies from all connected nodes. arg is the data argument to
2205 * pass in the srvid_request structure - pass 0 if this isn't needed.
2207 static int srvid_broadcast(struct ctdb_context *ctdb,
2208 uint64_t srvid, uint32_t *arg,
2209 const char *srvid_str, bool wait_for_all)
2211 int ret;
2212 TDB_DATA data;
2213 uint32_t pnn;
2214 uint64_t reply_srvid;
2215 struct srvid_request request;
2216 struct srvid_request_data request_data;
2217 struct srvid_reply_handler_data reply_data;
2218 struct timeval tv;
2220 ZERO_STRUCT(request);
2222 /* Time ticks to enable timeouts to be processed */
2223 event_add_timed(ctdb->ev, ctdb,
2224 timeval_current_ofs(1, 0),
2225 ctdb_every_second, ctdb);
2227 pnn = ctdb_get_pnn(ctdb);
2228 reply_srvid = getpid();
2230 if (arg == NULL) {
2231 request.pnn = pnn;
2232 request.srvid = reply_srvid;
2234 data.dptr = (uint8_t *)&request;
2235 data.dsize = sizeof(request);
2236 } else {
2237 request_data.pnn = pnn;
2238 request_data.srvid = reply_srvid;
2239 request_data.data = *arg;
2241 data.dptr = (uint8_t *)&request_data;
2242 data.dsize = sizeof(request_data);
2245 /* Register message port for reply from recovery master */
2246 ctdb_client_set_message_handler(ctdb, reply_srvid,
2247 srvid_broadcast_reply_handler,
2248 &reply_data);
2250 reply_data.wait_for_all = wait_for_all;
2251 reply_data.nodes = NULL;
2252 reply_data.srvid_str = srvid_str;
2254 again:
2255 reply_data.done = false;
2257 if (wait_for_all) {
2258 struct ctdb_node_map *nodemap;
2260 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2261 CTDB_CURRENT_NODE, ctdb, &nodemap);
2262 if (ret != 0) {
2263 DEBUG(DEBUG_ERR,
2264 ("Unable to get nodemap from current node, try again\n"));
2265 sleep(1);
2266 goto again;
2269 if (reply_data.nodes != NULL) {
2270 talloc_free(reply_data.nodes);
2272 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2273 NULL, true);
2275 talloc_free(nodemap);
2278 /* Send to all connected nodes. Only recmaster replies */
2279 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2280 srvid, data);
2281 if (ret != 0) {
2282 /* This can only happen if the socket is closed and
2283 * there's no way to recover from that, so don't try
2284 * again.
2286 DEBUG(DEBUG_ERR,
2287 ("Failed to send %s request to connected nodes\n",
2288 srvid_str));
2289 return -1;
2292 tv = timeval_current();
2293 /* This loop terminates the reply is received */
2294 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2295 event_loop_once(ctdb->ev);
2298 if (!reply_data.done) {
2299 DEBUG(DEBUG_NOTICE,
2300 ("Still waiting for confirmation of %s\n", srvid_str));
2301 sleep(1);
2302 goto again;
2305 ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
2307 talloc_free(reply_data.nodes);
2309 return 0;
2312 static int ipreallocate(struct ctdb_context *ctdb)
2314 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
2315 "IP reallocation", false);
2319 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2321 return ipreallocate(ctdb);
2325 add a public ip address to a node
2327 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2329 int i, ret;
2330 int len, retries = 0;
2331 unsigned mask;
2332 ctdb_sock_addr addr;
2333 struct ctdb_control_ip_iface *pub;
2334 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2335 struct ctdb_all_public_ips *ips;
2338 if (argc != 2) {
2339 talloc_free(tmp_ctx);
2340 usage();
2343 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2344 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2345 talloc_free(tmp_ctx);
2346 return -1;
2349 /* read the public ip list from the node */
2350 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2351 if (ret != 0) {
2352 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2353 talloc_free(tmp_ctx);
2354 return -1;
2356 for (i=0;i<ips->num;i++) {
2357 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2358 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2359 return 0;
2365 /* Dont timeout. This command waits for an ip reallocation
2366 which sometimes can take wuite a while if there has
2367 been a recent recovery
2369 alarm(0);
2371 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2372 pub = talloc_size(tmp_ctx, len);
2373 CTDB_NO_MEMORY(ctdb, pub);
2375 pub->addr = addr;
2376 pub->mask = mask;
2377 pub->len = strlen(argv[1])+1;
2378 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2380 do {
2381 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2382 if (ret != 0) {
2383 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2384 sleep(3);
2385 retries++;
2387 } while (retries < 5 && ret != 0);
2388 if (ret != 0) {
2389 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2390 talloc_free(tmp_ctx);
2391 return ret;
2394 if (rebalance_node(ctdb, options.pnn) != 0) {
2395 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2396 return ret;
2399 talloc_free(tmp_ctx);
2400 return 0;
2404 add a public ip address to a node
2406 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2408 ctdb_sock_addr addr;
2410 if (argc != 1) {
2411 usage();
2414 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2415 printf("Badly formed ip : %s\n", argv[0]);
2416 return -1;
2419 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2421 return 0;
2424 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2426 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2428 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2429 struct ctdb_node_map *nodemap=NULL;
2430 struct ctdb_all_public_ips *ips;
2431 int ret, i, j;
2433 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2434 if (ret != 0) {
2435 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2436 return ret;
2439 /* remove it from the nodes that are not hosting the ip currently */
2440 for(i=0;i<nodemap->num;i++){
2441 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2442 continue;
2444 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2445 if (ret != 0) {
2446 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2447 continue;
2450 for (j=0;j<ips->num;j++) {
2451 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2452 break;
2455 if (j==ips->num) {
2456 continue;
2459 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2460 continue;
2463 options.pnn = nodemap->nodes[i].pnn;
2464 control_delip(ctdb, argc, argv);
2468 /* remove it from every node (also the one hosting it) */
2469 for(i=0;i<nodemap->num;i++){
2470 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2471 continue;
2473 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2474 if (ret != 0) {
2475 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2476 continue;
2479 for (j=0;j<ips->num;j++) {
2480 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2481 break;
2484 if (j==ips->num) {
2485 continue;
2488 options.pnn = nodemap->nodes[i].pnn;
2489 control_delip(ctdb, argc, argv);
2492 talloc_free(tmp_ctx);
2493 return 0;
2497 delete a public ip address from a node
2499 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2501 int i, ret;
2502 ctdb_sock_addr addr;
2503 struct ctdb_control_ip_iface pub;
2504 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2505 struct ctdb_all_public_ips *ips;
2507 if (argc != 1) {
2508 talloc_free(tmp_ctx);
2509 usage();
2512 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2513 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2514 return -1;
2517 if (options.pnn == CTDB_BROADCAST_ALL) {
2518 return control_delip_all(ctdb, argc, argv, &addr);
2521 pub.addr = addr;
2522 pub.mask = 0;
2523 pub.len = 0;
2525 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2526 if (ret != 0) {
2527 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2528 talloc_free(tmp_ctx);
2529 return ret;
2532 for (i=0;i<ips->num;i++) {
2533 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2534 break;
2538 if (i==ips->num) {
2539 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2540 ctdb_addr_to_str(&addr)));
2541 talloc_free(tmp_ctx);
2542 return -1;
2545 /* This is an optimisation. If this node is hosting the IP
2546 * then try to move it somewhere else without invoking a full
2547 * takeover run. We don't care if this doesn't work!
2549 if (ips->ips[i].pnn == options.pnn) {
2550 (void) try_moveip(ctdb, &addr, -1);
2553 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2554 if (ret != 0) {
2555 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2556 talloc_free(tmp_ctx);
2557 return ret;
2560 talloc_free(tmp_ctx);
2561 return 0;
2564 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2565 int argc, const char **argv)
2567 struct ctdb_control_killtcp *killtcp;
2568 int max_entries, current, i;
2569 struct timeval timeout;
2570 char line[128], src[128], dst[128];
2571 int linenum;
2572 TDB_DATA data;
2573 struct client_async_data *async_data;
2574 struct ctdb_client_control_state *state;
2576 if (argc != 0) {
2577 usage();
2580 linenum = 1;
2581 killtcp = NULL;
2582 max_entries = 0;
2583 current = 0;
2584 while (!feof(stdin)) {
2585 if (fgets(line, sizeof(line), stdin) == NULL) {
2586 continue;
2589 /* Silently skip empty lines */
2590 if (line[0] == '\n') {
2591 continue;
2594 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2595 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2596 linenum, line));
2597 talloc_free(killtcp);
2598 return -1;
2601 if (current >= max_entries) {
2602 max_entries += 1024;
2603 killtcp = talloc_realloc(ctdb, killtcp,
2604 struct ctdb_control_killtcp,
2605 max_entries);
2606 CTDB_NO_MEMORY(ctdb, killtcp);
2609 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2610 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2611 linenum, src));
2612 talloc_free(killtcp);
2613 return -1;
2616 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2617 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2618 linenum, dst));
2619 talloc_free(killtcp);
2620 return -1;
2623 current++;
2626 async_data = talloc_zero(ctdb, struct client_async_data);
2627 if (async_data == NULL) {
2628 talloc_free(killtcp);
2629 return -1;
2632 for (i = 0; i < current; i++) {
2634 data.dsize = sizeof(struct ctdb_control_killtcp);
2635 data.dptr = (unsigned char *)&killtcp[i];
2637 timeout = TIMELIMIT();
2638 state = ctdb_control_send(ctdb, options.pnn, 0,
2639 CTDB_CONTROL_KILL_TCP, 0, data,
2640 async_data, &timeout, NULL);
2642 if (state == NULL) {
2643 DEBUG(DEBUG_ERR,
2644 ("Failed to call async killtcp control to node %u\n",
2645 options.pnn));
2646 talloc_free(killtcp);
2647 return -1;
2650 ctdb_client_async_add(async_data, state);
2653 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2654 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2655 talloc_free(killtcp);
2656 return -1;
2659 talloc_free(killtcp);
2660 return 0;
2665 kill a tcp connection
2667 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2669 int ret;
2670 struct ctdb_control_killtcp killtcp;
2672 assert_single_node_only();
2674 if (argc == 0) {
2675 return kill_tcp_from_file(ctdb, argc, argv);
2678 if (argc < 2) {
2679 usage();
2682 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2683 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2684 return -1;
2687 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2688 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2689 return -1;
2692 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2693 if (ret != 0) {
2694 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2695 return ret;
2698 return 0;
2703 send a gratious arp
2705 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2707 int ret;
2708 ctdb_sock_addr addr;
2710 assert_single_node_only();
2712 if (argc < 2) {
2713 usage();
2716 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2717 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2718 return -1;
2721 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2722 if (ret != 0) {
2723 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2724 return ret;
2727 return 0;
2731 register a server id
2733 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2735 int ret;
2736 struct ctdb_server_id server_id;
2738 if (argc < 3) {
2739 usage();
2742 server_id.pnn = strtoul(argv[0], NULL, 0);
2743 server_id.type = strtoul(argv[1], NULL, 0);
2744 server_id.server_id = strtoul(argv[2], NULL, 0);
2746 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2747 if (ret != 0) {
2748 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2749 return ret;
2751 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2752 sleep(999);
2753 return -1;
2757 unregister a server id
2759 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2761 int ret;
2762 struct ctdb_server_id server_id;
2764 if (argc < 3) {
2765 usage();
2768 server_id.pnn = strtoul(argv[0], NULL, 0);
2769 server_id.type = strtoul(argv[1], NULL, 0);
2770 server_id.server_id = strtoul(argv[2], NULL, 0);
2772 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2773 if (ret != 0) {
2774 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2775 return ret;
2777 return -1;
2781 check if a server id exists
2783 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2785 uint32_t status;
2786 int ret;
2787 struct ctdb_server_id server_id;
2789 if (argc < 3) {
2790 usage();
2793 server_id.pnn = strtoul(argv[0], NULL, 0);
2794 server_id.type = strtoul(argv[1], NULL, 0);
2795 server_id.server_id = strtoul(argv[2], NULL, 0);
2797 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2798 if (ret != 0) {
2799 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2800 return ret;
2803 if (status) {
2804 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2805 } else {
2806 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2808 return 0;
2812 get a list of all server ids that are registered on a node
2814 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2816 int i, ret;
2817 struct ctdb_server_id_list *server_ids;
2819 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2820 if (ret != 0) {
2821 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2822 return ret;
2825 for (i=0; i<server_ids->num; i++) {
2826 printf("Server id %d:%d:%d\n",
2827 server_ids->server_ids[i].pnn,
2828 server_ids->server_ids[i].type,
2829 server_ids->server_ids[i].server_id);
2832 return -1;
2836 check if a server id exists
2838 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2840 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2841 uint64_t *ids;
2842 uint8_t *result;
2843 int i;
2845 if (argc < 1) {
2846 talloc_free(tmp_ctx);
2847 usage();
2850 ids = talloc_array(tmp_ctx, uint64_t, argc);
2851 result = talloc_array(tmp_ctx, uint8_t, argc);
2853 for (i = 0; i < argc; i++) {
2854 ids[i] = strtoull(argv[i], NULL, 0);
2857 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2858 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2859 options.pnn));
2860 talloc_free(tmp_ctx);
2861 return -1;
2864 for (i=0; i < argc; i++) {
2865 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2866 result[i] ? "exists" : "does not exist");
2869 talloc_free(tmp_ctx);
2870 return 0;
2874 send a tcp tickle ack
2876 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2878 int ret;
2879 ctdb_sock_addr src, dst;
2881 if (argc < 2) {
2882 usage();
2885 if (!parse_ip_port(argv[0], &src)) {
2886 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2887 return -1;
2890 if (!parse_ip_port(argv[1], &dst)) {
2891 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2892 return -1;
2895 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2896 if (ret==0) {
2897 return 0;
2899 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2901 return -1;
2906 display public ip status
2908 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2910 int i, ret;
2911 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2912 struct ctdb_all_public_ips *ips;
2914 if (options.pnn == CTDB_BROADCAST_ALL) {
2915 /* read the list of public ips from all nodes */
2916 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2917 } else {
2918 /* read the public ip list from this node */
2919 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2921 if (ret != 0) {
2922 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2923 talloc_free(tmp_ctx);
2924 return ret;
2927 if (options.machinereadable){
2928 printf(":Public IP:Node:");
2929 if (options.verbose){
2930 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2932 printf("\n");
2933 } else {
2934 if (options.pnn == CTDB_BROADCAST_ALL) {
2935 printf("Public IPs on ALL nodes\n");
2936 } else {
2937 printf("Public IPs on node %u\n", options.pnn);
2941 for (i=1;i<=ips->num;i++) {
2942 struct ctdb_control_public_ip_info *info = NULL;
2943 int32_t pnn;
2944 char *aciface = NULL;
2945 char *avifaces = NULL;
2946 char *cifaces = NULL;
2948 if (options.pnn == CTDB_BROADCAST_ALL) {
2949 pnn = ips->ips[ips->num-i].pnn;
2950 } else {
2951 pnn = options.pnn;
2954 if (pnn != -1) {
2955 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2956 &ips->ips[ips->num-i].addr, &info);
2957 } else {
2958 ret = -1;
2961 if (ret == 0) {
2962 int j;
2963 for (j=0; j < info->num; j++) {
2964 if (cifaces == NULL) {
2965 cifaces = talloc_strdup(info,
2966 info->ifaces[j].name);
2967 } else {
2968 cifaces = talloc_asprintf_append(cifaces,
2969 ",%s",
2970 info->ifaces[j].name);
2973 if (info->active_idx == j) {
2974 aciface = info->ifaces[j].name;
2977 if (info->ifaces[j].link_state == 0) {
2978 continue;
2981 if (avifaces == NULL) {
2982 avifaces = talloc_strdup(info, info->ifaces[j].name);
2983 } else {
2984 avifaces = talloc_asprintf_append(avifaces,
2985 ",%s",
2986 info->ifaces[j].name);
2991 if (options.machinereadable){
2992 printf(":%s:%d:",
2993 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2994 ips->ips[ips->num-i].pnn);
2995 if (options.verbose){
2996 printf("%s:%s:%s:",
2997 aciface?aciface:"",
2998 avifaces?avifaces:"",
2999 cifaces?cifaces:"");
3001 printf("\n");
3002 } else {
3003 if (options.verbose) {
3004 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3005 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3006 ips->ips[ips->num-i].pnn,
3007 aciface?aciface:"",
3008 avifaces?avifaces:"",
3009 cifaces?cifaces:"");
3010 } else {
3011 printf("%s %d\n",
3012 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
3013 ips->ips[ips->num-i].pnn);
3016 talloc_free(info);
3019 talloc_free(tmp_ctx);
3020 return 0;
3024 public ip info
3026 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
3028 int i, ret;
3029 ctdb_sock_addr addr;
3030 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3031 struct ctdb_control_public_ip_info *info;
3033 if (argc != 1) {
3034 talloc_free(tmp_ctx);
3035 usage();
3038 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
3039 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
3040 return -1;
3043 /* read the public ip info from this node */
3044 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
3045 tmp_ctx, &addr, &info);
3046 if (ret != 0) {
3047 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
3048 argv[0], options.pnn));
3049 talloc_free(tmp_ctx);
3050 return ret;
3053 printf("Public IP[%s] info on node %u\n",
3054 ctdb_addr_to_str(&info->ip.addr),
3055 options.pnn);
3057 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3058 ctdb_addr_to_str(&info->ip.addr),
3059 info->ip.pnn, info->num);
3061 for (i=0; i<info->num; i++) {
3062 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
3064 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3065 i+1, info->ifaces[i].name,
3066 info->ifaces[i].link_state?"up":"down",
3067 (unsigned int)info->ifaces[i].references,
3068 (i==info->active_idx)?" (active)":"");
3071 talloc_free(tmp_ctx);
3072 return 0;
3076 display interfaces status
3078 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
3080 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3081 int i;
3082 struct ctdb_control_get_ifaces *ifaces;
3083 int ret;
3085 /* read the public ip list from this node */
3086 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
3087 if (ret != 0) {
3088 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
3089 options.pnn));
3090 talloc_free(tmp_ctx);
3091 return -1;
3094 if (options.machinereadable){
3095 printf(":Name:LinkStatus:References:\n");
3096 } else {
3097 printf("Interfaces on node %u\n", options.pnn);
3100 for (i=0; i<ifaces->num; i++) {
3101 if (options.machinereadable){
3102 printf(":%s:%s:%u\n",
3103 ifaces->ifaces[i].name,
3104 ifaces->ifaces[i].link_state?"1":"0",
3105 (unsigned int)ifaces->ifaces[i].references);
3106 } else {
3107 printf("name:%s link:%s references:%u\n",
3108 ifaces->ifaces[i].name,
3109 ifaces->ifaces[i].link_state?"up":"down",
3110 (unsigned int)ifaces->ifaces[i].references);
3114 talloc_free(tmp_ctx);
3115 return 0;
3120 set link status of an interface
3122 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3124 int ret;
3125 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3126 struct ctdb_control_iface_info info;
3128 ZERO_STRUCT(info);
3130 if (argc != 2) {
3131 usage();
3134 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3135 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3136 argv[0]));
3137 talloc_free(tmp_ctx);
3138 return -1;
3140 strcpy(info.name, argv[0]);
3142 if (strcmp(argv[1], "up") == 0) {
3143 info.link_state = 1;
3144 } else if (strcmp(argv[1], "down") == 0) {
3145 info.link_state = 0;
3146 } else {
3147 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3148 argv[1]));
3149 talloc_free(tmp_ctx);
3150 return -1;
3153 /* read the public ip list from this node */
3154 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3155 tmp_ctx, &info);
3156 if (ret != 0) {
3157 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3158 argv[0], options.pnn));
3159 talloc_free(tmp_ctx);
3160 return ret;
3163 talloc_free(tmp_ctx);
3164 return 0;
3168 display pid of a ctdb daemon
3170 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3172 uint32_t pid;
3173 int ret;
3175 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3176 if (ret != 0) {
3177 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3178 return ret;
3180 printf("Pid:%d\n", pid);
3182 return 0;
3185 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3187 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3188 void *data,
3189 update_flags_handler_t handler,
3190 uint32_t flag,
3191 const char *desc,
3192 bool set_flag)
3194 struct ctdb_node_map *nodemap = NULL;
3195 bool flag_is_set;
3196 int ret;
3198 /* Check if the node is already in the desired state */
3199 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3200 if (ret != 0) {
3201 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3202 exit(10);
3204 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3205 if (set_flag == flag_is_set) {
3206 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3207 (set_flag ? "already" : "not"), desc));
3208 return 0;
3211 do {
3212 if (!handler(ctdb, data)) {
3213 DEBUG(DEBUG_WARNING,
3214 ("Failed to send control to set state %s on node %u, try again\n",
3215 desc, options.pnn));
3218 sleep(1);
3220 /* Read the nodemap and verify the change took effect.
3221 * Even if the above control/hanlder timed out then it
3222 * could still have worked!
3224 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3225 ctdb, &nodemap);
3226 if (ret != 0) {
3227 DEBUG(DEBUG_WARNING,
3228 ("Unable to get nodemap from local node, try again\n"));
3230 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3231 } while (nodemap == NULL || (set_flag != flag_is_set));
3233 return ipreallocate(ctdb);
3236 /* Administratively disable a node */
3237 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3239 int ret;
3241 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3242 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3243 return ret == 0;
3246 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3248 return update_flags_and_ipreallocate(ctdb, NULL,
3249 update_flags_disabled,
3250 NODE_FLAGS_PERMANENTLY_DISABLED,
3251 "disabled",
3252 true /* set_flag*/);
3255 /* Administratively re-enable a node */
3256 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3258 int ret;
3260 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3261 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3262 return ret == 0;
3265 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3267 return update_flags_and_ipreallocate(ctdb, NULL,
3268 update_flags_not_disabled,
3269 NODE_FLAGS_PERMANENTLY_DISABLED,
3270 "disabled",
3271 false /* set_flag*/);
3274 /* Stop a node */
3275 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3277 int ret;
3279 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3281 return ret == 0;
3284 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3286 return update_flags_and_ipreallocate(ctdb, NULL,
3287 update_flags_stopped,
3288 NODE_FLAGS_STOPPED,
3289 "stopped",
3290 true /* set_flag*/);
3293 /* Continue a stopped node */
3294 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3296 int ret;
3298 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3300 return ret == 0;
3303 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3305 return update_flags_and_ipreallocate(ctdb, NULL,
3306 update_flags_not_stopped,
3307 NODE_FLAGS_STOPPED,
3308 "stopped",
3309 false /* set_flag */);
3312 static uint32_t get_generation(struct ctdb_context *ctdb)
3314 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3315 struct ctdb_vnn_map *vnnmap=NULL;
3316 int ret;
3317 uint32_t generation;
3319 /* wait until the recmaster is not in recovery mode */
3320 while (1) {
3321 uint32_t recmode, recmaster;
3323 if (vnnmap != NULL) {
3324 talloc_free(vnnmap);
3325 vnnmap = NULL;
3328 /* get the recmaster */
3329 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3330 if (ret != 0) {
3331 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3332 talloc_free(tmp_ctx);
3333 exit(10);
3336 /* get recovery mode */
3337 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3338 if (ret != 0) {
3339 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3340 talloc_free(tmp_ctx);
3341 exit(10);
3344 /* get the current generation number */
3345 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3346 if (ret != 0) {
3347 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3348 talloc_free(tmp_ctx);
3349 exit(10);
3352 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3353 generation = vnnmap->generation;
3354 talloc_free(tmp_ctx);
3355 return generation;
3357 sleep(1);
3361 /* Ban a node */
3362 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3364 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3365 int ret;
3367 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3369 return ret == 0;
3372 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3374 struct ctdb_ban_time bantime;
3376 if (argc < 1) {
3377 usage();
3380 bantime.pnn = options.pnn;
3381 bantime.time = strtoul(argv[0], NULL, 0);
3383 if (bantime.time == 0) {
3384 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3385 return -1;
3388 return update_flags_and_ipreallocate(ctdb, &bantime,
3389 update_state_banned,
3390 NODE_FLAGS_BANNED,
3391 "banned",
3392 true /* set_flag*/);
3396 /* Unban a node */
3397 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3399 struct ctdb_ban_time bantime;
3401 bantime.pnn = options.pnn;
3402 bantime.time = 0;
3404 return update_flags_and_ipreallocate(ctdb, &bantime,
3405 update_state_banned,
3406 NODE_FLAGS_BANNED,
3407 "banned",
3408 false /* set_flag*/);
3412 show ban information for a node
3414 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3416 int ret;
3417 struct ctdb_node_map *nodemap=NULL;
3418 struct ctdb_ban_time *bantime;
3420 /* verify the node exists */
3421 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3422 if (ret != 0) {
3423 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3424 return ret;
3427 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3428 if (ret != 0) {
3429 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3430 return -1;
3433 if (bantime->time == 0) {
3434 printf("Node %u is not banned\n", bantime->pnn);
3435 } else {
3436 printf("Node %u is banned, %d seconds remaining\n",
3437 bantime->pnn, bantime->time);
3440 return 0;
3444 shutdown a daemon
3446 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3448 int ret;
3450 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3451 if (ret != 0) {
3452 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3453 return ret;
3456 return 0;
3460 trigger a recovery
3462 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3464 int ret;
3465 uint32_t generation, next_generation;
3467 /* record the current generation number */
3468 generation = get_generation(ctdb);
3470 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3471 if (ret != 0) {
3472 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3473 return ret;
3476 /* wait until we are in a new generation */
3477 while (1) {
3478 next_generation = get_generation(ctdb);
3479 if (next_generation != generation) {
3480 return 0;
3482 sleep(1);
3485 return 0;
3490 display monitoring mode of a remote node
3492 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3494 uint32_t monmode;
3495 int ret;
3497 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3498 if (ret != 0) {
3499 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3500 return ret;
3502 if (!options.machinereadable){
3503 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3504 } else {
3505 printf(":mode:\n");
3506 printf(":%d:\n",monmode);
3508 return 0;
3513 display capabilities of a remote node
3515 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3517 uint32_t capabilities;
3518 int ret;
3520 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3521 if (ret != 0) {
3522 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3523 return -1;
3526 if (!options.machinereadable){
3527 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3528 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3529 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3530 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3531 } else {
3532 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3533 printf(":%d:%d:%d:%d:\n",
3534 !!(capabilities&CTDB_CAP_RECMASTER),
3535 !!(capabilities&CTDB_CAP_LMASTER),
3536 !!(capabilities&CTDB_CAP_LVS),
3537 !!(capabilities&CTDB_CAP_NATGW));
3539 return 0;
3543 display lvs configuration
3546 static uint32_t lvs_exclude_flags[] = {
3547 /* Look for a nice healthy node */
3548 NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED,
3549 /* If not found, an UNHEALTHY node will do */
3550 NODE_FLAGS_INACTIVE|NODE_FLAGS_PERMANENTLY_DISABLED,
3554 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3556 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3557 struct ctdb_node_map *orig_nodemap=NULL;
3558 struct ctdb_node_map *nodemap;
3559 int i, ret;
3561 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3562 tmp_ctx, &orig_nodemap);
3563 if (ret != 0) {
3564 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3565 talloc_free(tmp_ctx);
3566 return -1;
3569 nodemap = filter_nodemap_by_capabilities(ctdb, orig_nodemap,
3570 CTDB_CAP_LVS, false);
3571 if (nodemap == NULL) {
3572 /* No memory */
3573 ret = -1;
3574 goto done;
3577 ret = 0;
3579 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3580 struct ctdb_node_map *t =
3581 filter_nodemap_by_flags(ctdb, nodemap,
3582 lvs_exclude_flags[i]);
3583 if (t == NULL) {
3584 /* No memory */
3585 ret = -1;
3586 goto done;
3588 if (t->num > 0) {
3589 /* At least 1 node without excluded flags */
3590 int j;
3591 for (j = 0; j < t->num; j++) {
3592 printf("%d:%s\n", t->nodes[j].pnn,
3593 ctdb_addr_to_str(&t->nodes[j].addr));
3595 goto done;
3597 talloc_free(t);
3599 done:
3600 talloc_free(tmp_ctx);
3601 return ret;
3605 display who is the lvs master
3607 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3609 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3610 struct ctdb_node_map *nodemap=NULL;
3611 int i, ret;
3613 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn,
3614 tmp_ctx, &nodemap);
3615 if (ret != 0) {
3616 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3617 talloc_free(tmp_ctx);
3618 return -1;
3621 for (i = 0; lvs_exclude_flags[i] != 0; i++) {
3622 struct ctdb_node_map *t =
3623 filter_nodemap_by_flags(ctdb, nodemap,
3624 lvs_exclude_flags[i]);
3625 if (t == NULL) {
3626 /* No memory */
3627 ret = -1;
3628 goto done;
3630 if (t->num > 0) {
3631 struct ctdb_node_map *n;
3632 n = filter_nodemap_by_capabilities(ctdb,
3634 CTDB_CAP_LVS,
3635 true);
3636 if (n == NULL) {
3637 /* No memory */
3638 ret = -1;
3639 goto done;
3641 if (n->num > 0) {
3642 ret = 0;
3643 printf(options.machinereadable ?
3644 "%d\n" : "Node %d is LVS master\n",
3645 n->nodes[0].pnn);
3646 goto done;
3649 talloc_free(t);
3652 printf("There is no LVS master\n");
3653 ret = 255;
3654 done:
3655 talloc_free(tmp_ctx);
3656 return ret;
3660 disable monitoring on a node
3662 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3665 int ret;
3667 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3668 if (ret != 0) {
3669 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3670 return ret;
3672 printf("Monitoring mode:%s\n","DISABLED");
3674 return 0;
3678 enable monitoring on a node
3680 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3683 int ret;
3685 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3686 if (ret != 0) {
3687 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3688 return ret;
3690 printf("Monitoring mode:%s\n","ACTIVE");
3692 return 0;
3696 display remote list of keys/data for a db
3698 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3700 const char *db_name;
3701 struct ctdb_db_context *ctdb_db;
3702 int ret;
3703 struct ctdb_dump_db_context c;
3704 uint8_t flags;
3706 if (argc < 1) {
3707 usage();
3710 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3711 return -1;
3714 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3715 if (ctdb_db == NULL) {
3716 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3717 return -1;
3720 if (options.printlmaster) {
3721 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3722 ctdb, &ctdb->vnn_map);
3723 if (ret != 0) {
3724 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3725 options.pnn));
3726 return ret;
3730 ZERO_STRUCT(c);
3731 c.f = stdout;
3732 c.printemptyrecords = (bool)options.printemptyrecords;
3733 c.printdatasize = (bool)options.printdatasize;
3734 c.printlmaster = (bool)options.printlmaster;
3735 c.printhash = (bool)options.printhash;
3736 c.printrecordflags = (bool)options.printrecordflags;
3738 /* traverse and dump the cluster tdb */
3739 ret = ctdb_dump_db(ctdb_db, &c);
3740 if (ret == -1) {
3741 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3742 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3743 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3744 db_name));
3745 return -1;
3747 talloc_free(ctdb_db);
3749 printf("Dumped %d records\n", ret);
3750 return 0;
3753 struct cattdb_data {
3754 struct ctdb_context *ctdb;
3755 uint32_t count;
3758 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3760 struct cattdb_data *d = private_data;
3761 struct ctdb_dump_db_context c;
3763 d->count++;
3765 ZERO_STRUCT(c);
3766 c.f = stdout;
3767 c.printemptyrecords = (bool)options.printemptyrecords;
3768 c.printdatasize = (bool)options.printdatasize;
3769 c.printlmaster = false;
3770 c.printhash = (bool)options.printhash;
3771 c.printrecordflags = true;
3773 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3777 cat the local tdb database using same format as catdb
3779 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3781 const char *db_name;
3782 struct ctdb_db_context *ctdb_db;
3783 struct cattdb_data d;
3784 uint8_t flags;
3786 if (argc < 1) {
3787 usage();
3790 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3791 return -1;
3794 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3795 if (ctdb_db == NULL) {
3796 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3797 return -1;
3800 /* traverse the local tdb */
3801 d.count = 0;
3802 d.ctdb = ctdb;
3803 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3804 printf("Failed to cattdb data\n");
3805 exit(10);
3807 talloc_free(ctdb_db);
3809 printf("Dumped %d records\n", d.count);
3810 return 0;
3814 display the content of a database key
3816 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3818 const char *db_name;
3819 struct ctdb_db_context *ctdb_db;
3820 struct ctdb_record_handle *h;
3821 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3822 TDB_DATA key, data;
3823 uint8_t flags;
3825 if (argc < 2) {
3826 usage();
3829 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3830 return -1;
3833 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3834 if (ctdb_db == NULL) {
3835 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3836 return -1;
3839 key.dptr = discard_const(argv[1]);
3840 key.dsize = strlen((char *)key.dptr);
3842 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3843 if (h == NULL) {
3844 printf("Failed to fetch record '%s' on node %d\n",
3845 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3846 talloc_free(tmp_ctx);
3847 exit(10);
3850 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3852 talloc_free(tmp_ctx);
3853 talloc_free(ctdb_db);
3854 return 0;
3858 display the content of a database key
3860 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3862 const char *db_name;
3863 struct ctdb_db_context *ctdb_db;
3864 struct ctdb_record_handle *h;
3865 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3866 TDB_DATA key, data;
3867 uint8_t flags;
3869 if (argc < 3) {
3870 usage();
3873 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3874 return -1;
3877 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3878 if (ctdb_db == NULL) {
3879 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3880 return -1;
3883 key.dptr = discard_const(argv[1]);
3884 key.dsize = strlen((char *)key.dptr);
3886 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3887 if (h == NULL) {
3888 printf("Failed to fetch record '%s' on node %d\n",
3889 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3890 talloc_free(tmp_ctx);
3891 exit(10);
3894 data.dptr = discard_const(argv[2]);
3895 data.dsize = strlen((char *)data.dptr);
3897 if (ctdb_record_store(h, data) != 0) {
3898 printf("Failed to store record\n");
3901 talloc_free(h);
3902 talloc_free(tmp_ctx);
3903 talloc_free(ctdb_db);
3904 return 0;
3908 fetch a record from a persistent database
3910 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3912 const char *db_name;
3913 struct ctdb_db_context *ctdb_db;
3914 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3915 struct ctdb_transaction_handle *h;
3916 TDB_DATA key, data;
3917 int fd, ret;
3918 bool persistent;
3919 uint8_t flags;
3921 if (argc < 2) {
3922 talloc_free(tmp_ctx);
3923 usage();
3926 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
3927 talloc_free(tmp_ctx);
3928 return -1;
3931 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3932 if (!persistent) {
3933 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3934 talloc_free(tmp_ctx);
3935 return -1;
3938 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3939 if (ctdb_db == NULL) {
3940 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3941 talloc_free(tmp_ctx);
3942 return -1;
3945 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3946 if (h == NULL) {
3947 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3948 talloc_free(tmp_ctx);
3949 return -1;
3952 key.dptr = discard_const(argv[1]);
3953 key.dsize = strlen(argv[1]);
3954 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3955 if (ret != 0) {
3956 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3957 talloc_free(tmp_ctx);
3958 return -1;
3961 if (data.dsize == 0 || data.dptr == NULL) {
3962 DEBUG(DEBUG_ERR,("Record is empty\n"));
3963 talloc_free(tmp_ctx);
3964 return -1;
3967 if (argc == 3) {
3968 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3969 if (fd == -1) {
3970 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3971 talloc_free(tmp_ctx);
3972 return -1;
3974 write(fd, data.dptr, data.dsize);
3975 close(fd);
3976 } else {
3977 write(1, data.dptr, data.dsize);
3980 /* abort the transaction */
3981 talloc_free(h);
3984 talloc_free(tmp_ctx);
3985 return 0;
3989 fetch a record from a tdb-file
3991 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3993 const char *tdb_file;
3994 TDB_CONTEXT *tdb;
3995 TDB_DATA key, data;
3996 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3997 int fd;
3999 if (argc < 2) {
4000 usage();
4003 tdb_file = argv[0];
4005 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
4006 if (tdb == NULL) {
4007 printf("Failed to open TDB file %s\n", tdb_file);
4008 return -1;
4011 if (!strncmp(argv[1], "0x", 2)) {
4012 key = hextodata(tmp_ctx, argv[1] + 2);
4013 if (key.dsize == 0) {
4014 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4015 return -1;
4017 } else {
4018 key.dptr = discard_const(argv[1]);
4019 key.dsize = strlen(argv[1]);
4022 data = tdb_fetch(tdb, key);
4023 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
4024 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
4025 tdb_close(tdb);
4026 return -1;
4029 tdb_close(tdb);
4031 if (argc == 3) {
4032 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
4033 if (fd == -1) {
4034 printf("Failed to open output file %s\n", argv[2]);
4035 return -1;
4037 if (options.verbose){
4038 write(fd, data.dptr, data.dsize);
4039 } else {
4040 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4042 close(fd);
4043 } else {
4044 if (options.verbose){
4045 write(1, data.dptr, data.dsize);
4046 } else {
4047 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4051 talloc_free(tmp_ctx);
4052 return 0;
4056 store a record and header to a tdb-file
4058 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4060 const char *tdb_file;
4061 TDB_CONTEXT *tdb;
4062 TDB_DATA key, value, data;
4063 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4064 struct ctdb_ltdb_header header;
4066 if (argc < 3) {
4067 usage();
4070 tdb_file = argv[0];
4072 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4073 if (tdb == NULL) {
4074 printf("Failed to open TDB file %s\n", tdb_file);
4075 return -1;
4078 if (!strncmp(argv[1], "0x", 2)) {
4079 key = hextodata(tmp_ctx, argv[1] + 2);
4080 if (key.dsize == 0) {
4081 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4082 return -1;
4084 } else {
4085 key.dptr = discard_const(argv[1]);
4086 key.dsize = strlen(argv[1]);
4089 if (!strncmp(argv[2], "0x", 2)) {
4090 value = hextodata(tmp_ctx, argv[2] + 2);
4091 if (value.dsize == 0) {
4092 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4093 return -1;
4095 } else {
4096 value.dptr = discard_const(argv[2]);
4097 value.dsize = strlen(argv[2]);
4100 ZERO_STRUCT(header);
4101 if (argc > 3) {
4102 header.rsn = atoll(argv[3]);
4104 if (argc > 4) {
4105 header.dmaster = atoi(argv[4]);
4107 if (argc > 5) {
4108 header.flags = atoi(argv[5]);
4111 data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
4112 data.dptr = talloc_size(tmp_ctx, data.dsize);
4113 if (data.dptr == NULL) {
4114 printf("Failed to allocate header+value\n");
4115 return -1;
4118 *(struct ctdb_ltdb_header *)data.dptr = header;
4119 memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
4121 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4122 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4123 tdb_close(tdb);
4124 return -1;
4127 tdb_close(tdb);
4129 talloc_free(tmp_ctx);
4130 return 0;
4134 write a record to a persistent database
4136 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4138 const char *db_name;
4139 struct ctdb_db_context *ctdb_db;
4140 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4141 struct ctdb_transaction_handle *h;
4142 struct stat st;
4143 TDB_DATA key, data;
4144 int fd, ret;
4146 if (argc < 3) {
4147 talloc_free(tmp_ctx);
4148 usage();
4151 fd = open(argv[2], O_RDONLY);
4152 if (fd == -1) {
4153 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4154 talloc_free(tmp_ctx);
4155 return -1;
4158 ret = fstat(fd, &st);
4159 if (ret == -1) {
4160 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4161 close(fd);
4162 talloc_free(tmp_ctx);
4163 return -1;
4166 if (!S_ISREG(st.st_mode)) {
4167 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4168 close(fd);
4169 talloc_free(tmp_ctx);
4170 return -1;
4173 data.dsize = st.st_size;
4174 if (data.dsize == 0) {
4175 data.dptr = NULL;
4176 } else {
4177 data.dptr = talloc_size(tmp_ctx, data.dsize);
4178 if (data.dptr == NULL) {
4179 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4180 close(fd);
4181 talloc_free(tmp_ctx);
4182 return -1;
4184 ret = read(fd, data.dptr, data.dsize);
4185 if (ret != data.dsize) {
4186 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4187 close(fd);
4188 talloc_free(tmp_ctx);
4189 return -1;
4192 close(fd);
4195 db_name = argv[0];
4197 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4198 if (ctdb_db == NULL) {
4199 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4200 talloc_free(tmp_ctx);
4201 return -1;
4204 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4205 if (h == NULL) {
4206 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4207 talloc_free(tmp_ctx);
4208 return -1;
4211 key.dptr = discard_const(argv[1]);
4212 key.dsize = strlen(argv[1]);
4213 ret = ctdb_transaction_store(h, key, data);
4214 if (ret != 0) {
4215 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4216 talloc_free(tmp_ctx);
4217 return -1;
4220 ret = ctdb_transaction_commit(h);
4221 if (ret != 0) {
4222 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4223 talloc_free(tmp_ctx);
4224 return -1;
4228 talloc_free(tmp_ctx);
4229 return 0;
4233 * delete a record from a persistent database
4235 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4237 const char *db_name;
4238 struct ctdb_db_context *ctdb_db;
4239 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4240 struct ctdb_transaction_handle *h;
4241 TDB_DATA key;
4242 int ret;
4243 bool persistent;
4244 uint8_t flags;
4246 if (argc < 2) {
4247 talloc_free(tmp_ctx);
4248 usage();
4251 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
4252 talloc_free(tmp_ctx);
4253 return -1;
4256 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4257 if (!persistent) {
4258 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4259 talloc_free(tmp_ctx);
4260 return -1;
4263 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4264 if (ctdb_db == NULL) {
4265 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4266 talloc_free(tmp_ctx);
4267 return -1;
4270 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4271 if (h == NULL) {
4272 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4273 talloc_free(tmp_ctx);
4274 return -1;
4277 key.dptr = discard_const(argv[1]);
4278 key.dsize = strlen(argv[1]);
4279 ret = ctdb_transaction_store(h, key, tdb_null);
4280 if (ret != 0) {
4281 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4282 talloc_free(tmp_ctx);
4283 return -1;
4286 ret = ctdb_transaction_commit(h);
4287 if (ret != 0) {
4288 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4289 talloc_free(tmp_ctx);
4290 return -1;
4293 talloc_free(tmp_ctx);
4294 return 0;
4297 static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
4298 TDB_DATA *data)
4300 const char *t;
4301 size_t n;
4302 const char *ret; /* Next byte after successfully parsed value */
4304 /* Error, unless someone says otherwise */
4305 ret = NULL;
4306 /* Indicates no value to parse */
4307 *data = tdb_null;
4309 /* Skip whitespace */
4310 n = strspn(s, " \t");
4311 t = s + n;
4313 if (t[0] == '"') {
4314 /* Quoted ASCII string - no wide characters! */
4315 t++;
4316 n = strcspn(t, "\"");
4317 if (t[n] == '"') {
4318 if (n > 0) {
4319 data->dsize = n;
4320 data->dptr = talloc_memdup(mem_ctx, t, n);
4321 CTDB_NOMEM_ABORT(data->dptr);
4323 ret = t + n + 1;
4324 } else {
4325 DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
4327 } else {
4328 DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
4331 return ret;
4334 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
4335 TDB_DATA *key, TDB_DATA *value)
4337 char line [1024]; /* FIXME: make this more flexible? */
4338 const char *t;
4339 char *ptr;
4341 ptr = fgets(line, sizeof(line), file);
4343 if (ptr == NULL) {
4344 return false;
4347 /* Get key */
4348 t = ptrans_parse_string(mem_ctx, line, key);
4349 if (t == NULL || key->dptr == NULL) {
4350 /* Line Ignored but not EOF */
4351 return true;
4354 /* Get value */
4355 t = ptrans_parse_string(mem_ctx, t, value);
4356 if (t == NULL) {
4357 /* Line Ignored but not EOF */
4358 talloc_free(key->dptr);
4359 *key = tdb_null;
4360 return true;
4363 return true;
4367 * Update a persistent database as per file/stdin
4369 static int control_ptrans(struct ctdb_context *ctdb,
4370 int argc, const char **argv)
4372 const char *db_name;
4373 struct ctdb_db_context *ctdb_db;
4374 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4375 struct ctdb_transaction_handle *h;
4376 TDB_DATA key, value;
4377 FILE *file;
4378 int ret;
4380 if (argc < 1) {
4381 talloc_free(tmp_ctx);
4382 usage();
4385 file = stdin;
4386 if (argc == 2) {
4387 file = fopen(argv[1], "r");
4388 if (file == NULL) {
4389 DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
4390 talloc_free(tmp_ctx);
4391 return -1;
4395 db_name = argv[0];
4397 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4398 if (ctdb_db == NULL) {
4399 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4400 goto error;
4403 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4404 if (h == NULL) {
4405 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4406 goto error;
4409 while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
4410 if (key.dsize != 0) {
4411 ret = ctdb_transaction_store(h, key, value);
4412 /* Minimise memory use */
4413 talloc_free(key.dptr);
4414 if (value.dptr != NULL) {
4415 talloc_free(value.dptr);
4417 if (ret != 0) {
4418 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4419 ctdb_transaction_cancel(h);
4420 goto error;
4425 ret = ctdb_transaction_commit(h);
4426 if (ret != 0) {
4427 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4428 goto error;
4431 if (file != stdin) {
4432 fclose(file);
4434 talloc_free(tmp_ctx);
4435 return 0;
4437 error:
4438 if (file != stdin) {
4439 fclose(file);
4442 talloc_free(tmp_ctx);
4443 return -1;
4447 check if a service is bound to a port or not
4449 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4451 int s, ret;
4452 int v;
4453 int port;
4454 struct sockaddr_in sin;
4456 if (argc != 1) {
4457 printf("Use: ctdb chktcport <port>\n");
4458 return EINVAL;
4461 port = atoi(argv[0]);
4463 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4464 if (s == -1) {
4465 printf("Failed to open local socket\n");
4466 return errno;
4469 v = fcntl(s, F_GETFL, 0);
4470 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
4471 printf("Unable to set socket non-blocking: %s\n", strerror(errno));
4474 bzero(&sin, sizeof(sin));
4475 sin.sin_family = PF_INET;
4476 sin.sin_port = htons(port);
4477 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4478 close(s);
4479 if (ret == -1) {
4480 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4481 return errno;
4484 return 0;
4489 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4490 TDB_DATA data, void *private_data)
4492 DEBUG(DEBUG_ERR,("Log data received\n"));
4493 if (data.dsize > 0) {
4494 printf("%s", data.dptr);
4497 exit(0);
4501 display a list of log messages from the in memory ringbuffer
4503 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4505 int ret, i;
4506 bool main_daemon;
4507 struct ctdb_get_log_addr log_addr;
4508 TDB_DATA data;
4509 struct timeval tv;
4511 /* Process options */
4512 main_daemon = true;
4513 log_addr.pnn = ctdb_get_pnn(ctdb);
4514 log_addr.level = DEBUG_NOTICE;
4515 for (i = 0; i < argc; i++) {
4516 if (strcmp(argv[i], "recoverd") == 0) {
4517 main_daemon = false;
4518 } else {
4519 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4520 log_addr.level = get_debug_by_desc(argv[i]);
4521 } else {
4522 log_addr.level = strtol(argv[i], NULL, 0);
4527 /* Our message port is our PID */
4528 log_addr.srvid = getpid();
4530 data.dptr = (unsigned char *)&log_addr;
4531 data.dsize = sizeof(log_addr);
4533 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4535 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4536 sleep(1);
4538 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4540 if (main_daemon) {
4541 int32_t res;
4542 char *errmsg;
4543 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4545 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4546 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4547 if (ret != 0 || res != 0) {
4548 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4549 talloc_free(tmp_ctx);
4550 return -1;
4552 talloc_free(tmp_ctx);
4553 } else {
4554 ret = ctdb_client_send_message(ctdb, options.pnn,
4555 CTDB_SRVID_GETLOG, data);
4556 if (ret != 0) {
4557 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4558 return -1;
4562 tv = timeval_current();
4563 /* this loop will terminate when we have received the reply */
4564 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4565 event_loop_once(ctdb->ev);
4568 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4570 return 0;
4574 clear the in memory log area
4576 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4578 int ret;
4580 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4581 /* "recoverd" not given - get logs from main daemon */
4582 int32_t res;
4583 char *errmsg;
4584 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4586 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4587 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4588 if (ret != 0 || res != 0) {
4589 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4590 talloc_free(tmp_ctx);
4591 return -1;
4594 talloc_free(tmp_ctx);
4595 } else {
4596 TDB_DATA data; /* unused in recoverd... */
4597 data.dsize = 0;
4599 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4600 if (ret != 0) {
4601 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4602 return -1;
4606 return 0;
4609 /* Reload public IPs on a specified nodes */
4610 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4612 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4613 uint32_t *nodes;
4614 uint32_t pnn_mode;
4615 uint32_t timeout;
4616 int ret;
4618 assert_single_node_only();
4620 if (argc > 1) {
4621 usage();
4624 /* Determine the nodes where IPs need to be reloaded */
4625 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4626 options.pnn, true, &nodes, &pnn_mode)) {
4627 ret = -1;
4628 goto done;
4631 again:
4632 /* Disable takeover runs on all connected nodes. A reply
4633 * indicating success is needed from each node so all nodes
4634 * will need to be active. This will retry until maxruntime
4635 * is exceeded, hence no error handling.
4637 * A check could be added to not allow reloading of IPs when
4638 * there are disconnected nodes. However, this should
4639 * probably be left up to the administrator.
4641 timeout = LONGTIMEOUT;
4642 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4643 "Disable takeover runs", true);
4645 /* Now tell all the desired nodes to reload their public IPs.
4646 * Keep trying this until it succeeds. This assumes all
4647 * failures are transient, which might not be true...
4649 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4650 nodes, 0, LONGTIMELIMIT(),
4651 false, tdb_null,
4652 NULL, NULL, NULL) != 0) {
4653 DEBUG(DEBUG_ERR,
4654 ("Unable to reload IPs on some nodes, try again.\n"));
4655 goto again;
4658 /* It isn't strictly necessary to wait until takeover runs are
4659 * re-enabled but doing so can't hurt.
4661 timeout = 0;
4662 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
4663 "Enable takeover runs", true);
4665 ipreallocate(ctdb);
4667 ret = 0;
4668 done:
4669 talloc_free(tmp_ctx);
4670 return ret;
4674 display a list of the databases on a remote ctdb
4676 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4678 int i, ret;
4679 struct ctdb_dbid_map *dbmap=NULL;
4681 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4682 if (ret != 0) {
4683 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4684 return ret;
4687 if(options.machinereadable){
4688 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4689 for(i=0;i<dbmap->num;i++){
4690 const char *path;
4691 const char *name;
4692 const char *health;
4693 bool persistent;
4694 bool readonly;
4695 bool sticky;
4697 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4698 dbmap->dbs[i].dbid, ctdb, &path);
4699 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4700 dbmap->dbs[i].dbid, ctdb, &name);
4701 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4702 dbmap->dbs[i].dbid, ctdb, &health);
4703 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4704 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4705 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4706 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4707 dbmap->dbs[i].dbid, name, path,
4708 !!(persistent), !!(sticky),
4709 !!(health), !!(readonly));
4711 return 0;
4714 printf("Number of databases:%d\n", dbmap->num);
4715 for(i=0;i<dbmap->num;i++){
4716 const char *path;
4717 const char *name;
4718 const char *health;
4719 bool persistent;
4720 bool readonly;
4721 bool sticky;
4723 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4724 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4725 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4726 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4727 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4728 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4729 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4730 dbmap->dbs[i].dbid, name, path,
4731 persistent?" PERSISTENT":"",
4732 sticky?" STICKY":"",
4733 readonly?" READONLY":"",
4734 health?" UNHEALTHY":"");
4737 return 0;
4741 display the status of a database on a remote ctdb
4743 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4745 const char *db_name;
4746 uint32_t db_id;
4747 uint8_t flags;
4748 const char *path;
4749 const char *health;
4751 if (argc < 1) {
4752 usage();
4755 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
4756 return -1;
4759 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4760 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4761 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4762 db_id, db_name, path,
4763 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4764 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4765 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4766 (health ? health : "OK"));
4768 return 0;
4772 check if the local node is recmaster or not
4773 it will return 1 if this node is the recmaster and 0 if it is not
4774 or if the local ctdb daemon could not be contacted
4776 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4778 uint32_t mypnn, recmaster;
4779 int ret;
4781 assert_single_node_only();
4783 mypnn = getpnn(ctdb);
4785 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4786 if (ret != 0) {
4787 printf("Failed to get the recmaster\n");
4788 return 1;
4791 if (recmaster != mypnn) {
4792 printf("this node is not the recmaster\n");
4793 return 1;
4796 printf("this node is the recmaster\n");
4797 return 0;
4801 ping a node
4803 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4805 int ret;
4806 struct timeval tv = timeval_current();
4807 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4808 if (ret == -1) {
4809 printf("Unable to get ping response from node %u\n", options.pnn);
4810 return -1;
4811 } else {
4812 printf("response from %u time=%.6f sec (%d clients)\n",
4813 options.pnn, timeval_elapsed(&tv), ret);
4815 return 0;
4820 get a node's runstate
4822 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4824 int ret;
4825 enum ctdb_runstate runstate;
4827 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4828 if (ret == -1) {
4829 printf("Unable to get runstate response from node %u\n",
4830 options.pnn);
4831 return -1;
4832 } else {
4833 bool found = true;
4834 enum ctdb_runstate t;
4835 int i;
4836 for (i=0; i<argc; i++) {
4837 found = false;
4838 t = runstate_from_string(argv[i]);
4839 if (t == CTDB_RUNSTATE_UNKNOWN) {
4840 printf("Invalid run state (%s)\n", argv[i]);
4841 return -1;
4844 if (t == runstate) {
4845 found = true;
4846 break;
4850 if (!found) {
4851 printf("CTDB not in required run state (got %s)\n",
4852 runstate_to_string((enum ctdb_runstate)runstate));
4853 return -1;
4857 printf("%s\n", runstate_to_string(runstate));
4858 return 0;
4863 get a tunable
4865 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4867 const char *name;
4868 uint32_t value;
4869 int ret;
4871 if (argc < 1) {
4872 usage();
4875 name = argv[0];
4876 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4877 if (ret != 0) {
4878 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4879 return -1;
4882 printf("%-23s = %u\n", name, value);
4883 return 0;
4887 set a tunable
4889 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4891 const char *name;
4892 uint32_t value;
4893 int ret;
4895 if (argc < 2) {
4896 usage();
4899 name = argv[0];
4900 value = strtoul(argv[1], NULL, 0);
4902 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4903 if (ret == -1) {
4904 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4905 return -1;
4907 return 0;
4911 list all tunables
4913 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4915 uint32_t count;
4916 const char **list;
4917 int ret, i;
4919 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4920 if (ret == -1) {
4921 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4922 return -1;
4925 for (i=0;i<count;i++) {
4926 control_getvar(ctdb, 1, &list[i]);
4929 talloc_free(list);
4931 return 0;
4935 display debug level on a node
4937 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4939 int ret;
4940 int32_t level;
4942 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4943 if (ret != 0) {
4944 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4945 return ret;
4946 } else {
4947 if (options.machinereadable){
4948 printf(":Name:Level:\n");
4949 printf(":%s:%d:\n",get_debug_by_level(level),level);
4950 } else {
4951 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4954 return 0;
4958 display reclock file of a node
4960 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4962 int ret;
4963 const char *reclock;
4965 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4966 if (ret != 0) {
4967 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4968 return ret;
4969 } else {
4970 if (options.machinereadable){
4971 if (reclock != NULL) {
4972 printf("%s", reclock);
4974 } else {
4975 if (reclock == NULL) {
4976 printf("No reclock file used.\n");
4977 } else {
4978 printf("Reclock file:%s\n", reclock);
4982 return 0;
4986 set the reclock file of a node
4988 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4990 int ret;
4991 const char *reclock;
4993 if (argc == 0) {
4994 reclock = NULL;
4995 } else if (argc == 1) {
4996 reclock = argv[0];
4997 } else {
4998 usage();
5001 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
5002 if (ret != 0) {
5003 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
5004 return ret;
5006 return 0;
5010 set the natgw state on/off
5012 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
5014 int ret;
5015 uint32_t natgwstate;
5017 if (argc == 0) {
5018 usage();
5021 if (!strcmp(argv[0], "on")) {
5022 natgwstate = 1;
5023 } else if (!strcmp(argv[0], "off")) {
5024 natgwstate = 0;
5025 } else {
5026 usage();
5029 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
5030 if (ret != 0) {
5031 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
5032 return ret;
5035 return 0;
5039 set the lmaster role on/off
5041 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5043 int ret;
5044 uint32_t lmasterrole;
5046 if (argc == 0) {
5047 usage();
5050 if (!strcmp(argv[0], "on")) {
5051 lmasterrole = 1;
5052 } else if (!strcmp(argv[0], "off")) {
5053 lmasterrole = 0;
5054 } else {
5055 usage();
5058 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
5059 if (ret != 0) {
5060 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
5061 return ret;
5064 return 0;
5068 set the recmaster role on/off
5070 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
5072 int ret;
5073 uint32_t recmasterrole;
5075 if (argc == 0) {
5076 usage();
5079 if (!strcmp(argv[0], "on")) {
5080 recmasterrole = 1;
5081 } else if (!strcmp(argv[0], "off")) {
5082 recmasterrole = 0;
5083 } else {
5084 usage();
5087 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
5088 if (ret != 0) {
5089 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
5090 return ret;
5093 return 0;
5097 set debug level on a node or all nodes
5099 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
5101 int i, ret;
5102 int32_t level;
5104 if (argc == 0) {
5105 printf("You must specify the debug level. Valid levels are:\n");
5106 for (i=0; debug_levels[i].description != NULL; i++) {
5107 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5110 return 0;
5113 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
5114 level = get_debug_by_desc(argv[0]);
5115 } else {
5116 level = strtol(argv[0], NULL, 0);
5119 for (i=0; debug_levels[i].description != NULL; i++) {
5120 if (level == debug_levels[i].level) {
5121 break;
5124 if (debug_levels[i].description == NULL) {
5125 printf("Invalid debug level, must be one of\n");
5126 for (i=0; debug_levels[i].description != NULL; i++) {
5127 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
5129 return -1;
5132 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
5133 if (ret != 0) {
5134 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
5136 return 0;
5141 thaw a node
5143 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
5145 int ret;
5146 uint32_t priority;
5148 if (argc == 1) {
5149 priority = strtol(argv[0], NULL, 0);
5150 } else {
5151 priority = 0;
5153 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
5155 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
5156 if (ret != 0) {
5157 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
5159 return 0;
5164 attach to a database
5166 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
5168 const char *db_name;
5169 struct ctdb_db_context *ctdb_db;
5170 bool persistent = false;
5172 if (argc < 1) {
5173 usage();
5175 db_name = argv[0];
5176 if (argc > 2) {
5177 usage();
5179 if (argc == 2) {
5180 if (strcmp(argv[1], "persistent") != 0) {
5181 usage();
5183 persistent = true;
5186 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
5187 if (ctdb_db == NULL) {
5188 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
5189 return -1;
5192 return 0;
5196 * detach from a database
5198 static int control_detach(struct ctdb_context *ctdb, int argc,
5199 const char **argv)
5201 uint32_t db_id;
5202 uint8_t flags;
5203 int ret, i, status = 0;
5204 struct ctdb_node_map *nodemap = NULL;
5205 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5206 uint32_t recmode;
5208 if (argc < 1) {
5209 usage();
5212 assert_single_node_only();
5214 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
5215 &recmode);
5216 if (ret != 0) {
5217 DEBUG(DEBUG_ERR, ("Database cannot be detached "
5218 "when recovery is active\n"));
5219 talloc_free(tmp_ctx);
5220 return -1;
5223 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5224 &nodemap);
5225 if (ret != 0) {
5226 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5227 options.pnn));
5228 talloc_free(tmp_ctx);
5229 return -1;
5232 for (i=0; i<nodemap->num; i++) {
5233 uint32_t value;
5235 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
5236 continue;
5239 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
5240 continue;
5243 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
5244 DEBUG(DEBUG_ERR, ("Database cannot be detached on "
5245 "inactive (stopped or banned) node "
5246 "%u\n", nodemap->nodes[i].pnn));
5247 talloc_free(tmp_ctx);
5248 return -1;
5251 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
5252 nodemap->nodes[i].pnn,
5253 "AllowClientDBAttach",
5254 &value);
5255 if (ret != 0) {
5256 DEBUG(DEBUG_ERR, ("Unable to get tunable "
5257 "AllowClientDBAttach from node %u\n",
5258 nodemap->nodes[i].pnn));
5259 talloc_free(tmp_ctx);
5260 return -1;
5263 if (value == 1) {
5264 DEBUG(DEBUG_ERR, ("Database access is still active on "
5265 "node %u. Set AllowClientDBAttach=0 "
5266 "on all nodes.\n",
5267 nodemap->nodes[i].pnn));
5268 talloc_free(tmp_ctx);
5269 return -1;
5273 talloc_free(tmp_ctx);
5275 for (i=0; i<argc; i++) {
5276 if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
5277 continue;
5280 if (flags & CTDB_DB_FLAGS_PERSISTENT) {
5281 DEBUG(DEBUG_ERR, ("Persistent database '%s' "
5282 "cannot be detached\n", argv[i]));
5283 status = -1;
5284 continue;
5287 ret = ctdb_detach(ctdb, db_id);
5288 if (ret != 0) {
5289 DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
5290 argv[i]));
5291 status = ret;
5295 return status;
5299 set db priority
5301 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5303 struct ctdb_db_priority db_prio;
5304 int ret;
5306 if (argc < 2) {
5307 usage();
5310 db_prio.db_id = strtoul(argv[0], NULL, 0);
5311 db_prio.priority = strtoul(argv[1], NULL, 0);
5313 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5314 if (ret != 0) {
5315 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5316 return -1;
5319 return 0;
5323 get db priority
5325 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5327 uint32_t db_id, priority;
5328 int ret;
5330 if (argc < 1) {
5331 usage();
5334 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5335 return -1;
5338 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5339 if (ret != 0) {
5340 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5341 return -1;
5344 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5346 return 0;
5350 set the sticky records capability for a database
5352 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5354 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5355 uint32_t db_id;
5356 int ret;
5358 if (argc < 1) {
5359 usage();
5362 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5363 return -1;
5366 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5367 if (ret != 0) {
5368 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5369 talloc_free(tmp_ctx);
5370 return -1;
5373 talloc_free(tmp_ctx);
5374 return 0;
5378 set the readonly capability for a database
5380 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5382 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5383 uint32_t db_id;
5384 int ret;
5386 if (argc < 1) {
5387 usage();
5390 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5391 return -1;
5394 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5395 if (ret != 0) {
5396 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5397 talloc_free(tmp_ctx);
5398 return -1;
5401 talloc_free(tmp_ctx);
5402 return 0;
5406 get db seqnum
5408 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5410 uint32_t db_id;
5411 uint64_t seqnum;
5412 int ret;
5414 if (argc < 1) {
5415 usage();
5418 if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
5419 return -1;
5422 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5423 if (ret != 0) {
5424 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5425 return -1;
5428 printf("Sequence number:%lld\n", (long long)seqnum);
5430 return 0;
5434 run an eventscript on a node
5436 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5438 TDB_DATA data;
5439 int ret;
5440 int32_t res;
5441 char *errmsg;
5442 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5444 if (argc != 1) {
5445 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5446 return -1;
5449 data.dptr = (unsigned char *)discard_const(argv[0]);
5450 data.dsize = strlen((char *)data.dptr) + 1;
5452 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5454 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5455 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5456 if (ret != 0 || res != 0) {
5457 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5458 talloc_free(tmp_ctx);
5459 return -1;
5461 talloc_free(tmp_ctx);
5462 return 0;
5465 #define DB_VERSION 1
5466 #define MAX_DB_NAME 64
5467 struct db_file_header {
5468 unsigned long version;
5469 time_t timestamp;
5470 unsigned long persistent;
5471 unsigned long size;
5472 const char name[MAX_DB_NAME];
5475 struct backup_data {
5476 struct ctdb_marshall_buffer *records;
5477 uint32_t len;
5478 uint32_t total;
5479 bool traverse_error;
5482 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5484 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5485 struct ctdb_rec_data *rec;
5487 /* add the record */
5488 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5489 if (rec == NULL) {
5490 bd->traverse_error = true;
5491 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5492 return -1;
5494 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5495 if (bd->records == NULL) {
5496 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5497 bd->traverse_error = true;
5498 return -1;
5500 bd->records->count++;
5501 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5502 bd->len += rec->length;
5503 talloc_free(rec);
5505 bd->total++;
5506 return 0;
5510 * backup a database to a file
5512 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5514 const char *db_name;
5515 int ret;
5516 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5517 struct db_file_header dbhdr;
5518 struct ctdb_db_context *ctdb_db;
5519 struct backup_data *bd;
5520 int fh = -1;
5521 int status = -1;
5522 const char *reason = NULL;
5523 uint32_t db_id;
5524 uint8_t flags;
5526 assert_single_node_only();
5528 if (argc != 2) {
5529 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5530 return -1;
5533 if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
5534 return -1;
5537 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5538 db_id, tmp_ctx, &reason);
5539 if (ret != 0) {
5540 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5541 argv[0]));
5542 talloc_free(tmp_ctx);
5543 return -1;
5545 if (reason) {
5546 uint32_t allow_unhealthy = 0;
5548 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5549 "AllowUnhealthyDBRead",
5550 &allow_unhealthy);
5552 if (allow_unhealthy != 1) {
5553 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5554 argv[0], reason));
5556 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5557 allow_unhealthy));
5558 talloc_free(tmp_ctx);
5559 return -1;
5562 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5563 argv[0], argv[0]));
5564 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5565 "tunnable AllowUnhealthyDBRead = %u\n",
5566 allow_unhealthy));
5569 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5570 if (ctdb_db == NULL) {
5571 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5572 talloc_free(tmp_ctx);
5573 return -1;
5577 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5578 if (ret == -1) {
5579 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5580 talloc_free(tmp_ctx);
5581 return -1;
5585 bd = talloc_zero(tmp_ctx, struct backup_data);
5586 if (bd == NULL) {
5587 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5588 talloc_free(tmp_ctx);
5589 return -1;
5592 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5593 if (bd->records == NULL) {
5594 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5595 talloc_free(tmp_ctx);
5596 return -1;
5599 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5600 bd->records->db_id = ctdb_db->db_id;
5601 /* traverse the database collecting all records */
5602 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5603 bd->traverse_error) {
5604 DEBUG(DEBUG_ERR,("Traverse error\n"));
5605 talloc_free(tmp_ctx);
5606 return -1;
5609 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5612 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5613 if (fh == -1) {
5614 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5615 talloc_free(tmp_ctx);
5616 return -1;
5619 ZERO_STRUCT(dbhdr);
5620 dbhdr.version = DB_VERSION;
5621 dbhdr.timestamp = time(NULL);
5622 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5623 dbhdr.size = bd->len;
5624 if (strlen(argv[0]) >= MAX_DB_NAME) {
5625 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5626 goto done;
5628 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
5629 ret = write(fh, &dbhdr, sizeof(dbhdr));
5630 if (ret == -1) {
5631 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5632 goto done;
5634 ret = write(fh, bd->records, bd->len);
5635 if (ret == -1) {
5636 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5637 goto done;
5640 status = 0;
5641 done:
5642 if (fh != -1) {
5643 ret = close(fh);
5644 if (ret == -1) {
5645 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5649 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5651 talloc_free(tmp_ctx);
5652 return status;
5656 * restore a database from a file
5658 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5660 int ret;
5661 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5662 TDB_DATA outdata;
5663 TDB_DATA data;
5664 struct db_file_header dbhdr;
5665 struct ctdb_db_context *ctdb_db;
5666 struct ctdb_node_map *nodemap=NULL;
5667 struct ctdb_vnn_map *vnnmap=NULL;
5668 int i, fh;
5669 struct ctdb_control_wipe_database w;
5670 uint32_t *nodes;
5671 uint32_t generation;
5672 struct tm *tm;
5673 char tbuf[100];
5674 char *dbname;
5676 assert_single_node_only();
5678 if (argc < 1 || argc > 2) {
5679 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5680 return -1;
5683 fh = open(argv[0], O_RDONLY);
5684 if (fh == -1) {
5685 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5686 talloc_free(tmp_ctx);
5687 return -1;
5690 read(fh, &dbhdr, sizeof(dbhdr));
5691 if (dbhdr.version != DB_VERSION) {
5692 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5693 close(fh);
5694 talloc_free(tmp_ctx);
5695 return -1;
5698 dbname = discard_const(dbhdr.name);
5699 if (argc == 2) {
5700 dbname = discard_const(argv[1]);
5703 outdata.dsize = dbhdr.size;
5704 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5705 if (outdata.dptr == NULL) {
5706 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5707 close(fh);
5708 talloc_free(tmp_ctx);
5709 return -1;
5711 read(fh, outdata.dptr, outdata.dsize);
5712 close(fh);
5714 tm = localtime(&dbhdr.timestamp);
5715 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5716 printf("Restoring database '%s' from backup @ %s\n",
5717 dbname, tbuf);
5720 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5721 if (ctdb_db == NULL) {
5722 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5723 talloc_free(tmp_ctx);
5724 return -1;
5727 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5728 if (ret != 0) {
5729 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5730 talloc_free(tmp_ctx);
5731 return ret;
5735 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5736 if (ret != 0) {
5737 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5738 talloc_free(tmp_ctx);
5739 return ret;
5742 /* freeze all nodes */
5743 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5744 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5745 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5746 nodes, i,
5747 TIMELIMIT(),
5748 false, tdb_null,
5749 NULL, NULL,
5750 NULL) != 0) {
5751 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5752 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5753 talloc_free(tmp_ctx);
5754 return -1;
5758 generation = vnnmap->generation;
5759 data.dptr = (void *)&generation;
5760 data.dsize = sizeof(generation);
5762 /* start a cluster wide transaction */
5763 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5764 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5765 nodes, 0,
5766 TIMELIMIT(), false, data,
5767 NULL, NULL,
5768 NULL) != 0) {
5769 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5770 return -1;
5774 w.db_id = ctdb_db->db_id;
5775 w.transaction_id = generation;
5777 data.dptr = (void *)&w;
5778 data.dsize = sizeof(w);
5780 /* wipe all the remote databases. */
5781 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5782 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5783 nodes, 0,
5784 TIMELIMIT(), false, data,
5785 NULL, NULL,
5786 NULL) != 0) {
5787 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5788 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5789 talloc_free(tmp_ctx);
5790 return -1;
5793 /* push the database */
5794 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5795 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5796 nodes, 0,
5797 TIMELIMIT(), false, outdata,
5798 NULL, NULL,
5799 NULL) != 0) {
5800 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5801 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5802 talloc_free(tmp_ctx);
5803 return -1;
5806 data.dptr = (void *)&ctdb_db->db_id;
5807 data.dsize = sizeof(ctdb_db->db_id);
5809 /* mark the database as healthy */
5810 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5811 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5812 nodes, 0,
5813 TIMELIMIT(), false, data,
5814 NULL, NULL,
5815 NULL) != 0) {
5816 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5817 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5818 talloc_free(tmp_ctx);
5819 return -1;
5822 data.dptr = (void *)&generation;
5823 data.dsize = sizeof(generation);
5825 /* commit all the changes */
5826 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5827 nodes, 0,
5828 TIMELIMIT(), false, data,
5829 NULL, NULL,
5830 NULL) != 0) {
5831 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5832 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5833 talloc_free(tmp_ctx);
5834 return -1;
5838 /* thaw all nodes */
5839 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5840 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5841 nodes, 0,
5842 TIMELIMIT(),
5843 false, tdb_null,
5844 NULL, NULL,
5845 NULL) != 0) {
5846 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5847 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5848 talloc_free(tmp_ctx);
5849 return -1;
5853 talloc_free(tmp_ctx);
5854 return 0;
5858 * dump a database backup from a file
5860 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5862 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5863 TDB_DATA outdata;
5864 struct db_file_header dbhdr;
5865 int i, fh;
5866 struct tm *tm;
5867 char tbuf[100];
5868 struct ctdb_rec_data *rec = NULL;
5869 struct ctdb_marshall_buffer *m;
5870 struct ctdb_dump_db_context c;
5872 assert_single_node_only();
5874 if (argc != 1) {
5875 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5876 return -1;
5879 fh = open(argv[0], O_RDONLY);
5880 if (fh == -1) {
5881 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5882 talloc_free(tmp_ctx);
5883 return -1;
5886 read(fh, &dbhdr, sizeof(dbhdr));
5887 if (dbhdr.version != DB_VERSION) {
5888 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5889 close(fh);
5890 talloc_free(tmp_ctx);
5891 return -1;
5894 outdata.dsize = dbhdr.size;
5895 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5896 if (outdata.dptr == NULL) {
5897 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5898 close(fh);
5899 talloc_free(tmp_ctx);
5900 return -1;
5902 read(fh, outdata.dptr, outdata.dsize);
5903 close(fh);
5904 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5906 tm = localtime(&dbhdr.timestamp);
5907 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5908 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5909 dbhdr.name, m->db_id, tbuf);
5911 ZERO_STRUCT(c);
5912 c.f = stdout;
5913 c.printemptyrecords = (bool)options.printemptyrecords;
5914 c.printdatasize = (bool)options.printdatasize;
5915 c.printlmaster = false;
5916 c.printhash = (bool)options.printhash;
5917 c.printrecordflags = (bool)options.printrecordflags;
5919 for (i=0; i < m->count; i++) {
5920 uint32_t reqid = 0;
5921 TDB_DATA key, data;
5923 /* we do not want the header splitted, so we pass NULL*/
5924 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5925 NULL, &key, &data);
5927 ctdb_dumpdb_record(ctdb, key, data, &c);
5930 printf("Dumped %d records\n", i);
5931 talloc_free(tmp_ctx);
5932 return 0;
5936 * wipe a database from a file
5938 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5939 const char **argv)
5941 const char *db_name;
5942 int ret;
5943 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5944 TDB_DATA data;
5945 struct ctdb_db_context *ctdb_db;
5946 struct ctdb_node_map *nodemap = NULL;
5947 struct ctdb_vnn_map *vnnmap = NULL;
5948 int i;
5949 struct ctdb_control_wipe_database w;
5950 uint32_t *nodes;
5951 uint32_t generation;
5952 uint8_t flags;
5954 assert_single_node_only();
5956 if (argc != 1) {
5957 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5958 return -1;
5961 if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
5962 return -1;
5965 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5966 if (ctdb_db == NULL) {
5967 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5968 argv[0]));
5969 talloc_free(tmp_ctx);
5970 return -1;
5973 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5974 &nodemap);
5975 if (ret != 0) {
5976 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5977 options.pnn));
5978 talloc_free(tmp_ctx);
5979 return ret;
5982 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5983 &vnnmap);
5984 if (ret != 0) {
5985 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5986 options.pnn));
5987 talloc_free(tmp_ctx);
5988 return ret;
5991 /* freeze all nodes */
5992 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5993 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5994 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5995 nodes, i,
5996 TIMELIMIT(),
5997 false, tdb_null,
5998 NULL, NULL,
5999 NULL);
6000 if (ret != 0) {
6001 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
6002 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
6003 CTDB_RECOVERY_ACTIVE);
6004 talloc_free(tmp_ctx);
6005 return -1;
6009 generation = vnnmap->generation;
6010 data.dptr = (void *)&generation;
6011 data.dsize = sizeof(generation);
6013 /* start a cluster wide transaction */
6014 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6015 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
6016 nodes, 0,
6017 TIMELIMIT(), false, data,
6018 NULL, NULL,
6019 NULL);
6020 if (ret!= 0) {
6021 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
6022 "transactions.\n"));
6023 return -1;
6026 w.db_id = ctdb_db->db_id;
6027 w.transaction_id = generation;
6029 data.dptr = (void *)&w;
6030 data.dsize = sizeof(w);
6032 /* wipe all the remote databases. */
6033 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6034 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
6035 nodes, 0,
6036 TIMELIMIT(), false, data,
6037 NULL, NULL,
6038 NULL) != 0) {
6039 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
6040 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6041 talloc_free(tmp_ctx);
6042 return -1;
6045 data.dptr = (void *)&ctdb_db->db_id;
6046 data.dsize = sizeof(ctdb_db->db_id);
6048 /* mark the database as healthy */
6049 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6050 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
6051 nodes, 0,
6052 TIMELIMIT(), false, data,
6053 NULL, NULL,
6054 NULL) != 0) {
6055 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
6056 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6057 talloc_free(tmp_ctx);
6058 return -1;
6061 data.dptr = (void *)&generation;
6062 data.dsize = sizeof(generation);
6064 /* commit all the changes */
6065 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
6066 nodes, 0,
6067 TIMELIMIT(), false, data,
6068 NULL, NULL,
6069 NULL) != 0) {
6070 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
6071 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6072 talloc_free(tmp_ctx);
6073 return -1;
6076 /* thaw all nodes */
6077 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
6078 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
6079 nodes, 0,
6080 TIMELIMIT(),
6081 false, tdb_null,
6082 NULL, NULL,
6083 NULL) != 0) {
6084 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
6085 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
6086 talloc_free(tmp_ctx);
6087 return -1;
6090 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
6092 talloc_free(tmp_ctx);
6093 return 0;
6097 dump memory usage
6099 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6101 TDB_DATA data;
6102 int ret;
6103 int32_t res;
6104 char *errmsg;
6105 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
6106 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
6107 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
6108 if (ret != 0 || res != 0) {
6109 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
6110 talloc_free(tmp_ctx);
6111 return -1;
6113 write(1, data.dptr, data.dsize);
6114 talloc_free(tmp_ctx);
6115 return 0;
6119 handler for memory dumps
6121 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
6122 TDB_DATA data, void *private_data)
6124 write(1, data.dptr, data.dsize);
6125 exit(0);
6129 dump memory usage on the recovery daemon
6131 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
6133 int ret;
6134 TDB_DATA data;
6135 struct srvid_request rd;
6137 rd.pnn = ctdb_get_pnn(ctdb);
6138 rd.srvid = getpid();
6140 /* register a message port for receiveing the reply so that we
6141 can receive the reply
6143 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
6146 data.dptr = (uint8_t *)&rd;
6147 data.dsize = sizeof(rd);
6149 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
6150 if (ret != 0) {
6151 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6152 return -1;
6155 /* this loop will terminate when we have received the reply */
6156 while (1) {
6157 event_loop_once(ctdb->ev);
6160 return 0;
6164 send a message to a srvid
6166 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
6168 unsigned long srvid;
6169 int ret;
6170 TDB_DATA data;
6172 if (argc < 2) {
6173 usage();
6176 srvid = strtoul(argv[0], NULL, 0);
6178 data.dptr = (uint8_t *)discard_const(argv[1]);
6179 data.dsize= strlen(argv[1]);
6181 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
6182 if (ret != 0) {
6183 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
6184 return -1;
6187 return 0;
6191 handler for msglisten
6193 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
6194 TDB_DATA data, void *private_data)
6196 int i;
6198 printf("Message received: ");
6199 for (i=0;i<data.dsize;i++) {
6200 printf("%c", data.dptr[i]);
6202 printf("\n");
6206 listen for messages on a messageport
6208 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
6210 uint64_t srvid;
6212 srvid = getpid();
6214 /* register a message port and listen for messages
6216 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
6217 printf("Listening for messages on srvid:%d\n", (int)srvid);
6219 while (1) {
6220 event_loop_once(ctdb->ev);
6223 return 0;
6227 list all nodes in the cluster
6228 we parse the nodes file directly
6230 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
6232 TALLOC_CTX *mem_ctx = talloc_new(NULL);
6233 struct pnn_node *pnn_nodes;
6234 struct pnn_node *pnn_node;
6236 assert_single_node_only();
6238 pnn_nodes = read_nodes_file(mem_ctx);
6239 if (pnn_nodes == NULL) {
6240 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6241 talloc_free(mem_ctx);
6242 return -1;
6245 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6246 const char *addr = ctdb_addr_to_str(&pnn_node->addr);
6247 if (options.machinereadable){
6248 printf(":%d:%s:\n", pnn_node->pnn, addr);
6249 } else {
6250 printf("%s\n", addr);
6253 talloc_free(mem_ctx);
6255 return 0;
6259 reload the nodes file on the local node
6261 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6263 int i, ret;
6264 int mypnn;
6265 struct ctdb_node_map *nodemap=NULL;
6267 assert_single_node_only();
6269 mypnn = ctdb_get_pnn(ctdb);
6271 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6272 if (ret != 0) {
6273 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6274 return ret;
6277 /* reload the nodes file on all remote nodes */
6278 for (i=0;i<nodemap->num;i++) {
6279 if (nodemap->nodes[i].pnn == mypnn) {
6280 continue;
6282 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6283 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6284 nodemap->nodes[i].pnn);
6285 if (ret != 0) {
6286 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6290 /* reload the nodes file on the local node */
6291 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6292 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6293 if (ret != 0) {
6294 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6297 /* initiate a recovery */
6298 control_recover(ctdb, argc, argv);
6300 return 0;
6304 static const struct {
6305 const char *name;
6306 int (*fn)(struct ctdb_context *, int, const char **);
6307 bool auto_all;
6308 bool without_daemon; /* can be run without daemon running ? */
6309 const char *msg;
6310 const char *args;
6311 } ctdb_commands[] = {
6312 { "version", control_version, true, true, "show version of ctdb" },
6313 { "status", control_status, true, false, "show node status" },
6314 { "uptime", control_uptime, true, false, "show node uptime" },
6315 { "ping", control_ping, true, false, "ping all nodes" },
6316 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6317 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6318 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6319 { "listvars", control_listvars, true, false, "list tunable variables"},
6320 { "statistics", control_statistics, false, false, "show statistics" },
6321 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6322 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6323 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6324 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6325 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6326 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6327 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6328 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6329 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6330 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6331 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6332 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6333 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6334 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6335 { "lvs", control_lvs, true, false, "show lvs configuration" },
6336 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6337 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6338 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6339 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6340 { "getdebug", control_getdebug, true, false, "get debug level" },
6341 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6342 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6343 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6344 { "detach", control_detach, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6345 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6346 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6347 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6348 { "disable", control_disable, true, false, "disable a nodes public IP" },
6349 { "enable", control_enable, true, false, "enable a nodes public IP" },
6350 { "stop", control_stop, true, false, "stop a node" },
6351 { "continue", control_continue, true, false, "re-start a stopped node" },
6352 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime>"},
6353 { "unban", control_unban, true, false, "unban a node" },
6354 { "showban", control_showban, true, false, "show ban information"},
6355 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6356 { "recover", control_recover, true, false, "force recovery" },
6357 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6358 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6359 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6360 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6361 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6362 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6363 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6364 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6365 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6367 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6369 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6370 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6371 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6372 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6373 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6374 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6375 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6376 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6377 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6378 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6379 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6380 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6381 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6382 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6383 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6384 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6385 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6386 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6387 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6388 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6389 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6390 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6391 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6392 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6393 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6394 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6395 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6396 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6397 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6398 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6399 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6400 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6401 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6402 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6403 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6404 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6405 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6406 { "ptrans", control_ptrans, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6407 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6408 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6409 { "readkey", control_readkey, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6410 { "writekey", control_writekey, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6411 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6412 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6413 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6414 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6415 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6416 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6417 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6421 show usage message
6423 static void usage(void)
6425 int i;
6426 printf(
6427 "Usage: ctdb [options] <control>\n" \
6428 "Options:\n" \
6429 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6430 " -Y generate machinereadable output\n"
6431 " -v generate verbose output\n"
6432 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6433 printf("Controls:\n");
6434 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6435 printf(" %-15s %-27s %s\n",
6436 ctdb_commands[i].name,
6437 ctdb_commands[i].args?ctdb_commands[i].args:"",
6438 ctdb_commands[i].msg);
6440 exit(1);
6444 static void ctdb_alarm(int sig)
6446 printf("Maximum runtime exceeded - exiting\n");
6447 _exit(ERR_TIMEOUT);
6451 main program
6453 int main(int argc, const char *argv[])
6455 struct ctdb_context *ctdb;
6456 char *nodestring = NULL;
6457 struct poptOption popt_options[] = {
6458 POPT_AUTOHELP
6459 POPT_CTDB_CMDLINE
6460 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6461 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6462 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6463 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6464 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6465 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6466 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6467 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6468 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6469 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6470 POPT_TABLEEND
6472 int opt;
6473 const char **extra_argv;
6474 int extra_argc = 0;
6475 int ret=-1, i;
6476 poptContext pc;
6477 struct event_context *ev;
6478 const char *control;
6480 setlinebuf(stdout);
6482 /* set some defaults */
6483 options.maxruntime = 0;
6484 options.timelimit = 10;
6485 options.pnn = CTDB_CURRENT_NODE;
6487 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6489 while ((opt = poptGetNextOpt(pc)) != -1) {
6490 switch (opt) {
6491 default:
6492 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6493 poptBadOption(pc, 0), poptStrerror(opt)));
6494 exit(1);
6498 /* setup the remaining options for the main program to use */
6499 extra_argv = poptGetArgs(pc);
6500 if (extra_argv) {
6501 extra_argv++;
6502 while (extra_argv[extra_argc]) extra_argc++;
6505 if (extra_argc < 1) {
6506 usage();
6509 if (options.maxruntime == 0) {
6510 const char *ctdb_timeout;
6511 ctdb_timeout = getenv("CTDB_TIMEOUT");
6512 if (ctdb_timeout != NULL) {
6513 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6514 } else {
6515 /* default timeout is 120 seconds */
6516 options.maxruntime = 120;
6520 signal(SIGALRM, ctdb_alarm);
6521 alarm(options.maxruntime);
6523 control = extra_argv[0];
6525 /* Default value for CTDB_BASE - don't override */
6526 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
6528 ev = event_context_init(NULL);
6529 if (!ev) {
6530 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6531 exit(1);
6534 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6535 if (strcmp(control, ctdb_commands[i].name) == 0) {
6536 break;
6540 if (i == ARRAY_SIZE(ctdb_commands)) {
6541 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6542 exit(1);
6545 if (ctdb_commands[i].without_daemon == true) {
6546 if (nodestring != NULL) {
6547 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6548 exit(1);
6550 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6553 /* initialise ctdb */
6554 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6556 if (ctdb == NULL) {
6557 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6558 exit(1);
6561 /* setup the node number(s) to contact */
6562 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6563 &options.nodes, &options.pnn)) {
6564 usage();
6567 if (options.pnn == CTDB_CURRENT_NODE) {
6568 options.pnn = options.nodes[0];
6571 if (ctdb_commands[i].auto_all &&
6572 ((options.pnn == CTDB_BROADCAST_ALL) ||
6573 (options.pnn == CTDB_MULTICAST))) {
6574 int j;
6576 ret = 0;
6577 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6578 options.pnn = options.nodes[j];
6579 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6581 } else {
6582 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6585 talloc_free(ctdb);
6586 talloc_free(ev);
6587 (void)poptFreeContext(pc);
6589 return ret;