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);
51 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
52 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
55 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
58 #define XSTR(x) STR(x)
59 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
66 verify that a node exists and is reachable
68 static void verify_node(struct ctdb_context *ctdb)
71 struct ctdb_node_map *nodemap=NULL;
73 if (options.pnn == CTDB_CURRENT_NODE) {
76 if (options.pnn == CTDB_BROADCAST_ALL) {
80 /* verify the node exists */
81 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
82 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
85 if (options.pnn >= nodemap->num) {
86 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
89 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DELETED) {
90 DEBUG(DEBUG_ERR, ("Node %u is DELETED\n", options.pnn));
93 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
94 DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
98 /* verify we can access the node */
99 ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
101 DEBUG(DEBUG_ERR,("Can not access node. Node is not operational.\n"));
107 check if a database exists
109 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
112 struct ctdb_dbid_map *dbmap=NULL;
114 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
116 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
120 for(i=0;i<dbmap->num;i++){
123 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
124 if (!strcmp(name, db_name)) {
133 see if a process exists
135 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
143 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
144 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
148 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
150 printf("%u:%u exists\n", pnn, pid);
152 printf("%u:%u does not exist\n", pnn, pid);
158 display statistics structure
160 static void show_statistics(struct ctdb_statistics *s, int show_header)
162 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
164 const char *prefix=NULL;
166 int tmp, days, hours, minutes, seconds;
171 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
172 STATISTICS_FIELD(num_clients),
173 STATISTICS_FIELD(frozen),
174 STATISTICS_FIELD(recovering),
175 STATISTICS_FIELD(num_recoveries),
176 STATISTICS_FIELD(client_packets_sent),
177 STATISTICS_FIELD(client_packets_recv),
178 STATISTICS_FIELD(node_packets_sent),
179 STATISTICS_FIELD(node_packets_recv),
180 STATISTICS_FIELD(keepalive_packets_sent),
181 STATISTICS_FIELD(keepalive_packets_recv),
182 STATISTICS_FIELD(node.req_call),
183 STATISTICS_FIELD(node.reply_call),
184 STATISTICS_FIELD(node.req_dmaster),
185 STATISTICS_FIELD(node.reply_dmaster),
186 STATISTICS_FIELD(node.reply_error),
187 STATISTICS_FIELD(node.req_message),
188 STATISTICS_FIELD(node.req_control),
189 STATISTICS_FIELD(node.reply_control),
190 STATISTICS_FIELD(client.req_call),
191 STATISTICS_FIELD(client.req_message),
192 STATISTICS_FIELD(client.req_control),
193 STATISTICS_FIELD(timeouts.call),
194 STATISTICS_FIELD(timeouts.control),
195 STATISTICS_FIELD(timeouts.traverse),
196 STATISTICS_FIELD(total_calls),
197 STATISTICS_FIELD(pending_calls),
198 STATISTICS_FIELD(lockwait_calls),
199 STATISTICS_FIELD(pending_lockwait_calls),
200 STATISTICS_FIELD(childwrite_calls),
201 STATISTICS_FIELD(pending_childwrite_calls),
202 STATISTICS_FIELD(memory_used),
203 STATISTICS_FIELD(max_hop_count),
205 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
214 if (options.machinereadable){
216 printf("CTDB version:");
217 printf("Current time of statistics:");
218 printf("Statistics collected since:");
219 for (i=0;i<ARRAY_SIZE(fields);i++) {
220 printf("%s:", fields[i].name);
222 printf("num_reclock_ctdbd_latency:");
223 printf("min_reclock_ctdbd_latency:");
224 printf("avg_reclock_ctdbd_latency:");
225 printf("max_reclock_ctdbd_latency:");
227 printf("num_reclock_recd_latency:");
228 printf("min_reclock_recd_latency:");
229 printf("avg_reclock_recd_latency:");
230 printf("max_reclock_recd_latency:");
232 printf("num_call_latency:");
233 printf("min_call_latency:");
234 printf("avg_call_latency:");
235 printf("max_call_latency:");
237 printf("num_lockwait_latency:");
238 printf("min_lockwait_latency:");
239 printf("avg_lockwait_latency:");
240 printf("max_lockwait_latency:");
242 printf("num_childwrite_latency:");
243 printf("min_childwrite_latency:");
244 printf("avg_childwrite_latency:");
245 printf("max_childwrite_latency:");
248 printf("%d:", CTDB_VERSION);
249 printf("%d:", (int)s->statistics_current_time.tv_sec);
250 printf("%d:", (int)s->statistics_start_time.tv_sec);
251 for (i=0;i<ARRAY_SIZE(fields);i++) {
252 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
254 printf("%d:", s->reclock.ctdbd.num);
255 printf("%.6f:", s->reclock.ctdbd.min);
256 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
257 printf("%.6f:", s->reclock.ctdbd.max);
259 printf("%d:", s->reclock.recd.num);
260 printf("%.6f:", s->reclock.recd.min);
261 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
262 printf("%.6f:", s->reclock.recd.max);
264 printf("%d:", s->call_latency.num);
265 printf("%.6f:", s->call_latency.min);
266 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
267 printf("%.6f:", s->call_latency.max);
269 printf("%d:", s->lockwait_latency.num);
270 printf("%.6f:", s->lockwait_latency.min);
271 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
272 printf("%.6f:", s->lockwait_latency.max);
274 printf("%d:", s->childwrite_latency.num);
275 printf("%.6f:", s->childwrite_latency.min);
276 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
277 printf("%.6f:", s->childwrite_latency.max);
280 printf("CTDB version %u\n", CTDB_VERSION);
281 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
282 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
284 for (i=0;i<ARRAY_SIZE(fields);i++) {
285 if (strchr(fields[i].name, '.')) {
286 preflen = strcspn(fields[i].name, ".")+1;
287 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
288 prefix = fields[i].name;
289 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
294 printf(" %*s%-22s%*s%10u\n",
296 fields[i].name+preflen,
298 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
300 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);
302 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);
304 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);
305 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);
306 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);
309 talloc_free(tmp_ctx);
313 display remote ctdb statistics combined from all nodes
315 static int control_statistics_all(struct ctdb_context *ctdb)
318 struct ctdb_statistics statistics;
322 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
323 CTDB_NO_MEMORY(ctdb, nodes);
325 ZERO_STRUCT(statistics);
327 for (i=0;i<num_nodes;i++) {
328 struct ctdb_statistics s1;
330 uint32_t *v1 = (uint32_t *)&s1;
331 uint32_t *v2 = (uint32_t *)&statistics;
333 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
334 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
336 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
339 for (j=0;j<num_ints;j++) {
342 statistics.max_hop_count =
343 MAX(statistics.max_hop_count, s1.max_hop_count);
344 statistics.call_latency.max =
345 MAX(statistics.call_latency.max, s1.call_latency.max);
346 statistics.lockwait_latency.max =
347 MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
350 printf("Gathered statistics for %u nodes\n", num_nodes);
351 show_statistics(&statistics, 1);
356 display remote ctdb statistics
358 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
361 struct ctdb_statistics statistics;
363 if (options.pnn == CTDB_BROADCAST_ALL) {
364 return control_statistics_all(ctdb);
367 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
369 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
372 show_statistics(&statistics, 1);
378 reset remote ctdb statistics
380 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
384 ret = ctdb_statistics_reset(ctdb, options.pnn);
386 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
394 display remote ctdb rolling statistics
396 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
399 struct ctdb_statistics_wire *stats;
400 int i, num_records = -1;
403 num_records = atoi(argv[0]) - 1;
406 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
408 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
411 for (i=0;i<stats->num;i++) {
412 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
415 show_statistics(&stats->stats[i], i==0);
416 if (i == num_records) {
425 display uptime of remote node
427 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
430 struct ctdb_uptime *uptime = NULL;
431 int tmp, days, hours, minutes, seconds;
433 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
435 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
439 if (options.machinereadable){
440 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
441 printf(":%u:%u:%u:%lf\n",
442 (unsigned int)uptime->current_time.tv_sec,
443 (unsigned int)uptime->ctdbd_start_time.tv_sec,
444 (unsigned int)uptime->last_recovery_finished.tv_sec,
445 timeval_delta(&uptime->last_recovery_finished,
446 &uptime->last_recovery_started)
451 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
453 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
461 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
463 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
471 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
473 printf("Duration of last recovery/failover: %lf seconds\n",
474 timeval_delta(&uptime->last_recovery_finished,
475 &uptime->last_recovery_started));
481 show the PNN of the current node
483 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
488 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
490 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
494 printf("PNN:%d\n", mypnn);
500 struct pnn_node *next;
505 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
507 const char *nodes_list;
511 struct pnn_node *pnn_nodes = NULL;
512 struct pnn_node *pnn_node;
513 struct pnn_node *tmp_node;
515 /* read the nodes file */
516 nodes_list = getenv("CTDB_NODES");
517 if (nodes_list == NULL) {
518 nodes_list = "/etc/ctdb/nodes";
520 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
524 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
527 for (i=0, pnn=0; i<nlines; i++) {
531 /* strip leading spaces */
532 while((*node == ' ') || (*node == '\t')) {
539 if (strcmp(node, "") == 0) {
542 pnn_node = talloc(mem_ctx, struct pnn_node);
543 pnn_node->pnn = pnn++;
544 pnn_node->addr = talloc_strdup(pnn_node, node);
545 pnn_node->next = pnn_nodes;
546 pnn_nodes = pnn_node;
549 /* swap them around so we return them in incrementing order */
550 pnn_node = pnn_nodes;
554 pnn_node = pnn_node->next;
556 tmp_node->next = pnn_nodes;
557 pnn_nodes = tmp_node;
564 show the PNN of the current node
565 discover the pnn by loading the nodes file and try to bind to all
566 addresses one at a time until the ip address is found.
568 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
570 TALLOC_CTX *mem_ctx = talloc_new(NULL);
571 struct pnn_node *pnn_nodes;
572 struct pnn_node *pnn_node;
574 pnn_nodes = read_nodes_file(mem_ctx);
575 if (pnn_nodes == NULL) {
576 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
577 talloc_free(mem_ctx);
581 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
584 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
585 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
586 talloc_free(mem_ctx);
590 if (ctdb_sys_have_ip(&addr)) {
591 printf("PNN:%d\n", pnn_node->pnn);
592 talloc_free(mem_ctx);
597 printf("Failed to detect which PNN this node is\n");
598 talloc_free(mem_ctx);
603 display remote ctdb status
605 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
608 struct ctdb_vnn_map *vnnmap=NULL;
609 struct ctdb_node_map *nodemap=NULL;
610 uint32_t recmode, recmaster;
613 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
618 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
620 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
624 if(options.machinereadable){
625 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped:Inactive:\n");
626 for(i=0;i<nodemap->num;i++){
627 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
630 printf(":%d:%s:%d:%d:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
631 ctdb_addr_to_str(&nodemap->nodes[i].addr),
632 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
633 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
634 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
635 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
636 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED),
637 !!(nodemap->nodes[i].flags&NODE_FLAGS_INACTIVE));
642 printf("Number of nodes:%d\n", nodemap->num);
643 for(i=0;i<nodemap->num;i++){
644 static const struct {
648 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
649 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
650 { NODE_FLAGS_BANNED, "BANNED" },
651 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
652 { NODE_FLAGS_DELETED, "DELETED" },
653 { NODE_FLAGS_STOPPED, "STOPPED" },
654 { NODE_FLAGS_INACTIVE, "INACTIVE" },
656 char *flags_str = NULL;
659 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
662 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
663 if (nodemap->nodes[i].flags & flag_names[j].flag) {
664 if (flags_str == NULL) {
665 flags_str = talloc_strdup(ctdb, flag_names[j].name);
667 flags_str = talloc_asprintf_append(flags_str, "|%s",
670 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
673 if (flags_str == NULL) {
674 flags_str = talloc_strdup(ctdb, "OK");
675 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
677 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
678 ctdb_addr_to_str(&nodemap->nodes[i].addr),
680 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
681 talloc_free(flags_str);
684 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
686 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
689 if (vnnmap->generation == INVALID_GENERATION) {
690 printf("Generation:INVALID\n");
692 printf("Generation:%d\n",vnnmap->generation);
694 printf("Size:%d\n",vnnmap->size);
695 for(i=0;i<vnnmap->size;i++){
696 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
699 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
701 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
704 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
706 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
708 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
711 printf("Recovery master:%d\n",recmaster);
718 struct natgw_node *next;
723 display the list of nodes belonging to this natgw configuration
725 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
728 uint32_t capabilities;
729 const char *natgw_list;
732 struct natgw_node *natgw_nodes = NULL;
733 struct natgw_node *natgw_node;
734 struct ctdb_node_map *nodemap=NULL;
737 /* read the natgw nodes file into a linked list */
738 natgw_list = getenv("NATGW_NODES");
739 if (natgw_list == NULL) {
740 natgw_list = "/etc/ctdb/natgw_nodes";
742 lines = file_lines_load(natgw_list, &nlines, ctdb);
744 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
747 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
750 for (i=0;i<nlines;i++) {
754 /* strip leading spaces */
755 while((*node == ' ') || (*node == '\t')) {
761 if (strcmp(node, "") == 0) {
764 natgw_node = talloc(ctdb, struct natgw_node);
765 natgw_node->addr = talloc_strdup(natgw_node, node);
766 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
767 natgw_node->next = natgw_nodes;
768 natgw_nodes = natgw_node;
771 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
773 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
778 while(i<nodemap->num) {
779 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
780 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
785 /* this node was not in the natgw so we just remove it from
788 if ((natgw_node == NULL)
789 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
792 for (j=i+1; j<nodemap->num; j++) {
793 nodemap->nodes[j-1] = nodemap->nodes[j];
802 /* pick a node to be natgwmaster
803 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
805 for(i=0;i<nodemap->num;i++){
806 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
807 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
809 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
812 if (!(capabilities&CTDB_CAP_NATGW)) {
815 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
819 /* we couldnt find any healthy node, try unhealthy ones */
820 if (i == nodemap->num) {
821 for(i=0;i<nodemap->num;i++){
822 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
823 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
825 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
828 if (!(capabilities&CTDB_CAP_NATGW)) {
831 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
836 /* unless all nodes are STOPPED, when we pick one anyway */
837 if (i == nodemap->num) {
838 for(i=0;i<nodemap->num;i++){
839 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
840 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
842 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
845 if (!(capabilities&CTDB_CAP_NATGW)) {
848 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
852 /* or if we still can not find any */
853 if (i == nodemap->num) {
854 printf("-1 0.0.0.0\n");
858 /* print the pruned list of nodes belonging to this natgw list */
859 for(i=0;i<nodemap->num;i++){
860 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
863 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
864 ctdb_addr_to_str(&nodemap->nodes[i].addr),
865 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
866 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
867 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
868 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
869 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
876 display the status of the scripts for monitoring (or other events)
878 static int control_one_scriptstatus(struct ctdb_context *ctdb,
879 enum ctdb_eventscript_call type)
881 struct ctdb_scripts_wire *script_status;
884 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
886 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
890 if (script_status == NULL) {
891 if (!options.machinereadable) {
892 printf("%s cycle never run\n",
893 ctdb_eventscript_call_names[type]);
898 if (!options.machinereadable) {
899 printf("%d scripts were executed last %s cycle\n",
900 script_status->num_scripts,
901 ctdb_eventscript_call_names[type]);
903 for (i=0; i<script_status->num_scripts; i++) {
904 const char *status = NULL;
906 switch (script_status->scripts[i].status) {
917 if (script_status->scripts[i].status > 0)
921 if (options.machinereadable) {
922 printf("%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
923 ctdb_eventscript_call_names[type],
924 script_status->scripts[i].name,
925 script_status->scripts[i].status,
927 (long)script_status->scripts[i].start.tv_sec,
928 (long)script_status->scripts[i].start.tv_usec,
929 (long)script_status->scripts[i].finished.tv_sec,
930 (long)script_status->scripts[i].finished.tv_usec,
931 script_status->scripts[i].output);
935 printf("%-20s Status:%s ",
936 script_status->scripts[i].name, status);
938 /* Some other error, eg from stat. */
939 printf("%-20s Status:CANNOT RUN (%s)",
940 script_status->scripts[i].name,
941 strerror(-script_status->scripts[i].status));
943 if (script_status->scripts[i].status >= 0) {
944 printf("Duration:%.3lf ",
945 timeval_delta(&script_status->scripts[i].finished,
946 &script_status->scripts[i].start));
948 if (script_status->scripts[i].status != -ENOEXEC) {
950 ctime(&script_status->scripts[i].start.tv_sec));
951 if (script_status->scripts[i].status != 0) {
952 printf(" OUTPUT:%s\n",
953 script_status->scripts[i].output);
963 static int control_scriptstatus(struct ctdb_context *ctdb,
964 int argc, const char **argv)
967 enum ctdb_eventscript_call type, min, max;
971 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
976 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
980 for (type = 0; type < CTDB_EVENT_MAX; type++) {
981 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
987 if (type == CTDB_EVENT_MAX) {
988 if (strcmp(arg, "all") == 0) {
990 max = CTDB_EVENT_MAX;
992 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
997 if (options.machinereadable) {
998 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1001 for (type = min; type < max; type++) {
1002 ret = control_one_scriptstatus(ctdb, type);
1012 enable an eventscript
1014 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1022 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1024 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1032 disable an eventscript
1034 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1042 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1044 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1052 display the pnn of the recovery master
1054 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1059 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1061 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1064 printf("%d\n",recmaster);
1070 add a tickle to a public address
1072 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1074 struct ctdb_tcp_connection t;
1082 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1083 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1086 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1087 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1091 data.dptr = (uint8_t *)&t;
1092 data.dsize = sizeof(t);
1094 /* tell all nodes about this tcp connection */
1095 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1096 0, data, ctdb, NULL, NULL, NULL, NULL);
1098 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1107 delete a tickle from a node
1109 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1111 struct ctdb_tcp_connection t;
1119 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1120 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1123 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1124 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1128 data.dptr = (uint8_t *)&t;
1129 data.dsize = sizeof(t);
1131 /* tell all nodes about this tcp connection */
1132 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1133 0, data, ctdb, NULL, NULL, NULL, NULL);
1135 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1144 get a list of all tickles for this pnn
1146 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1148 struct ctdb_control_tcp_tickle_list *list;
1149 ctdb_sock_addr addr;
1158 port = atoi(argv[1]);
1161 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1162 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1166 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1168 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1172 if (options.machinereadable){
1173 printf(":source ip:port:destination ip:port:\n");
1174 for (i=0;i<list->tickles.num;i++) {
1175 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1178 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1179 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1182 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1183 printf("Num tickles:%u\n", list->tickles.num);
1184 for (i=0;i<list->tickles.num;i++) {
1185 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1188 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1189 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));
1199 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1201 struct ctdb_all_public_ips *ips;
1202 struct ctdb_public_ip ip;
1205 uint32_t disable_time;
1207 struct ctdb_node_map *nodemap=NULL;
1208 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1211 data.dptr = (uint8_t*)&disable_time;
1212 data.dsize = sizeof(disable_time);
1213 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1215 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1221 /* read the public ip list from the node */
1222 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1224 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1225 talloc_free(tmp_ctx);
1229 for (i=0;i<ips->num;i++) {
1230 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1235 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1236 pnn, ctdb_addr_to_str(addr)));
1237 talloc_free(tmp_ctx);
1244 data.dptr = (uint8_t *)&ip;
1245 data.dsize = sizeof(ip);
1247 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1249 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1250 talloc_free(tmp_ctx);
1254 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1255 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1262 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1263 talloc_free(tmp_ctx);
1267 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1269 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1270 talloc_free(tmp_ctx);
1274 /* update the recovery daemon so it now knows to expect the new
1275 node assignment for this ip.
1277 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1279 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1283 talloc_free(tmp_ctx);
1288 move/failover an ip address to a specific node
1290 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1293 int ret, retries = 0;
1294 ctdb_sock_addr addr;
1301 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1302 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1307 if (sscanf(argv[1], "%u", &pnn) != 1) {
1308 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1313 ret = move_ip(ctdb, &addr, pnn);
1315 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1319 } while (retries < 5 && ret != 0);
1321 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1328 void getips_store_callback(void *param, void *data)
1330 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1331 struct ctdb_all_public_ips *ips = param;
1335 ips->ips[i].pnn = node_ip->pnn;
1336 ips->ips[i].addr = node_ip->addr;
1339 void getips_count_callback(void *param, void *data)
1341 uint32_t *count = param;
1347 static uint32_t *ip_key(ctdb_sock_addr *ip)
1349 static uint32_t key[IP_KEYLEN];
1351 bzero(key, sizeof(key));
1353 switch (ip->sa.sa_family) {
1355 key[0] = ip->ip.sin_addr.s_addr;
1358 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
1359 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
1360 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
1361 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
1364 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1371 static void *add_ip_callback(void *parm, void *data)
1377 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1379 struct ctdb_all_public_ips *tmp_ips;
1380 struct ctdb_node_map *nodemap=NULL;
1381 trbt_tree_t *ip_tree;
1385 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1387 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1391 ip_tree = trbt_create(tmp_ctx, 0);
1393 for(i=0;i<nodemap->num;i++){
1394 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1397 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1401 /* read the public ip list from this node */
1402 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1404 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1408 for (j=0; j<tmp_ips->num;j++) {
1409 struct ctdb_public_ip *node_ip;
1411 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1412 node_ip->pnn = tmp_ips->ips[j].pnn;
1413 node_ip->addr = tmp_ips->ips[j].addr;
1415 trbt_insertarray32_callback(ip_tree,
1416 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1420 talloc_free(tmp_ips);
1425 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1427 len = offsetof(struct ctdb_all_public_ips, ips) +
1428 count*sizeof(struct ctdb_public_ip);
1429 tmp_ips = talloc_zero_size(tmp_ctx, len);
1430 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1439 * scans all other nodes and returns a pnn for another node that can host this
1443 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1445 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1446 struct ctdb_all_public_ips *ips;
1447 struct ctdb_node_map *nodemap=NULL;
1450 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1452 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1453 talloc_free(tmp_ctx);
1457 for(i=0;i<nodemap->num;i++){
1458 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1461 if (nodemap->nodes[i].pnn == options.pnn) {
1465 /* read the public ip list from this node */
1466 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1468 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1472 for (j=0;j<ips->num;j++) {
1473 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1474 talloc_free(tmp_ctx);
1475 return nodemap->nodes[i].pnn;
1481 talloc_free(tmp_ctx);
1485 static uint32_t ipreallocate_finished;
1488 handler for receiving the response to ipreallocate
1490 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1491 TDB_DATA data, void *private_data)
1493 ipreallocate_finished = 1;
1496 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1498 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1500 event_add_timed(ctdb->ev, ctdb,
1501 timeval_current_ofs(1, 0),
1502 ctdb_every_second, ctdb);
1506 ask the recovery daemon on the recovery master to perform a ip reallocation
1508 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1512 struct takeover_run_reply rd;
1514 struct ctdb_node_map *nodemap=NULL;
1516 struct timeval tv = timeval_current();
1518 /* we need some events to trigger so we can timeout and restart
1521 event_add_timed(ctdb->ev, ctdb,
1522 timeval_current_ofs(1, 0),
1523 ctdb_every_second, ctdb);
1525 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1527 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1530 rd.srvid = getpid();
1532 /* register a message port for receiveing the reply so that we
1533 can receive the reply
1535 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1537 data.dptr = (uint8_t *)&rd;
1538 data.dsize = sizeof(rd);
1541 /* check that there are valid nodes available */
1542 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1543 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1546 for (i=0; i<nodemap->num;i++) {
1547 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1551 if (i==nodemap->num) {
1552 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1557 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1559 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1563 /* verify the node exists */
1564 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1565 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1570 /* check tha there are nodes available that can act as a recmaster */
1571 for (i=0; i<nodemap->num; i++) {
1572 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1577 if (i == nodemap->num) {
1578 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1582 /* verify the recovery master is not STOPPED, nor BANNED */
1583 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1584 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1590 /* verify the recovery master is not STOPPED, nor BANNED */
1591 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1592 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1598 ipreallocate_finished = 0;
1599 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1601 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1605 tv = timeval_current();
1606 /* this loop will terminate when we have received the reply */
1607 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1608 event_loop_once(ctdb->ev);
1610 if (ipreallocate_finished == 1) {
1623 add a public ip address to a node
1625 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1628 int len, retries = 0;
1631 ctdb_sock_addr addr;
1632 struct ctdb_control_ip_iface *pub;
1633 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1634 struct ctdb_all_public_ips *ips;
1638 talloc_free(tmp_ctx);
1642 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1643 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1644 talloc_free(tmp_ctx);
1648 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1650 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1651 talloc_free(tmp_ctx);
1656 /* check if some other node is already serving this ip, if not,
1659 for (i=0;i<ips->num;i++) {
1660 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1665 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1666 pub = talloc_size(tmp_ctx, len);
1667 CTDB_NO_MEMORY(ctdb, pub);
1671 pub->len = strlen(argv[1])+1;
1672 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1675 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1677 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1681 } while (retries < 5 && ret != 0);
1683 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1684 talloc_free(tmp_ctx);
1688 if (i == ips->num) {
1689 /* no one has this ip so we claim it */
1692 pnn = ips->ips[i].pnn;
1696 ret = move_ip(ctdb, &addr, pnn);
1698 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. wait 3 seconds and try again.\n", pnn));
1702 } while (retries < 5 && ret != 0);
1704 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1705 talloc_free(tmp_ctx);
1710 ret = control_ipreallocate(ctdb, argc, argv);
1712 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Wait 3 seconds and try again.\n", options.pnn));
1716 } while (retries < 5 && ret != 0);
1718 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Giving up.\n", options.pnn));
1719 talloc_free(tmp_ctx);
1723 talloc_free(tmp_ctx);
1727 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1729 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1731 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1732 struct ctdb_node_map *nodemap=NULL;
1733 struct ctdb_all_public_ips *ips;
1736 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1738 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1742 /* remove it from the nodes that are not hosting the ip currently */
1743 for(i=0;i<nodemap->num;i++){
1744 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1747 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1748 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1752 for (j=0;j<ips->num;j++) {
1753 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1761 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1765 options.pnn = nodemap->nodes[i].pnn;
1766 control_delip(ctdb, argc, argv);
1770 /* remove it from every node (also the one hosting it) */
1771 for(i=0;i<nodemap->num;i++){
1772 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1775 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1776 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1780 for (j=0;j<ips->num;j++) {
1781 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1789 options.pnn = nodemap->nodes[i].pnn;
1790 control_delip(ctdb, argc, argv);
1793 talloc_free(tmp_ctx);
1798 delete a public ip address from a node
1800 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1804 ctdb_sock_addr addr;
1805 struct ctdb_control_ip_iface pub;
1806 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1807 struct ctdb_all_public_ips *ips;
1810 talloc_free(tmp_ctx);
1814 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1815 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1819 if (options.pnn == CTDB_BROADCAST_ALL) {
1820 return control_delip_all(ctdb, argc, argv, &addr);
1827 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1829 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1830 talloc_free(tmp_ctx);
1834 for (i=0;i<ips->num;i++) {
1835 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1841 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1842 ctdb_addr_to_str(&addr)));
1843 talloc_free(tmp_ctx);
1847 if (ips->ips[i].pnn == options.pnn) {
1848 ret = find_other_host_for_public_ip(ctdb, &addr);
1851 ret = move_ip(ctdb, &addr, ret);
1853 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
1857 } while (retries < 5 && ret != 0);
1859 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
1865 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1867 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1868 talloc_free(tmp_ctx);
1872 talloc_free(tmp_ctx);
1877 kill a tcp connection
1879 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1882 struct ctdb_control_killtcp killtcp;
1888 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1889 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1893 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1894 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1898 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1900 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1911 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1914 ctdb_sock_addr addr;
1920 if (!parse_ip(argv[0], NULL, 0, &addr)) {
1921 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1925 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1927 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1935 register a server id
1937 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1940 struct ctdb_server_id server_id;
1946 server_id.pnn = strtoul(argv[0], NULL, 0);
1947 server_id.type = strtoul(argv[1], NULL, 0);
1948 server_id.server_id = strtoul(argv[2], NULL, 0);
1950 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1952 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1955 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
1961 unregister a server id
1963 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1966 struct ctdb_server_id server_id;
1972 server_id.pnn = strtoul(argv[0], NULL, 0);
1973 server_id.type = strtoul(argv[1], NULL, 0);
1974 server_id.server_id = strtoul(argv[2], NULL, 0);
1976 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1978 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1985 check if a server id exists
1987 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1991 struct ctdb_server_id server_id;
1997 server_id.pnn = strtoul(argv[0], NULL, 0);
1998 server_id.type = strtoul(argv[1], NULL, 0);
1999 server_id.server_id = strtoul(argv[2], NULL, 0);
2001 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2003 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2008 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2010 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2016 get a list of all server ids that are registered on a node
2018 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2021 struct ctdb_server_id_list *server_ids;
2023 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2025 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2029 for (i=0; i<server_ids->num; i++) {
2030 printf("Server id %d:%d:%d\n",
2031 server_ids->server_ids[i].pnn,
2032 server_ids->server_ids[i].type,
2033 server_ids->server_ids[i].server_id);
2040 send a tcp tickle ack
2042 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2045 ctdb_sock_addr src, dst;
2051 if (!parse_ip_port(argv[0], &src)) {
2052 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2056 if (!parse_ip_port(argv[1], &dst)) {
2057 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2061 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2065 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2072 display public ip status
2074 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2077 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2078 struct ctdb_all_public_ips *ips;
2080 if (options.pnn == CTDB_BROADCAST_ALL) {
2081 /* read the list of public ips from all nodes */
2082 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2084 /* read the public ip list from this node */
2085 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2088 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2089 talloc_free(tmp_ctx);
2093 if (options.machinereadable){
2094 printf(":Public IP:Node:");
2095 if (options.verbose){
2096 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2100 if (options.pnn == CTDB_BROADCAST_ALL) {
2101 printf("Public IPs on ALL nodes\n");
2103 printf("Public IPs on node %u\n", options.pnn);
2107 for (i=1;i<=ips->num;i++) {
2108 struct ctdb_control_public_ip_info *info = NULL;
2110 char *aciface = NULL;
2111 char *avifaces = NULL;
2112 char *cifaces = NULL;
2114 if (options.pnn == CTDB_BROADCAST_ALL) {
2115 pnn = ips->ips[ips->num-i].pnn;
2121 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2122 &ips->ips[ips->num-i].addr, &info);
2129 for (j=0; j < info->num; j++) {
2130 if (cifaces == NULL) {
2131 cifaces = talloc_strdup(info,
2132 info->ifaces[j].name);
2134 cifaces = talloc_asprintf_append(cifaces,
2136 info->ifaces[j].name);
2139 if (info->active_idx == j) {
2140 aciface = info->ifaces[j].name;
2143 if (info->ifaces[j].link_state == 0) {
2147 if (avifaces == NULL) {
2148 avifaces = talloc_strdup(info, info->ifaces[j].name);
2150 avifaces = talloc_asprintf_append(avifaces,
2152 info->ifaces[j].name);
2157 if (options.machinereadable){
2159 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2160 ips->ips[ips->num-i].pnn);
2161 if (options.verbose){
2164 avifaces?avifaces:"",
2165 cifaces?cifaces:"");
2169 if (options.verbose) {
2170 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2171 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2172 ips->ips[ips->num-i].pnn,
2174 avifaces?avifaces:"",
2175 cifaces?cifaces:"");
2178 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2179 ips->ips[ips->num-i].pnn);
2185 talloc_free(tmp_ctx);
2192 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2195 ctdb_sock_addr addr;
2196 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2197 struct ctdb_control_public_ip_info *info;
2200 talloc_free(tmp_ctx);
2204 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2205 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2209 /* read the public ip info from this node */
2210 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2211 tmp_ctx, &addr, &info);
2213 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2214 argv[0], options.pnn));
2215 talloc_free(tmp_ctx);
2219 printf("Public IP[%s] info on node %u\n",
2220 ctdb_addr_to_str(&info->ip.addr),
2223 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2224 ctdb_addr_to_str(&info->ip.addr),
2225 info->ip.pnn, info->num);
2227 for (i=0; i<info->num; i++) {
2228 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2230 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2231 i+1, info->ifaces[i].name,
2232 info->ifaces[i].link_state?"up":"down",
2233 (unsigned int)info->ifaces[i].references,
2234 (i==info->active_idx)?" (active)":"");
2237 talloc_free(tmp_ctx);
2242 display interfaces status
2244 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2247 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2248 struct ctdb_control_get_ifaces *ifaces;
2250 /* read the public ip list from this node */
2251 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn,
2254 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2256 talloc_free(tmp_ctx);
2260 if (options.machinereadable){
2261 printf(":Name:LinkStatus:References:\n");
2263 printf("Interfaces on node %u\n", options.pnn);
2266 for (i=0; i<ifaces->num; i++) {
2267 if (options.machinereadable){
2268 printf(":%s:%s:%u\n",
2269 ifaces->ifaces[i].name,
2270 ifaces->ifaces[i].link_state?"1":"0",
2271 (unsigned int)ifaces->ifaces[i].references);
2273 printf("name:%s link:%s references:%u\n",
2274 ifaces->ifaces[i].name,
2275 ifaces->ifaces[i].link_state?"up":"down",
2276 (unsigned int)ifaces->ifaces[i].references);
2280 talloc_free(tmp_ctx);
2286 set link status of an interface
2288 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2291 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2292 struct ctdb_control_iface_info info;
2300 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2301 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2303 talloc_free(tmp_ctx);
2306 strcpy(info.name, argv[0]);
2308 if (strcmp(argv[1], "up") == 0) {
2309 info.link_state = 1;
2310 } else if (strcmp(argv[1], "down") == 0) {
2311 info.link_state = 0;
2313 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2315 talloc_free(tmp_ctx);
2319 /* read the public ip list from this node */
2320 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2323 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2324 argv[0], options.pnn));
2325 talloc_free(tmp_ctx);
2329 talloc_free(tmp_ctx);
2334 display pid of a ctdb daemon
2336 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2341 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2343 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2346 printf("Pid:%d\n", pid);
2352 disable a remote node
2354 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2357 struct ctdb_node_map *nodemap=NULL;
2359 /* check if the node is already disabled */
2360 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2361 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2364 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2365 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2370 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2372 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2378 /* read the nodemap and verify the change took effect */
2379 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2380 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2384 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2385 ret = control_ipreallocate(ctdb, argc, argv);
2387 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2395 enable a disabled remote node
2397 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2401 struct ctdb_node_map *nodemap=NULL;
2404 /* check if the node is already enabled */
2405 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2406 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2409 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2410 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2415 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2417 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2423 /* read the nodemap and verify the change took effect */
2424 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2425 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2429 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2431 ret = control_ipreallocate(ctdb, argc, argv);
2433 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2443 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2446 struct ctdb_node_map *nodemap=NULL;
2449 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2451 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2456 /* read the nodemap and verify the change took effect */
2457 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2458 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2462 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2463 ret = control_ipreallocate(ctdb, argc, argv);
2465 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2473 restart a stopped remote node
2475 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2479 struct ctdb_node_map *nodemap=NULL;
2482 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2484 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2490 /* read the nodemap and verify the change took effect */
2491 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2492 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2496 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2497 ret = control_ipreallocate(ctdb, argc, argv);
2499 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2506 static uint32_t get_generation(struct ctdb_context *ctdb)
2508 struct ctdb_vnn_map *vnnmap=NULL;
2511 /* wait until the recmaster is not in recovery mode */
2513 uint32_t recmode, recmaster;
2515 if (vnnmap != NULL) {
2516 talloc_free(vnnmap);
2520 /* get the recmaster */
2521 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2523 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2527 /* get recovery mode */
2528 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
2530 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2534 /* get the current generation number */
2535 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2537 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2541 if ((recmode == CTDB_RECOVERY_NORMAL)
2542 && (vnnmap->generation != 1)){
2543 return vnnmap->generation;
2550 ban a node from the cluster
2552 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2555 struct ctdb_node_map *nodemap=NULL;
2556 struct ctdb_ban_time bantime;
2562 /* verify the node exists */
2563 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2565 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2569 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2570 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2574 bantime.pnn = options.pnn;
2575 bantime.time = strtoul(argv[0], NULL, 0);
2577 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2579 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2583 ret = control_ipreallocate(ctdb, argc, argv);
2585 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2594 unban a node from the cluster
2596 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2599 struct ctdb_node_map *nodemap=NULL;
2600 struct ctdb_ban_time bantime;
2602 /* verify the node exists */
2603 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2605 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2609 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2610 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2614 bantime.pnn = options.pnn;
2617 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2619 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2623 ret = control_ipreallocate(ctdb, argc, argv);
2625 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2634 show ban information for a node
2636 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2639 struct ctdb_node_map *nodemap=NULL;
2640 struct ctdb_ban_time *bantime;
2642 /* verify the node exists */
2643 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2645 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2649 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2651 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2655 if (bantime->time == 0) {
2656 printf("Node %u is not banned\n", bantime->pnn);
2658 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2667 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2671 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2673 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2683 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2686 uint32_t generation, next_generation;
2688 /* record the current generation number */
2689 generation = get_generation(ctdb);
2691 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2693 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2697 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2699 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2703 /* wait until we are in a new generation */
2705 next_generation = get_generation(ctdb);
2706 if (next_generation != generation) {
2717 display monitoring mode of a remote node
2719 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2724 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2726 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2729 if (!options.machinereadable){
2730 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2733 printf(":%d:\n",monmode);
2740 display capabilities of a remote node
2742 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2744 uint32_t capabilities;
2747 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2749 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2753 if (!options.machinereadable){
2754 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2755 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2756 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2757 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2759 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2760 printf(":%d:%d:%d:%d:\n",
2761 !!(capabilities&CTDB_CAP_RECMASTER),
2762 !!(capabilities&CTDB_CAP_LMASTER),
2763 !!(capabilities&CTDB_CAP_LVS),
2764 !!(capabilities&CTDB_CAP_NATGW));
2770 display lvs configuration
2772 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2774 uint32_t *capabilities;
2775 struct ctdb_node_map *nodemap=NULL;
2777 int healthy_count = 0;
2779 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2781 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2785 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2786 CTDB_NO_MEMORY(ctdb, capabilities);
2788 /* collect capabilities for all connected nodes */
2789 for (i=0; i<nodemap->num; i++) {
2790 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2793 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2797 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2799 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2803 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2807 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2812 /* Print all LVS nodes */
2813 for (i=0; i<nodemap->num; i++) {
2814 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2817 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2820 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2824 if (healthy_count != 0) {
2825 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2830 printf("%d:%s\n", i,
2831 ctdb_addr_to_str(&nodemap->nodes[i].addr));
2838 display who is the lvs master
2840 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2842 uint32_t *capabilities;
2843 struct ctdb_node_map *nodemap=NULL;
2845 int healthy_count = 0;
2847 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2849 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2853 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2854 CTDB_NO_MEMORY(ctdb, capabilities);
2856 /* collect capabilities for all connected nodes */
2857 for (i=0; i<nodemap->num; i++) {
2858 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2861 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2865 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2867 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2871 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2875 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2880 /* find and show the lvsmaster */
2881 for (i=0; i<nodemap->num; i++) {
2882 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2885 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2888 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2892 if (healthy_count != 0) {
2893 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2898 if (options.machinereadable){
2901 printf("Node %d is LVS master\n", i);
2906 printf("There is no LVS master\n");
2911 disable monitoring on a node
2913 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2918 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2920 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2923 printf("Monitoring mode:%s\n","DISABLED");
2929 enable monitoring on a node
2931 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2936 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
2938 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
2941 printf("Monitoring mode:%s\n","ACTIVE");
2947 display remote list of keys/data for a db
2949 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
2951 const char *db_name;
2952 struct ctdb_db_context *ctdb_db;
2962 if (db_exists(ctdb, db_name)) {
2963 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2967 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2969 if (ctdb_db == NULL) {
2970 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2974 /* traverse and dump the cluster tdb */
2975 ret = ctdb_dump_db(ctdb_db, stdout);
2977 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
2978 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
2979 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
2983 talloc_free(ctdb_db);
2985 printf("Dumped %d records\n", ret);
2990 display the content of a database key
2992 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
2994 const char *db_name;
2995 struct ctdb_db_context *ctdb_db;
2996 struct ctdb_record_handle *h;
2997 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3007 if (db_exists(ctdb, db_name)) {
3008 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3012 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3014 if (ctdb_db == NULL) {
3015 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3019 key.dptr = discard_const(argv[1]);
3020 key.dsize = strlen((char *)key.dptr);
3022 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3024 printf("Failed to fetch record '%s' on node %d\n",
3025 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3026 talloc_free(tmp_ctx);
3030 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3032 talloc_free(ctdb_db);
3033 talloc_free(tmp_ctx);
3038 display the content of a database key
3040 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3042 const char *db_name;
3043 struct ctdb_db_context *ctdb_db;
3044 struct ctdb_record_handle *h;
3045 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3055 if (db_exists(ctdb, db_name)) {
3056 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3060 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3062 if (ctdb_db == NULL) {
3063 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3067 key.dptr = discard_const(argv[1]);
3068 key.dsize = strlen((char *)key.dptr);
3070 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3072 printf("Failed to fetch record '%s' on node %d\n",
3073 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3074 talloc_free(tmp_ctx);
3078 data.dptr = discard_const(argv[2]);
3079 data.dsize = strlen((char *)data.dptr);
3081 if (ctdb_record_store(h, data) != 0) {
3082 printf("Failed to store record\n");
3086 talloc_free(ctdb_db);
3087 talloc_free(tmp_ctx);
3092 fetch a record from a persistent database
3094 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3096 const char *db_name;
3097 struct ctdb_db_context *ctdb_db;
3098 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3099 struct ctdb_transaction_handle *h;
3104 talloc_free(tmp_ctx);
3111 if (db_exists(ctdb, db_name)) {
3112 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3113 talloc_free(tmp_ctx);
3117 ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3119 if (ctdb_db == NULL) {
3120 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3121 talloc_free(tmp_ctx);
3125 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3127 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3128 talloc_free(tmp_ctx);
3132 key.dptr = discard_const(argv[1]);
3133 key.dsize = strlen(argv[1]);
3134 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3136 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3137 talloc_free(tmp_ctx);
3141 if (data.dsize == 0 || data.dptr == NULL) {
3142 DEBUG(DEBUG_ERR,("Record is empty\n"));
3143 talloc_free(tmp_ctx);
3148 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3150 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3151 talloc_free(tmp_ctx);
3154 write(fd, data.dptr, data.dsize);
3157 write(1, data.dptr, data.dsize);
3160 /* abort the transaction */
3164 talloc_free(tmp_ctx);
3169 fetch a record from a tdb-file
3171 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3173 const char *tdb_file;
3184 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3186 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
3190 key.dptr = discard_const(argv[1]);
3191 key.dsize = strlen(argv[1]);
3192 data = tdb_fetch(tdb, key);
3193 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3194 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
3202 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3204 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3207 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3210 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3217 write a record to a persistent database
3219 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3221 const char *db_name;
3222 struct ctdb_db_context *ctdb_db;
3223 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3224 struct ctdb_transaction_handle *h;
3230 talloc_free(tmp_ctx);
3234 fd = open(argv[2], O_RDONLY);
3236 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3237 talloc_free(tmp_ctx);
3241 ret = fstat(fd, &st);
3243 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3245 talloc_free(tmp_ctx);
3249 if (!S_ISREG(st.st_mode)) {
3250 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3252 talloc_free(tmp_ctx);
3256 data.dsize = st.st_size;
3257 if (data.dsize == 0) {
3260 data.dptr = talloc_size(tmp_ctx, data.dsize);
3261 if (data.dptr == NULL) {
3262 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3264 talloc_free(tmp_ctx);
3267 ret = read(fd, data.dptr, data.dsize);
3268 if (ret != data.dsize) {
3269 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3271 talloc_free(tmp_ctx);
3280 ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3282 if (ctdb_db == NULL) {
3283 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3284 talloc_free(tmp_ctx);
3288 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3290 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3291 talloc_free(tmp_ctx);
3295 key.dptr = discard_const(argv[1]);
3296 key.dsize = strlen(argv[1]);
3297 ret = ctdb_transaction_store(h, key, data);
3299 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3300 talloc_free(tmp_ctx);
3304 ret = ctdb_transaction_commit(h);
3306 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3307 talloc_free(tmp_ctx);
3312 talloc_free(tmp_ctx);
3316 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3317 TDB_DATA data, void *private_data)
3319 DEBUG(DEBUG_ERR,("Log data received\n"));
3320 if (data.dsize > 0) {
3321 printf("%s", data.dptr);
3328 display a list of log messages from the in memory ringbuffer
3330 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3334 struct ctdb_get_log_addr log_addr;
3336 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3341 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3342 talloc_free(tmp_ctx);
3346 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3347 log_addr.srvid = getpid();
3348 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3349 log_addr.level = get_debug_by_desc(argv[0]);
3351 log_addr.level = strtol(argv[0], NULL, 0);
3355 data.dptr = (unsigned char *)&log_addr;
3356 data.dsize = sizeof(log_addr);
3358 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3360 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3363 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3365 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3366 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3367 if (ret != 0 || res != 0) {
3368 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3369 talloc_free(tmp_ctx);
3374 tv = timeval_current();
3375 /* this loop will terminate when we have received the reply */
3376 while (timeval_elapsed(&tv) < 3.0) {
3377 event_loop_once(ctdb->ev);
3380 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3382 talloc_free(tmp_ctx);
3387 clear the in memory log area
3389 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3394 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3396 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3397 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3398 if (ret != 0 || res != 0) {
3399 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3400 talloc_free(tmp_ctx);
3404 talloc_free(tmp_ctx);
3411 display a list of the databases on a remote ctdb
3413 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3416 struct ctdb_dbid_map *dbmap=NULL;
3418 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3420 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3424 if(options.machinereadable){
3425 printf(":ID:Name:Path:Persistent:Unhealthy:\n");
3426 for(i=0;i<dbmap->num;i++){
3432 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3433 dbmap->dbs[i].dbid, ctdb, &path);
3434 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3435 dbmap->dbs[i].dbid, ctdb, &name);
3436 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3437 dbmap->dbs[i].dbid, ctdb, &health);
3438 persistent = dbmap->dbs[i].persistent;
3439 printf(":0x%08X:%s:%s:%d:%d:\n",
3440 dbmap->dbs[i].dbid, name, path,
3441 !!(persistent), !!(health));
3446 printf("Number of databases:%d\n", dbmap->num);
3447 for(i=0;i<dbmap->num;i++){
3453 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3454 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3455 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3456 persistent = dbmap->dbs[i].persistent;
3457 printf("dbid:0x%08x name:%s path:%s%s%s\n",
3458 dbmap->dbs[i].dbid, name, path,
3459 persistent?" PERSISTENT":"",
3460 health?" UNHEALTHY":"");
3467 display the status of a database on a remote ctdb
3469 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3472 struct ctdb_dbid_map *dbmap=NULL;
3473 const char *db_name;
3481 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3483 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3487 for(i=0;i<dbmap->num;i++){
3493 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3494 if (strcmp(name, db_name) != 0) {
3498 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3499 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3500 persistent = dbmap->dbs[i].persistent;
3501 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
3502 dbmap->dbs[i].dbid, name, path,
3503 persistent?"yes":"no",
3504 health?health:"OK");
3508 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3513 check if the local node is recmaster or not
3514 it will return 1 if this node is the recmaster and 0 if it is not
3515 or if the local ctdb daemon could not be contacted
3517 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3519 uint32_t mypnn, recmaster;
3522 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3524 printf("Failed to get pnn of node\n");
3528 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
3530 printf("Failed to get the recmaster\n");
3534 if (recmaster != mypnn) {
3535 printf("this node is not the recmaster\n");
3539 printf("this node is the recmaster\n");
3546 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3549 struct timeval tv = timeval_current();
3550 ret = ctdb_ctrl_ping(ctdb, options.pnn);
3552 printf("Unable to get ping response from node %u\n", options.pnn);
3555 printf("response from %u time=%.6f sec (%d clients)\n",
3556 options.pnn, timeval_elapsed(&tv), ret);
3565 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3576 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3578 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3582 printf("%-19s = %u\n", name, value);
3589 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3600 value = strtoul(argv[1], NULL, 0);
3602 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3604 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3613 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3619 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3621 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3625 for (i=0;i<count;i++) {
3626 control_getvar(ctdb, 1, &list[i]);
3635 display debug level on a node
3637 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3642 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3644 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3647 if (options.machinereadable){
3648 printf(":Name:Level:\n");
3649 printf(":%s:%d:\n",get_debug_by_level(level),level);
3651 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3658 display reclock file of a node
3660 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3663 const char *reclock;
3665 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3667 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3670 if (options.machinereadable){
3671 if (reclock != NULL) {
3672 printf("%s", reclock);
3675 if (reclock == NULL) {
3676 printf("No reclock file used.\n");
3678 printf("Reclock file:%s\n", reclock);
3686 set the reclock file of a node
3688 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3691 const char *reclock;
3695 } else if (argc == 1) {
3701 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3703 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3710 set the natgw state on/off
3712 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3715 uint32_t natgwstate;
3721 if (!strcmp(argv[0], "on")) {
3723 } else if (!strcmp(argv[0], "off")) {
3729 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3731 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3739 set the lmaster role on/off
3741 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3744 uint32_t lmasterrole;
3750 if (!strcmp(argv[0], "on")) {
3752 } else if (!strcmp(argv[0], "off")) {
3758 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3760 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3768 set the recmaster role on/off
3770 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3773 uint32_t recmasterrole;
3779 if (!strcmp(argv[0], "on")) {
3781 } else if (!strcmp(argv[0], "off")) {
3787 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3789 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3797 set debug level on a node or all nodes
3799 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3805 printf("You must specify the debug level. Valid levels are:\n");
3806 for (i=0; debug_levels[i].description != NULL; i++) {
3807 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3813 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3814 level = get_debug_by_desc(argv[0]);
3816 level = strtol(argv[0], NULL, 0);
3819 for (i=0; debug_levels[i].description != NULL; i++) {
3820 if (level == debug_levels[i].level) {
3824 if (debug_levels[i].description == NULL) {
3825 printf("Invalid debug level, must be one of\n");
3826 for (i=0; debug_levels[i].description != NULL; i++) {
3827 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3832 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
3834 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
3843 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
3849 priority = strtol(argv[0], NULL, 0);
3853 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
3855 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
3857 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
3864 attach to a database
3866 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
3868 const char *db_name;
3869 struct ctdb_db_context *ctdb_db;
3876 ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3877 if (ctdb_db == NULL) {
3878 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3888 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3890 struct ctdb_db_priority db_prio;
3897 db_prio.db_id = strtoul(argv[0], NULL, 0);
3898 db_prio.priority = strtoul(argv[1], NULL, 0);
3900 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
3902 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
3912 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3914 uint32_t db_id, priority;
3921 db_id = strtoul(argv[0], NULL, 0);
3923 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
3925 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
3929 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
3935 run an eventscript on a node
3937 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
3943 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3946 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3950 data.dptr = (unsigned char *)discard_const(argv[0]);
3951 data.dsize = strlen((char *)data.dptr) + 1;
3953 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
3955 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
3956 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3957 if (ret != 0 || res != 0) {
3958 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
3959 talloc_free(tmp_ctx);
3962 talloc_free(tmp_ctx);
3966 #define DB_VERSION 1
3967 #define MAX_DB_NAME 64
3968 struct db_file_header {
3969 unsigned long version;
3971 unsigned long persistent;
3973 const char name[MAX_DB_NAME];
3976 struct backup_data {
3977 struct ctdb_marshall_buffer *records;
3980 bool traverse_error;
3983 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
3985 struct backup_data *bd = talloc_get_type(private, struct backup_data);
3986 struct ctdb_rec_data *rec;
3988 /* add the record */
3989 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
3991 bd->traverse_error = true;
3992 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
3995 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
3996 if (bd->records == NULL) {
3997 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
3998 bd->traverse_error = true;
4001 bd->records->count++;
4002 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4003 bd->len += rec->length;
4011 * backup a database to a file
4013 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4016 struct ctdb_dbid_map *dbmap=NULL;
4017 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4018 struct db_file_header dbhdr;
4019 struct ctdb_db_context *ctdb_db;
4020 struct backup_data *bd;
4023 const char *reason = NULL;
4026 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4030 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4032 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4036 for(i=0;i<dbmap->num;i++){
4039 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4040 if(!strcmp(argv[0], name)){
4041 talloc_free(discard_const(name));
4044 talloc_free(discard_const(name));
4046 if (i == dbmap->num) {
4047 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4048 talloc_free(tmp_ctx);
4052 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4053 dbmap->dbs[i].dbid, tmp_ctx, &reason);
4055 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4057 talloc_free(tmp_ctx);
4061 uint32_t allow_unhealthy = 0;
4063 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4064 "AllowUnhealthyDBRead",
4067 if (allow_unhealthy != 1) {
4068 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4071 DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
4073 talloc_free(tmp_ctx);
4077 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4079 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4080 "tunnable AllowUnhealthyDBRead = %u\n",
4084 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4085 if (ctdb_db == NULL) {
4086 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4087 talloc_free(tmp_ctx);
4092 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4094 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4095 talloc_free(tmp_ctx);
4100 bd = talloc_zero(tmp_ctx, struct backup_data);
4102 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4103 talloc_free(tmp_ctx);
4107 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4108 if (bd->records == NULL) {
4109 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4110 talloc_free(tmp_ctx);
4114 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4115 bd->records->db_id = ctdb_db->db_id;
4116 /* traverse the database collecting all records */
4117 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4118 bd->traverse_error) {
4119 DEBUG(DEBUG_ERR,("Traverse error\n"));
4120 talloc_free(tmp_ctx);
4124 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4127 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4129 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4130 talloc_free(tmp_ctx);
4134 dbhdr.version = DB_VERSION;
4135 dbhdr.timestamp = time(NULL);
4136 dbhdr.persistent = dbmap->dbs[i].persistent;
4137 dbhdr.size = bd->len;
4138 if (strlen(argv[0]) >= MAX_DB_NAME) {
4139 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4142 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4143 ret = write(fh, &dbhdr, sizeof(dbhdr));
4145 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4148 ret = write(fh, bd->records, bd->len);
4150 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4159 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4162 talloc_free(tmp_ctx);
4167 * restore a database from a file
4169 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4172 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4175 struct db_file_header dbhdr;
4176 struct ctdb_db_context *ctdb_db;
4177 struct ctdb_node_map *nodemap=NULL;
4178 struct ctdb_vnn_map *vnnmap=NULL;
4180 struct ctdb_control_wipe_database w;
4182 uint32_t generation;
4187 if (argc < 1 || argc > 2) {
4188 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4192 fh = open(argv[0], O_RDONLY);
4194 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4195 talloc_free(tmp_ctx);
4199 read(fh, &dbhdr, sizeof(dbhdr));
4200 if (dbhdr.version != DB_VERSION) {
4201 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4202 talloc_free(tmp_ctx);
4206 dbname = discard_const(dbhdr.name);
4208 dbname = discard_const(argv[1]);
4211 outdata.dsize = dbhdr.size;
4212 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4213 if (outdata.dptr == NULL) {
4214 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4216 talloc_free(tmp_ctx);
4219 read(fh, outdata.dptr, outdata.dsize);
4222 tm = localtime(&dbhdr.timestamp);
4223 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4224 printf("Restoring database '%s' from backup @ %s\n",
4228 ctdb_db = ctdb_attach(ctdb, dbname, dbhdr.persistent, 0);
4229 if (ctdb_db == NULL) {
4230 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4231 talloc_free(tmp_ctx);
4235 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4237 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4238 talloc_free(tmp_ctx);
4243 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4245 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4246 talloc_free(tmp_ctx);
4250 /* freeze all nodes */
4251 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4252 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4253 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4259 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4260 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4261 talloc_free(tmp_ctx);
4266 generation = vnnmap->generation;
4267 data.dptr = (void *)&generation;
4268 data.dsize = sizeof(generation);
4270 /* start a cluster wide transaction */
4271 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4272 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4274 TIMELIMIT(), false, data,
4277 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4282 w.db_id = ctdb_db->db_id;
4283 w.transaction_id = generation;
4285 data.dptr = (void *)&w;
4286 data.dsize = sizeof(w);
4288 /* wipe all the remote databases. */
4289 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4290 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4292 TIMELIMIT(), false, data,
4295 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4296 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4297 talloc_free(tmp_ctx);
4301 /* push the database */
4302 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4303 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4305 TIMELIMIT(), false, outdata,
4308 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4309 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4310 talloc_free(tmp_ctx);
4314 data.dptr = (void *)&ctdb_db->db_id;
4315 data.dsize = sizeof(ctdb_db->db_id);
4317 /* mark the database as healthy */
4318 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4319 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4321 TIMELIMIT(), false, data,
4324 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4325 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4326 talloc_free(tmp_ctx);
4330 data.dptr = (void *)&generation;
4331 data.dsize = sizeof(generation);
4333 /* commit all the changes */
4334 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4336 TIMELIMIT(), false, data,
4339 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4340 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4341 talloc_free(tmp_ctx);
4346 /* thaw all nodes */
4347 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4348 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4354 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4355 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4356 talloc_free(tmp_ctx);
4361 talloc_free(tmp_ctx);
4366 * dump a database backup from a file
4368 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4370 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4372 struct db_file_header dbhdr;
4376 struct ctdb_rec_data *rec = NULL;
4377 struct ctdb_marshall_buffer *m;
4380 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4384 fh = open(argv[0], O_RDONLY);
4386 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4387 talloc_free(tmp_ctx);
4391 read(fh, &dbhdr, sizeof(dbhdr));
4392 if (dbhdr.version != DB_VERSION) {
4393 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4394 talloc_free(tmp_ctx);
4398 outdata.dsize = dbhdr.size;
4399 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4400 if (outdata.dptr == NULL) {
4401 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4403 talloc_free(tmp_ctx);
4406 read(fh, outdata.dptr, outdata.dsize);
4408 m = (struct ctdb_marshall_buffer *)outdata.dptr;
4410 tm = localtime(&dbhdr.timestamp);
4411 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4412 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4413 dbhdr.name, m->db_id, tbuf);
4415 for (i=0; i < m->count; i++) {
4419 /* we do not want the header splitted, so we pass NULL*/
4420 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4423 ctdb_dumpdb_record(ctdb, key, data, stdout);
4426 printf("Dumped %d records\n", i);
4427 talloc_free(tmp_ctx);
4432 * wipe a database from a file
4434 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4438 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4440 struct ctdb_db_context *ctdb_db;
4441 struct ctdb_node_map *nodemap = NULL;
4442 struct ctdb_vnn_map *vnnmap = NULL;
4444 struct ctdb_control_wipe_database w;
4446 uint32_t generation;
4447 struct ctdb_dbid_map *dbmap = NULL;
4450 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4454 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4457 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4462 for(i=0;i<dbmap->num;i++){
4465 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4466 dbmap->dbs[i].dbid, tmp_ctx, &name);
4467 if(!strcmp(argv[0], name)){
4468 talloc_free(discard_const(name));
4471 talloc_free(discard_const(name));
4473 if (i == dbmap->num) {
4474 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4476 talloc_free(tmp_ctx);
4480 ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4481 if (ctdb_db == NULL) {
4482 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4484 talloc_free(tmp_ctx);
4488 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4491 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4493 talloc_free(tmp_ctx);
4497 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4500 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4502 talloc_free(tmp_ctx);
4506 /* freeze all nodes */
4507 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4508 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4509 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4516 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4517 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4518 CTDB_RECOVERY_ACTIVE);
4519 talloc_free(tmp_ctx);
4524 generation = vnnmap->generation;
4525 data.dptr = (void *)&generation;
4526 data.dsize = sizeof(generation);
4528 /* start a cluster wide transaction */
4529 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4530 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4532 TIMELIMIT(), false, data,
4536 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4537 "transactions.\n"));
4541 w.db_id = ctdb_db->db_id;
4542 w.transaction_id = generation;
4544 data.dptr = (void *)&w;
4545 data.dsize = sizeof(w);
4547 /* wipe all the remote databases. */
4548 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4549 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4551 TIMELIMIT(), false, data,
4554 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4555 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4556 talloc_free(tmp_ctx);
4560 data.dptr = (void *)&ctdb_db->db_id;
4561 data.dsize = sizeof(ctdb_db->db_id);
4563 /* mark the database as healthy */
4564 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4565 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4567 TIMELIMIT(), false, data,
4570 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4571 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4572 talloc_free(tmp_ctx);
4576 data.dptr = (void *)&generation;
4577 data.dsize = sizeof(generation);
4579 /* commit all the changes */
4580 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4582 TIMELIMIT(), false, data,
4585 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4586 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4587 talloc_free(tmp_ctx);
4591 /* thaw all nodes */
4592 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4593 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4599 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4600 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4601 talloc_free(tmp_ctx);
4605 talloc_free(tmp_ctx);
4612 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4618 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4619 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4620 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4621 if (ret != 0 || res != 0) {
4622 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4623 talloc_free(tmp_ctx);
4626 write(1, data.dptr, data.dsize);
4627 talloc_free(tmp_ctx);
4632 handler for memory dumps
4634 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
4635 TDB_DATA data, void *private_data)
4637 write(1, data.dptr, data.dsize);
4642 dump memory usage on the recovery daemon
4644 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4648 struct rd_memdump_reply rd;
4650 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4652 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4655 rd.srvid = getpid();
4657 /* register a message port for receiveing the reply so that we
4658 can receive the reply
4660 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4663 data.dptr = (uint8_t *)&rd;
4664 data.dsize = sizeof(rd);
4666 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4668 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4672 /* this loop will terminate when we have received the reply */
4674 event_loop_once(ctdb->ev);
4681 send a message to a srvid
4683 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4685 unsigned long srvid;
4693 srvid = strtoul(argv[0], NULL, 0);
4695 data.dptr = (uint8_t *)discard_const(argv[1]);
4696 data.dsize= strlen(argv[1]);
4698 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4700 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4708 handler for msglisten
4710 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
4711 TDB_DATA data, void *private_data)
4715 printf("Message received: ");
4716 for (i=0;i<data.dsize;i++) {
4717 printf("%c", data.dptr[i]);
4723 listen for messages on a messageport
4725 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
4731 /* register a message port and listen for messages
4733 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
4734 printf("Listening for messages on srvid:%d\n", (int)srvid);
4737 event_loop_once(ctdb->ev);
4744 list all nodes in the cluster
4745 we parse the nodes file directly
4747 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
4749 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4750 struct pnn_node *pnn_nodes;
4751 struct pnn_node *pnn_node;
4753 pnn_nodes = read_nodes_file(mem_ctx);
4754 if (pnn_nodes == NULL) {
4755 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
4756 talloc_free(mem_ctx);
4760 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
4761 ctdb_sock_addr addr;
4762 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
4763 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
4764 talloc_free(mem_ctx);
4767 if (options.machinereadable){
4768 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
4770 printf("%s\n", pnn_node->addr);
4773 talloc_free(mem_ctx);
4779 reload the nodes file on the local node
4781 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
4785 struct ctdb_node_map *nodemap=NULL;
4787 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4789 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
4793 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
4795 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4799 /* reload the nodes file on all remote nodes */
4800 for (i=0;i<nodemap->num;i++) {
4801 if (nodemap->nodes[i].pnn == mypnn) {
4804 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
4805 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
4806 nodemap->nodes[i].pnn);
4808 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
4812 /* reload the nodes file on the local node */
4813 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
4814 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
4816 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
4819 /* initiate a recovery */
4820 control_recover(ctdb, argc, argv);
4826 static const struct {
4828 int (*fn)(struct ctdb_context *, int, const char **);
4830 bool without_daemon; /* can be run without daemon running ? */
4833 } ctdb_commands[] = {
4835 { "version", control_version, true, false, "show version of ctdb" },
4837 { "status", control_status, true, false, "show node status" },
4838 { "uptime", control_uptime, true, false, "show node uptime" },
4839 { "ping", control_ping, true, false, "ping all nodes" },
4840 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
4841 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
4842 { "listvars", control_listvars, true, false, "list tunable variables"},
4843 { "statistics", control_statistics, false, false, "show statistics" },
4844 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
4845 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
4846 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
4847 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
4848 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
4849 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
4850 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
4851 { "getdbmap", control_getdbmap, true, false, "show the database map" },
4852 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
4853 { "catdb", control_catdb, true, false, "dump a database" , "<dbname>"},
4854 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
4855 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
4856 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
4857 { "lvs", control_lvs, true, false, "show lvs configuration" },
4858 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
4859 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
4860 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
4861 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
4862 { "getdebug", control_getdebug, true, false, "get debug level" },
4863 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
4864 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
4865 { "attach", control_attach, true, false, "attach to a database", "<dbname>" },
4866 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
4867 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
4868 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
4869 { "disable", control_disable, true, false, "disable a nodes public IP" },
4870 { "enable", control_enable, true, false, "enable a nodes public IP" },
4871 { "stop", control_stop, true, false, "stop a node" },
4872 { "continue", control_continue, true, false, "re-start a stopped node" },
4873 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
4874 { "unban", control_unban, true, false, "unban a node" },
4875 { "showban", control_showban, true, false, "show ban information"},
4876 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
4877 { "recover", control_recover, true, false, "force recovery" },
4878 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
4879 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
4880 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
4881 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
4882 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
4883 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
4884 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
4885 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
4886 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
4888 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
4890 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
4891 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
4892 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
4893 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
4894 { "vacuum", ctdb_vacuum, false, false, "vacuum the databases of empty records", "[max_records]"},
4895 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
4896 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
4897 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
4898 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
4899 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
4900 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
4901 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
4902 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
4903 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
4904 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
4905 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
4906 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
4907 { "scriptstatus", control_scriptstatus, false, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
4908 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
4909 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
4910 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
4911 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
4912 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
4913 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
4914 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
4915 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
4916 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
4917 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
4918 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
4919 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
4920 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
4921 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
4922 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
4923 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
4924 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
4925 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
4926 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
4932 static void usage(void)
4936 "Usage: ctdb [options] <control>\n" \
4938 " -n <node> choose node number, or 'all' (defaults to local node)\n"
4939 " -Y generate machinereadable output\n"
4940 " -v generate verbose output\n"
4941 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
4942 printf("Controls:\n");
4943 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
4944 printf(" %-15s %-27s %s\n",
4945 ctdb_commands[i].name,
4946 ctdb_commands[i].args?ctdb_commands[i].args:"",
4947 ctdb_commands[i].msg);
4953 static void ctdb_alarm(int sig)
4955 printf("Maximum runtime exceeded - exiting\n");
4962 int main(int argc, const char *argv[])
4964 struct ctdb_context *ctdb;
4965 char *nodestring = NULL;
4966 struct poptOption popt_options[] = {
4969 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
4970 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
4971 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
4972 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
4973 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
4977 const char **extra_argv;
4981 struct event_context *ev;
4982 const char *control;
4986 /* set some defaults */
4987 options.maxruntime = 0;
4988 options.timelimit = 3;
4989 options.pnn = CTDB_CURRENT_NODE;
4991 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
4993 while ((opt = poptGetNextOpt(pc)) != -1) {
4996 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
4997 poptBadOption(pc, 0), poptStrerror(opt)));
5002 /* setup the remaining options for the main program to use */
5003 extra_argv = poptGetArgs(pc);
5006 while (extra_argv[extra_argc]) extra_argc++;
5009 if (extra_argc < 1) {
5013 if (options.maxruntime == 0) {
5014 const char *ctdb_timeout;
5015 ctdb_timeout = getenv("CTDB_TIMEOUT");
5016 if (ctdb_timeout != NULL) {
5017 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5019 /* default timeout is 120 seconds */
5020 options.maxruntime = 120;
5024 signal(SIGALRM, ctdb_alarm);
5025 alarm(options.maxruntime);
5027 /* setup the node number to contact */
5028 if (nodestring != NULL) {
5029 if (strcmp(nodestring, "all") == 0) {
5030 options.pnn = CTDB_BROADCAST_ALL;
5032 options.pnn = strtoul(nodestring, NULL, 0);
5036 control = extra_argv[0];
5038 ev = event_context_init(NULL);
5040 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5043 tevent_loop_allow_nesting(ev);
5045 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5046 if (strcmp(control, ctdb_commands[i].name) == 0) {
5049 if (ctdb_commands[i].without_daemon == true) {
5053 if (ctdb_commands[i].without_daemon == false) {
5054 const char *socket_name;
5056 /* initialise ctdb */
5057 ctdb = ctdb_cmdline_client(ev);
5060 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5064 /* initialize a libctdb connection as well */
5065 socket_name = ctdb_get_socketname(ctdb);
5066 ctdb_connection = ctdb_connect(socket_name,
5067 ctdb_log_file, stderr);
5068 if (ctdb_connection == NULL) {
5069 fprintf(stderr, "Failed to connect to daemon from libctdb\n");
5073 /* verify the node exists */
5076 if (options.pnn == CTDB_CURRENT_NODE) {
5078 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
5086 if (ctdb_commands[i].auto_all &&
5087 options.pnn == CTDB_BROADCAST_ALL) {
5092 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
5093 CTDB_NO_MEMORY(ctdb, nodes);
5095 for (j=0;j<num_nodes;j++) {
5096 options.pnn = nodes[j];
5097 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5101 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5107 if (i == ARRAY_SIZE(ctdb_commands)) {
5108 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));