tools/ctdb: Fix help messages for ctdb commands
[Samba.git] / ctdb / tools / ctdb.c
blobcb97ef9775f7b863c739e1ec259d6475cd92d430
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.h"
30 #include "../include/ctdb_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.h"
33 #include "db_wrap.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 struct ctdb_connection *ctdb_connection;
41 static void usage(void);
43 static struct {
44 int timelimit;
45 uint32_t pnn;
46 uint32_t *nodes;
47 int machinereadable;
48 int verbose;
49 int maxruntime;
50 int printemptyrecords;
51 int printdatasize;
52 int printlmaster;
53 int printhash;
54 int printrecordflags;
55 } options;
57 #define LONGTIMEOUT options.timelimit*10
59 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
60 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
62 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
64 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
65 return 0;
68 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
69 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
70 "Out of memory in " __location__ )); \
71 abort(); \
72 }} while (0)
74 static uint32_t getpnn(struct ctdb_context *ctdb)
76 if ((options.pnn == CTDB_BROADCAST_ALL) ||
77 (options.pnn == CTDB_MULTICAST)) {
78 DEBUG(DEBUG_ERR,
79 ("Cannot get PNN for node %u\n", options.pnn));
80 exit(1);
83 if (options.pnn == CTDB_CURRENT_NODE) {
84 return ctdb_get_pnn(ctdb);
85 } else {
86 return options.pnn;
90 static void assert_single_node_only(void)
92 if ((options.pnn == CTDB_BROADCAST_ALL) ||
93 (options.pnn == CTDB_MULTICAST)) {
94 DEBUG(DEBUG_ERR,
95 ("This control can not be applied to multiple PNNs\n"));
96 exit(1);
100 /* Pretty print the flags to a static buffer in human-readable format.
101 * This never returns NULL!
103 static const char *pretty_print_flags(uint32_t flags)
105 int j;
106 static const struct {
107 uint32_t flag;
108 const char *name;
109 } flag_names[] = {
110 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
111 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
112 { NODE_FLAGS_BANNED, "BANNED" },
113 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
114 { NODE_FLAGS_DELETED, "DELETED" },
115 { NODE_FLAGS_STOPPED, "STOPPED" },
116 { NODE_FLAGS_INACTIVE, "INACTIVE" },
118 static char flags_str[512]; /* Big enough to contain all flag names */
120 flags_str[0] = '\0';
121 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
122 if (flags & flag_names[j].flag) {
123 if (flags_str[0] == '\0') {
124 (void) strcpy(flags_str, flag_names[j].name);
125 } else {
126 (void) strcat(flags_str, "|");
127 (void) strcat(flags_str, flag_names[j].name);
131 if (flags_str[0] == '\0') {
132 (void) strcpy(flags_str, "OK");
135 return flags_str;
138 static int h2i(char h)
140 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
141 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
142 return h - '0';
145 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
147 int i, len;
148 TDB_DATA key = {NULL, 0};
150 len = strlen(str);
151 if (len & 0x01) {
152 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
153 return key;
156 key.dsize = len>>1;
157 key.dptr = talloc_size(mem_ctx, key.dsize);
159 for (i=0; i < len/2; i++) {
160 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
162 return key;
165 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
166 * that are disconnected or deleted. If dd_ok is true those nodes are
167 * included in the output list of nodes. If dd_ok is false, those
168 * nodes are filtered from the "all" case and cause an error if
169 * explicitly specified.
171 static bool parse_nodestring(struct ctdb_context *ctdb,
172 const char * nodestring,
173 uint32_t current_pnn,
174 bool dd_ok,
175 uint32_t **nodes,
176 uint32_t *pnn_mode)
178 int n;
179 uint32_t i;
180 struct ctdb_node_map *nodemap;
181 bool ret = true;
183 *nodes = NULL;
185 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
186 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
187 exit(10);
190 if (nodestring != NULL) {
191 *nodes = talloc_array(ctdb, uint32_t, 0);
192 CTDB_NOMEM_ABORT(*nodes);
194 n = 0;
196 if (strcmp(nodestring, "all") == 0) {
197 *pnn_mode = CTDB_BROADCAST_ALL;
199 /* all */
200 for (i = 0; i < nodemap->num; i++) {
201 if ((nodemap->nodes[i].flags &
202 (NODE_FLAGS_DISCONNECTED |
203 NODE_FLAGS_DELETED)) && !dd_ok) {
204 continue;
206 *nodes = talloc_realloc(ctdb, *nodes,
207 uint32_t, n+1);
208 CTDB_NOMEM_ABORT(*nodes);
209 (*nodes)[n] = i;
210 n++;
212 } else {
213 /* x{,y...} */
214 char *ns, *tok;
216 ns = talloc_strdup(ctdb, nodestring);
217 tok = strtok(ns, ",");
218 while (tok != NULL) {
219 uint32_t pnn;
220 i = (uint32_t)strtoul(tok, NULL, 0);
221 if (i >= nodemap->num) {
222 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
223 exit(ERR_NONODE);
225 if ((nodemap->nodes[i].flags &
226 (NODE_FLAGS_DISCONNECTED |
227 NODE_FLAGS_DELETED)) && !dd_ok) {
228 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
229 exit(ERR_DISNODE);
231 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
232 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
233 exit(10);
236 *nodes = talloc_realloc(ctdb, *nodes,
237 uint32_t, n+1);
238 CTDB_NOMEM_ABORT(*nodes);
240 (*nodes)[n] = i;
241 n++;
243 tok = strtok(NULL, ",");
245 talloc_free(ns);
247 if (n == 1) {
248 *pnn_mode = (*nodes)[0];
249 } else {
250 *pnn_mode = CTDB_MULTICAST;
253 } else {
254 /* default - no nodes specified */
255 *nodes = talloc_array(ctdb, uint32_t, 1);
256 CTDB_NOMEM_ABORT(*nodes);
257 *pnn_mode = CTDB_CURRENT_NODE;
259 if (!ctdb_getpnn(ctdb_connection, current_pnn,
260 &((*nodes)[0]))) {
261 ret = false;
265 ctdb_free_nodemap(nodemap);
267 return ret;
271 check if a database exists
273 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg, uint32_t *dbid, uint8_t *flags)
275 int i, ret;
276 struct ctdb_dbid_map *dbmap=NULL;
277 bool dbid_given = false, found = false;
278 uint32_t id;
279 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
281 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
282 if (ret != 0) {
283 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
284 goto fail;
287 if (strncmp(dbarg, "0x", 2) == 0) {
288 id = strtoul(dbarg, NULL, 0);
289 dbid_given = true;
292 for(i=0; i<dbmap->num; i++) {
293 if (dbid_given) {
294 if (id == dbmap->dbs[i].dbid) {
295 found = true;
296 break;
298 } else {
299 const char *name;
300 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
301 if (ret != 0) {
302 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
303 goto fail;
306 if (strcmp(name, dbarg) == 0) {
307 id = dbmap->dbs[i].dbid;
308 found = true;
309 break;
314 if (found) {
315 if (dbid) *dbid = id;
316 if (flags) *flags = dbmap->dbs[i].flags;
317 } else {
318 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
321 fail:
322 talloc_free(tmp_ctx);
323 return found;
327 see if a process exists
329 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
331 uint32_t pnn, pid;
332 int ret;
333 if (argc < 1) {
334 usage();
337 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
338 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
339 return -1;
342 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
343 if (ret == 0) {
344 printf("%u:%u exists\n", pnn, pid);
345 } else {
346 printf("%u:%u does not exist\n", pnn, pid);
348 return ret;
352 display statistics structure
354 static void show_statistics(struct ctdb_statistics *s, int show_header)
356 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
357 int i;
358 const char *prefix=NULL;
359 int preflen=0;
360 int tmp, days, hours, minutes, seconds;
361 const struct {
362 const char *name;
363 uint32_t offset;
364 } fields[] = {
365 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
366 STATISTICS_FIELD(num_clients),
367 STATISTICS_FIELD(frozen),
368 STATISTICS_FIELD(recovering),
369 STATISTICS_FIELD(num_recoveries),
370 STATISTICS_FIELD(client_packets_sent),
371 STATISTICS_FIELD(client_packets_recv),
372 STATISTICS_FIELD(node_packets_sent),
373 STATISTICS_FIELD(node_packets_recv),
374 STATISTICS_FIELD(keepalive_packets_sent),
375 STATISTICS_FIELD(keepalive_packets_recv),
376 STATISTICS_FIELD(node.req_call),
377 STATISTICS_FIELD(node.reply_call),
378 STATISTICS_FIELD(node.req_dmaster),
379 STATISTICS_FIELD(node.reply_dmaster),
380 STATISTICS_FIELD(node.reply_error),
381 STATISTICS_FIELD(node.req_message),
382 STATISTICS_FIELD(node.req_control),
383 STATISTICS_FIELD(node.reply_control),
384 STATISTICS_FIELD(client.req_call),
385 STATISTICS_FIELD(client.req_message),
386 STATISTICS_FIELD(client.req_control),
387 STATISTICS_FIELD(timeouts.call),
388 STATISTICS_FIELD(timeouts.control),
389 STATISTICS_FIELD(timeouts.traverse),
390 STATISTICS_FIELD(locks.num_calls),
391 STATISTICS_FIELD(locks.num_current),
392 STATISTICS_FIELD(locks.num_pending),
393 STATISTICS_FIELD(locks.num_failed),
394 STATISTICS_FIELD(total_calls),
395 STATISTICS_FIELD(pending_calls),
396 STATISTICS_FIELD(childwrite_calls),
397 STATISTICS_FIELD(pending_childwrite_calls),
398 STATISTICS_FIELD(memory_used),
399 STATISTICS_FIELD(max_hop_count),
400 STATISTICS_FIELD(total_ro_delegations),
401 STATISTICS_FIELD(total_ro_revokes),
404 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
405 seconds = tmp%60;
406 tmp /= 60;
407 minutes = tmp%60;
408 tmp /= 60;
409 hours = tmp%24;
410 tmp /= 24;
411 days = tmp;
413 if (options.machinereadable){
414 if (show_header) {
415 printf("CTDB version:");
416 printf("Current time of statistics:");
417 printf("Statistics collected since:");
418 for (i=0;i<ARRAY_SIZE(fields);i++) {
419 printf("%s:", fields[i].name);
421 printf("num_reclock_ctdbd_latency:");
422 printf("min_reclock_ctdbd_latency:");
423 printf("avg_reclock_ctdbd_latency:");
424 printf("max_reclock_ctdbd_latency:");
426 printf("num_reclock_recd_latency:");
427 printf("min_reclock_recd_latency:");
428 printf("avg_reclock_recd_latency:");
429 printf("max_reclock_recd_latency:");
431 printf("num_call_latency:");
432 printf("min_call_latency:");
433 printf("avg_call_latency:");
434 printf("max_call_latency:");
436 printf("num_lockwait_latency:");
437 printf("min_lockwait_latency:");
438 printf("avg_lockwait_latency:");
439 printf("max_lockwait_latency:");
441 printf("num_childwrite_latency:");
442 printf("min_childwrite_latency:");
443 printf("avg_childwrite_latency:");
444 printf("max_childwrite_latency:");
445 printf("\n");
447 printf("%d:", CTDB_VERSION);
448 printf("%d:", (int)s->statistics_current_time.tv_sec);
449 printf("%d:", (int)s->statistics_start_time.tv_sec);
450 for (i=0;i<ARRAY_SIZE(fields);i++) {
451 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
453 printf("%d:", s->reclock.ctdbd.num);
454 printf("%.6f:", s->reclock.ctdbd.min);
455 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
456 printf("%.6f:", s->reclock.ctdbd.max);
458 printf("%d:", s->reclock.recd.num);
459 printf("%.6f:", s->reclock.recd.min);
460 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
461 printf("%.6f:", s->reclock.recd.max);
463 printf("%d:", s->call_latency.num);
464 printf("%.6f:", s->call_latency.min);
465 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
466 printf("%.6f:", s->call_latency.max);
468 printf("%d:", s->childwrite_latency.num);
469 printf("%.6f:", s->childwrite_latency.min);
470 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
471 printf("%.6f:", s->childwrite_latency.max);
472 printf("\n");
473 } else {
474 printf("CTDB version %u\n", CTDB_VERSION);
475 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
476 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
478 for (i=0;i<ARRAY_SIZE(fields);i++) {
479 if (strchr(fields[i].name, '.')) {
480 preflen = strcspn(fields[i].name, ".")+1;
481 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
482 prefix = fields[i].name;
483 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
485 } else {
486 preflen = 0;
488 printf(" %*s%-22s%*s%10u\n",
489 preflen?4:0, "",
490 fields[i].name+preflen,
491 preflen?0:4, "",
492 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
494 printf(" hop_count_buckets:");
495 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
496 printf(" %d", s->hop_count_bucket[i]);
498 printf("\n");
499 printf(" lock_buckets:");
500 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
501 printf(" %d", s->locks.buckets[i]);
503 printf("\n");
504 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);
506 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);
508 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);
510 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);
511 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);
514 talloc_free(tmp_ctx);
518 display remote ctdb statistics combined from all nodes
520 static int control_statistics_all(struct ctdb_context *ctdb)
522 int ret, i;
523 struct ctdb_statistics statistics;
524 uint32_t *nodes;
525 uint32_t num_nodes;
527 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
528 CTDB_NO_MEMORY(ctdb, nodes);
530 ZERO_STRUCT(statistics);
532 for (i=0;i<num_nodes;i++) {
533 struct ctdb_statistics s1;
534 int j;
535 uint32_t *v1 = (uint32_t *)&s1;
536 uint32_t *v2 = (uint32_t *)&statistics;
537 uint32_t num_ints =
538 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
539 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
540 if (ret != 0) {
541 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
542 return ret;
544 for (j=0;j<num_ints;j++) {
545 v2[j] += v1[j];
547 statistics.max_hop_count =
548 MAX(statistics.max_hop_count, s1.max_hop_count);
549 statistics.call_latency.max =
550 MAX(statistics.call_latency.max, s1.call_latency.max);
552 talloc_free(nodes);
553 printf("Gathered statistics for %u nodes\n", num_nodes);
554 show_statistics(&statistics, 1);
555 return 0;
559 display remote ctdb statistics
561 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
563 int ret;
564 struct ctdb_statistics statistics;
566 if (options.pnn == CTDB_BROADCAST_ALL) {
567 return control_statistics_all(ctdb);
570 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
571 if (ret != 0) {
572 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
573 return ret;
575 show_statistics(&statistics, 1);
576 return 0;
581 reset remote ctdb statistics
583 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
585 int ret;
587 ret = ctdb_statistics_reset(ctdb, options.pnn);
588 if (ret != 0) {
589 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
590 return ret;
592 return 0;
597 display remote ctdb rolling statistics
599 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
601 int ret;
602 struct ctdb_statistics_wire *stats;
603 int i, num_records = -1;
605 assert_single_node_only();
607 if (argc ==1) {
608 num_records = atoi(argv[0]) - 1;
611 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
612 if (ret != 0) {
613 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
614 return ret;
616 for (i=0;i<stats->num;i++) {
617 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
618 continue;
620 show_statistics(&stats->stats[i], i==0);
621 if (i == num_records) {
622 break;
625 return 0;
630 display remote ctdb db statistics
632 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
634 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
635 struct ctdb_db_statistics *dbstat;
636 int i;
637 uint32_t db_id;
638 int num_hot_keys;
640 if (argc < 1) {
641 usage();
644 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
645 return -1;
648 if (!ctdb_getdbstat(ctdb_connection, options.pnn, db_id, &dbstat)) {
649 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
650 talloc_free(tmp_ctx);
651 return -1;
654 printf("DB Statistics: %s\n", argv[0]);
655 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
656 dbstat->db_ro_delegations);
657 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
658 dbstat->db_ro_delegations);
659 printf(" %s\n", "locks");
660 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
661 dbstat->locks.num_calls);
662 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
663 dbstat->locks.num_failed);
664 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
665 dbstat->locks.num_current);
666 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
667 dbstat->locks.num_pending);
668 printf(" %s", "hop_count_buckets:");
669 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
670 printf(" %d", dbstat->hop_count_bucket[i]);
672 printf("\n");
673 printf(" %s", "lock_buckets:");
674 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
675 printf(" %d", dbstat->locks.buckets[i]);
677 printf("\n");
678 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
679 "locks_latency MIN/AVG/MAX",
680 dbstat->locks.latency.min,
681 (dbstat->locks.latency.num ?
682 dbstat->locks.latency.total /dbstat->locks.latency.num :
683 0.0),
684 dbstat->locks.latency.max,
685 dbstat->locks.latency.num);
686 num_hot_keys = 0;
687 for (i=0; i<dbstat->num_hot_keys; i++) {
688 if (dbstat->hot_keys[i].count > 0) {
689 num_hot_keys++;
692 dbstat->num_hot_keys = num_hot_keys;
694 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
695 for (i = 0; i < dbstat->num_hot_keys; i++) {
696 int j;
697 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
698 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
699 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
701 printf("\n");
704 ctdb_free_dbstat(dbstat);
705 return 0;
709 display uptime of remote node
711 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
713 int ret;
714 struct ctdb_uptime *uptime = NULL;
715 int tmp, days, hours, minutes, seconds;
717 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
718 if (ret != 0) {
719 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
720 return ret;
723 if (options.machinereadable){
724 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
725 printf(":%u:%u:%u:%lf\n",
726 (unsigned int)uptime->current_time.tv_sec,
727 (unsigned int)uptime->ctdbd_start_time.tv_sec,
728 (unsigned int)uptime->last_recovery_finished.tv_sec,
729 timeval_delta(&uptime->last_recovery_finished,
730 &uptime->last_recovery_started)
732 return 0;
735 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
737 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
738 seconds = tmp%60;
739 tmp /= 60;
740 minutes = tmp%60;
741 tmp /= 60;
742 hours = tmp%24;
743 tmp /= 24;
744 days = tmp;
745 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
747 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
748 seconds = tmp%60;
749 tmp /= 60;
750 minutes = tmp%60;
751 tmp /= 60;
752 hours = tmp%24;
753 tmp /= 24;
754 days = tmp;
755 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
757 printf("Duration of last recovery/failover: %lf seconds\n",
758 timeval_delta(&uptime->last_recovery_finished,
759 &uptime->last_recovery_started));
761 return 0;
765 show the PNN of the current node
767 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
769 uint32_t mypnn;
771 mypnn = getpnn(ctdb);
773 printf("PNN:%d\n", mypnn);
774 return 0;
778 struct pnn_node {
779 struct pnn_node *next;
780 const char *addr;
781 int pnn;
784 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
786 const char *nodes_list;
787 int nlines;
788 char **lines;
789 int i, pnn;
790 struct pnn_node *pnn_nodes = NULL;
791 struct pnn_node *pnn_node;
792 struct pnn_node *tmp_node;
794 /* read the nodes file */
795 nodes_list = getenv("CTDB_NODES");
796 if (nodes_list == NULL) {
797 nodes_list = "/etc/ctdb/nodes";
799 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
800 if (lines == NULL) {
801 return NULL;
803 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
804 nlines--;
806 for (i=0, pnn=0; i<nlines; i++) {
807 char *node;
809 node = lines[i];
810 /* strip leading spaces */
811 while((*node == ' ') || (*node == '\t')) {
812 node++;
814 if (*node == '#') {
815 pnn++;
816 continue;
818 if (strcmp(node, "") == 0) {
819 continue;
821 pnn_node = talloc(mem_ctx, struct pnn_node);
822 pnn_node->pnn = pnn++;
823 pnn_node->addr = talloc_strdup(pnn_node, node);
824 pnn_node->next = pnn_nodes;
825 pnn_nodes = pnn_node;
828 /* swap them around so we return them in incrementing order */
829 pnn_node = pnn_nodes;
830 pnn_nodes = NULL;
831 while (pnn_node) {
832 tmp_node = pnn_node;
833 pnn_node = pnn_node->next;
835 tmp_node->next = pnn_nodes;
836 pnn_nodes = tmp_node;
839 return pnn_nodes;
843 show the PNN of the current node
844 discover the pnn by loading the nodes file and try to bind to all
845 addresses one at a time until the ip address is found.
847 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
849 TALLOC_CTX *mem_ctx = talloc_new(NULL);
850 struct pnn_node *pnn_nodes;
851 struct pnn_node *pnn_node;
853 assert_single_node_only();
855 pnn_nodes = read_nodes_file(mem_ctx);
856 if (pnn_nodes == NULL) {
857 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
858 talloc_free(mem_ctx);
859 return -1;
862 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
863 ctdb_sock_addr addr;
865 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
866 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
867 talloc_free(mem_ctx);
868 return -1;
871 if (ctdb_sys_have_ip(&addr)) {
872 printf("PNN:%d\n", pnn_node->pnn);
873 talloc_free(mem_ctx);
874 return 0;
878 printf("Failed to detect which PNN this node is\n");
879 talloc_free(mem_ctx);
880 return -1;
883 /* Helpers for ctdb status
885 static bool is_partially_online(struct ctdb_node_and_flags *node)
887 int j;
888 bool ret = false;
890 if (node->flags == 0) {
891 struct ctdb_ifaces_list *ifaces;
893 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
894 for (j=0; j < ifaces->num; j++) {
895 if (ifaces->ifaces[j].link_state != 0) {
896 continue;
898 ret = true;
899 break;
901 ctdb_free_ifaces(ifaces);
905 return ret;
908 static void control_status_header_machine(void)
910 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
911 ":Inactive:PartiallyOnline:ThisNode:\n");
914 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
916 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
917 ctdb_addr_to_str(&node->addr),
918 !!(node->flags&NODE_FLAGS_DISCONNECTED),
919 !!(node->flags&NODE_FLAGS_BANNED),
920 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
921 !!(node->flags&NODE_FLAGS_UNHEALTHY),
922 !!(node->flags&NODE_FLAGS_STOPPED),
923 !!(node->flags&NODE_FLAGS_INACTIVE),
924 is_partially_online(node) ? 1 : 0,
925 (node->pnn == mypnn)?'Y':'N');
927 return node->flags;
930 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
932 printf("pnn:%d %-16s %s%s\n", node->pnn,
933 ctdb_addr_to_str(&node->addr),
934 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
935 node->pnn == mypnn?" (THIS NODE)":"");
937 return node->flags;
941 display remote ctdb status
943 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
945 int i;
946 struct ctdb_vnn_map *vnnmap=NULL;
947 struct ctdb_node_map *nodemap=NULL;
948 uint32_t recmode, recmaster, mypnn;
949 int num_deleted_nodes = 0;
951 mypnn = getpnn(ctdb);
953 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
954 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
955 return -1;
958 if (options.machinereadable) {
959 control_status_header_machine();
960 for (i=0;i<nodemap->num;i++) {
961 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
962 continue;
964 (void) control_status_1_machine(mypnn,
965 &nodemap->nodes[i]);
967 return 0;
970 for (i=0; i<nodemap->num; i++) {
971 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
972 num_deleted_nodes++;
975 if (num_deleted_nodes == 0) {
976 printf("Number of nodes:%d\n", nodemap->num);
977 } else {
978 printf("Number of nodes:%d (including %d deleted nodes)\n",
979 nodemap->num, num_deleted_nodes);
981 for(i=0;i<nodemap->num;i++){
982 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
983 continue;
985 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
988 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
989 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
990 return -1;
992 if (vnnmap->generation == INVALID_GENERATION) {
993 printf("Generation:INVALID\n");
994 } else {
995 printf("Generation:%d\n",vnnmap->generation);
997 printf("Size:%d\n",vnnmap->size);
998 for(i=0;i<vnnmap->size;i++){
999 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1001 ctdb_free_vnnmap(vnnmap);
1003 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
1004 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1005 return -1;
1007 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1009 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1010 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1011 return -1;
1013 printf("Recovery master:%d\n",recmaster);
1015 return 0;
1018 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1020 int i, ret;
1021 struct ctdb_node_map *nodemap=NULL;
1022 uint32_t * nodes;
1023 uint32_t pnn_mode, mypnn;
1025 if (argc > 1) {
1026 usage();
1029 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
1030 options.pnn, true, &nodes, &pnn_mode)) {
1031 return -1;
1034 if (options.machinereadable) {
1035 control_status_header_machine();
1036 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1037 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1040 mypnn = getpnn(ctdb);
1042 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
1043 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1044 return -1;
1047 ret = 0;
1049 for (i = 0; i < talloc_array_length(nodes); i++) {
1050 if (options.machinereadable) {
1051 ret |= control_status_1_machine(mypnn,
1052 &nodemap->nodes[nodes[i]]);
1053 } else {
1054 ret |= control_status_1_human(mypnn,
1055 &nodemap->nodes[nodes[i]]);
1058 return ret;
1061 struct natgw_node {
1062 struct natgw_node *next;
1063 const char *addr;
1066 static int find_natgw(struct ctdb_context *ctdb,
1067 struct ctdb_node_map *nodemap, uint32_t flags,
1068 uint32_t *pnn, const char **ip)
1070 int i;
1071 uint32_t capabilities;
1073 for (i=0;i<nodemap->num;i++) {
1074 if (!(nodemap->nodes[i].flags & flags)) {
1075 if (!ctdb_getcapabilities(ctdb_connection, nodemap->nodes[i].pnn, &capabilities)) {
1076 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1077 return -1;
1079 if (!(capabilities&CTDB_CAP_NATGW)) {
1080 continue;
1082 *pnn = nodemap->nodes[i].pnn;
1083 *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
1084 return 0;
1088 return 2; /* matches ENOENT */
1092 display the list of nodes belonging to this natgw configuration
1094 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1096 int i, ret;
1097 const char *natgw_list;
1098 int nlines;
1099 char **lines;
1100 struct natgw_node *natgw_nodes = NULL;
1101 struct natgw_node *natgw_node;
1102 struct ctdb_node_map *nodemap=NULL;
1103 uint32_t mypnn, pnn;
1104 const char *ip;
1106 /* When we have some nodes that could be the NATGW, make a
1107 * series of attempts to find the first node that doesn't have
1108 * certain status flags set.
1110 uint32_t exclude_flags[] = {
1111 /* Look for a nice healthy node */
1112 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1113 /* If not found, an UNHEALTHY/BANNED node will do */
1114 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1115 /* If not found, a STOPPED node will do */
1116 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1120 /* read the natgw nodes file into a linked list */
1121 natgw_list = getenv("CTDB_NATGW_NODES");
1122 if (natgw_list == NULL) {
1123 natgw_list = "/etc/ctdb/natgw_nodes";
1125 lines = file_lines_load(natgw_list, &nlines, ctdb);
1126 if (lines == NULL) {
1127 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1128 return -1;
1130 for (i=0;i<nlines;i++) {
1131 char *node;
1133 node = lines[i];
1134 /* strip leading spaces */
1135 while((*node == ' ') || (*node == '\t')) {
1136 node++;
1138 if (*node == '#') {
1139 continue;
1141 if (strcmp(node, "") == 0) {
1142 continue;
1144 natgw_node = talloc(ctdb, struct natgw_node);
1145 natgw_node->addr = talloc_strdup(natgw_node, node);
1146 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1147 natgw_node->next = natgw_nodes;
1148 natgw_nodes = natgw_node;
1151 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
1152 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1153 return -1;
1156 /* Trim the nodemap so it only includes connected nodes in the
1157 * current natgw group.
1159 i=0;
1160 while(i<nodemap->num) {
1161 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1162 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1163 break;
1167 /* this node was not in the natgw so we just remove it from
1168 * the list
1170 if ((natgw_node == NULL)
1171 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1172 int j;
1174 for (j=i+1; j<nodemap->num; j++) {
1175 nodemap->nodes[j-1] = nodemap->nodes[j];
1177 nodemap->num--;
1178 continue;
1181 i++;
1184 ret = 2; /* matches ENOENT */
1185 pnn = -1;
1186 ip = "0.0.0.0";
1187 for (i = 0; exclude_flags[i] != 0; i++) {
1188 ret = find_natgw(ctdb, nodemap,
1189 exclude_flags[i],
1190 &pnn, &ip);
1191 if (ret == -1) {
1192 goto done;
1194 if (ret == 0) {
1195 break;
1199 if (options.machinereadable) {
1200 printf(":Node:IP:\n");
1201 printf(":%d:%s:\n", pnn, ip);
1202 } else {
1203 printf("%d %s\n", pnn, ip);
1206 /* print the pruned list of nodes belonging to this natgw list */
1207 mypnn = getpnn(ctdb);
1208 if (options.machinereadable) {
1209 control_status_header_machine();
1210 } else {
1211 printf("Number of nodes:%d\n", nodemap->num);
1213 for(i=0;i<nodemap->num;i++){
1214 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1215 continue;
1217 if (options.machinereadable) {
1218 control_status_1_machine(mypnn, &(nodemap->nodes[i]));
1219 } else {
1220 control_status_1_human(mypnn, &(nodemap->nodes[i]));
1224 done:
1225 ctdb_free_nodemap(nodemap);
1226 return ret;
1230 display the status of the scripts for monitoring (or other events)
1232 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1233 enum ctdb_eventscript_call type)
1235 struct ctdb_scripts_wire *script_status;
1236 int ret, i;
1238 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1239 if (ret != 0) {
1240 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1241 return ret;
1244 if (script_status == NULL) {
1245 if (!options.machinereadable) {
1246 printf("%s cycle never run\n",
1247 ctdb_eventscript_call_names[type]);
1249 return 0;
1252 if (!options.machinereadable) {
1253 printf("%d scripts were executed last %s cycle\n",
1254 script_status->num_scripts,
1255 ctdb_eventscript_call_names[type]);
1257 for (i=0; i<script_status->num_scripts; i++) {
1258 const char *status = NULL;
1260 switch (script_status->scripts[i].status) {
1261 case -ETIME:
1262 status = "TIMEDOUT";
1263 break;
1264 case -ENOEXEC:
1265 status = "DISABLED";
1266 break;
1267 case 0:
1268 status = "OK";
1269 break;
1270 default:
1271 if (script_status->scripts[i].status > 0)
1272 status = "ERROR";
1273 break;
1275 if (options.machinereadable) {
1276 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1277 ctdb_eventscript_call_names[type],
1278 script_status->scripts[i].name,
1279 script_status->scripts[i].status,
1280 status,
1281 (long)script_status->scripts[i].start.tv_sec,
1282 (long)script_status->scripts[i].start.tv_usec,
1283 (long)script_status->scripts[i].finished.tv_sec,
1284 (long)script_status->scripts[i].finished.tv_usec,
1285 script_status->scripts[i].output);
1286 continue;
1288 if (status)
1289 printf("%-20s Status:%s ",
1290 script_status->scripts[i].name, status);
1291 else
1292 /* Some other error, eg from stat. */
1293 printf("%-20s Status:CANNOT RUN (%s)",
1294 script_status->scripts[i].name,
1295 strerror(-script_status->scripts[i].status));
1297 if (script_status->scripts[i].status >= 0) {
1298 printf("Duration:%.3lf ",
1299 timeval_delta(&script_status->scripts[i].finished,
1300 &script_status->scripts[i].start));
1302 if (script_status->scripts[i].status != -ENOEXEC) {
1303 printf("%s",
1304 ctime(&script_status->scripts[i].start.tv_sec));
1305 if (script_status->scripts[i].status != 0) {
1306 printf(" OUTPUT:%s\n",
1307 script_status->scripts[i].output);
1309 } else {
1310 printf("\n");
1313 return 0;
1317 static int control_scriptstatus(struct ctdb_context *ctdb,
1318 int argc, const char **argv)
1320 int ret;
1321 enum ctdb_eventscript_call type, min, max;
1322 const char *arg;
1324 if (argc > 1) {
1325 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1326 return -1;
1329 if (argc == 0)
1330 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1331 else
1332 arg = argv[0];
1334 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1335 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1336 min = type;
1337 max = type+1;
1338 break;
1341 if (type == CTDB_EVENT_MAX) {
1342 if (strcmp(arg, "all") == 0) {
1343 min = 0;
1344 max = CTDB_EVENT_MAX;
1345 } else {
1346 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1347 return -1;
1351 if (options.machinereadable) {
1352 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1355 for (type = min; type < max; type++) {
1356 ret = control_one_scriptstatus(ctdb, type);
1357 if (ret != 0) {
1358 return ret;
1362 return 0;
1366 enable an eventscript
1368 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1370 int ret;
1372 if (argc < 1) {
1373 usage();
1376 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1377 if (ret != 0) {
1378 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1379 return ret;
1382 return 0;
1386 disable an eventscript
1388 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1390 int ret;
1392 if (argc < 1) {
1393 usage();
1396 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1397 if (ret != 0) {
1398 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1399 return ret;
1402 return 0;
1406 display the pnn of the recovery master
1408 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1410 uint32_t recmaster;
1412 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1413 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1414 return -1;
1416 printf("%d\n",recmaster);
1418 return 0;
1422 add a tickle to a public address
1424 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1426 struct ctdb_tcp_connection t;
1427 TDB_DATA data;
1428 int ret;
1430 assert_single_node_only();
1432 if (argc < 2) {
1433 usage();
1436 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1437 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1438 return -1;
1440 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1441 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1442 return -1;
1445 data.dptr = (uint8_t *)&t;
1446 data.dsize = sizeof(t);
1448 /* tell all nodes about this tcp connection */
1449 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1450 0, data, ctdb, NULL, NULL, NULL, NULL);
1451 if (ret != 0) {
1452 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1453 return -1;
1456 return 0;
1461 delete a tickle from a node
1463 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1465 struct ctdb_tcp_connection t;
1466 TDB_DATA data;
1467 int ret;
1469 assert_single_node_only();
1471 if (argc < 2) {
1472 usage();
1475 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1476 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1477 return -1;
1479 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1480 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1481 return -1;
1484 data.dptr = (uint8_t *)&t;
1485 data.dsize = sizeof(t);
1487 /* tell all nodes about this tcp connection */
1488 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1489 0, data, ctdb, NULL, NULL, NULL, NULL);
1490 if (ret != 0) {
1491 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1492 return -1;
1495 return 0;
1500 get a list of all tickles for this pnn
1502 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1504 struct ctdb_control_tcp_tickle_list *list;
1505 ctdb_sock_addr addr;
1506 int i, ret;
1507 unsigned port = 0;
1509 assert_single_node_only();
1511 if (argc < 1) {
1512 usage();
1515 if (argc == 2) {
1516 port = atoi(argv[1]);
1519 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1520 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1521 return -1;
1524 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1525 if (ret == -1) {
1526 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1527 return -1;
1530 if (options.machinereadable){
1531 printf(":source ip:port:destination ip:port:\n");
1532 for (i=0;i<list->tickles.num;i++) {
1533 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1534 continue;
1536 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1537 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1539 } else {
1540 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1541 printf("Num tickles:%u\n", list->tickles.num);
1542 for (i=0;i<list->tickles.num;i++) {
1543 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1544 continue;
1546 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1547 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));
1551 talloc_free(list);
1553 return 0;
1557 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1559 struct ctdb_all_public_ips *ips;
1560 struct ctdb_public_ip ip;
1561 int i, ret;
1562 uint32_t *nodes;
1563 uint32_t disable_time;
1564 TDB_DATA data;
1565 struct ctdb_node_map *nodemap=NULL;
1566 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1568 disable_time = 30;
1569 data.dptr = (uint8_t*)&disable_time;
1570 data.dsize = sizeof(disable_time);
1571 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1572 if (ret != 0) {
1573 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1574 return -1;
1579 /* read the public ip list from the node */
1580 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1581 if (ret != 0) {
1582 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1583 talloc_free(tmp_ctx);
1584 return -1;
1587 for (i=0;i<ips->num;i++) {
1588 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1589 break;
1592 if (i==ips->num) {
1593 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1594 pnn, ctdb_addr_to_str(addr)));
1595 talloc_free(tmp_ctx);
1596 return -1;
1599 ip.pnn = pnn;
1600 ip.addr = *addr;
1602 data.dptr = (uint8_t *)&ip;
1603 data.dsize = sizeof(ip);
1605 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1606 if (ret != 0) {
1607 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1608 talloc_free(tmp_ctx);
1609 return ret;
1612 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1613 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1614 nodes, 0,
1615 LONGTIMELIMIT(),
1616 false, data,
1617 NULL, NULL,
1618 NULL);
1619 if (ret != 0) {
1620 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1621 talloc_free(tmp_ctx);
1622 return -1;
1625 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1626 if (ret != 0) {
1627 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1628 talloc_free(tmp_ctx);
1629 return -1;
1632 /* update the recovery daemon so it now knows to expect the new
1633 node assignment for this ip.
1635 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1636 if (ret != 0) {
1637 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1638 return -1;
1641 talloc_free(tmp_ctx);
1642 return 0;
1647 * scans all other nodes and returns a pnn for another node that can host this
1648 * ip address or -1
1650 static int
1651 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1653 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1654 struct ctdb_all_public_ips *ips;
1655 struct ctdb_node_map *nodemap=NULL;
1656 int i, j, ret;
1658 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1659 if (ret != 0) {
1660 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1661 talloc_free(tmp_ctx);
1662 return ret;
1665 for(i=0;i<nodemap->num;i++){
1666 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1667 continue;
1669 if (nodemap->nodes[i].pnn == options.pnn) {
1670 continue;
1673 /* read the public ip list from this node */
1674 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1675 if (ret != 0) {
1676 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1677 return -1;
1680 for (j=0;j<ips->num;j++) {
1681 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1682 talloc_free(tmp_ctx);
1683 return nodemap->nodes[i].pnn;
1686 talloc_free(ips);
1689 talloc_free(tmp_ctx);
1690 return -1;
1693 /* If pnn is -1 then try to find a node to move IP to... */
1694 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1696 bool pnn_specified = (pnn == -1 ? false : true);
1697 int retries = 0;
1699 while (retries < 5) {
1700 if (!pnn_specified) {
1701 pnn = find_other_host_for_public_ip(ctdb, addr);
1702 if (pnn == -1) {
1703 return false;
1705 DEBUG(DEBUG_NOTICE,
1706 ("Trying to move public IP to node %u\n", pnn));
1709 if (move_ip(ctdb, addr, pnn) == 0) {
1710 return true;
1713 sleep(3);
1714 retries++;
1717 return false;
1722 move/failover an ip address to a specific node
1724 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1726 uint32_t pnn;
1727 ctdb_sock_addr addr;
1729 assert_single_node_only();
1731 if (argc < 2) {
1732 usage();
1733 return -1;
1736 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1737 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1738 return -1;
1742 if (sscanf(argv[1], "%u", &pnn) != 1) {
1743 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1744 return -1;
1747 if (!try_moveip(ctdb, &addr, pnn)) {
1748 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1749 return -1;
1752 return 0;
1755 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1757 TDB_DATA data;
1759 data.dptr = (uint8_t *)&pnn;
1760 data.dsize = sizeof(uint32_t);
1761 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1762 DEBUG(DEBUG_ERR,
1763 ("Failed to send message to force node %u to be a rebalancing target\n",
1764 pnn));
1765 return -1;
1768 return 0;
1773 rebalance a node by setting it to allow failback and triggering a
1774 takeover run
1776 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1778 uint32_t *nodes;
1779 uint32_t pnn_mode;
1780 int i, ret;
1782 assert_single_node_only();
1784 if (argc > 1) {
1785 usage();
1788 /* Determine the nodes where IPs need to be reloaded */
1789 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
1790 options.pnn, true, &nodes, &pnn_mode)) {
1791 return -1;
1794 for (i = 0; i < talloc_array_length(nodes); i++) {
1795 if (!rebalance_node(ctdb, nodes[i])) {
1796 ret = -1;
1800 return ret;
1803 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1805 struct ctdb_public_ip ip;
1806 int ret;
1807 uint32_t *nodes;
1808 uint32_t disable_time;
1809 TDB_DATA data;
1810 struct ctdb_node_map *nodemap=NULL;
1811 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1813 disable_time = 30;
1814 data.dptr = (uint8_t*)&disable_time;
1815 data.dsize = sizeof(disable_time);
1816 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1817 if (ret != 0) {
1818 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1819 return -1;
1822 ip.pnn = -1;
1823 ip.addr = *addr;
1825 data.dptr = (uint8_t *)&ip;
1826 data.dsize = sizeof(ip);
1828 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1829 if (ret != 0) {
1830 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1831 talloc_free(tmp_ctx);
1832 return ret;
1835 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1836 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1837 nodes, 0,
1838 LONGTIMELIMIT(),
1839 false, data,
1840 NULL, NULL,
1841 NULL);
1842 if (ret != 0) {
1843 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1844 talloc_free(tmp_ctx);
1845 return -1;
1848 talloc_free(tmp_ctx);
1849 return 0;
1853 release an ip form all nodes and have it re-assigned by recd
1855 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1857 ctdb_sock_addr addr;
1859 assert_single_node_only();
1861 if (argc < 1) {
1862 usage();
1863 return -1;
1866 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1867 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1868 return -1;
1871 if (rebalance_ip(ctdb, &addr) != 0) {
1872 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1873 return -1;
1876 return 0;
1879 static int getips_store_callback(void *param, void *data)
1881 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1882 struct ctdb_all_public_ips *ips = param;
1883 int i;
1885 i = ips->num++;
1886 ips->ips[i].pnn = node_ip->pnn;
1887 ips->ips[i].addr = node_ip->addr;
1888 return 0;
1891 static int getips_count_callback(void *param, void *data)
1893 uint32_t *count = param;
1895 (*count)++;
1896 return 0;
1899 #define IP_KEYLEN 4
1900 static uint32_t *ip_key(ctdb_sock_addr *ip)
1902 static uint32_t key[IP_KEYLEN];
1904 bzero(key, sizeof(key));
1906 switch (ip->sa.sa_family) {
1907 case AF_INET:
1908 key[0] = ip->ip.sin_addr.s_addr;
1909 break;
1910 case AF_INET6: {
1911 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1912 key[0] = s6_a32[3];
1913 key[1] = s6_a32[2];
1914 key[2] = s6_a32[1];
1915 key[3] = s6_a32[0];
1916 break;
1918 default:
1919 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1920 return key;
1923 return key;
1926 static void *add_ip_callback(void *parm, void *data)
1928 return parm;
1931 static int
1932 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1934 struct ctdb_all_public_ips *tmp_ips;
1935 struct ctdb_node_map *nodemap=NULL;
1936 trbt_tree_t *ip_tree;
1937 int i, j, len, ret;
1938 uint32_t count;
1940 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1941 if (ret != 0) {
1942 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1943 return ret;
1946 ip_tree = trbt_create(tmp_ctx, 0);
1948 for(i=0;i<nodemap->num;i++){
1949 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1950 continue;
1952 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1953 continue;
1956 /* read the public ip list from this node */
1957 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1958 if (ret != 0) {
1959 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1960 return -1;
1963 for (j=0; j<tmp_ips->num;j++) {
1964 struct ctdb_public_ip *node_ip;
1966 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1967 node_ip->pnn = tmp_ips->ips[j].pnn;
1968 node_ip->addr = tmp_ips->ips[j].addr;
1970 trbt_insertarray32_callback(ip_tree,
1971 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1972 add_ip_callback,
1973 node_ip);
1975 talloc_free(tmp_ips);
1978 /* traverse */
1979 count = 0;
1980 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1982 len = offsetof(struct ctdb_all_public_ips, ips) +
1983 count*sizeof(struct ctdb_public_ip);
1984 tmp_ips = talloc_zero_size(tmp_ctx, len);
1985 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1987 *ips = tmp_ips;
1989 return 0;
1993 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1995 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1997 event_add_timed(ctdb->ev, ctdb,
1998 timeval_current_ofs(1, 0),
1999 ctdb_every_second, ctdb);
2002 struct srvid_reply_handler_data {
2003 bool done;
2004 bool wait_for_all;
2005 uint32_t *nodes;
2006 const char *srvid_str;
2009 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2010 uint64_t srvid,
2011 TDB_DATA data,
2012 void *private_data)
2014 struct srvid_reply_handler_data *d =
2015 (struct srvid_reply_handler_data *)private_data;
2016 int i;
2017 int32_t ret;
2019 if (data.dsize != sizeof(ret)) {
2020 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2021 return;
2024 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2025 ret = *(int32_t *)data.dptr;
2026 if (ret < 0) {
2027 DEBUG(DEBUG_ERR,
2028 ("%s failed with result %d\n", d->srvid_str, ret));
2029 return;
2032 if (!d->wait_for_all) {
2033 d->done = true;
2034 return;
2037 /* Wait for all replies */
2038 d->done = true;
2039 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2040 if (d->nodes[i] == ret) {
2041 DEBUG(DEBUG_INFO,
2042 ("%s reply received from node %u\n",
2043 d->srvid_str, ret));
2044 d->nodes[i] = -1;
2046 if (d->nodes[i] != -1) {
2047 /* Found a node that hasn't yet replied */
2048 d->done = false;
2053 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2054 * or replies from all connected nodes. arg is the data argument to
2055 * pass in the srvid_request structure - pass 0 if this isn't needed.
2057 static int srvid_broadcast(struct ctdb_context *ctdb,
2058 uint64_t srvid, uint32_t arg,
2059 const char *srvid_str, bool wait_for_all)
2061 int ret;
2062 TDB_DATA data;
2063 struct srvid_request request;
2064 struct srvid_reply_handler_data reply_data;
2065 struct timeval tv;
2067 ZERO_STRUCT(request);
2069 /* Time ticks to enable timeouts to be processed */
2070 event_add_timed(ctdb->ev, ctdb,
2071 timeval_current_ofs(1, 0),
2072 ctdb_every_second, ctdb);
2074 request.pnn = ctdb_get_pnn(ctdb);
2075 request.srvid = getpid();
2076 request.data = arg;
2078 /* Register message port for reply from recovery master */
2079 ctdb_client_set_message_handler(ctdb, request.srvid,
2080 srvid_broadcast_reply_handler,
2081 &reply_data);
2083 data.dptr = (uint8_t *)&request;
2084 data.dsize = sizeof(request);
2086 reply_data.wait_for_all = wait_for_all;
2087 reply_data.nodes = NULL;
2088 reply_data.srvid_str = srvid_str;
2090 again:
2091 reply_data.done = false;
2093 if (wait_for_all) {
2094 struct ctdb_node_map *nodemap;
2096 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2097 CTDB_CURRENT_NODE, ctdb, &nodemap);
2098 if (ret != 0) {
2099 DEBUG(DEBUG_ERR,
2100 ("Unable to get nodemap from current node, try again\n"));
2101 sleep(1);
2102 goto again;
2105 if (reply_data.nodes != NULL) {
2106 talloc_free(reply_data.nodes);
2108 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2109 NULL, true);
2111 talloc_free(nodemap);
2114 /* Send to all connected nodes. Only recmaster replies */
2115 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2116 srvid, data);
2117 if (ret != 0) {
2118 /* This can only happen if the socket is closed and
2119 * there's no way to recover from that, so don't try
2120 * again.
2122 DEBUG(DEBUG_ERR,
2123 ("Failed to send %s request to connected nodes\n",
2124 srvid_str));
2125 return -1;
2128 tv = timeval_current();
2129 /* This loop terminates the reply is received */
2130 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2131 event_loop_once(ctdb->ev);
2134 if (!reply_data.done) {
2135 DEBUG(DEBUG_NOTICE,
2136 ("Still waiting for confirmation of %s\n", srvid_str));
2137 sleep(1);
2138 goto again;
2141 ctdb_client_remove_message_handler(ctdb, request.srvid, &reply_data);
2143 talloc_free(reply_data.nodes);
2145 return 0;
2148 static int ipreallocate(struct ctdb_context *ctdb)
2150 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, 0,
2151 "IP reallocation", false);
2155 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2157 return ipreallocate(ctdb);
2161 add a public ip address to a node
2163 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2165 int i, ret;
2166 int len, retries = 0;
2167 unsigned mask;
2168 ctdb_sock_addr addr;
2169 struct ctdb_control_ip_iface *pub;
2170 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2171 struct ctdb_all_public_ips *ips;
2174 if (argc != 2) {
2175 talloc_free(tmp_ctx);
2176 usage();
2179 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2180 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2181 talloc_free(tmp_ctx);
2182 return -1;
2185 /* read the public ip list from the node */
2186 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2187 if (ret != 0) {
2188 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2189 talloc_free(tmp_ctx);
2190 return -1;
2192 for (i=0;i<ips->num;i++) {
2193 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2194 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2195 return 0;
2201 /* Dont timeout. This command waits for an ip reallocation
2202 which sometimes can take wuite a while if there has
2203 been a recent recovery
2205 alarm(0);
2207 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2208 pub = talloc_size(tmp_ctx, len);
2209 CTDB_NO_MEMORY(ctdb, pub);
2211 pub->addr = addr;
2212 pub->mask = mask;
2213 pub->len = strlen(argv[1])+1;
2214 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2216 do {
2217 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2218 if (ret != 0) {
2219 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2220 sleep(3);
2221 retries++;
2223 } while (retries < 5 && ret != 0);
2224 if (ret != 0) {
2225 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2226 talloc_free(tmp_ctx);
2227 return ret;
2230 if (rebalance_node(ctdb, options.pnn) != 0) {
2231 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2232 return ret;
2235 talloc_free(tmp_ctx);
2236 return 0;
2240 add a public ip address to a node
2242 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2244 ctdb_sock_addr addr;
2246 if (argc != 1) {
2247 usage();
2250 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2251 printf("Badly formed ip : %s\n", argv[0]);
2252 return -1;
2255 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2257 return 0;
2260 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2262 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2264 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2265 struct ctdb_node_map *nodemap=NULL;
2266 struct ctdb_all_public_ips *ips;
2267 int ret, i, j;
2269 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2270 if (ret != 0) {
2271 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2272 return ret;
2275 /* remove it from the nodes that are not hosting the ip currently */
2276 for(i=0;i<nodemap->num;i++){
2277 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2278 continue;
2280 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2281 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2282 continue;
2285 for (j=0;j<ips->num;j++) {
2286 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2287 break;
2290 if (j==ips->num) {
2291 continue;
2294 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2295 continue;
2298 options.pnn = nodemap->nodes[i].pnn;
2299 control_delip(ctdb, argc, argv);
2303 /* remove it from every node (also the one hosting it) */
2304 for(i=0;i<nodemap->num;i++){
2305 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2306 continue;
2308 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2309 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2310 continue;
2313 for (j=0;j<ips->num;j++) {
2314 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2315 break;
2318 if (j==ips->num) {
2319 continue;
2322 options.pnn = nodemap->nodes[i].pnn;
2323 control_delip(ctdb, argc, argv);
2326 talloc_free(tmp_ctx);
2327 return 0;
2331 delete a public ip address from a node
2333 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2335 int i, ret;
2336 ctdb_sock_addr addr;
2337 struct ctdb_control_ip_iface pub;
2338 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2339 struct ctdb_all_public_ips *ips;
2341 if (argc != 1) {
2342 talloc_free(tmp_ctx);
2343 usage();
2346 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2347 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2348 return -1;
2351 if (options.pnn == CTDB_BROADCAST_ALL) {
2352 return control_delip_all(ctdb, argc, argv, &addr);
2355 pub.addr = addr;
2356 pub.mask = 0;
2357 pub.len = 0;
2359 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2360 if (ret != 0) {
2361 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2362 talloc_free(tmp_ctx);
2363 return ret;
2366 for (i=0;i<ips->num;i++) {
2367 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2368 break;
2372 if (i==ips->num) {
2373 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2374 ctdb_addr_to_str(&addr)));
2375 talloc_free(tmp_ctx);
2376 return -1;
2379 /* This is an optimisation. If this node is hosting the IP
2380 * then try to move it somewhere else without invoking a full
2381 * takeover run. We don't care if this doesn't work!
2383 if (ips->ips[i].pnn == options.pnn) {
2384 (void) try_moveip(ctdb, &addr, -1);
2387 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2388 if (ret != 0) {
2389 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2390 talloc_free(tmp_ctx);
2391 return ret;
2394 talloc_free(tmp_ctx);
2395 return 0;
2398 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2399 int argc, const char **argv)
2401 struct ctdb_control_killtcp *killtcp;
2402 int max_entries, current, i;
2403 struct timeval timeout;
2404 char line[128], src[128], dst[128];
2405 int linenum;
2406 TDB_DATA data;
2407 struct client_async_data *async_data;
2408 struct ctdb_client_control_state *state;
2410 if (argc != 0) {
2411 usage();
2414 linenum = 1;
2415 killtcp = NULL;
2416 max_entries = 0;
2417 current = 0;
2418 while (!feof(stdin)) {
2419 if (fgets(line, sizeof(line), stdin) == NULL) {
2420 continue;
2423 /* Silently skip empty lines */
2424 if (line[0] == '\n') {
2425 continue;
2428 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2429 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2430 linenum, line));
2431 talloc_free(killtcp);
2432 return -1;
2435 if (current >= max_entries) {
2436 max_entries += 1024;
2437 killtcp = talloc_realloc(ctdb, killtcp,
2438 struct ctdb_control_killtcp,
2439 max_entries);
2440 CTDB_NO_MEMORY(ctdb, killtcp);
2443 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2444 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2445 linenum, src));
2446 talloc_free(killtcp);
2447 return -1;
2450 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2451 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2452 linenum, dst));
2453 talloc_free(killtcp);
2454 return -1;
2457 current++;
2460 async_data = talloc_zero(ctdb, struct client_async_data);
2461 if (async_data == NULL) {
2462 talloc_free(killtcp);
2463 return -1;
2466 for (i = 0; i < current; i++) {
2468 data.dsize = sizeof(struct ctdb_control_killtcp);
2469 data.dptr = (unsigned char *)&killtcp[i];
2471 timeout = TIMELIMIT();
2472 state = ctdb_control_send(ctdb, options.pnn, 0,
2473 CTDB_CONTROL_KILL_TCP, 0, data,
2474 async_data, &timeout, NULL);
2476 if (state == NULL) {
2477 DEBUG(DEBUG_ERR,
2478 ("Failed to call async killtcp control to node %u\n",
2479 options.pnn));
2480 talloc_free(killtcp);
2481 return -1;
2484 ctdb_client_async_add(async_data, state);
2487 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2488 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2489 talloc_free(killtcp);
2490 return -1;
2493 talloc_free(killtcp);
2494 return 0;
2499 kill a tcp connection
2501 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2503 int ret;
2504 struct ctdb_control_killtcp killtcp;
2506 assert_single_node_only();
2508 if (argc == 0) {
2509 return kill_tcp_from_file(ctdb, argc, argv);
2512 if (argc < 2) {
2513 usage();
2516 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2517 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2518 return -1;
2521 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2522 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2523 return -1;
2526 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2527 if (ret != 0) {
2528 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2529 return ret;
2532 return 0;
2537 send a gratious arp
2539 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2541 int ret;
2542 ctdb_sock_addr addr;
2544 assert_single_node_only();
2546 if (argc < 2) {
2547 usage();
2550 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2551 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2552 return -1;
2555 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2556 if (ret != 0) {
2557 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2558 return ret;
2561 return 0;
2565 register a server id
2567 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2569 int ret;
2570 struct ctdb_server_id server_id;
2572 if (argc < 3) {
2573 usage();
2576 server_id.pnn = strtoul(argv[0], NULL, 0);
2577 server_id.type = strtoul(argv[1], NULL, 0);
2578 server_id.server_id = strtoul(argv[2], NULL, 0);
2580 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2581 if (ret != 0) {
2582 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2583 return ret;
2585 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2586 sleep(999);
2587 return -1;
2591 unregister a server id
2593 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2595 int ret;
2596 struct ctdb_server_id server_id;
2598 if (argc < 3) {
2599 usage();
2602 server_id.pnn = strtoul(argv[0], NULL, 0);
2603 server_id.type = strtoul(argv[1], NULL, 0);
2604 server_id.server_id = strtoul(argv[2], NULL, 0);
2606 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2607 if (ret != 0) {
2608 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2609 return ret;
2611 return -1;
2615 check if a server id exists
2617 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2619 uint32_t status;
2620 int ret;
2621 struct ctdb_server_id server_id;
2623 if (argc < 3) {
2624 usage();
2627 server_id.pnn = strtoul(argv[0], NULL, 0);
2628 server_id.type = strtoul(argv[1], NULL, 0);
2629 server_id.server_id = strtoul(argv[2], NULL, 0);
2631 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2632 if (ret != 0) {
2633 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2634 return ret;
2637 if (status) {
2638 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2639 } else {
2640 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2642 return 0;
2646 get a list of all server ids that are registered on a node
2648 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2650 int i, ret;
2651 struct ctdb_server_id_list *server_ids;
2653 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2654 if (ret != 0) {
2655 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2656 return ret;
2659 for (i=0; i<server_ids->num; i++) {
2660 printf("Server id %d:%d:%d\n",
2661 server_ids->server_ids[i].pnn,
2662 server_ids->server_ids[i].type,
2663 server_ids->server_ids[i].server_id);
2666 return -1;
2670 check if a server id exists
2672 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2674 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2675 uint64_t *ids;
2676 uint8_t *result;
2677 int i;
2679 if (argc < 1) {
2680 talloc_free(tmp_ctx);
2681 usage();
2684 ids = talloc_array(tmp_ctx, uint64_t, argc);
2685 result = talloc_array(tmp_ctx, uint8_t, argc);
2687 for (i = 0; i < argc; i++) {
2688 ids[i] = strtoull(argv[i], NULL, 0);
2691 if (!ctdb_check_message_handlers(ctdb_connection,
2692 options.pnn, argc, ids, result)) {
2693 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2694 options.pnn));
2695 talloc_free(tmp_ctx);
2696 return -1;
2699 for (i=0; i < argc; i++) {
2700 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2701 result[i] ? "exists" : "does not exist");
2704 talloc_free(tmp_ctx);
2705 return 0;
2709 send a tcp tickle ack
2711 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2713 int ret;
2714 ctdb_sock_addr src, dst;
2716 if (argc < 2) {
2717 usage();
2720 if (!parse_ip_port(argv[0], &src)) {
2721 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2722 return -1;
2725 if (!parse_ip_port(argv[1], &dst)) {
2726 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2727 return -1;
2730 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2731 if (ret==0) {
2732 return 0;
2734 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2736 return -1;
2741 display public ip status
2743 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2745 int i, ret;
2746 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2747 struct ctdb_all_public_ips *ips;
2749 if (options.pnn == CTDB_BROADCAST_ALL) {
2750 /* read the list of public ips from all nodes */
2751 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2752 } else {
2753 /* read the public ip list from this node */
2754 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2756 if (ret != 0) {
2757 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2758 talloc_free(tmp_ctx);
2759 return ret;
2762 if (options.machinereadable){
2763 printf(":Public IP:Node:");
2764 if (options.verbose){
2765 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2767 printf("\n");
2768 } else {
2769 if (options.pnn == CTDB_BROADCAST_ALL) {
2770 printf("Public IPs on ALL nodes\n");
2771 } else {
2772 printf("Public IPs on node %u\n", options.pnn);
2776 for (i=1;i<=ips->num;i++) {
2777 struct ctdb_control_public_ip_info *info = NULL;
2778 int32_t pnn;
2779 char *aciface = NULL;
2780 char *avifaces = NULL;
2781 char *cifaces = NULL;
2783 if (options.pnn == CTDB_BROADCAST_ALL) {
2784 pnn = ips->ips[ips->num-i].pnn;
2785 } else {
2786 pnn = options.pnn;
2789 if (pnn != -1) {
2790 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2791 &ips->ips[ips->num-i].addr, &info);
2792 } else {
2793 ret = -1;
2796 if (ret == 0) {
2797 int j;
2798 for (j=0; j < info->num; j++) {
2799 if (cifaces == NULL) {
2800 cifaces = talloc_strdup(info,
2801 info->ifaces[j].name);
2802 } else {
2803 cifaces = talloc_asprintf_append(cifaces,
2804 ",%s",
2805 info->ifaces[j].name);
2808 if (info->active_idx == j) {
2809 aciface = info->ifaces[j].name;
2812 if (info->ifaces[j].link_state == 0) {
2813 continue;
2816 if (avifaces == NULL) {
2817 avifaces = talloc_strdup(info, info->ifaces[j].name);
2818 } else {
2819 avifaces = talloc_asprintf_append(avifaces,
2820 ",%s",
2821 info->ifaces[j].name);
2826 if (options.machinereadable){
2827 printf(":%s:%d:",
2828 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2829 ips->ips[ips->num-i].pnn);
2830 if (options.verbose){
2831 printf("%s:%s:%s:",
2832 aciface?aciface:"",
2833 avifaces?avifaces:"",
2834 cifaces?cifaces:"");
2836 printf("\n");
2837 } else {
2838 if (options.verbose) {
2839 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2840 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2841 ips->ips[ips->num-i].pnn,
2842 aciface?aciface:"",
2843 avifaces?avifaces:"",
2844 cifaces?cifaces:"");
2845 } else {
2846 printf("%s %d\n",
2847 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2848 ips->ips[ips->num-i].pnn);
2851 talloc_free(info);
2854 talloc_free(tmp_ctx);
2855 return 0;
2859 public ip info
2861 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2863 int i, ret;
2864 ctdb_sock_addr addr;
2865 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2866 struct ctdb_control_public_ip_info *info;
2868 if (argc != 1) {
2869 talloc_free(tmp_ctx);
2870 usage();
2873 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2874 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2875 return -1;
2878 /* read the public ip info from this node */
2879 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2880 tmp_ctx, &addr, &info);
2881 if (ret != 0) {
2882 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2883 argv[0], options.pnn));
2884 talloc_free(tmp_ctx);
2885 return ret;
2888 printf("Public IP[%s] info on node %u\n",
2889 ctdb_addr_to_str(&info->ip.addr),
2890 options.pnn);
2892 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2893 ctdb_addr_to_str(&info->ip.addr),
2894 info->ip.pnn, info->num);
2896 for (i=0; i<info->num; i++) {
2897 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2899 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2900 i+1, info->ifaces[i].name,
2901 info->ifaces[i].link_state?"up":"down",
2902 (unsigned int)info->ifaces[i].references,
2903 (i==info->active_idx)?" (active)":"");
2906 talloc_free(tmp_ctx);
2907 return 0;
2911 display interfaces status
2913 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2915 int i;
2916 struct ctdb_ifaces_list *ifaces;
2918 /* read the public ip list from this node */
2919 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2920 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2921 options.pnn));
2922 return -1;
2925 if (options.machinereadable){
2926 printf(":Name:LinkStatus:References:\n");
2927 } else {
2928 printf("Interfaces on node %u\n", options.pnn);
2931 for (i=0; i<ifaces->num; i++) {
2932 if (options.machinereadable){
2933 printf(":%s:%s:%u\n",
2934 ifaces->ifaces[i].name,
2935 ifaces->ifaces[i].link_state?"1":"0",
2936 (unsigned int)ifaces->ifaces[i].references);
2937 } else {
2938 printf("name:%s link:%s references:%u\n",
2939 ifaces->ifaces[i].name,
2940 ifaces->ifaces[i].link_state?"up":"down",
2941 (unsigned int)ifaces->ifaces[i].references);
2945 ctdb_free_ifaces(ifaces);
2946 return 0;
2951 set link status of an interface
2953 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2955 int ret;
2956 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2957 struct ctdb_control_iface_info info;
2959 ZERO_STRUCT(info);
2961 if (argc != 2) {
2962 usage();
2965 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2966 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2967 argv[0]));
2968 talloc_free(tmp_ctx);
2969 return -1;
2971 strcpy(info.name, argv[0]);
2973 if (strcmp(argv[1], "up") == 0) {
2974 info.link_state = 1;
2975 } else if (strcmp(argv[1], "down") == 0) {
2976 info.link_state = 0;
2977 } else {
2978 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2979 argv[1]));
2980 talloc_free(tmp_ctx);
2981 return -1;
2984 /* read the public ip list from this node */
2985 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2986 tmp_ctx, &info);
2987 if (ret != 0) {
2988 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2989 argv[0], options.pnn));
2990 talloc_free(tmp_ctx);
2991 return ret;
2994 talloc_free(tmp_ctx);
2995 return 0;
2999 display pid of a ctdb daemon
3001 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3003 uint32_t pid;
3004 int ret;
3006 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3007 if (ret != 0) {
3008 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3009 return ret;
3011 printf("Pid:%d\n", pid);
3013 return 0;
3016 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3018 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3019 void *data,
3020 update_flags_handler_t handler,
3021 uint32_t flag,
3022 const char *desc,
3023 bool set_flag)
3025 struct ctdb_node_map *nodemap = NULL;
3026 bool flag_is_set;
3028 /* Check if the node is already in the desired state */
3029 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
3030 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3031 exit(10);
3033 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3034 if (set_flag == flag_is_set) {
3035 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3036 (set_flag ? "already" : "not"), desc));
3037 return 0;
3040 do {
3041 if (!handler(ctdb, data)) {
3042 DEBUG(DEBUG_WARNING,
3043 ("Failed to send control to set state %s on node %u, try again\n",
3044 desc, options.pnn));
3047 sleep(1);
3049 /* Read the nodemap and verify the change took effect.
3050 * Even if the above control/hanlder timed out then it
3051 * could still have worked!
3053 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3054 ctdb, &nodemap) != 0) {
3055 DEBUG(DEBUG_WARNING,
3056 ("Unable to get nodemap from local node, try again\n"));
3058 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3059 } while (nodemap == NULL || (set_flag != flag_is_set));
3061 return ipreallocate(ctdb);
3064 /* Administratively disable a node */
3065 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3067 return ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3068 NODE_FLAGS_PERMANENTLY_DISABLED, 0) == 0;
3071 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3073 return update_flags_and_ipreallocate(ctdb, NULL,
3074 update_flags_disabled,
3075 NODE_FLAGS_PERMANENTLY_DISABLED,
3076 "disabled",
3077 true /* set_flag*/);
3080 /* Administratively re-enable a node */
3081 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3083 return ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3084 0, NODE_FLAGS_PERMANENTLY_DISABLED) == 0;
3087 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3089 return update_flags_and_ipreallocate(ctdb, NULL,
3090 update_flags_not_disabled,
3091 NODE_FLAGS_PERMANENTLY_DISABLED,
3092 "disabled",
3093 false /* set_flag*/);
3096 /* Stop a node */
3097 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3099 return ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn) == 0;
3102 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3104 return update_flags_and_ipreallocate(ctdb, NULL,
3105 update_flags_stopped,
3106 NODE_FLAGS_STOPPED,
3107 "stopped",
3108 true /* set_flag*/);
3111 /* Continue a stopped node */
3112 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3114 return ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn) == 0;
3117 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3119 return update_flags_and_ipreallocate(ctdb, NULL,
3120 update_flags_not_stopped,
3121 NODE_FLAGS_STOPPED,
3122 "stopped",
3123 false /* set_flag */);
3126 static uint32_t get_generation(struct ctdb_context *ctdb)
3128 struct ctdb_vnn_map *vnnmap=NULL;
3129 int ret;
3131 /* wait until the recmaster is not in recovery mode */
3132 while (1) {
3133 uint32_t recmode, recmaster;
3135 if (vnnmap != NULL) {
3136 talloc_free(vnnmap);
3137 vnnmap = NULL;
3140 /* get the recmaster */
3141 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
3142 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3143 exit(10);
3146 /* get recovery mode */
3147 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
3148 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3149 exit(10);
3152 /* get the current generation number */
3153 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
3154 if (ret != 0) {
3155 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3156 exit(10);
3159 if ((recmode == CTDB_RECOVERY_NORMAL)
3160 && (vnnmap->generation != 1)){
3161 return vnnmap->generation;
3163 sleep(1);
3167 /* Ban a node */
3168 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3170 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3171 return ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime) == 0;
3174 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3176 struct ctdb_ban_time bantime;
3178 if (argc < 1) {
3179 usage();
3182 bantime.pnn = options.pnn;
3183 bantime.time = strtoul(argv[0], NULL, 0);
3185 if (bantime.time == 0) {
3186 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3187 return -1;
3190 return update_flags_and_ipreallocate(ctdb, &bantime,
3191 update_state_banned,
3192 NODE_FLAGS_BANNED,
3193 "banned",
3194 true /* set_flag*/);
3198 /* Unban a node */
3199 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3201 struct ctdb_ban_time bantime;
3203 bantime.pnn = options.pnn;
3204 bantime.time = 0;
3206 return update_flags_and_ipreallocate(ctdb, &bantime,
3207 update_state_banned,
3208 NODE_FLAGS_BANNED,
3209 "banned",
3210 false /* set_flag*/);
3214 show ban information for a node
3216 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3218 int ret;
3219 struct ctdb_node_map *nodemap=NULL;
3220 struct ctdb_ban_time *bantime;
3222 /* verify the node exists */
3223 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3224 if (ret != 0) {
3225 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3226 return ret;
3229 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3230 if (ret != 0) {
3231 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3232 return -1;
3235 if (bantime->time == 0) {
3236 printf("Node %u is not banned\n", bantime->pnn);
3237 } else {
3238 printf("Node %u is banned, %d seconds remaining\n",
3239 bantime->pnn, bantime->time);
3242 return 0;
3246 shutdown a daemon
3248 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3250 int ret;
3252 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3253 if (ret != 0) {
3254 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3255 return ret;
3258 return 0;
3262 trigger a recovery
3264 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3266 int ret;
3267 uint32_t generation, next_generation;
3268 bool force;
3270 /* "force" option ignores freeze failure and forces recovery */
3271 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3273 /* record the current generation number */
3274 generation = get_generation(ctdb);
3276 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3277 if (ret != 0) {
3278 if (!force) {
3279 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3280 return ret;
3282 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3285 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3286 if (ret != 0) {
3287 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3288 return ret;
3291 /* wait until we are in a new generation */
3292 while (1) {
3293 next_generation = get_generation(ctdb);
3294 if (next_generation != generation) {
3295 return 0;
3297 sleep(1);
3300 return 0;
3305 display monitoring mode of a remote node
3307 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3309 uint32_t monmode;
3310 int ret;
3312 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3313 if (ret != 0) {
3314 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3315 return ret;
3317 if (!options.machinereadable){
3318 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3319 } else {
3320 printf(":mode:\n");
3321 printf(":%d:\n",monmode);
3323 return 0;
3328 display capabilities of a remote node
3330 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3332 uint32_t capabilities;
3334 if (!ctdb_getcapabilities(ctdb_connection, options.pnn, &capabilities)) {
3335 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3336 return -1;
3339 if (!options.machinereadable){
3340 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3341 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3342 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3343 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3344 } else {
3345 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3346 printf(":%d:%d:%d:%d:\n",
3347 !!(capabilities&CTDB_CAP_RECMASTER),
3348 !!(capabilities&CTDB_CAP_LMASTER),
3349 !!(capabilities&CTDB_CAP_LVS),
3350 !!(capabilities&CTDB_CAP_NATGW));
3352 return 0;
3356 display lvs configuration
3358 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3360 uint32_t *capabilities;
3361 struct ctdb_node_map *nodemap=NULL;
3362 int i, ret;
3363 int healthy_count = 0;
3365 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3366 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3367 return -1;
3370 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3371 CTDB_NO_MEMORY(ctdb, capabilities);
3373 ret = 0;
3375 /* collect capabilities for all connected nodes */
3376 for (i=0; i<nodemap->num; i++) {
3377 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3378 continue;
3380 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3381 continue;
3384 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3385 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3386 ret = -1;
3387 goto done;
3390 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3391 continue;
3394 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3395 healthy_count++;
3399 /* Print all LVS nodes */
3400 for (i=0; i<nodemap->num; i++) {
3401 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3402 continue;
3404 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3405 continue;
3407 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3408 continue;
3411 if (healthy_count != 0) {
3412 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3413 continue;
3417 printf("%d:%s\n", i,
3418 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3421 done:
3422 ctdb_free_nodemap(nodemap);
3423 return ret;
3427 display who is the lvs master
3429 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3431 uint32_t *capabilities;
3432 struct ctdb_node_map *nodemap=NULL;
3433 int i, ret;
3434 int healthy_count = 0;
3436 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3437 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3438 return -1;
3441 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3442 CTDB_NO_MEMORY(ctdb, capabilities);
3444 ret = -1;
3446 /* collect capabilities for all connected nodes */
3447 for (i=0; i<nodemap->num; i++) {
3448 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3449 continue;
3451 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3452 continue;
3455 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3456 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3457 ret = -1;
3458 goto done;
3461 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3462 continue;
3465 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3466 healthy_count++;
3470 /* find and show the lvsmaster */
3471 for (i=0; i<nodemap->num; i++) {
3472 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3473 continue;
3475 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3476 continue;
3478 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3479 continue;
3482 if (healthy_count != 0) {
3483 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3484 continue;
3488 if (options.machinereadable){
3489 printf("%d\n", i);
3490 } else {
3491 printf("Node %d is LVS master\n", i);
3493 ret = 0;
3494 goto done;
3497 printf("There is no LVS master\n");
3498 done:
3499 ctdb_free_nodemap(nodemap);
3500 return ret;
3504 disable monitoring on a node
3506 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3509 int ret;
3511 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3512 if (ret != 0) {
3513 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3514 return ret;
3516 printf("Monitoring mode:%s\n","DISABLED");
3518 return 0;
3522 enable monitoring on a node
3524 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3527 int ret;
3529 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3530 if (ret != 0) {
3531 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3532 return ret;
3534 printf("Monitoring mode:%s\n","ACTIVE");
3536 return 0;
3540 display remote list of keys/data for a db
3542 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3544 const char *db_name;
3545 struct ctdb_db_context *ctdb_db;
3546 int ret;
3547 struct ctdb_dump_db_context c;
3548 uint8_t flags;
3550 if (argc < 1) {
3551 usage();
3554 db_name = argv[0];
3556 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3557 return -1;
3560 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3561 if (ctdb_db == NULL) {
3562 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3563 return -1;
3566 if (options.printlmaster) {
3567 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3568 ctdb, &ctdb->vnn_map);
3569 if (ret != 0) {
3570 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3571 options.pnn));
3572 return ret;
3576 ZERO_STRUCT(c);
3577 c.f = stdout;
3578 c.printemptyrecords = (bool)options.printemptyrecords;
3579 c.printdatasize = (bool)options.printdatasize;
3580 c.printlmaster = (bool)options.printlmaster;
3581 c.printhash = (bool)options.printhash;
3582 c.printrecordflags = (bool)options.printrecordflags;
3584 /* traverse and dump the cluster tdb */
3585 ret = ctdb_dump_db(ctdb_db, &c);
3586 if (ret == -1) {
3587 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3588 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3589 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3590 db_name));
3591 return -1;
3593 talloc_free(ctdb_db);
3595 printf("Dumped %d records\n", ret);
3596 return 0;
3599 struct cattdb_data {
3600 struct ctdb_context *ctdb;
3601 uint32_t count;
3604 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3606 struct cattdb_data *d = private_data;
3607 struct ctdb_dump_db_context c;
3609 d->count++;
3611 ZERO_STRUCT(c);
3612 c.f = stdout;
3613 c.printemptyrecords = (bool)options.printemptyrecords;
3614 c.printdatasize = (bool)options.printdatasize;
3615 c.printlmaster = false;
3616 c.printhash = (bool)options.printhash;
3617 c.printrecordflags = true;
3619 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3623 cat the local tdb database using same format as catdb
3625 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3627 const char *db_name;
3628 struct ctdb_db_context *ctdb_db;
3629 struct cattdb_data d;
3630 uint8_t flags;
3632 if (argc < 1) {
3633 usage();
3636 db_name = argv[0];
3638 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3639 return -1;
3642 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3643 if (ctdb_db == NULL) {
3644 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3645 return -1;
3648 /* traverse the local tdb */
3649 d.count = 0;
3650 d.ctdb = ctdb;
3651 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3652 printf("Failed to cattdb data\n");
3653 exit(10);
3655 talloc_free(ctdb_db);
3657 printf("Dumped %d records\n", d.count);
3658 return 0;
3662 display the content of a database key
3664 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3666 const char *db_name;
3667 struct ctdb_db_context *ctdb_db;
3668 struct ctdb_record_handle *h;
3669 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3670 TDB_DATA key, data;
3671 uint8_t flags;
3673 if (argc < 2) {
3674 usage();
3677 db_name = argv[0];
3679 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3680 return -1;
3683 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3684 if (ctdb_db == NULL) {
3685 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3686 return -1;
3689 key.dptr = discard_const(argv[1]);
3690 key.dsize = strlen((char *)key.dptr);
3692 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3693 if (h == NULL) {
3694 printf("Failed to fetch record '%s' on node %d\n",
3695 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3696 talloc_free(tmp_ctx);
3697 exit(10);
3700 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3702 talloc_free(ctdb_db);
3703 talloc_free(tmp_ctx);
3704 return 0;
3708 display the content of a database key
3710 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3712 const char *db_name;
3713 struct ctdb_db_context *ctdb_db;
3714 struct ctdb_record_handle *h;
3715 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3716 TDB_DATA key, data;
3717 uint8_t flags;
3719 if (argc < 3) {
3720 usage();
3723 db_name = argv[0];
3725 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3726 return -1;
3729 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3730 if (ctdb_db == NULL) {
3731 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3732 return -1;
3735 key.dptr = discard_const(argv[1]);
3736 key.dsize = strlen((char *)key.dptr);
3738 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3739 if (h == NULL) {
3740 printf("Failed to fetch record '%s' on node %d\n",
3741 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3742 talloc_free(tmp_ctx);
3743 exit(10);
3746 data.dptr = discard_const(argv[2]);
3747 data.dsize = strlen((char *)data.dptr);
3749 if (ctdb_record_store(h, data) != 0) {
3750 printf("Failed to store record\n");
3753 talloc_free(h);
3754 talloc_free(ctdb_db);
3755 talloc_free(tmp_ctx);
3756 return 0;
3760 fetch a record from a persistent database
3762 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3764 const char *db_name;
3765 struct ctdb_db_context *ctdb_db;
3766 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3767 struct ctdb_transaction_handle *h;
3768 TDB_DATA key, data;
3769 int fd, ret;
3770 bool persistent;
3771 uint8_t flags;
3773 if (argc < 2) {
3774 talloc_free(tmp_ctx);
3775 usage();
3778 db_name = argv[0];
3780 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3781 talloc_free(tmp_ctx);
3782 return -1;
3785 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3786 if (!persistent) {
3787 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3788 talloc_free(tmp_ctx);
3789 return -1;
3792 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3793 if (ctdb_db == NULL) {
3794 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3795 talloc_free(tmp_ctx);
3796 return -1;
3799 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3800 if (h == NULL) {
3801 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3802 talloc_free(tmp_ctx);
3803 return -1;
3806 key.dptr = discard_const(argv[1]);
3807 key.dsize = strlen(argv[1]);
3808 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3809 if (ret != 0) {
3810 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3811 talloc_free(tmp_ctx);
3812 return -1;
3815 if (data.dsize == 0 || data.dptr == NULL) {
3816 DEBUG(DEBUG_ERR,("Record is empty\n"));
3817 talloc_free(tmp_ctx);
3818 return -1;
3821 if (argc == 3) {
3822 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3823 if (fd == -1) {
3824 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3825 talloc_free(tmp_ctx);
3826 return -1;
3828 write(fd, data.dptr, data.dsize);
3829 close(fd);
3830 } else {
3831 write(1, data.dptr, data.dsize);
3834 /* abort the transaction */
3835 talloc_free(h);
3838 talloc_free(tmp_ctx);
3839 return 0;
3843 fetch a record from a tdb-file
3845 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3847 const char *tdb_file;
3848 TDB_CONTEXT *tdb;
3849 TDB_DATA key, data;
3850 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3851 int fd;
3853 if (argc < 2) {
3854 usage();
3857 tdb_file = argv[0];
3859 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3860 if (tdb == NULL) {
3861 printf("Failed to open TDB file %s\n", tdb_file);
3862 return -1;
3865 if (!strncmp(argv[1], "0x", 2)) {
3866 key = hextodata(tmp_ctx, argv[1] + 2);
3867 if (key.dsize == 0) {
3868 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3869 return -1;
3871 } else {
3872 key.dptr = discard_const(argv[1]);
3873 key.dsize = strlen(argv[1]);
3876 data = tdb_fetch(tdb, key);
3877 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3878 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3879 tdb_close(tdb);
3880 return -1;
3883 tdb_close(tdb);
3885 if (argc == 3) {
3886 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3887 if (fd == -1) {
3888 printf("Failed to open output file %s\n", argv[2]);
3889 return -1;
3891 if (options.verbose){
3892 write(fd, data.dptr, data.dsize);
3893 } else {
3894 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3896 close(fd);
3897 } else {
3898 if (options.verbose){
3899 write(1, data.dptr, data.dsize);
3900 } else {
3901 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3905 talloc_free(tmp_ctx);
3906 return 0;
3910 store a record and header to a tdb-file
3912 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3914 const char *tdb_file;
3915 TDB_CONTEXT *tdb;
3916 TDB_DATA key, data;
3917 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3919 if (argc < 3) {
3920 usage();
3923 tdb_file = argv[0];
3925 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3926 if (tdb == NULL) {
3927 printf("Failed to open TDB file %s\n", tdb_file);
3928 return -1;
3931 if (!strncmp(argv[1], "0x", 2)) {
3932 key = hextodata(tmp_ctx, argv[1] + 2);
3933 if (key.dsize == 0) {
3934 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3935 return -1;
3937 } else {
3938 key.dptr = discard_const(argv[1]);
3939 key.dsize = strlen(argv[1]);
3942 if (!strncmp(argv[2], "0x", 2)) {
3943 data = hextodata(tmp_ctx, argv[2] + 2);
3944 if (data.dsize == 0) {
3945 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3946 return -1;
3948 } else {
3949 data.dptr = discard_const(argv[2]);
3950 data.dsize = strlen(argv[2]);
3953 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
3954 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
3955 return -1;
3957 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3958 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3959 tdb_close(tdb);
3960 return -1;
3963 tdb_close(tdb);
3965 talloc_free(tmp_ctx);
3966 return 0;
3970 write a record to a persistent database
3972 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3974 const char *db_name;
3975 struct ctdb_db_context *ctdb_db;
3976 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3977 struct ctdb_transaction_handle *h;
3978 struct stat st;
3979 TDB_DATA key, data;
3980 int fd, ret;
3982 if (argc < 3) {
3983 talloc_free(tmp_ctx);
3984 usage();
3987 fd = open(argv[2], O_RDONLY);
3988 if (fd == -1) {
3989 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3990 talloc_free(tmp_ctx);
3991 return -1;
3994 ret = fstat(fd, &st);
3995 if (ret == -1) {
3996 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3997 close(fd);
3998 talloc_free(tmp_ctx);
3999 return -1;
4002 if (!S_ISREG(st.st_mode)) {
4003 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4004 close(fd);
4005 talloc_free(tmp_ctx);
4006 return -1;
4009 data.dsize = st.st_size;
4010 if (data.dsize == 0) {
4011 data.dptr = NULL;
4012 } else {
4013 data.dptr = talloc_size(tmp_ctx, data.dsize);
4014 if (data.dptr == NULL) {
4015 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4016 close(fd);
4017 talloc_free(tmp_ctx);
4018 return -1;
4020 ret = read(fd, data.dptr, data.dsize);
4021 if (ret != data.dsize) {
4022 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4023 close(fd);
4024 talloc_free(tmp_ctx);
4025 return -1;
4028 close(fd);
4031 db_name = argv[0];
4033 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4034 if (ctdb_db == NULL) {
4035 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4036 talloc_free(tmp_ctx);
4037 return -1;
4040 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4041 if (h == NULL) {
4042 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4043 talloc_free(tmp_ctx);
4044 return -1;
4047 key.dptr = discard_const(argv[1]);
4048 key.dsize = strlen(argv[1]);
4049 ret = ctdb_transaction_store(h, key, data);
4050 if (ret != 0) {
4051 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4052 talloc_free(tmp_ctx);
4053 return -1;
4056 ret = ctdb_transaction_commit(h);
4057 if (ret != 0) {
4058 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4059 talloc_free(tmp_ctx);
4060 return -1;
4064 talloc_free(tmp_ctx);
4065 return 0;
4069 * delete a record from a persistent database
4071 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4073 const char *db_name;
4074 struct ctdb_db_context *ctdb_db;
4075 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4076 struct ctdb_transaction_handle *h;
4077 TDB_DATA key;
4078 int ret;
4079 bool persistent;
4080 uint8_t flags;
4082 if (argc < 2) {
4083 talloc_free(tmp_ctx);
4084 usage();
4087 db_name = argv[0];
4089 if (!db_exists(ctdb, db_name, NULL, &flags)) {
4090 talloc_free(tmp_ctx);
4091 return -1;
4094 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4095 if (!persistent) {
4096 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4097 talloc_free(tmp_ctx);
4098 return -1;
4101 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4102 if (ctdb_db == NULL) {
4103 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4104 talloc_free(tmp_ctx);
4105 return -1;
4108 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4109 if (h == NULL) {
4110 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4111 talloc_free(tmp_ctx);
4112 return -1;
4115 key.dptr = discard_const(argv[1]);
4116 key.dsize = strlen(argv[1]);
4117 ret = ctdb_transaction_store(h, key, tdb_null);
4118 if (ret != 0) {
4119 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4120 talloc_free(tmp_ctx);
4121 return -1;
4124 ret = ctdb_transaction_commit(h);
4125 if (ret != 0) {
4126 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4127 talloc_free(tmp_ctx);
4128 return -1;
4131 talloc_free(tmp_ctx);
4132 return 0;
4136 check if a service is bound to a port or not
4138 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4140 int s, ret;
4141 unsigned v;
4142 int port;
4143 struct sockaddr_in sin;
4145 if (argc != 1) {
4146 printf("Use: ctdb chktcport <port>\n");
4147 return EINVAL;
4150 port = atoi(argv[0]);
4152 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4153 if (s == -1) {
4154 printf("Failed to open local socket\n");
4155 return errno;
4158 v = fcntl(s, F_GETFL, 0);
4159 fcntl(s, F_SETFL, v | O_NONBLOCK);
4161 bzero(&sin, sizeof(sin));
4162 sin.sin_family = PF_INET;
4163 sin.sin_port = htons(port);
4164 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4165 close(s);
4166 if (ret == -1) {
4167 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4168 return errno;
4171 return 0;
4176 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4177 TDB_DATA data, void *private_data)
4179 DEBUG(DEBUG_ERR,("Log data received\n"));
4180 if (data.dsize > 0) {
4181 printf("%s", data.dptr);
4184 exit(0);
4188 display a list of log messages from the in memory ringbuffer
4190 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4192 int ret, i;
4193 bool main_daemon;
4194 struct ctdb_get_log_addr log_addr;
4195 TDB_DATA data;
4196 struct timeval tv;
4198 /* Process options */
4199 main_daemon = true;
4200 log_addr.pnn = ctdb_get_pnn(ctdb);
4201 log_addr.level = DEBUG_NOTICE;
4202 for (i = 0; i < argc; i++) {
4203 if (strcmp(argv[i], "recoverd") == 0) {
4204 main_daemon = false;
4205 } else {
4206 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4207 log_addr.level = get_debug_by_desc(argv[i]);
4208 } else {
4209 log_addr.level = strtol(argv[i], NULL, 0);
4214 /* Our message port is our PID */
4215 log_addr.srvid = getpid();
4217 data.dptr = (unsigned char *)&log_addr;
4218 data.dsize = sizeof(log_addr);
4220 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4222 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4223 sleep(1);
4225 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4227 if (main_daemon) {
4228 int32_t res;
4229 char *errmsg;
4230 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4232 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4233 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4234 if (ret != 0 || res != 0) {
4235 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4236 talloc_free(tmp_ctx);
4237 return -1;
4239 talloc_free(tmp_ctx);
4240 } else {
4241 ret = ctdb_client_send_message(ctdb, options.pnn,
4242 CTDB_SRVID_GETLOG, data);
4243 if (ret != 0) {
4244 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4245 return -1;
4249 tv = timeval_current();
4250 /* this loop will terminate when we have received the reply */
4251 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4252 event_loop_once(ctdb->ev);
4255 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4257 return 0;
4261 clear the in memory log area
4263 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4265 int ret;
4267 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4268 /* "recoverd" not given - get logs from main daemon */
4269 int32_t res;
4270 char *errmsg;
4271 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4273 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4274 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4275 if (ret != 0 || res != 0) {
4276 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4277 talloc_free(tmp_ctx);
4278 return -1;
4281 talloc_free(tmp_ctx);
4282 } else {
4283 TDB_DATA data; /* unused in recoverd... */
4284 data.dsize = 0;
4286 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4287 if (ret != 0) {
4288 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4289 return -1;
4293 return 0;
4296 /* Reload public IPs on a specified nodes */
4297 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4299 uint32_t *nodes;
4300 uint32_t pnn_mode;
4302 assert_single_node_only();
4304 if (argc > 1) {
4305 usage();
4308 /* Determine the nodes where IPs need to be reloaded */
4309 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
4310 options.pnn, true, &nodes, &pnn_mode)) {
4311 return -1;
4314 again:
4315 /* Disable takeover runs on all connected nodes. A reply
4316 * indicating success is needed from each node so all nodes
4317 * will need to be active. This will retry until maxruntime
4318 * is exceeded, hence no error handling.
4320 * A check could be added to not allow reloading of IPs when
4321 * there are disconnected nodes. However, this should
4322 * probably be left up to the administrator.
4324 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, LONGTIMEOUT,
4325 "Disable takeover runs", true);
4327 /* Now tell all the desired nodes to reload their public IPs.
4328 * Keep trying this until it succeeds. This assumes all
4329 * failures are transient, which might not be true...
4331 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4332 nodes, 0, LONGTIMELIMIT(),
4333 false, tdb_null,
4334 NULL, NULL, NULL) != 0) {
4335 DEBUG(DEBUG_ERR,
4336 ("Unable to reload IPs on some nodes, try again.\n"));
4337 goto again;
4340 /* It isn't strictly necessary to wait until takeover runs are
4341 * re-enabled but doing so can't hurt.
4343 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, 0,
4344 "Enable takeover runs", true);
4346 ipreallocate(ctdb);
4348 talloc_free(nodes);
4349 return 0;
4353 display a list of the databases on a remote ctdb
4355 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4357 int i, ret;
4358 struct ctdb_dbid_map *dbmap=NULL;
4360 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4361 if (ret != 0) {
4362 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4363 return ret;
4366 if(options.machinereadable){
4367 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4368 for(i=0;i<dbmap->num;i++){
4369 const char *path;
4370 const char *name;
4371 const char *health;
4372 bool persistent;
4373 bool readonly;
4374 bool sticky;
4376 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4377 dbmap->dbs[i].dbid, ctdb, &path);
4378 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4379 dbmap->dbs[i].dbid, ctdb, &name);
4380 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4381 dbmap->dbs[i].dbid, ctdb, &health);
4382 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4383 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4384 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4385 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4386 dbmap->dbs[i].dbid, name, path,
4387 !!(persistent), !!(sticky),
4388 !!(health), !!(readonly));
4390 return 0;
4393 printf("Number of databases:%d\n", dbmap->num);
4394 for(i=0;i<dbmap->num;i++){
4395 const char *path;
4396 const char *name;
4397 const char *health;
4398 bool persistent;
4399 bool readonly;
4400 bool sticky;
4402 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4403 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4404 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4405 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4406 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4407 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4408 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4409 dbmap->dbs[i].dbid, name, path,
4410 persistent?" PERSISTENT":"",
4411 sticky?" STICKY":"",
4412 readonly?" READONLY":"",
4413 health?" UNHEALTHY":"");
4416 return 0;
4420 display the status of a database on a remote ctdb
4422 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4424 const char *db_name;
4425 uint32_t db_id;
4426 uint8_t flags;
4427 const char *path;
4428 const char *health;
4430 if (argc < 1) {
4431 usage();
4434 db_name = argv[0];
4436 if (!db_exists(ctdb, db_name, &db_id, &flags)) {
4437 return -1;
4440 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4441 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4442 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4443 db_id, db_name, path,
4444 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4445 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4446 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4447 (health ? health : "OK"));
4449 return 0;
4453 check if the local node is recmaster or not
4454 it will return 1 if this node is the recmaster and 0 if it is not
4455 or if the local ctdb daemon could not be contacted
4457 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4459 uint32_t mypnn, recmaster;
4461 assert_single_node_only();
4463 mypnn = getpnn(ctdb);
4465 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
4466 printf("Failed to get the recmaster\n");
4467 return 1;
4470 if (recmaster != mypnn) {
4471 printf("this node is not the recmaster\n");
4472 return 1;
4475 printf("this node is the recmaster\n");
4476 return 0;
4480 ping a node
4482 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4484 int ret;
4485 struct timeval tv = timeval_current();
4486 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4487 if (ret == -1) {
4488 printf("Unable to get ping response from node %u\n", options.pnn);
4489 return -1;
4490 } else {
4491 printf("response from %u time=%.6f sec (%d clients)\n",
4492 options.pnn, timeval_elapsed(&tv), ret);
4494 return 0;
4499 get a node's runstate
4501 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4503 int ret;
4504 enum ctdb_runstate runstate;
4506 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4507 if (ret == -1) {
4508 printf("Unable to get runstate response from node %u\n",
4509 options.pnn);
4510 return -1;
4511 } else {
4512 bool found = true;
4513 enum ctdb_runstate t;
4514 int i;
4515 for (i=0; i<argc; i++) {
4516 found = false;
4517 t = runstate_from_string(argv[i]);
4518 if (t == CTDB_RUNSTATE_UNKNOWN) {
4519 printf("Invalid run state (%s)\n", argv[i]);
4520 return -1;
4523 if (t == runstate) {
4524 found = true;
4525 break;
4529 if (!found) {
4530 printf("CTDB not in required run state (got %s)\n",
4531 runstate_to_string((enum ctdb_runstate)runstate));
4532 return -1;
4536 printf("%s\n", runstate_to_string(runstate));
4537 return 0;
4542 get a tunable
4544 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4546 const char *name;
4547 uint32_t value;
4548 int ret;
4550 if (argc < 1) {
4551 usage();
4554 name = argv[0];
4555 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4556 if (ret != 0) {
4557 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4558 return -1;
4561 printf("%-23s = %u\n", name, value);
4562 return 0;
4566 set a tunable
4568 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4570 const char *name;
4571 uint32_t value;
4572 int ret;
4574 if (argc < 2) {
4575 usage();
4578 name = argv[0];
4579 value = strtoul(argv[1], NULL, 0);
4581 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4582 if (ret == -1) {
4583 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4584 return -1;
4586 return 0;
4590 list all tunables
4592 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4594 uint32_t count;
4595 const char **list;
4596 int ret, i;
4598 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4599 if (ret == -1) {
4600 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4601 return -1;
4604 for (i=0;i<count;i++) {
4605 control_getvar(ctdb, 1, &list[i]);
4608 talloc_free(list);
4610 return 0;
4614 display debug level on a node
4616 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4618 int ret;
4619 int32_t level;
4621 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4622 if (ret != 0) {
4623 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4624 return ret;
4625 } else {
4626 if (options.machinereadable){
4627 printf(":Name:Level:\n");
4628 printf(":%s:%d:\n",get_debug_by_level(level),level);
4629 } else {
4630 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4633 return 0;
4637 display reclock file of a node
4639 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4641 int ret;
4642 const char *reclock;
4644 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4645 if (ret != 0) {
4646 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4647 return ret;
4648 } else {
4649 if (options.machinereadable){
4650 if (reclock != NULL) {
4651 printf("%s", reclock);
4653 } else {
4654 if (reclock == NULL) {
4655 printf("No reclock file used.\n");
4656 } else {
4657 printf("Reclock file:%s\n", reclock);
4661 return 0;
4665 set the reclock file of a node
4667 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4669 int ret;
4670 const char *reclock;
4672 if (argc == 0) {
4673 reclock = NULL;
4674 } else if (argc == 1) {
4675 reclock = argv[0];
4676 } else {
4677 usage();
4680 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4681 if (ret != 0) {
4682 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4683 return ret;
4685 return 0;
4689 set the natgw state on/off
4691 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4693 int ret;
4694 uint32_t natgwstate;
4696 if (argc == 0) {
4697 usage();
4700 if (!strcmp(argv[0], "on")) {
4701 natgwstate = 1;
4702 } else if (!strcmp(argv[0], "off")) {
4703 natgwstate = 0;
4704 } else {
4705 usage();
4708 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4709 if (ret != 0) {
4710 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4711 return ret;
4714 return 0;
4718 set the lmaster role on/off
4720 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4722 int ret;
4723 uint32_t lmasterrole;
4725 if (argc == 0) {
4726 usage();
4729 if (!strcmp(argv[0], "on")) {
4730 lmasterrole = 1;
4731 } else if (!strcmp(argv[0], "off")) {
4732 lmasterrole = 0;
4733 } else {
4734 usage();
4737 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4738 if (ret != 0) {
4739 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4740 return ret;
4743 return 0;
4747 set the recmaster role on/off
4749 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4751 int ret;
4752 uint32_t recmasterrole;
4754 if (argc == 0) {
4755 usage();
4758 if (!strcmp(argv[0], "on")) {
4759 recmasterrole = 1;
4760 } else if (!strcmp(argv[0], "off")) {
4761 recmasterrole = 0;
4762 } else {
4763 usage();
4766 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4767 if (ret != 0) {
4768 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4769 return ret;
4772 return 0;
4776 set debug level on a node or all nodes
4778 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4780 int i, ret;
4781 int32_t level;
4783 if (argc == 0) {
4784 printf("You must specify the debug level. Valid levels are:\n");
4785 for (i=0; debug_levels[i].description != NULL; i++) {
4786 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4789 return 0;
4792 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4793 level = get_debug_by_desc(argv[0]);
4794 } else {
4795 level = strtol(argv[0], NULL, 0);
4798 for (i=0; debug_levels[i].description != NULL; i++) {
4799 if (level == debug_levels[i].level) {
4800 break;
4803 if (debug_levels[i].description == NULL) {
4804 printf("Invalid debug level, must be one of\n");
4805 for (i=0; debug_levels[i].description != NULL; i++) {
4806 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4808 return -1;
4811 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4812 if (ret != 0) {
4813 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4815 return 0;
4820 thaw a node
4822 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4824 int ret;
4825 uint32_t priority;
4827 if (argc == 1) {
4828 priority = strtol(argv[0], NULL, 0);
4829 } else {
4830 priority = 0;
4832 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4834 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4835 if (ret != 0) {
4836 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4838 return 0;
4843 attach to a database
4845 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4847 const char *db_name;
4848 struct ctdb_db_context *ctdb_db;
4849 bool persistent = false;
4851 if (argc < 1) {
4852 usage();
4854 db_name = argv[0];
4855 if (argc > 2) {
4856 usage();
4858 if (argc == 2) {
4859 if (strcmp(argv[1], "persistent") != 0) {
4860 usage();
4862 persistent = true;
4865 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4866 if (ctdb_db == NULL) {
4867 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4868 return -1;
4871 return 0;
4875 set db priority
4877 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4879 struct ctdb_db_priority db_prio;
4880 int ret;
4882 if (argc < 2) {
4883 usage();
4886 db_prio.db_id = strtoul(argv[0], NULL, 0);
4887 db_prio.priority = strtoul(argv[1], NULL, 0);
4889 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4890 if (ret != 0) {
4891 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4892 return -1;
4895 return 0;
4899 get db priority
4901 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4903 uint32_t db_id, priority;
4904 int ret;
4906 if (argc < 1) {
4907 usage();
4910 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4911 return -1;
4914 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4915 if (ret != 0) {
4916 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4917 return -1;
4920 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4922 return 0;
4926 set the sticky records capability for a database
4928 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
4930 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4931 uint32_t db_id;
4932 int ret;
4934 if (argc < 1) {
4935 usage();
4938 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4939 return -1;
4942 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
4943 if (ret != 0) {
4944 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
4945 talloc_free(tmp_ctx);
4946 return -1;
4949 talloc_free(tmp_ctx);
4950 return 0;
4954 set the readonly capability for a database
4956 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4958 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4959 uint32_t db_id;
4960 int ret;
4962 if (argc < 1) {
4963 usage();
4966 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4967 return -1;
4970 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4971 if (ret != 0) {
4972 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4973 talloc_free(tmp_ctx);
4974 return -1;
4977 talloc_free(tmp_ctx);
4978 return 0;
4982 get db seqnum
4984 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4986 bool ret;
4987 uint32_t db_id;
4988 uint64_t seqnum;
4990 if (argc < 1) {
4991 usage();
4994 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4995 return -1;
4998 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4999 if (!ret) {
5000 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5001 return -1;
5004 printf("Sequence number:%lld\n", (long long)seqnum);
5006 return 0;
5010 * set db seqnum
5012 static int control_setdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5014 bool ret;
5015 struct ctdb_db_context *ctdb_db;
5016 uint32_t db_id;
5017 uint8_t flags;
5018 uint64_t old_seqnum, new_seqnum;
5019 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5020 struct ctdb_transaction_handle *h;
5021 TDB_DATA key, data;
5022 bool persistent;
5024 if (argc != 2) {
5025 talloc_free(tmp_ctx);
5026 usage();
5029 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5030 talloc_free(tmp_ctx);
5031 return -1;
5034 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5035 if (!persistent) {
5036 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", argv[0]));
5037 talloc_free(tmp_ctx);
5038 return -1;
5041 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &old_seqnum);
5042 if (!ret) {
5043 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5044 talloc_free(tmp_ctx);
5045 return -1;
5048 new_seqnum = strtoull(argv[1], NULL, 0);
5049 if (new_seqnum <= old_seqnum) {
5050 DEBUG(DEBUG_ERR, ("New sequence number is less than current sequence number\n"));
5051 talloc_free(tmp_ctx);
5052 return -1;
5055 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], persistent, 0);
5056 if (ctdb_db == NULL) {
5057 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5058 talloc_free(tmp_ctx);
5059 return -1;
5062 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
5063 if (h == NULL) {
5064 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", argv[0]));
5065 talloc_free(tmp_ctx);
5066 return -1;
5069 key.dptr = (uint8_t *)discard_const(CTDB_DB_SEQNUM_KEY);
5070 key.dsize = strlen(CTDB_DB_SEQNUM_KEY) + 1;
5072 data.dsize = sizeof(new_seqnum);
5073 data.dptr = talloc_size(tmp_ctx, data.dsize);
5074 *data.dptr = new_seqnum;
5076 ret = ctdb_transaction_store(h, key, data);
5077 if (ret != 0) {
5078 DEBUG(DEBUG_ERR,("Failed to store record\n"));
5079 talloc_free(tmp_ctx);
5080 return -1;
5083 ret = ctdb_transaction_commit(h);
5084 if (ret != 0) {
5085 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
5086 talloc_free(tmp_ctx);
5087 return -1;
5090 talloc_free(tmp_ctx);
5091 return 0;
5095 run an eventscript on a node
5097 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5099 TDB_DATA data;
5100 int ret;
5101 int32_t res;
5102 char *errmsg;
5103 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5105 if (argc != 1) {
5106 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5107 return -1;
5110 data.dptr = (unsigned char *)discard_const(argv[0]);
5111 data.dsize = strlen((char *)data.dptr) + 1;
5113 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5115 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5116 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5117 if (ret != 0 || res != 0) {
5118 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5119 talloc_free(tmp_ctx);
5120 return -1;
5122 talloc_free(tmp_ctx);
5123 return 0;
5126 #define DB_VERSION 1
5127 #define MAX_DB_NAME 64
5128 struct db_file_header {
5129 unsigned long version;
5130 time_t timestamp;
5131 unsigned long persistent;
5132 unsigned long size;
5133 const char name[MAX_DB_NAME];
5136 struct backup_data {
5137 struct ctdb_marshall_buffer *records;
5138 uint32_t len;
5139 uint32_t total;
5140 bool traverse_error;
5143 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5145 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5146 struct ctdb_rec_data *rec;
5148 /* add the record */
5149 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5150 if (rec == NULL) {
5151 bd->traverse_error = true;
5152 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5153 return -1;
5155 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5156 if (bd->records == NULL) {
5157 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5158 bd->traverse_error = true;
5159 return -1;
5161 bd->records->count++;
5162 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5163 bd->len += rec->length;
5164 talloc_free(rec);
5166 bd->total++;
5167 return 0;
5171 * backup a database to a file
5173 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5175 int ret;
5176 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5177 struct db_file_header dbhdr;
5178 struct ctdb_db_context *ctdb_db;
5179 struct backup_data *bd;
5180 int fh = -1;
5181 int status = -1;
5182 const char *reason = NULL;
5183 uint32_t db_id;
5184 uint8_t flags;
5186 assert_single_node_only();
5188 if (argc != 2) {
5189 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5190 return -1;
5193 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5194 return -1;
5197 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5198 db_id, tmp_ctx, &reason);
5199 if (ret != 0) {
5200 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5201 argv[0]));
5202 talloc_free(tmp_ctx);
5203 return -1;
5205 if (reason) {
5206 uint32_t allow_unhealthy = 0;
5208 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5209 "AllowUnhealthyDBRead",
5210 &allow_unhealthy);
5212 if (allow_unhealthy != 1) {
5213 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5214 argv[0], reason));
5216 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5217 allow_unhealthy));
5218 talloc_free(tmp_ctx);
5219 return -1;
5222 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5223 argv[0], argv[0]));
5224 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5225 "tunnable AllowUnhealthyDBRead = %u\n",
5226 allow_unhealthy));
5229 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5230 if (ctdb_db == NULL) {
5231 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5232 talloc_free(tmp_ctx);
5233 return -1;
5237 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5238 if (ret == -1) {
5239 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5240 talloc_free(tmp_ctx);
5241 return -1;
5245 bd = talloc_zero(tmp_ctx, struct backup_data);
5246 if (bd == NULL) {
5247 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5248 talloc_free(tmp_ctx);
5249 return -1;
5252 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5253 if (bd->records == NULL) {
5254 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5255 talloc_free(tmp_ctx);
5256 return -1;
5259 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5260 bd->records->db_id = ctdb_db->db_id;
5261 /* traverse the database collecting all records */
5262 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5263 bd->traverse_error) {
5264 DEBUG(DEBUG_ERR,("Traverse error\n"));
5265 talloc_free(tmp_ctx);
5266 return -1;
5269 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5272 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5273 if (fh == -1) {
5274 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5275 talloc_free(tmp_ctx);
5276 return -1;
5279 dbhdr.version = DB_VERSION;
5280 dbhdr.timestamp = time(NULL);
5281 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5282 dbhdr.size = bd->len;
5283 if (strlen(argv[0]) >= MAX_DB_NAME) {
5284 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5285 goto done;
5287 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
5288 ret = write(fh, &dbhdr, sizeof(dbhdr));
5289 if (ret == -1) {
5290 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5291 goto done;
5293 ret = write(fh, bd->records, bd->len);
5294 if (ret == -1) {
5295 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5296 goto done;
5299 status = 0;
5300 done:
5301 if (fh != -1) {
5302 ret = close(fh);
5303 if (ret == -1) {
5304 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5308 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5310 talloc_free(tmp_ctx);
5311 return status;
5315 * restore a database from a file
5317 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5319 int ret;
5320 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5321 TDB_DATA outdata;
5322 TDB_DATA data;
5323 struct db_file_header dbhdr;
5324 struct ctdb_db_context *ctdb_db;
5325 struct ctdb_node_map *nodemap=NULL;
5326 struct ctdb_vnn_map *vnnmap=NULL;
5327 int i, fh;
5328 struct ctdb_control_wipe_database w;
5329 uint32_t *nodes;
5330 uint32_t generation;
5331 struct tm *tm;
5332 char tbuf[100];
5333 char *dbname;
5335 assert_single_node_only();
5337 if (argc < 1 || argc > 2) {
5338 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5339 return -1;
5342 fh = open(argv[0], O_RDONLY);
5343 if (fh == -1) {
5344 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5345 talloc_free(tmp_ctx);
5346 return -1;
5349 read(fh, &dbhdr, sizeof(dbhdr));
5350 if (dbhdr.version != DB_VERSION) {
5351 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5352 talloc_free(tmp_ctx);
5353 return -1;
5356 dbname = discard_const(dbhdr.name);
5357 if (argc == 2) {
5358 dbname = discard_const(argv[1]);
5361 outdata.dsize = dbhdr.size;
5362 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5363 if (outdata.dptr == NULL) {
5364 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5365 close(fh);
5366 talloc_free(tmp_ctx);
5367 return -1;
5369 read(fh, outdata.dptr, outdata.dsize);
5370 close(fh);
5372 tm = localtime(&dbhdr.timestamp);
5373 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5374 printf("Restoring database '%s' from backup @ %s\n",
5375 dbname, tbuf);
5378 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5379 if (ctdb_db == NULL) {
5380 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5381 talloc_free(tmp_ctx);
5382 return -1;
5385 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5386 if (ret != 0) {
5387 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5388 talloc_free(tmp_ctx);
5389 return ret;
5393 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5394 if (ret != 0) {
5395 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5396 talloc_free(tmp_ctx);
5397 return ret;
5400 /* freeze all nodes */
5401 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5402 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5403 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5404 nodes, i,
5405 TIMELIMIT(),
5406 false, tdb_null,
5407 NULL, NULL,
5408 NULL) != 0) {
5409 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5410 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5411 talloc_free(tmp_ctx);
5412 return -1;
5416 generation = vnnmap->generation;
5417 data.dptr = (void *)&generation;
5418 data.dsize = sizeof(generation);
5420 /* start a cluster wide transaction */
5421 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5422 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5423 nodes, 0,
5424 TIMELIMIT(), false, data,
5425 NULL, NULL,
5426 NULL) != 0) {
5427 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5428 return -1;
5432 w.db_id = ctdb_db->db_id;
5433 w.transaction_id = generation;
5435 data.dptr = (void *)&w;
5436 data.dsize = sizeof(w);
5438 /* wipe all the remote databases. */
5439 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5440 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5441 nodes, 0,
5442 TIMELIMIT(), false, data,
5443 NULL, NULL,
5444 NULL) != 0) {
5445 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5446 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5447 talloc_free(tmp_ctx);
5448 return -1;
5451 /* push the database */
5452 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5453 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5454 nodes, 0,
5455 TIMELIMIT(), false, outdata,
5456 NULL, NULL,
5457 NULL) != 0) {
5458 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5459 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5460 talloc_free(tmp_ctx);
5461 return -1;
5464 data.dptr = (void *)&ctdb_db->db_id;
5465 data.dsize = sizeof(ctdb_db->db_id);
5467 /* mark the database as healthy */
5468 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5469 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5470 nodes, 0,
5471 TIMELIMIT(), false, data,
5472 NULL, NULL,
5473 NULL) != 0) {
5474 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5475 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5476 talloc_free(tmp_ctx);
5477 return -1;
5480 data.dptr = (void *)&generation;
5481 data.dsize = sizeof(generation);
5483 /* commit all the changes */
5484 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5485 nodes, 0,
5486 TIMELIMIT(), false, data,
5487 NULL, NULL,
5488 NULL) != 0) {
5489 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5490 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5491 talloc_free(tmp_ctx);
5492 return -1;
5496 /* thaw all nodes */
5497 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5498 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5499 nodes, 0,
5500 TIMELIMIT(),
5501 false, tdb_null,
5502 NULL, NULL,
5503 NULL) != 0) {
5504 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5505 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5506 talloc_free(tmp_ctx);
5507 return -1;
5511 talloc_free(tmp_ctx);
5512 return 0;
5516 * dump a database backup from a file
5518 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5520 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5521 TDB_DATA outdata;
5522 struct db_file_header dbhdr;
5523 int i, fh;
5524 struct tm *tm;
5525 char tbuf[100];
5526 struct ctdb_rec_data *rec = NULL;
5527 struct ctdb_marshall_buffer *m;
5528 struct ctdb_dump_db_context c;
5530 assert_single_node_only();
5532 if (argc != 1) {
5533 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5534 return -1;
5537 fh = open(argv[0], O_RDONLY);
5538 if (fh == -1) {
5539 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5540 talloc_free(tmp_ctx);
5541 return -1;
5544 read(fh, &dbhdr, sizeof(dbhdr));
5545 if (dbhdr.version != DB_VERSION) {
5546 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5547 talloc_free(tmp_ctx);
5548 return -1;
5551 outdata.dsize = dbhdr.size;
5552 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5553 if (outdata.dptr == NULL) {
5554 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5555 close(fh);
5556 talloc_free(tmp_ctx);
5557 return -1;
5559 read(fh, outdata.dptr, outdata.dsize);
5560 close(fh);
5561 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5563 tm = localtime(&dbhdr.timestamp);
5564 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5565 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5566 dbhdr.name, m->db_id, tbuf);
5568 ZERO_STRUCT(c);
5569 c.f = stdout;
5570 c.printemptyrecords = (bool)options.printemptyrecords;
5571 c.printdatasize = (bool)options.printdatasize;
5572 c.printlmaster = false;
5573 c.printhash = (bool)options.printhash;
5574 c.printrecordflags = (bool)options.printrecordflags;
5576 for (i=0; i < m->count; i++) {
5577 uint32_t reqid = 0;
5578 TDB_DATA key, data;
5580 /* we do not want the header splitted, so we pass NULL*/
5581 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5582 NULL, &key, &data);
5584 ctdb_dumpdb_record(ctdb, key, data, &c);
5587 printf("Dumped %d records\n", i);
5588 talloc_free(tmp_ctx);
5589 return 0;
5593 * wipe a database from a file
5595 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5596 const char **argv)
5598 int ret;
5599 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5600 TDB_DATA data;
5601 struct ctdb_db_context *ctdb_db;
5602 struct ctdb_node_map *nodemap = NULL;
5603 struct ctdb_vnn_map *vnnmap = NULL;
5604 int i;
5605 struct ctdb_control_wipe_database w;
5606 uint32_t *nodes;
5607 uint32_t generation;
5608 uint8_t flags;
5610 assert_single_node_only();
5612 if (argc != 1) {
5613 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5614 return -1;
5617 if (!db_exists(ctdb, argv[0], NULL, &flags)) {
5618 return -1;
5621 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5622 if (ctdb_db == NULL) {
5623 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5624 argv[0]));
5625 talloc_free(tmp_ctx);
5626 return -1;
5629 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5630 &nodemap);
5631 if (ret != 0) {
5632 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5633 options.pnn));
5634 talloc_free(tmp_ctx);
5635 return ret;
5638 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5639 &vnnmap);
5640 if (ret != 0) {
5641 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5642 options.pnn));
5643 talloc_free(tmp_ctx);
5644 return ret;
5647 /* freeze all nodes */
5648 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5649 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5650 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5651 nodes, i,
5652 TIMELIMIT(),
5653 false, tdb_null,
5654 NULL, NULL,
5655 NULL);
5656 if (ret != 0) {
5657 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5658 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5659 CTDB_RECOVERY_ACTIVE);
5660 talloc_free(tmp_ctx);
5661 return -1;
5665 generation = vnnmap->generation;
5666 data.dptr = (void *)&generation;
5667 data.dsize = sizeof(generation);
5669 /* start a cluster wide transaction */
5670 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5671 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5672 nodes, 0,
5673 TIMELIMIT(), false, data,
5674 NULL, NULL,
5675 NULL);
5676 if (ret!= 0) {
5677 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5678 "transactions.\n"));
5679 return -1;
5682 w.db_id = ctdb_db->db_id;
5683 w.transaction_id = generation;
5685 data.dptr = (void *)&w;
5686 data.dsize = sizeof(w);
5688 /* wipe all the remote databases. */
5689 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5690 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5691 nodes, 0,
5692 TIMELIMIT(), false, data,
5693 NULL, NULL,
5694 NULL) != 0) {
5695 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5696 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5697 talloc_free(tmp_ctx);
5698 return -1;
5701 data.dptr = (void *)&ctdb_db->db_id;
5702 data.dsize = sizeof(ctdb_db->db_id);
5704 /* mark the database as healthy */
5705 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5706 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5707 nodes, 0,
5708 TIMELIMIT(), false, data,
5709 NULL, NULL,
5710 NULL) != 0) {
5711 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5712 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5713 talloc_free(tmp_ctx);
5714 return -1;
5717 data.dptr = (void *)&generation;
5718 data.dsize = sizeof(generation);
5720 /* commit all the changes */
5721 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5722 nodes, 0,
5723 TIMELIMIT(), false, data,
5724 NULL, NULL,
5725 NULL) != 0) {
5726 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5727 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5728 talloc_free(tmp_ctx);
5729 return -1;
5732 /* thaw all nodes */
5733 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5734 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5735 nodes, 0,
5736 TIMELIMIT(),
5737 false, tdb_null,
5738 NULL, NULL,
5739 NULL) != 0) {
5740 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5741 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5742 talloc_free(tmp_ctx);
5743 return -1;
5746 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5748 talloc_free(tmp_ctx);
5749 return 0;
5753 dump memory usage
5755 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5757 TDB_DATA data;
5758 int ret;
5759 int32_t res;
5760 char *errmsg;
5761 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5762 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5763 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5764 if (ret != 0 || res != 0) {
5765 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5766 talloc_free(tmp_ctx);
5767 return -1;
5769 write(1, data.dptr, data.dsize);
5770 talloc_free(tmp_ctx);
5771 return 0;
5775 handler for memory dumps
5777 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5778 TDB_DATA data, void *private_data)
5780 write(1, data.dptr, data.dsize);
5781 exit(0);
5785 dump memory usage on the recovery daemon
5787 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5789 int ret;
5790 TDB_DATA data;
5791 struct srvid_request rd;
5793 rd.pnn = ctdb_get_pnn(ctdb);
5794 rd.srvid = getpid();
5796 /* register a message port for receiveing the reply so that we
5797 can receive the reply
5799 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5802 data.dptr = (uint8_t *)&rd;
5803 data.dsize = sizeof(rd);
5805 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5806 if (ret != 0) {
5807 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5808 return -1;
5811 /* this loop will terminate when we have received the reply */
5812 while (1) {
5813 event_loop_once(ctdb->ev);
5816 return 0;
5820 send a message to a srvid
5822 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5824 unsigned long srvid;
5825 int ret;
5826 TDB_DATA data;
5828 if (argc < 2) {
5829 usage();
5832 srvid = strtoul(argv[0], NULL, 0);
5834 data.dptr = (uint8_t *)discard_const(argv[1]);
5835 data.dsize= strlen(argv[1]);
5837 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5838 if (ret != 0) {
5839 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5840 return -1;
5843 return 0;
5847 handler for msglisten
5849 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5850 TDB_DATA data, void *private_data)
5852 int i;
5854 printf("Message received: ");
5855 for (i=0;i<data.dsize;i++) {
5856 printf("%c", data.dptr[i]);
5858 printf("\n");
5862 listen for messages on a messageport
5864 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5866 uint64_t srvid;
5868 srvid = getpid();
5870 /* register a message port and listen for messages
5872 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5873 printf("Listening for messages on srvid:%d\n", (int)srvid);
5875 while (1) {
5876 event_loop_once(ctdb->ev);
5879 return 0;
5883 list all nodes in the cluster
5884 we parse the nodes file directly
5886 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5888 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5889 struct pnn_node *pnn_nodes;
5890 struct pnn_node *pnn_node;
5892 assert_single_node_only();
5894 pnn_nodes = read_nodes_file(mem_ctx);
5895 if (pnn_nodes == NULL) {
5896 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5897 talloc_free(mem_ctx);
5898 return -1;
5901 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5902 ctdb_sock_addr addr;
5903 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5904 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5905 talloc_free(mem_ctx);
5906 return -1;
5908 if (options.machinereadable){
5909 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5910 } else {
5911 printf("%s\n", pnn_node->addr);
5914 talloc_free(mem_ctx);
5916 return 0;
5920 reload the nodes file on the local node
5922 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5924 int i, ret;
5925 int mypnn;
5926 struct ctdb_node_map *nodemap=NULL;
5928 assert_single_node_only();
5930 mypnn = ctdb_get_pnn(ctdb);
5932 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5933 if (ret != 0) {
5934 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5935 return ret;
5938 /* reload the nodes file on all remote nodes */
5939 for (i=0;i<nodemap->num;i++) {
5940 if (nodemap->nodes[i].pnn == mypnn) {
5941 continue;
5943 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5944 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5945 nodemap->nodes[i].pnn);
5946 if (ret != 0) {
5947 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5951 /* reload the nodes file on the local node */
5952 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5953 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5954 if (ret != 0) {
5955 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5958 /* initiate a recovery */
5959 control_recover(ctdb, argc, argv);
5961 return 0;
5965 static const struct {
5966 const char *name;
5967 int (*fn)(struct ctdb_context *, int, const char **);
5968 bool auto_all;
5969 bool without_daemon; /* can be run without daemon running ? */
5970 const char *msg;
5971 const char *args;
5972 } ctdb_commands[] = {
5973 { "version", control_version, true, true, "show version of ctdb" },
5974 { "status", control_status, true, false, "show node status" },
5975 { "uptime", control_uptime, true, false, "show node uptime" },
5976 { "ping", control_ping, true, false, "ping all nodes" },
5977 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
5978 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5979 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5980 { "listvars", control_listvars, true, false, "list tunable variables"},
5981 { "statistics", control_statistics, false, false, "show statistics" },
5982 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5983 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5984 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5985 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5986 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5987 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5988 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5989 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5990 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
5991 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
5992 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
5993 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5994 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5995 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5996 { "lvs", control_lvs, true, false, "show lvs configuration" },
5997 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5998 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5999 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6000 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6001 { "getdebug", control_getdebug, true, false, "get debug level" },
6002 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6003 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6004 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6005 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6006 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6007 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6008 { "disable", control_disable, true, false, "disable a nodes public IP" },
6009 { "enable", control_enable, true, false, "enable a nodes public IP" },
6010 { "stop", control_stop, true, false, "stop a node" },
6011 { "continue", control_continue, true, false, "re-start a stopped node" },
6012 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
6013 { "unban", control_unban, true, false, "unban a node" },
6014 { "showban", control_showban, true, false, "show ban information"},
6015 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6016 { "recover", control_recover, true, false, "force recovery" },
6017 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6018 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6019 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6020 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6021 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6022 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6023 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6024 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6025 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6027 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6029 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6030 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6031 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6032 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6033 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6034 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
6035 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6036 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6037 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6038 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6039 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6040 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6041 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6042 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6043 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6044 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6045 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6046 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6047 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6048 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6049 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6050 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6051 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6052 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6053 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6054 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6055 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6056 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6057 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6058 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6059 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6060 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6061 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6062 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6063 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6064 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6065 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6066 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6067 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6068 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
6069 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
6070 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
6071 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6072 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6073 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6074 { "setdbseqnum", control_setdbseqnum, false, false, "set the sequence number for a database", "<dbname|dbid> <seqnum>" },
6075 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6076 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6077 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6078 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6082 show usage message
6084 static void usage(void)
6086 int i;
6087 printf(
6088 "Usage: ctdb [options] <control>\n" \
6089 "Options:\n" \
6090 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6091 " -Y generate machinereadable output\n"
6092 " -v generate verbose output\n"
6093 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6094 printf("Controls:\n");
6095 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6096 printf(" %-15s %-27s %s\n",
6097 ctdb_commands[i].name,
6098 ctdb_commands[i].args?ctdb_commands[i].args:"",
6099 ctdb_commands[i].msg);
6101 exit(1);
6105 static void ctdb_alarm(int sig)
6107 printf("Maximum runtime exceeded - exiting\n");
6108 _exit(ERR_TIMEOUT);
6112 main program
6114 int main(int argc, const char *argv[])
6116 struct ctdb_context *ctdb;
6117 char *nodestring = NULL;
6118 struct poptOption popt_options[] = {
6119 POPT_AUTOHELP
6120 POPT_CTDB_CMDLINE
6121 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6122 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6123 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6124 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6125 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6126 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6127 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6128 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6129 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6130 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6131 POPT_TABLEEND
6133 int opt;
6134 const char **extra_argv;
6135 int extra_argc = 0;
6136 int ret=-1, i;
6137 poptContext pc;
6138 struct event_context *ev;
6139 const char *control;
6140 const char *socket_name;
6142 setlinebuf(stdout);
6144 /* set some defaults */
6145 options.maxruntime = 0;
6146 options.timelimit = 10;
6147 options.pnn = CTDB_CURRENT_NODE;
6149 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6151 while ((opt = poptGetNextOpt(pc)) != -1) {
6152 switch (opt) {
6153 default:
6154 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6155 poptBadOption(pc, 0), poptStrerror(opt)));
6156 exit(1);
6160 /* setup the remaining options for the main program to use */
6161 extra_argv = poptGetArgs(pc);
6162 if (extra_argv) {
6163 extra_argv++;
6164 while (extra_argv[extra_argc]) extra_argc++;
6167 if (extra_argc < 1) {
6168 usage();
6171 if (options.maxruntime == 0) {
6172 const char *ctdb_timeout;
6173 ctdb_timeout = getenv("CTDB_TIMEOUT");
6174 if (ctdb_timeout != NULL) {
6175 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6176 } else {
6177 /* default timeout is 120 seconds */
6178 options.maxruntime = 120;
6182 signal(SIGALRM, ctdb_alarm);
6183 alarm(options.maxruntime);
6185 control = extra_argv[0];
6187 ev = event_context_init(NULL);
6188 if (!ev) {
6189 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6190 exit(1);
6193 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6194 if (strcmp(control, ctdb_commands[i].name) == 0) {
6195 break;
6199 if (i == ARRAY_SIZE(ctdb_commands)) {
6200 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6201 exit(1);
6204 if (ctdb_commands[i].without_daemon == true) {
6205 if (nodestring != NULL) {
6206 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6207 exit(1);
6209 close(2);
6210 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6213 /* initialise ctdb */
6214 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6216 if (ctdb == NULL) {
6217 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6218 exit(1);
6221 /* initialize a libctdb connection as well */
6222 socket_name = ctdb_get_socketname(ctdb);
6223 ctdb_connection = ctdb_connect(socket_name,
6224 ctdb_log_file, stderr);
6225 if (ctdb_connection == NULL) {
6226 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
6227 exit(1);
6230 /* setup the node number(s) to contact */
6231 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
6232 &options.nodes, &options.pnn)) {
6233 usage();
6236 if (options.pnn == CTDB_CURRENT_NODE) {
6237 options.pnn = options.nodes[0];
6240 if (ctdb_commands[i].auto_all &&
6241 ((options.pnn == CTDB_BROADCAST_ALL) ||
6242 (options.pnn == CTDB_MULTICAST))) {
6243 int j;
6245 ret = 0;
6246 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6247 options.pnn = options.nodes[j];
6248 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6250 } else {
6251 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6254 ctdb_disconnect(ctdb_connection);
6255 talloc_free(ctdb);
6256 talloc_free(ev);
6257 (void)poptFreeContext(pc);
6259 return ret;