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_client.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
34 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
35 #define ERR_NONODE 21 /* node does not exist */
36 #define ERR_DISNODE 22 /* node is disconnected */
38 static void usage(void);
47 int printemptyrecords;
54 #define LONGTIMEOUT options.timelimit*10
56 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
57 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
59 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
61 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
65 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
66 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
67 "Out of memory in " __location__ )); \
71 static uint32_t getpnn(struct ctdb_context *ctdb)
73 if ((options.pnn == CTDB_BROADCAST_ALL) ||
74 (options.pnn == CTDB_MULTICAST)) {
76 ("Cannot get PNN for node %u\n", options.pnn));
80 if (options.pnn == CTDB_CURRENT_NODE) {
81 return ctdb_get_pnn(ctdb);
87 static void assert_single_node_only(void)
89 if ((options.pnn == CTDB_BROADCAST_ALL) ||
90 (options.pnn == CTDB_MULTICAST)) {
92 ("This control can not be applied to multiple PNNs\n"));
97 /* Pretty print the flags to a static buffer in human-readable format.
98 * This never returns NULL!
100 static const char *pretty_print_flags(uint32_t flags)
103 static const struct {
107 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
108 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
109 { NODE_FLAGS_BANNED, "BANNED" },
110 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
111 { NODE_FLAGS_DELETED, "DELETED" },
112 { NODE_FLAGS_STOPPED, "STOPPED" },
113 { NODE_FLAGS_INACTIVE, "INACTIVE" },
115 static char flags_str[512]; /* Big enough to contain all flag names */
118 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
119 if (flags & flag_names[j].flag) {
120 if (flags_str[0] == '\0') {
121 (void) strcpy(flags_str, flag_names[j].name);
123 (void) strcat(flags_str, "|");
124 (void) strcat(flags_str, flag_names[j].name);
128 if (flags_str[0] == '\0') {
129 (void) strcpy(flags_str, "OK");
135 static int h2i(char h)
137 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
138 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
142 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
145 TDB_DATA key = {NULL, 0};
149 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
154 key.dptr = talloc_size(mem_ctx, key.dsize);
156 for (i=0; i < len/2; i++) {
157 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
162 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
163 * that are disconnected or deleted. If dd_ok is true those nodes are
164 * included in the output list of nodes. If dd_ok is false, those
165 * nodes are filtered from the "all" case and cause an error if
166 * explicitly specified.
168 static bool parse_nodestring(struct ctdb_context *ctdb,
170 const char * nodestring,
171 uint32_t current_pnn,
176 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
179 struct ctdb_node_map *nodemap;
184 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
186 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
187 talloc_free(tmp_ctx);
191 if (nodestring != NULL) {
192 *nodes = talloc_array(mem_ctx, uint32_t, 0);
193 if (*nodes == NULL) {
199 if (strcmp(nodestring, "all") == 0) {
200 *pnn_mode = CTDB_BROADCAST_ALL;
203 for (i = 0; i < nodemap->num; i++) {
204 if ((nodemap->nodes[i].flags &
205 (NODE_FLAGS_DISCONNECTED |
206 NODE_FLAGS_DELETED)) && !dd_ok) {
209 *nodes = talloc_realloc(mem_ctx, *nodes,
211 if (*nodes == NULL) {
221 ns = talloc_strdup(tmp_ctx, nodestring);
222 tok = strtok(ns, ",");
223 while (tok != NULL) {
225 i = (uint32_t)strtoul(tok, NULL, 0);
226 if (i >= nodemap->num) {
227 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
228 talloc_free(tmp_ctx);
231 if ((nodemap->nodes[i].flags &
232 (NODE_FLAGS_DISCONNECTED |
233 NODE_FLAGS_DELETED)) && !dd_ok) {
234 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
235 talloc_free(tmp_ctx);
238 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
239 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
240 talloc_free(tmp_ctx);
244 *nodes = talloc_realloc(mem_ctx, *nodes,
246 if (*nodes == NULL) {
253 tok = strtok(NULL, ",");
258 *pnn_mode = (*nodes)[0];
260 *pnn_mode = CTDB_MULTICAST;
264 /* default - no nodes specified */
265 *nodes = talloc_array(mem_ctx, uint32_t, 1);
266 if (*nodes == NULL) {
269 *pnn_mode = CTDB_CURRENT_NODE;
271 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
276 talloc_free(tmp_ctx);
280 talloc_free(tmp_ctx);
285 check if a database exists
287 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg, uint32_t *dbid, uint8_t *flags)
290 struct ctdb_dbid_map *dbmap=NULL;
291 bool dbid_given = false, found = false;
293 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
295 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
297 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
301 if (strncmp(dbarg, "0x", 2) == 0) {
302 id = strtoul(dbarg, NULL, 0);
306 for(i=0; i<dbmap->num; i++) {
308 if (id == dbmap->dbs[i].dbid) {
314 ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
316 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
320 if (strcmp(name, dbarg) == 0) {
321 id = dbmap->dbs[i].dbid;
329 if (dbid) *dbid = id;
330 if (flags) *flags = dbmap->dbs[i].flags;
332 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
336 talloc_free(tmp_ctx);
341 see if a process exists
343 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
351 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
352 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
356 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
358 printf("%u:%u exists\n", pnn, pid);
360 printf("%u:%u does not exist\n", pnn, pid);
366 display statistics structure
368 static void show_statistics(struct ctdb_statistics *s, int show_header)
370 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
372 const char *prefix=NULL;
374 int tmp, days, hours, minutes, seconds;
379 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
380 STATISTICS_FIELD(num_clients),
381 STATISTICS_FIELD(frozen),
382 STATISTICS_FIELD(recovering),
383 STATISTICS_FIELD(num_recoveries),
384 STATISTICS_FIELD(client_packets_sent),
385 STATISTICS_FIELD(client_packets_recv),
386 STATISTICS_FIELD(node_packets_sent),
387 STATISTICS_FIELD(node_packets_recv),
388 STATISTICS_FIELD(keepalive_packets_sent),
389 STATISTICS_FIELD(keepalive_packets_recv),
390 STATISTICS_FIELD(node.req_call),
391 STATISTICS_FIELD(node.reply_call),
392 STATISTICS_FIELD(node.req_dmaster),
393 STATISTICS_FIELD(node.reply_dmaster),
394 STATISTICS_FIELD(node.reply_error),
395 STATISTICS_FIELD(node.req_message),
396 STATISTICS_FIELD(node.req_control),
397 STATISTICS_FIELD(node.reply_control),
398 STATISTICS_FIELD(client.req_call),
399 STATISTICS_FIELD(client.req_message),
400 STATISTICS_FIELD(client.req_control),
401 STATISTICS_FIELD(timeouts.call),
402 STATISTICS_FIELD(timeouts.control),
403 STATISTICS_FIELD(timeouts.traverse),
404 STATISTICS_FIELD(locks.num_calls),
405 STATISTICS_FIELD(locks.num_current),
406 STATISTICS_FIELD(locks.num_pending),
407 STATISTICS_FIELD(locks.num_failed),
408 STATISTICS_FIELD(total_calls),
409 STATISTICS_FIELD(pending_calls),
410 STATISTICS_FIELD(childwrite_calls),
411 STATISTICS_FIELD(pending_childwrite_calls),
412 STATISTICS_FIELD(memory_used),
413 STATISTICS_FIELD(max_hop_count),
414 STATISTICS_FIELD(total_ro_delegations),
415 STATISTICS_FIELD(total_ro_revokes),
418 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
427 if (options.machinereadable){
429 printf("CTDB version:");
430 printf("Current time of statistics:");
431 printf("Statistics collected since:");
432 for (i=0;i<ARRAY_SIZE(fields);i++) {
433 printf("%s:", fields[i].name);
435 printf("num_reclock_ctdbd_latency:");
436 printf("min_reclock_ctdbd_latency:");
437 printf("avg_reclock_ctdbd_latency:");
438 printf("max_reclock_ctdbd_latency:");
440 printf("num_reclock_recd_latency:");
441 printf("min_reclock_recd_latency:");
442 printf("avg_reclock_recd_latency:");
443 printf("max_reclock_recd_latency:");
445 printf("num_call_latency:");
446 printf("min_call_latency:");
447 printf("avg_call_latency:");
448 printf("max_call_latency:");
450 printf("num_lockwait_latency:");
451 printf("min_lockwait_latency:");
452 printf("avg_lockwait_latency:");
453 printf("max_lockwait_latency:");
455 printf("num_childwrite_latency:");
456 printf("min_childwrite_latency:");
457 printf("avg_childwrite_latency:");
458 printf("max_childwrite_latency:");
461 printf("%d:", CTDB_VERSION);
462 printf("%d:", (int)s->statistics_current_time.tv_sec);
463 printf("%d:", (int)s->statistics_start_time.tv_sec);
464 for (i=0;i<ARRAY_SIZE(fields);i++) {
465 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
467 printf("%d:", s->reclock.ctdbd.num);
468 printf("%.6f:", s->reclock.ctdbd.min);
469 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
470 printf("%.6f:", s->reclock.ctdbd.max);
472 printf("%d:", s->reclock.recd.num);
473 printf("%.6f:", s->reclock.recd.min);
474 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
475 printf("%.6f:", s->reclock.recd.max);
477 printf("%d:", s->call_latency.num);
478 printf("%.6f:", s->call_latency.min);
479 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
480 printf("%.6f:", s->call_latency.max);
482 printf("%d:", s->childwrite_latency.num);
483 printf("%.6f:", s->childwrite_latency.min);
484 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
485 printf("%.6f:", s->childwrite_latency.max);
488 printf("CTDB version %u\n", CTDB_VERSION);
489 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
490 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
492 for (i=0;i<ARRAY_SIZE(fields);i++) {
493 if (strchr(fields[i].name, '.')) {
494 preflen = strcspn(fields[i].name, ".")+1;
495 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
496 prefix = fields[i].name;
497 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
502 printf(" %*s%-22s%*s%10u\n",
504 fields[i].name+preflen,
506 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
508 printf(" hop_count_buckets:");
509 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
510 printf(" %d", s->hop_count_bucket[i]);
513 printf(" lock_buckets:");
514 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
515 printf(" %d", s->locks.buckets[i]);
518 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);
520 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);
522 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);
524 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);
525 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);
528 talloc_free(tmp_ctx);
532 display remote ctdb statistics combined from all nodes
534 static int control_statistics_all(struct ctdb_context *ctdb)
537 struct ctdb_statistics statistics;
541 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
542 CTDB_NO_MEMORY(ctdb, nodes);
544 ZERO_STRUCT(statistics);
546 for (i=0;i<num_nodes;i++) {
547 struct ctdb_statistics s1;
549 uint32_t *v1 = (uint32_t *)&s1;
550 uint32_t *v2 = (uint32_t *)&statistics;
552 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
553 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
555 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
558 for (j=0;j<num_ints;j++) {
561 statistics.max_hop_count =
562 MAX(statistics.max_hop_count, s1.max_hop_count);
563 statistics.call_latency.max =
564 MAX(statistics.call_latency.max, s1.call_latency.max);
567 printf("Gathered statistics for %u nodes\n", num_nodes);
568 show_statistics(&statistics, 1);
573 display remote ctdb statistics
575 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
578 struct ctdb_statistics statistics;
580 if (options.pnn == CTDB_BROADCAST_ALL) {
581 return control_statistics_all(ctdb);
584 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
586 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
589 show_statistics(&statistics, 1);
595 reset remote ctdb statistics
597 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
601 ret = ctdb_statistics_reset(ctdb, options.pnn);
603 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
611 display remote ctdb rolling statistics
613 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
616 struct ctdb_statistics_wire *stats;
617 int i, num_records = -1;
619 assert_single_node_only();
622 num_records = atoi(argv[0]) - 1;
625 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
627 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
630 for (i=0;i<stats->num;i++) {
631 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
634 show_statistics(&stats->stats[i], i==0);
635 if (i == num_records) {
644 display remote ctdb db statistics
646 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
648 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
649 struct ctdb_db_statistics *dbstat;
659 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
663 ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
665 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
666 talloc_free(tmp_ctx);
670 printf("DB Statistics: %s\n", argv[0]);
671 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
672 dbstat->db_ro_delegations);
673 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
674 dbstat->db_ro_delegations);
675 printf(" %s\n", "locks");
676 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
677 dbstat->locks.num_calls);
678 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
679 dbstat->locks.num_failed);
680 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
681 dbstat->locks.num_current);
682 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
683 dbstat->locks.num_pending);
684 printf(" %s", "hop_count_buckets:");
685 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
686 printf(" %d", dbstat->hop_count_bucket[i]);
689 printf(" %s", "lock_buckets:");
690 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
691 printf(" %d", dbstat->locks.buckets[i]);
694 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
695 "locks_latency MIN/AVG/MAX",
696 dbstat->locks.latency.min,
697 (dbstat->locks.latency.num ?
698 dbstat->locks.latency.total /dbstat->locks.latency.num :
700 dbstat->locks.latency.max,
701 dbstat->locks.latency.num);
703 for (i=0; i<dbstat->num_hot_keys; i++) {
704 if (dbstat->hot_keys[i].count > 0) {
708 dbstat->num_hot_keys = num_hot_keys;
710 printf(" Num Hot Keys: %d\n", dbstat->num_hot_keys);
711 for (i = 0; i < dbstat->num_hot_keys; i++) {
713 printf(" Count:%d Key:", dbstat->hot_keys[i].count);
714 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
715 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
720 talloc_free(tmp_ctx);
725 display uptime of remote node
727 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
730 struct ctdb_uptime *uptime = NULL;
731 int tmp, days, hours, minutes, seconds;
733 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
735 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
739 if (options.machinereadable){
740 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
741 printf(":%u:%u:%u:%lf\n",
742 (unsigned int)uptime->current_time.tv_sec,
743 (unsigned int)uptime->ctdbd_start_time.tv_sec,
744 (unsigned int)uptime->last_recovery_finished.tv_sec,
745 timeval_delta(&uptime->last_recovery_finished,
746 &uptime->last_recovery_started)
751 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
753 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
761 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
763 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
771 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
773 printf("Duration of last recovery/failover: %lf seconds\n",
774 timeval_delta(&uptime->last_recovery_finished,
775 &uptime->last_recovery_started));
781 show the PNN of the current node
783 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
787 mypnn = getpnn(ctdb);
789 printf("PNN:%d\n", mypnn);
795 struct pnn_node *next;
800 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
802 const char *nodes_list;
806 struct pnn_node *pnn_nodes = NULL;
807 struct pnn_node *pnn_node;
808 struct pnn_node *tmp_node;
810 /* read the nodes file */
811 nodes_list = getenv("CTDB_NODES");
812 if (nodes_list == NULL) {
813 nodes_list = "/etc/ctdb/nodes";
815 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
819 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
822 for (i=0, pnn=0; i<nlines; i++) {
826 /* strip leading spaces */
827 while((*node == ' ') || (*node == '\t')) {
834 if (strcmp(node, "") == 0) {
837 pnn_node = talloc(mem_ctx, struct pnn_node);
838 pnn_node->pnn = pnn++;
839 pnn_node->addr = talloc_strdup(pnn_node, node);
840 pnn_node->next = pnn_nodes;
841 pnn_nodes = pnn_node;
844 /* swap them around so we return them in incrementing order */
845 pnn_node = pnn_nodes;
849 pnn_node = pnn_node->next;
851 tmp_node->next = pnn_nodes;
852 pnn_nodes = tmp_node;
859 show the PNN of the current node
860 discover the pnn by loading the nodes file and try to bind to all
861 addresses one at a time until the ip address is found.
863 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
865 TALLOC_CTX *mem_ctx = talloc_new(NULL);
866 struct pnn_node *pnn_nodes;
867 struct pnn_node *pnn_node;
869 assert_single_node_only();
871 pnn_nodes = read_nodes_file(mem_ctx);
872 if (pnn_nodes == NULL) {
873 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
874 talloc_free(mem_ctx);
878 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
881 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
882 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
883 talloc_free(mem_ctx);
887 if (ctdb_sys_have_ip(&addr)) {
888 printf("PNN:%d\n", pnn_node->pnn);
889 talloc_free(mem_ctx);
894 printf("Failed to detect which PNN this node is\n");
895 talloc_free(mem_ctx);
899 /* Helpers for ctdb status
901 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
903 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
907 if (node->flags == 0) {
908 struct ctdb_control_get_ifaces *ifaces;
910 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
911 tmp_ctx, &ifaces) == 0) {
912 for (j=0; j < ifaces->num; j++) {
913 if (ifaces->ifaces[j].link_state != 0) {
921 talloc_free(tmp_ctx);
926 static void control_status_header_machine(void)
928 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
929 ":Inactive:PartiallyOnline:ThisNode:\n");
932 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
933 struct ctdb_node_and_flags *node)
935 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
936 ctdb_addr_to_str(&node->addr),
937 !!(node->flags&NODE_FLAGS_DISCONNECTED),
938 !!(node->flags&NODE_FLAGS_BANNED),
939 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
940 !!(node->flags&NODE_FLAGS_UNHEALTHY),
941 !!(node->flags&NODE_FLAGS_STOPPED),
942 !!(node->flags&NODE_FLAGS_INACTIVE),
943 is_partially_online(ctdb, node) ? 1 : 0,
944 (node->pnn == mypnn)?'Y':'N');
949 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
950 struct ctdb_node_and_flags *node)
952 printf("pnn:%d %-16s %s%s\n", node->pnn,
953 ctdb_addr_to_str(&node->addr),
954 is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
955 node->pnn == mypnn?" (THIS NODE)":"");
961 display remote ctdb status
963 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
965 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
967 struct ctdb_vnn_map *vnnmap=NULL;
968 struct ctdb_node_map *nodemap=NULL;
969 uint32_t recmode, recmaster, mypnn;
970 int num_deleted_nodes = 0;
973 mypnn = getpnn(ctdb);
975 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
977 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
978 talloc_free(tmp_ctx);
982 if (options.machinereadable) {
983 control_status_header_machine();
984 for (i=0;i<nodemap->num;i++) {
985 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
988 (void) control_status_1_machine(ctdb, mypnn,
991 talloc_free(tmp_ctx);
995 for (i=0; i<nodemap->num; i++) {
996 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1000 if (num_deleted_nodes == 0) {
1001 printf("Number of nodes:%d\n", nodemap->num);
1003 printf("Number of nodes:%d (including %d deleted nodes)\n",
1004 nodemap->num, num_deleted_nodes);
1006 for(i=0;i<nodemap->num;i++){
1007 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1010 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1013 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1015 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1016 talloc_free(tmp_ctx);
1019 if (vnnmap->generation == INVALID_GENERATION) {
1020 printf("Generation:INVALID\n");
1022 printf("Generation:%d\n",vnnmap->generation);
1024 printf("Size:%d\n",vnnmap->size);
1025 for(i=0;i<vnnmap->size;i++){
1026 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1029 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1031 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1032 talloc_free(tmp_ctx);
1035 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1037 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1039 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1040 talloc_free(tmp_ctx);
1043 printf("Recovery master:%d\n",recmaster);
1045 talloc_free(tmp_ctx);
1049 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1051 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1053 struct ctdb_node_map *nodemap=NULL;
1055 uint32_t pnn_mode, mypnn;
1061 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1062 options.pnn, true, &nodes, &pnn_mode)) {
1066 if (options.machinereadable) {
1067 control_status_header_machine();
1068 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1069 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1072 mypnn = getpnn(ctdb);
1074 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1076 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1077 talloc_free(tmp_ctx);
1083 for (i = 0; i < talloc_array_length(nodes); i++) {
1084 if (options.machinereadable) {
1085 ret |= control_status_1_machine(ctdb, mypnn,
1086 &nodemap->nodes[nodes[i]]);
1088 ret |= control_status_1_human(ctdb, mypnn,
1089 &nodemap->nodes[nodes[i]]);
1093 talloc_free(tmp_ctx);
1098 struct natgw_node *next;
1102 static int find_natgw(struct ctdb_context *ctdb,
1103 struct ctdb_node_map *nodemap, uint32_t flags,
1104 uint32_t *pnn, const char **ip)
1107 uint32_t capabilities;
1110 for (i=0;i<nodemap->num;i++) {
1111 if (!(nodemap->nodes[i].flags & flags)) {
1112 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1113 nodemap->nodes[i].pnn,
1116 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1117 nodemap->nodes[i].pnn));
1120 if (!(capabilities&CTDB_CAP_NATGW)) {
1123 *pnn = nodemap->nodes[i].pnn;
1124 *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
1129 return 2; /* matches ENOENT */
1133 display the list of nodes belonging to this natgw configuration
1135 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1137 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1139 const char *natgw_list;
1142 struct natgw_node *natgw_nodes = NULL;
1143 struct natgw_node *natgw_node;
1144 struct ctdb_node_map *nodemap=NULL;
1145 uint32_t mypnn, pnn;
1148 /* When we have some nodes that could be the NATGW, make a
1149 * series of attempts to find the first node that doesn't have
1150 * certain status flags set.
1152 uint32_t exclude_flags[] = {
1153 /* Look for a nice healthy node */
1154 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1155 /* If not found, an UNHEALTHY/BANNED node will do */
1156 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1157 /* If not found, a STOPPED node will do */
1158 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1162 /* read the natgw nodes file into a linked list */
1163 natgw_list = getenv("CTDB_NATGW_NODES");
1164 if (natgw_list == NULL) {
1165 natgw_list = "/etc/ctdb/natgw_nodes";
1167 lines = file_lines_load(natgw_list, &nlines, ctdb);
1168 if (lines == NULL) {
1169 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1170 talloc_free(tmp_ctx);
1173 for (i=0;i<nlines;i++) {
1177 /* strip leading spaces */
1178 while((*node == ' ') || (*node == '\t')) {
1184 if (strcmp(node, "") == 0) {
1187 natgw_node = talloc(ctdb, struct natgw_node);
1188 natgw_node->addr = talloc_strdup(natgw_node, node);
1189 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1190 natgw_node->next = natgw_nodes;
1191 natgw_nodes = natgw_node;
1194 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1196 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1197 talloc_free(tmp_ctx);
1201 /* Trim the nodemap so it only includes connected nodes in the
1202 * current natgw group.
1205 while(i<nodemap->num) {
1206 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1207 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1212 /* this node was not in the natgw so we just remove it from
1215 if ((natgw_node == NULL)
1216 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1219 for (j=i+1; j<nodemap->num; j++) {
1220 nodemap->nodes[j-1] = nodemap->nodes[j];
1229 ret = 2; /* matches ENOENT */
1232 for (i = 0; exclude_flags[i] != 0; i++) {
1233 ret = find_natgw(ctdb, nodemap,
1244 if (options.machinereadable) {
1245 printf(":Node:IP:\n");
1246 printf(":%d:%s:\n", pnn, ip);
1248 printf("%d %s\n", pnn, ip);
1251 /* print the pruned list of nodes belonging to this natgw list */
1252 mypnn = getpnn(ctdb);
1253 if (options.machinereadable) {
1254 control_status_header_machine();
1256 printf("Number of nodes:%d\n", nodemap->num);
1258 for(i=0;i<nodemap->num;i++){
1259 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1262 if (options.machinereadable) {
1263 control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1265 control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1270 talloc_free(tmp_ctx);
1275 display the status of the scripts for monitoring (or other events)
1277 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1278 enum ctdb_eventscript_call type)
1280 struct ctdb_scripts_wire *script_status;
1283 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1285 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1289 if (script_status == NULL) {
1290 if (!options.machinereadable) {
1291 printf("%s cycle never run\n",
1292 ctdb_eventscript_call_names[type]);
1297 if (!options.machinereadable) {
1298 printf("%d scripts were executed last %s cycle\n",
1299 script_status->num_scripts,
1300 ctdb_eventscript_call_names[type]);
1302 for (i=0; i<script_status->num_scripts; i++) {
1303 const char *status = NULL;
1305 switch (script_status->scripts[i].status) {
1307 status = "TIMEDOUT";
1310 status = "DISABLED";
1316 if (script_status->scripts[i].status > 0)
1320 if (options.machinereadable) {
1321 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1322 ctdb_eventscript_call_names[type],
1323 script_status->scripts[i].name,
1324 script_status->scripts[i].status,
1326 (long)script_status->scripts[i].start.tv_sec,
1327 (long)script_status->scripts[i].start.tv_usec,
1328 (long)script_status->scripts[i].finished.tv_sec,
1329 (long)script_status->scripts[i].finished.tv_usec,
1330 script_status->scripts[i].output);
1334 printf("%-20s Status:%s ",
1335 script_status->scripts[i].name, status);
1337 /* Some other error, eg from stat. */
1338 printf("%-20s Status:CANNOT RUN (%s)",
1339 script_status->scripts[i].name,
1340 strerror(-script_status->scripts[i].status));
1342 if (script_status->scripts[i].status >= 0) {
1343 printf("Duration:%.3lf ",
1344 timeval_delta(&script_status->scripts[i].finished,
1345 &script_status->scripts[i].start));
1347 if (script_status->scripts[i].status != -ENOEXEC) {
1349 ctime(&script_status->scripts[i].start.tv_sec));
1350 if (script_status->scripts[i].status != 0) {
1351 printf(" OUTPUT:%s\n",
1352 script_status->scripts[i].output);
1362 static int control_scriptstatus(struct ctdb_context *ctdb,
1363 int argc, const char **argv)
1366 enum ctdb_eventscript_call type, min, max;
1370 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1375 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1379 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1380 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1386 if (type == CTDB_EVENT_MAX) {
1387 if (strcmp(arg, "all") == 0) {
1389 max = CTDB_EVENT_MAX;
1391 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1396 if (options.machinereadable) {
1397 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1400 for (type = min; type < max; type++) {
1401 ret = control_one_scriptstatus(ctdb, type);
1411 enable an eventscript
1413 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1421 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1423 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1431 disable an eventscript
1433 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1441 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1443 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1451 display the pnn of the recovery master
1453 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1458 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1460 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1463 printf("%d\n",recmaster);
1469 add a tickle to a public address
1471 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1473 struct ctdb_tcp_connection t;
1477 assert_single_node_only();
1483 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1484 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1487 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1488 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1492 data.dptr = (uint8_t *)&t;
1493 data.dsize = sizeof(t);
1495 /* tell all nodes about this tcp connection */
1496 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1497 0, data, ctdb, NULL, NULL, NULL, NULL);
1499 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1508 delete a tickle from a node
1510 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1512 struct ctdb_tcp_connection t;
1516 assert_single_node_only();
1522 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1523 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1526 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1527 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1531 data.dptr = (uint8_t *)&t;
1532 data.dsize = sizeof(t);
1534 /* tell all nodes about this tcp connection */
1535 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1536 0, data, ctdb, NULL, NULL, NULL, NULL);
1538 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1547 get a list of all tickles for this pnn
1549 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1551 struct ctdb_control_tcp_tickle_list *list;
1552 ctdb_sock_addr addr;
1556 assert_single_node_only();
1563 port = atoi(argv[1]);
1566 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1567 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1571 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1573 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1577 if (options.machinereadable){
1578 printf(":source ip:port:destination ip:port:\n");
1579 for (i=0;i<list->tickles.num;i++) {
1580 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1583 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1584 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1587 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1588 printf("Num tickles:%u\n", list->tickles.num);
1589 for (i=0;i<list->tickles.num;i++) {
1590 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1593 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1594 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));
1604 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1606 struct ctdb_all_public_ips *ips;
1607 struct ctdb_public_ip ip;
1610 uint32_t disable_time;
1612 struct ctdb_node_map *nodemap=NULL;
1613 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1616 data.dptr = (uint8_t*)&disable_time;
1617 data.dsize = sizeof(disable_time);
1618 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1620 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1626 /* read the public ip list from the node */
1627 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1629 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1630 talloc_free(tmp_ctx);
1634 for (i=0;i<ips->num;i++) {
1635 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1640 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1641 pnn, ctdb_addr_to_str(addr)));
1642 talloc_free(tmp_ctx);
1649 data.dptr = (uint8_t *)&ip;
1650 data.dsize = sizeof(ip);
1652 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1654 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1655 talloc_free(tmp_ctx);
1659 nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1660 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1667 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1668 talloc_free(tmp_ctx);
1672 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1674 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1675 talloc_free(tmp_ctx);
1679 /* update the recovery daemon so it now knows to expect the new
1680 node assignment for this ip.
1682 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1684 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1688 talloc_free(tmp_ctx);
1694 * scans all other nodes and returns a pnn for another node that can host this
1698 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1700 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1701 struct ctdb_all_public_ips *ips;
1702 struct ctdb_node_map *nodemap=NULL;
1705 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1707 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1708 talloc_free(tmp_ctx);
1712 for(i=0;i<nodemap->num;i++){
1713 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1716 if (nodemap->nodes[i].pnn == options.pnn) {
1720 /* read the public ip list from this node */
1721 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1723 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1727 for (j=0;j<ips->num;j++) {
1728 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1729 talloc_free(tmp_ctx);
1730 return nodemap->nodes[i].pnn;
1736 talloc_free(tmp_ctx);
1740 /* If pnn is -1 then try to find a node to move IP to... */
1741 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1743 bool pnn_specified = (pnn == -1 ? false : true);
1746 while (retries < 5) {
1747 if (!pnn_specified) {
1748 pnn = find_other_host_for_public_ip(ctdb, addr);
1753 ("Trying to move public IP to node %u\n", pnn));
1756 if (move_ip(ctdb, addr, pnn) == 0) {
1769 move/failover an ip address to a specific node
1771 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1774 ctdb_sock_addr addr;
1776 assert_single_node_only();
1783 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1784 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1789 if (sscanf(argv[1], "%u", &pnn) != 1) {
1790 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1794 if (!try_moveip(ctdb, &addr, pnn)) {
1795 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1802 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1806 data.dptr = (uint8_t *)&pnn;
1807 data.dsize = sizeof(uint32_t);
1808 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1810 ("Failed to send message to force node %u to be a rebalancing target\n",
1820 rebalance a node by setting it to allow failback and triggering a
1823 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1825 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1830 assert_single_node_only();
1836 /* Determine the nodes where IPs need to be reloaded */
1837 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1838 options.pnn, true, &nodes, &pnn_mode)) {
1843 for (i = 0; i < talloc_array_length(nodes); i++) {
1844 if (!rebalance_node(ctdb, nodes[i])) {
1850 talloc_free(tmp_ctx);
1854 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1856 struct ctdb_public_ip ip;
1859 uint32_t disable_time;
1861 struct ctdb_node_map *nodemap=NULL;
1862 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1865 data.dptr = (uint8_t*)&disable_time;
1866 data.dsize = sizeof(disable_time);
1867 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1869 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1876 data.dptr = (uint8_t *)&ip;
1877 data.dsize = sizeof(ip);
1879 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1881 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1882 talloc_free(tmp_ctx);
1886 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1887 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1894 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1895 talloc_free(tmp_ctx);
1899 talloc_free(tmp_ctx);
1904 release an ip form all nodes and have it re-assigned by recd
1906 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1908 ctdb_sock_addr addr;
1910 assert_single_node_only();
1917 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1918 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1922 if (rebalance_ip(ctdb, &addr) != 0) {
1923 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1930 static int getips_store_callback(void *param, void *data)
1932 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1933 struct ctdb_all_public_ips *ips = param;
1937 ips->ips[i].pnn = node_ip->pnn;
1938 ips->ips[i].addr = node_ip->addr;
1942 static int getips_count_callback(void *param, void *data)
1944 uint32_t *count = param;
1951 static uint32_t *ip_key(ctdb_sock_addr *ip)
1953 static uint32_t key[IP_KEYLEN];
1955 bzero(key, sizeof(key));
1957 switch (ip->sa.sa_family) {
1959 key[0] = ip->ip.sin_addr.s_addr;
1962 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1970 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1977 static void *add_ip_callback(void *parm, void *data)
1983 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1985 struct ctdb_all_public_ips *tmp_ips;
1986 struct ctdb_node_map *nodemap=NULL;
1987 trbt_tree_t *ip_tree;
1991 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1993 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1997 ip_tree = trbt_create(tmp_ctx, 0);
1999 for(i=0;i<nodemap->num;i++){
2000 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2003 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2007 /* read the public ip list from this node */
2008 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2010 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2014 for (j=0; j<tmp_ips->num;j++) {
2015 struct ctdb_public_ip *node_ip;
2017 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2018 node_ip->pnn = tmp_ips->ips[j].pnn;
2019 node_ip->addr = tmp_ips->ips[j].addr;
2021 trbt_insertarray32_callback(ip_tree,
2022 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2026 talloc_free(tmp_ips);
2031 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2033 len = offsetof(struct ctdb_all_public_ips, ips) +
2034 count*sizeof(struct ctdb_public_ip);
2035 tmp_ips = talloc_zero_size(tmp_ctx, len);
2036 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2044 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2046 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2048 event_add_timed(ctdb->ev, ctdb,
2049 timeval_current_ofs(1, 0),
2050 ctdb_every_second, ctdb);
2053 struct srvid_reply_handler_data {
2057 const char *srvid_str;
2060 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2065 struct srvid_reply_handler_data *d =
2066 (struct srvid_reply_handler_data *)private_data;
2070 if (data.dsize != sizeof(ret)) {
2071 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2075 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2076 ret = *(int32_t *)data.dptr;
2079 ("%s failed with result %d\n", d->srvid_str, ret));
2083 if (!d->wait_for_all) {
2088 /* Wait for all replies */
2090 for (i = 0; i < talloc_array_length(d->nodes); i++) {
2091 if (d->nodes[i] == ret) {
2093 ("%s reply received from node %u\n",
2094 d->srvid_str, ret));
2097 if (d->nodes[i] != -1) {
2098 /* Found a node that hasn't yet replied */
2104 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2105 * or replies from all connected nodes. arg is the data argument to
2106 * pass in the srvid_request structure - pass 0 if this isn't needed.
2108 static int srvid_broadcast(struct ctdb_context *ctdb,
2109 uint64_t srvid, uint32_t arg,
2110 const char *srvid_str, bool wait_for_all)
2114 struct srvid_request request;
2115 struct srvid_reply_handler_data reply_data;
2118 ZERO_STRUCT(request);
2120 /* Time ticks to enable timeouts to be processed */
2121 event_add_timed(ctdb->ev, ctdb,
2122 timeval_current_ofs(1, 0),
2123 ctdb_every_second, ctdb);
2125 request.pnn = ctdb_get_pnn(ctdb);
2126 request.srvid = getpid();
2129 /* Register message port for reply from recovery master */
2130 ctdb_client_set_message_handler(ctdb, request.srvid,
2131 srvid_broadcast_reply_handler,
2134 data.dptr = (uint8_t *)&request;
2135 data.dsize = sizeof(request);
2137 reply_data.wait_for_all = wait_for_all;
2138 reply_data.nodes = NULL;
2139 reply_data.srvid_str = srvid_str;
2142 reply_data.done = false;
2145 struct ctdb_node_map *nodemap;
2147 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2148 CTDB_CURRENT_NODE, ctdb, &nodemap);
2151 ("Unable to get nodemap from current node, try again\n"));
2156 if (reply_data.nodes != NULL) {
2157 talloc_free(reply_data.nodes);
2159 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2162 talloc_free(nodemap);
2165 /* Send to all connected nodes. Only recmaster replies */
2166 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2169 /* This can only happen if the socket is closed and
2170 * there's no way to recover from that, so don't try
2174 ("Failed to send %s request to connected nodes\n",
2179 tv = timeval_current();
2180 /* This loop terminates the reply is received */
2181 while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2182 event_loop_once(ctdb->ev);
2185 if (!reply_data.done) {
2187 ("Still waiting for confirmation of %s\n", srvid_str));
2192 ctdb_client_remove_message_handler(ctdb, request.srvid, &reply_data);
2194 talloc_free(reply_data.nodes);
2199 static int ipreallocate(struct ctdb_context *ctdb)
2201 return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, 0,
2202 "IP reallocation", false);
2206 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2208 return ipreallocate(ctdb);
2212 add a public ip address to a node
2214 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2217 int len, retries = 0;
2219 ctdb_sock_addr addr;
2220 struct ctdb_control_ip_iface *pub;
2221 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2222 struct ctdb_all_public_ips *ips;
2226 talloc_free(tmp_ctx);
2230 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2231 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2232 talloc_free(tmp_ctx);
2236 /* read the public ip list from the node */
2237 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2239 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2240 talloc_free(tmp_ctx);
2243 for (i=0;i<ips->num;i++) {
2244 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2245 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2252 /* Dont timeout. This command waits for an ip reallocation
2253 which sometimes can take wuite a while if there has
2254 been a recent recovery
2258 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2259 pub = talloc_size(tmp_ctx, len);
2260 CTDB_NO_MEMORY(ctdb, pub);
2264 pub->len = strlen(argv[1])+1;
2265 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2268 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2270 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2274 } while (retries < 5 && ret != 0);
2276 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2277 talloc_free(tmp_ctx);
2281 if (rebalance_node(ctdb, options.pnn) != 0) {
2282 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2286 talloc_free(tmp_ctx);
2291 add a public ip address to a node
2293 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2295 ctdb_sock_addr addr;
2301 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2302 printf("Badly formed ip : %s\n", argv[0]);
2306 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2311 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2313 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2315 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2316 struct ctdb_node_map *nodemap=NULL;
2317 struct ctdb_all_public_ips *ips;
2320 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2322 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2326 /* remove it from the nodes that are not hosting the ip currently */
2327 for(i=0;i<nodemap->num;i++){
2328 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2331 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2333 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2337 for (j=0;j<ips->num;j++) {
2338 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2346 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2350 options.pnn = nodemap->nodes[i].pnn;
2351 control_delip(ctdb, argc, argv);
2355 /* remove it from every node (also the one hosting it) */
2356 for(i=0;i<nodemap->num;i++){
2357 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2360 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2362 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2366 for (j=0;j<ips->num;j++) {
2367 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2375 options.pnn = nodemap->nodes[i].pnn;
2376 control_delip(ctdb, argc, argv);
2379 talloc_free(tmp_ctx);
2384 delete a public ip address from a node
2386 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2389 ctdb_sock_addr addr;
2390 struct ctdb_control_ip_iface pub;
2391 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2392 struct ctdb_all_public_ips *ips;
2395 talloc_free(tmp_ctx);
2399 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2400 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2404 if (options.pnn == CTDB_BROADCAST_ALL) {
2405 return control_delip_all(ctdb, argc, argv, &addr);
2412 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2414 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2415 talloc_free(tmp_ctx);
2419 for (i=0;i<ips->num;i++) {
2420 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2426 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2427 ctdb_addr_to_str(&addr)));
2428 talloc_free(tmp_ctx);
2432 /* This is an optimisation. If this node is hosting the IP
2433 * then try to move it somewhere else without invoking a full
2434 * takeover run. We don't care if this doesn't work!
2436 if (ips->ips[i].pnn == options.pnn) {
2437 (void) try_moveip(ctdb, &addr, -1);
2440 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2442 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2443 talloc_free(tmp_ctx);
2447 talloc_free(tmp_ctx);
2451 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2452 int argc, const char **argv)
2454 struct ctdb_control_killtcp *killtcp;
2455 int max_entries, current, i;
2456 struct timeval timeout;
2457 char line[128], src[128], dst[128];
2460 struct client_async_data *async_data;
2461 struct ctdb_client_control_state *state;
2471 while (!feof(stdin)) {
2472 if (fgets(line, sizeof(line), stdin) == NULL) {
2476 /* Silently skip empty lines */
2477 if (line[0] == '\n') {
2481 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2482 DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2484 talloc_free(killtcp);
2488 if (current >= max_entries) {
2489 max_entries += 1024;
2490 killtcp = talloc_realloc(ctdb, killtcp,
2491 struct ctdb_control_killtcp,
2493 CTDB_NO_MEMORY(ctdb, killtcp);
2496 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2497 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2499 talloc_free(killtcp);
2503 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2504 DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2506 talloc_free(killtcp);
2513 async_data = talloc_zero(ctdb, struct client_async_data);
2514 if (async_data == NULL) {
2515 talloc_free(killtcp);
2519 for (i = 0; i < current; i++) {
2521 data.dsize = sizeof(struct ctdb_control_killtcp);
2522 data.dptr = (unsigned char *)&killtcp[i];
2524 timeout = TIMELIMIT();
2525 state = ctdb_control_send(ctdb, options.pnn, 0,
2526 CTDB_CONTROL_KILL_TCP, 0, data,
2527 async_data, &timeout, NULL);
2529 if (state == NULL) {
2531 ("Failed to call async killtcp control to node %u\n",
2533 talloc_free(killtcp);
2537 ctdb_client_async_add(async_data, state);
2540 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2541 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2542 talloc_free(killtcp);
2546 talloc_free(killtcp);
2552 kill a tcp connection
2554 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2557 struct ctdb_control_killtcp killtcp;
2559 assert_single_node_only();
2562 return kill_tcp_from_file(ctdb, argc, argv);
2569 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2570 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2574 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2575 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2579 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2581 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2592 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2595 ctdb_sock_addr addr;
2597 assert_single_node_only();
2603 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2604 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2608 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2610 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2618 register a server id
2620 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2623 struct ctdb_server_id server_id;
2629 server_id.pnn = strtoul(argv[0], NULL, 0);
2630 server_id.type = strtoul(argv[1], NULL, 0);
2631 server_id.server_id = strtoul(argv[2], NULL, 0);
2633 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2635 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2638 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2644 unregister a server id
2646 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2649 struct ctdb_server_id server_id;
2655 server_id.pnn = strtoul(argv[0], NULL, 0);
2656 server_id.type = strtoul(argv[1], NULL, 0);
2657 server_id.server_id = strtoul(argv[2], NULL, 0);
2659 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2661 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2668 check if a server id exists
2670 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2674 struct ctdb_server_id server_id;
2680 server_id.pnn = strtoul(argv[0], NULL, 0);
2681 server_id.type = strtoul(argv[1], NULL, 0);
2682 server_id.server_id = strtoul(argv[2], NULL, 0);
2684 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2686 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2691 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2693 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2699 get a list of all server ids that are registered on a node
2701 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2704 struct ctdb_server_id_list *server_ids;
2706 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2708 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2712 for (i=0; i<server_ids->num; i++) {
2713 printf("Server id %d:%d:%d\n",
2714 server_ids->server_ids[i].pnn,
2715 server_ids->server_ids[i].type,
2716 server_ids->server_ids[i].server_id);
2723 check if a server id exists
2725 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2727 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2733 talloc_free(tmp_ctx);
2737 ids = talloc_array(tmp_ctx, uint64_t, argc);
2738 result = talloc_array(tmp_ctx, uint8_t, argc);
2740 for (i = 0; i < argc; i++) {
2741 ids[i] = strtoull(argv[i], NULL, 0);
2744 if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2745 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2747 talloc_free(tmp_ctx);
2751 for (i=0; i < argc; i++) {
2752 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2753 result[i] ? "exists" : "does not exist");
2756 talloc_free(tmp_ctx);
2761 send a tcp tickle ack
2763 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2766 ctdb_sock_addr src, dst;
2772 if (!parse_ip_port(argv[0], &src)) {
2773 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2777 if (!parse_ip_port(argv[1], &dst)) {
2778 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2782 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2786 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2793 display public ip status
2795 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2798 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2799 struct ctdb_all_public_ips *ips;
2801 if (options.pnn == CTDB_BROADCAST_ALL) {
2802 /* read the list of public ips from all nodes */
2803 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2805 /* read the public ip list from this node */
2806 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2809 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2810 talloc_free(tmp_ctx);
2814 if (options.machinereadable){
2815 printf(":Public IP:Node:");
2816 if (options.verbose){
2817 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2821 if (options.pnn == CTDB_BROADCAST_ALL) {
2822 printf("Public IPs on ALL nodes\n");
2824 printf("Public IPs on node %u\n", options.pnn);
2828 for (i=1;i<=ips->num;i++) {
2829 struct ctdb_control_public_ip_info *info = NULL;
2831 char *aciface = NULL;
2832 char *avifaces = NULL;
2833 char *cifaces = NULL;
2835 if (options.pnn == CTDB_BROADCAST_ALL) {
2836 pnn = ips->ips[ips->num-i].pnn;
2842 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2843 &ips->ips[ips->num-i].addr, &info);
2850 for (j=0; j < info->num; j++) {
2851 if (cifaces == NULL) {
2852 cifaces = talloc_strdup(info,
2853 info->ifaces[j].name);
2855 cifaces = talloc_asprintf_append(cifaces,
2857 info->ifaces[j].name);
2860 if (info->active_idx == j) {
2861 aciface = info->ifaces[j].name;
2864 if (info->ifaces[j].link_state == 0) {
2868 if (avifaces == NULL) {
2869 avifaces = talloc_strdup(info, info->ifaces[j].name);
2871 avifaces = talloc_asprintf_append(avifaces,
2873 info->ifaces[j].name);
2878 if (options.machinereadable){
2880 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2881 ips->ips[ips->num-i].pnn);
2882 if (options.verbose){
2885 avifaces?avifaces:"",
2886 cifaces?cifaces:"");
2890 if (options.verbose) {
2891 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2892 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2893 ips->ips[ips->num-i].pnn,
2895 avifaces?avifaces:"",
2896 cifaces?cifaces:"");
2899 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2900 ips->ips[ips->num-i].pnn);
2906 talloc_free(tmp_ctx);
2913 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2916 ctdb_sock_addr addr;
2917 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2918 struct ctdb_control_public_ip_info *info;
2921 talloc_free(tmp_ctx);
2925 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2926 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2930 /* read the public ip info from this node */
2931 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2932 tmp_ctx, &addr, &info);
2934 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2935 argv[0], options.pnn));
2936 talloc_free(tmp_ctx);
2940 printf("Public IP[%s] info on node %u\n",
2941 ctdb_addr_to_str(&info->ip.addr),
2944 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2945 ctdb_addr_to_str(&info->ip.addr),
2946 info->ip.pnn, info->num);
2948 for (i=0; i<info->num; i++) {
2949 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2951 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2952 i+1, info->ifaces[i].name,
2953 info->ifaces[i].link_state?"up":"down",
2954 (unsigned int)info->ifaces[i].references,
2955 (i==info->active_idx)?" (active)":"");
2958 talloc_free(tmp_ctx);
2963 display interfaces status
2965 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2967 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2969 struct ctdb_control_get_ifaces *ifaces;
2972 /* read the public ip list from this node */
2973 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
2975 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2977 talloc_free(tmp_ctx);
2981 if (options.machinereadable){
2982 printf(":Name:LinkStatus:References:\n");
2984 printf("Interfaces on node %u\n", options.pnn);
2987 for (i=0; i<ifaces->num; i++) {
2988 if (options.machinereadable){
2989 printf(":%s:%s:%u\n",
2990 ifaces->ifaces[i].name,
2991 ifaces->ifaces[i].link_state?"1":"0",
2992 (unsigned int)ifaces->ifaces[i].references);
2994 printf("name:%s link:%s references:%u\n",
2995 ifaces->ifaces[i].name,
2996 ifaces->ifaces[i].link_state?"up":"down",
2997 (unsigned int)ifaces->ifaces[i].references);
3001 talloc_free(tmp_ctx);
3007 set link status of an interface
3009 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3012 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3013 struct ctdb_control_iface_info info;
3021 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3022 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3024 talloc_free(tmp_ctx);
3027 strcpy(info.name, argv[0]);
3029 if (strcmp(argv[1], "up") == 0) {
3030 info.link_state = 1;
3031 } else if (strcmp(argv[1], "down") == 0) {
3032 info.link_state = 0;
3034 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3036 talloc_free(tmp_ctx);
3040 /* read the public ip list from this node */
3041 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3044 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3045 argv[0], options.pnn));
3046 talloc_free(tmp_ctx);
3050 talloc_free(tmp_ctx);
3055 display pid of a ctdb daemon
3057 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3062 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3064 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3067 printf("Pid:%d\n", pid);
3072 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3074 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3076 update_flags_handler_t handler,
3081 struct ctdb_node_map *nodemap = NULL;
3085 /* Check if the node is already in the desired state */
3086 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3088 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3091 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3092 if (set_flag == flag_is_set) {
3093 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3094 (set_flag ? "already" : "not"), desc));
3099 if (!handler(ctdb, data)) {
3100 DEBUG(DEBUG_WARNING,
3101 ("Failed to send control to set state %s on node %u, try again\n",
3102 desc, options.pnn));
3107 /* Read the nodemap and verify the change took effect.
3108 * Even if the above control/hanlder timed out then it
3109 * could still have worked!
3111 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3114 DEBUG(DEBUG_WARNING,
3115 ("Unable to get nodemap from local node, try again\n"));
3117 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3118 } while (nodemap == NULL || (set_flag != flag_is_set));
3120 return ipreallocate(ctdb);
3123 /* Administratively disable a node */
3124 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3128 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3129 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3133 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3135 return update_flags_and_ipreallocate(ctdb, NULL,
3136 update_flags_disabled,
3137 NODE_FLAGS_PERMANENTLY_DISABLED,
3139 true /* set_flag*/);
3142 /* Administratively re-enable a node */
3143 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3147 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3148 0, NODE_FLAGS_PERMANENTLY_DISABLED);
3152 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
3154 return update_flags_and_ipreallocate(ctdb, NULL,
3155 update_flags_not_disabled,
3156 NODE_FLAGS_PERMANENTLY_DISABLED,
3158 false /* set_flag*/);
3162 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3166 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3171 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3173 return update_flags_and_ipreallocate(ctdb, NULL,
3174 update_flags_stopped,
3177 true /* set_flag*/);
3180 /* Continue a stopped node */
3181 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3185 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3190 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3192 return update_flags_and_ipreallocate(ctdb, NULL,
3193 update_flags_not_stopped,
3196 false /* set_flag */);
3199 static uint32_t get_generation(struct ctdb_context *ctdb)
3201 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3202 struct ctdb_vnn_map *vnnmap=NULL;
3204 uint32_t generation;
3206 /* wait until the recmaster is not in recovery mode */
3208 uint32_t recmode, recmaster;
3210 if (vnnmap != NULL) {
3211 talloc_free(vnnmap);
3215 /* get the recmaster */
3216 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3218 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3219 talloc_free(tmp_ctx);
3223 /* get recovery mode */
3224 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3226 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3227 talloc_free(tmp_ctx);
3231 /* get the current generation number */
3232 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3234 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3235 talloc_free(tmp_ctx);
3239 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3240 generation = vnnmap->generation;
3241 talloc_free(tmp_ctx);
3249 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3251 struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3254 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3259 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3261 struct ctdb_ban_time bantime;
3267 bantime.pnn = options.pnn;
3268 bantime.time = strtoul(argv[0], NULL, 0);
3270 if (bantime.time == 0) {
3271 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3275 return update_flags_and_ipreallocate(ctdb, &bantime,
3276 update_state_banned,
3279 true /* set_flag*/);
3284 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3286 struct ctdb_ban_time bantime;
3288 bantime.pnn = options.pnn;
3291 return update_flags_and_ipreallocate(ctdb, &bantime,
3292 update_state_banned,
3295 false /* set_flag*/);
3299 show ban information for a node
3301 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3304 struct ctdb_node_map *nodemap=NULL;
3305 struct ctdb_ban_time *bantime;
3307 /* verify the node exists */
3308 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3310 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3314 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3316 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3320 if (bantime->time == 0) {
3321 printf("Node %u is not banned\n", bantime->pnn);
3323 printf("Node %u is banned, %d seconds remaining\n",
3324 bantime->pnn, bantime->time);
3333 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3337 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3339 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3349 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3352 uint32_t generation, next_generation;
3355 /* "force" option ignores freeze failure and forces recovery */
3356 force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3358 /* record the current generation number */
3359 generation = get_generation(ctdb);
3361 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3364 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3367 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3370 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3372 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3376 /* wait until we are in a new generation */
3378 next_generation = get_generation(ctdb);
3379 if (next_generation != generation) {
3390 display monitoring mode of a remote node
3392 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3397 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3399 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3402 if (!options.machinereadable){
3403 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3406 printf(":%d:\n",monmode);
3413 display capabilities of a remote node
3415 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3417 uint32_t capabilities;
3420 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3422 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3426 if (!options.machinereadable){
3427 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3428 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3429 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3430 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3432 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3433 printf(":%d:%d:%d:%d:\n",
3434 !!(capabilities&CTDB_CAP_RECMASTER),
3435 !!(capabilities&CTDB_CAP_LMASTER),
3436 !!(capabilities&CTDB_CAP_LVS),
3437 !!(capabilities&CTDB_CAP_NATGW));
3443 display lvs configuration
3445 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3447 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3448 uint32_t *capabilities;
3449 struct ctdb_node_map *nodemap=NULL;
3451 int healthy_count = 0;
3453 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
3455 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3456 talloc_free(tmp_ctx);
3460 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3461 CTDB_NO_MEMORY(ctdb, capabilities);
3465 /* collect capabilities for all connected nodes */
3466 for (i=0; i<nodemap->num; i++) {
3467 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3470 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3474 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3476 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3481 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3485 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3490 /* Print all LVS nodes */
3491 for (i=0; i<nodemap->num; i++) {
3492 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3495 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3498 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3502 if (healthy_count != 0) {
3503 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3508 printf("%d:%s\n", i,
3509 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3513 talloc_free(tmp_ctx);
3518 display who is the lvs master
3520 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3522 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3523 uint32_t *capabilities;
3524 struct ctdb_node_map *nodemap=NULL;
3526 int healthy_count = 0;
3528 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
3530 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3531 talloc_free(tmp_ctx);
3535 capabilities = talloc_array(tmp_ctx, uint32_t, nodemap->num);
3536 if (capabilities == NULL) {
3537 talloc_free(tmp_ctx);
3538 CTDB_NO_MEMORY(ctdb, capabilities);
3543 /* collect capabilities for all connected nodes */
3544 for (i=0; i<nodemap->num; i++) {
3545 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3548 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3552 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3554 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3559 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3563 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3568 /* find and show the lvsmaster */
3569 for (i=0; i<nodemap->num; i++) {
3570 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3573 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3576 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3580 if (healthy_count != 0) {
3581 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3586 if (options.machinereadable){
3589 printf("Node %d is LVS master\n", i);
3595 printf("There is no LVS master\n");
3597 talloc_free(tmp_ctx);
3602 disable monitoring on a node
3604 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3609 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3611 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3614 printf("Monitoring mode:%s\n","DISABLED");
3620 enable monitoring on a node
3622 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3627 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3629 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3632 printf("Monitoring mode:%s\n","ACTIVE");
3638 display remote list of keys/data for a db
3640 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3642 const char *db_name;
3643 struct ctdb_db_context *ctdb_db;
3645 struct ctdb_dump_db_context c;
3654 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3658 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3659 if (ctdb_db == NULL) {
3660 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3664 if (options.printlmaster) {
3665 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3666 ctdb, &ctdb->vnn_map);
3668 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3676 c.printemptyrecords = (bool)options.printemptyrecords;
3677 c.printdatasize = (bool)options.printdatasize;
3678 c.printlmaster = (bool)options.printlmaster;
3679 c.printhash = (bool)options.printhash;
3680 c.printrecordflags = (bool)options.printrecordflags;
3682 /* traverse and dump the cluster tdb */
3683 ret = ctdb_dump_db(ctdb_db, &c);
3685 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3686 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3687 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3691 talloc_free(ctdb_db);
3693 printf("Dumped %d records\n", ret);
3697 struct cattdb_data {
3698 struct ctdb_context *ctdb;
3702 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3704 struct cattdb_data *d = private_data;
3705 struct ctdb_dump_db_context c;
3711 c.printemptyrecords = (bool)options.printemptyrecords;
3712 c.printdatasize = (bool)options.printdatasize;
3713 c.printlmaster = false;
3714 c.printhash = (bool)options.printhash;
3715 c.printrecordflags = true;
3717 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3721 cat the local tdb database using same format as catdb
3723 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3725 const char *db_name;
3726 struct ctdb_db_context *ctdb_db;
3727 struct cattdb_data d;
3736 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3740 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3741 if (ctdb_db == NULL) {
3742 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3746 /* traverse the local tdb */
3749 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3750 printf("Failed to cattdb data\n");
3753 talloc_free(ctdb_db);
3755 printf("Dumped %d records\n", d.count);
3760 display the content of a database key
3762 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3764 const char *db_name;
3765 struct ctdb_db_context *ctdb_db;
3766 struct ctdb_record_handle *h;
3767 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3777 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3781 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3782 if (ctdb_db == NULL) {
3783 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3787 key.dptr = discard_const(argv[1]);
3788 key.dsize = strlen((char *)key.dptr);
3790 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3792 printf("Failed to fetch record '%s' on node %d\n",
3793 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3794 talloc_free(tmp_ctx);
3798 printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3800 talloc_free(ctdb_db);
3801 talloc_free(tmp_ctx);
3806 display the content of a database key
3808 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3810 const char *db_name;
3811 struct ctdb_db_context *ctdb_db;
3812 struct ctdb_record_handle *h;
3813 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3823 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3827 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3828 if (ctdb_db == NULL) {
3829 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3833 key.dptr = discard_const(argv[1]);
3834 key.dsize = strlen((char *)key.dptr);
3836 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3838 printf("Failed to fetch record '%s' on node %d\n",
3839 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3840 talloc_free(tmp_ctx);
3844 data.dptr = discard_const(argv[2]);
3845 data.dsize = strlen((char *)data.dptr);
3847 if (ctdb_record_store(h, data) != 0) {
3848 printf("Failed to store record\n");
3852 talloc_free(ctdb_db);
3853 talloc_free(tmp_ctx);
3858 fetch a record from a persistent database
3860 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3862 const char *db_name;
3863 struct ctdb_db_context *ctdb_db;
3864 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3865 struct ctdb_transaction_handle *h;
3872 talloc_free(tmp_ctx);
3878 if (!db_exists(ctdb, db_name, NULL, &flags)) {
3879 talloc_free(tmp_ctx);
3883 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3885 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3886 talloc_free(tmp_ctx);
3890 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3891 if (ctdb_db == NULL) {
3892 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3893 talloc_free(tmp_ctx);
3897 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3899 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3900 talloc_free(tmp_ctx);
3904 key.dptr = discard_const(argv[1]);
3905 key.dsize = strlen(argv[1]);
3906 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3908 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3909 talloc_free(tmp_ctx);
3913 if (data.dsize == 0 || data.dptr == NULL) {
3914 DEBUG(DEBUG_ERR,("Record is empty\n"));
3915 talloc_free(tmp_ctx);
3920 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3922 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3923 talloc_free(tmp_ctx);
3926 write(fd, data.dptr, data.dsize);
3929 write(1, data.dptr, data.dsize);
3932 /* abort the transaction */
3936 talloc_free(tmp_ctx);
3941 fetch a record from a tdb-file
3943 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3945 const char *tdb_file;
3948 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3957 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3959 printf("Failed to open TDB file %s\n", tdb_file);
3963 if (!strncmp(argv[1], "0x", 2)) {
3964 key = hextodata(tmp_ctx, argv[1] + 2);
3965 if (key.dsize == 0) {
3966 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3970 key.dptr = discard_const(argv[1]);
3971 key.dsize = strlen(argv[1]);
3974 data = tdb_fetch(tdb, key);
3975 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3976 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3984 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3986 printf("Failed to open output file %s\n", argv[2]);
3989 if (options.verbose){
3990 write(fd, data.dptr, data.dsize);
3992 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3996 if (options.verbose){
3997 write(1, data.dptr, data.dsize);
3999 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4003 talloc_free(tmp_ctx);
4008 store a record and header to a tdb-file
4010 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4012 const char *tdb_file;
4015 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4023 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4025 printf("Failed to open TDB file %s\n", tdb_file);
4029 if (!strncmp(argv[1], "0x", 2)) {
4030 key = hextodata(tmp_ctx, argv[1] + 2);
4031 if (key.dsize == 0) {
4032 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4036 key.dptr = discard_const(argv[1]);
4037 key.dsize = strlen(argv[1]);
4040 if (!strncmp(argv[2], "0x", 2)) {
4041 data = hextodata(tmp_ctx, argv[2] + 2);
4042 if (data.dsize == 0) {
4043 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4047 data.dptr = discard_const(argv[2]);
4048 data.dsize = strlen(argv[2]);
4051 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
4052 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
4055 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4056 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4063 talloc_free(tmp_ctx);
4068 write a record to a persistent database
4070 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4072 const char *db_name;
4073 struct ctdb_db_context *ctdb_db;
4074 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4075 struct ctdb_transaction_handle *h;
4081 talloc_free(tmp_ctx);
4085 fd = open(argv[2], O_RDONLY);
4087 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
4088 talloc_free(tmp_ctx);
4092 ret = fstat(fd, &st);
4094 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4096 talloc_free(tmp_ctx);
4100 if (!S_ISREG(st.st_mode)) {
4101 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4103 talloc_free(tmp_ctx);
4107 data.dsize = st.st_size;
4108 if (data.dsize == 0) {
4111 data.dptr = talloc_size(tmp_ctx, data.dsize);
4112 if (data.dptr == NULL) {
4113 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4115 talloc_free(tmp_ctx);
4118 ret = read(fd, data.dptr, data.dsize);
4119 if (ret != data.dsize) {
4120 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4122 talloc_free(tmp_ctx);
4131 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4132 if (ctdb_db == NULL) {
4133 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4134 talloc_free(tmp_ctx);
4138 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4140 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4141 talloc_free(tmp_ctx);
4145 key.dptr = discard_const(argv[1]);
4146 key.dsize = strlen(argv[1]);
4147 ret = ctdb_transaction_store(h, key, data);
4149 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4150 talloc_free(tmp_ctx);
4154 ret = ctdb_transaction_commit(h);
4156 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4157 talloc_free(tmp_ctx);
4162 talloc_free(tmp_ctx);
4167 * delete a record from a persistent database
4169 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4171 const char *db_name;
4172 struct ctdb_db_context *ctdb_db;
4173 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4174 struct ctdb_transaction_handle *h;
4181 talloc_free(tmp_ctx);
4187 if (!db_exists(ctdb, db_name, NULL, &flags)) {
4188 talloc_free(tmp_ctx);
4192 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4194 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4195 talloc_free(tmp_ctx);
4199 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4200 if (ctdb_db == NULL) {
4201 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4202 talloc_free(tmp_ctx);
4206 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4208 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4209 talloc_free(tmp_ctx);
4213 key.dptr = discard_const(argv[1]);
4214 key.dsize = strlen(argv[1]);
4215 ret = ctdb_transaction_store(h, key, tdb_null);
4217 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4218 talloc_free(tmp_ctx);
4222 ret = ctdb_transaction_commit(h);
4224 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4225 talloc_free(tmp_ctx);
4229 talloc_free(tmp_ctx);
4234 check if a service is bound to a port or not
4236 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4241 struct sockaddr_in sin;
4244 printf("Use: ctdb chktcport <port>\n");
4248 port = atoi(argv[0]);
4250 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4252 printf("Failed to open local socket\n");
4256 v = fcntl(s, F_GETFL, 0);
4257 fcntl(s, F_SETFL, v | O_NONBLOCK);
4259 bzero(&sin, sizeof(sin));
4260 sin.sin_family = PF_INET;
4261 sin.sin_port = htons(port);
4262 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4265 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4274 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4275 TDB_DATA data, void *private_data)
4277 DEBUG(DEBUG_ERR,("Log data received\n"));
4278 if (data.dsize > 0) {
4279 printf("%s", data.dptr);
4286 display a list of log messages from the in memory ringbuffer
4288 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4292 struct ctdb_get_log_addr log_addr;
4296 /* Process options */
4298 log_addr.pnn = ctdb_get_pnn(ctdb);
4299 log_addr.level = DEBUG_NOTICE;
4300 for (i = 0; i < argc; i++) {
4301 if (strcmp(argv[i], "recoverd") == 0) {
4302 main_daemon = false;
4304 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4305 log_addr.level = get_debug_by_desc(argv[i]);
4307 log_addr.level = strtol(argv[i], NULL, 0);
4312 /* Our message port is our PID */
4313 log_addr.srvid = getpid();
4315 data.dptr = (unsigned char *)&log_addr;
4316 data.dsize = sizeof(log_addr);
4318 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4320 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4323 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4328 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4330 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4331 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4332 if (ret != 0 || res != 0) {
4333 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4334 talloc_free(tmp_ctx);
4337 talloc_free(tmp_ctx);
4339 ret = ctdb_client_send_message(ctdb, options.pnn,
4340 CTDB_SRVID_GETLOG, data);
4342 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4347 tv = timeval_current();
4348 /* this loop will terminate when we have received the reply */
4349 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4350 event_loop_once(ctdb->ev);
4353 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4359 clear the in memory log area
4361 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4365 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4366 /* "recoverd" not given - get logs from main daemon */
4369 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4371 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4372 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4373 if (ret != 0 || res != 0) {
4374 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4375 talloc_free(tmp_ctx);
4379 talloc_free(tmp_ctx);
4381 TDB_DATA data; /* unused in recoverd... */
4384 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4386 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4394 /* Reload public IPs on a specified nodes */
4395 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4397 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4402 assert_single_node_only();
4408 /* Determine the nodes where IPs need to be reloaded */
4409 if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4410 options.pnn, true, &nodes, &pnn_mode)) {
4416 /* Disable takeover runs on all connected nodes. A reply
4417 * indicating success is needed from each node so all nodes
4418 * will need to be active. This will retry until maxruntime
4419 * is exceeded, hence no error handling.
4421 * A check could be added to not allow reloading of IPs when
4422 * there are disconnected nodes. However, this should
4423 * probably be left up to the administrator.
4425 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, LONGTIMEOUT,
4426 "Disable takeover runs", true);
4428 /* Now tell all the desired nodes to reload their public IPs.
4429 * Keep trying this until it succeeds. This assumes all
4430 * failures are transient, which might not be true...
4432 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4433 nodes, 0, LONGTIMELIMIT(),
4435 NULL, NULL, NULL) != 0) {
4437 ("Unable to reload IPs on some nodes, try again.\n"));
4441 /* It isn't strictly necessary to wait until takeover runs are
4442 * re-enabled but doing so can't hurt.
4444 srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, 0,
4445 "Enable takeover runs", true);
4451 talloc_free(tmp_ctx);
4456 display a list of the databases on a remote ctdb
4458 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4461 struct ctdb_dbid_map *dbmap=NULL;
4463 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4465 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4469 if(options.machinereadable){
4470 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4471 for(i=0;i<dbmap->num;i++){
4479 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4480 dbmap->dbs[i].dbid, ctdb, &path);
4481 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4482 dbmap->dbs[i].dbid, ctdb, &name);
4483 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4484 dbmap->dbs[i].dbid, ctdb, &health);
4485 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4486 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4487 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4488 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4489 dbmap->dbs[i].dbid, name, path,
4490 !!(persistent), !!(sticky),
4491 !!(health), !!(readonly));
4496 printf("Number of databases:%d\n", dbmap->num);
4497 for(i=0;i<dbmap->num;i++){
4505 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4506 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4507 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4508 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4509 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4510 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4511 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4512 dbmap->dbs[i].dbid, name, path,
4513 persistent?" PERSISTENT":"",
4514 sticky?" STICKY":"",
4515 readonly?" READONLY":"",
4516 health?" UNHEALTHY":"");
4523 display the status of a database on a remote ctdb
4525 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4527 const char *db_name;
4539 if (!db_exists(ctdb, db_name, &db_id, &flags)) {
4543 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4544 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4545 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4546 db_id, db_name, path,
4547 (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4548 (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4549 (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4550 (health ? health : "OK"));
4556 check if the local node is recmaster or not
4557 it will return 1 if this node is the recmaster and 0 if it is not
4558 or if the local ctdb daemon could not be contacted
4560 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4562 uint32_t mypnn, recmaster;
4565 assert_single_node_only();
4567 mypnn = getpnn(ctdb);
4569 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4571 printf("Failed to get the recmaster\n");
4575 if (recmaster != mypnn) {
4576 printf("this node is not the recmaster\n");
4580 printf("this node is the recmaster\n");
4587 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4590 struct timeval tv = timeval_current();
4591 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4593 printf("Unable to get ping response from node %u\n", options.pnn);
4596 printf("response from %u time=%.6f sec (%d clients)\n",
4597 options.pnn, timeval_elapsed(&tv), ret);
4604 get a node's runstate
4606 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4609 enum ctdb_runstate runstate;
4611 ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4613 printf("Unable to get runstate response from node %u\n",
4618 enum ctdb_runstate t;
4620 for (i=0; i<argc; i++) {
4622 t = runstate_from_string(argv[i]);
4623 if (t == CTDB_RUNSTATE_UNKNOWN) {
4624 printf("Invalid run state (%s)\n", argv[i]);
4628 if (t == runstate) {
4635 printf("CTDB not in required run state (got %s)\n",
4636 runstate_to_string((enum ctdb_runstate)runstate));
4641 printf("%s\n", runstate_to_string(runstate));
4649 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4660 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4662 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4666 printf("%-23s = %u\n", name, value);
4673 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4684 value = strtoul(argv[1], NULL, 0);
4686 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4688 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4697 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4703 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4705 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4709 for (i=0;i<count;i++) {
4710 control_getvar(ctdb, 1, &list[i]);
4719 display debug level on a node
4721 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4726 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4728 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4731 if (options.machinereadable){
4732 printf(":Name:Level:\n");
4733 printf(":%s:%d:\n",get_debug_by_level(level),level);
4735 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4742 display reclock file of a node
4744 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4747 const char *reclock;
4749 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4751 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4754 if (options.machinereadable){
4755 if (reclock != NULL) {
4756 printf("%s", reclock);
4759 if (reclock == NULL) {
4760 printf("No reclock file used.\n");
4762 printf("Reclock file:%s\n", reclock);
4770 set the reclock file of a node
4772 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4775 const char *reclock;
4779 } else if (argc == 1) {
4785 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4787 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4794 set the natgw state on/off
4796 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4799 uint32_t natgwstate;
4805 if (!strcmp(argv[0], "on")) {
4807 } else if (!strcmp(argv[0], "off")) {
4813 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4815 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4823 set the lmaster role on/off
4825 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4828 uint32_t lmasterrole;
4834 if (!strcmp(argv[0], "on")) {
4836 } else if (!strcmp(argv[0], "off")) {
4842 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4844 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4852 set the recmaster role on/off
4854 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4857 uint32_t recmasterrole;
4863 if (!strcmp(argv[0], "on")) {
4865 } else if (!strcmp(argv[0], "off")) {
4871 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4873 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4881 set debug level on a node or all nodes
4883 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4889 printf("You must specify the debug level. Valid levels are:\n");
4890 for (i=0; debug_levels[i].description != NULL; i++) {
4891 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4897 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4898 level = get_debug_by_desc(argv[0]);
4900 level = strtol(argv[0], NULL, 0);
4903 for (i=0; debug_levels[i].description != NULL; i++) {
4904 if (level == debug_levels[i].level) {
4908 if (debug_levels[i].description == NULL) {
4909 printf("Invalid debug level, must be one of\n");
4910 for (i=0; debug_levels[i].description != NULL; i++) {
4911 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4916 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4918 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4927 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4933 priority = strtol(argv[0], NULL, 0);
4937 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4939 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4941 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4948 attach to a database
4950 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4952 const char *db_name;
4953 struct ctdb_db_context *ctdb_db;
4954 bool persistent = false;
4964 if (strcmp(argv[1], "persistent") != 0) {
4970 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4971 if (ctdb_db == NULL) {
4972 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4982 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4984 struct ctdb_db_priority db_prio;
4991 db_prio.db_id = strtoul(argv[0], NULL, 0);
4992 db_prio.priority = strtoul(argv[1], NULL, 0);
4994 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4996 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5006 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5008 uint32_t db_id, priority;
5015 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5019 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5021 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5025 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5031 set the sticky records capability for a database
5033 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5035 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5043 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5047 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5049 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5050 talloc_free(tmp_ctx);
5054 talloc_free(tmp_ctx);
5059 set the readonly capability for a database
5061 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5063 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5071 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5075 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5077 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5078 talloc_free(tmp_ctx);
5082 talloc_free(tmp_ctx);
5089 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5099 if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5103 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5105 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5109 printf("Sequence number:%lld\n", (long long)seqnum);
5117 static int control_setdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5120 struct ctdb_db_context *ctdb_db;
5123 uint64_t old_seqnum, new_seqnum;
5124 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5125 struct ctdb_transaction_handle *h;
5130 talloc_free(tmp_ctx);
5134 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5135 talloc_free(tmp_ctx);
5139 persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5141 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", argv[0]));
5142 talloc_free(tmp_ctx);
5146 ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &old_seqnum);
5148 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5149 talloc_free(tmp_ctx);
5153 new_seqnum = strtoull(argv[1], NULL, 0);
5154 if (new_seqnum <= old_seqnum) {
5155 DEBUG(DEBUG_ERR, ("New sequence number is less than current sequence number\n"));
5156 talloc_free(tmp_ctx);
5160 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], persistent, 0);
5161 if (ctdb_db == NULL) {
5162 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5163 talloc_free(tmp_ctx);
5167 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
5169 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", argv[0]));
5170 talloc_free(tmp_ctx);
5174 key.dptr = (uint8_t *)discard_const(CTDB_DB_SEQNUM_KEY);
5175 key.dsize = strlen(CTDB_DB_SEQNUM_KEY) + 1;
5177 data.dsize = sizeof(new_seqnum);
5178 data.dptr = talloc_size(tmp_ctx, data.dsize);
5179 *data.dptr = new_seqnum;
5181 ret = ctdb_transaction_store(h, key, data);
5183 DEBUG(DEBUG_ERR,("Failed to store record\n"));
5184 talloc_free(tmp_ctx);
5188 ret = ctdb_transaction_commit(h);
5190 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
5191 talloc_free(tmp_ctx);
5195 talloc_free(tmp_ctx);
5200 run an eventscript on a node
5202 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5208 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5211 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5215 data.dptr = (unsigned char *)discard_const(argv[0]);
5216 data.dsize = strlen((char *)data.dptr) + 1;
5218 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5220 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5221 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5222 if (ret != 0 || res != 0) {
5223 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5224 talloc_free(tmp_ctx);
5227 talloc_free(tmp_ctx);
5231 #define DB_VERSION 1
5232 #define MAX_DB_NAME 64
5233 struct db_file_header {
5234 unsigned long version;
5236 unsigned long persistent;
5238 const char name[MAX_DB_NAME];
5241 struct backup_data {
5242 struct ctdb_marshall_buffer *records;
5245 bool traverse_error;
5248 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5250 struct backup_data *bd = talloc_get_type(private, struct backup_data);
5251 struct ctdb_rec_data *rec;
5253 /* add the record */
5254 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5256 bd->traverse_error = true;
5257 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5260 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5261 if (bd->records == NULL) {
5262 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5263 bd->traverse_error = true;
5266 bd->records->count++;
5267 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5268 bd->len += rec->length;
5276 * backup a database to a file
5278 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5281 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5282 struct db_file_header dbhdr;
5283 struct ctdb_db_context *ctdb_db;
5284 struct backup_data *bd;
5287 const char *reason = NULL;
5291 assert_single_node_only();
5294 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5298 if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5302 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5303 db_id, tmp_ctx, &reason);
5305 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5307 talloc_free(tmp_ctx);
5311 uint32_t allow_unhealthy = 0;
5313 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5314 "AllowUnhealthyDBRead",
5317 if (allow_unhealthy != 1) {
5318 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5321 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5323 talloc_free(tmp_ctx);
5327 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5329 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5330 "tunnable AllowUnhealthyDBRead = %u\n",
5334 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5335 if (ctdb_db == NULL) {
5336 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5337 talloc_free(tmp_ctx);
5342 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5344 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5345 talloc_free(tmp_ctx);
5350 bd = talloc_zero(tmp_ctx, struct backup_data);
5352 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5353 talloc_free(tmp_ctx);
5357 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5358 if (bd->records == NULL) {
5359 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5360 talloc_free(tmp_ctx);
5364 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5365 bd->records->db_id = ctdb_db->db_id;
5366 /* traverse the database collecting all records */
5367 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5368 bd->traverse_error) {
5369 DEBUG(DEBUG_ERR,("Traverse error\n"));
5370 talloc_free(tmp_ctx);
5374 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5377 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5379 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5380 talloc_free(tmp_ctx);
5384 dbhdr.version = DB_VERSION;
5385 dbhdr.timestamp = time(NULL);
5386 dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5387 dbhdr.size = bd->len;
5388 if (strlen(argv[0]) >= MAX_DB_NAME) {
5389 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5392 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
5393 ret = write(fh, &dbhdr, sizeof(dbhdr));
5395 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5398 ret = write(fh, bd->records, bd->len);
5400 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5409 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5413 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5415 talloc_free(tmp_ctx);
5420 * restore a database from a file
5422 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5425 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5428 struct db_file_header dbhdr;
5429 struct ctdb_db_context *ctdb_db;
5430 struct ctdb_node_map *nodemap=NULL;
5431 struct ctdb_vnn_map *vnnmap=NULL;
5433 struct ctdb_control_wipe_database w;
5435 uint32_t generation;
5440 assert_single_node_only();
5442 if (argc < 1 || argc > 2) {
5443 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5447 fh = open(argv[0], O_RDONLY);
5449 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5450 talloc_free(tmp_ctx);
5454 read(fh, &dbhdr, sizeof(dbhdr));
5455 if (dbhdr.version != DB_VERSION) {
5456 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5457 talloc_free(tmp_ctx);
5461 dbname = discard_const(dbhdr.name);
5463 dbname = discard_const(argv[1]);
5466 outdata.dsize = dbhdr.size;
5467 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5468 if (outdata.dptr == NULL) {
5469 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5471 talloc_free(tmp_ctx);
5474 read(fh, outdata.dptr, outdata.dsize);
5477 tm = localtime(&dbhdr.timestamp);
5478 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5479 printf("Restoring database '%s' from backup @ %s\n",
5483 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5484 if (ctdb_db == NULL) {
5485 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5486 talloc_free(tmp_ctx);
5490 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5492 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5493 talloc_free(tmp_ctx);
5498 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5500 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5501 talloc_free(tmp_ctx);
5505 /* freeze all nodes */
5506 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5507 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5508 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5514 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5515 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5516 talloc_free(tmp_ctx);
5521 generation = vnnmap->generation;
5522 data.dptr = (void *)&generation;
5523 data.dsize = sizeof(generation);
5525 /* start a cluster wide transaction */
5526 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5527 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5529 TIMELIMIT(), false, data,
5532 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5537 w.db_id = ctdb_db->db_id;
5538 w.transaction_id = generation;
5540 data.dptr = (void *)&w;
5541 data.dsize = sizeof(w);
5543 /* wipe all the remote databases. */
5544 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5545 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5547 TIMELIMIT(), false, data,
5550 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5551 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5552 talloc_free(tmp_ctx);
5556 /* push the database */
5557 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5558 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5560 TIMELIMIT(), false, outdata,
5563 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5564 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5565 talloc_free(tmp_ctx);
5569 data.dptr = (void *)&ctdb_db->db_id;
5570 data.dsize = sizeof(ctdb_db->db_id);
5572 /* mark the database as healthy */
5573 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5574 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5576 TIMELIMIT(), false, data,
5579 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5580 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5581 talloc_free(tmp_ctx);
5585 data.dptr = (void *)&generation;
5586 data.dsize = sizeof(generation);
5588 /* commit all the changes */
5589 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5591 TIMELIMIT(), false, data,
5594 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5595 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5596 talloc_free(tmp_ctx);
5601 /* thaw all nodes */
5602 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5603 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5609 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5610 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5611 talloc_free(tmp_ctx);
5616 talloc_free(tmp_ctx);
5621 * dump a database backup from a file
5623 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5625 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5627 struct db_file_header dbhdr;
5631 struct ctdb_rec_data *rec = NULL;
5632 struct ctdb_marshall_buffer *m;
5633 struct ctdb_dump_db_context c;
5635 assert_single_node_only();
5638 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5642 fh = open(argv[0], O_RDONLY);
5644 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5645 talloc_free(tmp_ctx);
5649 read(fh, &dbhdr, sizeof(dbhdr));
5650 if (dbhdr.version != DB_VERSION) {
5651 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5652 talloc_free(tmp_ctx);
5656 outdata.dsize = dbhdr.size;
5657 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5658 if (outdata.dptr == NULL) {
5659 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5661 talloc_free(tmp_ctx);
5664 read(fh, outdata.dptr, outdata.dsize);
5666 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5668 tm = localtime(&dbhdr.timestamp);
5669 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5670 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5671 dbhdr.name, m->db_id, tbuf);
5675 c.printemptyrecords = (bool)options.printemptyrecords;
5676 c.printdatasize = (bool)options.printdatasize;
5677 c.printlmaster = false;
5678 c.printhash = (bool)options.printhash;
5679 c.printrecordflags = (bool)options.printrecordflags;
5681 for (i=0; i < m->count; i++) {
5685 /* we do not want the header splitted, so we pass NULL*/
5686 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5689 ctdb_dumpdb_record(ctdb, key, data, &c);
5692 printf("Dumped %d records\n", i);
5693 talloc_free(tmp_ctx);
5698 * wipe a database from a file
5700 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5704 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5706 struct ctdb_db_context *ctdb_db;
5707 struct ctdb_node_map *nodemap = NULL;
5708 struct ctdb_vnn_map *vnnmap = NULL;
5710 struct ctdb_control_wipe_database w;
5712 uint32_t generation;
5715 assert_single_node_only();
5718 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5722 if (!db_exists(ctdb, argv[0], NULL, &flags)) {
5726 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5727 if (ctdb_db == NULL) {
5728 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5730 talloc_free(tmp_ctx);
5734 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5737 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5739 talloc_free(tmp_ctx);
5743 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5746 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5748 talloc_free(tmp_ctx);
5752 /* freeze all nodes */
5753 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5754 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5755 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5762 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5763 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5764 CTDB_RECOVERY_ACTIVE);
5765 talloc_free(tmp_ctx);
5770 generation = vnnmap->generation;
5771 data.dptr = (void *)&generation;
5772 data.dsize = sizeof(generation);
5774 /* start a cluster wide transaction */
5775 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5776 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5778 TIMELIMIT(), false, data,
5782 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5783 "transactions.\n"));
5787 w.db_id = ctdb_db->db_id;
5788 w.transaction_id = generation;
5790 data.dptr = (void *)&w;
5791 data.dsize = sizeof(w);
5793 /* wipe all the remote databases. */
5794 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5795 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5797 TIMELIMIT(), false, data,
5800 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5801 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5802 talloc_free(tmp_ctx);
5806 data.dptr = (void *)&ctdb_db->db_id;
5807 data.dsize = sizeof(ctdb_db->db_id);
5809 /* mark the database as healthy */
5810 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5811 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5813 TIMELIMIT(), false, data,
5816 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5817 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5818 talloc_free(tmp_ctx);
5822 data.dptr = (void *)&generation;
5823 data.dsize = sizeof(generation);
5825 /* commit all the changes */
5826 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5828 TIMELIMIT(), false, data,
5831 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5832 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5833 talloc_free(tmp_ctx);
5837 /* thaw all nodes */
5838 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5839 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5845 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5846 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5847 talloc_free(tmp_ctx);
5851 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5853 talloc_free(tmp_ctx);
5860 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5866 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5867 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5868 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5869 if (ret != 0 || res != 0) {
5870 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5871 talloc_free(tmp_ctx);
5874 write(1, data.dptr, data.dsize);
5875 talloc_free(tmp_ctx);
5880 handler for memory dumps
5882 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5883 TDB_DATA data, void *private_data)
5885 write(1, data.dptr, data.dsize);
5890 dump memory usage on the recovery daemon
5892 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5896 struct srvid_request rd;
5898 rd.pnn = ctdb_get_pnn(ctdb);
5899 rd.srvid = getpid();
5901 /* register a message port for receiveing the reply so that we
5902 can receive the reply
5904 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5907 data.dptr = (uint8_t *)&rd;
5908 data.dsize = sizeof(rd);
5910 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5912 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5916 /* this loop will terminate when we have received the reply */
5918 event_loop_once(ctdb->ev);
5925 send a message to a srvid
5927 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5929 unsigned long srvid;
5937 srvid = strtoul(argv[0], NULL, 0);
5939 data.dptr = (uint8_t *)discard_const(argv[1]);
5940 data.dsize= strlen(argv[1]);
5942 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5944 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5952 handler for msglisten
5954 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5955 TDB_DATA data, void *private_data)
5959 printf("Message received: ");
5960 for (i=0;i<data.dsize;i++) {
5961 printf("%c", data.dptr[i]);
5967 listen for messages on a messageport
5969 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5975 /* register a message port and listen for messages
5977 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5978 printf("Listening for messages on srvid:%d\n", (int)srvid);
5981 event_loop_once(ctdb->ev);
5988 list all nodes in the cluster
5989 we parse the nodes file directly
5991 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5993 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5994 struct pnn_node *pnn_nodes;
5995 struct pnn_node *pnn_node;
5997 assert_single_node_only();
5999 pnn_nodes = read_nodes_file(mem_ctx);
6000 if (pnn_nodes == NULL) {
6001 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
6002 talloc_free(mem_ctx);
6006 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
6007 ctdb_sock_addr addr;
6008 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
6009 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
6010 talloc_free(mem_ctx);
6013 if (options.machinereadable){
6014 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
6016 printf("%s\n", pnn_node->addr);
6019 talloc_free(mem_ctx);
6025 reload the nodes file on the local node
6027 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
6031 struct ctdb_node_map *nodemap=NULL;
6033 assert_single_node_only();
6035 mypnn = ctdb_get_pnn(ctdb);
6037 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
6039 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
6043 /* reload the nodes file on all remote nodes */
6044 for (i=0;i<nodemap->num;i++) {
6045 if (nodemap->nodes[i].pnn == mypnn) {
6048 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
6049 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
6050 nodemap->nodes[i].pnn);
6052 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
6056 /* reload the nodes file on the local node */
6057 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
6058 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
6060 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
6063 /* initiate a recovery */
6064 control_recover(ctdb, argc, argv);
6070 static const struct {
6072 int (*fn)(struct ctdb_context *, int, const char **);
6074 bool without_daemon; /* can be run without daemon running ? */
6077 } ctdb_commands[] = {
6078 { "version", control_version, true, true, "show version of ctdb" },
6079 { "status", control_status, true, false, "show node status" },
6080 { "uptime", control_uptime, true, false, "show node uptime" },
6081 { "ping", control_ping, true, false, "ping all nodes" },
6082 { "runstate", control_runstate, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6083 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
6084 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
6085 { "listvars", control_listvars, true, false, "list tunable variables"},
6086 { "statistics", control_statistics, false, false, "show statistics" },
6087 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
6088 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
6089 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
6090 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6091 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
6092 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
6093 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
6094 { "getdbmap", control_getdbmap, true, false, "show the database map" },
6095 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname|dbid>" },
6096 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6097 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6098 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
6099 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
6100 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
6101 { "lvs", control_lvs, true, false, "show lvs configuration" },
6102 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
6103 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
6104 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
6105 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6106 { "getdebug", control_getdebug, true, false, "get debug level" },
6107 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6108 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6109 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
6110 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
6111 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
6112 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
6113 { "disable", control_disable, true, false, "disable a nodes public IP" },
6114 { "enable", control_enable, true, false, "enable a nodes public IP" },
6115 { "stop", control_stop, true, false, "stop a node" },
6116 { "continue", control_continue, true, false, "re-start a stopped node" },
6117 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
6118 { "unban", control_unban, true, false, "unban a node" },
6119 { "showban", control_showban, true, false, "show ban information"},
6120 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
6121 { "recover", control_recover, true, false, "force recovery" },
6122 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
6123 { "ipreallocate", control_ipreallocate, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6124 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
6125 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
6126 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6127 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
6128 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6129 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6130 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6132 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6134 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
6135 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
6136 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6137 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
6138 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
6139 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
6140 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
6141 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
6142 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6143 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6144 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6145 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
6146 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6147 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6148 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
6149 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
6150 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6151 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
6152 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6153 { "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
6154 { "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
6155 { "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
6156 { "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6157 { "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
6158 { "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},
6159 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
6160 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
6161 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6162 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6163 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbname|dbid>"},
6164 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6165 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6166 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6167 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
6168 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6169 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6170 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6171 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6172 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
6173 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
6174 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
6175 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6176 { "rebalancenode", control_rebalancenode, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6177 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6178 { "setdbseqnum", control_setdbseqnum, false, false, "set the sequence number for a database", "<dbname|dbid> <seqnum>" },
6179 { "nodestatus", control_nodestatus, true, false, "show and return node status", "[<pnn-list>]" },
6180 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<dbname|dbid>" },
6181 { "reloadips", control_reloadips, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6182 { "ipiface", control_ipiface, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6188 static void usage(void)
6192 "Usage: ctdb [options] <control>\n" \
6194 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6195 " -Y generate machinereadable output\n"
6196 " -v generate verbose output\n"
6197 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
6198 printf("Controls:\n");
6199 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6200 printf(" %-15s %-27s %s\n",
6201 ctdb_commands[i].name,
6202 ctdb_commands[i].args?ctdb_commands[i].args:"",
6203 ctdb_commands[i].msg);
6209 static void ctdb_alarm(int sig)
6211 printf("Maximum runtime exceeded - exiting\n");
6218 int main(int argc, const char *argv[])
6220 struct ctdb_context *ctdb;
6221 char *nodestring = NULL;
6222 struct poptOption popt_options[] = {
6225 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6226 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6227 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6228 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6229 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6230 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6231 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6232 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6233 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6234 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6238 const char **extra_argv;
6242 struct event_context *ev;
6243 const char *control;
6247 /* set some defaults */
6248 options.maxruntime = 0;
6249 options.timelimit = 10;
6250 options.pnn = CTDB_CURRENT_NODE;
6252 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6254 while ((opt = poptGetNextOpt(pc)) != -1) {
6257 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6258 poptBadOption(pc, 0), poptStrerror(opt)));
6263 /* setup the remaining options for the main program to use */
6264 extra_argv = poptGetArgs(pc);
6267 while (extra_argv[extra_argc]) extra_argc++;
6270 if (extra_argc < 1) {
6274 if (options.maxruntime == 0) {
6275 const char *ctdb_timeout;
6276 ctdb_timeout = getenv("CTDB_TIMEOUT");
6277 if (ctdb_timeout != NULL) {
6278 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6280 /* default timeout is 120 seconds */
6281 options.maxruntime = 120;
6285 signal(SIGALRM, ctdb_alarm);
6286 alarm(options.maxruntime);
6288 control = extra_argv[0];
6290 ev = event_context_init(NULL);
6292 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6296 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6297 if (strcmp(control, ctdb_commands[i].name) == 0) {
6302 if (i == ARRAY_SIZE(ctdb_commands)) {
6303 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6307 if (ctdb_commands[i].without_daemon == true) {
6308 if (nodestring != NULL) {
6309 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6313 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6316 /* initialise ctdb */
6317 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6320 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6324 /* setup the node number(s) to contact */
6325 if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6326 &options.nodes, &options.pnn)) {
6330 if (options.pnn == CTDB_CURRENT_NODE) {
6331 options.pnn = options.nodes[0];
6334 if (ctdb_commands[i].auto_all &&
6335 ((options.pnn == CTDB_BROADCAST_ALL) ||
6336 (options.pnn == CTDB_MULTICAST))) {
6340 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6341 options.pnn = options.nodes[j];
6342 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6345 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6350 (void)poptFreeContext(pc);