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 "lib/tevent/tevent.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/network.h"
26 #include "system/locale.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)
61 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
64 #define XSTR(x) STR(x)
65 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
70 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
71 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
72 "Out of memory in " __location__ )); \
76 /* Pretty print the flags to a static buffer in human-readable format.
77 * This never returns NULL!
79 static const char *pretty_print_flags(uint32_t flags)
86 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
87 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
88 { NODE_FLAGS_BANNED, "BANNED" },
89 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
90 { NODE_FLAGS_DELETED, "DELETED" },
91 { NODE_FLAGS_STOPPED, "STOPPED" },
92 { NODE_FLAGS_INACTIVE, "INACTIVE" },
94 static char flags_str[512]; /* Big enough to contain all flag names */
97 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
98 if (flags & flag_names[j].flag) {
99 if (flags_str[0] == '\0') {
100 (void) strcpy(flags_str, flag_names[j].name);
102 (void) strcat(flags_str, "|");
103 (void) strcat(flags_str, flag_names[j].name);
107 if (flags_str[0] == '\0') {
108 (void) strcpy(flags_str, "OK");
114 static int h2i(char h)
116 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
117 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
121 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
124 TDB_DATA key = {NULL, 0};
128 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
133 key.dptr = talloc_size(mem_ctx, key.dsize);
135 for (i=0; i < len/2; i++) {
136 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
141 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
142 * that are disconnected or deleted. If dd_ok is true those nodes are
143 * included in the output list of nodes. If dd_ok is false, those
144 * nodes are filtered from the "all" case and cause an error if
145 * explicitly specified.
147 static bool parse_nodestring(struct ctdb_context *ctdb,
148 const char * nodestring,
149 uint32_t current_pnn,
156 struct ctdb_node_map *nodemap;
160 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
161 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
165 if (nodestring != NULL) {
166 *nodes = talloc_array(ctdb, uint32_t, 0);
167 CTDB_NOMEM_ABORT(*nodes);
171 if (strcmp(nodestring, "all") == 0) {
172 *pnn_mode = CTDB_BROADCAST_ALL;
175 for (i = 0; i < nodemap->num; i++) {
176 if ((nodemap->nodes[i].flags &
177 (NODE_FLAGS_DISCONNECTED |
178 NODE_FLAGS_DELETED)) && !dd_ok) {
181 *nodes = talloc_realloc(ctdb, *nodes,
183 CTDB_NOMEM_ABORT(*nodes);
191 ns = talloc_strdup(ctdb, nodestring);
192 tok = strtok(ns, ",");
193 while (tok != NULL) {
195 i = (uint32_t)strtoul(tok, NULL, 0);
196 if (i >= nodemap->num) {
197 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
200 if ((nodemap->nodes[i].flags &
201 (NODE_FLAGS_DISCONNECTED |
202 NODE_FLAGS_DELETED)) && !dd_ok) {
203 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
206 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
207 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
211 *nodes = talloc_realloc(ctdb, *nodes,
213 CTDB_NOMEM_ABORT(*nodes);
218 tok = strtok(NULL, ",");
223 *pnn_mode = (*nodes)[0];
225 *pnn_mode = CTDB_MULTICAST;
229 /* default - no nodes specified */
230 *nodes = talloc_array(ctdb, uint32_t, 1);
231 CTDB_NOMEM_ABORT(*nodes);
232 *pnn_mode = CTDB_CURRENT_NODE;
234 if (!ctdb_getpnn(ctdb_connection, current_pnn,
240 ctdb_free_nodemap(nodemap);
246 check if a database exists
248 static int db_exists(struct ctdb_context *ctdb, const char *db_name, bool *persistent)
251 struct ctdb_dbid_map *dbmap=NULL;
253 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
255 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
259 for(i=0;i<dbmap->num;i++){
262 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
263 if (!strcmp(name, db_name)) {
265 *persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
275 see if a process exists
277 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
285 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
286 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
290 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
292 printf("%u:%u exists\n", pnn, pid);
294 printf("%u:%u does not exist\n", pnn, pid);
300 display statistics structure
302 static void show_statistics(struct ctdb_statistics *s, int show_header)
304 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
306 const char *prefix=NULL;
308 int tmp, days, hours, minutes, seconds;
313 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
314 STATISTICS_FIELD(num_clients),
315 STATISTICS_FIELD(frozen),
316 STATISTICS_FIELD(recovering),
317 STATISTICS_FIELD(num_recoveries),
318 STATISTICS_FIELD(client_packets_sent),
319 STATISTICS_FIELD(client_packets_recv),
320 STATISTICS_FIELD(node_packets_sent),
321 STATISTICS_FIELD(node_packets_recv),
322 STATISTICS_FIELD(keepalive_packets_sent),
323 STATISTICS_FIELD(keepalive_packets_recv),
324 STATISTICS_FIELD(node.req_call),
325 STATISTICS_FIELD(node.reply_call),
326 STATISTICS_FIELD(node.req_dmaster),
327 STATISTICS_FIELD(node.reply_dmaster),
328 STATISTICS_FIELD(node.reply_error),
329 STATISTICS_FIELD(node.req_message),
330 STATISTICS_FIELD(node.req_control),
331 STATISTICS_FIELD(node.reply_control),
332 STATISTICS_FIELD(client.req_call),
333 STATISTICS_FIELD(client.req_message),
334 STATISTICS_FIELD(client.req_control),
335 STATISTICS_FIELD(timeouts.call),
336 STATISTICS_FIELD(timeouts.control),
337 STATISTICS_FIELD(timeouts.traverse),
338 STATISTICS_FIELD(total_calls),
339 STATISTICS_FIELD(pending_calls),
340 STATISTICS_FIELD(lockwait_calls),
341 STATISTICS_FIELD(pending_lockwait_calls),
342 STATISTICS_FIELD(childwrite_calls),
343 STATISTICS_FIELD(pending_childwrite_calls),
344 STATISTICS_FIELD(memory_used),
345 STATISTICS_FIELD(max_hop_count),
346 STATISTICS_FIELD(total_ro_delegations),
347 STATISTICS_FIELD(total_ro_revokes),
350 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
359 if (options.machinereadable){
361 printf("CTDB version:");
362 printf("Current time of statistics:");
363 printf("Statistics collected since:");
364 for (i=0;i<ARRAY_SIZE(fields);i++) {
365 printf("%s:", fields[i].name);
367 printf("num_reclock_ctdbd_latency:");
368 printf("min_reclock_ctdbd_latency:");
369 printf("avg_reclock_ctdbd_latency:");
370 printf("max_reclock_ctdbd_latency:");
372 printf("num_reclock_recd_latency:");
373 printf("min_reclock_recd_latency:");
374 printf("avg_reclock_recd_latency:");
375 printf("max_reclock_recd_latency:");
377 printf("num_call_latency:");
378 printf("min_call_latency:");
379 printf("avg_call_latency:");
380 printf("max_call_latency:");
382 printf("num_lockwait_latency:");
383 printf("min_lockwait_latency:");
384 printf("avg_lockwait_latency:");
385 printf("max_lockwait_latency:");
387 printf("num_childwrite_latency:");
388 printf("min_childwrite_latency:");
389 printf("avg_childwrite_latency:");
390 printf("max_childwrite_latency:");
393 printf("%d:", CTDB_VERSION);
394 printf("%d:", (int)s->statistics_current_time.tv_sec);
395 printf("%d:", (int)s->statistics_start_time.tv_sec);
396 for (i=0;i<ARRAY_SIZE(fields);i++) {
397 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
399 printf("%d:", s->reclock.ctdbd.num);
400 printf("%.6f:", s->reclock.ctdbd.min);
401 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
402 printf("%.6f:", s->reclock.ctdbd.max);
404 printf("%d:", s->reclock.recd.num);
405 printf("%.6f:", s->reclock.recd.min);
406 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
407 printf("%.6f:", s->reclock.recd.max);
409 printf("%d:", s->call_latency.num);
410 printf("%.6f:", s->call_latency.min);
411 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
412 printf("%.6f:", s->call_latency.max);
414 printf("%d:", s->lockwait_latency.num);
415 printf("%.6f:", s->lockwait_latency.min);
416 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
417 printf("%.6f:", s->lockwait_latency.max);
419 printf("%d:", s->childwrite_latency.num);
420 printf("%.6f:", s->childwrite_latency.min);
421 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
422 printf("%.6f:", s->childwrite_latency.max);
425 printf("CTDB version %u\n", CTDB_VERSION);
426 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
427 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
429 for (i=0;i<ARRAY_SIZE(fields);i++) {
430 if (strchr(fields[i].name, '.')) {
431 preflen = strcspn(fields[i].name, ".")+1;
432 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
433 prefix = fields[i].name;
434 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
439 printf(" %*s%-22s%*s%10u\n",
441 fields[i].name+preflen,
443 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
445 printf(" hop_count_buckets:");
446 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
447 printf(" %d", s->hop_count_bucket[i]);
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", "lockwait_latency MIN/AVG/MAX", s->lockwait_latency.min, s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0, s->lockwait_latency.max, s->lockwait_latency.num);
456 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);
459 talloc_free(tmp_ctx);
463 display remote ctdb statistics combined from all nodes
465 static int control_statistics_all(struct ctdb_context *ctdb)
468 struct ctdb_statistics statistics;
472 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
473 CTDB_NO_MEMORY(ctdb, nodes);
475 ZERO_STRUCT(statistics);
477 for (i=0;i<num_nodes;i++) {
478 struct ctdb_statistics s1;
480 uint32_t *v1 = (uint32_t *)&s1;
481 uint32_t *v2 = (uint32_t *)&statistics;
483 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
484 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
486 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
489 for (j=0;j<num_ints;j++) {
492 statistics.max_hop_count =
493 MAX(statistics.max_hop_count, s1.max_hop_count);
494 statistics.call_latency.max =
495 MAX(statistics.call_latency.max, s1.call_latency.max);
496 statistics.lockwait_latency.max =
497 MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
500 printf("Gathered statistics for %u nodes\n", num_nodes);
501 show_statistics(&statistics, 1);
506 display remote ctdb statistics
508 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
511 struct ctdb_statistics statistics;
513 if (options.pnn == CTDB_BROADCAST_ALL) {
514 return control_statistics_all(ctdb);
517 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
519 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
522 show_statistics(&statistics, 1);
528 reset remote ctdb statistics
530 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
534 ret = ctdb_statistics_reset(ctdb, options.pnn);
536 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
544 display remote ctdb rolling statistics
546 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
549 struct ctdb_statistics_wire *stats;
550 int i, num_records = -1;
553 num_records = atoi(argv[0]) - 1;
556 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
558 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
561 for (i=0;i<stats->num;i++) {
562 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
565 show_statistics(&stats->stats[i], i==0);
566 if (i == num_records) {
575 display remote ctdb db statistics
577 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
579 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
580 struct ctdb_db_statistics *dbstatistics;
581 struct ctdb_dbid_map *dbmap=NULL;
588 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
590 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
593 for(i=0;i<dbmap->num;i++){
596 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
597 if(!strcmp(argv[0], name)){
598 talloc_free(discard_const(name));
601 talloc_free(discard_const(name));
603 if (i == dbmap->num) {
604 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
605 talloc_free(tmp_ctx);
609 if (!ctdb_getdbstat(ctdb_connection, options.pnn, dbmap->dbs[i].dbid, &dbstatistics)) {
610 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
611 talloc_free(tmp_ctx);
615 printf("DB Statistics:\n");
616 printf("RO Delegations: %d\n", dbstatistics->db_ro_delegations);
617 printf("RO Revokes: %d\n", dbstatistics->db_ro_revokes);
618 printf(" hop_count_buckets:");
619 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
620 printf(" %d", dbstatistics->hop_count_bucket[i]);
624 ctdb_free_dbstat(dbstatistics);
629 display uptime of remote node
631 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
634 struct ctdb_uptime *uptime = NULL;
635 int tmp, days, hours, minutes, seconds;
637 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
639 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
643 if (options.machinereadable){
644 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
645 printf(":%u:%u:%u:%lf\n",
646 (unsigned int)uptime->current_time.tv_sec,
647 (unsigned int)uptime->ctdbd_start_time.tv_sec,
648 (unsigned int)uptime->last_recovery_finished.tv_sec,
649 timeval_delta(&uptime->last_recovery_finished,
650 &uptime->last_recovery_started)
655 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
657 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
665 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
667 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
675 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
677 printf("Duration of last recovery/failover: %lf seconds\n",
678 timeval_delta(&uptime->last_recovery_finished,
679 &uptime->last_recovery_started));
685 show the PNN of the current node
687 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
692 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
694 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
698 printf("PNN:%d\n", mypnn);
704 struct pnn_node *next;
709 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
711 const char *nodes_list;
715 struct pnn_node *pnn_nodes = NULL;
716 struct pnn_node *pnn_node;
717 struct pnn_node *tmp_node;
719 /* read the nodes file */
720 nodes_list = getenv("CTDB_NODES");
721 if (nodes_list == NULL) {
722 nodes_list = "/etc/ctdb/nodes";
724 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
728 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
731 for (i=0, pnn=0; i<nlines; i++) {
735 /* strip leading spaces */
736 while((*node == ' ') || (*node == '\t')) {
743 if (strcmp(node, "") == 0) {
746 pnn_node = talloc(mem_ctx, struct pnn_node);
747 pnn_node->pnn = pnn++;
748 pnn_node->addr = talloc_strdup(pnn_node, node);
749 pnn_node->next = pnn_nodes;
750 pnn_nodes = pnn_node;
753 /* swap them around so we return them in incrementing order */
754 pnn_node = pnn_nodes;
758 pnn_node = pnn_node->next;
760 tmp_node->next = pnn_nodes;
761 pnn_nodes = tmp_node;
768 show the PNN of the current node
769 discover the pnn by loading the nodes file and try to bind to all
770 addresses one at a time until the ip address is found.
772 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
774 TALLOC_CTX *mem_ctx = talloc_new(NULL);
775 struct pnn_node *pnn_nodes;
776 struct pnn_node *pnn_node;
778 pnn_nodes = read_nodes_file(mem_ctx);
779 if (pnn_nodes == NULL) {
780 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
781 talloc_free(mem_ctx);
785 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
788 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
789 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
790 talloc_free(mem_ctx);
794 if (ctdb_sys_have_ip(&addr)) {
795 printf("PNN:%d\n", pnn_node->pnn);
796 talloc_free(mem_ctx);
801 printf("Failed to detect which PNN this node is\n");
802 talloc_free(mem_ctx);
806 /* Helpers for ctdb status
808 static bool is_partially_online(struct ctdb_node_and_flags *node)
813 if (node->flags == 0) {
814 struct ctdb_ifaces_list *ifaces;
816 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
817 for (j=0; j < ifaces->num; j++) {
818 if (ifaces->ifaces[j].link_state != 0) {
824 ctdb_free_ifaces(ifaces);
831 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
833 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
834 ctdb_addr_to_str(&node->addr),
835 !!(node->flags&NODE_FLAGS_DISCONNECTED),
836 !!(node->flags&NODE_FLAGS_BANNED),
837 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
838 !!(node->flags&NODE_FLAGS_UNHEALTHY),
839 !!(node->flags&NODE_FLAGS_STOPPED),
840 !!(node->flags&NODE_FLAGS_INACTIVE),
841 is_partially_online(node) ? 1 : 0,
842 (node->pnn == mypnn)?'Y':'N');
847 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
849 printf("pnn:%d %-16s %s%s\n", node->pnn,
850 ctdb_addr_to_str(&node->addr),
851 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
852 node->pnn == mypnn?" (THIS NODE)":"");
858 display remote ctdb status
860 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
863 struct ctdb_vnn_map *vnnmap=NULL;
864 struct ctdb_node_map *nodemap=NULL;
865 uint32_t recmode, recmaster, mypnn;
867 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
871 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
872 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
876 if (options.machinereadable) {
877 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
878 ":Inactive:PartiallyOnline:ThisNode:\n");
879 for (i=0;i<nodemap->num;i++) {
880 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
883 (void) control_status_1_machine(mypnn,
889 printf("Number of nodes:%d\n", nodemap->num);
890 for(i=0;i<nodemap->num;i++){
891 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
894 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
897 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
898 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
901 if (vnnmap->generation == INVALID_GENERATION) {
902 printf("Generation:INVALID\n");
904 printf("Generation:%d\n",vnnmap->generation);
906 printf("Size:%d\n",vnnmap->size);
907 for(i=0;i<vnnmap->size;i++){
908 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
910 ctdb_free_vnnmap(vnnmap);
912 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
913 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
916 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
918 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
919 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
922 printf("Recovery master:%d\n",recmaster);
927 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
930 struct ctdb_node_map *nodemap=NULL;
932 uint32_t pnn_mode, mypnn;
938 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
939 options.pnn, true, &nodes, &pnn_mode)) {
943 if (options.machinereadable) {
944 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
945 ":Inactive:PartiallyOnline:ThisNode:\n");
946 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
947 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
950 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
951 DEBUG(DEBUG_ERR, ("Unable to get PNN from local node\n"));
955 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
956 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
962 for (i = 0; i < talloc_array_length(nodes); i++) {
963 if (options.machinereadable) {
964 ret |= control_status_1_machine(mypnn,
965 &nodemap->nodes[nodes[i]]);
967 ret |= control_status_1_human(mypnn,
968 &nodemap->nodes[nodes[i]]);
975 struct natgw_node *next;
980 display the list of nodes belonging to this natgw configuration
982 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
985 uint32_t capabilities;
986 const char *natgw_list;
989 struct natgw_node *natgw_nodes = NULL;
990 struct natgw_node *natgw_node;
991 struct ctdb_node_map *nodemap=NULL;
994 /* read the natgw nodes file into a linked list */
995 natgw_list = getenv("NATGW_NODES");
996 if (natgw_list == NULL) {
997 natgw_list = "/etc/ctdb/natgw_nodes";
999 lines = file_lines_load(natgw_list, &nlines, ctdb);
1000 if (lines == NULL) {
1001 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1004 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
1007 for (i=0;i<nlines;i++) {
1011 /* strip leading spaces */
1012 while((*node == ' ') || (*node == '\t')) {
1018 if (strcmp(node, "") == 0) {
1021 natgw_node = talloc(ctdb, struct natgw_node);
1022 natgw_node->addr = talloc_strdup(natgw_node, node);
1023 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1024 natgw_node->next = natgw_nodes;
1025 natgw_nodes = natgw_node;
1028 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1030 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1035 while(i<nodemap->num) {
1036 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1037 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1042 /* this node was not in the natgw so we just remove it from
1045 if ((natgw_node == NULL)
1046 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1049 for (j=i+1; j<nodemap->num; j++) {
1050 nodemap->nodes[j-1] = nodemap->nodes[j];
1059 /* pick a node to be natgwmaster
1060 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
1062 for(i=0;i<nodemap->num;i++){
1063 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
1064 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1066 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1069 if (!(capabilities&CTDB_CAP_NATGW)) {
1072 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1076 /* we couldnt find any healthy node, try unhealthy ones */
1077 if (i == nodemap->num) {
1078 for(i=0;i<nodemap->num;i++){
1079 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
1080 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1082 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1085 if (!(capabilities&CTDB_CAP_NATGW)) {
1088 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1093 /* unless all nodes are STOPPED, when we pick one anyway */
1094 if (i == nodemap->num) {
1095 for(i=0;i<nodemap->num;i++){
1096 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
1097 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1099 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1102 if (!(capabilities&CTDB_CAP_NATGW)) {
1105 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
1109 /* or if we still can not find any */
1110 if (i == nodemap->num) {
1111 printf("-1 0.0.0.0\n");
1112 ret = 2; /* matches ENOENT */
1116 /* print the pruned list of nodes belonging to this natgw list */
1117 for(i=0;i<nodemap->num;i++){
1118 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1121 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
1122 ctdb_addr_to_str(&nodemap->nodes[i].addr),
1123 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
1124 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
1125 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
1126 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
1127 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
1134 display the status of the scripts for monitoring (or other events)
1136 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1137 enum ctdb_eventscript_call type)
1139 struct ctdb_scripts_wire *script_status;
1142 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1144 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1148 if (script_status == NULL) {
1149 if (!options.machinereadable) {
1150 printf("%s cycle never run\n",
1151 ctdb_eventscript_call_names[type]);
1156 if (!options.machinereadable) {
1157 printf("%d scripts were executed last %s cycle\n",
1158 script_status->num_scripts,
1159 ctdb_eventscript_call_names[type]);
1161 for (i=0; i<script_status->num_scripts; i++) {
1162 const char *status = NULL;
1164 switch (script_status->scripts[i].status) {
1166 status = "TIMEDOUT";
1169 status = "DISABLED";
1175 if (script_status->scripts[i].status > 0)
1179 if (options.machinereadable) {
1180 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1181 ctdb_eventscript_call_names[type],
1182 script_status->scripts[i].name,
1183 script_status->scripts[i].status,
1185 (long)script_status->scripts[i].start.tv_sec,
1186 (long)script_status->scripts[i].start.tv_usec,
1187 (long)script_status->scripts[i].finished.tv_sec,
1188 (long)script_status->scripts[i].finished.tv_usec,
1189 script_status->scripts[i].output);
1193 printf("%-20s Status:%s ",
1194 script_status->scripts[i].name, status);
1196 /* Some other error, eg from stat. */
1197 printf("%-20s Status:CANNOT RUN (%s)",
1198 script_status->scripts[i].name,
1199 strerror(-script_status->scripts[i].status));
1201 if (script_status->scripts[i].status >= 0) {
1202 printf("Duration:%.3lf ",
1203 timeval_delta(&script_status->scripts[i].finished,
1204 &script_status->scripts[i].start));
1206 if (script_status->scripts[i].status != -ENOEXEC) {
1208 ctime(&script_status->scripts[i].start.tv_sec));
1209 if (script_status->scripts[i].status != 0) {
1210 printf(" OUTPUT:%s\n",
1211 script_status->scripts[i].output);
1221 static int control_scriptstatus(struct ctdb_context *ctdb,
1222 int argc, const char **argv)
1225 enum ctdb_eventscript_call type, min, max;
1229 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1234 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1238 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1239 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1245 if (type == CTDB_EVENT_MAX) {
1246 if (strcmp(arg, "all") == 0) {
1248 max = CTDB_EVENT_MAX;
1250 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1255 if (options.machinereadable) {
1256 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1259 for (type = min; type < max; type++) {
1260 ret = control_one_scriptstatus(ctdb, type);
1270 enable an eventscript
1272 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1280 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1282 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1290 disable an eventscript
1292 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1300 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1302 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1310 display the pnn of the recovery master
1312 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1316 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1317 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1320 printf("%d\n",recmaster);
1326 add a tickle to a public address
1328 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1330 struct ctdb_tcp_connection t;
1338 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1339 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1342 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1343 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1347 data.dptr = (uint8_t *)&t;
1348 data.dsize = sizeof(t);
1350 /* tell all nodes about this tcp connection */
1351 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1352 0, data, ctdb, NULL, NULL, NULL, NULL);
1354 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1363 delete a tickle from a node
1365 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1367 struct ctdb_tcp_connection t;
1375 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1376 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1379 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1380 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1384 data.dptr = (uint8_t *)&t;
1385 data.dsize = sizeof(t);
1387 /* tell all nodes about this tcp connection */
1388 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1389 0, data, ctdb, NULL, NULL, NULL, NULL);
1391 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1400 get a list of all tickles for this pnn
1402 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1404 struct ctdb_control_tcp_tickle_list *list;
1405 ctdb_sock_addr addr;
1414 port = atoi(argv[1]);
1417 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1418 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1422 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1424 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1428 if (options.machinereadable){
1429 printf(":source ip:port:destination ip:port:\n");
1430 for (i=0;i<list->tickles.num;i++) {
1431 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1434 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1435 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1438 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1439 printf("Num tickles:%u\n", list->tickles.num);
1440 for (i=0;i<list->tickles.num;i++) {
1441 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1444 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1445 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));
1455 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1457 struct ctdb_all_public_ips *ips;
1458 struct ctdb_public_ip ip;
1461 uint32_t disable_time;
1463 struct ctdb_node_map *nodemap=NULL;
1464 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1467 data.dptr = (uint8_t*)&disable_time;
1468 data.dsize = sizeof(disable_time);
1469 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1471 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1477 /* read the public ip list from the node */
1478 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1480 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1481 talloc_free(tmp_ctx);
1485 for (i=0;i<ips->num;i++) {
1486 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1491 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1492 pnn, ctdb_addr_to_str(addr)));
1493 talloc_free(tmp_ctx);
1500 data.dptr = (uint8_t *)&ip;
1501 data.dsize = sizeof(ip);
1503 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1505 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1506 talloc_free(tmp_ctx);
1510 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1511 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1518 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1519 talloc_free(tmp_ctx);
1523 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1525 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1526 talloc_free(tmp_ctx);
1530 /* update the recovery daemon so it now knows to expect the new
1531 node assignment for this ip.
1533 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1535 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1539 talloc_free(tmp_ctx);
1544 move/failover an ip address to a specific node
1546 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1549 int ret, retries = 0;
1550 ctdb_sock_addr addr;
1557 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1558 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1563 if (sscanf(argv[1], "%u", &pnn) != 1) {
1564 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1569 ret = move_ip(ctdb, &addr, pnn);
1571 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1575 } while (retries < 5 && ret != 0);
1577 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1584 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1589 if (ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), pnn, &recmaster) != 0) {
1590 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", pnn));
1594 data.dptr = (uint8_t *)&pnn;
1595 data.dsize = sizeof(uint32_t);
1596 if (ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1597 DEBUG(DEBUG_ERR,("Failed to send message to force node reallocation\n"));
1606 rebalance a node by setting it to allow failback and triggering a
1609 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1611 switch (options.pnn) {
1612 case CTDB_BROADCAST_ALL:
1613 case CTDB_CURRENT_NODE:
1614 DEBUG(DEBUG_ERR,("You must specify a node number with -n <pnn> for the node to rebalance\n"));
1618 return rebalance_node(ctdb, options.pnn);
1622 static int getips_store_callback(void *param, void *data)
1624 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1625 struct ctdb_all_public_ips *ips = param;
1629 ips->ips[i].pnn = node_ip->pnn;
1630 ips->ips[i].addr = node_ip->addr;
1634 static int getips_count_callback(void *param, void *data)
1636 uint32_t *count = param;
1643 static uint32_t *ip_key(ctdb_sock_addr *ip)
1645 static uint32_t key[IP_KEYLEN];
1647 bzero(key, sizeof(key));
1649 switch (ip->sa.sa_family) {
1651 key[0] = ip->ip.sin_addr.s_addr;
1654 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1662 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1669 static void *add_ip_callback(void *parm, void *data)
1675 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1677 struct ctdb_all_public_ips *tmp_ips;
1678 struct ctdb_node_map *nodemap=NULL;
1679 trbt_tree_t *ip_tree;
1683 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1685 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1689 ip_tree = trbt_create(tmp_ctx, 0);
1691 for(i=0;i<nodemap->num;i++){
1692 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1695 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1699 /* read the public ip list from this node */
1700 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1702 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1706 for (j=0; j<tmp_ips->num;j++) {
1707 struct ctdb_public_ip *node_ip;
1709 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1710 node_ip->pnn = tmp_ips->ips[j].pnn;
1711 node_ip->addr = tmp_ips->ips[j].addr;
1713 trbt_insertarray32_callback(ip_tree,
1714 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1718 talloc_free(tmp_ips);
1723 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1725 len = offsetof(struct ctdb_all_public_ips, ips) +
1726 count*sizeof(struct ctdb_public_ip);
1727 tmp_ips = talloc_zero_size(tmp_ctx, len);
1728 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1737 * scans all other nodes and returns a pnn for another node that can host this
1741 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1743 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1744 struct ctdb_all_public_ips *ips;
1745 struct ctdb_node_map *nodemap=NULL;
1748 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1750 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1751 talloc_free(tmp_ctx);
1755 for(i=0;i<nodemap->num;i++){
1756 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1759 if (nodemap->nodes[i].pnn == options.pnn) {
1763 /* read the public ip list from this node */
1764 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1766 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1770 for (j=0;j<ips->num;j++) {
1771 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1772 talloc_free(tmp_ctx);
1773 return nodemap->nodes[i].pnn;
1779 talloc_free(tmp_ctx);
1783 static uint32_t ipreallocate_finished;
1786 handler for receiving the response to ipreallocate
1788 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1789 TDB_DATA data, void *private_data)
1791 ipreallocate_finished = 1;
1794 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1796 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1798 event_add_timed(ctdb->ev, ctdb,
1799 timeval_current_ofs(1, 0),
1800 ctdb_every_second, ctdb);
1804 ask the recovery daemon on the recovery master to perform a ip reallocation
1806 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1810 struct takeover_run_reply rd;
1812 struct ctdb_node_map *nodemap=NULL;
1814 struct timeval tv = timeval_current();
1816 /* we need some events to trigger so we can timeout and restart
1819 event_add_timed(ctdb->ev, ctdb,
1820 timeval_current_ofs(1, 0),
1821 ctdb_every_second, ctdb);
1823 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1825 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1828 rd.srvid = getpid();
1830 /* register a message port for receiveing the reply so that we
1831 can receive the reply
1833 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1835 data.dptr = (uint8_t *)&rd;
1836 data.dsize = sizeof(rd);
1839 /* check that there are valid nodes available */
1840 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1841 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1844 for (i=0; i<nodemap->num;i++) {
1845 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1849 if (i==nodemap->num) {
1850 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1855 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1856 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1860 /* verify the node exists */
1861 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1862 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1867 /* check tha there are nodes available that can act as a recmaster */
1868 for (i=0; i<nodemap->num; i++) {
1869 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1874 if (i == nodemap->num) {
1875 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1879 /* verify the recovery master is not STOPPED, nor BANNED */
1880 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1881 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1887 /* verify the recovery master is not STOPPED, nor BANNED */
1888 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1889 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1895 ipreallocate_finished = 0;
1896 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1898 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1902 tv = timeval_current();
1903 /* this loop will terminate when we have received the reply */
1904 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1905 event_loop_once(ctdb->ev);
1907 if (ipreallocate_finished == 1) {
1920 add a public ip address to a node
1922 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1925 int len, retries = 0;
1927 ctdb_sock_addr addr;
1928 struct ctdb_control_ip_iface *pub;
1929 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1930 struct ctdb_all_public_ips *ips;
1934 talloc_free(tmp_ctx);
1938 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1939 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1940 talloc_free(tmp_ctx);
1944 /* read the public ip list from the node */
1945 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1947 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1948 talloc_free(tmp_ctx);
1951 for (i=0;i<ips->num;i++) {
1952 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1953 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1960 /* Dont timeout. This command waits for an ip reallocation
1961 which sometimes can take wuite a while if there has
1962 been a recent recovery
1966 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1967 pub = talloc_size(tmp_ctx, len);
1968 CTDB_NO_MEMORY(ctdb, pub);
1972 pub->len = strlen(argv[1])+1;
1973 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1976 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1978 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1982 } while (retries < 5 && ret != 0);
1984 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1985 talloc_free(tmp_ctx);
1989 if (rebalance_node(ctdb, options.pnn) != 0) {
1990 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
1994 talloc_free(tmp_ctx);
1998 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2000 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2002 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2003 struct ctdb_node_map *nodemap=NULL;
2004 struct ctdb_all_public_ips *ips;
2007 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2009 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2013 /* remove it from the nodes that are not hosting the ip currently */
2014 for(i=0;i<nodemap->num;i++){
2015 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2018 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2019 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2023 for (j=0;j<ips->num;j++) {
2024 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2032 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2036 options.pnn = nodemap->nodes[i].pnn;
2037 control_delip(ctdb, argc, argv);
2041 /* remove it from every node (also the one hosting it) */
2042 for(i=0;i<nodemap->num;i++){
2043 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2046 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
2047 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2051 for (j=0;j<ips->num;j++) {
2052 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2060 options.pnn = nodemap->nodes[i].pnn;
2061 control_delip(ctdb, argc, argv);
2064 talloc_free(tmp_ctx);
2069 delete a public ip address from a node
2071 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2075 ctdb_sock_addr addr;
2076 struct ctdb_control_ip_iface pub;
2077 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2078 struct ctdb_all_public_ips *ips;
2081 talloc_free(tmp_ctx);
2085 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2086 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2090 if (options.pnn == CTDB_BROADCAST_ALL) {
2091 return control_delip_all(ctdb, argc, argv, &addr);
2098 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2100 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2101 talloc_free(tmp_ctx);
2105 for (i=0;i<ips->num;i++) {
2106 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2112 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2113 ctdb_addr_to_str(&addr)));
2114 talloc_free(tmp_ctx);
2118 if (ips->ips[i].pnn == options.pnn) {
2119 ret = find_other_host_for_public_ip(ctdb, &addr);
2122 ret = move_ip(ctdb, &addr, ret);
2124 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
2128 } while (retries < 5 && ret != 0);
2130 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
2136 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2138 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2139 talloc_free(tmp_ctx);
2143 talloc_free(tmp_ctx);
2148 kill a tcp connection
2150 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2153 struct ctdb_control_killtcp killtcp;
2159 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2160 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2164 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2165 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2169 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2171 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2182 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2185 ctdb_sock_addr addr;
2191 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2192 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2196 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2198 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2206 register a server id
2208 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2211 struct ctdb_server_id server_id;
2217 server_id.pnn = strtoul(argv[0], NULL, 0);
2218 server_id.type = strtoul(argv[1], NULL, 0);
2219 server_id.server_id = strtoul(argv[2], NULL, 0);
2221 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2223 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2226 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2232 unregister a server id
2234 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2237 struct ctdb_server_id server_id;
2243 server_id.pnn = strtoul(argv[0], NULL, 0);
2244 server_id.type = strtoul(argv[1], NULL, 0);
2245 server_id.server_id = strtoul(argv[2], NULL, 0);
2247 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2249 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2256 check if a server id exists
2258 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2262 struct ctdb_server_id server_id;
2268 server_id.pnn = strtoul(argv[0], NULL, 0);
2269 server_id.type = strtoul(argv[1], NULL, 0);
2270 server_id.server_id = strtoul(argv[2], NULL, 0);
2272 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2274 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2279 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2281 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2287 get a list of all server ids that are registered on a node
2289 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2292 struct ctdb_server_id_list *server_ids;
2294 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2296 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2300 for (i=0; i<server_ids->num; i++) {
2301 printf("Server id %d:%d:%d\n",
2302 server_ids->server_ids[i].pnn,
2303 server_ids->server_ids[i].type,
2304 server_ids->server_ids[i].server_id);
2311 check if a server id exists
2313 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2315 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2321 talloc_free(tmp_ctx);
2325 ids = talloc_array(tmp_ctx, uint64_t, argc);
2326 result = talloc_array(tmp_ctx, uint8_t, argc);
2328 for (i = 0; i < argc; i++) {
2329 ids[i] = strtoull(argv[i], NULL, 0);
2332 if (!ctdb_check_message_handlers(ctdb_connection,
2333 options.pnn, argc, ids, result)) {
2334 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2336 talloc_free(tmp_ctx);
2340 for (i=0; i < argc; i++) {
2341 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2342 result[i] ? "exists" : "does not exist");
2345 talloc_free(tmp_ctx);
2350 send a tcp tickle ack
2352 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2355 ctdb_sock_addr src, dst;
2361 if (!parse_ip_port(argv[0], &src)) {
2362 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2366 if (!parse_ip_port(argv[1], &dst)) {
2367 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2371 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2375 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2382 display public ip status
2384 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2387 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2388 struct ctdb_all_public_ips *ips;
2390 if (options.pnn == CTDB_BROADCAST_ALL) {
2391 /* read the list of public ips from all nodes */
2392 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2394 /* read the public ip list from this node */
2395 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2398 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2399 talloc_free(tmp_ctx);
2403 if (options.machinereadable){
2404 printf(":Public IP:Node:");
2405 if (options.verbose){
2406 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2410 if (options.pnn == CTDB_BROADCAST_ALL) {
2411 printf("Public IPs on ALL nodes\n");
2413 printf("Public IPs on node %u\n", options.pnn);
2417 for (i=1;i<=ips->num;i++) {
2418 struct ctdb_control_public_ip_info *info = NULL;
2420 char *aciface = NULL;
2421 char *avifaces = NULL;
2422 char *cifaces = NULL;
2424 if (options.pnn == CTDB_BROADCAST_ALL) {
2425 pnn = ips->ips[ips->num-i].pnn;
2431 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2432 &ips->ips[ips->num-i].addr, &info);
2439 for (j=0; j < info->num; j++) {
2440 if (cifaces == NULL) {
2441 cifaces = talloc_strdup(info,
2442 info->ifaces[j].name);
2444 cifaces = talloc_asprintf_append(cifaces,
2446 info->ifaces[j].name);
2449 if (info->active_idx == j) {
2450 aciface = info->ifaces[j].name;
2453 if (info->ifaces[j].link_state == 0) {
2457 if (avifaces == NULL) {
2458 avifaces = talloc_strdup(info, info->ifaces[j].name);
2460 avifaces = talloc_asprintf_append(avifaces,
2462 info->ifaces[j].name);
2467 if (options.machinereadable){
2469 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2470 ips->ips[ips->num-i].pnn);
2471 if (options.verbose){
2474 avifaces?avifaces:"",
2475 cifaces?cifaces:"");
2479 if (options.verbose) {
2480 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2481 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2482 ips->ips[ips->num-i].pnn,
2484 avifaces?avifaces:"",
2485 cifaces?cifaces:"");
2488 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2489 ips->ips[ips->num-i].pnn);
2495 talloc_free(tmp_ctx);
2502 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2505 ctdb_sock_addr addr;
2506 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2507 struct ctdb_control_public_ip_info *info;
2510 talloc_free(tmp_ctx);
2514 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2515 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2519 /* read the public ip info from this node */
2520 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2521 tmp_ctx, &addr, &info);
2523 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2524 argv[0], options.pnn));
2525 talloc_free(tmp_ctx);
2529 printf("Public IP[%s] info on node %u\n",
2530 ctdb_addr_to_str(&info->ip.addr),
2533 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2534 ctdb_addr_to_str(&info->ip.addr),
2535 info->ip.pnn, info->num);
2537 for (i=0; i<info->num; i++) {
2538 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2540 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2541 i+1, info->ifaces[i].name,
2542 info->ifaces[i].link_state?"up":"down",
2543 (unsigned int)info->ifaces[i].references,
2544 (i==info->active_idx)?" (active)":"");
2547 talloc_free(tmp_ctx);
2552 display interfaces status
2554 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2557 struct ctdb_ifaces_list *ifaces;
2559 /* read the public ip list from this node */
2560 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2561 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2566 if (options.machinereadable){
2567 printf(":Name:LinkStatus:References:\n");
2569 printf("Interfaces on node %u\n", options.pnn);
2572 for (i=0; i<ifaces->num; i++) {
2573 if (options.machinereadable){
2574 printf(":%s:%s:%u\n",
2575 ifaces->ifaces[i].name,
2576 ifaces->ifaces[i].link_state?"1":"0",
2577 (unsigned int)ifaces->ifaces[i].references);
2579 printf("name:%s link:%s references:%u\n",
2580 ifaces->ifaces[i].name,
2581 ifaces->ifaces[i].link_state?"up":"down",
2582 (unsigned int)ifaces->ifaces[i].references);
2586 ctdb_free_ifaces(ifaces);
2592 set link status of an interface
2594 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2597 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2598 struct ctdb_control_iface_info info;
2606 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2607 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2609 talloc_free(tmp_ctx);
2612 strcpy(info.name, argv[0]);
2614 if (strcmp(argv[1], "up") == 0) {
2615 info.link_state = 1;
2616 } else if (strcmp(argv[1], "down") == 0) {
2617 info.link_state = 0;
2619 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2621 talloc_free(tmp_ctx);
2625 /* read the public ip list from this node */
2626 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2629 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2630 argv[0], options.pnn));
2631 talloc_free(tmp_ctx);
2635 talloc_free(tmp_ctx);
2640 display pid of a ctdb daemon
2642 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2647 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2649 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2652 printf("Pid:%d\n", pid);
2658 disable a remote node
2660 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2663 struct ctdb_node_map *nodemap=NULL;
2665 /* check if the node is already disabled */
2666 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2667 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2670 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2671 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2676 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2678 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2684 /* read the nodemap and verify the change took effect */
2685 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2686 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2690 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2691 ret = control_ipreallocate(ctdb, argc, argv);
2693 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2701 enable a disabled remote node
2703 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2707 struct ctdb_node_map *nodemap=NULL;
2710 /* check if the node is already enabled */
2711 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2712 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2715 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2716 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2721 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2723 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2729 /* read the nodemap and verify the change took effect */
2730 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2731 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2735 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2737 ret = control_ipreallocate(ctdb, argc, argv);
2739 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2749 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2752 struct ctdb_node_map *nodemap=NULL;
2755 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2757 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2762 /* read the nodemap and verify the change took effect */
2763 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2764 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2768 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2769 ret = control_ipreallocate(ctdb, argc, argv);
2771 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2779 restart a stopped remote node
2781 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2785 struct ctdb_node_map *nodemap=NULL;
2788 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2790 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2796 /* read the nodemap and verify the change took effect */
2797 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2798 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2802 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2803 ret = control_ipreallocate(ctdb, argc, argv);
2805 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2812 static uint32_t get_generation(struct ctdb_context *ctdb)
2814 struct ctdb_vnn_map *vnnmap=NULL;
2817 /* wait until the recmaster is not in recovery mode */
2819 uint32_t recmode, recmaster;
2821 if (vnnmap != NULL) {
2822 talloc_free(vnnmap);
2826 /* get the recmaster */
2827 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2828 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2832 /* get recovery mode */
2833 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2834 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2838 /* get the current generation number */
2839 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2841 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2845 if ((recmode == CTDB_RECOVERY_NORMAL)
2846 && (vnnmap->generation != 1)){
2847 return vnnmap->generation;
2854 ban a node from the cluster
2856 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2859 struct ctdb_node_map *nodemap=NULL;
2860 struct ctdb_ban_time bantime;
2866 /* verify the node exists */
2867 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2869 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2873 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2874 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2878 bantime.pnn = options.pnn;
2879 bantime.time = strtoul(argv[0], NULL, 0);
2881 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2883 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2887 ret = control_ipreallocate(ctdb, argc, argv);
2889 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2898 unban a node from the cluster
2900 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2903 struct ctdb_node_map *nodemap=NULL;
2904 struct ctdb_ban_time bantime;
2906 /* verify the node exists */
2907 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2909 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2913 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2914 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2918 bantime.pnn = options.pnn;
2921 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2923 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2927 ret = control_ipreallocate(ctdb, argc, argv);
2929 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2938 show ban information for a node
2940 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2943 struct ctdb_node_map *nodemap=NULL;
2944 struct ctdb_ban_time *bantime;
2946 /* verify the node exists */
2947 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2949 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2953 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2955 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2959 if (bantime->time == 0) {
2960 printf("Node %u is not banned\n", bantime->pnn);
2962 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2971 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2975 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2977 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2987 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2990 uint32_t generation, next_generation;
2992 /* record the current generation number */
2993 generation = get_generation(ctdb);
2995 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2997 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3001 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3003 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3007 /* wait until we are in a new generation */
3009 next_generation = get_generation(ctdb);
3010 if (next_generation != generation) {
3021 display monitoring mode of a remote node
3023 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3028 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3030 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3033 if (!options.machinereadable){
3034 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3037 printf(":%d:\n",monmode);
3044 display capabilities of a remote node
3046 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3048 uint32_t capabilities;
3051 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3053 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3057 if (!options.machinereadable){
3058 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3059 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3060 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3061 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3063 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3064 printf(":%d:%d:%d:%d:\n",
3065 !!(capabilities&CTDB_CAP_RECMASTER),
3066 !!(capabilities&CTDB_CAP_LMASTER),
3067 !!(capabilities&CTDB_CAP_LVS),
3068 !!(capabilities&CTDB_CAP_NATGW));
3074 display lvs configuration
3076 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3078 uint32_t *capabilities;
3079 struct ctdb_node_map *nodemap=NULL;
3081 int healthy_count = 0;
3083 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
3085 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3089 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3090 CTDB_NO_MEMORY(ctdb, capabilities);
3092 /* collect capabilities for all connected nodes */
3093 for (i=0; i<nodemap->num; i++) {
3094 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3097 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3101 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3103 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3107 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3111 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3116 /* Print all LVS nodes */
3117 for (i=0; i<nodemap->num; i++) {
3118 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3121 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3124 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3128 if (healthy_count != 0) {
3129 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3134 printf("%d:%s\n", i,
3135 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3142 display who is the lvs master
3144 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3146 uint32_t *capabilities;
3147 struct ctdb_node_map *nodemap=NULL;
3149 int healthy_count = 0;
3151 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
3153 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3157 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3158 CTDB_NO_MEMORY(ctdb, capabilities);
3160 /* collect capabilities for all connected nodes */
3161 for (i=0; i<nodemap->num; i++) {
3162 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3165 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3169 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3171 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3175 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3179 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3184 /* find and show the lvsmaster */
3185 for (i=0; i<nodemap->num; i++) {
3186 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3189 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3192 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3196 if (healthy_count != 0) {
3197 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3202 if (options.machinereadable){
3205 printf("Node %d is LVS master\n", i);
3210 printf("There is no LVS master\n");
3215 disable monitoring on a node
3217 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3222 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3224 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3227 printf("Monitoring mode:%s\n","DISABLED");
3233 enable monitoring on a node
3235 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3240 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3242 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3245 printf("Monitoring mode:%s\n","ACTIVE");
3251 display remote list of keys/data for a db
3253 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3255 const char *db_name;
3256 struct ctdb_db_context *ctdb_db;
3259 struct ctdb_dump_db_context c;
3268 if (db_exists(ctdb, db_name, &persistent)) {
3269 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3273 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3275 if (ctdb_db == NULL) {
3276 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3280 if (options.printlmaster) {
3281 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3282 ctdb, &ctdb->vnn_map);
3284 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3292 c.printemptyrecords = (bool)options.printemptyrecords;
3293 c.printdatasize = (bool)options.printdatasize;
3294 c.printlmaster = (bool)options.printlmaster;
3295 c.printhash = (bool)options.printhash;
3296 c.printrecordflags = (bool)options.printrecordflags;
3298 /* traverse and dump the cluster tdb */
3299 ret = ctdb_dump_db(ctdb_db, &c);
3301 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3302 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3303 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3307 talloc_free(ctdb_db);
3309 printf("Dumped %d records\n", ret);
3313 struct cattdb_data {
3314 struct ctdb_context *ctdb;
3318 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3320 struct cattdb_data *d = private_data;
3321 struct ctdb_dump_db_context c;
3327 c.printemptyrecords = (bool)options.printemptyrecords;
3328 c.printdatasize = (bool)options.printdatasize;
3329 c.printlmaster = false;
3330 c.printhash = (bool)options.printhash;
3331 c.printrecordflags = true;
3333 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3337 cat the local tdb database using same format as catdb
3339 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3341 const char *db_name;
3342 struct ctdb_db_context *ctdb_db;
3343 struct cattdb_data d;
3353 if (db_exists(ctdb, db_name, &persistent)) {
3354 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3358 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, false, 0);
3360 if (ctdb_db == NULL) {
3361 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3365 /* traverse the local tdb */
3368 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3369 printf("Failed to cattdb data\n");
3372 talloc_free(ctdb_db);
3374 printf("Dumped %d records\n", d.count);
3379 display the content of a database key
3381 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3383 const char *db_name;
3384 struct ctdb_db_context *ctdb_db;
3385 struct ctdb_record_handle *h;
3386 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3396 if (db_exists(ctdb, db_name, &persistent)) {
3397 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3401 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3403 if (ctdb_db == NULL) {
3404 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3408 key.dptr = discard_const(argv[1]);
3409 key.dsize = strlen((char *)key.dptr);
3411 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3413 printf("Failed to fetch record '%s' on node %d\n",
3414 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3415 talloc_free(tmp_ctx);
3419 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3421 talloc_free(ctdb_db);
3422 talloc_free(tmp_ctx);
3427 display the content of a database key
3429 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3431 const char *db_name;
3432 struct ctdb_db_context *ctdb_db;
3433 struct ctdb_record_handle *h;
3434 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3444 if (db_exists(ctdb, db_name, &persistent)) {
3445 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3449 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3451 if (ctdb_db == NULL) {
3452 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3456 key.dptr = discard_const(argv[1]);
3457 key.dsize = strlen((char *)key.dptr);
3459 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3461 printf("Failed to fetch record '%s' on node %d\n",
3462 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3463 talloc_free(tmp_ctx);
3467 data.dptr = discard_const(argv[2]);
3468 data.dsize = strlen((char *)data.dptr);
3470 if (ctdb_record_store(h, data) != 0) {
3471 printf("Failed to store record\n");
3475 talloc_free(ctdb_db);
3476 talloc_free(tmp_ctx);
3481 fetch a record from a persistent database
3483 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3485 const char *db_name;
3486 struct ctdb_db_context *ctdb_db;
3487 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3488 struct ctdb_transaction_handle *h;
3494 talloc_free(tmp_ctx);
3501 if (db_exists(ctdb, db_name, &persistent)) {
3502 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3503 talloc_free(tmp_ctx);
3508 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3509 talloc_free(tmp_ctx);
3513 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3515 if (ctdb_db == NULL) {
3516 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3517 talloc_free(tmp_ctx);
3521 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3523 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3524 talloc_free(tmp_ctx);
3528 key.dptr = discard_const(argv[1]);
3529 key.dsize = strlen(argv[1]);
3530 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3532 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3533 talloc_free(tmp_ctx);
3537 if (data.dsize == 0 || data.dptr == NULL) {
3538 DEBUG(DEBUG_ERR,("Record is empty\n"));
3539 talloc_free(tmp_ctx);
3544 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3546 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3547 talloc_free(tmp_ctx);
3550 write(fd, data.dptr, data.dsize);
3553 write(1, data.dptr, data.dsize);
3556 /* abort the transaction */
3560 talloc_free(tmp_ctx);
3565 fetch a record from a tdb-file
3567 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3569 const char *tdb_file;
3572 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3581 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3583 printf("Failed to open TDB file %s\n", tdb_file);
3587 if (!strncmp(argv[1], "0x", 2)) {
3588 key = hextodata(tmp_ctx, argv[1] + 2);
3589 if (key.dsize == 0) {
3590 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3594 key.dptr = discard_const(argv[1]);
3595 key.dsize = strlen(argv[1]);
3598 data = tdb_fetch(tdb, key);
3599 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3600 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3608 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3610 printf("Failed to open output file %s\n", argv[2]);
3613 if (options.verbose){
3614 write(fd, data.dptr, data.dsize);
3616 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3620 if (options.verbose){
3621 write(1, data.dptr, data.dsize);
3623 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3627 talloc_free(tmp_ctx);
3632 store a record and header to a tdb-file
3634 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3636 const char *tdb_file;
3639 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3647 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3649 printf("Failed to open TDB file %s\n", tdb_file);
3653 if (!strncmp(argv[1], "0x", 2)) {
3654 key = hextodata(tmp_ctx, argv[1] + 2);
3655 if (key.dsize == 0) {
3656 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3660 key.dptr = discard_const(argv[1]);
3661 key.dsize = strlen(argv[1]);
3664 if (!strncmp(argv[2], "0x", 2)) {
3665 data = hextodata(tmp_ctx, argv[2] + 2);
3666 if (data.dsize == 0) {
3667 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3671 data.dptr = discard_const(argv[2]);
3672 data.dsize = strlen(argv[2]);
3675 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
3676 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
3679 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3680 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3687 talloc_free(tmp_ctx);
3692 write a record to a persistent database
3694 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3696 const char *db_name;
3697 struct ctdb_db_context *ctdb_db;
3698 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3699 struct ctdb_transaction_handle *h;
3705 talloc_free(tmp_ctx);
3709 fd = open(argv[2], O_RDONLY);
3711 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3712 talloc_free(tmp_ctx);
3716 ret = fstat(fd, &st);
3718 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3720 talloc_free(tmp_ctx);
3724 if (!S_ISREG(st.st_mode)) {
3725 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3727 talloc_free(tmp_ctx);
3731 data.dsize = st.st_size;
3732 if (data.dsize == 0) {
3735 data.dptr = talloc_size(tmp_ctx, data.dsize);
3736 if (data.dptr == NULL) {
3737 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3739 talloc_free(tmp_ctx);
3742 ret = read(fd, data.dptr, data.dsize);
3743 if (ret != data.dsize) {
3744 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3746 talloc_free(tmp_ctx);
3755 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3756 if (ctdb_db == NULL) {
3757 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3758 talloc_free(tmp_ctx);
3762 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3764 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3765 talloc_free(tmp_ctx);
3769 key.dptr = discard_const(argv[1]);
3770 key.dsize = strlen(argv[1]);
3771 ret = ctdb_transaction_store(h, key, data);
3773 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3774 talloc_free(tmp_ctx);
3778 ret = ctdb_transaction_commit(h);
3780 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3781 talloc_free(tmp_ctx);
3786 talloc_free(tmp_ctx);
3791 check if a service is bound to a port or not
3793 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3798 struct sockaddr_in sin;
3801 printf("Use: ctdb chktcport <port>\n");
3805 port = atoi(argv[0]);
3807 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3809 printf("Failed to open local socket\n");
3813 v = fcntl(s, F_GETFL, 0);
3814 fcntl(s, F_SETFL, v | O_NONBLOCK);
3816 bzero(&sin, sizeof(sin));
3817 sin.sin_family = PF_INET;
3818 sin.sin_port = htons(port);
3819 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3822 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3831 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3832 TDB_DATA data, void *private_data)
3834 DEBUG(DEBUG_ERR,("Log data received\n"));
3835 if (data.dsize > 0) {
3836 printf("%s", data.dptr);
3843 display a list of log messages from the in memory ringbuffer
3845 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3849 struct ctdb_get_log_addr log_addr;
3851 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3856 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3857 talloc_free(tmp_ctx);
3861 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3862 log_addr.srvid = getpid();
3863 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3864 log_addr.level = get_debug_by_desc(argv[0]);
3866 log_addr.level = strtol(argv[0], NULL, 0);
3870 data.dptr = (unsigned char *)&log_addr;
3871 data.dsize = sizeof(log_addr);
3873 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3875 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3878 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3880 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3881 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3882 if (ret != 0 || res != 0) {
3883 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3884 talloc_free(tmp_ctx);
3889 tv = timeval_current();
3890 /* this loop will terminate when we have received the reply */
3891 while (timeval_elapsed(&tv) < 3.0) {
3892 event_loop_once(ctdb->ev);
3895 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3897 talloc_free(tmp_ctx);
3902 clear the in memory log area
3904 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3909 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3911 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3912 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3913 if (ret != 0 || res != 0) {
3914 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3915 talloc_free(tmp_ctx);
3919 talloc_free(tmp_ctx);
3926 display a list of the databases on a remote ctdb
3928 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3931 struct ctdb_dbid_map *dbmap=NULL;
3933 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3935 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3939 if(options.machinereadable){
3940 printf(":ID:Name:Path:Persistent:Unhealthy:ReadOnly:\n");
3941 for(i=0;i<dbmap->num;i++){
3948 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3949 dbmap->dbs[i].dbid, ctdb, &path);
3950 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3951 dbmap->dbs[i].dbid, ctdb, &name);
3952 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3953 dbmap->dbs[i].dbid, ctdb, &health);
3954 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3955 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3956 printf(":0x%08X:%s:%s:%d:%d:%d:\n",
3957 dbmap->dbs[i].dbid, name, path,
3958 !!(persistent), !!(health), !!(readonly));
3963 printf("Number of databases:%d\n", dbmap->num);
3964 for(i=0;i<dbmap->num;i++){
3971 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3972 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3973 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3974 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3975 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3976 printf("dbid:0x%08x name:%s path:%s%s%s%s\n",
3977 dbmap->dbs[i].dbid, name, path,
3978 persistent?" PERSISTENT":"",
3979 readonly?" READONLY":"",
3980 health?" UNHEALTHY":"");
3987 display the status of a database on a remote ctdb
3989 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3992 struct ctdb_dbid_map *dbmap=NULL;
3993 const char *db_name;
4001 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4003 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4007 for(i=0;i<dbmap->num;i++){
4014 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4015 if (strcmp(name, db_name) != 0) {
4019 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4020 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4021 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4022 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4023 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nREADONLY: %s\nHEALTH: %s\n",
4024 dbmap->dbs[i].dbid, name, path,
4025 persistent?"yes":"no",
4026 readonly?"yes":"no",
4027 health?health:"OK");
4031 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
4036 check if the local node is recmaster or not
4037 it will return 1 if this node is the recmaster and 0 if it is not
4038 or if the local ctdb daemon could not be contacted
4040 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4042 uint32_t mypnn, recmaster;
4044 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
4046 printf("Failed to get pnn of node\n");
4050 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
4051 printf("Failed to get the recmaster\n");
4055 if (recmaster != mypnn) {
4056 printf("this node is not the recmaster\n");
4060 printf("this node is the recmaster\n");
4067 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4070 struct timeval tv = timeval_current();
4071 ret = ctdb_ctrl_ping(ctdb, options.pnn);
4073 printf("Unable to get ping response from node %u\n", options.pnn);
4076 printf("response from %u time=%.6f sec (%d clients)\n",
4077 options.pnn, timeval_elapsed(&tv), ret);
4086 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4097 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4099 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4103 printf("%-23s = %u\n", name, value);
4110 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4121 value = strtoul(argv[1], NULL, 0);
4123 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4125 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4134 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4140 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4142 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4146 for (i=0;i<count;i++) {
4147 control_getvar(ctdb, 1, &list[i]);
4156 display debug level on a node
4158 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4163 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4165 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4168 if (options.machinereadable){
4169 printf(":Name:Level:\n");
4170 printf(":%s:%d:\n",get_debug_by_level(level),level);
4172 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4179 display reclock file of a node
4181 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4184 const char *reclock;
4186 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4188 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4191 if (options.machinereadable){
4192 if (reclock != NULL) {
4193 printf("%s", reclock);
4196 if (reclock == NULL) {
4197 printf("No reclock file used.\n");
4199 printf("Reclock file:%s\n", reclock);
4207 set the reclock file of a node
4209 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4212 const char *reclock;
4216 } else if (argc == 1) {
4222 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4224 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4231 set the natgw state on/off
4233 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4236 uint32_t natgwstate;
4242 if (!strcmp(argv[0], "on")) {
4244 } else if (!strcmp(argv[0], "off")) {
4250 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4252 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4260 set the lmaster role on/off
4262 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4265 uint32_t lmasterrole;
4271 if (!strcmp(argv[0], "on")) {
4273 } else if (!strcmp(argv[0], "off")) {
4279 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4281 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4289 set the recmaster role on/off
4291 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4294 uint32_t recmasterrole;
4300 if (!strcmp(argv[0], "on")) {
4302 } else if (!strcmp(argv[0], "off")) {
4308 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4310 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4318 set debug level on a node or all nodes
4320 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4326 printf("You must specify the debug level. Valid levels are:\n");
4327 for (i=0; debug_levels[i].description != NULL; i++) {
4328 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4334 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4335 level = get_debug_by_desc(argv[0]);
4337 level = strtol(argv[0], NULL, 0);
4340 for (i=0; debug_levels[i].description != NULL; i++) {
4341 if (level == debug_levels[i].level) {
4345 if (debug_levels[i].description == NULL) {
4346 printf("Invalid debug level, must be one of\n");
4347 for (i=0; debug_levels[i].description != NULL; i++) {
4348 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4353 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4355 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4364 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4370 priority = strtol(argv[0], NULL, 0);
4374 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4376 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4378 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4385 attach to a database
4387 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4389 const char *db_name;
4390 struct ctdb_db_context *ctdb_db;
4391 bool persistent = false;
4401 if (strcmp(argv[1], "persistent") != 0) {
4407 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4408 if (ctdb_db == NULL) {
4409 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4419 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4421 struct ctdb_db_priority db_prio;
4428 db_prio.db_id = strtoul(argv[0], NULL, 0);
4429 db_prio.priority = strtoul(argv[1], NULL, 0);
4431 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4433 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4443 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4445 uint32_t db_id, priority;
4452 db_id = strtoul(argv[0], NULL, 0);
4454 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4456 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4460 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4466 set the readonly capability for a database
4468 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4470 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4472 struct ctdb_dbid_map *dbmap=NULL;
4479 if (!strncmp(argv[0], "0x", 2)) {
4480 db_id = strtoul(argv[0] + 2, NULL, 0);
4482 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4484 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4485 talloc_free(tmp_ctx);
4488 for(i=0;i<dbmap->num;i++){
4491 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4492 if(!strcmp(argv[0], name)){
4493 talloc_free(discard_const(name));
4496 talloc_free(discard_const(name));
4498 if (i == dbmap->num) {
4499 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4500 talloc_free(tmp_ctx);
4503 db_id = dbmap->dbs[i].dbid;
4506 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4508 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4509 talloc_free(tmp_ctx);
4513 talloc_free(tmp_ctx);
4520 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4530 db_id = strtoul(argv[0], NULL, 0);
4532 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4534 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4538 printf("Sequence number:%lld\n", (long long)seqnum);
4544 run an eventscript on a node
4546 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4552 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4555 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4559 data.dptr = (unsigned char *)discard_const(argv[0]);
4560 data.dsize = strlen((char *)data.dptr) + 1;
4562 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4564 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4565 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4566 if (ret != 0 || res != 0) {
4567 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4568 talloc_free(tmp_ctx);
4571 talloc_free(tmp_ctx);
4575 #define DB_VERSION 1
4576 #define MAX_DB_NAME 64
4577 struct db_file_header {
4578 unsigned long version;
4580 unsigned long persistent;
4582 const char name[MAX_DB_NAME];
4585 struct backup_data {
4586 struct ctdb_marshall_buffer *records;
4589 bool traverse_error;
4592 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4594 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4595 struct ctdb_rec_data *rec;
4597 /* add the record */
4598 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4600 bd->traverse_error = true;
4601 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4604 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4605 if (bd->records == NULL) {
4606 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4607 bd->traverse_error = true;
4610 bd->records->count++;
4611 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4612 bd->len += rec->length;
4620 * backup a database to a file
4622 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4625 struct ctdb_dbid_map *dbmap=NULL;
4626 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4627 struct db_file_header dbhdr;
4628 struct ctdb_db_context *ctdb_db;
4629 struct backup_data *bd;
4632 const char *reason = NULL;
4635 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4639 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4641 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4645 for(i=0;i<dbmap->num;i++){
4648 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4649 if(!strcmp(argv[0], name)){
4650 talloc_free(discard_const(name));
4653 talloc_free(discard_const(name));
4655 if (i == dbmap->num) {
4656 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4657 talloc_free(tmp_ctx);
4661 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4662 dbmap->dbs[i].dbid, tmp_ctx, &reason);
4664 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4666 talloc_free(tmp_ctx);
4670 uint32_t allow_unhealthy = 0;
4672 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4673 "AllowUnhealthyDBRead",
4676 if (allow_unhealthy != 1) {
4677 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4680 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4682 talloc_free(tmp_ctx);
4686 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4688 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4689 "tunnable AllowUnhealthyDBRead = %u\n",
4693 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4694 if (ctdb_db == NULL) {
4695 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4696 talloc_free(tmp_ctx);
4701 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4703 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4704 talloc_free(tmp_ctx);
4709 bd = talloc_zero(tmp_ctx, struct backup_data);
4711 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4712 talloc_free(tmp_ctx);
4716 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4717 if (bd->records == NULL) {
4718 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4719 talloc_free(tmp_ctx);
4723 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4724 bd->records->db_id = ctdb_db->db_id;
4725 /* traverse the database collecting all records */
4726 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4727 bd->traverse_error) {
4728 DEBUG(DEBUG_ERR,("Traverse error\n"));
4729 talloc_free(tmp_ctx);
4733 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4736 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4738 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4739 talloc_free(tmp_ctx);
4743 dbhdr.version = DB_VERSION;
4744 dbhdr.timestamp = time(NULL);
4745 dbhdr.persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4746 dbhdr.size = bd->len;
4747 if (strlen(argv[0]) >= MAX_DB_NAME) {
4748 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4751 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4752 ret = write(fh, &dbhdr, sizeof(dbhdr));
4754 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4757 ret = write(fh, bd->records, bd->len);
4759 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4768 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4772 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4774 talloc_free(tmp_ctx);
4779 * restore a database from a file
4781 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4784 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4787 struct db_file_header dbhdr;
4788 struct ctdb_db_context *ctdb_db;
4789 struct ctdb_node_map *nodemap=NULL;
4790 struct ctdb_vnn_map *vnnmap=NULL;
4792 struct ctdb_control_wipe_database w;
4794 uint32_t generation;
4799 if (argc < 1 || argc > 2) {
4800 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4804 fh = open(argv[0], O_RDONLY);
4806 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4807 talloc_free(tmp_ctx);
4811 read(fh, &dbhdr, sizeof(dbhdr));
4812 if (dbhdr.version != DB_VERSION) {
4813 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4814 talloc_free(tmp_ctx);
4818 dbname = discard_const(dbhdr.name);
4820 dbname = discard_const(argv[1]);
4823 outdata.dsize = dbhdr.size;
4824 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4825 if (outdata.dptr == NULL) {
4826 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4828 talloc_free(tmp_ctx);
4831 read(fh, outdata.dptr, outdata.dsize);
4834 tm = localtime(&dbhdr.timestamp);
4835 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4836 printf("Restoring database '%s' from backup @ %s\n",
4840 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4841 if (ctdb_db == NULL) {
4842 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4843 talloc_free(tmp_ctx);
4847 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4849 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4850 talloc_free(tmp_ctx);
4855 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4857 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4858 talloc_free(tmp_ctx);
4862 /* freeze all nodes */
4863 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4864 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4865 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4871 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4872 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4873 talloc_free(tmp_ctx);
4878 generation = vnnmap->generation;
4879 data.dptr = (void *)&generation;
4880 data.dsize = sizeof(generation);
4882 /* start a cluster wide transaction */
4883 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4884 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4886 TIMELIMIT(), false, data,
4889 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4894 w.db_id = ctdb_db->db_id;
4895 w.transaction_id = generation;
4897 data.dptr = (void *)&w;
4898 data.dsize = sizeof(w);
4900 /* wipe all the remote databases. */
4901 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4902 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4904 TIMELIMIT(), false, data,
4907 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4908 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4909 talloc_free(tmp_ctx);
4913 /* push the database */
4914 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4915 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4917 TIMELIMIT(), false, outdata,
4920 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4921 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4922 talloc_free(tmp_ctx);
4926 data.dptr = (void *)&ctdb_db->db_id;
4927 data.dsize = sizeof(ctdb_db->db_id);
4929 /* mark the database as healthy */
4930 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4931 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4933 TIMELIMIT(), false, data,
4936 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4937 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4938 talloc_free(tmp_ctx);
4942 data.dptr = (void *)&generation;
4943 data.dsize = sizeof(generation);
4945 /* commit all the changes */
4946 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4948 TIMELIMIT(), false, data,
4951 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4952 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4953 talloc_free(tmp_ctx);
4958 /* thaw all nodes */
4959 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4960 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4966 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4967 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4968 talloc_free(tmp_ctx);
4973 talloc_free(tmp_ctx);
4978 * dump a database backup from a file
4980 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4982 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4984 struct db_file_header dbhdr;
4988 struct ctdb_rec_data *rec = NULL;
4989 struct ctdb_marshall_buffer *m;
4990 struct ctdb_dump_db_context c;
4993 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4997 fh = open(argv[0], O_RDONLY);
4999 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5000 talloc_free(tmp_ctx);
5004 read(fh, &dbhdr, sizeof(dbhdr));
5005 if (dbhdr.version != DB_VERSION) {
5006 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5007 talloc_free(tmp_ctx);
5011 outdata.dsize = dbhdr.size;
5012 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5013 if (outdata.dptr == NULL) {
5014 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5016 talloc_free(tmp_ctx);
5019 read(fh, outdata.dptr, outdata.dsize);
5021 m = (struct ctdb_marshall_buffer *)outdata.dptr;
5023 tm = localtime(&dbhdr.timestamp);
5024 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5025 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5026 dbhdr.name, m->db_id, tbuf);
5030 c.printemptyrecords = (bool)options.printemptyrecords;
5031 c.printdatasize = (bool)options.printdatasize;
5032 c.printlmaster = false;
5033 c.printhash = (bool)options.printhash;
5034 c.printrecordflags = (bool)options.printrecordflags;
5036 for (i=0; i < m->count; i++) {
5040 /* we do not want the header splitted, so we pass NULL*/
5041 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5044 ctdb_dumpdb_record(ctdb, key, data, &c);
5047 printf("Dumped %d records\n", i);
5048 talloc_free(tmp_ctx);
5053 * wipe a database from a file
5055 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5059 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5061 struct ctdb_db_context *ctdb_db;
5062 struct ctdb_node_map *nodemap = NULL;
5063 struct ctdb_vnn_map *vnnmap = NULL;
5065 struct ctdb_control_wipe_database w;
5067 uint32_t generation;
5068 struct ctdb_dbid_map *dbmap = NULL;
5071 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5075 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5078 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
5083 for(i=0;i<dbmap->num;i++){
5086 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
5087 dbmap->dbs[i].dbid, tmp_ctx, &name);
5088 if(!strcmp(argv[0], name)){
5089 talloc_free(discard_const(name));
5092 talloc_free(discard_const(name));
5094 if (i == dbmap->num) {
5095 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
5097 talloc_free(tmp_ctx);
5101 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5102 if (ctdb_db == NULL) {
5103 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5105 talloc_free(tmp_ctx);
5109 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5112 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5114 talloc_free(tmp_ctx);
5118 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5121 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5123 talloc_free(tmp_ctx);
5127 /* freeze all nodes */
5128 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5129 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5130 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5137 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5138 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5139 CTDB_RECOVERY_ACTIVE);
5140 talloc_free(tmp_ctx);
5145 generation = vnnmap->generation;
5146 data.dptr = (void *)&generation;
5147 data.dsize = sizeof(generation);
5149 /* start a cluster wide transaction */
5150 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5151 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5153 TIMELIMIT(), false, data,
5157 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5158 "transactions.\n"));
5162 w.db_id = ctdb_db->db_id;
5163 w.transaction_id = generation;
5165 data.dptr = (void *)&w;
5166 data.dsize = sizeof(w);
5168 /* wipe all the remote databases. */
5169 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5170 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5172 TIMELIMIT(), false, data,
5175 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5176 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5177 talloc_free(tmp_ctx);
5181 data.dptr = (void *)&ctdb_db->db_id;
5182 data.dsize = sizeof(ctdb_db->db_id);
5184 /* mark the database as healthy */
5185 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5186 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5188 TIMELIMIT(), false, data,
5191 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5192 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5193 talloc_free(tmp_ctx);
5197 data.dptr = (void *)&generation;
5198 data.dsize = sizeof(generation);
5200 /* commit all the changes */
5201 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5203 TIMELIMIT(), false, data,
5206 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5207 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5208 talloc_free(tmp_ctx);
5212 /* thaw all nodes */
5213 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5214 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5220 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5221 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5222 talloc_free(tmp_ctx);
5226 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5228 talloc_free(tmp_ctx);
5235 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5241 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5242 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5243 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5244 if (ret != 0 || res != 0) {
5245 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5246 talloc_free(tmp_ctx);
5249 write(1, data.dptr, data.dsize);
5250 talloc_free(tmp_ctx);
5255 handler for memory dumps
5257 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5258 TDB_DATA data, void *private_data)
5260 write(1, data.dptr, data.dsize);
5265 dump memory usage on the recovery daemon
5267 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5271 struct rd_memdump_reply rd;
5273 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5275 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
5278 rd.srvid = getpid();
5280 /* register a message port for receiveing the reply so that we
5281 can receive the reply
5283 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5286 data.dptr = (uint8_t *)&rd;
5287 data.dsize = sizeof(rd);
5289 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5291 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5295 /* this loop will terminate when we have received the reply */
5297 event_loop_once(ctdb->ev);
5304 send a message to a srvid
5306 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5308 unsigned long srvid;
5316 srvid = strtoul(argv[0], NULL, 0);
5318 data.dptr = (uint8_t *)discard_const(argv[1]);
5319 data.dsize= strlen(argv[1]);
5321 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5323 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5331 handler for msglisten
5333 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5334 TDB_DATA data, void *private_data)
5338 printf("Message received: ");
5339 for (i=0;i<data.dsize;i++) {
5340 printf("%c", data.dptr[i]);
5346 listen for messages on a messageport
5348 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5354 /* register a message port and listen for messages
5356 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5357 printf("Listening for messages on srvid:%d\n", (int)srvid);
5360 event_loop_once(ctdb->ev);
5367 list all nodes in the cluster
5368 we parse the nodes file directly
5370 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5372 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5373 struct pnn_node *pnn_nodes;
5374 struct pnn_node *pnn_node;
5376 pnn_nodes = read_nodes_file(mem_ctx);
5377 if (pnn_nodes == NULL) {
5378 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5379 talloc_free(mem_ctx);
5383 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5384 ctdb_sock_addr addr;
5385 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5386 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5387 talloc_free(mem_ctx);
5390 if (options.machinereadable){
5391 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5393 printf("%s\n", pnn_node->addr);
5396 talloc_free(mem_ctx);
5402 reload the nodes file on the local node
5404 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5408 struct ctdb_node_map *nodemap=NULL;
5410 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5412 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
5416 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5418 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5422 /* reload the nodes file on all remote nodes */
5423 for (i=0;i<nodemap->num;i++) {
5424 if (nodemap->nodes[i].pnn == mypnn) {
5427 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5428 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5429 nodemap->nodes[i].pnn);
5431 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5435 /* reload the nodes file on the local node */
5436 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5437 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5439 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5442 /* initiate a recovery */
5443 control_recover(ctdb, argc, argv);
5449 static const struct {
5451 int (*fn)(struct ctdb_context *, int, const char **);
5453 bool without_daemon; /* can be run without daemon running ? */
5456 } ctdb_commands[] = {
5458 { "version", control_version, true, false, "show version of ctdb" },
5460 { "status", control_status, true, false, "show node status" },
5461 { "uptime", control_uptime, true, false, "show node uptime" },
5462 { "ping", control_ping, true, false, "ping all nodes" },
5463 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5464 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5465 { "listvars", control_listvars, true, false, "list tunable variables"},
5466 { "statistics", control_statistics, false, false, "show statistics" },
5467 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5468 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5469 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5470 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5471 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5472 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5473 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5474 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5475 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
5476 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname>"},
5477 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname>"},
5478 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5479 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5480 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5481 { "lvs", control_lvs, true, false, "show lvs configuration" },
5482 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5483 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5484 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5485 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5486 { "getdebug", control_getdebug, true, false, "get debug level" },
5487 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
5488 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
5489 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5490 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5491 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5492 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5493 { "disable", control_disable, true, false, "disable a nodes public IP" },
5494 { "enable", control_enable, true, false, "enable a nodes public IP" },
5495 { "stop", control_stop, true, false, "stop a node" },
5496 { "continue", control_continue, true, false, "re-start a stopped node" },
5497 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5498 { "unban", control_unban, true, false, "unban a node" },
5499 { "showban", control_showban, true, false, "show ban information"},
5500 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5501 { "recover", control_recover, true, false, "force recovery" },
5502 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
5503 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5504 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5505 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5506 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5507 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5508 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5509 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5510 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5512 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5514 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5515 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5516 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5517 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5518 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5519 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
5520 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5521 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5522 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5523 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5524 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5525 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5526 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5527 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
5528 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5529 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5530 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
5531 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
5532 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5533 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5534 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5535 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5536 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5537 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5538 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5539 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5540 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5541 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5542 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
5543 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
5544 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbid>"},
5545 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5546 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5547 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5548 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
5549 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
5550 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
5551 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
5552 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5553 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5554 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5555 { "rebalancenode", control_rebalancenode, false, false, "release a node by allowing it to takeover ips", "<pnn>"},
5556 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbid>" },
5557 { "nodestatus", control_nodestatus, true, false, "show and return node status" },
5558 { "dbstatistics", control_dbstatistics, false, false, "show db statistics", "<db>" },
5564 static void usage(void)
5568 "Usage: ctdb [options] <control>\n" \
5570 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5571 " -Y generate machinereadable output\n"
5572 " -v generate verbose output\n"
5573 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5574 printf("Controls:\n");
5575 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5576 printf(" %-15s %-27s %s\n",
5577 ctdb_commands[i].name,
5578 ctdb_commands[i].args?ctdb_commands[i].args:"",
5579 ctdb_commands[i].msg);
5585 static void ctdb_alarm(int sig)
5587 printf("Maximum runtime exceeded - exiting\n");
5594 int main(int argc, const char *argv[])
5596 struct ctdb_context *ctdb;
5597 char *nodestring = NULL;
5598 struct poptOption popt_options[] = {
5601 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5602 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5603 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
5604 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5605 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5606 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
5607 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
5608 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
5609 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
5610 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
5614 const char **extra_argv;
5618 struct event_context *ev;
5619 const char *control;
5620 const char *socket_name;
5624 /* set some defaults */
5625 options.maxruntime = 0;
5626 options.timelimit = 3;
5627 options.pnn = CTDB_CURRENT_NODE;
5629 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5631 while ((opt = poptGetNextOpt(pc)) != -1) {
5634 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5635 poptBadOption(pc, 0), poptStrerror(opt)));
5640 /* setup the remaining options for the main program to use */
5641 extra_argv = poptGetArgs(pc);
5644 while (extra_argv[extra_argc]) extra_argc++;
5647 if (extra_argc < 1) {
5651 if (options.maxruntime == 0) {
5652 const char *ctdb_timeout;
5653 ctdb_timeout = getenv("CTDB_TIMEOUT");
5654 if (ctdb_timeout != NULL) {
5655 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5657 /* default timeout is 120 seconds */
5658 options.maxruntime = 120;
5662 signal(SIGALRM, ctdb_alarm);
5663 alarm(options.maxruntime);
5665 control = extra_argv[0];
5667 ev = event_context_init(NULL);
5669 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5672 tevent_loop_allow_nesting(ev);
5674 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5675 if (strcmp(control, ctdb_commands[i].name) == 0) {
5680 if (i == ARRAY_SIZE(ctdb_commands)) {
5681 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5685 if (ctdb_commands[i].without_daemon == true) {
5686 if (nodestring != NULL) {
5687 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
5691 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
5694 /* initialise ctdb */
5695 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5698 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5702 /* initialize a libctdb connection as well */
5703 socket_name = ctdb_get_socketname(ctdb);
5704 ctdb_connection = ctdb_connect(socket_name,
5705 ctdb_log_file, stderr);
5706 if (ctdb_connection == NULL) {
5707 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
5711 /* setup the node number(s) to contact */
5712 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
5713 &options.nodes, &options.pnn)) {
5717 if (options.pnn == CTDB_CURRENT_NODE) {
5718 options.pnn = options.nodes[0];
5721 if (ctdb_commands[i].auto_all &&
5722 ((options.pnn == CTDB_BROADCAST_ALL) ||
5723 (options.pnn == CTDB_MULTICAST))) {
5727 for (j = 0; j < talloc_array_length(options.nodes); j++) {
5728 options.pnn = options.nodes[j];
5729 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5732 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5735 ctdb_disconnect(ctdb_connection);
5737 (void)poptFreeContext(pc);