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),
347 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
356 if (options.machinereadable){
358 printf("CTDB version:");
359 printf("Current time of statistics:");
360 printf("Statistics collected since:");
361 for (i=0;i<ARRAY_SIZE(fields);i++) {
362 printf("%s:", fields[i].name);
364 printf("num_reclock_ctdbd_latency:");
365 printf("min_reclock_ctdbd_latency:");
366 printf("avg_reclock_ctdbd_latency:");
367 printf("max_reclock_ctdbd_latency:");
369 printf("num_reclock_recd_latency:");
370 printf("min_reclock_recd_latency:");
371 printf("avg_reclock_recd_latency:");
372 printf("max_reclock_recd_latency:");
374 printf("num_call_latency:");
375 printf("min_call_latency:");
376 printf("avg_call_latency:");
377 printf("max_call_latency:");
379 printf("num_lockwait_latency:");
380 printf("min_lockwait_latency:");
381 printf("avg_lockwait_latency:");
382 printf("max_lockwait_latency:");
384 printf("num_childwrite_latency:");
385 printf("min_childwrite_latency:");
386 printf("avg_childwrite_latency:");
387 printf("max_childwrite_latency:");
390 printf("%d:", CTDB_VERSION);
391 printf("%d:", (int)s->statistics_current_time.tv_sec);
392 printf("%d:", (int)s->statistics_start_time.tv_sec);
393 for (i=0;i<ARRAY_SIZE(fields);i++) {
394 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
396 printf("%d:", s->reclock.ctdbd.num);
397 printf("%.6f:", s->reclock.ctdbd.min);
398 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
399 printf("%.6f:", s->reclock.ctdbd.max);
401 printf("%d:", s->reclock.recd.num);
402 printf("%.6f:", s->reclock.recd.min);
403 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
404 printf("%.6f:", s->reclock.recd.max);
406 printf("%d:", s->call_latency.num);
407 printf("%.6f:", s->call_latency.min);
408 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
409 printf("%.6f:", s->call_latency.max);
411 printf("%d:", s->lockwait_latency.num);
412 printf("%.6f:", s->lockwait_latency.min);
413 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
414 printf("%.6f:", s->lockwait_latency.max);
416 printf("%d:", s->childwrite_latency.num);
417 printf("%.6f:", s->childwrite_latency.min);
418 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
419 printf("%.6f:", s->childwrite_latency.max);
422 printf("CTDB version %u\n", CTDB_VERSION);
423 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
424 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
426 for (i=0;i<ARRAY_SIZE(fields);i++) {
427 if (strchr(fields[i].name, '.')) {
428 preflen = strcspn(fields[i].name, ".")+1;
429 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
430 prefix = fields[i].name;
431 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
436 printf(" %*s%-22s%*s%10u\n",
438 fields[i].name+preflen,
440 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
442 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);
444 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);
446 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);
447 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);
448 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);
451 talloc_free(tmp_ctx);
455 display remote ctdb statistics combined from all nodes
457 static int control_statistics_all(struct ctdb_context *ctdb)
460 struct ctdb_statistics statistics;
464 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
465 CTDB_NO_MEMORY(ctdb, nodes);
467 ZERO_STRUCT(statistics);
469 for (i=0;i<num_nodes;i++) {
470 struct ctdb_statistics s1;
472 uint32_t *v1 = (uint32_t *)&s1;
473 uint32_t *v2 = (uint32_t *)&statistics;
475 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
476 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
478 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
481 for (j=0;j<num_ints;j++) {
484 statistics.max_hop_count =
485 MAX(statistics.max_hop_count, s1.max_hop_count);
486 statistics.call_latency.max =
487 MAX(statistics.call_latency.max, s1.call_latency.max);
488 statistics.lockwait_latency.max =
489 MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
492 printf("Gathered statistics for %u nodes\n", num_nodes);
493 show_statistics(&statistics, 1);
498 display remote ctdb statistics
500 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
503 struct ctdb_statistics statistics;
505 if (options.pnn == CTDB_BROADCAST_ALL) {
506 return control_statistics_all(ctdb);
509 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
511 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
514 show_statistics(&statistics, 1);
520 reset remote ctdb statistics
522 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
526 ret = ctdb_statistics_reset(ctdb, options.pnn);
528 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
536 display remote ctdb rolling statistics
538 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
541 struct ctdb_statistics_wire *stats;
542 int i, num_records = -1;
545 num_records = atoi(argv[0]) - 1;
548 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
550 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
553 for (i=0;i<stats->num;i++) {
554 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
557 show_statistics(&stats->stats[i], i==0);
558 if (i == num_records) {
567 display uptime of remote node
569 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
572 struct ctdb_uptime *uptime = NULL;
573 int tmp, days, hours, minutes, seconds;
575 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
577 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
581 if (options.machinereadable){
582 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
583 printf(":%u:%u:%u:%lf\n",
584 (unsigned int)uptime->current_time.tv_sec,
585 (unsigned int)uptime->ctdbd_start_time.tv_sec,
586 (unsigned int)uptime->last_recovery_finished.tv_sec,
587 timeval_delta(&uptime->last_recovery_finished,
588 &uptime->last_recovery_started)
593 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
595 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
603 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
605 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
613 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
615 printf("Duration of last recovery/failover: %lf seconds\n",
616 timeval_delta(&uptime->last_recovery_finished,
617 &uptime->last_recovery_started));
623 show the PNN of the current node
625 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
630 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
632 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
636 printf("PNN:%d\n", mypnn);
642 struct pnn_node *next;
647 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
649 const char *nodes_list;
653 struct pnn_node *pnn_nodes = NULL;
654 struct pnn_node *pnn_node;
655 struct pnn_node *tmp_node;
657 /* read the nodes file */
658 nodes_list = getenv("CTDB_NODES");
659 if (nodes_list == NULL) {
660 nodes_list = "/etc/ctdb/nodes";
662 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
666 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
669 for (i=0, pnn=0; i<nlines; i++) {
673 /* strip leading spaces */
674 while((*node == ' ') || (*node == '\t')) {
681 if (strcmp(node, "") == 0) {
684 pnn_node = talloc(mem_ctx, struct pnn_node);
685 pnn_node->pnn = pnn++;
686 pnn_node->addr = talloc_strdup(pnn_node, node);
687 pnn_node->next = pnn_nodes;
688 pnn_nodes = pnn_node;
691 /* swap them around so we return them in incrementing order */
692 pnn_node = pnn_nodes;
696 pnn_node = pnn_node->next;
698 tmp_node->next = pnn_nodes;
699 pnn_nodes = tmp_node;
706 show the PNN of the current node
707 discover the pnn by loading the nodes file and try to bind to all
708 addresses one at a time until the ip address is found.
710 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
712 TALLOC_CTX *mem_ctx = talloc_new(NULL);
713 struct pnn_node *pnn_nodes;
714 struct pnn_node *pnn_node;
716 pnn_nodes = read_nodes_file(mem_ctx);
717 if (pnn_nodes == NULL) {
718 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
719 talloc_free(mem_ctx);
723 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
726 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
727 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
728 talloc_free(mem_ctx);
732 if (ctdb_sys_have_ip(&addr)) {
733 printf("PNN:%d\n", pnn_node->pnn);
734 talloc_free(mem_ctx);
739 printf("Failed to detect which PNN this node is\n");
740 talloc_free(mem_ctx);
744 /* Helpers for ctdb status
746 static bool is_partially_online(struct ctdb_node_and_flags *node)
751 if (node->flags == 0) {
752 struct ctdb_ifaces_list *ifaces;
754 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
755 for (j=0; j < ifaces->num; j++) {
756 if (ifaces->ifaces[j].link_state != 0) {
762 ctdb_free_ifaces(ifaces);
769 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
771 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
772 ctdb_addr_to_str(&node->addr),
773 !!(node->flags&NODE_FLAGS_DISCONNECTED),
774 !!(node->flags&NODE_FLAGS_BANNED),
775 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
776 !!(node->flags&NODE_FLAGS_UNHEALTHY),
777 !!(node->flags&NODE_FLAGS_STOPPED),
778 !!(node->flags&NODE_FLAGS_INACTIVE),
779 is_partially_online(node) ? 1 : 0,
780 (node->pnn == mypnn)?'Y':'N');
785 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
787 printf("pnn:%d %-16s %s%s\n", node->pnn,
788 ctdb_addr_to_str(&node->addr),
789 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
790 node->pnn == mypnn?" (THIS NODE)":"");
796 display remote ctdb status
798 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
801 struct ctdb_vnn_map *vnnmap=NULL;
802 struct ctdb_node_map *nodemap=NULL;
803 uint32_t recmode, recmaster, mypnn;
805 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
809 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
810 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
814 if (options.machinereadable) {
815 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
816 ":Inactive:PartiallyOnline:ThisNode:\n");
817 for (i=0;i<nodemap->num;i++) {
818 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
821 (void) control_status_1_machine(mypnn,
827 printf("Number of nodes:%d\n", nodemap->num);
828 for(i=0;i<nodemap->num;i++){
829 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
832 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
835 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
836 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
839 if (vnnmap->generation == INVALID_GENERATION) {
840 printf("Generation:INVALID\n");
842 printf("Generation:%d\n",vnnmap->generation);
844 printf("Size:%d\n",vnnmap->size);
845 for(i=0;i<vnnmap->size;i++){
846 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
848 ctdb_free_vnnmap(vnnmap);
850 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
851 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
854 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
856 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
857 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
860 printf("Recovery master:%d\n",recmaster);
865 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
868 struct ctdb_node_map *nodemap=NULL;
870 uint32_t pnn_mode, mypnn;
876 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
877 options.pnn, true, &nodes, &pnn_mode)) {
881 if (options.machinereadable) {
882 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
883 ":Inactive:PartiallyOnline:ThisNode:\n");
884 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
885 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
888 if (!ctdb_getpnn(ctdb_connection, CTDB_CURRENT_NODE, &mypnn)) {
889 DEBUG(DEBUG_ERR, ("Unable to get PNN from local node\n"));
893 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
894 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
900 for (i = 0; i < talloc_array_length(nodes); i++) {
901 if (options.machinereadable) {
902 ret |= control_status_1_machine(mypnn,
903 &nodemap->nodes[nodes[i]]);
905 ret |= control_status_1_human(mypnn,
906 &nodemap->nodes[nodes[i]]);
913 struct natgw_node *next;
918 display the list of nodes belonging to this natgw configuration
920 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
923 uint32_t capabilities;
924 const char *natgw_list;
927 struct natgw_node *natgw_nodes = NULL;
928 struct natgw_node *natgw_node;
929 struct ctdb_node_map *nodemap=NULL;
932 /* read the natgw nodes file into a linked list */
933 natgw_list = getenv("NATGW_NODES");
934 if (natgw_list == NULL) {
935 natgw_list = "/etc/ctdb/natgw_nodes";
937 lines = file_lines_load(natgw_list, &nlines, ctdb);
939 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
942 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
945 for (i=0;i<nlines;i++) {
949 /* strip leading spaces */
950 while((*node == ' ') || (*node == '\t')) {
956 if (strcmp(node, "") == 0) {
959 natgw_node = talloc(ctdb, struct natgw_node);
960 natgw_node->addr = talloc_strdup(natgw_node, node);
961 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
962 natgw_node->next = natgw_nodes;
963 natgw_nodes = natgw_node;
966 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
968 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
973 while(i<nodemap->num) {
974 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
975 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
980 /* this node was not in the natgw so we just remove it from
983 if ((natgw_node == NULL)
984 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
987 for (j=i+1; j<nodemap->num; j++) {
988 nodemap->nodes[j-1] = nodemap->nodes[j];
997 /* pick a node to be natgwmaster
998 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
1000 for(i=0;i<nodemap->num;i++){
1001 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
1002 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1004 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1007 if (!(capabilities&CTDB_CAP_NATGW)) {
1010 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1014 /* we couldnt find any healthy node, try unhealthy ones */
1015 if (i == nodemap->num) {
1016 for(i=0;i<nodemap->num;i++){
1017 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
1018 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1020 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1023 if (!(capabilities&CTDB_CAP_NATGW)) {
1026 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1031 /* unless all nodes are STOPPED, when we pick one anyway */
1032 if (i == nodemap->num) {
1033 for(i=0;i<nodemap->num;i++){
1034 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
1035 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1037 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1040 if (!(capabilities&CTDB_CAP_NATGW)) {
1043 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
1047 /* or if we still can not find any */
1048 if (i == nodemap->num) {
1049 printf("-1 0.0.0.0\n");
1050 ret = 2; /* matches ENOENT */
1054 /* print the pruned list of nodes belonging to this natgw list */
1055 for(i=0;i<nodemap->num;i++){
1056 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1059 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
1060 ctdb_addr_to_str(&nodemap->nodes[i].addr),
1061 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
1062 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
1063 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
1064 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
1065 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
1072 display the status of the scripts for monitoring (or other events)
1074 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1075 enum ctdb_eventscript_call type)
1077 struct ctdb_scripts_wire *script_status;
1080 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1082 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1086 if (script_status == NULL) {
1087 if (!options.machinereadable) {
1088 printf("%s cycle never run\n",
1089 ctdb_eventscript_call_names[type]);
1094 if (!options.machinereadable) {
1095 printf("%d scripts were executed last %s cycle\n",
1096 script_status->num_scripts,
1097 ctdb_eventscript_call_names[type]);
1099 for (i=0; i<script_status->num_scripts; i++) {
1100 const char *status = NULL;
1102 switch (script_status->scripts[i].status) {
1104 status = "TIMEDOUT";
1107 status = "DISABLED";
1113 if (script_status->scripts[i].status > 0)
1117 if (options.machinereadable) {
1118 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1119 ctdb_eventscript_call_names[type],
1120 script_status->scripts[i].name,
1121 script_status->scripts[i].status,
1123 (long)script_status->scripts[i].start.tv_sec,
1124 (long)script_status->scripts[i].start.tv_usec,
1125 (long)script_status->scripts[i].finished.tv_sec,
1126 (long)script_status->scripts[i].finished.tv_usec,
1127 script_status->scripts[i].output);
1131 printf("%-20s Status:%s ",
1132 script_status->scripts[i].name, status);
1134 /* Some other error, eg from stat. */
1135 printf("%-20s Status:CANNOT RUN (%s)",
1136 script_status->scripts[i].name,
1137 strerror(-script_status->scripts[i].status));
1139 if (script_status->scripts[i].status >= 0) {
1140 printf("Duration:%.3lf ",
1141 timeval_delta(&script_status->scripts[i].finished,
1142 &script_status->scripts[i].start));
1144 if (script_status->scripts[i].status != -ENOEXEC) {
1146 ctime(&script_status->scripts[i].start.tv_sec));
1147 if (script_status->scripts[i].status != 0) {
1148 printf(" OUTPUT:%s\n",
1149 script_status->scripts[i].output);
1159 static int control_scriptstatus(struct ctdb_context *ctdb,
1160 int argc, const char **argv)
1163 enum ctdb_eventscript_call type, min, max;
1167 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1172 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1176 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1177 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1183 if (type == CTDB_EVENT_MAX) {
1184 if (strcmp(arg, "all") == 0) {
1186 max = CTDB_EVENT_MAX;
1188 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1193 if (options.machinereadable) {
1194 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1197 for (type = min; type < max; type++) {
1198 ret = control_one_scriptstatus(ctdb, type);
1208 enable an eventscript
1210 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1218 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1220 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1228 disable an eventscript
1230 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1238 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1240 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1248 display the pnn of the recovery master
1250 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1254 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1255 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1258 printf("%d\n",recmaster);
1264 add a tickle to a public address
1266 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1268 struct ctdb_tcp_connection t;
1276 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1277 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1280 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1281 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1285 data.dptr = (uint8_t *)&t;
1286 data.dsize = sizeof(t);
1288 /* tell all nodes about this tcp connection */
1289 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1290 0, data, ctdb, NULL, NULL, NULL, NULL);
1292 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1301 delete a tickle from a node
1303 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1305 struct ctdb_tcp_connection t;
1313 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1314 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1317 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1318 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1322 data.dptr = (uint8_t *)&t;
1323 data.dsize = sizeof(t);
1325 /* tell all nodes about this tcp connection */
1326 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1327 0, data, ctdb, NULL, NULL, NULL, NULL);
1329 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1338 get a list of all tickles for this pnn
1340 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1342 struct ctdb_control_tcp_tickle_list *list;
1343 ctdb_sock_addr addr;
1352 port = atoi(argv[1]);
1355 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1356 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1360 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1362 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1366 if (options.machinereadable){
1367 printf(":source ip:port:destination ip:port:\n");
1368 for (i=0;i<list->tickles.num;i++) {
1369 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1372 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1373 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1376 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1377 printf("Num tickles:%u\n", list->tickles.num);
1378 for (i=0;i<list->tickles.num;i++) {
1379 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1382 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1383 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));
1393 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1395 struct ctdb_all_public_ips *ips;
1396 struct ctdb_public_ip ip;
1399 uint32_t disable_time;
1401 struct ctdb_node_map *nodemap=NULL;
1402 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1405 data.dptr = (uint8_t*)&disable_time;
1406 data.dsize = sizeof(disable_time);
1407 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1409 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1415 /* read the public ip list from the node */
1416 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1418 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1419 talloc_free(tmp_ctx);
1423 for (i=0;i<ips->num;i++) {
1424 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1429 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1430 pnn, ctdb_addr_to_str(addr)));
1431 talloc_free(tmp_ctx);
1438 data.dptr = (uint8_t *)&ip;
1439 data.dsize = sizeof(ip);
1441 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1443 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1444 talloc_free(tmp_ctx);
1448 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1449 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1456 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1457 talloc_free(tmp_ctx);
1461 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1463 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1464 talloc_free(tmp_ctx);
1468 /* update the recovery daemon so it now knows to expect the new
1469 node assignment for this ip.
1471 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1473 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1477 talloc_free(tmp_ctx);
1482 move/failover an ip address to a specific node
1484 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1487 int ret, retries = 0;
1488 ctdb_sock_addr addr;
1495 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1496 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1501 if (sscanf(argv[1], "%u", &pnn) != 1) {
1502 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1507 ret = move_ip(ctdb, &addr, pnn);
1509 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1513 } while (retries < 5 && ret != 0);
1515 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1522 static int getips_store_callback(void *param, void *data)
1524 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1525 struct ctdb_all_public_ips *ips = param;
1529 ips->ips[i].pnn = node_ip->pnn;
1530 ips->ips[i].addr = node_ip->addr;
1534 static int getips_count_callback(void *param, void *data)
1536 uint32_t *count = param;
1543 static uint32_t *ip_key(ctdb_sock_addr *ip)
1545 static uint32_t key[IP_KEYLEN];
1547 bzero(key, sizeof(key));
1549 switch (ip->sa.sa_family) {
1551 key[0] = ip->ip.sin_addr.s_addr;
1554 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
1555 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
1556 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
1557 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
1560 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1567 static void *add_ip_callback(void *parm, void *data)
1573 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1575 struct ctdb_all_public_ips *tmp_ips;
1576 struct ctdb_node_map *nodemap=NULL;
1577 trbt_tree_t *ip_tree;
1581 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1583 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1587 ip_tree = trbt_create(tmp_ctx, 0);
1589 for(i=0;i<nodemap->num;i++){
1590 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1593 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1597 /* read the public ip list from this node */
1598 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1600 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1604 for (j=0; j<tmp_ips->num;j++) {
1605 struct ctdb_public_ip *node_ip;
1607 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1608 node_ip->pnn = tmp_ips->ips[j].pnn;
1609 node_ip->addr = tmp_ips->ips[j].addr;
1611 trbt_insertarray32_callback(ip_tree,
1612 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1616 talloc_free(tmp_ips);
1621 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1623 len = offsetof(struct ctdb_all_public_ips, ips) +
1624 count*sizeof(struct ctdb_public_ip);
1625 tmp_ips = talloc_zero_size(tmp_ctx, len);
1626 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1635 * scans all other nodes and returns a pnn for another node that can host this
1639 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1641 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1642 struct ctdb_all_public_ips *ips;
1643 struct ctdb_node_map *nodemap=NULL;
1646 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1648 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1649 talloc_free(tmp_ctx);
1653 for(i=0;i<nodemap->num;i++){
1654 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1657 if (nodemap->nodes[i].pnn == options.pnn) {
1661 /* read the public ip list from this node */
1662 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1664 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1668 for (j=0;j<ips->num;j++) {
1669 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1670 talloc_free(tmp_ctx);
1671 return nodemap->nodes[i].pnn;
1677 talloc_free(tmp_ctx);
1681 static uint32_t ipreallocate_finished;
1684 handler for receiving the response to ipreallocate
1686 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1687 TDB_DATA data, void *private_data)
1689 ipreallocate_finished = 1;
1692 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1694 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1696 event_add_timed(ctdb->ev, ctdb,
1697 timeval_current_ofs(1, 0),
1698 ctdb_every_second, ctdb);
1702 ask the recovery daemon on the recovery master to perform a ip reallocation
1704 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1708 struct takeover_run_reply rd;
1710 struct ctdb_node_map *nodemap=NULL;
1712 struct timeval tv = timeval_current();
1714 /* we need some events to trigger so we can timeout and restart
1717 event_add_timed(ctdb->ev, ctdb,
1718 timeval_current_ofs(1, 0),
1719 ctdb_every_second, ctdb);
1721 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1723 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1726 rd.srvid = getpid();
1728 /* register a message port for receiveing the reply so that we
1729 can receive the reply
1731 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1733 data.dptr = (uint8_t *)&rd;
1734 data.dsize = sizeof(rd);
1737 /* check that there are valid nodes available */
1738 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1739 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1742 for (i=0; i<nodemap->num;i++) {
1743 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1747 if (i==nodemap->num) {
1748 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1753 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1754 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1758 /* verify the node exists */
1759 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1760 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1765 /* check tha there are nodes available that can act as a recmaster */
1766 for (i=0; i<nodemap->num; i++) {
1767 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1772 if (i == nodemap->num) {
1773 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1777 /* verify the recovery master is not STOPPED, nor BANNED */
1778 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1779 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1785 /* verify the recovery master is not STOPPED, nor BANNED */
1786 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1787 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1793 ipreallocate_finished = 0;
1794 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1796 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1800 tv = timeval_current();
1801 /* this loop will terminate when we have received the reply */
1802 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1803 event_loop_once(ctdb->ev);
1805 if (ipreallocate_finished == 1) {
1818 add a public ip address to a node
1820 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1823 int len, retries = 0;
1825 ctdb_sock_addr addr;
1826 struct ctdb_control_ip_iface *pub;
1827 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1828 struct ctdb_all_public_ips *ips;
1832 talloc_free(tmp_ctx);
1836 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1837 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1838 talloc_free(tmp_ctx);
1842 /* read the public ip list from the node */
1843 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1845 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1846 talloc_free(tmp_ctx);
1849 for (i=0;i<ips->num;i++) {
1850 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1851 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1858 /* Dont timeout. This command waits for an ip reallocation
1859 which sometimes can take wuite a while if there has
1860 been a recent recovery
1864 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1865 pub = talloc_size(tmp_ctx, len);
1866 CTDB_NO_MEMORY(ctdb, pub);
1870 pub->len = strlen(argv[1])+1;
1871 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1874 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1876 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1880 } while (retries < 5 && ret != 0);
1882 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1883 talloc_free(tmp_ctx);
1888 ret = control_ipreallocate(ctdb, argc, argv);
1890 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Wait 3 seconds and try again.\n", options.pnn));
1894 } while (retries < 5 && ret != 0);
1896 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Giving up.\n", options.pnn));
1897 talloc_free(tmp_ctx);
1901 talloc_free(tmp_ctx);
1905 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1907 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1909 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1910 struct ctdb_node_map *nodemap=NULL;
1911 struct ctdb_all_public_ips *ips;
1914 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1916 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1920 /* remove it from the nodes that are not hosting the ip currently */
1921 for(i=0;i<nodemap->num;i++){
1922 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1925 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1926 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1930 for (j=0;j<ips->num;j++) {
1931 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1939 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1943 options.pnn = nodemap->nodes[i].pnn;
1944 control_delip(ctdb, argc, argv);
1948 /* remove it from every node (also the one hosting it) */
1949 for(i=0;i<nodemap->num;i++){
1950 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1953 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1954 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1958 for (j=0;j<ips->num;j++) {
1959 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1967 options.pnn = nodemap->nodes[i].pnn;
1968 control_delip(ctdb, argc, argv);
1971 talloc_free(tmp_ctx);
1976 delete a public ip address from a node
1978 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1982 ctdb_sock_addr addr;
1983 struct ctdb_control_ip_iface pub;
1984 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1985 struct ctdb_all_public_ips *ips;
1988 talloc_free(tmp_ctx);
1992 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1993 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1997 if (options.pnn == CTDB_BROADCAST_ALL) {
1998 return control_delip_all(ctdb, argc, argv, &addr);
2005 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2007 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2008 talloc_free(tmp_ctx);
2012 for (i=0;i<ips->num;i++) {
2013 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2019 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2020 ctdb_addr_to_str(&addr)));
2021 talloc_free(tmp_ctx);
2025 if (ips->ips[i].pnn == options.pnn) {
2026 ret = find_other_host_for_public_ip(ctdb, &addr);
2029 ret = move_ip(ctdb, &addr, ret);
2031 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
2035 } while (retries < 5 && ret != 0);
2037 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
2043 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2045 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2046 talloc_free(tmp_ctx);
2050 talloc_free(tmp_ctx);
2055 kill a tcp connection
2057 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2060 struct ctdb_control_killtcp killtcp;
2066 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2067 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2071 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2072 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2076 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2078 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2089 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2092 ctdb_sock_addr addr;
2098 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2099 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2103 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2105 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2113 register a server id
2115 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2118 struct ctdb_server_id server_id;
2124 server_id.pnn = strtoul(argv[0], NULL, 0);
2125 server_id.type = strtoul(argv[1], NULL, 0);
2126 server_id.server_id = strtoul(argv[2], NULL, 0);
2128 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2130 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2133 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2139 unregister a server id
2141 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2144 struct ctdb_server_id server_id;
2150 server_id.pnn = strtoul(argv[0], NULL, 0);
2151 server_id.type = strtoul(argv[1], NULL, 0);
2152 server_id.server_id = strtoul(argv[2], NULL, 0);
2154 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2156 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2163 check if a server id exists
2165 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2169 struct ctdb_server_id server_id;
2175 server_id.pnn = strtoul(argv[0], NULL, 0);
2176 server_id.type = strtoul(argv[1], NULL, 0);
2177 server_id.server_id = strtoul(argv[2], NULL, 0);
2179 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2181 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2186 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2188 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2194 get a list of all server ids that are registered on a node
2196 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2199 struct ctdb_server_id_list *server_ids;
2201 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2203 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2207 for (i=0; i<server_ids->num; i++) {
2208 printf("Server id %d:%d:%d\n",
2209 server_ids->server_ids[i].pnn,
2210 server_ids->server_ids[i].type,
2211 server_ids->server_ids[i].server_id);
2218 check if a server id exists
2220 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2222 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2228 talloc_free(tmp_ctx);
2232 ids = talloc_array(tmp_ctx, uint64_t, argc);
2233 result = talloc_array(tmp_ctx, uint8_t, argc);
2235 for (i = 0; i < argc; i++) {
2236 ids[i] = strtoull(argv[i], NULL, 0);
2239 if (!ctdb_check_message_handlers(ctdb_connection,
2240 options.pnn, argc, ids, result)) {
2241 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2243 talloc_free(tmp_ctx);
2247 for (i=0; i < argc; i++) {
2248 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2249 result[i] ? "exists" : "does not exist");
2252 talloc_free(tmp_ctx);
2257 send a tcp tickle ack
2259 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2262 ctdb_sock_addr src, dst;
2268 if (!parse_ip_port(argv[0], &src)) {
2269 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2273 if (!parse_ip_port(argv[1], &dst)) {
2274 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2278 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2282 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2289 display public ip status
2291 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2294 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2295 struct ctdb_all_public_ips *ips;
2297 if (options.pnn == CTDB_BROADCAST_ALL) {
2298 /* read the list of public ips from all nodes */
2299 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2301 /* read the public ip list from this node */
2302 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2305 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2306 talloc_free(tmp_ctx);
2310 if (options.machinereadable){
2311 printf(":Public IP:Node:");
2312 if (options.verbose){
2313 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2317 if (options.pnn == CTDB_BROADCAST_ALL) {
2318 printf("Public IPs on ALL nodes\n");
2320 printf("Public IPs on node %u\n", options.pnn);
2324 for (i=1;i<=ips->num;i++) {
2325 struct ctdb_control_public_ip_info *info = NULL;
2327 char *aciface = NULL;
2328 char *avifaces = NULL;
2329 char *cifaces = NULL;
2331 if (options.pnn == CTDB_BROADCAST_ALL) {
2332 pnn = ips->ips[ips->num-i].pnn;
2338 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2339 &ips->ips[ips->num-i].addr, &info);
2346 for (j=0; j < info->num; j++) {
2347 if (cifaces == NULL) {
2348 cifaces = talloc_strdup(info,
2349 info->ifaces[j].name);
2351 cifaces = talloc_asprintf_append(cifaces,
2353 info->ifaces[j].name);
2356 if (info->active_idx == j) {
2357 aciface = info->ifaces[j].name;
2360 if (info->ifaces[j].link_state == 0) {
2364 if (avifaces == NULL) {
2365 avifaces = talloc_strdup(info, info->ifaces[j].name);
2367 avifaces = talloc_asprintf_append(avifaces,
2369 info->ifaces[j].name);
2374 if (options.machinereadable){
2376 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2377 ips->ips[ips->num-i].pnn);
2378 if (options.verbose){
2381 avifaces?avifaces:"",
2382 cifaces?cifaces:"");
2386 if (options.verbose) {
2387 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2388 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2389 ips->ips[ips->num-i].pnn,
2391 avifaces?avifaces:"",
2392 cifaces?cifaces:"");
2395 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2396 ips->ips[ips->num-i].pnn);
2402 talloc_free(tmp_ctx);
2409 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2412 ctdb_sock_addr addr;
2413 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2414 struct ctdb_control_public_ip_info *info;
2417 talloc_free(tmp_ctx);
2421 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2422 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2426 /* read the public ip info from this node */
2427 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2428 tmp_ctx, &addr, &info);
2430 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2431 argv[0], options.pnn));
2432 talloc_free(tmp_ctx);
2436 printf("Public IP[%s] info on node %u\n",
2437 ctdb_addr_to_str(&info->ip.addr),
2440 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2441 ctdb_addr_to_str(&info->ip.addr),
2442 info->ip.pnn, info->num);
2444 for (i=0; i<info->num; i++) {
2445 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2447 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2448 i+1, info->ifaces[i].name,
2449 info->ifaces[i].link_state?"up":"down",
2450 (unsigned int)info->ifaces[i].references,
2451 (i==info->active_idx)?" (active)":"");
2454 talloc_free(tmp_ctx);
2459 display interfaces status
2461 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2464 struct ctdb_ifaces_list *ifaces;
2466 /* read the public ip list from this node */
2467 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2468 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2473 if (options.machinereadable){
2474 printf(":Name:LinkStatus:References:\n");
2476 printf("Interfaces on node %u\n", options.pnn);
2479 for (i=0; i<ifaces->num; i++) {
2480 if (options.machinereadable){
2481 printf(":%s:%s:%u\n",
2482 ifaces->ifaces[i].name,
2483 ifaces->ifaces[i].link_state?"1":"0",
2484 (unsigned int)ifaces->ifaces[i].references);
2486 printf("name:%s link:%s references:%u\n",
2487 ifaces->ifaces[i].name,
2488 ifaces->ifaces[i].link_state?"up":"down",
2489 (unsigned int)ifaces->ifaces[i].references);
2493 ctdb_free_ifaces(ifaces);
2499 set link status of an interface
2501 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2504 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2505 struct ctdb_control_iface_info info;
2513 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2514 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2516 talloc_free(tmp_ctx);
2519 strcpy(info.name, argv[0]);
2521 if (strcmp(argv[1], "up") == 0) {
2522 info.link_state = 1;
2523 } else if (strcmp(argv[1], "down") == 0) {
2524 info.link_state = 0;
2526 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2528 talloc_free(tmp_ctx);
2532 /* read the public ip list from this node */
2533 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2536 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2537 argv[0], options.pnn));
2538 talloc_free(tmp_ctx);
2542 talloc_free(tmp_ctx);
2547 display pid of a ctdb daemon
2549 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2554 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2556 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2559 printf("Pid:%d\n", pid);
2565 disable a remote node
2567 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2570 struct ctdb_node_map *nodemap=NULL;
2572 /* check if the node is already disabled */
2573 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2574 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2577 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2578 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2583 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2585 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2591 /* read the nodemap and verify the change took effect */
2592 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2593 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2597 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2598 ret = control_ipreallocate(ctdb, argc, argv);
2600 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2608 enable a disabled remote node
2610 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2614 struct ctdb_node_map *nodemap=NULL;
2617 /* check if the node is already enabled */
2618 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2619 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2622 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2623 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2628 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2630 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2636 /* read the nodemap and verify the change took effect */
2637 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2638 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2642 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2644 ret = control_ipreallocate(ctdb, argc, argv);
2646 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2656 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2659 struct ctdb_node_map *nodemap=NULL;
2662 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2664 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2669 /* read the nodemap and verify the change took effect */
2670 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2671 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2675 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2676 ret = control_ipreallocate(ctdb, argc, argv);
2678 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2686 restart a stopped remote node
2688 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2692 struct ctdb_node_map *nodemap=NULL;
2695 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2697 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2703 /* read the nodemap and verify the change took effect */
2704 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2705 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2709 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2710 ret = control_ipreallocate(ctdb, argc, argv);
2712 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2719 static uint32_t get_generation(struct ctdb_context *ctdb)
2721 struct ctdb_vnn_map *vnnmap=NULL;
2724 /* wait until the recmaster is not in recovery mode */
2726 uint32_t recmode, recmaster;
2728 if (vnnmap != NULL) {
2729 talloc_free(vnnmap);
2733 /* get the recmaster */
2734 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2735 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2739 /* get recovery mode */
2740 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2741 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2745 /* get the current generation number */
2746 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2748 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2752 if ((recmode == CTDB_RECOVERY_NORMAL)
2753 && (vnnmap->generation != 1)){
2754 return vnnmap->generation;
2761 ban a node from the cluster
2763 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2766 struct ctdb_node_map *nodemap=NULL;
2767 struct ctdb_ban_time bantime;
2773 /* verify the node exists */
2774 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2776 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2780 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2781 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2785 bantime.pnn = options.pnn;
2786 bantime.time = strtoul(argv[0], NULL, 0);
2788 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2790 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2794 ret = control_ipreallocate(ctdb, argc, argv);
2796 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2805 unban a node from the cluster
2807 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2810 struct ctdb_node_map *nodemap=NULL;
2811 struct ctdb_ban_time bantime;
2813 /* verify the node exists */
2814 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2816 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2820 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2821 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2825 bantime.pnn = options.pnn;
2828 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2830 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2834 ret = control_ipreallocate(ctdb, argc, argv);
2836 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2845 show ban information for a node
2847 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2850 struct ctdb_node_map *nodemap=NULL;
2851 struct ctdb_ban_time *bantime;
2853 /* verify the node exists */
2854 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2856 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2860 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2862 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2866 if (bantime->time == 0) {
2867 printf("Node %u is not banned\n", bantime->pnn);
2869 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2878 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2882 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2884 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2894 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2897 uint32_t generation, next_generation;
2899 /* record the current generation number */
2900 generation = get_generation(ctdb);
2902 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2904 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2908 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2910 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2914 /* wait until we are in a new generation */
2916 next_generation = get_generation(ctdb);
2917 if (next_generation != generation) {
2928 display monitoring mode of a remote node
2930 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2935 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2937 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2940 if (!options.machinereadable){
2941 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2944 printf(":%d:\n",monmode);
2951 display capabilities of a remote node
2953 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2955 uint32_t capabilities;
2958 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2960 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2964 if (!options.machinereadable){
2965 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2966 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2967 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2968 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2970 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2971 printf(":%d:%d:%d:%d:\n",
2972 !!(capabilities&CTDB_CAP_RECMASTER),
2973 !!(capabilities&CTDB_CAP_LMASTER),
2974 !!(capabilities&CTDB_CAP_LVS),
2975 !!(capabilities&CTDB_CAP_NATGW));
2981 display lvs configuration
2983 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2985 uint32_t *capabilities;
2986 struct ctdb_node_map *nodemap=NULL;
2988 int healthy_count = 0;
2990 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2992 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2996 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2997 CTDB_NO_MEMORY(ctdb, capabilities);
2999 /* collect capabilities for all connected nodes */
3000 for (i=0; i<nodemap->num; i++) {
3001 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3004 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3008 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3010 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3014 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3018 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3023 /* Print all LVS nodes */
3024 for (i=0; i<nodemap->num; i++) {
3025 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3028 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3031 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3035 if (healthy_count != 0) {
3036 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3041 printf("%d:%s\n", i,
3042 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3049 display who is the lvs master
3051 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3053 uint32_t *capabilities;
3054 struct ctdb_node_map *nodemap=NULL;
3056 int healthy_count = 0;
3058 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
3060 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3064 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3065 CTDB_NO_MEMORY(ctdb, capabilities);
3067 /* collect capabilities for all connected nodes */
3068 for (i=0; i<nodemap->num; i++) {
3069 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3072 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3076 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3078 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3082 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3086 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3091 /* find and show the lvsmaster */
3092 for (i=0; i<nodemap->num; i++) {
3093 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3096 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3099 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3103 if (healthy_count != 0) {
3104 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3109 if (options.machinereadable){
3112 printf("Node %d is LVS master\n", i);
3117 printf("There is no LVS master\n");
3122 disable monitoring on a node
3124 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3129 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3131 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3134 printf("Monitoring mode:%s\n","DISABLED");
3140 enable monitoring on a node
3142 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3147 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3149 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3152 printf("Monitoring mode:%s\n","ACTIVE");
3158 display remote list of keys/data for a db
3160 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3162 const char *db_name;
3163 struct ctdb_db_context *ctdb_db;
3166 struct ctdb_dump_db_context c;
3175 if (db_exists(ctdb, db_name, &persistent)) {
3176 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3180 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3182 if (ctdb_db == NULL) {
3183 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3187 if (options.printlmaster) {
3188 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3189 ctdb, &ctdb->vnn_map);
3191 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3199 c.printemptyrecords = (bool)options.printemptyrecords;
3200 c.printdatasize = (bool)options.printdatasize;
3201 c.printlmaster = (bool)options.printlmaster;
3202 c.printhash = (bool)options.printhash;
3203 c.printrecordflags = (bool)options.printrecordflags;
3205 /* traverse and dump the cluster tdb */
3206 ret = ctdb_dump_db(ctdb_db, &c);
3208 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3209 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3210 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3214 talloc_free(ctdb_db);
3216 printf("Dumped %d records\n", ret);
3220 struct cattdb_data {
3221 struct ctdb_context *ctdb;
3225 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3227 struct cattdb_data *d = private_data;
3228 struct ctdb_dump_db_context c;
3234 c.printemptyrecords = (bool)options.printemptyrecords;
3235 c.printdatasize = (bool)options.printdatasize;
3236 c.printlmaster = false;
3237 c.printhash = (bool)options.printhash;
3238 c.printrecordflags = true;
3240 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3244 cat the local tdb database using same format as catdb
3246 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3248 const char *db_name;
3249 struct ctdb_db_context *ctdb_db;
3250 struct cattdb_data d;
3260 if (db_exists(ctdb, db_name, &persistent)) {
3261 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3265 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, false, 0);
3267 if (ctdb_db == NULL) {
3268 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3272 /* traverse the local tdb */
3275 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3276 printf("Failed to cattdb data\n");
3279 talloc_free(ctdb_db);
3281 printf("Dumped %d records\n", d.count);
3286 display the content of a database key
3288 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3290 const char *db_name;
3291 struct ctdb_db_context *ctdb_db;
3292 struct ctdb_record_handle *h;
3293 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3303 if (db_exists(ctdb, db_name, &persistent)) {
3304 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3308 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3310 if (ctdb_db == NULL) {
3311 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3315 key.dptr = discard_const(argv[1]);
3316 key.dsize = strlen((char *)key.dptr);
3318 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3320 printf("Failed to fetch record '%s' on node %d\n",
3321 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3322 talloc_free(tmp_ctx);
3326 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3328 talloc_free(ctdb_db);
3329 talloc_free(tmp_ctx);
3334 display the content of a database key
3336 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3338 const char *db_name;
3339 struct ctdb_db_context *ctdb_db;
3340 struct ctdb_record_handle *h;
3341 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3351 if (db_exists(ctdb, db_name, &persistent)) {
3352 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3356 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3358 if (ctdb_db == NULL) {
3359 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3363 key.dptr = discard_const(argv[1]);
3364 key.dsize = strlen((char *)key.dptr);
3366 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3368 printf("Failed to fetch record '%s' on node %d\n",
3369 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3370 talloc_free(tmp_ctx);
3374 data.dptr = discard_const(argv[2]);
3375 data.dsize = strlen((char *)data.dptr);
3377 if (ctdb_record_store(h, data) != 0) {
3378 printf("Failed to store record\n");
3382 talloc_free(ctdb_db);
3383 talloc_free(tmp_ctx);
3388 fetch a record from a persistent database
3390 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3392 const char *db_name;
3393 struct ctdb_db_context *ctdb_db;
3394 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3395 struct ctdb_transaction_handle *h;
3401 talloc_free(tmp_ctx);
3408 if (db_exists(ctdb, db_name, &persistent)) {
3409 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3410 talloc_free(tmp_ctx);
3415 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3416 talloc_free(tmp_ctx);
3420 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3422 if (ctdb_db == NULL) {
3423 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3424 talloc_free(tmp_ctx);
3428 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3430 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3431 talloc_free(tmp_ctx);
3435 key.dptr = discard_const(argv[1]);
3436 key.dsize = strlen(argv[1]);
3437 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3439 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3440 talloc_free(tmp_ctx);
3444 if (data.dsize == 0 || data.dptr == NULL) {
3445 DEBUG(DEBUG_ERR,("Record is empty\n"));
3446 talloc_free(tmp_ctx);
3451 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3453 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3454 talloc_free(tmp_ctx);
3457 write(fd, data.dptr, data.dsize);
3460 write(1, data.dptr, data.dsize);
3463 /* abort the transaction */
3467 talloc_free(tmp_ctx);
3472 fetch a record from a tdb-file
3474 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3476 const char *tdb_file;
3479 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3488 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3490 fprintf(stderr, "Failed to open TDB file %s\n", tdb_file);
3494 if (!strncmp(argv[1], "0x", 2)) {
3495 key = hextodata(tmp_ctx, argv[1] + 2);
3496 if (key.dsize == 0) {
3497 fprintf(stderr, "Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3501 key.dptr = discard_const(argv[1]);
3502 key.dsize = strlen(argv[1]);
3504 data = tdb_fetch(tdb, key);
3505 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3506 fprintf(stderr, "Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3514 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3516 printf("Failed to open output file %s\n", argv[2]);
3519 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3522 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3525 talloc_free(tmp_ctx);
3530 write a record to a persistent database
3532 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3534 const char *db_name;
3535 struct ctdb_db_context *ctdb_db;
3536 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3537 struct ctdb_transaction_handle *h;
3543 talloc_free(tmp_ctx);
3547 fd = open(argv[2], O_RDONLY);
3549 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3550 talloc_free(tmp_ctx);
3554 ret = fstat(fd, &st);
3556 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3558 talloc_free(tmp_ctx);
3562 if (!S_ISREG(st.st_mode)) {
3563 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3565 talloc_free(tmp_ctx);
3569 data.dsize = st.st_size;
3570 if (data.dsize == 0) {
3573 data.dptr = talloc_size(tmp_ctx, data.dsize);
3574 if (data.dptr == NULL) {
3575 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3577 talloc_free(tmp_ctx);
3580 ret = read(fd, data.dptr, data.dsize);
3581 if (ret != data.dsize) {
3582 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3584 talloc_free(tmp_ctx);
3593 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3594 if (ctdb_db == NULL) {
3595 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3596 talloc_free(tmp_ctx);
3600 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3602 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3603 talloc_free(tmp_ctx);
3607 key.dptr = discard_const(argv[1]);
3608 key.dsize = strlen(argv[1]);
3609 ret = ctdb_transaction_store(h, key, data);
3611 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3612 talloc_free(tmp_ctx);
3616 ret = ctdb_transaction_commit(h);
3618 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3619 talloc_free(tmp_ctx);
3624 talloc_free(tmp_ctx);
3629 check if a service is bound to a port or not
3631 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3636 struct sockaddr_in sin;
3639 printf("Use: ctdb chktcport <port>\n");
3643 port = atoi(argv[0]);
3645 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3647 printf("Failed to open local socket\n");
3651 v = fcntl(s, F_GETFL, 0);
3652 fcntl(s, F_SETFL, v | O_NONBLOCK);
3654 bzero(&sin, sizeof(sin));
3655 sin.sin_family = PF_INET;
3656 sin.sin_port = htons(port);
3657 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3660 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3669 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3670 TDB_DATA data, void *private_data)
3672 DEBUG(DEBUG_ERR,("Log data received\n"));
3673 if (data.dsize > 0) {
3674 printf("%s", data.dptr);
3681 display a list of log messages from the in memory ringbuffer
3683 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3687 struct ctdb_get_log_addr log_addr;
3689 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3694 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3695 talloc_free(tmp_ctx);
3699 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3700 log_addr.srvid = getpid();
3701 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3702 log_addr.level = get_debug_by_desc(argv[0]);
3704 log_addr.level = strtol(argv[0], NULL, 0);
3708 data.dptr = (unsigned char *)&log_addr;
3709 data.dsize = sizeof(log_addr);
3711 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3713 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3716 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3718 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3719 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3720 if (ret != 0 || res != 0) {
3721 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3722 talloc_free(tmp_ctx);
3727 tv = timeval_current();
3728 /* this loop will terminate when we have received the reply */
3729 while (timeval_elapsed(&tv) < 3.0) {
3730 event_loop_once(ctdb->ev);
3733 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3735 talloc_free(tmp_ctx);
3740 clear the in memory log area
3742 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3747 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3749 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3750 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3751 if (ret != 0 || res != 0) {
3752 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3753 talloc_free(tmp_ctx);
3757 talloc_free(tmp_ctx);
3764 display a list of the databases on a remote ctdb
3766 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3769 struct ctdb_dbid_map *dbmap=NULL;
3771 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3773 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3777 if(options.machinereadable){
3778 printf(":ID:Name:Path:Persistent:Unhealthy:ReadOnly:\n");
3779 for(i=0;i<dbmap->num;i++){
3786 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3787 dbmap->dbs[i].dbid, ctdb, &path);
3788 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3789 dbmap->dbs[i].dbid, ctdb, &name);
3790 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3791 dbmap->dbs[i].dbid, ctdb, &health);
3792 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3793 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3794 printf(":0x%08X:%s:%s:%d:%d:%d:\n",
3795 dbmap->dbs[i].dbid, name, path,
3796 !!(persistent), !!(health), !!(readonly));
3801 printf("Number of databases:%d\n", dbmap->num);
3802 for(i=0;i<dbmap->num;i++){
3809 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3810 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3811 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3812 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3813 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3814 printf("dbid:0x%08x name:%s path:%s%s%s%s\n",
3815 dbmap->dbs[i].dbid, name, path,
3816 persistent?" PERSISTENT":"",
3817 readonly?" READONLY":"",
3818 health?" UNHEALTHY":"");
3825 display the status of a database on a remote ctdb
3827 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3830 struct ctdb_dbid_map *dbmap=NULL;
3831 const char *db_name;
3839 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3841 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3845 for(i=0;i<dbmap->num;i++){
3852 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3853 if (strcmp(name, db_name) != 0) {
3857 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3858 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3859 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3860 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3861 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nREADONLY: %s\nHEALTH: %s\n",
3862 dbmap->dbs[i].dbid, name, path,
3863 persistent?"yes":"no",
3864 readonly?"yes":"no",
3865 health?health:"OK");
3869 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3874 check if the local node is recmaster or not
3875 it will return 1 if this node is the recmaster and 0 if it is not
3876 or if the local ctdb daemon could not be contacted
3878 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3880 uint32_t mypnn, recmaster;
3882 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3884 printf("Failed to get pnn of node\n");
3888 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
3889 printf("Failed to get the recmaster\n");
3893 if (recmaster != mypnn) {
3894 printf("this node is not the recmaster\n");
3898 printf("this node is the recmaster\n");
3905 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3908 struct timeval tv = timeval_current();
3909 ret = ctdb_ctrl_ping(ctdb, options.pnn);
3911 printf("Unable to get ping response from node %u\n", options.pnn);
3914 printf("response from %u time=%.6f sec (%d clients)\n",
3915 options.pnn, timeval_elapsed(&tv), ret);
3924 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3935 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3937 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3941 printf("%-23s = %u\n", name, value);
3948 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3959 value = strtoul(argv[1], NULL, 0);
3961 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3963 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3972 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3978 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3980 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3984 for (i=0;i<count;i++) {
3985 control_getvar(ctdb, 1, &list[i]);
3994 display debug level on a node
3996 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4001 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4003 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4006 if (options.machinereadable){
4007 printf(":Name:Level:\n");
4008 printf(":%s:%d:\n",get_debug_by_level(level),level);
4010 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4017 display reclock file of a node
4019 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4022 const char *reclock;
4024 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4026 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4029 if (options.machinereadable){
4030 if (reclock != NULL) {
4031 printf("%s", reclock);
4034 if (reclock == NULL) {
4035 printf("No reclock file used.\n");
4037 printf("Reclock file:%s\n", reclock);
4045 set the reclock file of a node
4047 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4050 const char *reclock;
4054 } else if (argc == 1) {
4060 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4062 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4069 set the natgw state on/off
4071 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4074 uint32_t natgwstate;
4080 if (!strcmp(argv[0], "on")) {
4082 } else if (!strcmp(argv[0], "off")) {
4088 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4090 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4098 set the lmaster role on/off
4100 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4103 uint32_t lmasterrole;
4109 if (!strcmp(argv[0], "on")) {
4111 } else if (!strcmp(argv[0], "off")) {
4117 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4119 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4127 set the recmaster role on/off
4129 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4132 uint32_t recmasterrole;
4138 if (!strcmp(argv[0], "on")) {
4140 } else if (!strcmp(argv[0], "off")) {
4146 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4148 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4156 set debug level on a node or all nodes
4158 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4164 printf("You must specify the debug level. Valid levels are:\n");
4165 for (i=0; debug_levels[i].description != NULL; i++) {
4166 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4172 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4173 level = get_debug_by_desc(argv[0]);
4175 level = strtol(argv[0], NULL, 0);
4178 for (i=0; debug_levels[i].description != NULL; i++) {
4179 if (level == debug_levels[i].level) {
4183 if (debug_levels[i].description == NULL) {
4184 printf("Invalid debug level, must be one of\n");
4185 for (i=0; debug_levels[i].description != NULL; i++) {
4186 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4191 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4193 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4202 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4208 priority = strtol(argv[0], NULL, 0);
4212 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4214 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4216 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4223 attach to a database
4225 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4227 const char *db_name;
4228 struct ctdb_db_context *ctdb_db;
4229 bool persistent = false;
4239 if (strcmp(argv[1], "persistent") != 0) {
4245 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4246 if (ctdb_db == NULL) {
4247 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4257 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4259 struct ctdb_db_priority db_prio;
4266 db_prio.db_id = strtoul(argv[0], NULL, 0);
4267 db_prio.priority = strtoul(argv[1], NULL, 0);
4269 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4271 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4281 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4283 uint32_t db_id, priority;
4290 db_id = strtoul(argv[0], NULL, 0);
4292 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4294 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4298 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4304 set the readonly capability for a database
4306 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4315 db_id = strtoul(argv[0], NULL, 0);
4317 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4319 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4329 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4339 db_id = strtoul(argv[0], NULL, 0);
4341 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4343 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4347 printf("Sequence number:%lld\n", (long long)seqnum);
4353 run an eventscript on a node
4355 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4361 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4364 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4368 data.dptr = (unsigned char *)discard_const(argv[0]);
4369 data.dsize = strlen((char *)data.dptr) + 1;
4371 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4373 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4374 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4375 if (ret != 0 || res != 0) {
4376 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4377 talloc_free(tmp_ctx);
4380 talloc_free(tmp_ctx);
4384 #define DB_VERSION 1
4385 #define MAX_DB_NAME 64
4386 struct db_file_header {
4387 unsigned long version;
4389 unsigned long persistent;
4391 const char name[MAX_DB_NAME];
4394 struct backup_data {
4395 struct ctdb_marshall_buffer *records;
4398 bool traverse_error;
4401 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4403 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4404 struct ctdb_rec_data *rec;
4406 /* add the record */
4407 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4409 bd->traverse_error = true;
4410 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4413 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4414 if (bd->records == NULL) {
4415 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4416 bd->traverse_error = true;
4419 bd->records->count++;
4420 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4421 bd->len += rec->length;
4429 * backup a database to a file
4431 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4434 struct ctdb_dbid_map *dbmap=NULL;
4435 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4436 struct db_file_header dbhdr;
4437 struct ctdb_db_context *ctdb_db;
4438 struct backup_data *bd;
4441 const char *reason = NULL;
4444 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4448 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4450 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4454 for(i=0;i<dbmap->num;i++){
4457 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4458 if(!strcmp(argv[0], name)){
4459 talloc_free(discard_const(name));
4462 talloc_free(discard_const(name));
4464 if (i == dbmap->num) {
4465 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4466 talloc_free(tmp_ctx);
4470 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4471 dbmap->dbs[i].dbid, tmp_ctx, &reason);
4473 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4475 talloc_free(tmp_ctx);
4479 uint32_t allow_unhealthy = 0;
4481 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4482 "AllowUnhealthyDBRead",
4485 if (allow_unhealthy != 1) {
4486 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4489 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4491 talloc_free(tmp_ctx);
4495 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4497 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4498 "tunnable AllowUnhealthyDBRead = %u\n",
4502 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4503 if (ctdb_db == NULL) {
4504 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4505 talloc_free(tmp_ctx);
4510 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4512 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4513 talloc_free(tmp_ctx);
4518 bd = talloc_zero(tmp_ctx, struct backup_data);
4520 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4521 talloc_free(tmp_ctx);
4525 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4526 if (bd->records == NULL) {
4527 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4528 talloc_free(tmp_ctx);
4532 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4533 bd->records->db_id = ctdb_db->db_id;
4534 /* traverse the database collecting all records */
4535 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4536 bd->traverse_error) {
4537 DEBUG(DEBUG_ERR,("Traverse error\n"));
4538 talloc_free(tmp_ctx);
4542 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4545 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4547 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4548 talloc_free(tmp_ctx);
4552 dbhdr.version = DB_VERSION;
4553 dbhdr.timestamp = time(NULL);
4554 dbhdr.persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4555 dbhdr.size = bd->len;
4556 if (strlen(argv[0]) >= MAX_DB_NAME) {
4557 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4560 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4561 ret = write(fh, &dbhdr, sizeof(dbhdr));
4563 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4566 ret = write(fh, bd->records, bd->len);
4568 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4577 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4581 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4583 talloc_free(tmp_ctx);
4588 * restore a database from a file
4590 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4593 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4596 struct db_file_header dbhdr;
4597 struct ctdb_db_context *ctdb_db;
4598 struct ctdb_node_map *nodemap=NULL;
4599 struct ctdb_vnn_map *vnnmap=NULL;
4601 struct ctdb_control_wipe_database w;
4603 uint32_t generation;
4608 if (argc < 1 || argc > 2) {
4609 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4613 fh = open(argv[0], O_RDONLY);
4615 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4616 talloc_free(tmp_ctx);
4620 read(fh, &dbhdr, sizeof(dbhdr));
4621 if (dbhdr.version != DB_VERSION) {
4622 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4623 talloc_free(tmp_ctx);
4627 dbname = discard_const(dbhdr.name);
4629 dbname = discard_const(argv[1]);
4632 outdata.dsize = dbhdr.size;
4633 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4634 if (outdata.dptr == NULL) {
4635 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4637 talloc_free(tmp_ctx);
4640 read(fh, outdata.dptr, outdata.dsize);
4643 tm = localtime(&dbhdr.timestamp);
4644 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4645 printf("Restoring database '%s' from backup @ %s\n",
4649 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4650 if (ctdb_db == NULL) {
4651 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4652 talloc_free(tmp_ctx);
4656 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4658 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4659 talloc_free(tmp_ctx);
4664 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4666 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4667 talloc_free(tmp_ctx);
4671 /* freeze all nodes */
4672 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4673 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4674 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4680 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4681 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4682 talloc_free(tmp_ctx);
4687 generation = vnnmap->generation;
4688 data.dptr = (void *)&generation;
4689 data.dsize = sizeof(generation);
4691 /* start a cluster wide transaction */
4692 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4693 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4695 TIMELIMIT(), false, data,
4698 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4703 w.db_id = ctdb_db->db_id;
4704 w.transaction_id = generation;
4706 data.dptr = (void *)&w;
4707 data.dsize = sizeof(w);
4709 /* wipe all the remote databases. */
4710 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4711 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4713 TIMELIMIT(), false, data,
4716 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4717 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4718 talloc_free(tmp_ctx);
4722 /* push the database */
4723 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4724 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4726 TIMELIMIT(), false, outdata,
4729 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4730 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4731 talloc_free(tmp_ctx);
4735 data.dptr = (void *)&ctdb_db->db_id;
4736 data.dsize = sizeof(ctdb_db->db_id);
4738 /* mark the database as healthy */
4739 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4740 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4742 TIMELIMIT(), false, data,
4745 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4746 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4747 talloc_free(tmp_ctx);
4751 data.dptr = (void *)&generation;
4752 data.dsize = sizeof(generation);
4754 /* commit all the changes */
4755 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4757 TIMELIMIT(), false, data,
4760 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4761 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4762 talloc_free(tmp_ctx);
4767 /* thaw all nodes */
4768 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4769 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4775 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4776 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4777 talloc_free(tmp_ctx);
4782 talloc_free(tmp_ctx);
4787 * dump a database backup from a file
4789 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4791 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4793 struct db_file_header dbhdr;
4797 struct ctdb_rec_data *rec = NULL;
4798 struct ctdb_marshall_buffer *m;
4799 struct ctdb_dump_db_context c;
4802 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4806 fh = open(argv[0], O_RDONLY);
4808 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4809 talloc_free(tmp_ctx);
4813 read(fh, &dbhdr, sizeof(dbhdr));
4814 if (dbhdr.version != DB_VERSION) {
4815 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4816 talloc_free(tmp_ctx);
4820 outdata.dsize = dbhdr.size;
4821 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4822 if (outdata.dptr == NULL) {
4823 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4825 talloc_free(tmp_ctx);
4828 read(fh, outdata.dptr, outdata.dsize);
4830 m = (struct ctdb_marshall_buffer *)outdata.dptr;
4832 tm = localtime(&dbhdr.timestamp);
4833 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4834 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4835 dbhdr.name, m->db_id, tbuf);
4839 c.printemptyrecords = (bool)options.printemptyrecords;
4840 c.printdatasize = (bool)options.printdatasize;
4841 c.printlmaster = false;
4842 c.printhash = (bool)options.printhash;
4843 c.printrecordflags = (bool)options.printrecordflags;
4845 for (i=0; i < m->count; i++) {
4849 /* we do not want the header splitted, so we pass NULL*/
4850 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4853 ctdb_dumpdb_record(ctdb, key, data, &c);
4856 printf("Dumped %d records\n", i);
4857 talloc_free(tmp_ctx);
4862 * wipe a database from a file
4864 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4868 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4870 struct ctdb_db_context *ctdb_db;
4871 struct ctdb_node_map *nodemap = NULL;
4872 struct ctdb_vnn_map *vnnmap = NULL;
4874 struct ctdb_control_wipe_database w;
4876 uint32_t generation;
4877 struct ctdb_dbid_map *dbmap = NULL;
4880 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4884 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4887 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4892 for(i=0;i<dbmap->num;i++){
4895 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4896 dbmap->dbs[i].dbid, tmp_ctx, &name);
4897 if(!strcmp(argv[0], name)){
4898 talloc_free(discard_const(name));
4901 talloc_free(discard_const(name));
4903 if (i == dbmap->num) {
4904 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4906 talloc_free(tmp_ctx);
4910 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4911 if (ctdb_db == NULL) {
4912 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4914 talloc_free(tmp_ctx);
4918 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4921 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4923 talloc_free(tmp_ctx);
4927 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4930 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4932 talloc_free(tmp_ctx);
4936 /* freeze all nodes */
4937 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4938 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4939 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4946 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4947 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4948 CTDB_RECOVERY_ACTIVE);
4949 talloc_free(tmp_ctx);
4954 generation = vnnmap->generation;
4955 data.dptr = (void *)&generation;
4956 data.dsize = sizeof(generation);
4958 /* start a cluster wide transaction */
4959 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4960 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4962 TIMELIMIT(), false, data,
4966 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4967 "transactions.\n"));
4971 w.db_id = ctdb_db->db_id;
4972 w.transaction_id = generation;
4974 data.dptr = (void *)&w;
4975 data.dsize = sizeof(w);
4977 /* wipe all the remote databases. */
4978 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4979 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4981 TIMELIMIT(), false, data,
4984 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4985 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4986 talloc_free(tmp_ctx);
4990 data.dptr = (void *)&ctdb_db->db_id;
4991 data.dsize = sizeof(ctdb_db->db_id);
4993 /* mark the database as healthy */
4994 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4995 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4997 TIMELIMIT(), false, data,
5000 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5001 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5002 talloc_free(tmp_ctx);
5006 data.dptr = (void *)&generation;
5007 data.dsize = sizeof(generation);
5009 /* commit all the changes */
5010 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5012 TIMELIMIT(), false, data,
5015 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5016 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5017 talloc_free(tmp_ctx);
5021 /* thaw all nodes */
5022 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5023 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5029 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5030 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5031 talloc_free(tmp_ctx);
5035 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5037 talloc_free(tmp_ctx);
5044 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5050 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5051 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5052 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5053 if (ret != 0 || res != 0) {
5054 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5055 talloc_free(tmp_ctx);
5058 write(1, data.dptr, data.dsize);
5059 talloc_free(tmp_ctx);
5064 handler for memory dumps
5066 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5067 TDB_DATA data, void *private_data)
5069 write(1, data.dptr, data.dsize);
5074 dump memory usage on the recovery daemon
5076 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5080 struct rd_memdump_reply rd;
5082 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5084 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
5087 rd.srvid = getpid();
5089 /* register a message port for receiveing the reply so that we
5090 can receive the reply
5092 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5095 data.dptr = (uint8_t *)&rd;
5096 data.dsize = sizeof(rd);
5098 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5100 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5104 /* this loop will terminate when we have received the reply */
5106 event_loop_once(ctdb->ev);
5113 send a message to a srvid
5115 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5117 unsigned long srvid;
5125 srvid = strtoul(argv[0], NULL, 0);
5127 data.dptr = (uint8_t *)discard_const(argv[1]);
5128 data.dsize= strlen(argv[1]);
5130 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5132 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5140 handler for msglisten
5142 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5143 TDB_DATA data, void *private_data)
5147 printf("Message received: ");
5148 for (i=0;i<data.dsize;i++) {
5149 printf("%c", data.dptr[i]);
5155 listen for messages on a messageport
5157 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5163 /* register a message port and listen for messages
5165 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5166 printf("Listening for messages on srvid:%d\n", (int)srvid);
5169 event_loop_once(ctdb->ev);
5176 list all nodes in the cluster
5177 we parse the nodes file directly
5179 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5181 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5182 struct pnn_node *pnn_nodes;
5183 struct pnn_node *pnn_node;
5185 pnn_nodes = read_nodes_file(mem_ctx);
5186 if (pnn_nodes == NULL) {
5187 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5188 talloc_free(mem_ctx);
5192 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5193 ctdb_sock_addr addr;
5194 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5195 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5196 talloc_free(mem_ctx);
5199 if (options.machinereadable){
5200 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5202 printf("%s\n", pnn_node->addr);
5205 talloc_free(mem_ctx);
5211 reload the nodes file on the local node
5213 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5217 struct ctdb_node_map *nodemap=NULL;
5219 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5221 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
5225 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5227 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5231 /* reload the nodes file on all remote nodes */
5232 for (i=0;i<nodemap->num;i++) {
5233 if (nodemap->nodes[i].pnn == mypnn) {
5236 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5237 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5238 nodemap->nodes[i].pnn);
5240 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5244 /* reload the nodes file on the local node */
5245 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5246 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5248 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5251 /* initiate a recovery */
5252 control_recover(ctdb, argc, argv);
5258 static const struct {
5260 int (*fn)(struct ctdb_context *, int, const char **);
5262 bool without_daemon; /* can be run without daemon running ? */
5265 } ctdb_commands[] = {
5267 { "version", control_version, true, false, "show version of ctdb" },
5269 { "status", control_status, true, false, "show node status" },
5270 { "uptime", control_uptime, true, false, "show node uptime" },
5271 { "ping", control_ping, true, false, "ping all nodes" },
5272 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5273 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5274 { "listvars", control_listvars, true, false, "list tunable variables"},
5275 { "statistics", control_statistics, false, false, "show statistics" },
5276 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5277 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5278 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5279 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5280 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5281 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5282 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5283 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5284 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
5285 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname>"},
5286 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname>"},
5287 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5288 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5289 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5290 { "lvs", control_lvs, true, false, "show lvs configuration" },
5291 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5292 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5293 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5294 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5295 { "getdebug", control_getdebug, true, false, "get debug level" },
5296 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
5297 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
5298 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5299 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5300 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5301 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5302 { "disable", control_disable, true, false, "disable a nodes public IP" },
5303 { "enable", control_enable, true, false, "enable a nodes public IP" },
5304 { "stop", control_stop, true, false, "stop a node" },
5305 { "continue", control_continue, true, false, "re-start a stopped node" },
5306 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5307 { "unban", control_unban, true, false, "unban a node" },
5308 { "showban", control_showban, true, false, "show ban information"},
5309 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5310 { "recover", control_recover, true, false, "force recovery" },
5311 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
5312 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5313 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5314 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5315 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5316 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5317 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5318 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5319 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5321 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5323 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5324 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5325 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5326 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5327 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5328 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
5329 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5330 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5331 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5332 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5333 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5334 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5335 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5336 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
5337 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5338 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5339 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
5340 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
5341 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5342 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5343 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5344 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5345 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5346 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5347 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5348 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5349 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5350 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5351 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
5352 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
5353 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbid>"},
5354 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5355 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5356 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5357 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
5358 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
5359 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
5360 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5361 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5362 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5363 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbid>" },
5364 { "nodestatus", control_nodestatus, true, false, "show and return node status" },
5370 static void usage(void)
5374 "Usage: ctdb [options] <control>\n" \
5376 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5377 " -Y generate machinereadable output\n"
5378 " -v generate verbose output\n"
5379 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5380 printf("Controls:\n");
5381 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5382 printf(" %-15s %-27s %s\n",
5383 ctdb_commands[i].name,
5384 ctdb_commands[i].args?ctdb_commands[i].args:"",
5385 ctdb_commands[i].msg);
5391 static void ctdb_alarm(int sig)
5393 printf("Maximum runtime exceeded - exiting\n");
5400 int main(int argc, const char *argv[])
5402 struct ctdb_context *ctdb;
5403 char *nodestring = NULL;
5404 struct poptOption popt_options[] = {
5407 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5408 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5409 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
5410 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5411 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5412 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
5413 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
5414 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
5415 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
5416 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
5420 const char **extra_argv;
5424 struct event_context *ev;
5425 const char *control;
5426 const char *socket_name;
5430 /* set some defaults */
5431 options.maxruntime = 0;
5432 options.timelimit = 3;
5433 options.pnn = CTDB_CURRENT_NODE;
5435 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5437 while ((opt = poptGetNextOpt(pc)) != -1) {
5440 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5441 poptBadOption(pc, 0), poptStrerror(opt)));
5446 /* setup the remaining options for the main program to use */
5447 extra_argv = poptGetArgs(pc);
5450 while (extra_argv[extra_argc]) extra_argc++;
5453 if (extra_argc < 1) {
5457 if (options.maxruntime == 0) {
5458 const char *ctdb_timeout;
5459 ctdb_timeout = getenv("CTDB_TIMEOUT");
5460 if (ctdb_timeout != NULL) {
5461 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5463 /* default timeout is 120 seconds */
5464 options.maxruntime = 120;
5468 signal(SIGALRM, ctdb_alarm);
5469 alarm(options.maxruntime);
5471 control = extra_argv[0];
5473 ev = event_context_init(NULL);
5475 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5478 tevent_loop_allow_nesting(ev);
5480 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5481 if (strcmp(control, ctdb_commands[i].name) == 0) {
5486 if (i == ARRAY_SIZE(ctdb_commands)) {
5487 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5491 if (ctdb_commands[i].without_daemon == true) {
5492 if (nodestring != NULL) {
5493 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
5497 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
5500 /* initialise ctdb */
5501 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5504 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5508 /* initialize a libctdb connection as well */
5509 socket_name = ctdb_get_socketname(ctdb);
5510 ctdb_connection = ctdb_connect(socket_name,
5511 ctdb_log_file, stderr);
5512 if (ctdb_connection == NULL) {
5513 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
5517 /* setup the node number(s) to contact */
5518 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
5519 &options.nodes, &options.pnn)) {
5523 if (options.pnn == CTDB_CURRENT_NODE) {
5524 options.pnn = options.nodes[0];
5527 if (ctdb_commands[i].auto_all &&
5528 ((options.pnn == CTDB_BROADCAST_ALL) ||
5529 (options.pnn == CTDB_MULTICAST))) {
5533 for (j = 0; j < talloc_array_length(options.nodes); j++) {
5534 options.pnn = options.nodes[j];
5535 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5538 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5541 ctdb_disconnect(ctdb_connection);
5543 (void)poptFreeContext(pc);