4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/tevent/tevent.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/network.h"
26 #include "system/locale.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 struct ctdb_connection *ctdb_connection;
41 static void usage(void);
50 int printemptyrecords;
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
61 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
64 #define XSTR(x) STR(x)
65 printf("CTDB version: %s\n", XSTR(CTDB_VERS));
70 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
71 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
72 "Out of memory in " __location__ )); \
76 /* Pretty print the flags to a static buffer in human-readable format.
77 * This never returns NULL!
79 static const char *pretty_print_flags(uint32_t flags)
86 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
87 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
88 { NODE_FLAGS_BANNED, "BANNED" },
89 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
90 { NODE_FLAGS_DELETED, "DELETED" },
91 { NODE_FLAGS_STOPPED, "STOPPED" },
92 { NODE_FLAGS_INACTIVE, "INACTIVE" },
94 static char flags_str[512]; /* Big enough to contain all flag names */
97 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
98 if (flags & flag_names[j].flag) {
99 if (flags_str[0] == '\0') {
100 (void) strcpy(flags_str, flag_names[j].name);
102 (void) strcat(flags_str, "|");
103 (void) strcat(flags_str, flag_names[j].name);
107 if (flags_str[0] == '\0') {
108 (void) strcpy(flags_str, "OK");
114 static int h2i(char h)
116 if (h >= 'a' && h <= 'f') return h - 'a' + 10;
117 if (h >= 'A' && h <= 'F') return h - 'f' + 10;
121 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
124 TDB_DATA key = {NULL, 0};
128 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
133 key.dptr = talloc_size(mem_ctx, key.dsize);
135 for (i=0; i < len/2; i++) {
136 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
141 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
142 * that are disconnected or deleted. If dd_ok is true those nodes are
143 * included in the output list of nodes. If dd_ok is false, those
144 * nodes are filtered from the "all" case and cause an error if
145 * explicitly specified.
147 static bool parse_nodestring(struct ctdb_context *ctdb,
148 const char * nodestring,
149 uint32_t current_pnn,
156 struct ctdb_node_map *nodemap;
160 if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE, &nodemap)) {
161 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
165 if (nodestring != NULL) {
166 *nodes = talloc_array(ctdb, uint32_t, 0);
167 CTDB_NOMEM_ABORT(*nodes);
171 if (strcmp(nodestring, "all") == 0) {
172 *pnn_mode = CTDB_BROADCAST_ALL;
175 for (i = 0; i < nodemap->num; i++) {
176 if ((nodemap->nodes[i].flags &
177 (NODE_FLAGS_DISCONNECTED |
178 NODE_FLAGS_DELETED)) && !dd_ok) {
181 *nodes = talloc_realloc(ctdb, *nodes,
183 CTDB_NOMEM_ABORT(*nodes);
191 ns = talloc_strdup(ctdb, nodestring);
192 tok = strtok(ns, ",");
193 while (tok != NULL) {
195 i = (uint32_t)strtoul(tok, NULL, 0);
196 if (i >= nodemap->num) {
197 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
200 if ((nodemap->nodes[i].flags &
201 (NODE_FLAGS_DISCONNECTED |
202 NODE_FLAGS_DELETED)) && !dd_ok) {
203 DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
206 if (!ctdb_getpnn(ctdb_connection, i, &pnn)) {
207 DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
211 *nodes = talloc_realloc(ctdb, *nodes,
213 CTDB_NOMEM_ABORT(*nodes);
218 tok = strtok(NULL, ",");
223 *pnn_mode = (*nodes)[0];
225 *pnn_mode = CTDB_MULTICAST;
229 /* default - no nodes specified */
230 *nodes = talloc_array(ctdb, uint32_t, 1);
231 CTDB_NOMEM_ABORT(*nodes);
232 *pnn_mode = CTDB_CURRENT_NODE;
234 if (!ctdb_getpnn(ctdb_connection, current_pnn,
240 ctdb_free_nodemap(nodemap);
246 check if a database exists
248 static int db_exists(struct ctdb_context *ctdb, const char *db_name, bool *persistent)
251 struct ctdb_dbid_map *dbmap=NULL;
253 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
255 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
259 for(i=0;i<dbmap->num;i++){
262 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
263 if (!strcmp(name, db_name)) {
265 *persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
275 see if a process exists
277 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
285 if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
286 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
290 ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
292 printf("%u:%u exists\n", pnn, pid);
294 printf("%u:%u does not exist\n", pnn, pid);
300 display statistics structure
302 static void show_statistics(struct ctdb_statistics *s, int show_header)
304 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
306 const char *prefix=NULL;
308 int tmp, days, hours, minutes, seconds;
313 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
314 STATISTICS_FIELD(num_clients),
315 STATISTICS_FIELD(frozen),
316 STATISTICS_FIELD(recovering),
317 STATISTICS_FIELD(num_recoveries),
318 STATISTICS_FIELD(client_packets_sent),
319 STATISTICS_FIELD(client_packets_recv),
320 STATISTICS_FIELD(node_packets_sent),
321 STATISTICS_FIELD(node_packets_recv),
322 STATISTICS_FIELD(keepalive_packets_sent),
323 STATISTICS_FIELD(keepalive_packets_recv),
324 STATISTICS_FIELD(node.req_call),
325 STATISTICS_FIELD(node.reply_call),
326 STATISTICS_FIELD(node.req_dmaster),
327 STATISTICS_FIELD(node.reply_dmaster),
328 STATISTICS_FIELD(node.reply_error),
329 STATISTICS_FIELD(node.req_message),
330 STATISTICS_FIELD(node.req_control),
331 STATISTICS_FIELD(node.reply_control),
332 STATISTICS_FIELD(client.req_call),
333 STATISTICS_FIELD(client.req_message),
334 STATISTICS_FIELD(client.req_control),
335 STATISTICS_FIELD(timeouts.call),
336 STATISTICS_FIELD(timeouts.control),
337 STATISTICS_FIELD(timeouts.traverse),
338 STATISTICS_FIELD(total_calls),
339 STATISTICS_FIELD(pending_calls),
340 STATISTICS_FIELD(lockwait_calls),
341 STATISTICS_FIELD(pending_lockwait_calls),
342 STATISTICS_FIELD(childwrite_calls),
343 STATISTICS_FIELD(pending_childwrite_calls),
344 STATISTICS_FIELD(memory_used),
345 STATISTICS_FIELD(max_hop_count),
346 STATISTICS_FIELD(total_ro_delegations),
347 STATISTICS_FIELD(total_ro_revokes),
349 tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
358 if (options.machinereadable){
360 printf("CTDB version:");
361 printf("Current time of statistics:");
362 printf("Statistics collected since:");
363 for (i=0;i<ARRAY_SIZE(fields);i++) {
364 printf("%s:", fields[i].name);
366 printf("num_reclock_ctdbd_latency:");
367 printf("min_reclock_ctdbd_latency:");
368 printf("avg_reclock_ctdbd_latency:");
369 printf("max_reclock_ctdbd_latency:");
371 printf("num_reclock_recd_latency:");
372 printf("min_reclock_recd_latency:");
373 printf("avg_reclock_recd_latency:");
374 printf("max_reclock_recd_latency:");
376 printf("num_call_latency:");
377 printf("min_call_latency:");
378 printf("avg_call_latency:");
379 printf("max_call_latency:");
381 printf("num_lockwait_latency:");
382 printf("min_lockwait_latency:");
383 printf("avg_lockwait_latency:");
384 printf("max_lockwait_latency:");
386 printf("num_childwrite_latency:");
387 printf("min_childwrite_latency:");
388 printf("avg_childwrite_latency:");
389 printf("max_childwrite_latency:");
392 printf("%d:", CTDB_VERSION);
393 printf("%d:", (int)s->statistics_current_time.tv_sec);
394 printf("%d:", (int)s->statistics_start_time.tv_sec);
395 for (i=0;i<ARRAY_SIZE(fields);i++) {
396 printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
398 printf("%d:", s->reclock.ctdbd.num);
399 printf("%.6f:", s->reclock.ctdbd.min);
400 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
401 printf("%.6f:", s->reclock.ctdbd.max);
403 printf("%d:", s->reclock.recd.num);
404 printf("%.6f:", s->reclock.recd.min);
405 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
406 printf("%.6f:", s->reclock.recd.max);
408 printf("%d:", s->call_latency.num);
409 printf("%.6f:", s->call_latency.min);
410 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
411 printf("%.6f:", s->call_latency.max);
413 printf("%d:", s->lockwait_latency.num);
414 printf("%.6f:", s->lockwait_latency.min);
415 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
416 printf("%.6f:", s->lockwait_latency.max);
418 printf("%d:", s->childwrite_latency.num);
419 printf("%.6f:", s->childwrite_latency.min);
420 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
421 printf("%.6f:", s->childwrite_latency.max);
424 printf("CTDB version %u\n", CTDB_VERSION);
425 printf("Current time of statistics : %s", ctime(&s->statistics_current_time.tv_sec));
426 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
428 for (i=0;i<ARRAY_SIZE(fields);i++) {
429 if (strchr(fields[i].name, '.')) {
430 preflen = strcspn(fields[i].name, ".")+1;
431 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
432 prefix = fields[i].name;
433 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
438 printf(" %*s%-22s%*s%10u\n",
440 fields[i].name+preflen,
442 *(uint32_t *)(fields[i].offset+(uint8_t *)s));
444 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);
446 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);
448 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);
449 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);
450 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);
453 talloc_free(tmp_ctx);
457 display remote ctdb statistics combined from all nodes
459 static int control_statistics_all(struct ctdb_context *ctdb)
462 struct ctdb_statistics statistics;
466 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
467 CTDB_NO_MEMORY(ctdb, nodes);
469 ZERO_STRUCT(statistics);
471 for (i=0;i<num_nodes;i++) {
472 struct ctdb_statistics s1;
474 uint32_t *v1 = (uint32_t *)&s1;
475 uint32_t *v2 = (uint32_t *)&statistics;
477 offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
478 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
480 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
483 for (j=0;j<num_ints;j++) {
486 statistics.max_hop_count =
487 MAX(statistics.max_hop_count, s1.max_hop_count);
488 statistics.call_latency.max =
489 MAX(statistics.call_latency.max, s1.call_latency.max);
490 statistics.lockwait_latency.max =
491 MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
494 printf("Gathered statistics for %u nodes\n", num_nodes);
495 show_statistics(&statistics, 1);
500 display remote ctdb statistics
502 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
505 struct ctdb_statistics statistics;
507 if (options.pnn == CTDB_BROADCAST_ALL) {
508 return control_statistics_all(ctdb);
511 ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
513 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
516 show_statistics(&statistics, 1);
522 reset remote ctdb statistics
524 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
528 ret = ctdb_statistics_reset(ctdb, options.pnn);
530 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
538 display remote ctdb rolling statistics
540 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
543 struct ctdb_statistics_wire *stats;
544 int i, num_records = -1;
547 num_records = atoi(argv[0]) - 1;
550 ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
552 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
555 for (i=0;i<stats->num;i++) {
556 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
559 show_statistics(&stats->stats[i], i==0);
560 if (i == num_records) {
569 display uptime of remote node
571 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
574 struct ctdb_uptime *uptime = NULL;
575 int tmp, days, hours, minutes, seconds;
577 ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
579 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
583 if (options.machinereadable){
584 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
585 printf(":%u:%u:%u:%lf\n",
586 (unsigned int)uptime->current_time.tv_sec,
587 (unsigned int)uptime->ctdbd_start_time.tv_sec,
588 (unsigned int)uptime->last_recovery_finished.tv_sec,
589 timeval_delta(&uptime->last_recovery_finished,
590 &uptime->last_recovery_started)
595 printf("Current time of node : %s", ctime(&uptime->current_time.tv_sec));
597 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
605 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
607 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
615 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
617 printf("Duration of last recovery/failover: %lf seconds\n",
618 timeval_delta(&uptime->last_recovery_finished,
619 &uptime->last_recovery_started));
625 show the PNN of the current node
627 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
632 ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
634 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
638 printf("PNN:%d\n", mypnn);
644 struct pnn_node *next;
649 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
651 const char *nodes_list;
655 struct pnn_node *pnn_nodes = NULL;
656 struct pnn_node *pnn_node;
657 struct pnn_node *tmp_node;
659 /* read the nodes file */
660 nodes_list = getenv("CTDB_NODES");
661 if (nodes_list == NULL) {
662 nodes_list = "/etc/ctdb/nodes";
664 lines = file_lines_load(nodes_list, &nlines, mem_ctx);
668 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
671 for (i=0, pnn=0; i<nlines; i++) {
675 /* strip leading spaces */
676 while((*node == ' ') || (*node == '\t')) {
683 if (strcmp(node, "") == 0) {
686 pnn_node = talloc(mem_ctx, struct pnn_node);
687 pnn_node->pnn = pnn++;
688 pnn_node->addr = talloc_strdup(pnn_node, node);
689 pnn_node->next = pnn_nodes;
690 pnn_nodes = pnn_node;
693 /* swap them around so we return them in incrementing order */
694 pnn_node = pnn_nodes;
698 pnn_node = pnn_node->next;
700 tmp_node->next = pnn_nodes;
701 pnn_nodes = tmp_node;
708 show the PNN of the current node
709 discover the pnn by loading the nodes file and try to bind to all
710 addresses one at a time until the ip address is found.
712 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
714 TALLOC_CTX *mem_ctx = talloc_new(NULL);
715 struct pnn_node *pnn_nodes;
716 struct pnn_node *pnn_node;
718 pnn_nodes = read_nodes_file(mem_ctx);
719 if (pnn_nodes == NULL) {
720 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
721 talloc_free(mem_ctx);
725 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
728 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
729 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
730 talloc_free(mem_ctx);
734 if (ctdb_sys_have_ip(&addr)) {
735 printf("PNN:%d\n", pnn_node->pnn);
736 talloc_free(mem_ctx);
741 printf("Failed to detect which PNN this node is\n");
742 talloc_free(mem_ctx);
746 /* Helpers for ctdb status
748 static bool is_partially_online(struct ctdb_node_and_flags *node)
753 if (node->flags == 0) {
754 struct ctdb_ifaces_list *ifaces;
756 if (ctdb_getifaces(ctdb_connection, node->pnn, &ifaces)) {
757 for (j=0; j < ifaces->num; j++) {
758 if (ifaces->ifaces[j].link_state != 0) {
764 ctdb_free_ifaces(ifaces);
771 static int control_status_1_machine(int mypnn, struct ctdb_node_and_flags *node)
773 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
774 ctdb_addr_to_str(&node->addr),
775 !!(node->flags&NODE_FLAGS_DISCONNECTED),
776 !!(node->flags&NODE_FLAGS_BANNED),
777 !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
778 !!(node->flags&NODE_FLAGS_UNHEALTHY),
779 !!(node->flags&NODE_FLAGS_STOPPED),
780 !!(node->flags&NODE_FLAGS_INACTIVE),
781 is_partially_online(node) ? 1 : 0,
782 (node->pnn == mypnn)?'Y':'N');
787 static int control_status_1_human(int mypnn, struct ctdb_node_and_flags *node)
789 printf("pnn:%d %-16s %s%s\n", node->pnn,
790 ctdb_addr_to_str(&node->addr),
791 is_partially_online(node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
792 node->pnn == mypnn?" (THIS NODE)":"");
798 display remote ctdb status
800 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
803 struct ctdb_vnn_map *vnnmap=NULL;
804 struct ctdb_node_map *nodemap=NULL;
805 uint32_t recmode, recmaster, mypnn;
807 if (!ctdb_getpnn(ctdb_connection, options.pnn, &mypnn)) {
811 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
812 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
816 if (options.machinereadable) {
817 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
818 ":Inactive:PartiallyOnline:ThisNode:\n");
819 for (i=0;i<nodemap->num;i++) {
820 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
823 (void) control_status_1_machine(mypnn,
829 printf("Number of nodes:%d\n", nodemap->num);
830 for(i=0;i<nodemap->num;i++){
831 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
834 (void) control_status_1_human(mypnn, &nodemap->nodes[i]);
837 if (!ctdb_getvnnmap(ctdb_connection, options.pnn, &vnnmap)) {
838 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
841 if (vnnmap->generation == INVALID_GENERATION) {
842 printf("Generation:INVALID\n");
844 printf("Generation:%d\n",vnnmap->generation);
846 printf("Size:%d\n",vnnmap->size);
847 for(i=0;i<vnnmap->size;i++){
848 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
850 ctdb_free_vnnmap(vnnmap);
852 if (!ctdb_getrecmode(ctdb_connection, options.pnn, &recmode)) {
853 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
856 printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
858 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
859 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
862 printf("Recovery master:%d\n",recmaster);
867 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
870 struct ctdb_node_map *nodemap=NULL;
872 uint32_t pnn_mode, mypnn;
878 if (!parse_nodestring(ctdb, argc == 1 ? argv[0] : NULL,
879 options.pnn, true, &nodes, &pnn_mode)) {
883 if (options.machinereadable) {
884 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
885 ":Inactive:PartiallyOnline:ThisNode:\n");
886 } else if (pnn_mode == CTDB_BROADCAST_ALL) {
887 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
890 if (!ctdb_getpnn(ctdb_connection, CTDB_CURRENT_NODE, &mypnn)) {
891 DEBUG(DEBUG_ERR, ("Unable to get PNN from local node\n"));
895 if (!ctdb_getnodemap(ctdb_connection, options.pnn, &nodemap)) {
896 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
902 for (i = 0; i < talloc_array_length(nodes); i++) {
903 if (options.machinereadable) {
904 ret |= control_status_1_machine(mypnn,
905 &nodemap->nodes[nodes[i]]);
907 ret |= control_status_1_human(mypnn,
908 &nodemap->nodes[nodes[i]]);
915 struct natgw_node *next;
920 display the list of nodes belonging to this natgw configuration
922 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
925 uint32_t capabilities;
926 const char *natgw_list;
929 struct natgw_node *natgw_nodes = NULL;
930 struct natgw_node *natgw_node;
931 struct ctdb_node_map *nodemap=NULL;
934 /* read the natgw nodes file into a linked list */
935 natgw_list = getenv("NATGW_NODES");
936 if (natgw_list == NULL) {
937 natgw_list = "/etc/ctdb/natgw_nodes";
939 lines = file_lines_load(natgw_list, &nlines, ctdb);
941 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
944 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
947 for (i=0;i<nlines;i++) {
951 /* strip leading spaces */
952 while((*node == ' ') || (*node == '\t')) {
958 if (strcmp(node, "") == 0) {
961 natgw_node = talloc(ctdb, struct natgw_node);
962 natgw_node->addr = talloc_strdup(natgw_node, node);
963 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
964 natgw_node->next = natgw_nodes;
965 natgw_nodes = natgw_node;
968 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
970 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
975 while(i<nodemap->num) {
976 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
977 if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
982 /* this node was not in the natgw so we just remove it from
985 if ((natgw_node == NULL)
986 || (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
989 for (j=i+1; j<nodemap->num; j++) {
990 nodemap->nodes[j-1] = nodemap->nodes[j];
999 /* pick a node to be natgwmaster
1000 * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
1002 for(i=0;i<nodemap->num;i++){
1003 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
1004 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1006 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1009 if (!(capabilities&CTDB_CAP_NATGW)) {
1012 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1016 /* we couldnt find any healthy node, try unhealthy ones */
1017 if (i == nodemap->num) {
1018 for(i=0;i<nodemap->num;i++){
1019 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
1020 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1022 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1025 if (!(capabilities&CTDB_CAP_NATGW)) {
1028 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
1033 /* unless all nodes are STOPPED, when we pick one anyway */
1034 if (i == nodemap->num) {
1035 for(i=0;i<nodemap->num;i++){
1036 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
1037 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
1039 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
1042 if (!(capabilities&CTDB_CAP_NATGW)) {
1045 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
1049 /* or if we still can not find any */
1050 if (i == nodemap->num) {
1051 printf("-1 0.0.0.0\n");
1052 ret = 2; /* matches ENOENT */
1056 /* print the pruned list of nodes belonging to this natgw list */
1057 for(i=0;i<nodemap->num;i++){
1058 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1061 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
1062 ctdb_addr_to_str(&nodemap->nodes[i].addr),
1063 !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
1064 !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
1065 !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
1066 !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
1067 !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
1074 display the status of the scripts for monitoring (or other events)
1076 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1077 enum ctdb_eventscript_call type)
1079 struct ctdb_scripts_wire *script_status;
1082 ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1084 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1088 if (script_status == NULL) {
1089 if (!options.machinereadable) {
1090 printf("%s cycle never run\n",
1091 ctdb_eventscript_call_names[type]);
1096 if (!options.machinereadable) {
1097 printf("%d scripts were executed last %s cycle\n",
1098 script_status->num_scripts,
1099 ctdb_eventscript_call_names[type]);
1101 for (i=0; i<script_status->num_scripts; i++) {
1102 const char *status = NULL;
1104 switch (script_status->scripts[i].status) {
1106 status = "TIMEDOUT";
1109 status = "DISABLED";
1115 if (script_status->scripts[i].status > 0)
1119 if (options.machinereadable) {
1120 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1121 ctdb_eventscript_call_names[type],
1122 script_status->scripts[i].name,
1123 script_status->scripts[i].status,
1125 (long)script_status->scripts[i].start.tv_sec,
1126 (long)script_status->scripts[i].start.tv_usec,
1127 (long)script_status->scripts[i].finished.tv_sec,
1128 (long)script_status->scripts[i].finished.tv_usec,
1129 script_status->scripts[i].output);
1133 printf("%-20s Status:%s ",
1134 script_status->scripts[i].name, status);
1136 /* Some other error, eg from stat. */
1137 printf("%-20s Status:CANNOT RUN (%s)",
1138 script_status->scripts[i].name,
1139 strerror(-script_status->scripts[i].status));
1141 if (script_status->scripts[i].status >= 0) {
1142 printf("Duration:%.3lf ",
1143 timeval_delta(&script_status->scripts[i].finished,
1144 &script_status->scripts[i].start));
1146 if (script_status->scripts[i].status != -ENOEXEC) {
1148 ctime(&script_status->scripts[i].start.tv_sec));
1149 if (script_status->scripts[i].status != 0) {
1150 printf(" OUTPUT:%s\n",
1151 script_status->scripts[i].output);
1161 static int control_scriptstatus(struct ctdb_context *ctdb,
1162 int argc, const char **argv)
1165 enum ctdb_eventscript_call type, min, max;
1169 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1174 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1178 for (type = 0; type < CTDB_EVENT_MAX; type++) {
1179 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1185 if (type == CTDB_EVENT_MAX) {
1186 if (strcmp(arg, "all") == 0) {
1188 max = CTDB_EVENT_MAX;
1190 DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1195 if (options.machinereadable) {
1196 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1199 for (type = min; type < max; type++) {
1200 ret = control_one_scriptstatus(ctdb, type);
1210 enable an eventscript
1212 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1220 ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1222 DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1230 disable an eventscript
1232 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1240 ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1242 DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1250 display the pnn of the recovery master
1252 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1256 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1257 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1260 printf("%d\n",recmaster);
1266 add a tickle to a public address
1268 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1270 struct ctdb_tcp_connection t;
1278 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1279 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1282 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1283 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1287 data.dptr = (uint8_t *)&t;
1288 data.dsize = sizeof(t);
1290 /* tell all nodes about this tcp connection */
1291 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1292 0, data, ctdb, NULL, NULL, NULL, NULL);
1294 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1303 delete a tickle from a node
1305 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1307 struct ctdb_tcp_connection t;
1315 if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1316 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1319 if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1320 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1324 data.dptr = (uint8_t *)&t;
1325 data.dsize = sizeof(t);
1327 /* tell all nodes about this tcp connection */
1328 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1329 0, data, ctdb, NULL, NULL, NULL, NULL);
1331 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1340 get a list of all tickles for this pnn
1342 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1344 struct ctdb_control_tcp_tickle_list *list;
1345 ctdb_sock_addr addr;
1354 port = atoi(argv[1]);
1357 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1358 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1362 ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1364 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1368 if (options.machinereadable){
1369 printf(":source ip:port:destination ip:port:\n");
1370 for (i=0;i<list->tickles.num;i++) {
1371 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1374 printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1375 printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1378 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1379 printf("Num tickles:%u\n", list->tickles.num);
1380 for (i=0;i<list->tickles.num;i++) {
1381 if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1384 printf("SRC: %s:%u ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1385 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));
1395 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1397 struct ctdb_all_public_ips *ips;
1398 struct ctdb_public_ip ip;
1401 uint32_t disable_time;
1403 struct ctdb_node_map *nodemap=NULL;
1404 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1407 data.dptr = (uint8_t*)&disable_time;
1408 data.dsize = sizeof(disable_time);
1409 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1411 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1417 /* read the public ip list from the node */
1418 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1420 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1421 talloc_free(tmp_ctx);
1425 for (i=0;i<ips->num;i++) {
1426 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1431 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1432 pnn, ctdb_addr_to_str(addr)));
1433 talloc_free(tmp_ctx);
1440 data.dptr = (uint8_t *)&ip;
1441 data.dsize = sizeof(ip);
1443 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1445 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1446 talloc_free(tmp_ctx);
1450 nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1451 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1458 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1459 talloc_free(tmp_ctx);
1463 ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1465 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1466 talloc_free(tmp_ctx);
1470 /* update the recovery daemon so it now knows to expect the new
1471 node assignment for this ip.
1473 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1475 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1479 talloc_free(tmp_ctx);
1484 move/failover an ip address to a specific node
1486 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1489 int ret, retries = 0;
1490 ctdb_sock_addr addr;
1497 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1498 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1503 if (sscanf(argv[1], "%u", &pnn) != 1) {
1504 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1509 ret = move_ip(ctdb, &addr, pnn);
1511 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1515 } while (retries < 5 && ret != 0);
1517 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1524 static int getips_store_callback(void *param, void *data)
1526 struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1527 struct ctdb_all_public_ips *ips = param;
1531 ips->ips[i].pnn = node_ip->pnn;
1532 ips->ips[i].addr = node_ip->addr;
1536 static int getips_count_callback(void *param, void *data)
1538 uint32_t *count = param;
1545 static uint32_t *ip_key(ctdb_sock_addr *ip)
1547 static uint32_t key[IP_KEYLEN];
1549 bzero(key, sizeof(key));
1551 switch (ip->sa.sa_family) {
1553 key[0] = ip->ip.sin_addr.s_addr;
1556 key[0] = ip->ip6.sin6_addr.s6_addr32[3];
1557 key[1] = ip->ip6.sin6_addr.s6_addr32[2];
1558 key[2] = ip->ip6.sin6_addr.s6_addr32[1];
1559 key[3] = ip->ip6.sin6_addr.s6_addr32[0];
1562 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1569 static void *add_ip_callback(void *parm, void *data)
1575 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1577 struct ctdb_all_public_ips *tmp_ips;
1578 struct ctdb_node_map *nodemap=NULL;
1579 trbt_tree_t *ip_tree;
1583 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1585 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1589 ip_tree = trbt_create(tmp_ctx, 0);
1591 for(i=0;i<nodemap->num;i++){
1592 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1595 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1599 /* read the public ip list from this node */
1600 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1602 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1606 for (j=0; j<tmp_ips->num;j++) {
1607 struct ctdb_public_ip *node_ip;
1609 node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1610 node_ip->pnn = tmp_ips->ips[j].pnn;
1611 node_ip->addr = tmp_ips->ips[j].addr;
1613 trbt_insertarray32_callback(ip_tree,
1614 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1618 talloc_free(tmp_ips);
1623 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1625 len = offsetof(struct ctdb_all_public_ips, ips) +
1626 count*sizeof(struct ctdb_public_ip);
1627 tmp_ips = talloc_zero_size(tmp_ctx, len);
1628 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1637 * scans all other nodes and returns a pnn for another node that can host this
1641 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1643 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1644 struct ctdb_all_public_ips *ips;
1645 struct ctdb_node_map *nodemap=NULL;
1648 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1650 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1651 talloc_free(tmp_ctx);
1655 for(i=0;i<nodemap->num;i++){
1656 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1659 if (nodemap->nodes[i].pnn == options.pnn) {
1663 /* read the public ip list from this node */
1664 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1666 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1670 for (j=0;j<ips->num;j++) {
1671 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1672 talloc_free(tmp_ctx);
1673 return nodemap->nodes[i].pnn;
1679 talloc_free(tmp_ctx);
1683 static uint32_t ipreallocate_finished;
1686 handler for receiving the response to ipreallocate
1688 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid,
1689 TDB_DATA data, void *private_data)
1691 ipreallocate_finished = 1;
1694 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1696 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1698 event_add_timed(ctdb->ev, ctdb,
1699 timeval_current_ofs(1, 0),
1700 ctdb_every_second, ctdb);
1704 ask the recovery daemon on the recovery master to perform a ip reallocation
1706 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1710 struct takeover_run_reply rd;
1712 struct ctdb_node_map *nodemap=NULL;
1714 struct timeval tv = timeval_current();
1716 /* we need some events to trigger so we can timeout and restart
1719 event_add_timed(ctdb->ev, ctdb,
1720 timeval_current_ofs(1, 0),
1721 ctdb_every_second, ctdb);
1723 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1725 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1728 rd.srvid = getpid();
1730 /* register a message port for receiveing the reply so that we
1731 can receive the reply
1733 ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1735 data.dptr = (uint8_t *)&rd;
1736 data.dsize = sizeof(rd);
1739 /* check that there are valid nodes available */
1740 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1741 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1744 for (i=0; i<nodemap->num;i++) {
1745 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1749 if (i==nodemap->num) {
1750 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1755 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
1756 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1760 /* verify the node exists */
1761 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1762 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1767 /* check tha there are nodes available that can act as a recmaster */
1768 for (i=0; i<nodemap->num; i++) {
1769 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1774 if (i == nodemap->num) {
1775 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1779 /* verify the recovery master is not STOPPED, nor BANNED */
1780 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1781 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1787 /* verify the recovery master is not STOPPED, nor BANNED */
1788 if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1789 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1795 ipreallocate_finished = 0;
1796 ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1798 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1802 tv = timeval_current();
1803 /* this loop will terminate when we have received the reply */
1804 while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1805 event_loop_once(ctdb->ev);
1807 if (ipreallocate_finished == 1) {
1820 add a public ip address to a node
1822 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1825 int len, retries = 0;
1827 ctdb_sock_addr addr;
1828 struct ctdb_control_ip_iface *pub;
1829 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1830 struct ctdb_all_public_ips *ips;
1834 talloc_free(tmp_ctx);
1838 if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1839 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1840 talloc_free(tmp_ctx);
1844 /* read the public ip list from the node */
1845 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1847 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1848 talloc_free(tmp_ctx);
1851 for (i=0;i<ips->num;i++) {
1852 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1853 DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1860 /* Dont timeout. This command waits for an ip reallocation
1861 which sometimes can take wuite a while if there has
1862 been a recent recovery
1866 len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1867 pub = talloc_size(tmp_ctx, len);
1868 CTDB_NO_MEMORY(ctdb, pub);
1872 pub->len = strlen(argv[1])+1;
1873 memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1876 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1878 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1882 } while (retries < 5 && ret != 0);
1884 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1885 talloc_free(tmp_ctx);
1890 ret = control_ipreallocate(ctdb, argc, argv);
1892 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Wait 3 seconds and try again.\n", options.pnn));
1896 } while (retries < 5 && ret != 0);
1898 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Giving up.\n", options.pnn));
1899 talloc_free(tmp_ctx);
1903 talloc_free(tmp_ctx);
1907 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1909 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1911 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1912 struct ctdb_node_map *nodemap=NULL;
1913 struct ctdb_all_public_ips *ips;
1916 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1918 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1922 /* remove it from the nodes that are not hosting the ip currently */
1923 for(i=0;i<nodemap->num;i++){
1924 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1927 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1928 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1932 for (j=0;j<ips->num;j++) {
1933 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1941 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1945 options.pnn = nodemap->nodes[i].pnn;
1946 control_delip(ctdb, argc, argv);
1950 /* remove it from every node (also the one hosting it) */
1951 for(i=0;i<nodemap->num;i++){
1952 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1955 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1956 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1960 for (j=0;j<ips->num;j++) {
1961 if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1969 options.pnn = nodemap->nodes[i].pnn;
1970 control_delip(ctdb, argc, argv);
1973 talloc_free(tmp_ctx);
1978 delete a public ip address from a node
1980 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1984 ctdb_sock_addr addr;
1985 struct ctdb_control_ip_iface pub;
1986 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1987 struct ctdb_all_public_ips *ips;
1990 talloc_free(tmp_ctx);
1994 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1995 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1999 if (options.pnn == CTDB_BROADCAST_ALL) {
2000 return control_delip_all(ctdb, argc, argv, &addr);
2007 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2009 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2010 talloc_free(tmp_ctx);
2014 for (i=0;i<ips->num;i++) {
2015 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2021 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2022 ctdb_addr_to_str(&addr)));
2023 talloc_free(tmp_ctx);
2027 if (ips->ips[i].pnn == options.pnn) {
2028 ret = find_other_host_for_public_ip(ctdb, &addr);
2031 ret = move_ip(ctdb, &addr, ret);
2033 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
2037 } while (retries < 5 && ret != 0);
2039 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
2045 ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2047 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2048 talloc_free(tmp_ctx);
2052 talloc_free(tmp_ctx);
2057 kill a tcp connection
2059 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2062 struct ctdb_control_killtcp killtcp;
2068 if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2069 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2073 if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2074 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2078 ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2080 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2091 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2094 ctdb_sock_addr addr;
2100 if (!parse_ip(argv[0], NULL, 0, &addr)) {
2101 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2105 ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2107 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2115 register a server id
2117 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2120 struct ctdb_server_id server_id;
2126 server_id.pnn = strtoul(argv[0], NULL, 0);
2127 server_id.type = strtoul(argv[1], NULL, 0);
2128 server_id.server_id = strtoul(argv[2], NULL, 0);
2130 ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2132 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2135 DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2141 unregister a server id
2143 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2146 struct ctdb_server_id server_id;
2152 server_id.pnn = strtoul(argv[0], NULL, 0);
2153 server_id.type = strtoul(argv[1], NULL, 0);
2154 server_id.server_id = strtoul(argv[2], NULL, 0);
2156 ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2158 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2165 check if a server id exists
2167 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2171 struct ctdb_server_id server_id;
2177 server_id.pnn = strtoul(argv[0], NULL, 0);
2178 server_id.type = strtoul(argv[1], NULL, 0);
2179 server_id.server_id = strtoul(argv[2], NULL, 0);
2181 ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2183 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2188 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2190 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2196 get a list of all server ids that are registered on a node
2198 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2201 struct ctdb_server_id_list *server_ids;
2203 ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2205 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2209 for (i=0; i<server_ids->num; i++) {
2210 printf("Server id %d:%d:%d\n",
2211 server_ids->server_ids[i].pnn,
2212 server_ids->server_ids[i].type,
2213 server_ids->server_ids[i].server_id);
2220 check if a server id exists
2222 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2224 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2230 talloc_free(tmp_ctx);
2234 ids = talloc_array(tmp_ctx, uint64_t, argc);
2235 result = talloc_array(tmp_ctx, uint8_t, argc);
2237 for (i = 0; i < argc; i++) {
2238 ids[i] = strtoull(argv[i], NULL, 0);
2241 if (!ctdb_check_message_handlers(ctdb_connection,
2242 options.pnn, argc, ids, result)) {
2243 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2245 talloc_free(tmp_ctx);
2249 for (i=0; i < argc; i++) {
2250 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2251 result[i] ? "exists" : "does not exist");
2254 talloc_free(tmp_ctx);
2259 send a tcp tickle ack
2261 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2264 ctdb_sock_addr src, dst;
2270 if (!parse_ip_port(argv[0], &src)) {
2271 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2275 if (!parse_ip_port(argv[1], &dst)) {
2276 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2280 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2284 DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2291 display public ip status
2293 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2296 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2297 struct ctdb_all_public_ips *ips;
2299 if (options.pnn == CTDB_BROADCAST_ALL) {
2300 /* read the list of public ips from all nodes */
2301 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2303 /* read the public ip list from this node */
2304 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2307 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2308 talloc_free(tmp_ctx);
2312 if (options.machinereadable){
2313 printf(":Public IP:Node:");
2314 if (options.verbose){
2315 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2319 if (options.pnn == CTDB_BROADCAST_ALL) {
2320 printf("Public IPs on ALL nodes\n");
2322 printf("Public IPs on node %u\n", options.pnn);
2326 for (i=1;i<=ips->num;i++) {
2327 struct ctdb_control_public_ip_info *info = NULL;
2329 char *aciface = NULL;
2330 char *avifaces = NULL;
2331 char *cifaces = NULL;
2333 if (options.pnn == CTDB_BROADCAST_ALL) {
2334 pnn = ips->ips[ips->num-i].pnn;
2340 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2341 &ips->ips[ips->num-i].addr, &info);
2348 for (j=0; j < info->num; j++) {
2349 if (cifaces == NULL) {
2350 cifaces = talloc_strdup(info,
2351 info->ifaces[j].name);
2353 cifaces = talloc_asprintf_append(cifaces,
2355 info->ifaces[j].name);
2358 if (info->active_idx == j) {
2359 aciface = info->ifaces[j].name;
2362 if (info->ifaces[j].link_state == 0) {
2366 if (avifaces == NULL) {
2367 avifaces = talloc_strdup(info, info->ifaces[j].name);
2369 avifaces = talloc_asprintf_append(avifaces,
2371 info->ifaces[j].name);
2376 if (options.machinereadable){
2378 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2379 ips->ips[ips->num-i].pnn);
2380 if (options.verbose){
2383 avifaces?avifaces:"",
2384 cifaces?cifaces:"");
2388 if (options.verbose) {
2389 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2390 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2391 ips->ips[ips->num-i].pnn,
2393 avifaces?avifaces:"",
2394 cifaces?cifaces:"");
2397 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2398 ips->ips[ips->num-i].pnn);
2404 talloc_free(tmp_ctx);
2411 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2414 ctdb_sock_addr addr;
2415 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2416 struct ctdb_control_public_ip_info *info;
2419 talloc_free(tmp_ctx);
2423 if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2424 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2428 /* read the public ip info from this node */
2429 ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2430 tmp_ctx, &addr, &info);
2432 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2433 argv[0], options.pnn));
2434 talloc_free(tmp_ctx);
2438 printf("Public IP[%s] info on node %u\n",
2439 ctdb_addr_to_str(&info->ip.addr),
2442 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2443 ctdb_addr_to_str(&info->ip.addr),
2444 info->ip.pnn, info->num);
2446 for (i=0; i<info->num; i++) {
2447 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2449 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2450 i+1, info->ifaces[i].name,
2451 info->ifaces[i].link_state?"up":"down",
2452 (unsigned int)info->ifaces[i].references,
2453 (i==info->active_idx)?" (active)":"");
2456 talloc_free(tmp_ctx);
2461 display interfaces status
2463 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2466 struct ctdb_ifaces_list *ifaces;
2468 /* read the public ip list from this node */
2469 if (!ctdb_getifaces(ctdb_connection, options.pnn, &ifaces)) {
2470 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2475 if (options.machinereadable){
2476 printf(":Name:LinkStatus:References:\n");
2478 printf("Interfaces on node %u\n", options.pnn);
2481 for (i=0; i<ifaces->num; i++) {
2482 if (options.machinereadable){
2483 printf(":%s:%s:%u\n",
2484 ifaces->ifaces[i].name,
2485 ifaces->ifaces[i].link_state?"1":"0",
2486 (unsigned int)ifaces->ifaces[i].references);
2488 printf("name:%s link:%s references:%u\n",
2489 ifaces->ifaces[i].name,
2490 ifaces->ifaces[i].link_state?"up":"down",
2491 (unsigned int)ifaces->ifaces[i].references);
2495 ctdb_free_ifaces(ifaces);
2501 set link status of an interface
2503 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2506 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2507 struct ctdb_control_iface_info info;
2515 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2516 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2518 talloc_free(tmp_ctx);
2521 strcpy(info.name, argv[0]);
2523 if (strcmp(argv[1], "up") == 0) {
2524 info.link_state = 1;
2525 } else if (strcmp(argv[1], "down") == 0) {
2526 info.link_state = 0;
2528 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2530 talloc_free(tmp_ctx);
2534 /* read the public ip list from this node */
2535 ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2538 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2539 argv[0], options.pnn));
2540 talloc_free(tmp_ctx);
2544 talloc_free(tmp_ctx);
2549 display pid of a ctdb daemon
2551 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2556 ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2558 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2561 printf("Pid:%d\n", pid);
2567 disable a remote node
2569 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2572 struct ctdb_node_map *nodemap=NULL;
2574 /* check if the node is already disabled */
2575 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2576 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2579 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2580 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2585 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2587 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2593 /* read the nodemap and verify the change took effect */
2594 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2595 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2599 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2600 ret = control_ipreallocate(ctdb, argc, argv);
2602 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2610 enable a disabled remote node
2612 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2616 struct ctdb_node_map *nodemap=NULL;
2619 /* check if the node is already enabled */
2620 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2621 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2624 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2625 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2630 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2632 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2638 /* read the nodemap and verify the change took effect */
2639 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2640 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2644 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2646 ret = control_ipreallocate(ctdb, argc, argv);
2648 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2658 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2661 struct ctdb_node_map *nodemap=NULL;
2664 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2666 DEBUG(DEBUG_ERR, ("Unable to stop node %u try again\n", options.pnn));
2671 /* read the nodemap and verify the change took effect */
2672 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2673 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2677 } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2678 ret = control_ipreallocate(ctdb, argc, argv);
2680 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2688 restart a stopped remote node
2690 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2694 struct ctdb_node_map *nodemap=NULL;
2697 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2699 DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2705 /* read the nodemap and verify the change took effect */
2706 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2707 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2711 } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2712 ret = control_ipreallocate(ctdb, argc, argv);
2714 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2721 static uint32_t get_generation(struct ctdb_context *ctdb)
2723 struct ctdb_vnn_map *vnnmap=NULL;
2726 /* wait until the recmaster is not in recovery mode */
2728 uint32_t recmode, recmaster;
2730 if (vnnmap != NULL) {
2731 talloc_free(vnnmap);
2735 /* get the recmaster */
2736 if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
2737 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2741 /* get recovery mode */
2742 if (!ctdb_getrecmode(ctdb_connection, recmaster, &recmode)) {
2743 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2747 /* get the current generation number */
2748 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2750 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2754 if ((recmode == CTDB_RECOVERY_NORMAL)
2755 && (vnnmap->generation != 1)){
2756 return vnnmap->generation;
2763 ban a node from the cluster
2765 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2768 struct ctdb_node_map *nodemap=NULL;
2769 struct ctdb_ban_time bantime;
2775 /* verify the node exists */
2776 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2778 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2782 if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2783 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2787 bantime.pnn = options.pnn;
2788 bantime.time = strtoul(argv[0], NULL, 0);
2790 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2792 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2796 ret = control_ipreallocate(ctdb, argc, argv);
2798 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2807 unban a node from the cluster
2809 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2812 struct ctdb_node_map *nodemap=NULL;
2813 struct ctdb_ban_time bantime;
2815 /* verify the node exists */
2816 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2818 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2822 if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2823 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2827 bantime.pnn = options.pnn;
2830 ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2832 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2836 ret = control_ipreallocate(ctdb, argc, argv);
2838 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2847 show ban information for a node
2849 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2852 struct ctdb_node_map *nodemap=NULL;
2853 struct ctdb_ban_time *bantime;
2855 /* verify the node exists */
2856 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2858 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2862 ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2864 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2868 if (bantime->time == 0) {
2869 printf("Node %u is not banned\n", bantime->pnn);
2871 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2880 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2884 ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2886 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2896 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2899 uint32_t generation, next_generation;
2901 /* record the current generation number */
2902 generation = get_generation(ctdb);
2904 ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2906 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2910 ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2912 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2916 /* wait until we are in a new generation */
2918 next_generation = get_generation(ctdb);
2919 if (next_generation != generation) {
2930 display monitoring mode of a remote node
2932 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2937 ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2939 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2942 if (!options.machinereadable){
2943 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2946 printf(":%d:\n",monmode);
2953 display capabilities of a remote node
2955 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2957 uint32_t capabilities;
2960 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2962 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2966 if (!options.machinereadable){
2967 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2968 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2969 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2970 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2972 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2973 printf(":%d:%d:%d:%d:\n",
2974 !!(capabilities&CTDB_CAP_RECMASTER),
2975 !!(capabilities&CTDB_CAP_LMASTER),
2976 !!(capabilities&CTDB_CAP_LVS),
2977 !!(capabilities&CTDB_CAP_NATGW));
2983 display lvs configuration
2985 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2987 uint32_t *capabilities;
2988 struct ctdb_node_map *nodemap=NULL;
2990 int healthy_count = 0;
2992 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2994 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2998 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2999 CTDB_NO_MEMORY(ctdb, capabilities);
3001 /* collect capabilities for all connected nodes */
3002 for (i=0; i<nodemap->num; i++) {
3003 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3006 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3010 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3012 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3016 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3020 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3025 /* Print all LVS nodes */
3026 for (i=0; i<nodemap->num; i++) {
3027 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3030 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3033 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3037 if (healthy_count != 0) {
3038 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3043 printf("%d:%s\n", i,
3044 ctdb_addr_to_str(&nodemap->nodes[i].addr));
3051 display who is the lvs master
3053 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3055 uint32_t *capabilities;
3056 struct ctdb_node_map *nodemap=NULL;
3058 int healthy_count = 0;
3060 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
3062 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3066 capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3067 CTDB_NO_MEMORY(ctdb, capabilities);
3069 /* collect capabilities for all connected nodes */
3070 for (i=0; i<nodemap->num; i++) {
3071 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3074 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3078 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3080 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3084 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3088 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3093 /* find and show the lvsmaster */
3094 for (i=0; i<nodemap->num; i++) {
3095 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3098 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3101 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3105 if (healthy_count != 0) {
3106 if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3111 if (options.machinereadable){
3114 printf("Node %d is LVS master\n", i);
3119 printf("There is no LVS master\n");
3124 disable monitoring on a node
3126 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3131 ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3133 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3136 printf("Monitoring mode:%s\n","DISABLED");
3142 enable monitoring on a node
3144 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3149 ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3151 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3154 printf("Monitoring mode:%s\n","ACTIVE");
3160 display remote list of keys/data for a db
3162 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3164 const char *db_name;
3165 struct ctdb_db_context *ctdb_db;
3168 struct ctdb_dump_db_context c;
3177 if (db_exists(ctdb, db_name, &persistent)) {
3178 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3182 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3184 if (ctdb_db == NULL) {
3185 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3189 if (options.printlmaster) {
3190 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3191 ctdb, &ctdb->vnn_map);
3193 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3201 c.printemptyrecords = (bool)options.printemptyrecords;
3202 c.printdatasize = (bool)options.printdatasize;
3203 c.printlmaster = (bool)options.printlmaster;
3204 c.printhash = (bool)options.printhash;
3205 c.printrecordflags = (bool)options.printrecordflags;
3207 /* traverse and dump the cluster tdb */
3208 ret = ctdb_dump_db(ctdb_db, &c);
3210 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3211 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3212 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3216 talloc_free(ctdb_db);
3218 printf("Dumped %d records\n", ret);
3222 struct cattdb_data {
3223 struct ctdb_context *ctdb;
3227 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3229 struct cattdb_data *d = private_data;
3230 struct ctdb_dump_db_context c;
3236 c.printemptyrecords = (bool)options.printemptyrecords;
3237 c.printdatasize = (bool)options.printdatasize;
3238 c.printlmaster = false;
3239 c.printhash = (bool)options.printhash;
3240 c.printrecordflags = true;
3242 return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3246 cat the local tdb database using same format as catdb
3248 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3250 const char *db_name;
3251 struct ctdb_db_context *ctdb_db;
3252 struct cattdb_data d;
3262 if (db_exists(ctdb, db_name, &persistent)) {
3263 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3267 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, false, 0);
3269 if (ctdb_db == NULL) {
3270 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3274 /* traverse the local tdb */
3277 if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3278 printf("Failed to cattdb data\n");
3281 talloc_free(ctdb_db);
3283 printf("Dumped %d records\n", d.count);
3288 display the content of a database key
3290 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3292 const char *db_name;
3293 struct ctdb_db_context *ctdb_db;
3294 struct ctdb_record_handle *h;
3295 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3305 if (db_exists(ctdb, db_name, &persistent)) {
3306 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3310 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3312 if (ctdb_db == NULL) {
3313 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3317 key.dptr = discard_const(argv[1]);
3318 key.dsize = strlen((char *)key.dptr);
3320 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3322 printf("Failed to fetch record '%s' on node %d\n",
3323 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3324 talloc_free(tmp_ctx);
3328 printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3330 talloc_free(ctdb_db);
3331 talloc_free(tmp_ctx);
3336 display the content of a database key
3338 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3340 const char *db_name;
3341 struct ctdb_db_context *ctdb_db;
3342 struct ctdb_record_handle *h;
3343 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3353 if (db_exists(ctdb, db_name, &persistent)) {
3354 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3358 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3360 if (ctdb_db == NULL) {
3361 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3365 key.dptr = discard_const(argv[1]);
3366 key.dsize = strlen((char *)key.dptr);
3368 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3370 printf("Failed to fetch record '%s' on node %d\n",
3371 (const char *)key.dptr, ctdb_get_pnn(ctdb));
3372 talloc_free(tmp_ctx);
3376 data.dptr = discard_const(argv[2]);
3377 data.dsize = strlen((char *)data.dptr);
3379 if (ctdb_record_store(h, data) != 0) {
3380 printf("Failed to store record\n");
3384 talloc_free(ctdb_db);
3385 talloc_free(tmp_ctx);
3390 fetch a record from a persistent database
3392 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3394 const char *db_name;
3395 struct ctdb_db_context *ctdb_db;
3396 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3397 struct ctdb_transaction_handle *h;
3403 talloc_free(tmp_ctx);
3410 if (db_exists(ctdb, db_name, &persistent)) {
3411 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3412 talloc_free(tmp_ctx);
3417 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3418 talloc_free(tmp_ctx);
3422 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3424 if (ctdb_db == NULL) {
3425 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3426 talloc_free(tmp_ctx);
3430 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3432 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3433 talloc_free(tmp_ctx);
3437 key.dptr = discard_const(argv[1]);
3438 key.dsize = strlen(argv[1]);
3439 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3441 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3442 talloc_free(tmp_ctx);
3446 if (data.dsize == 0 || data.dptr == NULL) {
3447 DEBUG(DEBUG_ERR,("Record is empty\n"));
3448 talloc_free(tmp_ctx);
3453 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3455 DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3456 talloc_free(tmp_ctx);
3459 write(fd, data.dptr, data.dsize);
3462 write(1, data.dptr, data.dsize);
3465 /* abort the transaction */
3469 talloc_free(tmp_ctx);
3474 fetch a record from a tdb-file
3476 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3478 const char *tdb_file;
3481 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3490 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3492 printf("Failed to open TDB file %s\n", tdb_file);
3496 if (!strncmp(argv[1], "0x", 2)) {
3497 key = hextodata(tmp_ctx, argv[1] + 2);
3498 if (key.dsize == 0) {
3499 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3503 key.dptr = discard_const(argv[1]);
3504 key.dsize = strlen(argv[1]);
3507 data = tdb_fetch(tdb, key);
3508 if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3509 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3517 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3519 printf("Failed to open output file %s\n", argv[2]);
3522 if (options.verbose){
3523 write(fd, data.dptr, data.dsize);
3525 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3529 if (options.verbose){
3530 write(1, data.dptr, data.dsize);
3532 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3536 talloc_free(tmp_ctx);
3541 store a record and header to a tdb-file
3543 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
3545 const char *tdb_file;
3548 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3556 tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
3558 printf("Failed to open TDB file %s\n", tdb_file);
3562 if (!strncmp(argv[1], "0x", 2)) {
3563 key = hextodata(tmp_ctx, argv[1] + 2);
3564 if (key.dsize == 0) {
3565 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3569 key.dptr = discard_const(argv[1]);
3570 key.dsize = strlen(argv[1]);
3573 if (!strncmp(argv[2], "0x", 2)) {
3574 data = hextodata(tmp_ctx, argv[2] + 2);
3575 if (data.dsize == 0) {
3576 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
3580 data.dptr = discard_const(argv[2]);
3581 data.dsize = strlen(argv[2]);
3584 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
3585 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
3588 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
3589 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
3596 talloc_free(tmp_ctx);
3601 write a record to a persistent database
3603 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3605 const char *db_name;
3606 struct ctdb_db_context *ctdb_db;
3607 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3608 struct ctdb_transaction_handle *h;
3614 talloc_free(tmp_ctx);
3618 fd = open(argv[2], O_RDONLY);
3620 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s %s\n", argv[2], strerror(errno)));
3621 talloc_free(tmp_ctx);
3625 ret = fstat(fd, &st);
3627 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3629 talloc_free(tmp_ctx);
3633 if (!S_ISREG(st.st_mode)) {
3634 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3636 talloc_free(tmp_ctx);
3640 data.dsize = st.st_size;
3641 if (data.dsize == 0) {
3644 data.dptr = talloc_size(tmp_ctx, data.dsize);
3645 if (data.dptr == NULL) {
3646 DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3648 talloc_free(tmp_ctx);
3651 ret = read(fd, data.dptr, data.dsize);
3652 if (ret != data.dsize) {
3653 DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3655 talloc_free(tmp_ctx);
3664 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
3665 if (ctdb_db == NULL) {
3666 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3667 talloc_free(tmp_ctx);
3671 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3673 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3674 talloc_free(tmp_ctx);
3678 key.dptr = discard_const(argv[1]);
3679 key.dsize = strlen(argv[1]);
3680 ret = ctdb_transaction_store(h, key, data);
3682 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3683 talloc_free(tmp_ctx);
3687 ret = ctdb_transaction_commit(h);
3689 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3690 talloc_free(tmp_ctx);
3695 talloc_free(tmp_ctx);
3700 check if a service is bound to a port or not
3702 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
3707 struct sockaddr_in sin;
3710 printf("Use: ctdb chktcport <port>\n");
3714 port = atoi(argv[0]);
3716 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
3718 printf("Failed to open local socket\n");
3722 v = fcntl(s, F_GETFL, 0);
3723 fcntl(s, F_SETFL, v | O_NONBLOCK);
3725 bzero(&sin, sizeof(sin));
3726 sin.sin_family = PF_INET;
3727 sin.sin_port = htons(port);
3728 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
3731 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
3740 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid,
3741 TDB_DATA data, void *private_data)
3743 DEBUG(DEBUG_ERR,("Log data received\n"));
3744 if (data.dsize > 0) {
3745 printf("%s", data.dptr);
3752 display a list of log messages from the in memory ringbuffer
3754 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3758 struct ctdb_get_log_addr log_addr;
3760 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3765 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3766 talloc_free(tmp_ctx);
3770 log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3771 log_addr.srvid = getpid();
3772 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
3773 log_addr.level = get_debug_by_desc(argv[0]);
3775 log_addr.level = strtol(argv[0], NULL, 0);
3779 data.dptr = (unsigned char *)&log_addr;
3780 data.dsize = sizeof(log_addr);
3782 DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3784 ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3787 DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3789 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3790 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3791 if (ret != 0 || res != 0) {
3792 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3793 talloc_free(tmp_ctx);
3798 tv = timeval_current();
3799 /* this loop will terminate when we have received the reply */
3800 while (timeval_elapsed(&tv) < 3.0) {
3801 event_loop_once(ctdb->ev);
3804 DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3806 talloc_free(tmp_ctx);
3811 clear the in memory log area
3813 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3818 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3820 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3821 0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3822 if (ret != 0 || res != 0) {
3823 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3824 talloc_free(tmp_ctx);
3828 talloc_free(tmp_ctx);
3835 display a list of the databases on a remote ctdb
3837 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3840 struct ctdb_dbid_map *dbmap=NULL;
3842 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3844 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3848 if(options.machinereadable){
3849 printf(":ID:Name:Path:Persistent:Unhealthy:ReadOnly:\n");
3850 for(i=0;i<dbmap->num;i++){
3857 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3858 dbmap->dbs[i].dbid, ctdb, &path);
3859 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3860 dbmap->dbs[i].dbid, ctdb, &name);
3861 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3862 dbmap->dbs[i].dbid, ctdb, &health);
3863 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3864 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3865 printf(":0x%08X:%s:%s:%d:%d:%d:\n",
3866 dbmap->dbs[i].dbid, name, path,
3867 !!(persistent), !!(health), !!(readonly));
3872 printf("Number of databases:%d\n", dbmap->num);
3873 for(i=0;i<dbmap->num;i++){
3880 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3881 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3882 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3883 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3884 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3885 printf("dbid:0x%08x name:%s path:%s%s%s%s\n",
3886 dbmap->dbs[i].dbid, name, path,
3887 persistent?" PERSISTENT":"",
3888 readonly?" READONLY":"",
3889 health?" UNHEALTHY":"");
3896 display the status of a database on a remote ctdb
3898 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3901 struct ctdb_dbid_map *dbmap=NULL;
3902 const char *db_name;
3910 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3912 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3916 for(i=0;i<dbmap->num;i++){
3923 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3924 if (strcmp(name, db_name) != 0) {
3928 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3929 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3930 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
3931 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
3932 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nREADONLY: %s\nHEALTH: %s\n",
3933 dbmap->dbs[i].dbid, name, path,
3934 persistent?"yes":"no",
3935 readonly?"yes":"no",
3936 health?health:"OK");
3940 DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3945 check if the local node is recmaster or not
3946 it will return 1 if this node is the recmaster and 0 if it is not
3947 or if the local ctdb daemon could not be contacted
3949 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3951 uint32_t mypnn, recmaster;
3953 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3955 printf("Failed to get pnn of node\n");
3959 if (!ctdb_getrecmaster(ctdb_connection, options.pnn, &recmaster)) {
3960 printf("Failed to get the recmaster\n");
3964 if (recmaster != mypnn) {
3965 printf("this node is not the recmaster\n");
3969 printf("this node is the recmaster\n");
3976 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3979 struct timeval tv = timeval_current();
3980 ret = ctdb_ctrl_ping(ctdb, options.pnn);
3982 printf("Unable to get ping response from node %u\n", options.pnn);
3985 printf("response from %u time=%.6f sec (%d clients)\n",
3986 options.pnn, timeval_elapsed(&tv), ret);
3995 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4006 ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4008 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4012 printf("%-23s = %u\n", name, value);
4019 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4030 value = strtoul(argv[1], NULL, 0);
4032 ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4034 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4043 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4049 ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4051 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4055 for (i=0;i<count;i++) {
4056 control_getvar(ctdb, 1, &list[i]);
4065 display debug level on a node
4067 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4072 ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4074 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4077 if (options.machinereadable){
4078 printf(":Name:Level:\n");
4079 printf(":%s:%d:\n",get_debug_by_level(level),level);
4081 printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4088 display reclock file of a node
4090 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4093 const char *reclock;
4095 ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4097 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4100 if (options.machinereadable){
4101 if (reclock != NULL) {
4102 printf("%s", reclock);
4105 if (reclock == NULL) {
4106 printf("No reclock file used.\n");
4108 printf("Reclock file:%s\n", reclock);
4116 set the reclock file of a node
4118 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4121 const char *reclock;
4125 } else if (argc == 1) {
4131 ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4133 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4140 set the natgw state on/off
4142 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4145 uint32_t natgwstate;
4151 if (!strcmp(argv[0], "on")) {
4153 } else if (!strcmp(argv[0], "off")) {
4159 ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4161 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4169 set the lmaster role on/off
4171 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4174 uint32_t lmasterrole;
4180 if (!strcmp(argv[0], "on")) {
4182 } else if (!strcmp(argv[0], "off")) {
4188 ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4190 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4198 set the recmaster role on/off
4200 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4203 uint32_t recmasterrole;
4209 if (!strcmp(argv[0], "on")) {
4211 } else if (!strcmp(argv[0], "off")) {
4217 ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4219 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4227 set debug level on a node or all nodes
4229 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4235 printf("You must specify the debug level. Valid levels are:\n");
4236 for (i=0; debug_levels[i].description != NULL; i++) {
4237 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4243 if (isalpha(argv[0][0]) || argv[0][0] == '-') {
4244 level = get_debug_by_desc(argv[0]);
4246 level = strtol(argv[0], NULL, 0);
4249 for (i=0; debug_levels[i].description != NULL; i++) {
4250 if (level == debug_levels[i].level) {
4254 if (debug_levels[i].description == NULL) {
4255 printf("Invalid debug level, must be one of\n");
4256 for (i=0; debug_levels[i].description != NULL; i++) {
4257 printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4262 ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4264 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4273 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4279 priority = strtol(argv[0], NULL, 0);
4283 DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4285 ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4287 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4294 attach to a database
4296 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4298 const char *db_name;
4299 struct ctdb_db_context *ctdb_db;
4300 bool persistent = false;
4310 if (strcmp(argv[1], "persistent") != 0) {
4316 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4317 if (ctdb_db == NULL) {
4318 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4328 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4330 struct ctdb_db_priority db_prio;
4337 db_prio.db_id = strtoul(argv[0], NULL, 0);
4338 db_prio.priority = strtoul(argv[1], NULL, 0);
4340 ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
4342 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
4352 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4354 uint32_t db_id, priority;
4361 db_id = strtoul(argv[0], NULL, 0);
4363 ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
4365 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
4369 DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
4375 set the readonly capability for a database
4377 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
4386 db_id = strtoul(argv[0], NULL, 0);
4388 ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
4390 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
4400 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
4410 db_id = strtoul(argv[0], NULL, 0);
4412 ret = ctdb_getdbseqnum(ctdb_connection, options.pnn, db_id, &seqnum);
4414 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
4418 printf("Sequence number:%lld\n", (long long)seqnum);
4424 run an eventscript on a node
4426 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
4432 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4435 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4439 data.dptr = (unsigned char *)discard_const(argv[0]);
4440 data.dsize = strlen((char *)data.dptr) + 1;
4442 DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
4444 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
4445 0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4446 if (ret != 0 || res != 0) {
4447 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
4448 talloc_free(tmp_ctx);
4451 talloc_free(tmp_ctx);
4455 #define DB_VERSION 1
4456 #define MAX_DB_NAME 64
4457 struct db_file_header {
4458 unsigned long version;
4460 unsigned long persistent;
4462 const char name[MAX_DB_NAME];
4465 struct backup_data {
4466 struct ctdb_marshall_buffer *records;
4469 bool traverse_error;
4472 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
4474 struct backup_data *bd = talloc_get_type(private, struct backup_data);
4475 struct ctdb_rec_data *rec;
4477 /* add the record */
4478 rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
4480 bd->traverse_error = true;
4481 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
4484 bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
4485 if (bd->records == NULL) {
4486 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
4487 bd->traverse_error = true;
4490 bd->records->count++;
4491 memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4492 bd->len += rec->length;
4500 * backup a database to a file
4502 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4505 struct ctdb_dbid_map *dbmap=NULL;
4506 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4507 struct db_file_header dbhdr;
4508 struct ctdb_db_context *ctdb_db;
4509 struct backup_data *bd;
4512 const char *reason = NULL;
4515 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4519 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4521 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4525 for(i=0;i<dbmap->num;i++){
4528 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4529 if(!strcmp(argv[0], name)){
4530 talloc_free(discard_const(name));
4533 talloc_free(discard_const(name));
4535 if (i == dbmap->num) {
4536 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4537 talloc_free(tmp_ctx);
4541 ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4542 dbmap->dbs[i].dbid, tmp_ctx, &reason);
4544 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4546 talloc_free(tmp_ctx);
4550 uint32_t allow_unhealthy = 0;
4552 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4553 "AllowUnhealthyDBRead",
4556 if (allow_unhealthy != 1) {
4557 DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4560 DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
4562 talloc_free(tmp_ctx);
4566 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4568 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4569 "tunnable AllowUnhealthyDBRead = %u\n",
4573 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4574 if (ctdb_db == NULL) {
4575 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4576 talloc_free(tmp_ctx);
4581 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4583 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4584 talloc_free(tmp_ctx);
4589 bd = talloc_zero(tmp_ctx, struct backup_data);
4591 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4592 talloc_free(tmp_ctx);
4596 bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4597 if (bd->records == NULL) {
4598 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4599 talloc_free(tmp_ctx);
4603 bd->len = offsetof(struct ctdb_marshall_buffer, data);
4604 bd->records->db_id = ctdb_db->db_id;
4605 /* traverse the database collecting all records */
4606 if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4607 bd->traverse_error) {
4608 DEBUG(DEBUG_ERR,("Traverse error\n"));
4609 talloc_free(tmp_ctx);
4613 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4616 fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4618 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4619 talloc_free(tmp_ctx);
4623 dbhdr.version = DB_VERSION;
4624 dbhdr.timestamp = time(NULL);
4625 dbhdr.persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4626 dbhdr.size = bd->len;
4627 if (strlen(argv[0]) >= MAX_DB_NAME) {
4628 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4631 strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4632 ret = write(fh, &dbhdr, sizeof(dbhdr));
4634 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4637 ret = write(fh, bd->records, bd->len);
4639 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4648 DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4652 DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
4654 talloc_free(tmp_ctx);
4659 * restore a database from a file
4661 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4664 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4667 struct db_file_header dbhdr;
4668 struct ctdb_db_context *ctdb_db;
4669 struct ctdb_node_map *nodemap=NULL;
4670 struct ctdb_vnn_map *vnnmap=NULL;
4672 struct ctdb_control_wipe_database w;
4674 uint32_t generation;
4679 if (argc < 1 || argc > 2) {
4680 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4684 fh = open(argv[0], O_RDONLY);
4686 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4687 talloc_free(tmp_ctx);
4691 read(fh, &dbhdr, sizeof(dbhdr));
4692 if (dbhdr.version != DB_VERSION) {
4693 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4694 talloc_free(tmp_ctx);
4698 dbname = discard_const(dbhdr.name);
4700 dbname = discard_const(argv[1]);
4703 outdata.dsize = dbhdr.size;
4704 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4705 if (outdata.dptr == NULL) {
4706 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4708 talloc_free(tmp_ctx);
4711 read(fh, outdata.dptr, outdata.dsize);
4714 tm = localtime(&dbhdr.timestamp);
4715 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4716 printf("Restoring database '%s' from backup @ %s\n",
4720 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
4721 if (ctdb_db == NULL) {
4722 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4723 talloc_free(tmp_ctx);
4727 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4729 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4730 talloc_free(tmp_ctx);
4735 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4737 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4738 talloc_free(tmp_ctx);
4742 /* freeze all nodes */
4743 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4744 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4745 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4751 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4752 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4753 talloc_free(tmp_ctx);
4758 generation = vnnmap->generation;
4759 data.dptr = (void *)&generation;
4760 data.dsize = sizeof(generation);
4762 /* start a cluster wide transaction */
4763 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4764 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4766 TIMELIMIT(), false, data,
4769 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4774 w.db_id = ctdb_db->db_id;
4775 w.transaction_id = generation;
4777 data.dptr = (void *)&w;
4778 data.dsize = sizeof(w);
4780 /* wipe all the remote databases. */
4781 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4782 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4784 TIMELIMIT(), false, data,
4787 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4788 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4789 talloc_free(tmp_ctx);
4793 /* push the database */
4794 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4795 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4797 TIMELIMIT(), false, outdata,
4800 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4801 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4802 talloc_free(tmp_ctx);
4806 data.dptr = (void *)&ctdb_db->db_id;
4807 data.dsize = sizeof(ctdb_db->db_id);
4809 /* mark the database as healthy */
4810 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4811 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4813 TIMELIMIT(), false, data,
4816 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4817 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4818 talloc_free(tmp_ctx);
4822 data.dptr = (void *)&generation;
4823 data.dsize = sizeof(generation);
4825 /* commit all the changes */
4826 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4828 TIMELIMIT(), false, data,
4831 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4832 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4833 talloc_free(tmp_ctx);
4838 /* thaw all nodes */
4839 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4840 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4846 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4847 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4848 talloc_free(tmp_ctx);
4853 talloc_free(tmp_ctx);
4858 * dump a database backup from a file
4860 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4862 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4864 struct db_file_header dbhdr;
4868 struct ctdb_rec_data *rec = NULL;
4869 struct ctdb_marshall_buffer *m;
4870 struct ctdb_dump_db_context c;
4873 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4877 fh = open(argv[0], O_RDONLY);
4879 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4880 talloc_free(tmp_ctx);
4884 read(fh, &dbhdr, sizeof(dbhdr));
4885 if (dbhdr.version != DB_VERSION) {
4886 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4887 talloc_free(tmp_ctx);
4891 outdata.dsize = dbhdr.size;
4892 outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4893 if (outdata.dptr == NULL) {
4894 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4896 talloc_free(tmp_ctx);
4899 read(fh, outdata.dptr, outdata.dsize);
4901 m = (struct ctdb_marshall_buffer *)outdata.dptr;
4903 tm = localtime(&dbhdr.timestamp);
4904 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4905 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4906 dbhdr.name, m->db_id, tbuf);
4910 c.printemptyrecords = (bool)options.printemptyrecords;
4911 c.printdatasize = (bool)options.printdatasize;
4912 c.printlmaster = false;
4913 c.printhash = (bool)options.printhash;
4914 c.printrecordflags = (bool)options.printrecordflags;
4916 for (i=0; i < m->count; i++) {
4920 /* we do not want the header splitted, so we pass NULL*/
4921 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4924 ctdb_dumpdb_record(ctdb, key, data, &c);
4927 printf("Dumped %d records\n", i);
4928 talloc_free(tmp_ctx);
4933 * wipe a database from a file
4935 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4939 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4941 struct ctdb_db_context *ctdb_db;
4942 struct ctdb_node_map *nodemap = NULL;
4943 struct ctdb_vnn_map *vnnmap = NULL;
4945 struct ctdb_control_wipe_database w;
4947 uint32_t generation;
4948 struct ctdb_dbid_map *dbmap = NULL;
4951 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4955 ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4958 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4963 for(i=0;i<dbmap->num;i++){
4966 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4967 dbmap->dbs[i].dbid, tmp_ctx, &name);
4968 if(!strcmp(argv[0], name)){
4969 talloc_free(discard_const(name));
4972 talloc_free(discard_const(name));
4974 if (i == dbmap->num) {
4975 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4977 talloc_free(tmp_ctx);
4981 ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT, 0);
4982 if (ctdb_db == NULL) {
4983 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4985 talloc_free(tmp_ctx);
4989 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4992 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4994 talloc_free(tmp_ctx);
4998 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5001 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5003 talloc_free(tmp_ctx);
5007 /* freeze all nodes */
5008 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5009 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5010 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5017 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5018 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5019 CTDB_RECOVERY_ACTIVE);
5020 talloc_free(tmp_ctx);
5025 generation = vnnmap->generation;
5026 data.dptr = (void *)&generation;
5027 data.dsize = sizeof(generation);
5029 /* start a cluster wide transaction */
5030 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5031 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5033 TIMELIMIT(), false, data,
5037 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5038 "transactions.\n"));
5042 w.db_id = ctdb_db->db_id;
5043 w.transaction_id = generation;
5045 data.dptr = (void *)&w;
5046 data.dsize = sizeof(w);
5048 /* wipe all the remote databases. */
5049 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5050 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5052 TIMELIMIT(), false, data,
5055 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5056 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5057 talloc_free(tmp_ctx);
5061 data.dptr = (void *)&ctdb_db->db_id;
5062 data.dsize = sizeof(ctdb_db->db_id);
5064 /* mark the database as healthy */
5065 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5066 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5068 TIMELIMIT(), false, data,
5071 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5072 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5073 talloc_free(tmp_ctx);
5077 data.dptr = (void *)&generation;
5078 data.dsize = sizeof(generation);
5080 /* commit all the changes */
5081 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5083 TIMELIMIT(), false, data,
5086 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5087 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5088 talloc_free(tmp_ctx);
5092 /* thaw all nodes */
5093 nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5094 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5100 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5101 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5102 talloc_free(tmp_ctx);
5106 DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5108 talloc_free(tmp_ctx);
5115 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5121 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5122 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5123 0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5124 if (ret != 0 || res != 0) {
5125 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5126 talloc_free(tmp_ctx);
5129 write(1, data.dptr, data.dsize);
5130 talloc_free(tmp_ctx);
5135 handler for memory dumps
5137 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid,
5138 TDB_DATA data, void *private_data)
5140 write(1, data.dptr, data.dsize);
5145 dump memory usage on the recovery daemon
5147 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5151 struct rd_memdump_reply rd;
5153 rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5155 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
5158 rd.srvid = getpid();
5160 /* register a message port for receiveing the reply so that we
5161 can receive the reply
5163 ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5166 data.dptr = (uint8_t *)&rd;
5167 data.dsize = sizeof(rd);
5169 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5171 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5175 /* this loop will terminate when we have received the reply */
5177 event_loop_once(ctdb->ev);
5184 send a message to a srvid
5186 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5188 unsigned long srvid;
5196 srvid = strtoul(argv[0], NULL, 0);
5198 data.dptr = (uint8_t *)discard_const(argv[1]);
5199 data.dsize= strlen(argv[1]);
5201 ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5203 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5211 handler for msglisten
5213 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid,
5214 TDB_DATA data, void *private_data)
5218 printf("Message received: ");
5219 for (i=0;i<data.dsize;i++) {
5220 printf("%c", data.dptr[i]);
5226 listen for messages on a messageport
5228 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5234 /* register a message port and listen for messages
5236 ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5237 printf("Listening for messages on srvid:%d\n", (int)srvid);
5240 event_loop_once(ctdb->ev);
5247 list all nodes in the cluster
5248 we parse the nodes file directly
5250 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5252 TALLOC_CTX *mem_ctx = talloc_new(NULL);
5253 struct pnn_node *pnn_nodes;
5254 struct pnn_node *pnn_node;
5256 pnn_nodes = read_nodes_file(mem_ctx);
5257 if (pnn_nodes == NULL) {
5258 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5259 talloc_free(mem_ctx);
5263 for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5264 ctdb_sock_addr addr;
5265 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5266 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5267 talloc_free(mem_ctx);
5270 if (options.machinereadable){
5271 printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5273 printf("%s\n", pnn_node->addr);
5276 talloc_free(mem_ctx);
5282 reload the nodes file on the local node
5284 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5288 struct ctdb_node_map *nodemap=NULL;
5290 mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
5292 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
5296 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5298 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5302 /* reload the nodes file on all remote nodes */
5303 for (i=0;i<nodemap->num;i++) {
5304 if (nodemap->nodes[i].pnn == mypnn) {
5307 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5308 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5309 nodemap->nodes[i].pnn);
5311 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5315 /* reload the nodes file on the local node */
5316 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5317 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5319 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5322 /* initiate a recovery */
5323 control_recover(ctdb, argc, argv);
5329 static const struct {
5331 int (*fn)(struct ctdb_context *, int, const char **);
5333 bool without_daemon; /* can be run without daemon running ? */
5336 } ctdb_commands[] = {
5338 { "version", control_version, true, false, "show version of ctdb" },
5340 { "status", control_status, true, false, "show node status" },
5341 { "uptime", control_uptime, true, false, "show node uptime" },
5342 { "ping", control_ping, true, false, "ping all nodes" },
5343 { "getvar", control_getvar, true, false, "get a tunable variable", "<name>"},
5344 { "setvar", control_setvar, true, false, "set a tunable variable", "<name> <value>"},
5345 { "listvars", control_listvars, true, false, "list tunable variables"},
5346 { "statistics", control_statistics, false, false, "show statistics" },
5347 { "statisticsreset", control_statistics_reset, true, false, "reset statistics"},
5348 { "stats", control_stats, false, false, "show rolling statistics", "[number of history records]" },
5349 { "ip", control_ip, false, false, "show which public ip's that ctdb manages" },
5350 { "ipinfo", control_ipinfo, true, false, "show details about a public ip that ctdb manages", "<ip>" },
5351 { "ifaces", control_ifaces, true, false, "show which interfaces that ctdb manages" },
5352 { "setifacelink", control_setifacelink, true, false, "set interface link status", "<iface> <status>" },
5353 { "process-exists", control_process_exists, true, false, "check if a process exists on a node", "<pid>"},
5354 { "getdbmap", control_getdbmap, true, false, "show the database map" },
5355 { "getdbstatus", control_getdbstatus, true, false, "show the status of a database", "<dbname>" },
5356 { "catdb", control_catdb, true, false, "dump a ctdb database" , "<dbname>"},
5357 { "cattdb", control_cattdb, true, false, "dump a local tdb database" , "<dbname>"},
5358 { "getmonmode", control_getmonmode, true, false, "show monitoring mode" },
5359 { "getcapabilities", control_getcapabilities, true, false, "show node capabilities" },
5360 { "pnn", control_pnn, true, false, "show the pnn of the currnet node" },
5361 { "lvs", control_lvs, true, false, "show lvs configuration" },
5362 { "lvsmaster", control_lvsmaster, true, false, "show which node is the lvs master" },
5363 { "disablemonitor", control_disable_monmode,true, false, "set monitoring mode to DISABLE" },
5364 { "enablemonitor", control_enable_monmode, true, false, "set monitoring mode to ACTIVE" },
5365 { "setdebug", control_setdebug, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
5366 { "getdebug", control_getdebug, true, false, "get debug level" },
5367 { "getlog", control_getlog, true, false, "get the log data from the in memory ringbuffer", "<level>" },
5368 { "clearlog", control_clearlog, true, false, "clear the log data from the in memory ringbuffer" },
5369 { "attach", control_attach, true, false, "attach to a database", "<dbname> [persistent]" },
5370 { "dumpmemory", control_dumpmemory, true, false, "dump memory map to stdout" },
5371 { "rddumpmemory", control_rddumpmemory, true, false, "dump memory map from the recovery daemon to stdout" },
5372 { "getpid", control_getpid, true, false, "get ctdbd process ID" },
5373 { "disable", control_disable, true, false, "disable a nodes public IP" },
5374 { "enable", control_enable, true, false, "enable a nodes public IP" },
5375 { "stop", control_stop, true, false, "stop a node" },
5376 { "continue", control_continue, true, false, "re-start a stopped node" },
5377 { "ban", control_ban, true, false, "ban a node from the cluster", "<bantime|0>"},
5378 { "unban", control_unban, true, false, "unban a node" },
5379 { "showban", control_showban, true, false, "show ban information"},
5380 { "shutdown", control_shutdown, true, false, "shutdown ctdbd" },
5381 { "recover", control_recover, true, false, "force recovery" },
5382 { "sync", control_ipreallocate, true, false, "wait until ctdbd has synced all state changes" },
5383 { "ipreallocate", control_ipreallocate, true, false, "force the recovery daemon to perform a ip reallocation procedure" },
5384 { "thaw", control_thaw, true, false, "thaw databases", "[priority:1-3]" },
5385 { "isnotrecmaster", control_isnotrecmaster, false, false, "check if the local node is recmaster or not" },
5386 { "killtcp", kill_tcp, false, false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
5387 { "gratiousarp", control_gratious_arp, false, false, "send a gratious arp", "<ip> <interface>" },
5388 { "tickle", tickle_tcp, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5389 { "gettickles", control_get_tickles, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
5390 { "addtickle", control_add_tickle, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
5392 { "deltickle", control_del_tickle, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
5394 { "regsrvid", regsrvid, false, false, "register a server id", "<pnn> <type> <id>" },
5395 { "unregsrvid", unregsrvid, false, false, "unregister a server id", "<pnn> <type> <id>" },
5396 { "chksrvid", chksrvid, false, false, "check if a server id exists", "<pnn> <type> <id>" },
5397 { "getsrvids", getsrvids, false, false, "get a list of all server ids"},
5398 { "check_srvids", check_srvids, false, false, "check if a srvid exists", "<id>+" },
5399 { "vacuum", ctdb_vacuum, false, true, "vacuum the databases of empty records", "[max_records]"},
5400 { "repack", ctdb_repack, false, false, "repack all databases", "[max_freelist]"},
5401 { "listnodes", control_listnodes, false, true, "list all nodes in the cluster"},
5402 { "reloadnodes", control_reload_nodes_file, false, false, "reload the nodes file and restart the transport on all nodes"},
5403 { "moveip", control_moveip, false, false, "move/failover an ip address to another node", "<ip> <node>"},
5404 { "addip", control_addip, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
5405 { "delip", control_delip, false, false, "delete an ip address from a node", "<ip>"},
5406 { "eventscript", control_eventscript, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
5407 { "backupdb", control_backupdb, false, false, "backup the database into a file.", "<database> <file>"},
5408 { "restoredb", control_restoredb, false, false, "restore the database from a file.", "<file> [dbname]"},
5409 { "dumpdbbackup", control_dumpdbbackup, false, true, "dump database backup from a file.", "<file>"},
5410 { "wipedb", control_wipedb, false, false, "wipe the contents of a database.", "<dbname>"},
5411 { "recmaster", control_recmaster, false, false, "show the pnn for the recovery master."},
5412 { "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
5413 { "enablescript", control_enablescript, false, false, "enable an eventscript", "<script>"},
5414 { "disablescript", control_disablescript, false, false, "disable an eventscript", "<script>"},
5415 { "natgwlist", control_natgwlist, false, false, "show the nodes belonging to this natgw configuration"},
5416 { "xpnn", control_xpnn, true, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
5417 { "getreclock", control_getreclock, false, false, "Show the reclock file of a node"},
5418 { "setreclock", control_setreclock, false, false, "Set/clear the reclock file of a node", "[filename]"},
5419 { "setnatgwstate", control_setnatgwstate, false, false, "Set NATGW state to on/off", "{on|off}"},
5420 { "setlmasterrole", control_setlmasterrole, false, false, "Set LMASTER role to on/off", "{on|off}"},
5421 { "setrecmasterrole", control_setrecmasterrole, false, false, "Set RECMASTER role to on/off", "{on|off}"},
5422 { "setdbprio", control_setdbprio, false, false, "Set DB priority", "<dbid> <prio:1-3>"},
5423 { "getdbprio", control_getdbprio, false, false, "Get DB priority", "<dbid>"},
5424 { "setdbreadonly", control_setdbreadonly, false, false, "Set DB readonly capable", "<dbid>"},
5425 { "msglisten", control_msglisten, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
5426 { "msgsend", control_msgsend, false, false, "Send a message to srvid", "<srvid> <message>"},
5427 { "sync", control_ipreallocate, false, false, "wait until ctdbd has synced all state changes" },
5428 { "pfetch", control_pfetch, false, false, "fetch a record from a persistent database", "<db> <key> [<file>]" },
5429 { "pstore", control_pstore, false, false, "write a record to a persistent database", "<db> <key> <file containing record>" },
5430 { "tfetch", control_tfetch, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
5431 { "tstore", control_tstore, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
5432 { "readkey", control_readkey, true, false, "read the content off a database key", "<tdb-file> <key>" },
5433 { "writekey", control_writekey, true, false, "write to a database key", "<tdb-file> <key> <value>" },
5434 { "checktcpport", control_chktcpport, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
5435 { "getdbseqnum", control_getdbseqnum, false, false, "get the sequence number off a database", "<dbid>" },
5436 { "nodestatus", control_nodestatus, true, false, "show and return node status" },
5442 static void usage(void)
5446 "Usage: ctdb [options] <control>\n" \
5448 " -n <node> choose node number, or 'all' (defaults to local node)\n"
5449 " -Y generate machinereadable output\n"
5450 " -v generate verbose output\n"
5451 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options.timelimit);
5452 printf("Controls:\n");
5453 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5454 printf(" %-15s %-27s %s\n",
5455 ctdb_commands[i].name,
5456 ctdb_commands[i].args?ctdb_commands[i].args:"",
5457 ctdb_commands[i].msg);
5463 static void ctdb_alarm(int sig)
5465 printf("Maximum runtime exceeded - exiting\n");
5472 int main(int argc, const char *argv[])
5474 struct ctdb_context *ctdb;
5475 char *nodestring = NULL;
5476 struct poptOption popt_options[] = {
5479 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
5480 { "node", 'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
5481 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
5482 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
5483 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
5484 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
5485 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
5486 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
5487 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
5488 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
5492 const char **extra_argv;
5496 struct event_context *ev;
5497 const char *control;
5498 const char *socket_name;
5502 /* set some defaults */
5503 options.maxruntime = 0;
5504 options.timelimit = 3;
5505 options.pnn = CTDB_CURRENT_NODE;
5507 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
5509 while ((opt = poptGetNextOpt(pc)) != -1) {
5512 DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n",
5513 poptBadOption(pc, 0), poptStrerror(opt)));
5518 /* setup the remaining options for the main program to use */
5519 extra_argv = poptGetArgs(pc);
5522 while (extra_argv[extra_argc]) extra_argc++;
5525 if (extra_argc < 1) {
5529 if (options.maxruntime == 0) {
5530 const char *ctdb_timeout;
5531 ctdb_timeout = getenv("CTDB_TIMEOUT");
5532 if (ctdb_timeout != NULL) {
5533 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5535 /* default timeout is 120 seconds */
5536 options.maxruntime = 120;
5540 signal(SIGALRM, ctdb_alarm);
5541 alarm(options.maxruntime);
5543 control = extra_argv[0];
5545 ev = event_context_init(NULL);
5547 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5550 tevent_loop_allow_nesting(ev);
5552 for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5553 if (strcmp(control, ctdb_commands[i].name) == 0) {
5558 if (i == ARRAY_SIZE(ctdb_commands)) {
5559 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5563 if (ctdb_commands[i].without_daemon == true) {
5564 if (nodestring != NULL) {
5565 DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
5569 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
5572 /* initialise ctdb */
5573 ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
5576 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5580 /* initialize a libctdb connection as well */
5581 socket_name = ctdb_get_socketname(ctdb);
5582 ctdb_connection = ctdb_connect(socket_name,
5583 ctdb_log_file, stderr);
5584 if (ctdb_connection == NULL) {
5585 DEBUG(DEBUG_ERR, ("Failed to connect to daemon from libctdb\n"));
5589 /* setup the node number(s) to contact */
5590 if (!parse_nodestring(ctdb, nodestring, CTDB_CURRENT_NODE, false,
5591 &options.nodes, &options.pnn)) {
5595 if (options.pnn == CTDB_CURRENT_NODE) {
5596 options.pnn = options.nodes[0];
5599 if (ctdb_commands[i].auto_all &&
5600 ((options.pnn == CTDB_BROADCAST_ALL) ||
5601 (options.pnn == CTDB_MULTICAST))) {
5605 for (j = 0; j < talloc_array_length(options.nodes); j++) {
5606 options.pnn = options.nodes[j];
5607 ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5610 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5613 ctdb_disconnect(ctdb_connection);
5615 (void)poptFreeContext(pc);