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, bool *persistent)
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)) {
126 *persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
136 see if a process exists
138 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
146 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
147 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
151 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
153 printf("%u:%u exists\n", pnn, pid);
155 printf("%u:%u does not exist\n", pnn, pid);
161 display statistics structure
163 static void show_statistics(struct ctdb_statistics *s, int show_header)
165 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
167 const char *prefix=NULL;
169 int tmp, days, hours, minutes, seconds;
174 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
175 STATISTICS_FIELD(num_clients),
176 STATISTICS_FIELD(frozen),
177 STATISTICS_FIELD(recovering),
178 STATISTICS_FIELD(num_recoveries),
179 STATISTICS_FIELD(client_packets_sent),
180 STATISTICS_FIELD(client_packets_recv),
181 STATISTICS_FIELD(node_packets_sent),
182 STATISTICS_FIELD(node_packets_recv),
183 STATISTICS_FIELD(keepalive_packets_sent),
184 STATISTICS_FIELD(keepalive_packets_recv),
185 STATISTICS_FIELD(node.req_call),
186 STATISTICS_FIELD(node.reply_call),
187 STATISTICS_FIELD(node.req_dmaster),
188 STATISTICS_FIELD(node.reply_dmaster),
189 STATISTICS_FIELD(node.reply_error),
190 STATISTICS_FIELD(node.req_message),
191 STATISTICS_FIELD(node.req_control),
192 STATISTICS_FIELD(node.reply_control),
193 STATISTICS_FIELD(client.req_call),
194 STATISTICS_FIELD(client.req_message),
195 STATISTICS_FIELD(client.req_control),
196 STATISTICS_FIELD(timeouts.call),
197 STATISTICS_FIELD(timeouts.control),
198 STATISTICS_FIELD(timeouts.traverse),
199 STATISTICS_FIELD(total_calls),
200 STATISTICS_FIELD(pending_calls),
201 STATISTICS_FIELD(lockwait_calls),
202 STATISTICS_FIELD(pending_lockwait_calls),
203 STATISTICS_FIELD(childwrite_calls),
204 STATISTICS_FIELD(pending_childwrite_calls),
205 STATISTICS_FIELD(memory_used),
206 STATISTICS_FIELD(max_hop_count),
208 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
217 if (options.machinereadable){
219 printf("CTDB version:");
220 printf("Current time of statistics:");
221 printf("Statistics collected since:");
222 for (i=0;i<ARRAY_SIZE(fields);i++) {
223 printf("%s:", fields[i].name);
225 printf("num_reclock_ctdbd_latency:");
226 printf("min_reclock_ctdbd_latency:");
227 printf("avg_reclock_ctdbd_latency:");
228 printf("max_reclock_ctdbd_latency:");
230 printf("num_reclock_recd_latency:");
231 printf("min_reclock_recd_latency:");
232 printf("avg_reclock_recd_latency:");
233 printf("max_reclock_recd_latency:");
235 printf("num_call_latency:");
236 printf("min_call_latency:");
237 printf("avg_call_latency:");
238 printf("max_call_latency:");
240 printf("num_lockwait_latency:");
241 printf("min_lockwait_latency:");
242 printf("avg_lockwait_latency:");
243 printf("max_lockwait_latency:");
245 printf("num_childwrite_latency:");
246 printf("min_childwrite_latency:");
247 printf("avg_childwrite_latency:");
248 printf("max_childwrite_latency:");
251 printf("%d:", CTDB_VERSION);
252 printf("%d:", (int)s->statistics_current_time.tv_sec);
253 printf("%d:", (int)s->statistics_start_time.tv_sec);
254 for (i=0;i<ARRAY_SIZE(fields);i++) {
255 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
257 printf("%d:", s->reclock.ctdbd.num);
258 printf("%.6f:", s->reclock.ctdbd.min);
259 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
260 printf("%.6f:", s->reclock.ctdbd.max);
262 printf("%d:", s->reclock.recd.num);
263 printf("%.6f:", s->reclock.recd.min);
264 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
265 printf("%.6f:", s->reclock.recd.max);
267 printf("%d:", s->call_latency.num);
268 printf("%.6f:", s->call_latency.min);
269 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
270 printf("%.6f:", s->call_latency.max);
272 printf("%d:", s->lockwait_latency.num);
273 printf("%.6f:", s->lockwait_latency.min);
274 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
275 printf("%.6f:", s->lockwait_latency.max);
277 printf("%d:", s->childwrite_latency.num);
278 printf("%.6f:", s->childwrite_latency.min);
279 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
280 printf("%.6f:", s->childwrite_latency.max);
283 printf("CTDB version %u\n", CTDB_VERSION);
284 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
285 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
287 for (i=0;i<ARRAY_SIZE(fields);i++) {
288 if (strchr(fields[i].name, '.')) {
289 preflen = strcspn(fields[i].name, ".")+1;
290 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
291 prefix = fields[i].name;
292 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
297 printf(" %*s%-22s%*s%10u\n",
299 fields[i].name+preflen,
301 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
303 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);
305 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);
307 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);
308 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);
309 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);
312 talloc_free(tmp_ctx);
316 display remote ctdb statistics combined from all nodes
318 static int control_statistics_all(struct ctdb_context *ctdb)
321 struct ctdb_statistics statistics;
325 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
326 CTDB_NO_MEMORY(ctdb, nodes);
328 ZERO_STRUCT(statistics);
330 for (i=0;i<num_nodes;i++) {
331 struct ctdb_statistics s1;
333 uint32_t *v1 = (uint32_t *)&s1;
334 uint32_t *v2 = (uint32_t *)&statistics;
336 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
337 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
339 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
342 for (j=0;j<num_ints;j++) {
345 statistics.max_hop_count =
346 MAX(statistics.max_hop_count, s1.max_hop_count);
347 statistics.call_latency.max =
348 MAX(statistics.call_latency.max, s1.call_latency.max);
349 statistics.lockwait_latency.max =
350 MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
353 printf("Gathered statistics for %u nodes\n", num_nodes);
354 show_statistics(&statistics, 1);
359 display remote ctdb statistics
361 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
364 struct ctdb_statistics statistics;
366 if (options.pnn == CTDB_BROADCAST_ALL) {
367 return control_statistics_all(ctdb);
370 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
372 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
375 show_statistics(&statistics, 1);
381 reset remote ctdb statistics
383 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
387 ret = ctdb_statistics_reset(ctdb, options.pnn);
389 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
397 display remote ctdb rolling statistics
399 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
402 struct ctdb_statistics_wire *stats;
403 int i, num_records = -1;
406 num_records = atoi(argv[0]) - 1;
409 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
411 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
414 for (i=0;i<stats->num;i++) {
415 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
418 show_statistics(&stats->stats[i], i==0);
419 if (i == num_records) {
428 display uptime of remote node
430 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
433 struct ctdb_uptime *uptime = NULL;
434 int tmp, days, hours, minutes, seconds;
436 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
438 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
442 if (options.machinereadable){
443 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
444 printf(":%u:%u:%u:%lf\n",
445 (unsigned int)uptime->current_time.tv_sec,
446 (unsigned int)uptime->ctdbd_start_time.tv_sec,
447 (unsigned int)uptime->last_recovery_finished.tv_sec,
448 timeval_delta(&uptime->last_recovery_finished,
449 &uptime->last_recovery_started)
454 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
456 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
464 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
466 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
474 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
476 printf("Duration of last recovery/failover: %lf seconds\n",
477 timeval_delta(&uptime->last_recovery_finished,
478 &uptime->last_recovery_started));
484 show the PNN of the current node
486 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
491 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
493 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
497 printf("PNN:%d\n", mypnn);
503 struct pnn_node *next;
508 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
510 const char *nodes_list;
514 struct pnn_node *pnn_nodes = NULL;
515 struct pnn_node *pnn_node;
516 struct pnn_node *tmp_node;
518 /* read the nodes file */
519 nodes_list = getenv("CTDB_NODES");
520 if (nodes_list == NULL) {
521 nodes_list = "/etc/ctdb/nodes";
523 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
527 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
530 for (i=0, pnn=0; i<nlines; i++) {
534 /* strip leading spaces */
535 while((*node == ' ') || (*node == '\t')) {
542 if (strcmp(node, "") == 0) {
545 pnn_node = talloc(mem_ctx, struct pnn_node);
546 pnn_node->pnn = pnn++;
547 pnn_node->addr = talloc_strdup(pnn_node, node);
548 pnn_node->next = pnn_nodes;
549 pnn_nodes = pnn_node;
552 /* swap them around so we return them in incrementing order */
553 pnn_node = pnn_nodes;
557 pnn_node = pnn_node->next;
559 tmp_node->next = pnn_nodes;
560 pnn_nodes = tmp_node;
567 show the PNN of the current node
568 discover the pnn by loading the nodes file and try to bind to all
569 addresses one at a time until the ip address is found.
571 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
573 TALLOC_CTX *mem_ctx = talloc_new(NULL);
574 struct pnn_node *pnn_nodes;
575 struct pnn_node *pnn_node;
577 pnn_nodes = read_nodes_file(mem_ctx);
578 if (pnn_nodes == NULL) {
579 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
580 talloc_free(mem_ctx);
584 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
587 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
588 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
589 talloc_free(mem_ctx);
593 if (ctdb_sys_have_ip(&addr)) {
594 printf("PNN:%d\n", pnn_node->pnn);
595 talloc_free(mem_ctx);
600 printf("Failed to detect which PNN this node is\n");
601 talloc_free(mem_ctx);
606 display remote ctdb status
608 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
611 struct ctdb_vnn_map *vnnmap=NULL;
612 struct ctdb_node_map *nodemap=NULL;
613 uint32_t recmode, recmaster;
616 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
621 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
623 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
627 if (options.machinereadable) {
628 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
629 ":Inactive:PartiallyOnline:ThisNode:\n");
630 for (i=0;i<nodemap->num;i++) {
631 int partially_online = 0;
634 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
637 if (nodemap->nodes[i].flags == 0) {
638 struct ctdb_control_get_ifaces *ifaces;
640 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(),
641 nodemap->nodes[i].pnn,
644 for (j=0; j < ifaces->num; j++) {
645 if (ifaces->ifaces[j].link_state != 0) {
648 partially_online = 1;
654 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", nodemap->nodes[i].pnn,
655 ctdb_addr_to_str(&nodemap->nodes[i].addr),
656 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
657 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
658 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
659 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
660 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED),
661 !!(nodemap->nodes[i].flags&NODE_FLAGS_INACTIVE),
663 (nodemap->nodes[i].pnn == mypnn)?'Y':'N');
668 printf("Number of nodes:%d\n", nodemap->num);
669 for(i=0;i<nodemap->num;i++){
670 static const struct {
674 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
675 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
676 { NODE_FLAGS_BANNED, "BANNED" },
677 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
678 { NODE_FLAGS_DELETED, "DELETED" },
679 { NODE_FLAGS_STOPPED, "STOPPED" },
680 { NODE_FLAGS_INACTIVE, "INACTIVE" },
682 char *flags_str = NULL;
685 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
688 if (nodemap->nodes[i].flags == 0) {
689 struct ctdb_control_get_ifaces *ifaces;
691 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(),
692 nodemap->nodes[i].pnn,
695 for (j=0; j < ifaces->num; j++) {
696 if (ifaces->ifaces[j].link_state != 0) {
699 flags_str = talloc_strdup(ctdb, "PARTIALLYONLINE");
705 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
706 if (nodemap->nodes[i].flags & flag_names[j].flag) {
707 if (flags_str == NULL) {
708 flags_str = talloc_strdup(ctdb, flag_names[j].name);
710 flags_str = talloc_asprintf_append(flags_str, "|%s",
713 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
716 if (flags_str == NULL) {
717 flags_str = talloc_strdup(ctdb, "OK");
718 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
720 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
721 ctdb_addr_to_str(&nodemap->nodes[i].addr),
723 nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
724 talloc_free(flags_str);
727 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
729 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
732 if (vnnmap->generation == INVALID_GENERATION) {
733 printf("Generation:INVALID\n");
735 printf("Generation:%d\n",vnnmap->generation);
737 printf("Size:%d\n",vnnmap->size);
738 for(i=0;i<vnnmap->size;i++){
739 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
742 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
743 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
746 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
748 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
749 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
752 printf("Recovery master:%d\n",recmaster);
759 struct natgw_node *next;
764 display the list of nodes belonging to this natgw configuration
766 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
769 uint32_t capabilities;
770 const char *natgw_list;
773 struct natgw_node *natgw_nodes = NULL;
774 struct natgw_node *natgw_node;
775 struct ctdb_node_map *nodemap=NULL;
778 /* read the natgw nodes file into a linked list */
779 natgw_list = getenv("NATGW_NODES");
780 if (natgw_list == NULL) {
781 natgw_list = "/etc/ctdb/natgw_nodes";
783 lines = file_lines_load(natgw_list, &nlines, ctdb);
785 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
788 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
791 for (i=0;i<nlines;i++) {
795 /* strip leading spaces */
796 while((*node == ' ') || (*node == '\t')) {
802 if (strcmp(node, "") == 0) {
805 natgw_node = talloc(ctdb, struct natgw_node);
806 natgw_node->addr = talloc_strdup(natgw_node, node);
807 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
808 natgw_node->next = natgw_nodes;
809 natgw_nodes = natgw_node;
812 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
814 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
819 while(i<nodemap->num) {
820 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
821 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
826 /* this node was not in the natgw so we just remove it from
829 if ((natgw_node == NULL)
830 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
833 for (j=i+1; j<nodemap->num; j++) {
834 nodemap->nodes[j-1] = nodemap->nodes[j];
843 /* pick a node to be natgwmaster
844 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
846 for(i=0;i<nodemap->num;i++){
847 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
848 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
850 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
853 if (!(capabilities&CTDB_CAP_NATGW)) {
856 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
860 /* we couldnt find any healthy node, try unhealthy ones */
861 if (i == nodemap->num) {
862 for(i=0;i<nodemap->num;i++){
863 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
864 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
866 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
869 if (!(capabilities&CTDB_CAP_NATGW)) {
872 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
877 /* unless all nodes are STOPPED, when we pick one anyway */
878 if (i == nodemap->num) {
879 for(i=0;i<nodemap->num;i++){
880 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
881 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
883 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
886 if (!(capabilities&CTDB_CAP_NATGW)) {
889 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
893 /* or if we still can not find any */
894 if (i == nodemap->num) {
895 printf("-1 0.0.0.0\n");
896 ret = 2; /* matches ENOENT */
900 /* print the pruned list of nodes belonging to this natgw list */
901 for(i=0;i<nodemap->num;i++){
902 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
905 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
906 ctdb_addr_to_str(&nodemap->nodes[i].addr),
907 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
908 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
909 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
910 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
911 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
918 display the status of the scripts for monitoring (or other events)
920 static int control_one_scriptstatus(struct ctdb_context *ctdb,
921 enum ctdb_eventscript_call type)
923 struct ctdb_scripts_wire *script_status;
926 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
928 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
932 if (script_status == NULL) {
933 if (!options.machinereadable) {
934 printf("%s cycle never run\n",
935 ctdb_eventscript_call_names[type]);
940 if (!options.machinereadable) {
941 printf("%d scripts were executed last %s cycle\n",
942 script_status->num_scripts,
943 ctdb_eventscript_call_names[type]);
945 for (i=0; i<script_status->num_scripts; i++) {
946 const char *status = NULL;
948 switch (script_status->scripts[i].status) {
959 if (script_status->scripts[i].status > 0)
963 if (options.machinereadable) {
964 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
965 ctdb_eventscript_call_names[type],
966 script_status->scripts[i].name,
967 script_status->scripts[i].status,
969 (long)script_status->scripts[i].start.tv_sec,
970 (long)script_status->scripts[i].start.tv_usec,
971 (long)script_status->scripts[i].finished.tv_sec,
972 (long)script_status->scripts[i].finished.tv_usec,
973 script_status->scripts[i].output);
977 printf("%-20s Status:%s ",
978 script_status->scripts[i].name, status);
980 /* Some other error, eg from stat. */
981 printf("%-20s Status:CANNOT RUN (%s)",
982 script_status->scripts[i].name,
983 strerror(-script_status->scripts[i].status));
985 if (script_status->scripts[i].status >= 0) {
986 printf("Duration:%.3lf ",
987 timeval_delta(&script_status->scripts[i].finished,
988 &script_status->scripts[i].start));
990 if (script_status->scripts[i].status != -ENOEXEC) {
992 ctime(&script_status->scripts[i].start.tv_sec));
993 if (script_status->scripts[i].status != 0) {
994 printf(" OUTPUT:%s\n",
995 script_status->scripts[i].output);
1005 static int control_scriptstatus(struct ctdb_context *ctdb,
1006 int argc, const char **argv)
1009 enum ctdb_eventscript_call type, min, max;
1013 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1018 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1022 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1023 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1029 if (type == CTDB_EVENT_MAX) {
1030 if (strcmp(arg, "all") == 0) {
1032 max = CTDB_EVENT_MAX;
1034 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1039 if (options.machinereadable) {
1040 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1043 for (type = min; type < max; type++) {
1044 ret = control_one_scriptstatus(ctdb, type);
1054 enable an eventscript
1056 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1064 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1066 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1074 disable an eventscript
1076 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1084 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1086 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1094 display the pnn of the recovery master
1096 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1100 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1101 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1104 printf("%d\n",recmaster);
1110 add a tickle to a public address
1112 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1114 struct ctdb_tcp_connection t;
1122 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1123 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1126 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1127 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1131 data.dptr = (uint8_t *)&t;
1132 data.dsize = sizeof(t);
1134 /* tell all nodes about this tcp connection */
1135 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1136 0, data, ctdb, NULL, NULL, NULL, NULL);
1138 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1147 delete a tickle from a node
1149 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1151 struct ctdb_tcp_connection t;
1159 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1160 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1163 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1164 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1168 data.dptr = (uint8_t *)&t;
1169 data.dsize = sizeof(t);
1171 /* tell all nodes about this tcp connection */
1172 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1173 0, data, ctdb, NULL, NULL, NULL, NULL);
1175 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1184 get a list of all tickles for this pnn
1186 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1188 struct ctdb_control_tcp_tickle_list *list;
1189 ctdb_sock_addr addr;
1198 port = atoi(argv[1]);
1201 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1202 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1206 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1208 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1212 if (options.machinereadable){
1213 printf(":source ip:port:destination ip:port:\n");
1214 for (i=0;i<list->tickles.num;i++) {
1215 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1218 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1219 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1222 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1223 printf("Num tickles:%u\n", list->tickles.num);
1224 for (i=0;i<list->tickles.num;i++) {
1225 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1228 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1229 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));
1239 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1241 struct ctdb_all_public_ips *ips;
1242 struct ctdb_public_ip ip;
1245 uint32_t disable_time;
1247 struct ctdb_node_map *nodemap=NULL;
1248 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1251 data.dptr = (uint8_t*)&disable_time;
1252 data.dsize = sizeof(disable_time);
1253 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1255 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1261 /* read the public ip list from the node */
1262 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1264 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1265 talloc_free(tmp_ctx);
1269 for (i=0;i<ips->num;i++) {
1270 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1275 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1276 pnn, ctdb_addr_to_str(addr)));
1277 talloc_free(tmp_ctx);
1284 data.dptr = (uint8_t *)&ip;
1285 data.dsize = sizeof(ip);
1287 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1289 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1290 talloc_free(tmp_ctx);
1294 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1295 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1302 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1303 talloc_free(tmp_ctx);
1307 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1309 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1310 talloc_free(tmp_ctx);
1314 /* update the recovery daemon so it now knows to expect the new
1315 node assignment for this ip.
1317 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1319 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1323 talloc_free(tmp_ctx);
1328 move/failover an ip address to a specific node
1330 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1333 int ret, retries = 0;
1334 ctdb_sock_addr addr;
1341 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1342 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1347 if (sscanf(argv[1], "%u", &pnn) != 1) {
1348 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1353 ret = move_ip(ctdb, &addr, pnn);
1355 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1359 } while (retries < 5 && ret != 0);
1361 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1368 void getips_store_callback(void *param, void *data)
1370 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1371 struct ctdb_all_public_ips *ips = param;
1375 ips->ips[i].pnn = node_ip->pnn;
1376 ips->ips[i].addr = node_ip->addr;
1379 void getips_count_callback(void *param, void *data)
1381 uint32_t *count = param;
1387 static uint32_t *ip_key(ctdb_sock_addr *ip)
1389 static uint32_t key[IP_KEYLEN];
1391 bzero(key, sizeof(key));
1393 switch (ip->sa.sa_family) {
1395 key[0] = ip->ip.sin_addr.s_addr;
1398 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
1399 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
1400 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
1401 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
1404 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1411 static void *add_ip_callback(void *parm, void *data)
1417 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1419 struct ctdb_all_public_ips *tmp_ips;
1420 struct ctdb_node_map *nodemap=NULL;
1421 trbt_tree_t *ip_tree;
1425 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1427 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1431 ip_tree = trbt_create(tmp_ctx, 0);
1433 for(i=0;i<nodemap->num;i++){
1434 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1437 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1441 /* read the public ip list from this node */
1442 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1444 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1448 for (j=0; j<tmp_ips->num;j++) {
1449 struct ctdb_public_ip *node_ip;
1451 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1452 node_ip->pnn = tmp_ips->ips[j].pnn;
1453 node_ip->addr = tmp_ips->ips[j].addr;
1455 trbt_insertarray32_callback(ip_tree,
1456 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1460 talloc_free(tmp_ips);
1465 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1467 len = offsetof(struct ctdb_all_public_ips, ips) +
1468 count*sizeof(struct ctdb_public_ip);
1469 tmp_ips = talloc_zero_size(tmp_ctx, len);
1470 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1479 * scans all other nodes and returns a pnn for another node that can host this
1483 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1485 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1486 struct ctdb_all_public_ips *ips;
1487 struct ctdb_node_map *nodemap=NULL;
1490 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1492 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1493 talloc_free(tmp_ctx);
1497 for(i=0;i<nodemap->num;i++){
1498 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1501 if (nodemap->nodes[i].pnn == options.pnn) {
1505 /* read the public ip list from this node */
1506 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1508 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1512 for (j=0;j<ips->num;j++) {
1513 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1514 talloc_free(tmp_ctx);
1515 return nodemap->nodes[i].pnn;
1521 talloc_free(tmp_ctx);
1525 static uint32_t ipreallocate_finished;
1528 handler for receiving the response to ipreallocate
1530 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1531 TDB_DATA data, void *private_data)
1533 ipreallocate_finished = 1;
1536 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1538 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1540 event_add_timed(ctdb->ev, ctdb,
1541 timeval_current_ofs(1, 0),
1542 ctdb_every_second, ctdb);
1546 ask the recovery daemon on the recovery master to perform a ip reallocation
1548 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1552 struct takeover_run_reply rd;
1554 struct ctdb_node_map *nodemap=NULL;
1556 struct timeval tv = timeval_current();
1558 /* we need some events to trigger so we can timeout and restart
1561 event_add_timed(ctdb->ev, ctdb,
1562 timeval_current_ofs(1, 0),
1563 ctdb_every_second, ctdb);
1565 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1567 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1570 rd.srvid = getpid();
1572 /* register a message port for receiveing the reply so that we
1573 can receive the reply
1575 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1577 data.dptr = (uint8_t *)&rd;
1578 data.dsize = sizeof(rd);
1581 /* check that there are valid nodes available */
1582 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1583 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1586 for (i=0; i<nodemap->num;i++) {
1587 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1591 if (i==nodemap->num) {
1592 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1597 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1598 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1602 /* verify the node exists */
1603 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1604 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1609 /* check tha there are nodes available that can act as a recmaster */
1610 for (i=0; i<nodemap->num; i++) {
1611 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1616 if (i == nodemap->num) {
1617 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1621 /* verify the recovery master is not STOPPED, nor BANNED */
1622 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1623 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1629 /* verify the recovery master is not STOPPED, nor BANNED */
1630 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1631 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1637 ipreallocate_finished = 0;
1638 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1640 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1644 tv = timeval_current();
1645 /* this loop will terminate when we have received the reply */
1646 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1647 event_loop_once(ctdb->ev);
1649 if (ipreallocate_finished == 1) {
1662 add a public ip address to a node
1664 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1667 int len, retries = 0;
1669 ctdb_sock_addr addr;
1670 struct ctdb_control_ip_iface *pub;
1671 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1672 struct ctdb_all_public_ips *ips;
1676 talloc_free(tmp_ctx);
1680 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1681 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1682 talloc_free(tmp_ctx);
1686 /* read the public ip list from the node */
1687 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1689 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1690 talloc_free(tmp_ctx);
1693 for (i=0;i<ips->num;i++) {
1694 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1695 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1702 /* Dont timeout. This command waits for an ip reallocation
1703 which sometimes can take wuite a while if there has
1704 been a recent recovery
1708 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1709 pub = talloc_size(tmp_ctx, len);
1710 CTDB_NO_MEMORY(ctdb, pub);
1714 pub->len = strlen(argv[1])+1;
1715 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1718 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1720 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1724 } while (retries < 5 && ret != 0);
1726 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1727 talloc_free(tmp_ctx);
1732 ret = control_ipreallocate(ctdb, argc, argv);
1734 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Wait 3 seconds and try again.\n", options.pnn));
1738 } while (retries < 5 && ret != 0);
1740 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Giving up.\n", options.pnn));
1741 talloc_free(tmp_ctx);
1745 talloc_free(tmp_ctx);
1749 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1751 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1753 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1754 struct ctdb_node_map *nodemap=NULL;
1755 struct ctdb_all_public_ips *ips;
1758 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1760 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1764 /* remove it from the nodes that are not hosting the ip currently */
1765 for(i=0;i<nodemap->num;i++){
1766 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1769 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1770 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1774 for (j=0;j<ips->num;j++) {
1775 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1783 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1787 options.pnn = nodemap->nodes[i].pnn;
1788 control_delip(ctdb, argc, argv);
1792 /* remove it from every node (also the one hosting it) */
1793 for(i=0;i<nodemap->num;i++){
1794 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1797 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1798 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1802 for (j=0;j<ips->num;j++) {
1803 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1811 options.pnn = nodemap->nodes[i].pnn;
1812 control_delip(ctdb, argc, argv);
1815 talloc_free(tmp_ctx);
1820 delete a public ip address from a node
1822 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1826 ctdb_sock_addr addr;
1827 struct ctdb_control_ip_iface pub;
1828 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1829 struct ctdb_all_public_ips *ips;
1832 talloc_free(tmp_ctx);
1836 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1837 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1841 if (options.pnn == CTDB_BROADCAST_ALL) {
1842 return control_delip_all(ctdb, argc, argv, &addr);
1849 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1851 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1852 talloc_free(tmp_ctx);
1856 for (i=0;i<ips->num;i++) {
1857 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1863 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1864 ctdb_addr_to_str(&addr)));
1865 talloc_free(tmp_ctx);
1869 if (ips->ips[i].pnn == options.pnn) {
1870 ret = find_other_host_for_public_ip(ctdb, &addr);
1873 ret = move_ip(ctdb, &addr, ret);
1875 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
1879 } while (retries < 5 && ret != 0);
1881 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
1887 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1889 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1890 talloc_free(tmp_ctx);
1894 talloc_free(tmp_ctx);
1899 kill a tcp connection
1901 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1904 struct ctdb_control_killtcp killtcp;
1910 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1911 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1915 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1916 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1920 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1922 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1933 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1936 ctdb_sock_addr addr;
1942 if (!parse_ip(argv[0], NULL, 0, &addr)) {
1943 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1947 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1949 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1957 register a server id
1959 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1962 struct ctdb_server_id server_id;
1968 server_id.pnn = strtoul(argv[0], NULL, 0);
1969 server_id.type = strtoul(argv[1], NULL, 0);
1970 server_id.server_id = strtoul(argv[2], NULL, 0);
1972 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1974 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1977 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
1983 unregister a server id
1985 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1988 struct ctdb_server_id server_id;
1994 server_id.pnn = strtoul(argv[0], NULL, 0);
1995 server_id.type = strtoul(argv[1], NULL, 0);
1996 server_id.server_id = strtoul(argv[2], NULL, 0);
1998 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2000 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2007 check if a server id exists
2009 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2013 struct ctdb_server_id server_id;
2019 server_id.pnn = strtoul(argv[0], NULL, 0);
2020 server_id.type = strtoul(argv[1], NULL, 0);
2021 server_id.server_id = strtoul(argv[2], NULL, 0);
2023 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2025 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2030 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2032 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2038 get a list of all server ids that are registered on a node
2040 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2043 struct ctdb_server_id_list *server_ids;
2045 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2047 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2051 for (i=0; i<server_ids->num; i++) {
2052 printf("Server id %d:%d:%d\n",
2053 server_ids->server_ids[i].pnn,
2054 server_ids->server_ids[i].type,
2055 server_ids->server_ids[i].server_id);
2062 send a tcp tickle ack
2064 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2067 ctdb_sock_addr src, dst;
2073 if (!parse_ip_port(argv[0], &src)) {
2074 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2078 if (!parse_ip_port(argv[1], &dst)) {
2079 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2083 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2087 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2094 display public ip status
2096 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2099 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2100 struct ctdb_all_public_ips *ips;
2102 if (options.pnn == CTDB_BROADCAST_ALL) {
2103 /* read the list of public ips from all nodes */
2104 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2106 /* read the public ip list from this node */
2107 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2110 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2111 talloc_free(tmp_ctx);
2115 if (options.machinereadable){
2116 printf(":Public IP:Node:");
2117 if (options.verbose){
2118 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2122 if (options.pnn == CTDB_BROADCAST_ALL) {
2123 printf("Public IPs on ALL nodes\n");
2125 printf("Public IPs on node %u\n", options.pnn);
2129 for (i=1;i<=ips->num;i++) {
2130 struct ctdb_control_public_ip_info *info = NULL;
2132 char *aciface = NULL;
2133 char *avifaces = NULL;
2134 char *cifaces = NULL;
2136 if (options.pnn == CTDB_BROADCAST_ALL) {
2137 pnn = ips->ips[ips->num-i].pnn;
2143 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2144 &ips->ips[ips->num-i].addr, &info);
2151 for (j=0; j < info->num; j++) {
2152 if (cifaces == NULL) {
2153 cifaces = talloc_strdup(info,
2154 info->ifaces[j].name);
2156 cifaces = talloc_asprintf_append(cifaces,
2158 info->ifaces[j].name);
2161 if (info->active_idx == j) {
2162 aciface = info->ifaces[j].name;
2165 if (info->ifaces[j].link_state == 0) {
2169 if (avifaces == NULL) {
2170 avifaces = talloc_strdup(info, info->ifaces[j].name);
2172 avifaces = talloc_asprintf_append(avifaces,
2174 info->ifaces[j].name);
2179 if (options.machinereadable){
2181 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2182 ips->ips[ips->num-i].pnn);
2183 if (options.verbose){
2186 avifaces?avifaces:"",
2187 cifaces?cifaces:"");
2191 if (options.verbose) {
2192 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2193 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2194 ips->ips[ips->num-i].pnn,
2196 avifaces?avifaces:"",
2197 cifaces?cifaces:"");
2200 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2201 ips->ips[ips->num-i].pnn);
2207 talloc_free(tmp_ctx);
2214 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2217 ctdb_sock_addr addr;
2218 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2219 struct ctdb_control_public_ip_info *info;
2222 talloc_free(tmp_ctx);
2226 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2227 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2231 /* read the public ip info from this node */
2232 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2233 tmp_ctx, &addr, &info);
2235 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2236 argv[0], options.pnn));
2237 talloc_free(tmp_ctx);
2241 printf("Public IP[%s] info on node %u\n",
2242 ctdb_addr_to_str(&info->ip.addr),
2245 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2246 ctdb_addr_to_str(&info->ip.addr),
2247 info->ip.pnn, info->num);
2249 for (i=0; i<info->num; i++) {
2250 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2252 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2253 i+1, info->ifaces[i].name,
2254 info->ifaces[i].link_state?"up":"down",
2255 (unsigned int)info->ifaces[i].references,
2256 (i==info->active_idx)?" (active)":"");
2259 talloc_free(tmp_ctx);
2264 display interfaces status
2266 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2269 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2270 struct ctdb_control_get_ifaces *ifaces;
2272 /* read the public ip list from this node */
2273 ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn,
2276 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2278 talloc_free(tmp_ctx);
2282 if (options.machinereadable){
2283 printf(":Name:LinkStatus:References:\n");
2285 printf("Interfaces on node %u\n", options.pnn);
2288 for (i=0; i<ifaces->num; i++) {
2289 if (options.machinereadable){
2290 printf(":%s:%s:%u\n",
2291 ifaces->ifaces[i].name,
2292 ifaces->ifaces[i].link_state?"1":"0",
2293 (unsigned int)ifaces->ifaces[i].references);
2295 printf("name:%s link:%s references:%u\n",
2296 ifaces->ifaces[i].name,
2297 ifaces->ifaces[i].link_state?"up":"down",
2298 (unsigned int)ifaces->ifaces[i].references);
2302 talloc_free(tmp_ctx);
2308 set link status of an interface
2310 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2313 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2314 struct ctdb_control_iface_info info;
2322 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2323 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2325 talloc_free(tmp_ctx);
2328 strcpy(info.name, argv[0]);
2330 if (strcmp(argv[1], "up") == 0) {
2331 info.link_state = 1;
2332 } else if (strcmp(argv[1], "down") == 0) {
2333 info.link_state = 0;
2335 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2337 talloc_free(tmp_ctx);
2341 /* read the public ip list from this node */
2342 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2345 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2346 argv[0], options.pnn));
2347 talloc_free(tmp_ctx);
2351 talloc_free(tmp_ctx);
2356 display pid of a ctdb daemon
2358 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2363 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2365 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2368 printf("Pid:%d\n", pid);
2374 disable a remote node
2376 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2379 struct ctdb_node_map *nodemap=NULL;
2381 /* check if the node is already disabled */
2382 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2383 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2386 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2387 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2392 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2394 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2400 /* read the nodemap and verify the change took effect */
2401 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2402 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2406 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2407 ret = control_ipreallocate(ctdb, argc, argv);
2409 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2417 enable a disabled remote node
2419 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2423 struct ctdb_node_map *nodemap=NULL;
2426 /* check if the node is already enabled */
2427 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2428 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2431 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2432 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2437 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2439 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2445 /* read the nodemap and verify the change took effect */
2446 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2447 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2451 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2453 ret = control_ipreallocate(ctdb, argc, argv);
2455 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2465 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2468 struct ctdb_node_map *nodemap=NULL;
2471 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2473 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2478 /* read the nodemap and verify the change took effect */
2479 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2480 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2484 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2485 ret = control_ipreallocate(ctdb, argc, argv);
2487 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2495 restart a stopped remote node
2497 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2501 struct ctdb_node_map *nodemap=NULL;
2504 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2506 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2512 /* read the nodemap and verify the change took effect */
2513 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2514 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2518 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2519 ret = control_ipreallocate(ctdb, argc, argv);
2521 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2528 static uint32_t get_generation(struct ctdb_context *ctdb)
2530 struct ctdb_vnn_map *vnnmap=NULL;
2533 /* wait until the recmaster is not in recovery mode */
2535 uint32_t recmode, recmaster;
2537 if (vnnmap != NULL) {
2538 talloc_free(vnnmap);
2542 /* get the recmaster */
2543 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2544 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2548 /* get recovery mode */
2549 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2550 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2554 /* get the current generation number */
2555 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2557 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2561 if ((recmode == CTDB_RECOVERY_NORMAL)
2562 && (vnnmap->generation != 1)){
2563 return vnnmap->generation;
2570 ban a node from the cluster
2572 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2575 struct ctdb_node_map *nodemap=NULL;
2576 struct ctdb_ban_time bantime;
2582 /* verify the node exists */
2583 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2585 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2589 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2590 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2594 bantime.pnn = options.pnn;
2595 bantime.time = strtoul(argv[0], NULL, 0);
2597 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2599 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2603 ret = control_ipreallocate(ctdb, argc, argv);
2605 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2614 unban a node from the cluster
2616 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2619 struct ctdb_node_map *nodemap=NULL;
2620 struct ctdb_ban_time bantime;
2622 /* verify the node exists */
2623 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2625 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2629 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2630 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2634 bantime.pnn = options.pnn;
2637 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2639 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2643 ret = control_ipreallocate(ctdb, argc, argv);
2645 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2654 show ban information for a node
2656 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2659 struct ctdb_node_map *nodemap=NULL;
2660 struct ctdb_ban_time *bantime;
2662 /* verify the node exists */
2663 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2665 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2669 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2671 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2675 if (bantime->time == 0) {
2676 printf("Node %u is not banned\n", bantime->pnn);
2678 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2687 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2691 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2693 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2703 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2706 uint32_t generation, next_generation;
2708 /* record the current generation number */
2709 generation = get_generation(ctdb);
2711 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2713 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2717 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2719 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2723 /* wait until we are in a new generation */
2725 next_generation = get_generation(ctdb);
2726 if (next_generation != generation) {
2737 display monitoring mode of a remote node
2739 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2744 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2746 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2749 if (!options.machinereadable){
2750 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2753 printf(":%d:\n",monmode);
2760 display capabilities of a remote node
2762 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2764 uint32_t capabilities;
2767 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2769 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2773 if (!options.machinereadable){
2774 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2775 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2776 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2777 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2779 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2780 printf(":%d:%d:%d:%d:\n",
2781 !!(capabilities&CTDB_CAP_RECMASTER),
2782 !!(capabilities&CTDB_CAP_LMASTER),
2783 !!(capabilities&CTDB_CAP_LVS),
2784 !!(capabilities&CTDB_CAP_NATGW));
2790 display lvs configuration
2792 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2794 uint32_t *capabilities;
2795 struct ctdb_node_map *nodemap=NULL;
2797 int healthy_count = 0;
2799 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2801 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2805 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2806 CTDB_NO_MEMORY(ctdb, capabilities);
2808 /* collect capabilities for all connected nodes */
2809 for (i=0; i<nodemap->num; i++) {
2810 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2813 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2817 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2819 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2823 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2827 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2832 /* Print all LVS nodes */
2833 for (i=0; i<nodemap->num; i++) {
2834 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2837 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2840 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2844 if (healthy_count != 0) {
2845 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2850 printf("%d:%s\n", i,
2851 ctdb_addr_to_str(&nodemap->nodes[i].addr));
2858 display who is the lvs master
2860 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2862 uint32_t *capabilities;
2863 struct ctdb_node_map *nodemap=NULL;
2865 int healthy_count = 0;
2867 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2869 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2873 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2874 CTDB_NO_MEMORY(ctdb, capabilities);
2876 /* collect capabilities for all connected nodes */
2877 for (i=0; i<nodemap->num; i++) {
2878 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2881 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2885 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2887 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2891 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2895 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2900 /* find and show the lvsmaster */
2901 for (i=0; i<nodemap->num; i++) {
2902 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2905 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2908 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2912 if (healthy_count != 0) {
2913 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2918 if (options.machinereadable){
2921 printf("Node %d is LVS master\n", i);
2926 printf("There is no LVS master\n");
2931 disable monitoring on a node
2933 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2938 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2940 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2943 printf("Monitoring mode:%s\n","DISABLED");
2949 enable monitoring on a node
2951 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2956 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
2958 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
2961 printf("Monitoring mode:%s\n","ACTIVE");
2967 display remote list of keys/data for a db
2969 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
2971 const char *db_name;
2972 struct ctdb_db_context *ctdb_db;
2983 if (db_exists(ctdb, db_name, &persistent)) {
2984 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2988 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
2990 if (ctdb_db == NULL) {
2991 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2995 /* traverse and dump the cluster tdb */
2996 ret = ctdb_dump_db(ctdb_db, stdout);
2998 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
2999 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3000 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3004 talloc_free(ctdb_db);
3006 printf("Dumped %d records\n", ret);
3010 struct cattdb_data {
3011 struct ctdb_context *ctdb;
3015 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3017 struct cattdb_data *d = private_data;
3021 return ctdb_dumpdb_record(d->ctdb, key, data, stdout);
3025 cat the local tdb database using same format as catdb
3027 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3029 const char *db_name;
3030 struct ctdb_db_context *ctdb_db;
3031 struct cattdb_data d;
3041 if (db_exists(ctdb, db_name, &persistent)) {
3042 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3046 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, false, 0);
3048 if (ctdb_db == NULL) {
3049 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3053 /* traverse the local tdb */
3056 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3057 printf("Failed to cattdb data\n");
3060 talloc_free(ctdb_db);
3062 printf("Dumped %d records\n", d.count);
3067 display the content of a database key
3069 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3071 const char *db_name;
3072 struct ctdb_db_context *ctdb_db;
3073 struct ctdb_record_handle *h;
3074 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3085 if (db_exists(ctdb, db_name, &persistent)) {
3086 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3090 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3092 if (ctdb_db == NULL) {
3093 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3097 key.dptr = discard_const(argv[1]);
3098 key.dsize = strlen((char *)key.dptr);
3100 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3102 printf("Failed to fetch record '%s' on node %d\n",
3103 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3104 talloc_free(tmp_ctx);
3108 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3110 talloc_free(ctdb_db);
3111 talloc_free(tmp_ctx);
3116 display the content of a database key
3118 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3120 const char *db_name;
3121 struct ctdb_db_context *ctdb_db;
3122 struct ctdb_record_handle *h;
3123 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3134 if (db_exists(ctdb, db_name, &persistent)) {
3135 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3139 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3141 if (ctdb_db == NULL) {
3142 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3146 key.dptr = discard_const(argv[1]);
3147 key.dsize = strlen((char *)key.dptr);
3149 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3151 printf("Failed to fetch record '%s' on node %d\n",
3152 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3153 talloc_free(tmp_ctx);
3157 data.dptr = discard_const(argv[2]);
3158 data.dsize = strlen((char *)data.dptr);
3160 if (ctdb_record_store(h, data) != 0) {
3161 printf("Failed to store record\n");
3165 talloc_free(ctdb_db);
3166 talloc_free(tmp_ctx);
3171 fetch a record from a persistent database
3173 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3175 const char *db_name;
3176 struct ctdb_db_context *ctdb_db;
3177 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3178 struct ctdb_transaction_handle *h;
3184 talloc_free(tmp_ctx);
3191 if (db_exists(ctdb, db_name, &persistent)) {
3192 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3193 talloc_free(tmp_ctx);
3198 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3199 talloc_free(tmp_ctx);
3203 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3205 if (ctdb_db == NULL) {
3206 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3207 talloc_free(tmp_ctx);
3211 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3213 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3214 talloc_free(tmp_ctx);
3218 key.dptr = discard_const(argv[1]);
3219 key.dsize = strlen(argv[1]);
3220 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3222 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3223 talloc_free(tmp_ctx);
3227 if (data.dsize == 0 || data.dptr == NULL) {
3228 DEBUG(DEBUG_ERR,("Record is empty\n"));
3229 talloc_free(tmp_ctx);
3234 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3236 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3237 talloc_free(tmp_ctx);
3240 write(fd, data.dptr, data.dsize);
3243 write(1, data.dptr, data.dsize);
3246 /* abort the transaction */
3250 talloc_free(tmp_ctx);
3255 fetch a record from a tdb-file
3257 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3259 const char *tdb_file;
3270 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3272 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
3276 key.dptr = discard_const(argv[1]);
3277 key.dsize = strlen(argv[1]);
3278 data = tdb_fetch(tdb, key);
3279 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3280 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
3288 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3290 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3293 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3296 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3303 write a record to a persistent database
3305 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3307 const char *db_name;
3308 struct ctdb_db_context *ctdb_db;
3309 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3310 struct ctdb_transaction_handle *h;
3316 talloc_free(tmp_ctx);
3320 fd = open(argv[2], O_RDONLY);
3322 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3323 talloc_free(tmp_ctx);
3327 ret = fstat(fd, &st);
3329 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3331 talloc_free(tmp_ctx);
3335 if (!S_ISREG(st.st_mode)) {
3336 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3338 talloc_free(tmp_ctx);
3342 data.dsize = st.st_size;
3343 if (data.dsize == 0) {
3346 data.dptr = talloc_size(tmp_ctx, data.dsize);
3347 if (data.dptr == NULL) {
3348 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3350 talloc_free(tmp_ctx);
3353 ret = read(fd, data.dptr, data.dsize);
3354 if (ret != data.dsize) {
3355 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3357 talloc_free(tmp_ctx);
3366 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3367 if (ctdb_db == NULL) {
3368 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3369 talloc_free(tmp_ctx);
3373 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3375 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3376 talloc_free(tmp_ctx);
3380 key.dptr = discard_const(argv[1]);
3381 key.dsize = strlen(argv[1]);
3382 ret = ctdb_transaction_store(h, key, data);
3384 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3385 talloc_free(tmp_ctx);
3389 ret = ctdb_transaction_commit(h);
3391 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3392 talloc_free(tmp_ctx);
3397 talloc_free(tmp_ctx);
3402 check if a service is bound to a port or not
3404 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3409 struct sockaddr_in sin;
3412 printf("Use: ctdb chktcport <port>\n");
3416 port = atoi(argv[0]);
3418 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3420 printf("Failed to open local socket\n");
3424 v = fcntl(s, F_GETFL, 0);
3425 fcntl(s, F_SETFL, v | O_NONBLOCK);
3427 bzero(&sin, sizeof(sin));
3428 sin.sin_family = PF_INET;
3429 sin.sin_port = htons(port);
3430 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3433 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3442 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3443 TDB_DATA data, void *private_data)
3445 DEBUG(DEBUG_ERR,("Log data received\n"));
3446 if (data.dsize > 0) {
3447 printf("%s", data.dptr);
3454 display a list of log messages from the in memory ringbuffer
3456 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3460 struct ctdb_get_log_addr log_addr;
3462 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3467 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3468 talloc_free(tmp_ctx);
3472 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3473 log_addr.srvid = getpid();
3474 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3475 log_addr.level = get_debug_by_desc(argv[0]);
3477 log_addr.level = strtol(argv[0], NULL, 0);
3481 data.dptr = (unsigned char *)&log_addr;
3482 data.dsize = sizeof(log_addr);
3484 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3486 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3489 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3491 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3492 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3493 if (ret != 0 || res != 0) {
3494 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3495 talloc_free(tmp_ctx);
3500 tv = timeval_current();
3501 /* this loop will terminate when we have received the reply */
3502 while (timeval_elapsed(&tv) < 3.0) {
3503 event_loop_once(ctdb->ev);
3506 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3508 talloc_free(tmp_ctx);
3513 clear the in memory log area
3515 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3520 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3522 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3523 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3524 if (ret != 0 || res != 0) {
3525 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3526 talloc_free(tmp_ctx);
3530 talloc_free(tmp_ctx);
3537 display a list of the databases on a remote ctdb
3539 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3542 struct ctdb_dbid_map *dbmap=NULL;
3544 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3546 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3550 if(options.machinereadable){
3551 printf(":ID:Name:Path:Persistent:Unhealthy:ReadOnly:\n");
3552 for(i=0;i<dbmap->num;i++){
3559 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3560 dbmap->dbs[i].dbid, ctdb, &path);
3561 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3562 dbmap->dbs[i].dbid, ctdb, &name);
3563 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3564 dbmap->dbs[i].dbid, ctdb, &health);
3565 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3566 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3567 printf(":0x%08X:%s:%s:%d:%d:%d:\n",
3568 dbmap->dbs[i].dbid, name, path,
3569 !!(persistent), !!(health), !!(readonly));
3574 printf("Number of databases:%d\n", dbmap->num);
3575 for(i=0;i<dbmap->num;i++){
3582 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3583 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3584 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3585 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3586 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3587 printf("dbid:0x%08x name:%s path:%s%s%s%s\n",
3588 dbmap->dbs[i].dbid, name, path,
3589 persistent?" PERSISTENT":"",
3590 readonly?" READONLY":"",
3591 health?" UNHEALTHY":"");
3598 display the status of a database on a remote ctdb
3600 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3603 struct ctdb_dbid_map *dbmap=NULL;
3604 const char *db_name;
3612 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3614 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3618 for(i=0;i<dbmap->num;i++){
3625 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3626 if (strcmp(name, db_name) != 0) {
3630 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3631 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3632 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3633 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3634 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nREADONLY: %s\nHEALTH: %s\n",
3635 dbmap->dbs[i].dbid, name, path,
3636 persistent?"yes":"no",
3637 readonly?"yes":"no",
3638 health?health:"OK");
3642 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3647 check if the local node is recmaster or not
3648 it will return 1 if this node is the recmaster and 0 if it is not
3649 or if the local ctdb daemon could not be contacted
3651 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3653 uint32_t mypnn, recmaster;
3655 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3657 printf("Failed to get pnn of node\n");
3661 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
3662 printf("Failed to get the recmaster\n");
3666 if (recmaster != mypnn) {
3667 printf("this node is not the recmaster\n");
3671 printf("this node is the recmaster\n");
3678 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3681 struct timeval tv = timeval_current();
3682 ret = ctdb_ctrl_ping(ctdb, options.pnn);
3684 printf("Unable to get ping response from node %u\n", options.pnn);
3687 printf("response from %u time=%.6f sec (%d clients)\n",
3688 options.pnn, timeval_elapsed(&tv), ret);
3697 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3708 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3710 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3714 printf("%-19s = %u\n", name, value);
3721 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3732 value = strtoul(argv[1], NULL, 0);
3734 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3736 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3745 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3751 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3753 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3757 for (i=0;i<count;i++) {
3758 control_getvar(ctdb, 1, &list[i]);
3767 display debug level on a node
3769 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3774 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3776 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3779 if (options.machinereadable){
3780 printf(":Name:Level:\n");
3781 printf(":%s:%d:\n",get_debug_by_level(level),level);
3783 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3790 display reclock file of a node
3792 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3795 const char *reclock;
3797 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3799 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3802 if (options.machinereadable){
3803 if (reclock != NULL) {
3804 printf("%s", reclock);
3807 if (reclock == NULL) {
3808 printf("No reclock file used.\n");
3810 printf("Reclock file:%s\n", reclock);
3818 set the reclock file of a node
3820 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3823 const char *reclock;
3827 } else if (argc == 1) {
3833 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3835 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3842 set the natgw state on/off
3844 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3847 uint32_t natgwstate;
3853 if (!strcmp(argv[0], "on")) {
3855 } else if (!strcmp(argv[0], "off")) {
3861 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3863 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3871 set the lmaster role on/off
3873 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3876 uint32_t lmasterrole;
3882 if (!strcmp(argv[0], "on")) {
3884 } else if (!strcmp(argv[0], "off")) {
3890 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3892 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3900 set the recmaster role on/off
3902 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3905 uint32_t recmasterrole;
3911 if (!strcmp(argv[0], "on")) {
3913 } else if (!strcmp(argv[0], "off")) {
3919 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3921 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3929 set debug level on a node or all nodes
3931 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3937 printf("You must specify the debug level. Valid levels are:\n");
3938 for (i=0; debug_levels[i].description != NULL; i++) {
3939 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3945 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3946 level = get_debug_by_desc(argv[0]);
3948 level = strtol(argv[0], NULL, 0);
3951 for (i=0; debug_levels[i].description != NULL; i++) {
3952 if (level == debug_levels[i].level) {
3956 if (debug_levels[i].description == NULL) {
3957 printf("Invalid debug level, must be one of\n");
3958 for (i=0; debug_levels[i].description != NULL; i++) {
3959 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3964 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
3966 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
3975 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
3981 priority = strtol(argv[0], NULL, 0);
3985 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
3987 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
3989 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
3996 attach to a database
3998 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4000 const char *db_name;
4001 struct ctdb_db_context *ctdb_db;
4002 bool persistent = false;
4012 if (strcmp(argv[1], "persistent") != 0) {
4018 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4019 if (ctdb_db == NULL) {
4020 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4030 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4032 struct ctdb_db_priority db_prio;
4039 db_prio.db_id = strtoul(argv[0], NULL, 0);
4040 db_prio.priority = strtoul(argv[1], NULL, 0);
4042 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4044 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4054 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4056 uint32_t db_id, priority;
4063 db_id = strtoul(argv[0], NULL, 0);
4065 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4067 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4071 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4077 set the readonly capability for a database
4079 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4088 db_id = strtoul(argv[0], NULL, 0);
4090 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4092 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4100 run an eventscript on a node
4102 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4108 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4111 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4115 data.dptr = (unsigned char *)discard_const(argv[0]);
4116 data.dsize = strlen((char *)data.dptr) + 1;
4118 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4120 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4121 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4122 if (ret != 0 || res != 0) {
4123 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4124 talloc_free(tmp_ctx);
4127 talloc_free(tmp_ctx);
4131 #define DB_VERSION 1
4132 #define MAX_DB_NAME 64
4133 struct db_file_header {
4134 unsigned long version;
4136 unsigned long persistent;
4138 const char name[MAX_DB_NAME];
4141 struct backup_data {
4142 struct ctdb_marshall_buffer *records;
4145 bool traverse_error;
4148 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4150 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4151 struct ctdb_rec_data *rec;
4153 /* add the record */
4154 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4156 bd->traverse_error = true;
4157 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4160 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4161 if (bd->records == NULL) {
4162 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4163 bd->traverse_error = true;
4166 bd->records->count++;
4167 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4168 bd->len += rec->length;
4176 * backup a database to a file
4178 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4181 struct ctdb_dbid_map *dbmap=NULL;
4182 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4183 struct db_file_header dbhdr;
4184 struct ctdb_db_context *ctdb_db;
4185 struct backup_data *bd;
4188 const char *reason = NULL;
4191 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4195 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4197 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4201 for(i=0;i<dbmap->num;i++){
4204 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4205 if(!strcmp(argv[0], name)){
4206 talloc_free(discard_const(name));
4209 talloc_free(discard_const(name));
4211 if (i == dbmap->num) {
4212 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4213 talloc_free(tmp_ctx);
4217 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4218 dbmap->dbs[i].dbid, tmp_ctx, &reason);
4220 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4222 talloc_free(tmp_ctx);
4226 uint32_t allow_unhealthy = 0;
4228 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4229 "AllowUnhealthyDBRead",
4232 if (allow_unhealthy != 1) {
4233 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4236 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4238 talloc_free(tmp_ctx);
4242 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4244 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4245 "tunnable AllowUnhealthyDBRead = %u\n",
4249 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4250 if (ctdb_db == NULL) {
4251 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4252 talloc_free(tmp_ctx);
4257 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4259 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4260 talloc_free(tmp_ctx);
4265 bd = talloc_zero(tmp_ctx, struct backup_data);
4267 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4268 talloc_free(tmp_ctx);
4272 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4273 if (bd->records == NULL) {
4274 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4275 talloc_free(tmp_ctx);
4279 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4280 bd->records->db_id = ctdb_db->db_id;
4281 /* traverse the database collecting all records */
4282 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4283 bd->traverse_error) {
4284 DEBUG(DEBUG_ERR,("Traverse error\n"));
4285 talloc_free(tmp_ctx);
4289 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4292 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4294 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4295 talloc_free(tmp_ctx);
4299 dbhdr.version = DB_VERSION;
4300 dbhdr.timestamp = time(NULL);
4301 dbhdr.persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4302 dbhdr.size = bd->len;
4303 if (strlen(argv[0]) >= MAX_DB_NAME) {
4304 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4307 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4308 ret = write(fh, &dbhdr, sizeof(dbhdr));
4310 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4313 ret = write(fh, bd->records, bd->len);
4315 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4324 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4328 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4330 talloc_free(tmp_ctx);
4335 * restore a database from a file
4337 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4340 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4343 struct db_file_header dbhdr;
4344 struct ctdb_db_context *ctdb_db;
4345 struct ctdb_node_map *nodemap=NULL;
4346 struct ctdb_vnn_map *vnnmap=NULL;
4348 struct ctdb_control_wipe_database w;
4350 uint32_t generation;
4355 if (argc < 1 || argc > 2) {
4356 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4360 fh = open(argv[0], O_RDONLY);
4362 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4363 talloc_free(tmp_ctx);
4367 read(fh, &dbhdr, sizeof(dbhdr));
4368 if (dbhdr.version != DB_VERSION) {
4369 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4370 talloc_free(tmp_ctx);
4374 dbname = discard_const(dbhdr.name);
4376 dbname = discard_const(argv[1]);
4379 outdata.dsize = dbhdr.size;
4380 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4381 if (outdata.dptr == NULL) {
4382 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4384 talloc_free(tmp_ctx);
4387 read(fh, outdata.dptr, outdata.dsize);
4390 tm = localtime(&dbhdr.timestamp);
4391 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4392 printf("Restoring database '%s' from backup @ %s\n",
4396 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4397 if (ctdb_db == NULL) {
4398 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4399 talloc_free(tmp_ctx);
4403 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4405 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4406 talloc_free(tmp_ctx);
4411 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4413 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4414 talloc_free(tmp_ctx);
4418 /* freeze all nodes */
4419 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4420 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4421 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4427 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4428 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4429 talloc_free(tmp_ctx);
4434 generation = vnnmap->generation;
4435 data.dptr = (void *)&generation;
4436 data.dsize = sizeof(generation);
4438 /* start a cluster wide transaction */
4439 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4440 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4442 TIMELIMIT(), false, data,
4445 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4450 w.db_id = ctdb_db->db_id;
4451 w.transaction_id = generation;
4453 data.dptr = (void *)&w;
4454 data.dsize = sizeof(w);
4456 /* wipe all the remote databases. */
4457 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4458 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4460 TIMELIMIT(), false, data,
4463 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4464 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4465 talloc_free(tmp_ctx);
4469 /* push the database */
4470 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4471 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4473 TIMELIMIT(), false, outdata,
4476 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4477 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4478 talloc_free(tmp_ctx);
4482 data.dptr = (void *)&ctdb_db->db_id;
4483 data.dsize = sizeof(ctdb_db->db_id);
4485 /* mark the database as healthy */
4486 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4487 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4489 TIMELIMIT(), false, data,
4492 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4493 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4494 talloc_free(tmp_ctx);
4498 data.dptr = (void *)&generation;
4499 data.dsize = sizeof(generation);
4501 /* commit all the changes */
4502 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4504 TIMELIMIT(), false, data,
4507 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4508 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4509 talloc_free(tmp_ctx);
4514 /* thaw all nodes */
4515 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4516 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4522 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4523 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4524 talloc_free(tmp_ctx);
4529 talloc_free(tmp_ctx);
4534 * dump a database backup from a file
4536 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4538 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4540 struct db_file_header dbhdr;
4544 struct ctdb_rec_data *rec = NULL;
4545 struct ctdb_marshall_buffer *m;
4548 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4552 fh = open(argv[0], O_RDONLY);
4554 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4555 talloc_free(tmp_ctx);
4559 read(fh, &dbhdr, sizeof(dbhdr));
4560 if (dbhdr.version != DB_VERSION) {
4561 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4562 talloc_free(tmp_ctx);
4566 outdata.dsize = dbhdr.size;
4567 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4568 if (outdata.dptr == NULL) {
4569 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4571 talloc_free(tmp_ctx);
4574 read(fh, outdata.dptr, outdata.dsize);
4576 m = (struct ctdb_marshall_buffer *)outdata.dptr;
4578 tm = localtime(&dbhdr.timestamp);
4579 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4580 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4581 dbhdr.name, m->db_id, tbuf);
4583 for (i=0; i < m->count; i++) {
4587 /* we do not want the header splitted, so we pass NULL*/
4588 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4591 ctdb_dumpdb_record(ctdb, key, data, stdout);
4594 printf("Dumped %d records\n", i);
4595 talloc_free(tmp_ctx);
4600 * wipe a database from a file
4602 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4606 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4608 struct ctdb_db_context *ctdb_db;
4609 struct ctdb_node_map *nodemap = NULL;
4610 struct ctdb_vnn_map *vnnmap = NULL;
4612 struct ctdb_control_wipe_database w;
4614 uint32_t generation;
4615 struct ctdb_dbid_map *dbmap = NULL;
4618 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4622 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4625 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4630 for(i=0;i<dbmap->num;i++){
4633 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4634 dbmap->dbs[i].dbid, tmp_ctx, &name);
4635 if(!strcmp(argv[0], name)){
4636 talloc_free(discard_const(name));
4639 talloc_free(discard_const(name));
4641 if (i == dbmap->num) {
4642 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4644 talloc_free(tmp_ctx);
4648 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4649 if (ctdb_db == NULL) {
4650 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4652 talloc_free(tmp_ctx);
4656 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4659 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4661 talloc_free(tmp_ctx);
4665 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4668 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4670 talloc_free(tmp_ctx);
4674 /* freeze all nodes */
4675 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4676 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4677 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4684 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4685 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4686 CTDB_RECOVERY_ACTIVE);
4687 talloc_free(tmp_ctx);
4692 generation = vnnmap->generation;
4693 data.dptr = (void *)&generation;
4694 data.dsize = sizeof(generation);
4696 /* start a cluster wide transaction */
4697 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4698 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4700 TIMELIMIT(), false, data,
4704 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4705 "transactions.\n"));
4709 w.db_id = ctdb_db->db_id;
4710 w.transaction_id = generation;
4712 data.dptr = (void *)&w;
4713 data.dsize = sizeof(w);
4715 /* wipe all the remote databases. */
4716 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4717 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4719 TIMELIMIT(), false, data,
4722 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4723 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4724 talloc_free(tmp_ctx);
4728 data.dptr = (void *)&ctdb_db->db_id;
4729 data.dsize = sizeof(ctdb_db->db_id);
4731 /* mark the database as healthy */
4732 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4733 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4735 TIMELIMIT(), false, data,
4738 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4739 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4740 talloc_free(tmp_ctx);
4744 data.dptr = (void *)&generation;
4745 data.dsize = sizeof(generation);
4747 /* commit all the changes */
4748 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4750 TIMELIMIT(), false, data,
4753 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4754 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4755 talloc_free(tmp_ctx);
4759 /* thaw all nodes */
4760 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4761 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4767 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4768 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4769 talloc_free(tmp_ctx);
4773 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
4775 talloc_free(tmp_ctx);
4782 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4788 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4789 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4790 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4791 if (ret != 0 || res != 0) {
4792 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4793 talloc_free(tmp_ctx);
4796 write(1, data.dptr, data.dsize);
4797 talloc_free(tmp_ctx);
4802 handler for memory dumps
4804 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
4805 TDB_DATA data, void *private_data)
4807 write(1, data.dptr, data.dsize);
4812 dump memory usage on the recovery daemon
4814 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4818 struct rd_memdump_reply rd;
4820 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4822 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4825 rd.srvid = getpid();
4827 /* register a message port for receiveing the reply so that we
4828 can receive the reply
4830 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4833 data.dptr = (uint8_t *)&rd;
4834 data.dsize = sizeof(rd);
4836 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4838 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4842 /* this loop will terminate when we have received the reply */
4844 event_loop_once(ctdb->ev);
4851 send a message to a srvid
4853 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4855 unsigned long srvid;
4863 srvid = strtoul(argv[0], NULL, 0);
4865 data.dptr = (uint8_t *)discard_const(argv[1]);
4866 data.dsize= strlen(argv[1]);
4868 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4870 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4878 handler for msglisten
4880 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
4881 TDB_DATA data, void *private_data)
4885 printf("Message received: ");
4886 for (i=0;i<data.dsize;i++) {
4887 printf("%c", data.dptr[i]);
4893 listen for messages on a messageport
4895 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
4901 /* register a message port and listen for messages
4903 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
4904 printf("Listening for messages on srvid:%d\n", (int)srvid);
4907 event_loop_once(ctdb->ev);
4914 list all nodes in the cluster
4915 we parse the nodes file directly
4917 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
4919 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4920 struct pnn_node *pnn_nodes;
4921 struct pnn_node *pnn_node;
4923 pnn_nodes = read_nodes_file(mem_ctx);
4924 if (pnn_nodes == NULL) {
4925 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
4926 talloc_free(mem_ctx);
4930 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
4931 ctdb_sock_addr addr;
4932 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
4933 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
4934 talloc_free(mem_ctx);
4937 if (options.machinereadable){
4938 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
4940 printf("%s\n", pnn_node->addr);
4943 talloc_free(mem_ctx);
4949 reload the nodes file on the local node
4951 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
4955 struct ctdb_node_map *nodemap=NULL;
4957 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4959 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
4963 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
4965 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4969 /* reload the nodes file on all remote nodes */
4970 for (i=0;i<nodemap->num;i++) {
4971 if (nodemap->nodes[i].pnn == mypnn) {
4974 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
4975 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
4976 nodemap->nodes[i].pnn);
4978 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
4982 /* reload the nodes file on the local node */
4983 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
4984 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
4986 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
4989 /* initiate a recovery */
4990 control_recover(ctdb, argc, argv);
4996 static const struct {
4998 int (*fn)(struct ctdb_context *, int, const char **);
5000 bool without_daemon; /* can be run without daemon running ? */
5003 } ctdb_commands[] = {
5005 { "version", control_version, true, false, "show version of ctdb" },
5007 { "status", control_status, true, false, "show node status" },
5008 { "uptime", control_uptime, true, false, "show node uptime" },
5009 { "ping", control_ping, true, false, "ping all nodes" },
5010 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5011 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5012 { "listvars", control_listvars, true, false, "list tunable variables"},
5013 { "statistics", control_statistics, false, false, "show statistics" },
5014 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5015 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5016 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5017 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5018 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5019 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5020 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5021 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5022 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
5023 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname>"},
5024 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname>"},
5025 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5026 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5027 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5028 { "lvs", control_lvs, true, false, "show lvs configuration" },
5029 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5030 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5031 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5032 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5033 { "getdebug", control_getdebug, true, false, "get debug level" },
5034 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
5035 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
5036 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5037 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5038 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5039 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5040 { "disable", control_disable, true, false, "disable a nodes public IP" },
5041 { "enable", control_enable, true, false, "enable a nodes public IP" },
5042 { "stop", control_stop, true, false, "stop a node" },
5043 { "continue", control_continue, true, false, "re-start a stopped node" },
5044 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5045 { "unban", control_unban, true, false, "unban a node" },
5046 { "showban", control_showban, true, false, "show ban information"},
5047 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5048 { "recover", control_recover, true, false, "force recovery" },
5049 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
5050 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5051 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5052 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5053 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5054 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5055 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5056 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5057 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5059 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5061 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5062 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5063 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5064 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5065 { "vacuum", ctdb_vacuum, false, false, "vacuum the databases of empty records", "[max_records]"},
5066 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5067 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5068 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5069 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5070 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5071 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5072 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5073 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
5074 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5075 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5076 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
5077 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
5078 { "scriptstatus", control_scriptstatus, false, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5079 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5080 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5081 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5082 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5083 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5084 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5085 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5086 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5087 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5088 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
5089 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
5090 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbid>"},
5091 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5092 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5093 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5094 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
5095 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
5096 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
5097 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5098 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5099 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5105 static void usage(void)
5109 "Usage: ctdb [options] <control>\n" \
5111 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5112 " -Y generate machinereadable output\n"
5113 " -v generate verbose output\n"
5114 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5115 printf("Controls:\n");
5116 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5117 printf(" %-15s %-27s %s\n",
5118 ctdb_commands[i].name,
5119 ctdb_commands[i].args?ctdb_commands[i].args:"",
5120 ctdb_commands[i].msg);
5126 static void ctdb_alarm(int sig)
5128 printf("Maximum runtime exceeded - exiting\n");
5135 int main(int argc, const char *argv[])
5137 struct ctdb_context *ctdb;
5138 char *nodestring = NULL;
5139 struct poptOption popt_options[] = {
5142 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5143 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5144 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
5145 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5146 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5150 const char **extra_argv;
5154 struct event_context *ev;
5155 const char *control;
5159 /* set some defaults */
5160 options.maxruntime = 0;
5161 options.timelimit = 3;
5162 options.pnn = CTDB_CURRENT_NODE;
5164 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5166 while ((opt = poptGetNextOpt(pc)) != -1) {
5169 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5170 poptBadOption(pc, 0), poptStrerror(opt)));
5175 /* setup the remaining options for the main program to use */
5176 extra_argv = poptGetArgs(pc);
5179 while (extra_argv[extra_argc]) extra_argc++;
5182 if (extra_argc < 1) {
5186 if (options.maxruntime == 0) {
5187 const char *ctdb_timeout;
5188 ctdb_timeout = getenv("CTDB_TIMEOUT");
5189 if (ctdb_timeout != NULL) {
5190 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5192 /* default timeout is 120 seconds */
5193 options.maxruntime = 120;
5197 signal(SIGALRM, ctdb_alarm);
5198 alarm(options.maxruntime);
5200 /* setup the node number to contact */
5201 if (nodestring != NULL) {
5202 if (strcmp(nodestring, "all") == 0) {
5203 options.pnn = CTDB_BROADCAST_ALL;
5205 options.pnn = strtoul(nodestring, NULL, 0);
5209 control = extra_argv[0];
5211 ev = event_context_init(NULL);
5213 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5216 tevent_loop_allow_nesting(ev);
5218 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5219 if (strcmp(control, ctdb_commands[i].name) == 0) {
5222 if (ctdb_commands[i].without_daemon == true) {
5226 /* initialise ctdb */
5227 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5229 if (ctdb_commands[i].without_daemon == false) {
5230 const char *socket_name;
5233 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5237 /* initialize a libctdb connection as well */
5238 socket_name = ctdb_get_socketname(ctdb);
5239 ctdb_connection = ctdb_connect(socket_name,
5240 ctdb_log_file, stderr);
5241 if (ctdb_connection == NULL) {
5242 fprintf(stderr, "Failed to connect to daemon from libctdb\n");
5246 /* verify the node exists */
5249 if (options.pnn == CTDB_CURRENT_NODE) {
5251 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
5259 if (ctdb_commands[i].auto_all &&
5260 options.pnn == CTDB_BROADCAST_ALL) {
5265 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
5266 CTDB_NO_MEMORY(ctdb, nodes);
5268 for (j=0;j<num_nodes;j++) {
5269 options.pnn = nodes[j];
5270 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5274 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5280 if (i == ARRAY_SIZE(ctdb_commands)) {
5281 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));