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/version.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 struct ctdb_connection *ctdb_connection;
41 static void usage(void);
50 int printemptyrecords;
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
60 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
62 printf("CTDB version: %s\n", CTDB_VERSION_STRING);
66 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
67 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
68 "Out of memory in " __location__ )); \
72 /* Pretty print the flags to a static buffer in human-readable format.
73 * This never returns NULL!
75 static const char *pretty_print_flags(uint32_t flags)
82 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
83 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
84 { NODE_FLAGS_BANNED, "BANNED" },
85 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
86 { NODE_FLAGS_DELETED, "DELETED" },
87 { NODE_FLAGS_STOPPED, "STOPPED" },
88 { NODE_FLAGS_INACTIVE, "INACTIVE" },
90 static char flags_str[512]; /* Big enough to contain all flag names */
93 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
94 if (flags & flag_names[j].flag) {
95 if (flags_str[0] == '\0') {
96 (void) strcpy(flags_str, flag_names[j].name);
98 (void) strcat(flags_str, "|");
99 (void) strcat(flags_str, flag_names[j].name);
103 if (flags_str[0] == '\0') {
104 (void) strcpy(flags_str, "OK");
110 static int h2i(char h)
112 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
113 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
117 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
120 TDB_DATA key = {NULL, 0};
124 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
129 key.dptr = talloc_size(mem_ctx, key.dsize);
131 for (i=0; i < len/2; i++) {
132 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
137 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
138 * that are disconnected or deleted. If dd_ok is true those nodes are
139 * included in the output list of nodes. If dd_ok is false, those
140 * nodes are filtered from the "all" case and cause an error if
141 * explicitly specified.
143 static bool parse_nodestring(struct ctdb_context *ctdb,
144 const char * nodestring,
145 uint32_t current_pnn,
152 struct ctdb_node_map *nodemap;
156 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
157 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
161 if (nodestring != NULL) {
162 *nodes = talloc_array(ctdb, uint32_t, 0);
163 CTDB_NOMEM_ABORT(*nodes);
167 if (strcmp(nodestring, "all") == 0) {
168 *pnn_mode = CTDB_BROADCAST_ALL;
171 for (i = 0; i < nodemap->num; i++) {
172 if ((nodemap->nodes[i].flags &
173 (NODE_FLAGS_DISCONNECTED |
174 NODE_FLAGS_DELETED)) && !dd_ok) {
177 *nodes = talloc_realloc(ctdb, *nodes,
179 CTDB_NOMEM_ABORT(*nodes);
187 ns = talloc_strdup(ctdb, nodestring);
188 tok = strtok(ns, ",");
189 while (tok != NULL) {
191 i = (uint32_t)strtoul(tok, NULL, 0);
192 if (i >= nodemap->num) {
193 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
196 if ((nodemap->nodes[i].flags &
197 (NODE_FLAGS_DISCONNECTED |
198 NODE_FLAGS_DELETED)) && !dd_ok) {
199 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
202 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
203 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
207 *nodes = talloc_realloc(ctdb, *nodes,
209 CTDB_NOMEM_ABORT(*nodes);
214 tok = strtok(NULL, ",");
219 *pnn_mode = (*nodes)[0];
221 *pnn_mode = CTDB_MULTICAST;
225 /* default - no nodes specified */
226 *nodes = talloc_array(ctdb, uint32_t, 1);
227 CTDB_NOMEM_ABORT(*nodes);
228 *pnn_mode = CTDB_CURRENT_NODE;
230 if (!ctdb_getpnn(ctdb_connection, current_pnn,
236 ctdb_free_nodemap(nodemap);
242 check if a database exists
244 static int db_exists(struct ctdb_context *ctdb, const char *db_name, bool *persistent)
247 struct ctdb_dbid_map *dbmap=NULL;
249 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
251 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
255 for(i=0;i<dbmap->num;i++){
258 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
259 if (!strcmp(name, db_name)) {
261 *persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
271 see if a process exists
273 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
281 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
282 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
286 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
288 printf("%u:%u exists\n", pnn, pid);
290 printf("%u:%u does not exist\n", pnn, pid);
296 display statistics structure
298 static void show_statistics(struct ctdb_statistics *s, int show_header)
300 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
302 const char *prefix=NULL;
304 int tmp, days, hours, minutes, seconds;
309 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
310 STATISTICS_FIELD(num_clients),
311 STATISTICS_FIELD(frozen),
312 STATISTICS_FIELD(recovering),
313 STATISTICS_FIELD(num_recoveries),
314 STATISTICS_FIELD(client_packets_sent),
315 STATISTICS_FIELD(client_packets_recv),
316 STATISTICS_FIELD(node_packets_sent),
317 STATISTICS_FIELD(node_packets_recv),
318 STATISTICS_FIELD(keepalive_packets_sent),
319 STATISTICS_FIELD(keepalive_packets_recv),
320 STATISTICS_FIELD(node.req_call),
321 STATISTICS_FIELD(node.reply_call),
322 STATISTICS_FIELD(node.req_dmaster),
323 STATISTICS_FIELD(node.reply_dmaster),
324 STATISTICS_FIELD(node.reply_error),
325 STATISTICS_FIELD(node.req_message),
326 STATISTICS_FIELD(node.req_control),
327 STATISTICS_FIELD(node.reply_control),
328 STATISTICS_FIELD(client.req_call),
329 STATISTICS_FIELD(client.req_message),
330 STATISTICS_FIELD(client.req_control),
331 STATISTICS_FIELD(timeouts.call),
332 STATISTICS_FIELD(timeouts.control),
333 STATISTICS_FIELD(timeouts.traverse),
334 STATISTICS_FIELD(locks.num_calls),
335 STATISTICS_FIELD(locks.num_current),
336 STATISTICS_FIELD(locks.num_pending),
337 STATISTICS_FIELD(locks.num_failed),
338 STATISTICS_FIELD(total_calls),
339 STATISTICS_FIELD(pending_calls),
340 STATISTICS_FIELD(childwrite_calls),
341 STATISTICS_FIELD(pending_childwrite_calls),
342 STATISTICS_FIELD(memory_used),
343 STATISTICS_FIELD(max_hop_count),
344 STATISTICS_FIELD(total_ro_delegations),
345 STATISTICS_FIELD(total_ro_revokes),
348 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
357 if (options.machinereadable){
359 printf("CTDB version:");
360 printf("Current time of statistics:");
361 printf("Statistics collected since:");
362 for (i=0;i<ARRAY_SIZE(fields);i++) {
363 printf("%s:", fields[i].name);
365 printf("num_reclock_ctdbd_latency:");
366 printf("min_reclock_ctdbd_latency:");
367 printf("avg_reclock_ctdbd_latency:");
368 printf("max_reclock_ctdbd_latency:");
370 printf("num_reclock_recd_latency:");
371 printf("min_reclock_recd_latency:");
372 printf("avg_reclock_recd_latency:");
373 printf("max_reclock_recd_latency:");
375 printf("num_call_latency:");
376 printf("min_call_latency:");
377 printf("avg_call_latency:");
378 printf("max_call_latency:");
380 printf("num_lockwait_latency:");
381 printf("min_lockwait_latency:");
382 printf("avg_lockwait_latency:");
383 printf("max_lockwait_latency:");
385 printf("num_childwrite_latency:");
386 printf("min_childwrite_latency:");
387 printf("avg_childwrite_latency:");
388 printf("max_childwrite_latency:");
391 printf("%d:", CTDB_VERSION);
392 printf("%d:", (int)s->statistics_current_time.tv_sec);
393 printf("%d:", (int)s->statistics_start_time.tv_sec);
394 for (i=0;i<ARRAY_SIZE(fields);i++) {
395 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
397 printf("%d:", s->reclock.ctdbd.num);
398 printf("%.6f:", s->reclock.ctdbd.min);
399 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
400 printf("%.6f:", s->reclock.ctdbd.max);
402 printf("%d:", s->reclock.recd.num);
403 printf("%.6f:", s->reclock.recd.min);
404 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
405 printf("%.6f:", s->reclock.recd.max);
407 printf("%d:", s->call_latency.num);
408 printf("%.6f:", s->call_latency.min);
409 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
410 printf("%.6f:", s->call_latency.max);
412 printf("%d:", s->childwrite_latency.num);
413 printf("%.6f:", s->childwrite_latency.min);
414 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
415 printf("%.6f:", s->childwrite_latency.max);
418 printf("CTDB version %u\n", CTDB_VERSION);
419 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
420 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
422 for (i=0;i<ARRAY_SIZE(fields);i++) {
423 if (strchr(fields[i].name, '.')) {
424 preflen = strcspn(fields[i].name, ".")+1;
425 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
426 prefix = fields[i].name;
427 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
432 printf(" %*s%-22s%*s%10u\n",
434 fields[i].name+preflen,
436 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
438 printf(" hop_count_buckets:");
439 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
440 printf(" %d", s->hop_count_bucket[i]);
443 printf(" lock_buckets:");
444 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
445 printf(" %d", s->locks.buckets[i]);
448 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);
450 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);
452 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);
454 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);
455 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);
458 talloc_free(tmp_ctx);
462 display remote ctdb statistics combined from all nodes
464 static int control_statistics_all(struct ctdb_context *ctdb)
467 struct ctdb_statistics statistics;
471 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
472 CTDB_NO_MEMORY(ctdb, nodes);
474 ZERO_STRUCT(statistics);
476 for (i=0;i<num_nodes;i++) {
477 struct ctdb_statistics s1;
479 uint32_t *v1 = (uint32_t *)&s1;
480 uint32_t *v2 = (uint32_t *)&statistics;
482 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
483 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
485 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
488 for (j=0;j<num_ints;j++) {
491 statistics.max_hop_count =
492 MAX(statistics.max_hop_count, s1.max_hop_count);
493 statistics.call_latency.max =
494 MAX(statistics.call_latency.max, s1.call_latency.max);
497 printf("Gathered statistics for %u nodes\n", num_nodes);
498 show_statistics(&statistics, 1);
503 display remote ctdb statistics
505 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
508 struct ctdb_statistics statistics;
510 if (options.pnn == CTDB_BROADCAST_ALL) {
511 return control_statistics_all(ctdb);
514 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
516 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
519 show_statistics(&statistics, 1);
525 reset remote ctdb statistics
527 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
531 ret = ctdb_statistics_reset(ctdb, options.pnn);
533 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
541 display remote ctdb rolling statistics
543 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
546 struct ctdb_statistics_wire *stats;
547 int i, num_records = -1;
550 num_records = atoi(argv[0]) - 1;
553 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
555 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
558 for (i=0;i<stats->num;i++) {
559 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
562 show_statistics(&stats->stats[i], i==0);
563 if (i == num_records) {
572 display remote ctdb db statistics
574 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
576 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
577 struct ctdb_db_statistics *dbstat;
578 struct ctdb_dbid_map *dbmap=NULL;
585 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
587 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
590 for(i=0;i<dbmap->num;i++){
593 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
594 if(!strcmp(argv[0], name)){
595 talloc_free(discard_const(name));
598 talloc_free(discard_const(name));
600 if (i == dbmap->num) {
601 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
602 talloc_free(tmp_ctx);
606 if (!ctdb_getdbstat(ctdb_connection, options.pnn, dbmap->dbs[i].dbid, &dbstat)) {
607 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
608 talloc_free(tmp_ctx);
612 printf("DB Statistics:\n");
613 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
614 dbstat->db_ro_delegations);
615 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
616 dbstat->db_ro_delegations);
617 printf(" %s\n", "locks");
618 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
619 dbstat->locks.num_calls);
620 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
621 dbstat->locks.num_failed);
622 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
623 dbstat->locks.num_current);
624 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
625 dbstat->locks.num_pending);
626 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
627 " latency_ctdbd MIN/AVG/MAX",
628 dbstat->locks.latency.min,
629 (dbstat->locks.latency.num ?
630 dbstat->locks.latency.total /dbstat->locks.latency.num :
632 dbstat->locks.latency.max,
633 dbstat->locks.latency.num);
634 printf(" %s", " buckets:");
635 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
636 printf(" %d", dbstat->hop_count_bucket[i]);
639 printf("Num Hot Keys: %d\n", dbstat->num_hot_keys);
640 for (i = 0; i < dbstat->num_hot_keys; i++) {
642 printf("Count:%d Key:", dbstat->hot_keys[i].count);
643 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
644 printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
649 ctdb_free_dbstat(dbstat);
654 display uptime of remote node
656 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
659 struct ctdb_uptime *uptime = NULL;
660 int tmp, days, hours, minutes, seconds;
662 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
664 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
668 if (options.machinereadable){
669 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
670 printf(":%u:%u:%u:%lf\n",
671 (unsigned int)uptime->current_time.tv_sec,
672 (unsigned int)uptime->ctdbd_start_time.tv_sec,
673 (unsigned int)uptime->last_recovery_finished.tv_sec,
674 timeval_delta(&uptime->last_recovery_finished,
675 &uptime->last_recovery_started)
680 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
682 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
690 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
692 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
700 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
702 printf("Duration of last recovery/failover: %lf seconds\n",
703 timeval_delta(&uptime->last_recovery_finished,
704 &uptime->last_recovery_started));
710 show the PNN of the current node
712 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
717 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
719 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
723 printf("PNN:%d\n", mypnn);
729 struct pnn_node *next;
734 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
736 const char *nodes_list;
740 struct pnn_node *pnn_nodes = NULL;
741 struct pnn_node *pnn_node;
742 struct pnn_node *tmp_node;
744 /* read the nodes file */
745 nodes_list = getenv("CTDB_NODES");
746 if (nodes_list == NULL) {
747 nodes_list = "/etc/ctdb/nodes";
749 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
753 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
756 for (i=0, pnn=0; i<nlines; i++) {
760 /* strip leading spaces */
761 while((*node == ' ') || (*node == '\t')) {
768 if (strcmp(node, "") == 0) {
771 pnn_node = talloc(mem_ctx, struct pnn_node);
772 pnn_node->pnn = pnn++;
773 pnn_node->addr = talloc_strdup(pnn_node, node);
774 pnn_node->next = pnn_nodes;
775 pnn_nodes = pnn_node;
778 /* swap them around so we return them in incrementing order */
779 pnn_node = pnn_nodes;
783 pnn_node = pnn_node->next;
785 tmp_node->next = pnn_nodes;
786 pnn_nodes = tmp_node;
793 show the PNN of the current node
794 discover the pnn by loading the nodes file and try to bind to all
795 addresses one at a time until the ip address is found.
797 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
799 TALLOC_CTX *mem_ctx = talloc_new(NULL);
800 struct pnn_node *pnn_nodes;
801 struct pnn_node *pnn_node;
803 pnn_nodes = read_nodes_file(mem_ctx);
804 if (pnn_nodes == NULL) {
805 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
806 talloc_free(mem_ctx);
810 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
813 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
814 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
815 talloc_free(mem_ctx);
819 if (ctdb_sys_have_ip(&addr)) {
820 printf("PNN:%d\n", pnn_node->pnn);
821 talloc_free(mem_ctx);
826 printf("Failed to detect which PNN this node is\n");
827 talloc_free(mem_ctx);
831 /* Helpers for ctdb status
833 static bool is_partially_online(struct ctdb_node_and_flags *node)
838 if (node->flags == 0) {
839 struct ctdb_ifaces_list *ifaces;
841 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
842 for (j=0; j < ifaces->num; j++) {
843 if (ifaces->ifaces[j].link_state != 0) {
849 ctdb_free_ifaces(ifaces);
856 static void control_status_header_machine(void)
858 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
859 ":Inactive:PartiallyOnline:ThisNode:\n");
862 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
864 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
865 ctdb_addr_to_str(&node->addr),
866 !!(node->flags&NODE_FLAGS_DISCONNECTED),
867 !!(node->flags&NODE_FLAGS_BANNED),
868 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
869 !!(node->flags&NODE_FLAGS_UNHEALTHY),
870 !!(node->flags&NODE_FLAGS_STOPPED),
871 !!(node->flags&NODE_FLAGS_INACTIVE),
872 is_partially_online(node) ? 1 : 0,
873 (node->pnn == mypnn)?'Y':'N');
878 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
880 printf("pnn:%d %-16s %s%s\n", node->pnn,
881 ctdb_addr_to_str(&node->addr),
882 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
883 node->pnn == mypnn?" (THIS NODE)":"");
889 display remote ctdb status
891 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
894 struct ctdb_vnn_map *vnnmap=NULL;
895 struct ctdb_node_map *nodemap=NULL;
896 uint32_t recmode, recmaster, mypnn;
898 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
902 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
903 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
907 if (options.machinereadable) {
908 control_status_header_machine();
909 for (i=0;i<nodemap->num;i++) {
910 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
913 (void) control_status_1_machine(mypnn,
919 printf("Number of nodes:%d\n", nodemap->num);
920 for(i=0;i<nodemap->num;i++){
921 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
924 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
927 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
928 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
931 if (vnnmap->generation == INVALID_GENERATION) {
932 printf("Generation:INVALID\n");
934 printf("Generation:%d\n",vnnmap->generation);
936 printf("Size:%d\n",vnnmap->size);
937 for(i=0;i<vnnmap->size;i++){
938 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
940 ctdb_free_vnnmap(vnnmap);
942 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
943 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
946 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
948 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
949 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
952 printf("Recovery master:%d\n",recmaster);
957 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
960 struct ctdb_node_map *nodemap=NULL;
962 uint32_t pnn_mode, mypnn;
968 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
969 options.pnn, true, &nodes, &pnn_mode)) {
973 if (options.machinereadable) {
974 control_status_header_machine();
975 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
976 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
979 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
980 DEBUG(DEBUG_ERR, ("Unable to get PNN from local node\n"));
984 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
985 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
991 for (i = 0; i < talloc_array_length(nodes); i++) {
992 if (options.machinereadable) {
993 ret |= control_status_1_machine(mypnn,
994 &nodemap->nodes[nodes[i]]);
996 ret |= control_status_1_human(mypnn,
997 &nodemap->nodes[nodes[i]]);
1004 struct natgw_node *next;
1008 static int find_natgw(struct ctdb_context *ctdb,
1009 struct ctdb_node_map *nodemap, uint32_t flags,
1010 uint32_t *pnn, const char **ip)
1013 uint32_t capabilities;
1015 for (i=0;i<nodemap->num;i++) {
1016 if (!(nodemap->nodes[i].flags & flags)) {
1017 if (!ctdb_getcapabilities(ctdb_connection, nodemap->nodes[i].pnn, &capabilities)) {
1018 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1021 if (!(capabilities&CTDB_CAP_NATGW)) {
1024 *pnn = nodemap->nodes[i].pnn;
1025 *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
1030 return 2; /* matches ENOENT */
1034 display the list of nodes belonging to this natgw configuration
1036 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1039 const char *natgw_list;
1042 struct natgw_node *natgw_nodes = NULL;
1043 struct natgw_node *natgw_node;
1044 struct ctdb_node_map *nodemap=NULL;
1045 uint32_t mypnn, pnn;
1048 /* When we have some nodes that could be the NATGW, make a
1049 * series of attempts to find the first node that doesn't have
1050 * certain status flags set.
1052 uint32_t exclude_flags[] = {
1053 /* Look for a nice healthy node */
1054 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1055 /* If not found, an UNHEALTHY/BANNED node will do */
1056 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1057 /* If not found, a STOPPED node will do */
1058 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1062 /* read the natgw nodes file into a linked list */
1063 natgw_list = getenv("CTDB_NATGW_NODES");
1064 if (natgw_list == NULL) {
1065 natgw_list = "/etc/ctdb/natgw_nodes";
1067 lines = file_lines_load(natgw_list, &nlines, ctdb);
1068 if (lines == NULL) {
1069 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1072 for (i=0;i<nlines;i++) {
1076 /* strip leading spaces */
1077 while((*node == ' ') || (*node == '\t')) {
1083 if (strcmp(node, "") == 0) {
1086 natgw_node = talloc(ctdb, struct natgw_node);
1087 natgw_node->addr = talloc_strdup(natgw_node, node);
1088 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1089 natgw_node->next = natgw_nodes;
1090 natgw_nodes = natgw_node;
1093 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
1094 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1098 /* Trim the nodemap so it only includes connected nodes in the
1099 * current natgw group.
1102 while(i<nodemap->num) {
1103 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1104 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1109 /* this node was not in the natgw so we just remove it from
1112 if ((natgw_node == NULL)
1113 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1116 for (j=i+1; j<nodemap->num; j++) {
1117 nodemap->nodes[j-1] = nodemap->nodes[j];
1126 ret = 2; /* matches ENOENT */
1129 for (i = 0; exclude_flags[i] != 0; i++) {
1130 ret = find_natgw(ctdb, nodemap,
1141 if (options.machinereadable) {
1142 printf(":Node:IP:\n");
1143 printf(":%d:%s:\n", pnn, ip);
1145 printf("%d %s\n", pnn, ip);
1148 /* print the pruned list of nodes belonging to this natgw list */
1149 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
1150 DEBUG(DEBUG_NOTICE, ("Unable to get PNN from node %u\n", options.pnn));
1151 /* This is actually harmless and will only result in
1152 * the "this node" indication being missing
1156 if (options.machinereadable) {
1157 control_status_header_machine();
1159 printf("Number of nodes:%d\n", nodemap->num);
1161 for(i=0;i<nodemap->num;i++){
1162 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1165 if (options.machinereadable) {
1166 control_status_1_machine(mypnn, &(nodemap->nodes[i]));
1168 control_status_1_human(mypnn, &(nodemap->nodes[i]));
1173 ctdb_free_nodemap(nodemap);
1178 display the status of the scripts for monitoring (or other events)
1180 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1181 enum ctdb_eventscript_call type)
1183 struct ctdb_scripts_wire *script_status;
1186 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1188 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1192 if (script_status == NULL) {
1193 if (!options.machinereadable) {
1194 printf("%s cycle never run\n",
1195 ctdb_eventscript_call_names[type]);
1200 if (!options.machinereadable) {
1201 printf("%d scripts were executed last %s cycle\n",
1202 script_status->num_scripts,
1203 ctdb_eventscript_call_names[type]);
1205 for (i=0; i<script_status->num_scripts; i++) {
1206 const char *status = NULL;
1208 switch (script_status->scripts[i].status) {
1210 status = "TIMEDOUT";
1213 status = "DISABLED";
1219 if (script_status->scripts[i].status > 0)
1223 if (options.machinereadable) {
1224 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1225 ctdb_eventscript_call_names[type],
1226 script_status->scripts[i].name,
1227 script_status->scripts[i].status,
1229 (long)script_status->scripts[i].start.tv_sec,
1230 (long)script_status->scripts[i].start.tv_usec,
1231 (long)script_status->scripts[i].finished.tv_sec,
1232 (long)script_status->scripts[i].finished.tv_usec,
1233 script_status->scripts[i].output);
1237 printf("%-20s Status:%s ",
1238 script_status->scripts[i].name, status);
1240 /* Some other error, eg from stat. */
1241 printf("%-20s Status:CANNOT RUN (%s)",
1242 script_status->scripts[i].name,
1243 strerror(-script_status->scripts[i].status));
1245 if (script_status->scripts[i].status >= 0) {
1246 printf("Duration:%.3lf ",
1247 timeval_delta(&script_status->scripts[i].finished,
1248 &script_status->scripts[i].start));
1250 if (script_status->scripts[i].status != -ENOEXEC) {
1252 ctime(&script_status->scripts[i].start.tv_sec));
1253 if (script_status->scripts[i].status != 0) {
1254 printf(" OUTPUT:%s\n",
1255 script_status->scripts[i].output);
1265 static int control_scriptstatus(struct ctdb_context *ctdb,
1266 int argc, const char **argv)
1269 enum ctdb_eventscript_call type, min, max;
1273 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1278 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1282 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1283 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1289 if (type == CTDB_EVENT_MAX) {
1290 if (strcmp(arg, "all") == 0) {
1292 max = CTDB_EVENT_MAX;
1294 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1299 if (options.machinereadable) {
1300 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1303 for (type = min; type < max; type++) {
1304 ret = control_one_scriptstatus(ctdb, type);
1314 enable an eventscript
1316 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1324 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1326 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1334 disable an eventscript
1336 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1344 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1346 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1354 display the pnn of the recovery master
1356 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1360 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1361 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1364 printf("%d\n",recmaster);
1370 add a tickle to a public address
1372 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1374 struct ctdb_tcp_connection t;
1382 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1383 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1386 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1387 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1391 data.dptr = (uint8_t *)&t;
1392 data.dsize = sizeof(t);
1394 /* tell all nodes about this tcp connection */
1395 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1396 0, data, ctdb, NULL, NULL, NULL, NULL);
1398 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1407 delete a tickle from a node
1409 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1411 struct ctdb_tcp_connection t;
1419 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1420 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1423 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1424 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1428 data.dptr = (uint8_t *)&t;
1429 data.dsize = sizeof(t);
1431 /* tell all nodes about this tcp connection */
1432 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1433 0, data, ctdb, NULL, NULL, NULL, NULL);
1435 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1444 get a list of all tickles for this pnn
1446 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1448 struct ctdb_control_tcp_tickle_list *list;
1449 ctdb_sock_addr addr;
1458 port = atoi(argv[1]);
1461 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1462 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1466 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1468 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1472 if (options.machinereadable){
1473 printf(":source ip:port:destination ip:port:\n");
1474 for (i=0;i<list->tickles.num;i++) {
1475 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1478 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1479 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1482 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1483 printf("Num tickles:%u\n", list->tickles.num);
1484 for (i=0;i<list->tickles.num;i++) {
1485 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1488 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1489 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));
1499 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1501 struct ctdb_all_public_ips *ips;
1502 struct ctdb_public_ip ip;
1505 uint32_t disable_time;
1507 struct ctdb_node_map *nodemap=NULL;
1508 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1511 data.dptr = (uint8_t*)&disable_time;
1512 data.dsize = sizeof(disable_time);
1513 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1515 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1521 /* read the public ip list from the node */
1522 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1524 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1525 talloc_free(tmp_ctx);
1529 for (i=0;i<ips->num;i++) {
1530 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1535 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1536 pnn, ctdb_addr_to_str(addr)));
1537 talloc_free(tmp_ctx);
1544 data.dptr = (uint8_t *)&ip;
1545 data.dsize = sizeof(ip);
1547 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1549 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1550 talloc_free(tmp_ctx);
1554 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1555 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1562 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1563 talloc_free(tmp_ctx);
1567 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1569 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1570 talloc_free(tmp_ctx);
1574 /* update the recovery daemon so it now knows to expect the new
1575 node assignment for this ip.
1577 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1579 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1583 talloc_free(tmp_ctx);
1588 move/failover an ip address to a specific node
1590 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1593 int ret, retries = 0;
1594 ctdb_sock_addr addr;
1601 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1602 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1607 if (sscanf(argv[1], "%u", &pnn) != 1) {
1608 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1613 ret = move_ip(ctdb, &addr, pnn);
1615 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1619 } while (retries < 5 && ret != 0);
1621 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1628 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1633 if (ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), pnn, &recmaster) != 0) {
1634 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", pnn));
1638 data.dptr = (uint8_t *)&pnn;
1639 data.dsize = sizeof(uint32_t);
1640 if (ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1641 DEBUG(DEBUG_ERR,("Failed to send message to force node reallocation\n"));
1650 rebalance a node by setting it to allow failback and triggering a
1653 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1655 switch (options.pnn) {
1656 case CTDB_BROADCAST_ALL:
1657 case CTDB_CURRENT_NODE:
1658 DEBUG(DEBUG_ERR,("You must specify a node number with -n <pnn> for the node to rebalance\n"));
1662 return rebalance_node(ctdb, options.pnn);
1666 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1668 struct ctdb_public_ip ip;
1671 uint32_t disable_time;
1673 struct ctdb_node_map *nodemap=NULL;
1674 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1677 data.dptr = (uint8_t*)&disable_time;
1678 data.dsize = sizeof(disable_time);
1679 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1681 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1688 data.dptr = (uint8_t *)&ip;
1689 data.dsize = sizeof(ip);
1691 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1693 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1694 talloc_free(tmp_ctx);
1698 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1699 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1706 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1707 talloc_free(tmp_ctx);
1711 talloc_free(tmp_ctx);
1716 release an ip form all nodes and have it re-assigned by recd
1718 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1720 ctdb_sock_addr addr;
1727 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1728 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1732 if (rebalance_ip(ctdb, &addr) != 0) {
1733 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1740 static int getips_store_callback(void *param, void *data)
1742 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1743 struct ctdb_all_public_ips *ips = param;
1747 ips->ips[i].pnn = node_ip->pnn;
1748 ips->ips[i].addr = node_ip->addr;
1752 static int getips_count_callback(void *param, void *data)
1754 uint32_t *count = param;
1761 static uint32_t *ip_key(ctdb_sock_addr *ip)
1763 static uint32_t key[IP_KEYLEN];
1765 bzero(key, sizeof(key));
1767 switch (ip->sa.sa_family) {
1769 key[0] = ip->ip.sin_addr.s_addr;
1772 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1780 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1787 static void *add_ip_callback(void *parm, void *data)
1793 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1795 struct ctdb_all_public_ips *tmp_ips;
1796 struct ctdb_node_map *nodemap=NULL;
1797 trbt_tree_t *ip_tree;
1801 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1803 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1807 ip_tree = trbt_create(tmp_ctx, 0);
1809 for(i=0;i<nodemap->num;i++){
1810 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1813 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1817 /* read the public ip list from this node */
1818 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1820 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1824 for (j=0; j<tmp_ips->num;j++) {
1825 struct ctdb_public_ip *node_ip;
1827 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1828 node_ip->pnn = tmp_ips->ips[j].pnn;
1829 node_ip->addr = tmp_ips->ips[j].addr;
1831 trbt_insertarray32_callback(ip_tree,
1832 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1836 talloc_free(tmp_ips);
1841 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1843 len = offsetof(struct ctdb_all_public_ips, ips) +
1844 count*sizeof(struct ctdb_public_ip);
1845 tmp_ips = talloc_zero_size(tmp_ctx, len);
1846 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1855 * scans all other nodes and returns a pnn for another node that can host this
1859 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1861 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1862 struct ctdb_all_public_ips *ips;
1863 struct ctdb_node_map *nodemap=NULL;
1866 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1868 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1869 talloc_free(tmp_ctx);
1873 for(i=0;i<nodemap->num;i++){
1874 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1877 if (nodemap->nodes[i].pnn == options.pnn) {
1881 /* read the public ip list from this node */
1882 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1884 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1888 for (j=0;j<ips->num;j++) {
1889 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1890 talloc_free(tmp_ctx);
1891 return nodemap->nodes[i].pnn;
1897 talloc_free(tmp_ctx);
1901 static uint32_t ipreallocate_finished;
1904 handler for receiving the response to ipreallocate
1906 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1907 TDB_DATA data, void *private_data)
1909 ipreallocate_finished = 1;
1912 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1914 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1916 event_add_timed(ctdb->ev, ctdb,
1917 timeval_current_ofs(1, 0),
1918 ctdb_every_second, ctdb);
1922 ask the recovery daemon on the recovery master to perform a ip reallocation
1924 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1928 struct takeover_run_reply rd;
1930 struct ctdb_node_map *nodemap=NULL;
1932 struct timeval tv = timeval_current();
1934 /* we need some events to trigger so we can timeout and restart
1937 event_add_timed(ctdb->ev, ctdb,
1938 timeval_current_ofs(1, 0),
1939 ctdb_every_second, ctdb);
1941 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1943 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1946 rd.srvid = getpid();
1948 /* register a message port for receiveing the reply so that we
1949 can receive the reply
1951 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1953 data.dptr = (uint8_t *)&rd;
1954 data.dsize = sizeof(rd);
1957 /* check that there are valid nodes available */
1958 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1959 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1962 for (i=0; i<nodemap->num;i++) {
1963 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1967 if (i==nodemap->num) {
1968 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1973 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1974 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1978 /* verify the node exists */
1979 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1980 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1985 /* check tha there are nodes available that can act as a recmaster */
1986 for (i=0; i<nodemap->num; i++) {
1987 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1992 if (i == nodemap->num) {
1993 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1997 /* verify the recovery master is not STOPPED, nor BANNED */
1998 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1999 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
2005 /* verify the recovery master is not STOPPED, nor BANNED */
2006 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
2007 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
2013 ipreallocate_finished = 0;
2014 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
2016 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
2020 tv = timeval_current();
2021 /* this loop will terminate when we have received the reply */
2022 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
2023 event_loop_once(ctdb->ev);
2025 if (ipreallocate_finished == 1) {
2038 add a public ip address to a node
2040 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2043 int len, retries = 0;
2045 ctdb_sock_addr addr;
2046 struct ctdb_control_ip_iface *pub;
2047 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2048 struct ctdb_all_public_ips *ips;
2052 talloc_free(tmp_ctx);
2056 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2057 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2058 talloc_free(tmp_ctx);
2062 /* read the public ip list from the node */
2063 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2065 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2066 talloc_free(tmp_ctx);
2069 for (i=0;i<ips->num;i++) {
2070 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2071 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2078 /* Dont timeout. This command waits for an ip reallocation
2079 which sometimes can take wuite a while if there has
2080 been a recent recovery
2084 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2085 pub = talloc_size(tmp_ctx, len);
2086 CTDB_NO_MEMORY(ctdb, pub);
2090 pub->len = strlen(argv[1])+1;
2091 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2094 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2096 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2100 } while (retries < 5 && ret != 0);
2102 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2103 talloc_free(tmp_ctx);
2107 if (rebalance_node(ctdb, options.pnn) != 0) {
2108 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2112 talloc_free(tmp_ctx);
2117 add a public ip address to a node
2119 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2121 ctdb_sock_addr addr;
2127 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2128 printf("Badly formed ip : %s\n", argv[0]);
2132 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2137 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2139 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2141 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2142 struct ctdb_node_map *nodemap=NULL;
2143 struct ctdb_all_public_ips *ips;
2146 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2148 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2152 /* remove it from the nodes that are not hosting the ip currently */
2153 for(i=0;i<nodemap->num;i++){
2154 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2157 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2158 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2162 for (j=0;j<ips->num;j++) {
2163 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2171 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2175 options.pnn = nodemap->nodes[i].pnn;
2176 control_delip(ctdb, argc, argv);
2180 /* remove it from every node (also the one hosting it) */
2181 for(i=0;i<nodemap->num;i++){
2182 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2185 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2186 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2190 for (j=0;j<ips->num;j++) {
2191 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2199 options.pnn = nodemap->nodes[i].pnn;
2200 control_delip(ctdb, argc, argv);
2203 talloc_free(tmp_ctx);
2208 delete a public ip address from a node
2210 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2214 ctdb_sock_addr addr;
2215 struct ctdb_control_ip_iface pub;
2216 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2217 struct ctdb_all_public_ips *ips;
2220 talloc_free(tmp_ctx);
2224 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2225 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2229 if (options.pnn == CTDB_BROADCAST_ALL) {
2230 return control_delip_all(ctdb, argc, argv, &addr);
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 cluster\n"));
2240 talloc_free(tmp_ctx);
2244 for (i=0;i<ips->num;i++) {
2245 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2251 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2252 ctdb_addr_to_str(&addr)));
2253 talloc_free(tmp_ctx);
2257 if (ips->ips[i].pnn == options.pnn) {
2260 pnn = find_other_host_for_public_ip(ctdb, &addr);
2263 ret = move_ip(ctdb, &addr, pnn);
2265 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
2269 } while (retries < 5 && ret != 0);
2271 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
2277 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2279 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2280 talloc_free(tmp_ctx);
2284 talloc_free(tmp_ctx);
2289 kill a tcp connection
2291 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2294 struct ctdb_control_killtcp killtcp;
2300 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2301 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2305 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2306 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2310 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2312 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2323 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2326 ctdb_sock_addr addr;
2332 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2333 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2337 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2339 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2347 register a server id
2349 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2352 struct ctdb_server_id server_id;
2358 server_id.pnn = strtoul(argv[0], NULL, 0);
2359 server_id.type = strtoul(argv[1], NULL, 0);
2360 server_id.server_id = strtoul(argv[2], NULL, 0);
2362 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2364 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2367 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2373 unregister a server id
2375 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2378 struct ctdb_server_id server_id;
2384 server_id.pnn = strtoul(argv[0], NULL, 0);
2385 server_id.type = strtoul(argv[1], NULL, 0);
2386 server_id.server_id = strtoul(argv[2], NULL, 0);
2388 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2390 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2397 check if a server id exists
2399 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2403 struct ctdb_server_id server_id;
2409 server_id.pnn = strtoul(argv[0], NULL, 0);
2410 server_id.type = strtoul(argv[1], NULL, 0);
2411 server_id.server_id = strtoul(argv[2], NULL, 0);
2413 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2415 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2420 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2422 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2428 get a list of all server ids that are registered on a node
2430 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2433 struct ctdb_server_id_list *server_ids;
2435 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2437 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2441 for (i=0; i<server_ids->num; i++) {
2442 printf("Server id %d:%d:%d\n",
2443 server_ids->server_ids[i].pnn,
2444 server_ids->server_ids[i].type,
2445 server_ids->server_ids[i].server_id);
2452 check if a server id exists
2454 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2456 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2462 talloc_free(tmp_ctx);
2466 ids = talloc_array(tmp_ctx, uint64_t, argc);
2467 result = talloc_array(tmp_ctx, uint8_t, argc);
2469 for (i = 0; i < argc; i++) {
2470 ids[i] = strtoull(argv[i], NULL, 0);
2473 if (!ctdb_check_message_handlers(ctdb_connection,
2474 options.pnn, argc, ids, result)) {
2475 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2477 talloc_free(tmp_ctx);
2481 for (i=0; i < argc; i++) {
2482 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2483 result[i] ? "exists" : "does not exist");
2486 talloc_free(tmp_ctx);
2491 send a tcp tickle ack
2493 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2496 ctdb_sock_addr src, dst;
2502 if (!parse_ip_port(argv[0], &src)) {
2503 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2507 if (!parse_ip_port(argv[1], &dst)) {
2508 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2512 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2516 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2523 display public ip status
2525 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2528 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2529 struct ctdb_all_public_ips *ips;
2531 if (options.pnn == CTDB_BROADCAST_ALL) {
2532 /* read the list of public ips from all nodes */
2533 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2535 /* read the public ip list from this node */
2536 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2539 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2540 talloc_free(tmp_ctx);
2544 if (options.machinereadable){
2545 printf(":Public IP:Node:");
2546 if (options.verbose){
2547 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2551 if (options.pnn == CTDB_BROADCAST_ALL) {
2552 printf("Public IPs on ALL nodes\n");
2554 printf("Public IPs on node %u\n", options.pnn);
2558 for (i=1;i<=ips->num;i++) {
2559 struct ctdb_control_public_ip_info *info = NULL;
2561 char *aciface = NULL;
2562 char *avifaces = NULL;
2563 char *cifaces = NULL;
2565 if (options.pnn == CTDB_BROADCAST_ALL) {
2566 pnn = ips->ips[ips->num-i].pnn;
2572 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2573 &ips->ips[ips->num-i].addr, &info);
2580 for (j=0; j < info->num; j++) {
2581 if (cifaces == NULL) {
2582 cifaces = talloc_strdup(info,
2583 info->ifaces[j].name);
2585 cifaces = talloc_asprintf_append(cifaces,
2587 info->ifaces[j].name);
2590 if (info->active_idx == j) {
2591 aciface = info->ifaces[j].name;
2594 if (info->ifaces[j].link_state == 0) {
2598 if (avifaces == NULL) {
2599 avifaces = talloc_strdup(info, info->ifaces[j].name);
2601 avifaces = talloc_asprintf_append(avifaces,
2603 info->ifaces[j].name);
2608 if (options.machinereadable){
2610 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2611 ips->ips[ips->num-i].pnn);
2612 if (options.verbose){
2615 avifaces?avifaces:"",
2616 cifaces?cifaces:"");
2620 if (options.verbose) {
2621 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2622 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2623 ips->ips[ips->num-i].pnn,
2625 avifaces?avifaces:"",
2626 cifaces?cifaces:"");
2629 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2630 ips->ips[ips->num-i].pnn);
2636 talloc_free(tmp_ctx);
2643 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2646 ctdb_sock_addr addr;
2647 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2648 struct ctdb_control_public_ip_info *info;
2651 talloc_free(tmp_ctx);
2655 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2656 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2660 /* read the public ip info from this node */
2661 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2662 tmp_ctx, &addr, &info);
2664 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2665 argv[0], options.pnn));
2666 talloc_free(tmp_ctx);
2670 printf("Public IP[%s] info on node %u\n",
2671 ctdb_addr_to_str(&info->ip.addr),
2674 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2675 ctdb_addr_to_str(&info->ip.addr),
2676 info->ip.pnn, info->num);
2678 for (i=0; i<info->num; i++) {
2679 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2681 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2682 i+1, info->ifaces[i].name,
2683 info->ifaces[i].link_state?"up":"down",
2684 (unsigned int)info->ifaces[i].references,
2685 (i==info->active_idx)?" (active)":"");
2688 talloc_free(tmp_ctx);
2693 display interfaces status
2695 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2698 struct ctdb_ifaces_list *ifaces;
2700 /* read the public ip list from this node */
2701 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2702 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2707 if (options.machinereadable){
2708 printf(":Name:LinkStatus:References:\n");
2710 printf("Interfaces on node %u\n", options.pnn);
2713 for (i=0; i<ifaces->num; i++) {
2714 if (options.machinereadable){
2715 printf(":%s:%s:%u\n",
2716 ifaces->ifaces[i].name,
2717 ifaces->ifaces[i].link_state?"1":"0",
2718 (unsigned int)ifaces->ifaces[i].references);
2720 printf("name:%s link:%s references:%u\n",
2721 ifaces->ifaces[i].name,
2722 ifaces->ifaces[i].link_state?"up":"down",
2723 (unsigned int)ifaces->ifaces[i].references);
2727 ctdb_free_ifaces(ifaces);
2733 set link status of an interface
2735 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2738 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2739 struct ctdb_control_iface_info info;
2747 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2748 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2750 talloc_free(tmp_ctx);
2753 strcpy(info.name, argv[0]);
2755 if (strcmp(argv[1], "up") == 0) {
2756 info.link_state = 1;
2757 } else if (strcmp(argv[1], "down") == 0) {
2758 info.link_state = 0;
2760 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2762 talloc_free(tmp_ctx);
2766 /* read the public ip list from this node */
2767 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2770 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2771 argv[0], options.pnn));
2772 talloc_free(tmp_ctx);
2776 talloc_free(tmp_ctx);
2781 display pid of a ctdb daemon
2783 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2788 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2790 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2793 printf("Pid:%d\n", pid);
2799 disable a remote node
2801 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2804 struct ctdb_node_map *nodemap=NULL;
2806 /* check if the node is already disabled */
2807 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2808 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2811 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2812 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2817 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2819 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2825 /* read the nodemap and verify the change took effect */
2826 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2827 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2831 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2832 ret = control_ipreallocate(ctdb, argc, argv);
2834 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2842 enable a disabled remote node
2844 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2848 struct ctdb_node_map *nodemap=NULL;
2851 /* check if the node is already enabled */
2852 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2853 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2856 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2857 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2862 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2864 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2870 /* read the nodemap and verify the change took effect */
2871 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2872 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2876 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2878 ret = control_ipreallocate(ctdb, argc, argv);
2880 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2890 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2893 struct ctdb_node_map *nodemap=NULL;
2896 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2898 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2903 /* read the nodemap and verify the change took effect */
2904 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2905 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2909 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2910 ret = control_ipreallocate(ctdb, argc, argv);
2912 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2920 restart a stopped remote node
2922 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2926 struct ctdb_node_map *nodemap=NULL;
2929 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2931 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2937 /* read the nodemap and verify the change took effect */
2938 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2939 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2943 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2944 ret = control_ipreallocate(ctdb, argc, argv);
2946 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2953 static uint32_t get_generation(struct ctdb_context *ctdb)
2955 struct ctdb_vnn_map *vnnmap=NULL;
2958 /* wait until the recmaster is not in recovery mode */
2960 uint32_t recmode, recmaster;
2962 if (vnnmap != NULL) {
2963 talloc_free(vnnmap);
2967 /* get the recmaster */
2968 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2969 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2973 /* get recovery mode */
2974 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2975 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2979 /* get the current generation number */
2980 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2982 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2986 if ((recmode == CTDB_RECOVERY_NORMAL)
2987 && (vnnmap->generation != 1)){
2988 return vnnmap->generation;
2995 ban a node from the cluster
2997 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3000 struct ctdb_node_map *nodemap=NULL;
3001 struct ctdb_ban_time bantime;
3007 /* verify the node exists */
3008 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3010 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3014 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
3015 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
3019 bantime.pnn = options.pnn;
3020 bantime.time = strtoul(argv[0], NULL, 0);
3022 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
3024 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
3028 ret = control_ipreallocate(ctdb, argc, argv);
3030 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3039 unban a node from the cluster
3041 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3044 struct ctdb_node_map *nodemap=NULL;
3045 struct ctdb_ban_time bantime;
3047 /* verify the node exists */
3048 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3050 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3054 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
3055 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
3059 bantime.pnn = options.pnn;
3062 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
3064 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
3068 ret = control_ipreallocate(ctdb, argc, argv);
3070 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
3079 show ban information for a node
3081 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3084 struct ctdb_node_map *nodemap=NULL;
3085 struct ctdb_ban_time *bantime;
3087 /* verify the node exists */
3088 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3090 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3094 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3096 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3100 if (bantime->time == 0) {
3101 printf("Node %u is not banned\n", bantime->pnn);
3103 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
3112 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3116 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3118 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3128 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3131 uint32_t generation, next_generation;
3133 /* record the current generation number */
3134 generation = get_generation(ctdb);
3136 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3138 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3142 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3144 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3148 /* wait until we are in a new generation */
3150 next_generation = get_generation(ctdb);
3151 if (next_generation != generation) {
3162 display monitoring mode of a remote node
3164 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3169 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3171 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3174 if (!options.machinereadable){
3175 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3178 printf(":%d:\n",monmode);
3185 display capabilities of a remote node
3187 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3189 uint32_t capabilities;
3191 if (!ctdb_getcapabilities(ctdb_connection, options.pnn, &capabilities)) {
3192 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3196 if (!options.machinereadable){
3197 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3198 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3199 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3200 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3202 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3203 printf(":%d:%d:%d:%d:\n",
3204 !!(capabilities&CTDB_CAP_RECMASTER),
3205 !!(capabilities&CTDB_CAP_LMASTER),
3206 !!(capabilities&CTDB_CAP_LVS),
3207 !!(capabilities&CTDB_CAP_NATGW));
3213 display lvs configuration
3215 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3217 uint32_t *capabilities;
3218 struct ctdb_node_map *nodemap=NULL;
3220 int healthy_count = 0;
3222 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3223 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3227 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3228 CTDB_NO_MEMORY(ctdb, capabilities);
3232 /* collect capabilities for all connected nodes */
3233 for (i=0; i<nodemap->num; i++) {
3234 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3237 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3241 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3242 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3247 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3251 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3256 /* Print all LVS nodes */
3257 for (i=0; i<nodemap->num; i++) {
3258 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3261 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3264 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3268 if (healthy_count != 0) {
3269 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3274 printf("%d:%s\n", i,
3275 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3279 ctdb_free_nodemap(nodemap);
3284 display who is the lvs master
3286 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3288 uint32_t *capabilities;
3289 struct ctdb_node_map *nodemap=NULL;
3291 int healthy_count = 0;
3293 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
3294 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3298 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3299 CTDB_NO_MEMORY(ctdb, capabilities);
3303 /* collect capabilities for all connected nodes */
3304 for (i=0; i<nodemap->num; i++) {
3305 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3308 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3312 if (!ctdb_getcapabilities(ctdb_connection, i, &capabilities[i])) {
3313 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3318 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3322 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3327 /* find and show the lvsmaster */
3328 for (i=0; i<nodemap->num; i++) {
3329 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3332 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3335 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3339 if (healthy_count != 0) {
3340 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3345 if (options.machinereadable){
3348 printf("Node %d is LVS master\n", i);
3354 printf("There is no LVS master\n");
3356 ctdb_free_nodemap(nodemap);
3361 disable monitoring on a node
3363 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3368 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3370 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3373 printf("Monitoring mode:%s\n","DISABLED");
3379 enable monitoring on a node
3381 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3386 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3388 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3391 printf("Monitoring mode:%s\n","ACTIVE");
3397 display remote list of keys/data for a db
3399 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3401 const char *db_name;
3402 struct ctdb_db_context *ctdb_db;
3405 struct ctdb_dump_db_context c;
3414 if (db_exists(ctdb, db_name, &persistent)) {
3415 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3419 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3421 if (ctdb_db == NULL) {
3422 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3426 if (options.printlmaster) {
3427 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3428 ctdb, &ctdb->vnn_map);
3430 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3438 c.printemptyrecords = (bool)options.printemptyrecords;
3439 c.printdatasize = (bool)options.printdatasize;
3440 c.printlmaster = (bool)options.printlmaster;
3441 c.printhash = (bool)options.printhash;
3442 c.printrecordflags = (bool)options.printrecordflags;
3444 /* traverse and dump the cluster tdb */
3445 ret = ctdb_dump_db(ctdb_db, &c);
3447 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3448 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3449 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3453 talloc_free(ctdb_db);
3455 printf("Dumped %d records\n", ret);
3459 struct cattdb_data {
3460 struct ctdb_context *ctdb;
3464 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3466 struct cattdb_data *d = private_data;
3467 struct ctdb_dump_db_context c;
3473 c.printemptyrecords = (bool)options.printemptyrecords;
3474 c.printdatasize = (bool)options.printdatasize;
3475 c.printlmaster = false;
3476 c.printhash = (bool)options.printhash;
3477 c.printrecordflags = true;
3479 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3483 cat the local tdb database using same format as catdb
3485 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3487 const char *db_name;
3488 struct ctdb_db_context *ctdb_db;
3489 struct cattdb_data d;
3499 if (db_exists(ctdb, db_name, &persistent)) {
3500 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3504 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3506 if (ctdb_db == NULL) {
3507 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3511 /* traverse the local tdb */
3514 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3515 printf("Failed to cattdb data\n");
3518 talloc_free(ctdb_db);
3520 printf("Dumped %d records\n", d.count);
3525 display the content of a database key
3527 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3529 const char *db_name;
3530 struct ctdb_db_context *ctdb_db;
3531 struct ctdb_record_handle *h;
3532 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3542 if (db_exists(ctdb, db_name, &persistent)) {
3543 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3547 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3549 if (ctdb_db == NULL) {
3550 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3554 key.dptr = discard_const(argv[1]);
3555 key.dsize = strlen((char *)key.dptr);
3557 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3559 printf("Failed to fetch record '%s' on node %d\n",
3560 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3561 talloc_free(tmp_ctx);
3565 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3567 talloc_free(ctdb_db);
3568 talloc_free(tmp_ctx);
3573 display the content of a database key
3575 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3577 const char *db_name;
3578 struct ctdb_db_context *ctdb_db;
3579 struct ctdb_record_handle *h;
3580 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3590 if (db_exists(ctdb, db_name, &persistent)) {
3591 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3595 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3597 if (ctdb_db == NULL) {
3598 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3602 key.dptr = discard_const(argv[1]);
3603 key.dsize = strlen((char *)key.dptr);
3605 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3607 printf("Failed to fetch record '%s' on node %d\n",
3608 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3609 talloc_free(tmp_ctx);
3613 data.dptr = discard_const(argv[2]);
3614 data.dsize = strlen((char *)data.dptr);
3616 if (ctdb_record_store(h, data) != 0) {
3617 printf("Failed to store record\n");
3621 talloc_free(ctdb_db);
3622 talloc_free(tmp_ctx);
3627 fetch a record from a persistent database
3629 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3631 const char *db_name;
3632 struct ctdb_db_context *ctdb_db;
3633 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3634 struct ctdb_transaction_handle *h;
3640 talloc_free(tmp_ctx);
3647 if (db_exists(ctdb, db_name, &persistent)) {
3648 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3649 talloc_free(tmp_ctx);
3654 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3655 talloc_free(tmp_ctx);
3659 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3661 if (ctdb_db == NULL) {
3662 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3663 talloc_free(tmp_ctx);
3667 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3669 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3670 talloc_free(tmp_ctx);
3674 key.dptr = discard_const(argv[1]);
3675 key.dsize = strlen(argv[1]);
3676 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3678 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3679 talloc_free(tmp_ctx);
3683 if (data.dsize == 0 || data.dptr == NULL) {
3684 DEBUG(DEBUG_ERR,("Record is empty\n"));
3685 talloc_free(tmp_ctx);
3690 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3692 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3693 talloc_free(tmp_ctx);
3696 write(fd, data.dptr, data.dsize);
3699 write(1, data.dptr, data.dsize);
3702 /* abort the transaction */
3706 talloc_free(tmp_ctx);
3711 fetch a record from a tdb-file
3713 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3715 const char *tdb_file;
3718 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3727 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3729 printf("Failed to open TDB file %s\n", tdb_file);
3733 if (!strncmp(argv[1], "0x", 2)) {
3734 key = hextodata(tmp_ctx, argv[1] + 2);
3735 if (key.dsize == 0) {
3736 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3740 key.dptr = discard_const(argv[1]);
3741 key.dsize = strlen(argv[1]);
3744 data = tdb_fetch(tdb, key);
3745 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3746 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3754 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3756 printf("Failed to open output file %s\n", argv[2]);
3759 if (options.verbose){
3760 write(fd, data.dptr, data.dsize);
3762 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3766 if (options.verbose){
3767 write(1, data.dptr, data.dsize);
3769 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3773 talloc_free(tmp_ctx);
3778 store a record and header to a tdb-file
3780 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3782 const char *tdb_file;
3785 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3793 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3795 printf("Failed to open TDB file %s\n", tdb_file);
3799 if (!strncmp(argv[1], "0x", 2)) {
3800 key = hextodata(tmp_ctx, argv[1] + 2);
3801 if (key.dsize == 0) {
3802 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3806 key.dptr = discard_const(argv[1]);
3807 key.dsize = strlen(argv[1]);
3810 if (!strncmp(argv[2], "0x", 2)) {
3811 data = hextodata(tmp_ctx, argv[2] + 2);
3812 if (data.dsize == 0) {
3813 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3817 data.dptr = discard_const(argv[2]);
3818 data.dsize = strlen(argv[2]);
3821 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
3822 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
3825 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3826 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3833 talloc_free(tmp_ctx);
3838 write a record to a persistent database
3840 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3842 const char *db_name;
3843 struct ctdb_db_context *ctdb_db;
3844 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3845 struct ctdb_transaction_handle *h;
3851 talloc_free(tmp_ctx);
3855 fd = open(argv[2], O_RDONLY);
3857 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3858 talloc_free(tmp_ctx);
3862 ret = fstat(fd, &st);
3864 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3866 talloc_free(tmp_ctx);
3870 if (!S_ISREG(st.st_mode)) {
3871 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3873 talloc_free(tmp_ctx);
3877 data.dsize = st.st_size;
3878 if (data.dsize == 0) {
3881 data.dptr = talloc_size(tmp_ctx, data.dsize);
3882 if (data.dptr == NULL) {
3883 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3885 talloc_free(tmp_ctx);
3888 ret = read(fd, data.dptr, data.dsize);
3889 if (ret != data.dsize) {
3890 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3892 talloc_free(tmp_ctx);
3901 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3902 if (ctdb_db == NULL) {
3903 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3904 talloc_free(tmp_ctx);
3908 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3910 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3911 talloc_free(tmp_ctx);
3915 key.dptr = discard_const(argv[1]);
3916 key.dsize = strlen(argv[1]);
3917 ret = ctdb_transaction_store(h, key, data);
3919 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3920 talloc_free(tmp_ctx);
3924 ret = ctdb_transaction_commit(h);
3926 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3927 talloc_free(tmp_ctx);
3932 talloc_free(tmp_ctx);
3937 * delete a record from a persistent database
3939 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
3941 const char *db_name;
3942 struct ctdb_db_context *ctdb_db;
3943 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3944 struct ctdb_transaction_handle *h;
3950 talloc_free(tmp_ctx);
3956 if (db_exists(ctdb, db_name, &persistent)) {
3957 DEBUG(DEBUG_ERR, ("Database '%s' does not exist\n", db_name));
3958 talloc_free(tmp_ctx);
3963 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
3964 talloc_free(tmp_ctx);
3968 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3969 if (ctdb_db == NULL) {
3970 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
3971 talloc_free(tmp_ctx);
3975 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3977 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
3978 talloc_free(tmp_ctx);
3982 key.dptr = discard_const(argv[1]);
3983 key.dsize = strlen(argv[1]);
3984 ret = ctdb_transaction_store(h, key, tdb_null);
3986 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
3987 talloc_free(tmp_ctx);
3991 ret = ctdb_transaction_commit(h);
3993 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
3994 talloc_free(tmp_ctx);
3998 talloc_free(tmp_ctx);
4003 check if a service is bound to a port or not
4005 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4010 struct sockaddr_in sin;
4013 printf("Use: ctdb chktcport <port>\n");
4017 port = atoi(argv[0]);
4019 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4021 printf("Failed to open local socket\n");
4025 v = fcntl(s, F_GETFL, 0);
4026 fcntl(s, F_SETFL, v | O_NONBLOCK);
4028 bzero(&sin, sizeof(sin));
4029 sin.sin_family = PF_INET;
4030 sin.sin_port = htons(port);
4031 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4034 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4043 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
4044 TDB_DATA data, void *private_data)
4046 DEBUG(DEBUG_ERR,("Log data received\n"));
4047 if (data.dsize > 0) {
4048 printf("%s", data.dptr);
4055 display a list of log messages from the in memory ringbuffer
4057 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4061 struct ctdb_get_log_addr log_addr;
4065 /* Since this can fail, do it first */
4066 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4067 if (log_addr.pnn == -1) {
4068 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4072 /* Process options */
4074 log_addr.level = DEBUG_NOTICE;
4075 for (i = 0; i < argc; i++) {
4076 if (strcmp(argv[i], "recoverd") == 0) {
4077 main_daemon = false;
4079 if (isalpha(argv[i][0]) || argv[i][0] == '-') {
4080 log_addr.level = get_debug_by_desc(argv[i]);
4082 log_addr.level = strtol(argv[i], NULL, 0);
4087 /* Our message port is our PID */
4088 log_addr.srvid = getpid();
4090 data.dptr = (unsigned char *)&log_addr;
4091 data.dsize = sizeof(log_addr);
4093 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4095 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4098 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4103 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4105 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4106 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4107 if (ret != 0 || res != 0) {
4108 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4109 talloc_free(tmp_ctx);
4112 talloc_free(tmp_ctx);
4114 ret = ctdb_client_send_message(ctdb, options.pnn,
4115 CTDB_SRVID_GETLOG, data);
4117 DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4122 tv = timeval_current();
4123 /* this loop will terminate when we have received the reply */
4124 while (timeval_elapsed(&tv) < (double)options.timelimit) {
4125 event_loop_once(ctdb->ev);
4128 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4134 clear the in memory log area
4136 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4140 if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4141 /* "recoverd" not given - get logs from main daemon */
4144 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4146 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4147 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4148 if (ret != 0 || res != 0) {
4149 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4150 talloc_free(tmp_ctx);
4154 talloc_free(tmp_ctx);
4156 TDB_DATA data; /* unused in recoverd... */
4159 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4161 DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4170 static uint32_t reloadips_finished;
4172 static void reloadips_handler(struct ctdb_context *ctdb, uint64_t srvid,
4173 TDB_DATA data, void *private_data)
4175 reloadips_finished = 1;
4178 static int reloadips_all(struct ctdb_context *ctdb)
4180 struct reloadips_all_reply rips;
4181 struct ctdb_node_map *nodemap=NULL;
4186 /* check that there are valid nodes available */
4187 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
4188 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4191 for (i=0; i<nodemap->num;i++) {
4192 if (nodemap->nodes[i].flags != 0) {
4193 DEBUG(DEBUG_ERR,("reloadips -n all can only be used when all nodes are up and healthy. Aborting due to problem with node %d\n", i));
4199 rips.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4200 if (rips.pnn == -1) {
4201 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4204 rips.srvid = getpid();
4207 /* register a message port for receiveing the reply so that we
4208 can receive the reply
4210 ctdb_client_set_message_handler(ctdb, rips.srvid, reloadips_handler, NULL);
4212 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
4213 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node\n"));
4218 data.dptr = (uint8_t *)&rips;
4219 data.dsize = sizeof(rips);
4221 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_RELOAD_ALL_IPS, data);
4223 DEBUG(DEBUG_ERR,("Failed to send reload all ips request message to %u\n", options.pnn));
4227 reloadips_finished = 0;
4228 while (reloadips_finished == 0) {
4229 event_loop_once(ctdb->ev);
4236 reload public ips on a specific node
4238 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4243 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4245 if (options.pnn == CTDB_BROADCAST_ALL) {
4246 return reloadips_all(ctdb);
4249 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4250 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4251 if (ret != 0 || res != 0) {
4252 DEBUG(DEBUG_ERR,("Failed to reload ips\n"));
4253 talloc_free(tmp_ctx);
4257 talloc_free(tmp_ctx);
4262 display a list of the databases on a remote ctdb
4264 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4267 struct ctdb_dbid_map *dbmap=NULL;
4269 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4271 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4275 if(options.machinereadable){
4276 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4277 for(i=0;i<dbmap->num;i++){
4285 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4286 dbmap->dbs[i].dbid, ctdb, &path);
4287 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4288 dbmap->dbs[i].dbid, ctdb, &name);
4289 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4290 dbmap->dbs[i].dbid, ctdb, &health);
4291 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4292 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4293 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4294 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4295 dbmap->dbs[i].dbid, name, path,
4296 !!(persistent), !!(sticky),
4297 !!(health), !!(readonly));
4302 printf("Number of databases:%d\n", dbmap->num);
4303 for(i=0;i<dbmap->num;i++){
4311 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4312 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4313 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4314 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4315 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4316 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4317 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4318 dbmap->dbs[i].dbid, name, path,
4319 persistent?" PERSISTENT":"",
4320 sticky?" STICKY":"",
4321 readonly?" READONLY":"",
4322 health?" UNHEALTHY":"");
4329 display the status of a database on a remote ctdb
4331 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4334 struct ctdb_dbid_map *dbmap=NULL;
4335 const char *db_name;
4343 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4345 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4349 for(i=0;i<dbmap->num;i++){
4357 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4358 if (strcmp(name, db_name) != 0) {
4362 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4363 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4364 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4365 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4366 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4367 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4368 dbmap->dbs[i].dbid, name, path,
4369 persistent?"yes":"no",
4371 readonly?"yes":"no",
4372 health?health:"OK");
4376 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
4381 check if the local node is recmaster or not
4382 it will return 1 if this node is the recmaster and 0 if it is not
4383 or if the local ctdb daemon could not be contacted
4385 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4387 uint32_t mypnn, recmaster;
4389 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
4391 printf("Failed to get pnn of node\n");
4395 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
4396 printf("Failed to get the recmaster\n");
4400 if (recmaster != mypnn) {
4401 printf("this node is not the recmaster\n");
4405 printf("this node is the recmaster\n");
4412 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4415 struct timeval tv = timeval_current();
4416 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4418 printf("Unable to get ping response from node %u\n", options.pnn);
4421 printf("response from %u time=%.6f sec (%d clients)\n",
4422 options.pnn, timeval_elapsed(&tv), ret);
4431 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4442 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4444 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4448 printf("%-23s = %u\n", name, value);
4455 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4466 value = strtoul(argv[1], NULL, 0);
4468 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4470 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4479 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4485 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4487 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4491 for (i=0;i<count;i++) {
4492 control_getvar(ctdb, 1, &list[i]);
4501 display debug level on a node
4503 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4508 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4510 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4513 if (options.machinereadable){
4514 printf(":Name:Level:\n");
4515 printf(":%s:%d:\n",get_debug_by_level(level),level);
4517 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4524 display reclock file of a node
4526 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4529 const char *reclock;
4531 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4533 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4536 if (options.machinereadable){
4537 if (reclock != NULL) {
4538 printf("%s", reclock);
4541 if (reclock == NULL) {
4542 printf("No reclock file used.\n");
4544 printf("Reclock file:%s\n", reclock);
4552 set the reclock file of a node
4554 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4557 const char *reclock;
4561 } else if (argc == 1) {
4567 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4569 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4576 set the natgw state on/off
4578 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4581 uint32_t natgwstate;
4587 if (!strcmp(argv[0], "on")) {
4589 } else if (!strcmp(argv[0], "off")) {
4595 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4597 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4605 set the lmaster role on/off
4607 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4610 uint32_t lmasterrole;
4616 if (!strcmp(argv[0], "on")) {
4618 } else if (!strcmp(argv[0], "off")) {
4624 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4626 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4634 set the recmaster role on/off
4636 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4639 uint32_t recmasterrole;
4645 if (!strcmp(argv[0], "on")) {
4647 } else if (!strcmp(argv[0], "off")) {
4653 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4655 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4663 set debug level on a node or all nodes
4665 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4671 printf("You must specify the debug level. Valid levels are:\n");
4672 for (i=0; debug_levels[i].description != NULL; i++) {
4673 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4679 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4680 level = get_debug_by_desc(argv[0]);
4682 level = strtol(argv[0], NULL, 0);
4685 for (i=0; debug_levels[i].description != NULL; i++) {
4686 if (level == debug_levels[i].level) {
4690 if (debug_levels[i].description == NULL) {
4691 printf("Invalid debug level, must be one of\n");
4692 for (i=0; debug_levels[i].description != NULL; i++) {
4693 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4698 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4700 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4709 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4715 priority = strtol(argv[0], NULL, 0);
4719 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4721 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4723 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4730 attach to a database
4732 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4734 const char *db_name;
4735 struct ctdb_db_context *ctdb_db;
4736 bool persistent = false;
4746 if (strcmp(argv[1], "persistent") != 0) {
4752 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4753 if (ctdb_db == NULL) {
4754 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4764 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4766 struct ctdb_db_priority db_prio;
4773 db_prio.db_id = strtoul(argv[0], NULL, 0);
4774 db_prio.priority = strtoul(argv[1], NULL, 0);
4776 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4778 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4788 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4790 uint32_t db_id, priority;
4797 db_id = strtoul(argv[0], NULL, 0);
4799 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4801 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4805 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4811 set the sticky records capability for a database
4813 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
4815 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4817 struct ctdb_dbid_map *dbmap=NULL;
4824 if (!strncmp(argv[0], "0x", 2)) {
4825 db_id = strtoul(argv[0] + 2, NULL, 0);
4827 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4829 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4830 talloc_free(tmp_ctx);
4833 for(i=0;i<dbmap->num;i++){
4836 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4837 if(!strcmp(argv[0], name)){
4838 talloc_free(discard_const(name));
4841 talloc_free(discard_const(name));
4843 if (i == dbmap->num) {
4844 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4845 talloc_free(tmp_ctx);
4848 db_id = dbmap->dbs[i].dbid;
4851 ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
4853 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
4854 talloc_free(tmp_ctx);
4858 talloc_free(tmp_ctx);
4863 set the readonly capability for a database
4865 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4867 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4869 struct ctdb_dbid_map *dbmap=NULL;
4876 if (!strncmp(argv[0], "0x", 2)) {
4877 db_id = strtoul(argv[0] + 2, NULL, 0);
4879 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4881 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4882 talloc_free(tmp_ctx);
4885 for(i=0;i<dbmap->num;i++){
4888 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4889 if(!strcmp(argv[0], name)){
4890 talloc_free(discard_const(name));
4893 talloc_free(discard_const(name));
4895 if (i == dbmap->num) {
4896 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4897 talloc_free(tmp_ctx);
4900 db_id = dbmap->dbs[i].dbid;
4903 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4905 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4906 talloc_free(tmp_ctx);
4910 talloc_free(tmp_ctx);
4917 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4927 db_id = strtoul(argv[0], NULL, 0);
4929 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4931 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4935 printf("Sequence number:%lld\n", (long long)seqnum);
4941 run an eventscript on a node
4943 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4949 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4952 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4956 data.dptr = (unsigned char *)discard_const(argv[0]);
4957 data.dsize = strlen((char *)data.dptr) + 1;
4959 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4961 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4962 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4963 if (ret != 0 || res != 0) {
4964 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4965 talloc_free(tmp_ctx);
4968 talloc_free(tmp_ctx);
4972 #define DB_VERSION 1
4973 #define MAX_DB_NAME 64
4974 struct db_file_header {
4975 unsigned long version;
4977 unsigned long persistent;
4979 const char name[MAX_DB_NAME];
4982 struct backup_data {
4983 struct ctdb_marshall_buffer *records;
4986 bool traverse_error;
4989 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4991 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4992 struct ctdb_rec_data *rec;
4994 /* add the record */
4995 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4997 bd->traverse_error = true;
4998 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5001 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5002 if (bd->records == NULL) {
5003 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5004 bd->traverse_error = true;
5007 bd->records->count++;
5008 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5009 bd->len += rec->length;
5017 * backup a database to a file
5019 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5022 struct ctdb_dbid_map *dbmap=NULL;
5023 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5024 struct db_file_header dbhdr;
5025 struct ctdb_db_context *ctdb_db;
5026 struct backup_data *bd;
5029 const char *reason = NULL;
5032 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5036 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
5038 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
5042 for(i=0;i<dbmap->num;i++){
5045 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
5046 if(!strcmp(argv[0], name)){
5047 talloc_free(discard_const(name));
5050 talloc_free(discard_const(name));
5052 if (i == dbmap->num) {
5053 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
5054 talloc_free(tmp_ctx);
5058 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5059 dbmap->dbs[i].dbid, tmp_ctx, &reason);
5061 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5063 talloc_free(tmp_ctx);
5067 uint32_t allow_unhealthy = 0;
5069 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5070 "AllowUnhealthyDBRead",
5073 if (allow_unhealthy != 1) {
5074 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5077 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5079 talloc_free(tmp_ctx);
5083 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5085 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5086 "tunnable AllowUnhealthyDBRead = %u\n",
5090 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5091 if (ctdb_db == NULL) {
5092 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5093 talloc_free(tmp_ctx);
5098 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5100 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5101 talloc_free(tmp_ctx);
5106 bd = talloc_zero(tmp_ctx, struct backup_data);
5108 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5109 talloc_free(tmp_ctx);
5113 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5114 if (bd->records == NULL) {
5115 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5116 talloc_free(tmp_ctx);
5120 bd->len = offsetof(struct ctdb_marshall_buffer, data);
5121 bd->records->db_id = ctdb_db->db_id;
5122 /* traverse the database collecting all records */
5123 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5124 bd->traverse_error) {
5125 DEBUG(DEBUG_ERR,("Traverse error\n"));
5126 talloc_free(tmp_ctx);
5130 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5133 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5135 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5136 talloc_free(tmp_ctx);
5140 dbhdr.version = DB_VERSION;
5141 dbhdr.timestamp = time(NULL);
5142 dbhdr.persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
5143 dbhdr.size = bd->len;
5144 if (strlen(argv[0]) >= MAX_DB_NAME) {
5145 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5148 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
5149 ret = write(fh, &dbhdr, sizeof(dbhdr));
5151 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5154 ret = write(fh, bd->records, bd->len);
5156 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5165 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5169 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5171 talloc_free(tmp_ctx);
5176 * restore a database from a file
5178 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5181 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5184 struct db_file_header dbhdr;
5185 struct ctdb_db_context *ctdb_db;
5186 struct ctdb_node_map *nodemap=NULL;
5187 struct ctdb_vnn_map *vnnmap=NULL;
5189 struct ctdb_control_wipe_database w;
5191 uint32_t generation;
5196 if (argc < 1 || argc > 2) {
5197 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5201 fh = open(argv[0], O_RDONLY);
5203 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5204 talloc_free(tmp_ctx);
5208 read(fh, &dbhdr, sizeof(dbhdr));
5209 if (dbhdr.version != DB_VERSION) {
5210 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5211 talloc_free(tmp_ctx);
5215 dbname = discard_const(dbhdr.name);
5217 dbname = discard_const(argv[1]);
5220 outdata.dsize = dbhdr.size;
5221 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5222 if (outdata.dptr == NULL) {
5223 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5225 talloc_free(tmp_ctx);
5228 read(fh, outdata.dptr, outdata.dsize);
5231 tm = localtime(&dbhdr.timestamp);
5232 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5233 printf("Restoring database '%s' from backup @ %s\n",
5237 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5238 if (ctdb_db == NULL) {
5239 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5240 talloc_free(tmp_ctx);
5244 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5246 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5247 talloc_free(tmp_ctx);
5252 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5254 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5255 talloc_free(tmp_ctx);
5259 /* freeze all nodes */
5260 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5261 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5262 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5268 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5269 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5270 talloc_free(tmp_ctx);
5275 generation = vnnmap->generation;
5276 data.dptr = (void *)&generation;
5277 data.dsize = sizeof(generation);
5279 /* start a cluster wide transaction */
5280 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5281 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5283 TIMELIMIT(), false, data,
5286 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5291 w.db_id = ctdb_db->db_id;
5292 w.transaction_id = generation;
5294 data.dptr = (void *)&w;
5295 data.dsize = sizeof(w);
5297 /* wipe all the remote databases. */
5298 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5299 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5301 TIMELIMIT(), false, data,
5304 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5305 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5306 talloc_free(tmp_ctx);
5310 /* push the database */
5311 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5312 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5314 TIMELIMIT(), false, outdata,
5317 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5318 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5319 talloc_free(tmp_ctx);
5323 data.dptr = (void *)&ctdb_db->db_id;
5324 data.dsize = sizeof(ctdb_db->db_id);
5326 /* mark the database as healthy */
5327 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5328 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5330 TIMELIMIT(), false, data,
5333 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5334 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5335 talloc_free(tmp_ctx);
5339 data.dptr = (void *)&generation;
5340 data.dsize = sizeof(generation);
5342 /* commit all the changes */
5343 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5345 TIMELIMIT(), false, data,
5348 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5349 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5350 talloc_free(tmp_ctx);
5355 /* thaw all nodes */
5356 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5357 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5363 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5364 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5365 talloc_free(tmp_ctx);
5370 talloc_free(tmp_ctx);
5375 * dump a database backup from a file
5377 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5379 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5381 struct db_file_header dbhdr;
5385 struct ctdb_rec_data *rec = NULL;
5386 struct ctdb_marshall_buffer *m;
5387 struct ctdb_dump_db_context c;
5390 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5394 fh = open(argv[0], O_RDONLY);
5396 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5397 talloc_free(tmp_ctx);
5401 read(fh, &dbhdr, sizeof(dbhdr));
5402 if (dbhdr.version != DB_VERSION) {
5403 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5404 talloc_free(tmp_ctx);
5408 outdata.dsize = dbhdr.size;
5409 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5410 if (outdata.dptr == NULL) {
5411 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5413 talloc_free(tmp_ctx);
5416 read(fh, outdata.dptr, outdata.dsize);
5418 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5420 tm = localtime(&dbhdr.timestamp);
5421 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5422 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5423 dbhdr.name, m->db_id, tbuf);
5427 c.printemptyrecords = (bool)options.printemptyrecords;
5428 c.printdatasize = (bool)options.printdatasize;
5429 c.printlmaster = false;
5430 c.printhash = (bool)options.printhash;
5431 c.printrecordflags = (bool)options.printrecordflags;
5433 for (i=0; i < m->count; i++) {
5437 /* we do not want the header splitted, so we pass NULL*/
5438 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5441 ctdb_dumpdb_record(ctdb, key, data, &c);
5444 printf("Dumped %d records\n", i);
5445 talloc_free(tmp_ctx);
5450 * wipe a database from a file
5452 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5456 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5458 struct ctdb_db_context *ctdb_db;
5459 struct ctdb_node_map *nodemap = NULL;
5460 struct ctdb_vnn_map *vnnmap = NULL;
5462 struct ctdb_control_wipe_database w;
5464 uint32_t generation;
5465 struct ctdb_dbid_map *dbmap = NULL;
5468 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5472 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5475 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
5480 for(i=0;i<dbmap->num;i++){
5483 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
5484 dbmap->dbs[i].dbid, tmp_ctx, &name);
5485 if(!strcmp(argv[0], name)){
5486 talloc_free(discard_const(name));
5489 talloc_free(discard_const(name));
5491 if (i == dbmap->num) {
5492 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
5494 talloc_free(tmp_ctx);
5498 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5499 if (ctdb_db == NULL) {
5500 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5502 talloc_free(tmp_ctx);
5506 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5509 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5511 talloc_free(tmp_ctx);
5515 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5518 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5520 talloc_free(tmp_ctx);
5524 /* freeze all nodes */
5525 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5526 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5527 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5534 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5535 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5536 CTDB_RECOVERY_ACTIVE);
5537 talloc_free(tmp_ctx);
5542 generation = vnnmap->generation;
5543 data.dptr = (void *)&generation;
5544 data.dsize = sizeof(generation);
5546 /* start a cluster wide transaction */
5547 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5548 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5550 TIMELIMIT(), false, data,
5554 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5555 "transactions.\n"));
5559 w.db_id = ctdb_db->db_id;
5560 w.transaction_id = generation;
5562 data.dptr = (void *)&w;
5563 data.dsize = sizeof(w);
5565 /* wipe all the remote databases. */
5566 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5567 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5569 TIMELIMIT(), false, data,
5572 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5573 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5574 talloc_free(tmp_ctx);
5578 data.dptr = (void *)&ctdb_db->db_id;
5579 data.dsize = sizeof(ctdb_db->db_id);
5581 /* mark the database as healthy */
5582 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5583 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5585 TIMELIMIT(), false, data,
5588 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5589 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5590 talloc_free(tmp_ctx);
5594 data.dptr = (void *)&generation;
5595 data.dsize = sizeof(generation);
5597 /* commit all the changes */
5598 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5600 TIMELIMIT(), false, data,
5603 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5604 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5605 talloc_free(tmp_ctx);
5609 /* thaw all nodes */
5610 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5611 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5617 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5618 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5619 talloc_free(tmp_ctx);
5623 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5625 talloc_free(tmp_ctx);
5632 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5638 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5639 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5640 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5641 if (ret != 0 || res != 0) {
5642 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5643 talloc_free(tmp_ctx);
5646 write(1, data.dptr, data.dsize);
5647 talloc_free(tmp_ctx);
5652 handler for memory dumps
5654 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5655 TDB_DATA data, void *private_data)
5657 write(1, data.dptr, data.dsize);
5662 dump memory usage on the recovery daemon
5664 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5668 struct rd_memdump_reply rd;
5670 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5672 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
5675 rd.srvid = getpid();
5677 /* register a message port for receiveing the reply so that we
5678 can receive the reply
5680 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5683 data.dptr = (uint8_t *)&rd;
5684 data.dsize = sizeof(rd);
5686 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5688 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5692 /* this loop will terminate when we have received the reply */
5694 event_loop_once(ctdb->ev);
5701 send a message to a srvid
5703 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5705 unsigned long srvid;
5713 srvid = strtoul(argv[0], NULL, 0);
5715 data.dptr = (uint8_t *)discard_const(argv[1]);
5716 data.dsize= strlen(argv[1]);
5718 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5720 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5728 handler for msglisten
5730 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5731 TDB_DATA data, void *private_data)
5735 printf("Message received: ");
5736 for (i=0;i<data.dsize;i++) {
5737 printf("%c", data.dptr[i]);
5743 listen for messages on a messageport
5745 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5751 /* register a message port and listen for messages
5753 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5754 printf("Listening for messages on srvid:%d\n", (int)srvid);
5757 event_loop_once(ctdb->ev);
5764 list all nodes in the cluster
5765 we parse the nodes file directly
5767 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5769 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5770 struct pnn_node *pnn_nodes;
5771 struct pnn_node *pnn_node;
5773 pnn_nodes = read_nodes_file(mem_ctx);
5774 if (pnn_nodes == NULL) {
5775 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5776 talloc_free(mem_ctx);
5780 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5781 ctdb_sock_addr addr;
5782 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5783 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5784 talloc_free(mem_ctx);
5787 if (options.machinereadable){
5788 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5790 printf("%s\n", pnn_node->addr);
5793 talloc_free(mem_ctx);
5799 reload the nodes file on the local node
5801 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5805 struct ctdb_node_map *nodemap=NULL;
5807 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5809 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
5813 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5815 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5819 /* reload the nodes file on all remote nodes */
5820 for (i=0;i<nodemap->num;i++) {
5821 if (nodemap->nodes[i].pnn == mypnn) {
5824 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5825 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5826 nodemap->nodes[i].pnn);
5828 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5832 /* reload the nodes file on the local node */
5833 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5834 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5836 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5839 /* initiate a recovery */
5840 control_recover(ctdb, argc, argv);
5846 static const struct {
5848 int (*fn)(struct ctdb_context *, int, const char **);
5850 bool without_daemon; /* can be run without daemon running ? */
5853 } ctdb_commands[] = {
5854 { "version", control_version, true, true, "show version of ctdb" },
5855 { "status", control_status, true, false, "show node status" },
5856 { "uptime", control_uptime, true, false, "show node uptime" },
5857 { "ping", control_ping, true, false, "ping all nodes" },
5858 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5859 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5860 { "listvars", control_listvars, true, false, "list tunable variables"},
5861 { "statistics", control_statistics, false, false, "show statistics" },
5862 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5863 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5864 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5865 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5866 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5867 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5868 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5869 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5870 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
5871 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname>"},
5872 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname>"},
5873 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5874 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5875 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5876 { "lvs", control_lvs, true, false, "show lvs configuration" },
5877 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5878 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5879 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5880 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5881 { "getdebug", control_getdebug, true, false, "get debug level" },
5882 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
5883 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
5884 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5885 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5886 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5887 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5888 { "disable", control_disable, true, false, "disable a nodes public IP" },
5889 { "enable", control_enable, true, false, "enable a nodes public IP" },
5890 { "stop", control_stop, true, false, "stop a node" },
5891 { "continue", control_continue, true, false, "re-start a stopped node" },
5892 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5893 { "unban", control_unban, true, false, "unban a node" },
5894 { "showban", control_showban, true, false, "show ban information"},
5895 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5896 { "recover", control_recover, true, false, "force recovery" },
5897 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
5898 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5899 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5900 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5901 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5902 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5903 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5904 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5905 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5907 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5909 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5910 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5911 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5912 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5913 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5914 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
5915 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5916 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5917 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5918 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5919 { "rebalanceip", control_rebalanceip, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
5920 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5921 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5922 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5923 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
5924 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5925 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5926 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
5927 { "recmaster", control_recmaster, true, false, "show the pnn for the recovery master."},
5928 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5929 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5930 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5931 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5932 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5933 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5934 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5935 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5936 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5937 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5938 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
5939 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
5940 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbid>|<name>"},
5941 { "setdbsticky", control_setdbsticky, false, false, "Set DB sticky-records capable", "<dbid>|<name>"},
5942 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5943 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5944 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5945 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
5946 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
5947 { "pdelete", control_pdelete, false, false, "delete a record from a persistent database", "<db> <key>" },
5948 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
5949 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
5950 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5951 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5952 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5953 { "rebalancenode", control_rebalancenode, false, false, "release a node by allowing it to takeover ips", "<pnn>"},
5954 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbid>" },
5955 { "nodestatus", control_nodestatus, true, false, "show and return node status" },
5956 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<db>" },
5957 { "reloadips", control_reloadips, false, false, "reload the public addresses file on a node" },
5958 { "ipiface", control_ipiface, true, true, "Find which interface an ip address is hsoted on", "<ip>" },
5964 static void usage(void)
5968 "Usage: ctdb [options] <control>\n" \
5970 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5971 " -Y generate machinereadable output\n"
5972 " -v generate verbose output\n"
5973 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5974 printf("Controls:\n");
5975 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5976 printf(" %-15s %-27s %s\n",
5977 ctdb_commands[i].name,
5978 ctdb_commands[i].args?ctdb_commands[i].args:"",
5979 ctdb_commands[i].msg);
5985 static void ctdb_alarm(int sig)
5987 printf("Maximum runtime exceeded - exiting\n");
5994 int main(int argc, const char *argv[])
5996 struct ctdb_context *ctdb;
5997 char *nodestring = NULL;
5998 struct poptOption popt_options[] = {
6001 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6002 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6003 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6004 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6005 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6006 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6007 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6008 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6009 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6010 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6014 const char **extra_argv;
6018 struct event_context *ev;
6019 const char *control;
6020 const char *socket_name;
6024 /* set some defaults */
6025 options.maxruntime = 0;
6026 options.timelimit = 3;
6027 options.pnn = CTDB_CURRENT_NODE;
6029 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6031 while ((opt = poptGetNextOpt(pc)) != -1) {
6034 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
6035 poptBadOption(pc, 0), poptStrerror(opt)));
6040 /* setup the remaining options for the main program to use */
6041 extra_argv = poptGetArgs(pc);
6044 while (extra_argv[extra_argc]) extra_argc++;
6047 if (extra_argc < 1) {
6051 if (options.maxruntime == 0) {
6052 const char *ctdb_timeout;
6053 ctdb_timeout = getenv("CTDB_TIMEOUT");
6054 if (ctdb_timeout != NULL) {
6055 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6057 /* default timeout is 120 seconds */
6058 options.maxruntime = 120;
6062 signal(SIGALRM, ctdb_alarm);
6063 alarm(options.maxruntime);
6065 control = extra_argv[0];
6067 ev = event_context_init(NULL);
6069 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6073 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6074 if (strcmp(control, ctdb_commands[i].name) == 0) {
6079 if (i == ARRAY_SIZE(ctdb_commands)) {
6080 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6084 if (ctdb_commands[i].without_daemon == true) {
6085 if (nodestring != NULL) {
6086 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6090 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6093 /* initialise ctdb */
6094 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6097 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6101 /* initialize a libctdb connection as well */
6102 socket_name = ctdb_get_socketname(ctdb);
6103 ctdb_connection = ctdb_connect(socket_name,
6104 ctdb_log_file, stderr);
6105 if (ctdb_connection == NULL) {
6106 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
6110 /* setup the node number(s) to contact */
6111 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
6112 &options.nodes, &options.pnn)) {
6116 if (options.pnn == CTDB_CURRENT_NODE) {
6117 options.pnn = options.nodes[0];
6120 if (ctdb_commands[i].auto_all &&
6121 ((options.pnn == CTDB_BROADCAST_ALL) ||
6122 (options.pnn == CTDB_MULTICAST))) {
6126 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6127 options.pnn = options.nodes[j];
6128 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6131 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6134 ctdb_disconnect(ctdb_connection);
6137 (void)poptFreeContext(pc);