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/>.
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "system/network.h"
25 #include "system/locale.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"
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);
50 int printemptyrecords;
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);
68 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
69 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
70 "Out of memory in " __location__ )); \
74 static uint32_t getpnn(struct ctdb_context *ctdb)
76 if ((options.pnn == CTDB_BROADCAST_ALL) ||
77 (options.pnn == CTDB_MULTICAST)) {
79 ("Cannot get PNN for node %u\n", options.pnn));
83 if (options.pnn == CTDB_CURRENT_NODE) {
84 return ctdb_get_pnn(ctdb);
90 static void assert_single_node_only(void)
92 if ((options.pnn == CTDB_BROADCAST_ALL) ||
93 (options.pnn == CTDB_MULTICAST)) {
95 ("This control can not be applied to multiple PNNs\n"));
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)
106 static const struct {
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 */
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);
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");
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;
145 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
148 TDB_DATA key = {NULL, 0};
152 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
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]);
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,
180 struct ctdb_node_map *nodemap;
185 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
186 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
190 if (nodestring != NULL) {
191 *nodes = talloc_array(ctdb, uint32_t, 0);
192 CTDB_NOMEM_ABORT(*nodes);
196 if (strcmp(nodestring, "all") == 0) {
197 *pnn_mode = CTDB_BROADCAST_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) {
206 *nodes = talloc_realloc(ctdb, *nodes,
208 CTDB_NOMEM_ABORT(*nodes);
216 ns = talloc_strdup(ctdb, nodestring);
217 tok = strtok(ns, ",");
218 while (tok != NULL) {
220 i = (uint32_t)strtoul(tok, NULL, 0);
221 if (i >= nodemap->num) {
222 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
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)));
231 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
232 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
236 *nodes = talloc_realloc(ctdb, *nodes,
238 CTDB_NOMEM_ABORT(*nodes);
243 tok = strtok(NULL, ",");
248 *pnn_mode = (*nodes)[0];
250 *pnn_mode = CTDB_MULTICAST;
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,
265 ctdb_free_nodemap(nodemap);
271 check if a database exists
273 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg, uint32_t *dbid, uint8_t *flags)
276 struct ctdb_dbid_map *dbmap=NULL;
277 bool dbid_given = false, found = false;
279 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
281 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
283 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
287 if (strncmp(dbarg, "0x", 2) == 0) {
288 id = strtoul(dbarg, NULL, 0);
292 for(i=0; i<dbmap->num; i++) {
294 if (id == dbmap->dbs[i].dbid) {
300 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
302 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
306 if (strcmp(name, dbarg) == 0) {
307 id = dbmap->dbs[i].dbid;
315 if (dbid) *dbid = id;
316 if (flags) *flags = dbmap->dbs[i].flags;
318 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
322 talloc_free(tmp_ctx);
327 see if a process exists
329 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
337 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
338 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
342 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
344 printf("%u:%u exists\n", pnn, pid);
346 printf("%u:%u does not exist\n", pnn, pid);
352 display statistics structure
354 static void show_statistics(struct ctdb_statistics *s, int show_header)
356 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
358 const char *prefix=NULL;
360 int tmp, days, hours, minutes, seconds;
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;
413 if (options.machinereadable){
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:");
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);
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);
488 printf(" %*s%-22s%*s%10u\n",
490 fields[i].name+preflen,
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]);
499 printf(" lock_buckets:");
500 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
501 printf(" %d", s->locks.buckets[i]);
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)
523 struct ctdb_statistics statistics;
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;
535 uint32_t *v1 = (uint32_t *)&s1;
536 uint32_t *v2 = (uint32_t *)&statistics;
538 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
539 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
541 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
544 for (j=0;j<num_ints;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);
553 printf("Gathered statistics for %u nodes\n", num_nodes);
554 show_statistics(&statistics, 1);
559 display remote ctdb statistics
561 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
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);
572 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
575 show_statistics(&statistics, 1);
581 reset remote ctdb statistics
583 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
587 ret = ctdb_statistics_reset(ctdb, options.pnn);
589 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
597 display remote ctdb rolling statistics
599 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
602 struct ctdb_statistics_wire *stats;
603 int i, num_records = -1;
605 assert_single_node_only();
608 num_records = atoi(argv[0]) - 1;
611 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
613 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
616 for (i=0;i<stats->num;i++) {
617 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
620 show_statistics(&stats->stats[i], i==0);
621 if (i == num_records) {
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;
644 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
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);
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]);
673 printf(" %s", "lock_buckets:");
674 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
675 printf(" %d", dbstat->locks.buckets[i]);
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 :
684 dbstat->locks.latency.max,
685 dbstat->locks.latency.num);
687 for (i=0; i<dbstat->num_hot_keys; i++) {
688 if (dbstat->hot_keys[i].count > 0) {
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++) {
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);
704 ctdb_free_dbstat(dbstat);
709 display uptime of remote node
711 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
714 struct ctdb_uptime *uptime = NULL;
715 int tmp, days, hours, minutes, seconds;
717 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
719 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
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)
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;
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;
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));
765 show the PNN of the current node
767 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
771 mypnn = getpnn(ctdb);
773 printf("PNN:%d\n", mypnn);
779 struct pnn_node *next;
784 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
786 const char *nodes_list;
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);
803 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
806 for (i=0, pnn=0; i<nlines; i++) {
810 /* strip leading spaces */
811 while((*node == ' ') || (*node == '\t')) {
818 if (strcmp(node, "") == 0) {
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;
833 pnn_node = pnn_node->next;
835 tmp_node->next = pnn_nodes;
836 pnn_nodes = tmp_node;
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);
862 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
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);
871 if (ctdb_sys_have_ip(&addr)) {
872 printf("PNN:%d\n", pnn_node->pnn);
873 talloc_free(mem_ctx);
878 printf("Failed to detect which PNN this node is\n");
879 talloc_free(mem_ctx);
883 /* Helpers for ctdb status
885 static bool is_partially_online(struct ctdb_node_and_flags *node)
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) {
901 ctdb_free_ifaces(ifaces);
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');
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)":"");
941 display remote ctdb status
943 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
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));
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) {
964 (void) control_status_1_machine(mypnn,
970 for (i=0; i<nodemap->num; i++) {
971 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
975 if (num_deleted_nodes == 0) {
976 printf("Number of nodes:%d\n", nodemap->num);
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) {
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));
992 if (vnnmap->generation == INVALID_GENERATION) {
993 printf("Generation:INVALID\n");
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));
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));
1013 printf("Recovery master:%d\n",recmaster);
1018 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1021 struct ctdb_node_map *nodemap=NULL;
1023 uint32_t pnn_mode, mypnn;
1029 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
1030 options.pnn, true, &nodes, &pnn_mode)) {
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));
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]]);
1054 ret |= control_status_1_human(mypnn,
1055 &nodemap->nodes[nodes[i]]);
1062 struct natgw_node *next;
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)
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));
1079 if (!(capabilities&CTDB_CAP_NATGW)) {
1082 *pnn = nodemap->nodes[i].pnn;
1083 *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
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)
1097 const char *natgw_list;
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;
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);
1130 for (i=0;i<nlines;i++) {
1134 /* strip leading spaces */
1135 while((*node == ' ') || (*node == '\t')) {
1141 if (strcmp(node, "") == 0) {
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"));
1156 /* Trim the nodemap so it only includes connected nodes in the
1157 * current natgw group.
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))) {
1167 /* this node was not in the natgw so we just remove it from
1170 if ((natgw_node == NULL)
1171 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1174 for (j=i+1; j<nodemap->num; j++) {
1175 nodemap->nodes[j-1] = nodemap->nodes[j];
1184 ret = 2; /* matches ENOENT */
1187 for (i = 0; exclude_flags[i] != 0; i++) {
1188 ret = find_natgw(ctdb, nodemap,
1199 if (options.machinereadable) {
1200 printf(":Node:IP:\n");
1201 printf(":%d:%s:\n", pnn, ip);
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();
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) {
1217 if (options.machinereadable) {
1218 control_status_1_machine(mypnn, &(nodemap->nodes[i]));
1220 control_status_1_human(mypnn, &(nodemap->nodes[i]));
1225 ctdb_free_nodemap(nodemap);
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;
1238 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1240 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1244 if (script_status == NULL) {
1245 if (!options.machinereadable) {
1246 printf("%s cycle never run\n",
1247 ctdb_eventscript_call_names[type]);
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) {
1262 status = "TIMEDOUT";
1265 status = "DISABLED";
1271 if (script_status->scripts[i].status > 0)
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,
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);
1289 printf("%-20s Status:%s ",
1290 script_status->scripts[i].name, status);
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) {
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);
1317 static int control_scriptstatus(struct ctdb_context *ctdb,
1318 int argc, const char **argv)
1321 enum ctdb_eventscript_call type, min, max;
1325 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1330 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1334 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1335 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1341 if (type == CTDB_EVENT_MAX) {
1342 if (strcmp(arg, "all") == 0) {
1344 max = CTDB_EVENT_MAX;
1346 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
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);
1366 enable an eventscript
1368 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1376 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1378 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1386 disable an eventscript
1388 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1396 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1398 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1406 display the pnn of the recovery master
1408 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1412 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1413 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1416 printf("%d\n",recmaster);
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;
1430 assert_single_node_only();
1436 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1437 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1440 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1441 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[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);
1452 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
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;
1469 assert_single_node_only();
1475 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1476 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1479 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1480 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[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);
1491 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
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;
1509 assert_single_node_only();
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]));
1524 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1526 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
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)) {
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));
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)) {
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));
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;
1563 uint32_t disable_time;
1565 struct ctdb_node_map *nodemap=NULL;
1566 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
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);
1573 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1579 /* read the public ip list from the node */
1580 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1582 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1583 talloc_free(tmp_ctx);
1587 for (i=0;i<ips->num;i++) {
1588 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
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);
1602 data.dptr = (uint8_t *)&ip;
1603 data.dsize = sizeof(ip);
1605 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1607 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1608 talloc_free(tmp_ctx);
1612 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1613 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1620 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1621 talloc_free(tmp_ctx);
1625 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1627 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1628 talloc_free(tmp_ctx);
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);
1637 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1641 talloc_free(tmp_ctx);
1647 * scans all other nodes and returns a pnn for another node that can host this
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;
1658 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1660 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1661 talloc_free(tmp_ctx);
1665 for(i=0;i<nodemap->num;i++){
1666 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1669 if (nodemap->nodes[i].pnn == options.pnn) {
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);
1676 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
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;
1689 talloc_free(tmp_ctx);
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);
1699 while (retries < 5) {
1700 if (!pnn_specified) {
1701 pnn = find_other_host_for_public_ip(ctdb, addr);
1706 ("Trying to move public IP to node %u\n", pnn));
1709 if (move_ip(ctdb, addr, pnn) == 0) {
1722 move/failover an ip address to a specific node
1724 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1727 ctdb_sock_addr addr;
1729 assert_single_node_only();
1736 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1737 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1742 if (sscanf(argv[1], "%u", &pnn) != 1) {
1743 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1747 if (!try_moveip(ctdb, &addr, pnn)) {
1748 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1755 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
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) {
1763 ("Failed to send message to force node %u to be a rebalancing target\n",
1773 rebalance a node by setting it to allow failback and triggering a
1776 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1782 assert_single_node_only();
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)) {
1794 for (i = 0; i < talloc_array_length(nodes); i++) {
1795 if (!rebalance_node(ctdb, nodes[i])) {
1803 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1805 struct ctdb_public_ip ip;
1808 uint32_t disable_time;
1810 struct ctdb_node_map *nodemap=NULL;
1811 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
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);
1818 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1825 data.dptr = (uint8_t *)&ip;
1826 data.dsize = sizeof(ip);
1828 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1830 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1831 talloc_free(tmp_ctx);
1835 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1836 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1843 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1844 talloc_free(tmp_ctx);
1848 talloc_free(tmp_ctx);
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();
1866 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1867 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1871 if (rebalance_ip(ctdb, &addr) != 0) {
1872 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
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;
1886 ips->ips[i].pnn = node_ip->pnn;
1887 ips->ips[i].addr = node_ip->addr;
1891 static int getips_count_callback(void *param, void *data)
1893 uint32_t *count = param;
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) {
1908 key[0] = ip->ip.sin_addr.s_addr;
1911 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1919 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1926 static void *add_ip_callback(void *parm, void *data)
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;
1940 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1942 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
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) {
1952 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
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);
1959 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
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),
1975 talloc_free(tmp_ips);
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);
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 {
2006 const char *srvid_str;
2009 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2014 struct srvid_reply_handler_data *d =
2015 (struct srvid_reply_handler_data *)private_data;
2019 if (data.dsize != sizeof(ret)) {
2020 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2024 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2025 ret = *(int32_t *)data.dptr;
2028 ("%s failed with result %d\n", d->srvid_str, ret));
2032 if (!d->wait_for_all) {
2037 /* Wait for all replies */
2039 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2040 if (d->nodes[i] == ret) {
2042 ("%s reply received from node %u\n",
2043 d->srvid_str, ret));
2046 if (d->nodes[i] != -1) {
2047 /* Found a node that hasn't yet replied */
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)
2063 struct srvid_request request;
2064 struct srvid_reply_handler_data reply_data;
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();
2078 /* Register message port for reply from recovery master */
2079 ctdb_client_set_message_handler(ctdb, request.srvid,
2080 srvid_broadcast_reply_handler,
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;
2091 reply_data.done = false;
2094 struct ctdb_node_map *nodemap;
2096 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2097 CTDB_CURRENT_NODE, ctdb, &nodemap);
2100 ("Unable to get nodemap from current node, try again\n"));
2105 if (reply_data.nodes != NULL) {
2106 talloc_free(reply_data.nodes);
2108 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2111 talloc_free(nodemap);
2114 /* Send to all connected nodes. Only recmaster replies */
2115 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2118 /* This can only happen if the socket is closed and
2119 * there's no way to recover from that, so don't try
2123 ("Failed to send %s request to connected nodes\n",
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) {
2136 ("Still waiting for confirmation of %s\n", srvid_str));
2141 ctdb_client_remove_message_handler(ctdb, request.srvid, &reply_data);
2143 talloc_free(reply_data.nodes);
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)
2166 int len, retries = 0;
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;
2175 talloc_free(tmp_ctx);
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);
2185 /* read the public ip list from the node */
2186 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2188 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2189 talloc_free(tmp_ctx);
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"));
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
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);
2213 pub->len = strlen(argv[1])+1;
2214 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2217 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2219 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2223 } while (retries < 5 && ret != 0);
2225 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2226 talloc_free(tmp_ctx);
2230 if (rebalance_node(ctdb, options.pnn) != 0) {
2231 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2235 talloc_free(tmp_ctx);
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;
2250 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2251 printf("Badly formed ip : %s\n", argv[0]);
2255 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
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;
2269 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2271 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
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) {
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));
2285 for (j=0;j<ips->num;j++) {
2286 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2294 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
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) {
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));
2313 for (j=0;j<ips->num;j++) {
2314 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2322 options.pnn = nodemap->nodes[i].pnn;
2323 control_delip(ctdb, argc, argv);
2326 talloc_free(tmp_ctx);
2331 delete a public ip address from a node
2333 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
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;
2342 talloc_free(tmp_ctx);
2346 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2347 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2351 if (options.pnn == CTDB_BROADCAST_ALL) {
2352 return control_delip_all(ctdb, argc, argv, &addr);
2359 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2361 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2362 talloc_free(tmp_ctx);
2366 for (i=0;i<ips->num;i++) {
2367 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
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);
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);
2389 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2390 talloc_free(tmp_ctx);
2394 talloc_free(tmp_ctx);
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];
2407 struct client_async_data *async_data;
2408 struct ctdb_client_control_state *state;
2418 while (!feof(stdin)) {
2419 if (fgets(line, sizeof(line), stdin) == NULL) {
2423 /* Silently skip empty lines */
2424 if (line[0] == '\n') {
2428 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2429 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2431 talloc_free(killtcp);
2435 if (current >= max_entries) {
2436 max_entries += 1024;
2437 killtcp = talloc_realloc(ctdb, killtcp,
2438 struct ctdb_control_killtcp,
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",
2446 talloc_free(killtcp);
2450 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2451 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2453 talloc_free(killtcp);
2460 async_data = talloc_zero(ctdb, struct client_async_data);
2461 if (async_data == NULL) {
2462 talloc_free(killtcp);
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) {
2478 ("Failed to call async killtcp control to node %u\n",
2480 talloc_free(killtcp);
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);
2493 talloc_free(killtcp);
2499 kill a tcp connection
2501 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2504 struct ctdb_control_killtcp killtcp;
2506 assert_single_node_only();
2509 return kill_tcp_from_file(ctdb, argc, argv);
2516 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2517 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2521 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2522 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2526 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2528 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2539 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2542 ctdb_sock_addr addr;
2544 assert_single_node_only();
2550 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2551 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2555 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2557 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2565 register a server id
2567 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2570 struct ctdb_server_id server_id;
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);
2582 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2585 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2591 unregister a server id
2593 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2596 struct ctdb_server_id server_id;
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);
2608 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2615 check if a server id exists
2617 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2621 struct ctdb_server_id server_id;
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);
2633 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2638 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2640 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
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)
2651 struct ctdb_server_id_list *server_ids;
2653 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2655 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
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);
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);
2680 talloc_free(tmp_ctx);
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",
2695 talloc_free(tmp_ctx);
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);
2709 send a tcp tickle ack
2711 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2714 ctdb_sock_addr src, dst;
2720 if (!parse_ip_port(argv[0], &src)) {
2721 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2725 if (!parse_ip_port(argv[1], &dst)) {
2726 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2730 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2734 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2741 display public ip status
2743 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
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);
2753 /* read the public ip list from this node */
2754 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2757 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2758 talloc_free(tmp_ctx);
2762 if (options.machinereadable){
2763 printf(":Public IP:Node:");
2764 if (options.verbose){
2765 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2769 if (options.pnn == CTDB_BROADCAST_ALL) {
2770 printf("Public IPs on ALL nodes\n");
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;
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;
2790 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2791 &ips->ips[ips->num-i].addr, &info);
2798 for (j=0; j < info->num; j++) {
2799 if (cifaces == NULL) {
2800 cifaces = talloc_strdup(info,
2801 info->ifaces[j].name);
2803 cifaces = talloc_asprintf_append(cifaces,
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) {
2816 if (avifaces == NULL) {
2817 avifaces = talloc_strdup(info, info->ifaces[j].name);
2819 avifaces = talloc_asprintf_append(avifaces,
2821 info->ifaces[j].name);
2826 if (options.machinereadable){
2828 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2829 ips->ips[ips->num-i].pnn);
2830 if (options.verbose){
2833 avifaces?avifaces:"",
2834 cifaces?cifaces:"");
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,
2843 avifaces?avifaces:"",
2844 cifaces?cifaces:"");
2847 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2848 ips->ips[ips->num-i].pnn);
2854 talloc_free(tmp_ctx);
2861 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2864 ctdb_sock_addr addr;
2865 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2866 struct ctdb_control_public_ip_info *info;
2869 talloc_free(tmp_ctx);
2873 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2874 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
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);
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);
2888 printf("Public IP[%s] info on node %u\n",
2889 ctdb_addr_to_str(&info->ip.addr),
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);
2911 display interfaces status
2913 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
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",
2925 if (options.machinereadable){
2926 printf(":Name:LinkStatus:References:\n");
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);
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);
2951 set link status of an interface
2953 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2956 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2957 struct ctdb_control_iface_info info;
2965 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2966 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2968 talloc_free(tmp_ctx);
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;
2978 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2980 talloc_free(tmp_ctx);
2984 /* read the public ip list from this node */
2985 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
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);
2994 talloc_free(tmp_ctx);
2999 display pid of a ctdb daemon
3001 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3006 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3008 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3011 printf("Pid:%d\n", pid);
3016 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3018 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3020 update_flags_handler_t handler,
3025 struct ctdb_node_map *nodemap = NULL;
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"));
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));
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));
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,
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,
3093 false /* set_flag*/);
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,
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,
3123 false /* set_flag */);
3126 static uint32_t get_generation(struct ctdb_context *ctdb)
3128 struct ctdb_vnn_map *vnnmap=NULL;
3131 /* wait until the recmaster is not in recovery mode */
3133 uint32_t recmode, recmaster;
3135 if (vnnmap != NULL) {
3136 talloc_free(vnnmap);
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));
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));
3152 /* get the current generation number */
3153 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
3155 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3159 if ((recmode == CTDB_RECOVERY_NORMAL)
3160 && (vnnmap->generation != 1)){
3161 return vnnmap->generation;
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;
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"));
3190 return update_flags_and_ipreallocate(ctdb, &bantime,
3191 update_state_banned,
3194 true /* set_flag*/);
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;
3206 return update_flags_and_ipreallocate(ctdb, &bantime,
3207 update_state_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)
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);
3225 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3229 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3231 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3235 if (bantime->time == 0) {
3236 printf("Node %u is not banned\n", bantime->pnn);
3238 printf("Node %u is banned, %d seconds remaining\n",
3239 bantime->pnn, bantime->time);
3248 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3252 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3254 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3264 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3267 uint32_t generation, next_generation;
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);
3279 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
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);
3287 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3291 /* wait until we are in a new generation */
3293 next_generation = get_generation(ctdb);
3294 if (next_generation != generation) {
3305 display monitoring mode of a remote node
3307 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3312 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3314 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3317 if (!options.machinereadable){
3318 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3321 printf(":%d:\n",monmode);
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));
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");
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));
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;
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));
3370 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3371 CTDB_NO_MEMORY(ctdb, capabilities);
3375 /* collect capabilities for all connected nodes */
3376 for (i=0; i<nodemap->num; i++) {
3377 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3380 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3384 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3385 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3390 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3394 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3399 /* Print all LVS nodes */
3400 for (i=0; i<nodemap->num; i++) {
3401 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3404 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3407 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3411 if (healthy_count != 0) {
3412 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3417 printf("%d:%s\n", i,
3418 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3422 ctdb_free_nodemap(nodemap);
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;
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));
3441 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3442 CTDB_NO_MEMORY(ctdb, capabilities);
3446 /* collect capabilities for all connected nodes */
3447 for (i=0; i<nodemap->num; i++) {
3448 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3451 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3455 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3456 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3461 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3465 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3470 /* find and show the lvsmaster */
3471 for (i=0; i<nodemap->num; i++) {
3472 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3475 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3478 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3482 if (healthy_count != 0) {
3483 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3488 if (options.machinereadable){
3491 printf("Node %d is LVS master\n", i);
3497 printf("There is no LVS master\n");
3499 ctdb_free_nodemap(nodemap);
3504 disable monitoring on a node
3506 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3511 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3513 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3516 printf("Monitoring mode:%s\n","DISABLED");
3522 enable monitoring on a node
3524 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3529 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3531 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3534 printf("Monitoring mode:%s\n","ACTIVE");
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;
3547 struct ctdb_dump_db_context c;
3556 if (!db_exists(ctdb, db_name, NULL, &flags)) {
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));
3566 if (options.printlmaster) {
3567 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3568 ctdb, &ctdb->vnn_map);
3570 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
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);
3587 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3588 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3589 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3593 talloc_free(ctdb_db);
3595 printf("Dumped %d records\n", ret);
3599 struct cattdb_data {
3600 struct ctdb_context *ctdb;
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;
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;
3638 if (!db_exists(ctdb, db_name, NULL, &flags)) {
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));
3648 /* traverse the local tdb */
3651 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3652 printf("Failed to cattdb data\n");
3655 talloc_free(ctdb_db);
3657 printf("Dumped %d records\n", d.count);
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);
3679 if (!db_exists(ctdb, db_name, NULL, &flags)) {
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));
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);
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);
3700 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3702 talloc_free(ctdb_db);
3703 talloc_free(tmp_ctx);
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);
3725 if (!db_exists(ctdb, db_name, NULL, &flags)) {
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));
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);
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);
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");
3754 talloc_free(ctdb_db);
3755 talloc_free(tmp_ctx);
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;
3774 talloc_free(tmp_ctx);
3780 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3781 talloc_free(tmp_ctx);
3785 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3787 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3788 talloc_free(tmp_ctx);
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);
3799 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3801 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3802 talloc_free(tmp_ctx);
3806 key.dptr = discard_const(argv[1]);
3807 key.dsize = strlen(argv[1]);
3808 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3810 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3811 talloc_free(tmp_ctx);
3815 if (data.dsize == 0 || data.dptr == NULL) {
3816 DEBUG(DEBUG_ERR,("Record is empty\n"));
3817 talloc_free(tmp_ctx);
3822 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3824 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3825 talloc_free(tmp_ctx);
3828 write(fd, data.dptr, data.dsize);
3831 write(1, data.dptr, data.dsize);
3834 /* abort the transaction */
3838 talloc_free(tmp_ctx);
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;
3850 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3859 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3861 printf("Failed to open TDB file %s\n", tdb_file);
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]);
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);
3886 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3888 printf("Failed to open output file %s\n", argv[2]);
3891 if (options.verbose){
3892 write(fd, data.dptr, data.dsize);
3894 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3898 if (options.verbose){
3899 write(1, data.dptr, data.dsize);
3901 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3905 talloc_free(tmp_ctx);
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;
3917 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3925 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3927 printf("Failed to open TDB file %s\n", tdb_file);
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]);
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]);
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");
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);
3965 talloc_free(tmp_ctx);
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;
3983 talloc_free(tmp_ctx);
3987 fd = open(argv[2], O_RDONLY);
3989 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3990 talloc_free(tmp_ctx);
3994 ret = fstat(fd, &st);
3996 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3998 talloc_free(tmp_ctx);
4002 if (!S_ISREG(st.st_mode)) {
4003 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4005 talloc_free(tmp_ctx);
4009 data.dsize = st.st_size;
4010 if (data.dsize == 0) {
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));
4017 talloc_free(tmp_ctx);
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));
4024 talloc_free(tmp_ctx);
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);
4040 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4042 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4043 talloc_free(tmp_ctx);
4047 key.dptr = discard_const(argv[1]);
4048 key.dsize = strlen(argv[1]);
4049 ret = ctdb_transaction_store(h, key, data);
4051 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4052 talloc_free(tmp_ctx);
4056 ret = ctdb_transaction_commit(h);
4058 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4059 talloc_free(tmp_ctx);
4064 talloc_free(tmp_ctx);
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;
4083 talloc_free(tmp_ctx);
4089 if (!db_exists(ctdb, db_name, NULL, &flags)) {
4090 talloc_free(tmp_ctx);
4094 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4096 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4097 talloc_free(tmp_ctx);
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);
4108 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4110 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4111 talloc_free(tmp_ctx);
4115 key.dptr = discard_const(argv[1]);
4116 key.dsize = strlen(argv[1]);
4117 ret = ctdb_transaction_store(h, key, tdb_null);
4119 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4120 talloc_free(tmp_ctx);
4124 ret = ctdb_transaction_commit(h);
4126 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4127 talloc_free(tmp_ctx);
4131 talloc_free(tmp_ctx);
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)
4143 struct sockaddr_in sin;
4146 printf("Use: ctdb chktcport <port>\n");
4150 port = atoi(argv[0]);
4152 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4154 printf("Failed to open local socket\n");
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));
4167 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
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);
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)
4194 struct ctdb_get_log_addr log_addr;
4198 /* Process options */
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;
4206 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4207 log_addr.level = get_debug_by_desc(argv[i]);
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);
4225 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
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);
4239 talloc_free(tmp_ctx);
4241 ret = ctdb_client_send_message(ctdb, options.pnn,
4242 CTDB_SRVID_GETLOG, data);
4244 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
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"));
4261 clear the in memory log area
4263 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4267 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4268 /* "recoverd" not given - get logs from main daemon */
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);
4281 talloc_free(tmp_ctx);
4283 TDB_DATA data; /* unused in recoverd... */
4286 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4288 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4296 /* Reload public IPs on a specified nodes */
4297 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4302 assert_single_node_only();
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)) {
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(),
4334 NULL, NULL, NULL) != 0) {
4336 ("Unable to reload IPs on some nodes, try again.\n"));
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);
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)
4358 struct ctdb_dbid_map *dbmap=NULL;
4360 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4362 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4366 if(options.machinereadable){
4367 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4368 for(i=0;i<dbmap->num;i++){
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));
4393 printf("Number of databases:%d\n", dbmap->num);
4394 for(i=0;i<dbmap->num;i++){
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":"");
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;
4436 if (!db_exists(ctdb, db_name, &db_id, &flags)) {
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"));
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");
4470 if (recmaster != mypnn) {
4471 printf("this node is not the recmaster\n");
4475 printf("this node is the recmaster\n");
4482 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4485 struct timeval tv = timeval_current();
4486 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4488 printf("Unable to get ping response from node %u\n", options.pnn);
4491 printf("response from %u time=%.6f sec (%d clients)\n",
4492 options.pnn, timeval_elapsed(&tv), ret);
4499 get a node's runstate
4501 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4504 enum ctdb_runstate runstate;
4506 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4508 printf("Unable to get runstate response from node %u\n",
4513 enum ctdb_runstate t;
4515 for (i=0; i<argc; i++) {
4517 t = runstate_from_string(argv[i]);
4518 if (t == CTDB_RUNSTATE_UNKNOWN) {
4519 printf("Invalid run state (%s)\n", argv[i]);
4523 if (t == runstate) {
4530 printf("CTDB not in required run state (got %s)\n",
4531 runstate_to_string((enum ctdb_runstate)runstate));
4536 printf("%s\n", runstate_to_string(runstate));
4544 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4555 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4557 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4561 printf("%-23s = %u\n", name, value);
4568 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4579 value = strtoul(argv[1], NULL, 0);
4581 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4583 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4592 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4598 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4600 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4604 for (i=0;i<count;i++) {
4605 control_getvar(ctdb, 1, &list[i]);
4614 display debug level on a node
4616 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4621 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4623 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4626 if (options.machinereadable){
4627 printf(":Name:Level:\n");
4628 printf(":%s:%d:\n",get_debug_by_level(level),level);
4630 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4637 display reclock file of a node
4639 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4642 const char *reclock;
4644 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4646 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4649 if (options.machinereadable){
4650 if (reclock != NULL) {
4651 printf("%s", reclock);
4654 if (reclock == NULL) {
4655 printf("No reclock file used.\n");
4657 printf("Reclock file:%s\n", reclock);
4665 set the reclock file of a node
4667 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4670 const char *reclock;
4674 } else if (argc == 1) {
4680 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4682 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4689 set the natgw state on/off
4691 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4694 uint32_t natgwstate;
4700 if (!strcmp(argv[0], "on")) {
4702 } else if (!strcmp(argv[0], "off")) {
4708 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4710 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4718 set the lmaster role on/off
4720 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4723 uint32_t lmasterrole;
4729 if (!strcmp(argv[0], "on")) {
4731 } else if (!strcmp(argv[0], "off")) {
4737 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4739 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4747 set the recmaster role on/off
4749 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4752 uint32_t recmasterrole;
4758 if (!strcmp(argv[0], "on")) {
4760 } else if (!strcmp(argv[0], "off")) {
4766 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4768 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4776 set debug level on a node or all nodes
4778 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
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);
4792 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4793 level = get_debug_by_desc(argv[0]);
4795 level = strtol(argv[0], NULL, 0);
4798 for (i=0; debug_levels[i].description != NULL; i++) {
4799 if (level == debug_levels[i].level) {
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);
4811 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4813 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4822 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4828 priority = strtol(argv[0], NULL, 0);
4832 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4834 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4836 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
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;
4859 if (strcmp(argv[1], "persistent") != 0) {
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));
4877 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4879 struct ctdb_db_priority db_prio;
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);
4891 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4901 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4903 uint32_t db_id, priority;
4910 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4914 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4916 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4920 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
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);
4938 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4942 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
4944 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
4945 talloc_free(tmp_ctx);
4949 talloc_free(tmp_ctx);
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);
4966 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4970 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4972 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4973 talloc_free(tmp_ctx);
4977 talloc_free(tmp_ctx);
4984 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4994 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
4998 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
5000 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5004 printf("Sequence number:%lld\n", (long long)seqnum);
5012 static int control_setdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5015 struct ctdb_db_context *ctdb_db;
5018 uint64_t old_seqnum, new_seqnum;
5019 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5020 struct ctdb_transaction_handle *h;
5025 talloc_free(tmp_ctx);
5029 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5030 talloc_free(tmp_ctx);
5034 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5036 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", argv[0]));
5037 talloc_free(tmp_ctx);
5041 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &old_seqnum);
5043 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5044 talloc_free(tmp_ctx);
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);
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);
5062 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
5064 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", argv[0]));
5065 talloc_free(tmp_ctx);
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);
5078 DEBUG(DEBUG_ERR,("Failed to store record\n"));
5079 talloc_free(tmp_ctx);
5083 ret = ctdb_transaction_commit(h);
5085 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
5086 talloc_free(tmp_ctx);
5090 talloc_free(tmp_ctx);
5095 run an eventscript on a node
5097 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5103 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5106 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
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);
5122 talloc_free(tmp_ctx);
5126 #define DB_VERSION 1
5127 #define MAX_DB_NAME 64
5128 struct db_file_header {
5129 unsigned long version;
5131 unsigned long persistent;
5133 const char name[MAX_DB_NAME];
5136 struct backup_data {
5137 struct ctdb_marshall_buffer *records;
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);
5151 bd->traverse_error = true;
5152 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
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;
5161 bd->records->count++;
5162 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5163 bd->len += rec->length;
5171 * backup a database to a file
5173 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
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;
5182 const char *reason = NULL;
5186 assert_single_node_only();
5189 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5193 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5197 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5198 db_id, tmp_ctx, &reason);
5200 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5202 talloc_free(tmp_ctx);
5206 uint32_t allow_unhealthy = 0;
5208 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5209 "AllowUnhealthyDBRead",
5212 if (allow_unhealthy != 1) {
5213 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5216 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5218 talloc_free(tmp_ctx);
5222 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5224 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5225 "tunnable AllowUnhealthyDBRead = %u\n",
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);
5237 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5239 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5240 talloc_free(tmp_ctx);
5245 bd = talloc_zero(tmp_ctx, struct backup_data);
5247 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5248 talloc_free(tmp_ctx);
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);
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);
5269 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5272 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5274 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5275 talloc_free(tmp_ctx);
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"));
5287 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
5288 ret = write(fh, &dbhdr, sizeof(dbhdr));
5290 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5293 ret = write(fh, bd->records, bd->len);
5295 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
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);
5315 * restore a database from a file
5317 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5320 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
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;
5328 struct ctdb_control_wipe_database w;
5330 uint32_t generation;
5335 assert_single_node_only();
5337 if (argc < 1 || argc > 2) {
5338 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5342 fh = open(argv[0], O_RDONLY);
5344 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5345 talloc_free(tmp_ctx);
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);
5356 dbname = discard_const(dbhdr.name);
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));
5366 talloc_free(tmp_ctx);
5369 read(fh, outdata.dptr, outdata.dsize);
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",
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);
5385 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5387 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5388 talloc_free(tmp_ctx);
5393 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5395 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5396 talloc_free(tmp_ctx);
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,
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);
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,
5424 TIMELIMIT(), false, data,
5427 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
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,
5442 TIMELIMIT(), false, data,
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);
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,
5455 TIMELIMIT(), false, outdata,
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);
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,
5471 TIMELIMIT(), false, data,
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);
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,
5486 TIMELIMIT(), false, data,
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);
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,
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);
5511 talloc_free(tmp_ctx);
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);
5522 struct db_file_header dbhdr;
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();
5533 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5537 fh = open(argv[0], O_RDONLY);
5539 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5540 talloc_free(tmp_ctx);
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);
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));
5556 talloc_free(tmp_ctx);
5559 read(fh, outdata.dptr, outdata.dsize);
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);
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++) {
5580 /* we do not want the header splitted, so we pass NULL*/
5581 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5584 ctdb_dumpdb_record(ctdb, key, data, &c);
5587 printf("Dumped %d records\n", i);
5588 talloc_free(tmp_ctx);
5593 * wipe a database from a file
5595 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5599 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5601 struct ctdb_db_context *ctdb_db;
5602 struct ctdb_node_map *nodemap = NULL;
5603 struct ctdb_vnn_map *vnnmap = NULL;
5605 struct ctdb_control_wipe_database w;
5607 uint32_t generation;
5610 assert_single_node_only();
5613 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5617 if (!db_exists(ctdb, argv[0], NULL, &flags)) {
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",
5625 talloc_free(tmp_ctx);
5629 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5632 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5634 talloc_free(tmp_ctx);
5638 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5641 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5643 talloc_free(tmp_ctx);
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,
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);
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,
5673 TIMELIMIT(), false, data,
5677 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5678 "transactions.\n"));
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,
5692 TIMELIMIT(), false, data,
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);
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,
5708 TIMELIMIT(), false, data,
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);
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,
5723 TIMELIMIT(), false, data,
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);
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,
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);
5746 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5748 talloc_free(tmp_ctx);
5755 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
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);
5769 write(1, data.dptr, data.dsize);
5770 talloc_free(tmp_ctx);
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);
5785 dump memory usage on the recovery daemon
5787 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
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);
5807 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5811 /* this loop will terminate when we have received the reply */
5813 event_loop_once(ctdb->ev);
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;
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);
5839 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5847 handler for msglisten
5849 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5850 TDB_DATA data, void *private_data)
5854 printf("Message received: ");
5855 for (i=0;i<data.dsize;i++) {
5856 printf("%c", data.dptr[i]);
5862 listen for messages on a messageport
5864 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
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);
5876 event_loop_once(ctdb->ev);
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);
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);
5908 if (options.machinereadable){
5909 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5911 printf("%s\n", pnn_node->addr);
5914 talloc_free(mem_ctx);
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)
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);
5934 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5938 /* reload the nodes file on all remote nodes */
5939 for (i=0;i<nodemap->num;i++) {
5940 if (nodemap->nodes[i].pnn == mypnn) {
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);
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);
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);
5965 static const struct {
5967 int (*fn)(struct ctdb_context *, int, const char **);
5969 bool without_daemon; /* can be run without daemon running ? */
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 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6035 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6036 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6037 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6038 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6039 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6040 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6041 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6042 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6043 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6044 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6045 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6046 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6047 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6048 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6049 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6050 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6051 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6052 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6053 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6054 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6055 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6056 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6057 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6058 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6059 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6060 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6061 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6062 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6063 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6064 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6065 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6066 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6067 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
6068 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
6069 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
6070 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6071 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6072 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6073 { "setdbseqnum", control_setdbseqnum, false, false, "set the sequence number for a database", "<dbname|dbid> <seqnum>" },
6074 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6075 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6076 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6077 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6083 static void usage(void)
6087 "Usage: ctdb [options] <control>\n" \
6089 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6090 " -Y generate machinereadable output\n"
6091 " -v generate verbose output\n"
6092 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6093 printf("Controls:\n");
6094 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6095 printf(" %-15s %-27s %s\n",
6096 ctdb_commands[i].name,
6097 ctdb_commands[i].args?ctdb_commands[i].args:"",
6098 ctdb_commands[i].msg);
6104 static void ctdb_alarm(int sig)
6106 printf("Maximum runtime exceeded - exiting\n");
6113 int main(int argc, const char *argv[])
6115 struct ctdb_context *ctdb;
6116 char *nodestring = NULL;
6117 struct poptOption popt_options[] = {
6120 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6121 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6122 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6123 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6124 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6125 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6126 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6127 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6128 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6129 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6133 const char **extra_argv;
6137 struct event_context *ev;
6138 const char *control;
6139 const char *socket_name;
6143 /* set some defaults */
6144 options.maxruntime = 0;
6145 options.timelimit = 10;
6146 options.pnn = CTDB_CURRENT_NODE;
6148 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6150 while ((opt = poptGetNextOpt(pc)) != -1) {
6153 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6154 poptBadOption(pc, 0), poptStrerror(opt)));
6159 /* setup the remaining options for the main program to use */
6160 extra_argv = poptGetArgs(pc);
6163 while (extra_argv[extra_argc]) extra_argc++;
6166 if (extra_argc < 1) {
6170 if (options.maxruntime == 0) {
6171 const char *ctdb_timeout;
6172 ctdb_timeout = getenv("CTDB_TIMEOUT");
6173 if (ctdb_timeout != NULL) {
6174 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6176 /* default timeout is 120 seconds */
6177 options.maxruntime = 120;
6181 signal(SIGALRM, ctdb_alarm);
6182 alarm(options.maxruntime);
6184 control = extra_argv[0];
6186 ev = event_context_init(NULL);
6188 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6192 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6193 if (strcmp(control, ctdb_commands[i].name) == 0) {
6198 if (i == ARRAY_SIZE(ctdb_commands)) {
6199 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6203 if (ctdb_commands[i].without_daemon == true) {
6204 if (nodestring != NULL) {
6205 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6209 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6212 /* initialise ctdb */
6213 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6216 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6220 /* initialize a libctdb connection as well */
6221 socket_name = ctdb_get_socketname(ctdb);
6222 ctdb_connection = ctdb_connect(socket_name,
6223 ctdb_log_file, stderr);
6224 if (ctdb_connection == NULL) {
6225 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
6229 /* setup the node number(s) to contact */
6230 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
6231 &options.nodes, &options.pnn)) {
6235 if (options.pnn == CTDB_CURRENT_NODE) {
6236 options.pnn = options.nodes[0];
6239 if (ctdb_commands[i].auto_all &&
6240 ((options.pnn == CTDB_BROADCAST_ALL) ||
6241 (options.pnn == CTDB_MULTICAST))) {
6245 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6246 options.pnn = options.nodes[j];
6247 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6250 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6253 ctdb_disconnect(ctdb_connection);
6256 (void)poptFreeContext(pc);