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 "system/time.h"
23 #include "system/filesys.h"
24 #include "system/network.h"
25 #include "system/locale.h"
29 /* Allow use of deprecated function tevent_loop_allow_nesting() */
30 #define TEVENT_DEPRECATED
34 #include "lib/tdb_wrap/tdb_wrap.h"
35 #include "lib/util/dlinklist.h"
36 #include "lib/util/debug.h"
37 #include "lib/util/substitute.h"
38 #include "lib/util/time.h"
40 #include "ctdb_version.h"
41 #include "ctdb_private.h"
42 #include "ctdb_client.h"
44 #include "common/cmdline.h"
45 #include "common/rb_tree.h"
46 #include "common/system.h"
47 #include "common/common.h"
48 #include "common/logging.h"
50 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
51 #define ERR_NONODE 21 /* node does not exist */
52 #define ERR_DISNODE 22 /* node is disconnected */
54 static void usage(void);
61 const char *machineseparator
;
64 int printemptyrecords
;
71 #define LONGTIMEOUT options.timelimit*10
73 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
74 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
76 static double timeval_delta(struct timeval
*tv2
, struct timeval
*tv
)
78 return (tv2
->tv_sec
- tv
->tv_sec
) +
79 (tv2
->tv_usec
- tv
->tv_usec
)*1.0e-6;
82 static int control_version(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
84 printf("CTDB version: %s\n", CTDB_VERSION_STRING
);
88 /* Like printf(3) but substitute for separator in format */
89 static int printm(const char *format
, ...) PRINTF_ATTRIBUTE(1,2);
90 static int printm(const char *format
, ...)
94 size_t len
= strlen(format
);
95 char new_format
[len
+1];
97 strcpy(new_format
, format
);
99 if (options
.machineseparator
[0] != ':') {
100 all_string_sub(new_format
,
101 ":", options
.machineseparator
, len
+ 1);
104 va_start(ap
, format
);
105 ret
= vprintf(new_format
, ap
);
111 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
112 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
113 "Out of memory in " __location__ )); \
117 static uint32_t getpnn(struct ctdb_context
*ctdb
)
119 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
120 (options
.pnn
== CTDB_MULTICAST
)) {
122 ("Cannot get PNN for node %u\n", options
.pnn
));
126 if (options
.pnn
== CTDB_CURRENT_NODE
) {
127 return ctdb_get_pnn(ctdb
);
133 static void assert_single_node_only(void)
135 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
136 (options
.pnn
== CTDB_MULTICAST
)) {
138 ("This control can not be applied to multiple PNNs\n"));
143 static void assert_current_node_only(struct ctdb_context
*ctdb
)
145 if (options
.pnn
!= ctdb_get_pnn(ctdb
)) {
147 ("This control can only be applied to the current node\n"));
152 /* Pretty print the flags to a static buffer in human-readable format.
153 * This never returns NULL!
155 static const char *pretty_print_flags(uint32_t flags
)
158 static const struct {
162 { NODE_FLAGS_DISCONNECTED
, "DISCONNECTED" },
163 { NODE_FLAGS_PERMANENTLY_DISABLED
, "DISABLED" },
164 { NODE_FLAGS_BANNED
, "BANNED" },
165 { NODE_FLAGS_UNHEALTHY
, "UNHEALTHY" },
166 { NODE_FLAGS_DELETED
, "DELETED" },
167 { NODE_FLAGS_STOPPED
, "STOPPED" },
168 { NODE_FLAGS_INACTIVE
, "INACTIVE" },
170 static char flags_str
[512]; /* Big enough to contain all flag names */
173 for (j
=0;j
<ARRAY_SIZE(flag_names
);j
++) {
174 if (flags
& flag_names
[j
].flag
) {
175 if (flags_str
[0] == '\0') {
176 (void) strcpy(flags_str
, flag_names
[j
].name
);
178 (void) strncat(flags_str
, "|", sizeof(flags_str
)-1);
179 (void) strncat(flags_str
, flag_names
[j
].name
,
180 sizeof(flags_str
)-1);
184 if (flags_str
[0] == '\0') {
185 (void) strcpy(flags_str
, "OK");
191 static int h2i(char h
)
193 if (h
>= 'a' && h
<= 'f') return h
- 'a' + 10;
194 if (h
>= 'A' && h
<= 'F') return h
- 'f' + 10;
198 static TDB_DATA
hextodata(TALLOC_CTX
*mem_ctx
, const char *str
, size_t len
)
201 TDB_DATA key
= {NULL
, 0};
204 DEBUG(DEBUG_ERR
,("Key specified with odd number of hexadecimal digits\n"));
209 key
.dptr
= talloc_size(mem_ctx
, key
.dsize
);
211 for (i
=0; i
< len
/2; i
++) {
212 key
.dptr
[i
] = h2i(str
[i
*2]) << 4 | h2i(str
[i
*2+1]);
217 static TDB_DATA
strtodata(TALLOC_CTX
*mem_ctx
, const char *str
, size_t len
)
221 if (!strncmp(str
, "0x", 2)) {
222 key
= hextodata(mem_ctx
, str
+ 2, len
- 2);
224 key
.dptr
= talloc_memdup(mem_ctx
, str
, len
);
231 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
232 * that are disconnected or deleted. If dd_ok is true those nodes are
233 * included in the output list of nodes. If dd_ok is false, those
234 * nodes are filtered from the "all" case and cause an error if
235 * explicitly specified.
237 static bool parse_nodestring(struct ctdb_context
*ctdb
,
239 const char * nodestring
,
240 uint32_t current_pnn
,
245 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
248 struct ctdb_node_map_old
*nodemap
;
253 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
255 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
256 talloc_free(tmp_ctx
);
260 if (nodestring
!= NULL
) {
261 *nodes
= talloc_array(mem_ctx
, uint32_t, 0);
262 if (*nodes
== NULL
) {
268 if (strcmp(nodestring
, "all") == 0) {
269 *pnn_mode
= CTDB_BROADCAST_ALL
;
272 for (i
= 0; i
< nodemap
->num
; i
++) {
273 if ((nodemap
->nodes
[i
].flags
&
274 (NODE_FLAGS_DISCONNECTED
|
275 NODE_FLAGS_DELETED
)) && !dd_ok
) {
278 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
280 if (*nodes
== NULL
) {
290 ns
= talloc_strdup(tmp_ctx
, nodestring
);
291 tok
= strtok(ns
, ",");
292 while (tok
!= NULL
) {
295 i
= (uint32_t)strtoul(tok
, &endptr
, 0);
296 if (i
== 0 && tok
== endptr
) {
298 ("Invalid node %s\n", tok
));
299 talloc_free(tmp_ctx
);
302 if (i
>= nodemap
->num
) {
303 DEBUG(DEBUG_ERR
, ("Node %u does not exist\n", i
));
304 talloc_free(tmp_ctx
);
307 if ((nodemap
->nodes
[i
].flags
&
308 (NODE_FLAGS_DISCONNECTED
|
309 NODE_FLAGS_DELETED
)) && !dd_ok
) {
310 DEBUG(DEBUG_ERR
, ("Node %u has status %s\n", i
, pretty_print_flags(nodemap
->nodes
[i
].flags
)));
311 talloc_free(tmp_ctx
);
314 if ((pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), i
)) < 0) {
315 DEBUG(DEBUG_ERR
, ("Can not access node %u. Node is not operational.\n", i
));
316 talloc_free(tmp_ctx
);
320 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
322 if (*nodes
== NULL
) {
329 tok
= strtok(NULL
, ",");
334 *pnn_mode
= (*nodes
)[0];
336 *pnn_mode
= CTDB_MULTICAST
;
340 /* default - no nodes specified */
341 *nodes
= talloc_array(mem_ctx
, uint32_t, 1);
342 if (*nodes
== NULL
) {
345 *pnn_mode
= CTDB_CURRENT_NODE
;
347 if (((*nodes
)[0] = ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), current_pnn
)) < 0) {
352 talloc_free(tmp_ctx
);
356 talloc_free(tmp_ctx
);
361 check if a database exists
363 static bool db_exists(struct ctdb_context
*ctdb
, const char *dbarg
,
364 uint32_t *dbid
, const char **dbname
, uint8_t *flags
)
367 struct ctdb_dbid_map_old
*dbmap
=NULL
;
368 bool dbid_given
= false, found
= false;
370 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
371 const char *name
= NULL
;
373 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &dbmap
);
375 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
379 if (strncmp(dbarg
, "0x", 2) == 0) {
380 id
= strtoul(dbarg
, NULL
, 0);
384 for(i
=0; i
<dbmap
->num
; i
++) {
386 if (id
== dbmap
->dbs
[i
].db_id
) {
391 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].db_id
, tmp_ctx
, &name
);
393 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].db_id
));
397 if (strcmp(name
, dbarg
) == 0) {
398 id
= dbmap
->dbs
[i
].db_id
;
405 if (found
&& dbid_given
&& dbname
!= NULL
) {
406 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].db_id
, tmp_ctx
, &name
);
408 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].db_id
));
415 if (dbid
) *dbid
= id
;
416 if (dbname
) *dbname
= talloc_strdup(ctdb
, name
);
417 if (flags
) *flags
= dbmap
->dbs
[i
].flags
;
419 DEBUG(DEBUG_ERR
,("No database matching '%s' found\n", dbarg
));
423 talloc_free(tmp_ctx
);
428 see if a process exists
430 static int control_process_exists(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
438 if (sscanf(argv
[0], "%u:%u", &pnn
, &pid
) != 2) {
439 DEBUG(DEBUG_ERR
, ("Badly formed pnn:pid\n"));
443 ret
= ctdb_ctrl_process_exists(ctdb
, pnn
, pid
);
445 printf("%u:%u exists\n", pnn
, pid
);
447 printf("%u:%u does not exist\n", pnn
, pid
);
453 display statistics structure
455 static void show_statistics(struct ctdb_statistics
*s
, int show_header
)
457 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
459 const char *prefix
=NULL
;
461 int tmp
, days
, hours
, minutes
, seconds
;
466 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
467 STATISTICS_FIELD(num_clients
),
468 STATISTICS_FIELD(frozen
),
469 STATISTICS_FIELD(recovering
),
470 STATISTICS_FIELD(num_recoveries
),
471 STATISTICS_FIELD(client_packets_sent
),
472 STATISTICS_FIELD(client_packets_recv
),
473 STATISTICS_FIELD(node_packets_sent
),
474 STATISTICS_FIELD(node_packets_recv
),
475 STATISTICS_FIELD(keepalive_packets_sent
),
476 STATISTICS_FIELD(keepalive_packets_recv
),
477 STATISTICS_FIELD(node
.req_call
),
478 STATISTICS_FIELD(node
.reply_call
),
479 STATISTICS_FIELD(node
.req_dmaster
),
480 STATISTICS_FIELD(node
.reply_dmaster
),
481 STATISTICS_FIELD(node
.reply_error
),
482 STATISTICS_FIELD(node
.req_message
),
483 STATISTICS_FIELD(node
.req_control
),
484 STATISTICS_FIELD(node
.reply_control
),
485 STATISTICS_FIELD(client
.req_call
),
486 STATISTICS_FIELD(client
.req_message
),
487 STATISTICS_FIELD(client
.req_control
),
488 STATISTICS_FIELD(timeouts
.call
),
489 STATISTICS_FIELD(timeouts
.control
),
490 STATISTICS_FIELD(timeouts
.traverse
),
491 STATISTICS_FIELD(locks
.num_calls
),
492 STATISTICS_FIELD(locks
.num_current
),
493 STATISTICS_FIELD(locks
.num_pending
),
494 STATISTICS_FIELD(locks
.num_failed
),
495 STATISTICS_FIELD(total_calls
),
496 STATISTICS_FIELD(pending_calls
),
497 STATISTICS_FIELD(childwrite_calls
),
498 STATISTICS_FIELD(pending_childwrite_calls
),
499 STATISTICS_FIELD(memory_used
),
500 STATISTICS_FIELD(max_hop_count
),
501 STATISTICS_FIELD(total_ro_delegations
),
502 STATISTICS_FIELD(total_ro_revokes
),
505 tmp
= s
->statistics_current_time
.tv_sec
- s
->statistics_start_time
.tv_sec
;
514 if (options
.machinereadable
){
516 printm("CTDB version:");
517 printm("Current time of statistics:");
518 printm("Statistics collected since:");
519 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
520 printm("%s:", fields
[i
].name
);
522 printm("num_reclock_ctdbd_latency:");
523 printm("min_reclock_ctdbd_latency:");
524 printm("avg_reclock_ctdbd_latency:");
525 printm("max_reclock_ctdbd_latency:");
527 printm("num_reclock_recd_latency:");
528 printm("min_reclock_recd_latency:");
529 printm("avg_reclock_recd_latency:");
530 printm("max_reclock_recd_latency:");
532 printm("num_call_latency:");
533 printm("min_call_latency:");
534 printm("avg_call_latency:");
535 printm("max_call_latency:");
537 printm("num_lockwait_latency:");
538 printm("min_lockwait_latency:");
539 printm("avg_lockwait_latency:");
540 printm("max_lockwait_latency:");
542 printm("num_childwrite_latency:");
543 printm("min_childwrite_latency:");
544 printm("avg_childwrite_latency:");
545 printm("max_childwrite_latency:");
548 printm("%d:", CTDB_PROTOCOL
);
549 printm("%d:", (int)s
->statistics_current_time
.tv_sec
);
550 printm("%d:", (int)s
->statistics_start_time
.tv_sec
);
551 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
552 printm("%d:", *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
554 printm("%d:", s
->reclock
.ctdbd
.num
);
555 printm("%.6f:", s
->reclock
.ctdbd
.min
);
556 printm("%.6f:", s
->reclock
.ctdbd
.num
?s
->reclock
.ctdbd
.total
/s
->reclock
.ctdbd
.num
:0.0);
557 printm("%.6f:", s
->reclock
.ctdbd
.max
);
559 printm("%d:", s
->reclock
.recd
.num
);
560 printm("%.6f:", s
->reclock
.recd
.min
);
561 printm("%.6f:", s
->reclock
.recd
.num
?s
->reclock
.recd
.total
/s
->reclock
.recd
.num
:0.0);
562 printm("%.6f:", s
->reclock
.recd
.max
);
564 printm("%d:", s
->call_latency
.num
);
565 printm("%.6f:", s
->call_latency
.min
);
566 printm("%.6f:", s
->call_latency
.num
?s
->call_latency
.total
/s
->call_latency
.num
:0.0);
567 printm("%.6f:", s
->call_latency
.max
);
569 printm("%d:", s
->childwrite_latency
.num
);
570 printm("%.6f:", s
->childwrite_latency
.min
);
571 printm("%.6f:", s
->childwrite_latency
.num
?s
->childwrite_latency
.total
/s
->childwrite_latency
.num
:0.0);
572 printm("%.6f:", s
->childwrite_latency
.max
);
575 printf("CTDB version %u\n", CTDB_PROTOCOL
);
576 printf("Current time of statistics : %s", ctime(&s
->statistics_current_time
.tv_sec
));
577 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&s
->statistics_start_time
.tv_sec
));
579 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
580 if (strchr(fields
[i
].name
, '.')) {
581 preflen
= strcspn(fields
[i
].name
, ".")+1;
582 if (!prefix
|| strncmp(prefix
, fields
[i
].name
, preflen
) != 0) {
583 prefix
= fields
[i
].name
;
584 printf(" %*.*s\n", preflen
-1, preflen
-1, fields
[i
].name
);
589 printf(" %*s%-22s%*s%10u\n",
591 fields
[i
].name
+preflen
,
593 *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
595 printf(" hop_count_buckets:");
596 for (i
=0;i
<MAX_COUNT_BUCKETS
;i
++) {
597 printf(" %d", s
->hop_count_bucket
[i
]);
600 printf(" lock_buckets:");
601 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
602 printf(" %d", s
->locks
.buckets
[i
]);
605 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n", "locks_latency MIN/AVG/MAX", s
->locks
.latency
.min
, s
->locks
.latency
.num
?s
->locks
.latency
.total
/s
->locks
.latency
.num
:0.0, s
->locks
.latency
.max
, s
->locks
.latency
.num
);
607 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
);
609 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
);
611 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
);
612 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
);
615 talloc_free(tmp_ctx
);
619 display remote ctdb statistics combined from all nodes
621 static int control_statistics_all(struct ctdb_context
*ctdb
)
624 struct ctdb_statistics statistics
;
628 nodes
= ctdb_get_connected_nodes(ctdb
, TIMELIMIT(), ctdb
, &num_nodes
);
629 CTDB_NO_MEMORY(ctdb
, nodes
);
631 ZERO_STRUCT(statistics
);
633 for (i
=0;i
<num_nodes
;i
++) {
634 struct ctdb_statistics s1
;
636 uint32_t *v1
= (uint32_t *)&s1
;
637 uint32_t *v2
= (uint32_t *)&statistics
;
639 offsetof(struct ctdb_statistics
, __last_counter
) / sizeof(uint32_t);
640 ret
= ctdb_ctrl_statistics(ctdb
, nodes
[i
], &s1
);
642 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", nodes
[i
]));
645 for (j
=0;j
<num_ints
;j
++) {
648 statistics
.max_hop_count
=
649 MAX(statistics
.max_hop_count
, s1
.max_hop_count
);
650 statistics
.call_latency
.max
=
651 MAX(statistics
.call_latency
.max
, s1
.call_latency
.max
);
654 printf("Gathered statistics for %u nodes\n", num_nodes
);
655 show_statistics(&statistics
, 1);
660 display remote ctdb statistics
662 static int control_statistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
665 struct ctdb_statistics statistics
;
667 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
668 return control_statistics_all(ctdb
);
671 ret
= ctdb_ctrl_statistics(ctdb
, options
.pnn
, &statistics
);
673 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", options
.pnn
));
676 show_statistics(&statistics
, 1);
682 reset remote ctdb statistics
684 static int control_statistics_reset(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
688 ret
= ctdb_statistics_reset(ctdb
, options
.pnn
);
690 DEBUG(DEBUG_ERR
, ("Unable to reset statistics on node %u\n", options
.pnn
));
698 display remote ctdb rolling statistics
700 static int control_stats(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
703 struct ctdb_statistics_list_old
*stats
;
704 int i
, num_records
= -1;
706 assert_single_node_only();
709 num_records
= atoi(argv
[0]) - 1;
712 ret
= ctdb_ctrl_getstathistory(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &stats
);
714 DEBUG(DEBUG_ERR
, ("Unable to get rolling statistics from node %u\n", options
.pnn
));
717 for (i
=0;i
<stats
->num
;i
++) {
718 if (stats
->stats
[i
].statistics_start_time
.tv_sec
== 0) {
721 show_statistics(&stats
->stats
[i
], i
==0);
722 if (i
== num_records
) {
731 display remote ctdb db statistics
733 static int control_dbstatistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
735 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
736 struct ctdb_db_statistics_old
*dbstat
;
746 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
750 ret
= ctdb_ctrl_dbstatistics(ctdb
, options
.pnn
, db_id
, tmp_ctx
, &dbstat
);
752 DEBUG(DEBUG_ERR
,("Failed to read db statistics from node\n"));
753 talloc_free(tmp_ctx
);
757 printf("DB Statistics: %s\n", argv
[0]);
758 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
759 dbstat
->db_ro_delegations
);
760 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
761 dbstat
->db_ro_delegations
);
762 printf(" %s\n", "locks");
763 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
764 dbstat
->locks
.num_calls
);
765 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
766 dbstat
->locks
.num_failed
);
767 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
768 dbstat
->locks
.num_current
);
769 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
770 dbstat
->locks
.num_pending
);
771 printf(" %s", "hop_count_buckets:");
772 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
773 printf(" %d", dbstat
->hop_count_bucket
[i
]);
776 printf(" %s", "lock_buckets:");
777 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
778 printf(" %d", dbstat
->locks
.buckets
[i
]);
781 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
782 "locks_latency MIN/AVG/MAX",
783 dbstat
->locks
.latency
.min
,
784 (dbstat
->locks
.latency
.num
?
785 dbstat
->locks
.latency
.total
/dbstat
->locks
.latency
.num
:
787 dbstat
->locks
.latency
.max
,
788 dbstat
->locks
.latency
.num
);
789 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
790 "vacuum_latency MIN/AVG/MAX",
791 dbstat
->vacuum
.latency
.min
,
792 (dbstat
->vacuum
.latency
.num
?
793 dbstat
->vacuum
.latency
.total
/dbstat
->vacuum
.latency
.num
:
795 dbstat
->vacuum
.latency
.max
,
796 dbstat
->vacuum
.latency
.num
);
798 for (i
=0; i
<dbstat
->num_hot_keys
; i
++) {
799 if (dbstat
->hot_keys
[i
].count
> 0) {
803 dbstat
->num_hot_keys
= num_hot_keys
;
805 printf(" Num Hot Keys: %d\n", dbstat
->num_hot_keys
);
806 for (i
= 0; i
< dbstat
->num_hot_keys
; i
++) {
808 printf(" Count:%d Key:", dbstat
->hot_keys
[i
].count
);
809 for (j
= 0; j
< dbstat
->hot_keys
[i
].key
.dsize
; j
++) {
810 printf("%02x", dbstat
->hot_keys
[i
].key
.dptr
[j
]&0xff);
815 talloc_free(tmp_ctx
);
820 display uptime of remote node
822 static int control_uptime(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
825 struct ctdb_uptime
*uptime
= NULL
;
826 int tmp
, days
, hours
, minutes
, seconds
;
828 ret
= ctdb_ctrl_uptime(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &uptime
);
830 DEBUG(DEBUG_ERR
, ("Unable to get uptime from node %u\n", options
.pnn
));
834 if (options
.machinereadable
){
835 printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
836 printm(":%u:%u:%u:%lf\n",
837 (unsigned int)uptime
->current_time
.tv_sec
,
838 (unsigned int)uptime
->ctdbd_start_time
.tv_sec
,
839 (unsigned int)uptime
->last_recovery_finished
.tv_sec
,
840 timeval_delta(&uptime
->last_recovery_finished
,
841 &uptime
->last_recovery_started
)
846 printf("Current time of node : %s", ctime(&uptime
->current_time
.tv_sec
));
848 tmp
= uptime
->current_time
.tv_sec
- uptime
->ctdbd_start_time
.tv_sec
;
856 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->ctdbd_start_time
.tv_sec
));
858 tmp
= uptime
->current_time
.tv_sec
- uptime
->last_recovery_finished
.tv_sec
;
866 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->last_recovery_finished
.tv_sec
));
868 printf("Duration of last recovery/failover: %lf seconds\n",
869 timeval_delta(&uptime
->last_recovery_finished
,
870 &uptime
->last_recovery_started
));
876 show the PNN of the current node
878 static int control_pnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
882 mypnn
= getpnn(ctdb
);
884 printf("PNN:%d\n", mypnn
);
889 static struct ctdb_node_map_old
*read_nodes_file(TALLOC_CTX
*mem_ctx
)
891 const char *nodes_list
;
893 /* read the nodes file */
894 nodes_list
= getenv("CTDB_NODES");
895 if (nodes_list
== NULL
) {
896 nodes_list
= talloc_asprintf(mem_ctx
, "%s/nodes",
897 getenv("CTDB_BASE"));
898 if (nodes_list
== NULL
) {
899 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
904 return ctdb_read_nodes_file(mem_ctx
, nodes_list
);
908 show the PNN of the current node
909 discover the pnn by loading the nodes file and try to bind to all
910 addresses one at a time until the ip address is found.
912 static int find_node_xpnn(void)
914 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
915 struct ctdb_node_map_old
*node_map
;
918 node_map
= read_nodes_file(mem_ctx
);
919 if (node_map
== NULL
) {
920 talloc_free(mem_ctx
);
924 for (i
= 0; i
< node_map
->num
; i
++) {
925 if (node_map
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
928 if (ctdb_sys_have_ip(&node_map
->nodes
[i
].addr
)) {
929 pnn
= node_map
->nodes
[i
].pnn
;
930 talloc_free(mem_ctx
);
935 printf("Failed to detect which PNN this node is\n");
936 talloc_free(mem_ctx
);
940 static int control_xpnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
944 assert_single_node_only();
946 pnn
= find_node_xpnn();
951 printf("PNN:%d\n", pnn
);
955 /* Helpers for ctdb status
957 static bool is_partially_online(struct ctdb_context
*ctdb
, struct ctdb_node_and_flags
*node
)
959 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
963 if (node
->flags
== 0) {
964 struct ctdb_iface_list_old
*ifaces
;
966 if (ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), node
->pnn
,
967 tmp_ctx
, &ifaces
) == 0) {
968 for (j
=0; j
< ifaces
->num
; j
++) {
969 if (ifaces
->ifaces
[j
].link_state
!= 0) {
977 talloc_free(tmp_ctx
);
982 static void control_status_header_machine(void)
984 printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
985 ":Inactive:PartiallyOnline:ThisNode:\n");
988 static int control_status_1_machine(struct ctdb_context
*ctdb
, int mypnn
,
989 struct ctdb_node_and_flags
*node
)
991 printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node
->pnn
,
992 ctdb_addr_to_str(&node
->addr
),
993 !!(node
->flags
&NODE_FLAGS_DISCONNECTED
),
994 !!(node
->flags
&NODE_FLAGS_BANNED
),
995 !!(node
->flags
&NODE_FLAGS_PERMANENTLY_DISABLED
),
996 !!(node
->flags
&NODE_FLAGS_UNHEALTHY
),
997 !!(node
->flags
&NODE_FLAGS_STOPPED
),
998 !!(node
->flags
&NODE_FLAGS_INACTIVE
),
999 is_partially_online(ctdb
, node
) ? 1 : 0,
1000 (node
->pnn
== mypnn
)?'Y':'N');
1005 static int control_status_1_human(struct ctdb_context
*ctdb
, int mypnn
,
1006 struct ctdb_node_and_flags
*node
)
1008 printf("pnn:%d %-16s %s%s\n", node
->pnn
,
1009 ctdb_addr_to_str(&node
->addr
),
1010 is_partially_online(ctdb
, node
) ? "PARTIALLYONLINE" : pretty_print_flags(node
->flags
),
1011 node
->pnn
== mypnn
?" (THIS NODE)":"");
1017 display remote ctdb status
1019 static int control_status(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1021 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1023 struct ctdb_vnn_map
*vnnmap
=NULL
;
1024 struct ctdb_node_map_old
*nodemap
=NULL
;
1025 uint32_t recmode
, recmaster
, mypnn
;
1026 int num_deleted_nodes
= 0;
1029 mypnn
= getpnn(ctdb
);
1031 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1033 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1034 talloc_free(tmp_ctx
);
1038 if (options
.machinereadable
) {
1039 control_status_header_machine();
1040 for (i
=0;i
<nodemap
->num
;i
++) {
1041 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1044 (void) control_status_1_machine(ctdb
, mypnn
,
1045 &nodemap
->nodes
[i
]);
1047 talloc_free(tmp_ctx
);
1051 for (i
=0; i
<nodemap
->num
; i
++) {
1052 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1053 num_deleted_nodes
++;
1056 if (num_deleted_nodes
== 0) {
1057 printf("Number of nodes:%d\n", nodemap
->num
);
1059 printf("Number of nodes:%d (including %d deleted nodes)\n",
1060 nodemap
->num
, num_deleted_nodes
);
1062 for(i
=0;i
<nodemap
->num
;i
++){
1063 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1066 (void) control_status_1_human(ctdb
, mypnn
, &nodemap
->nodes
[i
]);
1069 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
1071 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
1072 talloc_free(tmp_ctx
);
1075 if (vnnmap
->generation
== INVALID_GENERATION
) {
1076 printf("Generation:INVALID\n");
1078 printf("Generation:%d\n",vnnmap
->generation
);
1080 printf("Size:%d\n",vnnmap
->size
);
1081 for(i
=0;i
<vnnmap
->size
;i
++){
1082 printf("hash:%d lmaster:%d\n", i
, vnnmap
->map
[i
]);
1085 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmode
);
1087 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
1088 talloc_free(tmp_ctx
);
1091 printf("Recovery mode:%s (%d)\n",recmode
==CTDB_RECOVERY_NORMAL
?"NORMAL":"RECOVERY",recmode
);
1093 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmaster
);
1095 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1096 talloc_free(tmp_ctx
);
1099 printf("Recovery master:%d\n",recmaster
);
1101 talloc_free(tmp_ctx
);
1105 static int control_nodestatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1107 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1109 struct ctdb_node_map_old
*nodemap
=NULL
;
1111 uint32_t pnn_mode
, mypnn
;
1117 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1118 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1122 if (options
.machinereadable
) {
1123 control_status_header_machine();
1124 } else if (pnn_mode
== CTDB_BROADCAST_ALL
) {
1125 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes
));
1128 mypnn
= getpnn(ctdb
);
1130 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1132 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1133 talloc_free(tmp_ctx
);
1139 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1140 if (options
.machinereadable
) {
1141 ret
|= control_status_1_machine(ctdb
, mypnn
,
1142 &nodemap
->nodes
[nodes
[i
]]);
1144 ret
|= control_status_1_human(ctdb
, mypnn
,
1145 &nodemap
->nodes
[nodes
[i
]]);
1149 talloc_free(tmp_ctx
);
1153 /* talloc off the existing nodemap... */
1154 static struct ctdb_node_map_old
*talloc_nodemap(struct ctdb_node_map_old
*nodemap
)
1156 return talloc_zero_size(nodemap
,
1157 offsetof(struct ctdb_node_map_old
, nodes
) +
1158 nodemap
->num
* sizeof(struct ctdb_node_and_flags
));
1161 static struct ctdb_node_map_old
*
1162 filter_nodemap_by_capabilities(struct ctdb_context
*ctdb
,
1163 struct ctdb_node_map_old
*nodemap
,
1164 uint32_t required_capabilities
,
1168 uint32_t capabilities
;
1169 struct ctdb_node_map_old
*ret
;
1171 ret
= talloc_nodemap(nodemap
);
1172 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1176 for (i
= 0; i
< nodemap
->num
; i
++) {
1179 /* Disconnected nodes have no capabilities! */
1180 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
1184 res
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(),
1185 nodemap
->nodes
[i
].pnn
,
1188 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n",
1189 nodemap
->nodes
[i
].pnn
));
1193 if (!(capabilities
& required_capabilities
)) {
1197 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1207 static struct ctdb_node_map_old
*
1208 filter_nodemap_by_flags(struct ctdb_context
*ctdb
,
1209 struct ctdb_node_map_old
*nodemap
,
1210 uint32_t flags_mask
)
1213 struct ctdb_node_map_old
*ret
;
1215 ret
= talloc_nodemap(nodemap
);
1216 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1220 for (i
= 0; i
< nodemap
->num
; i
++) {
1221 if (nodemap
->nodes
[i
].flags
& flags_mask
) {
1225 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1233 display the list of nodes belonging to this natgw configuration
1235 static int control_natgwlist(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1237 static char prog
[PATH_MAX
+1] = "";
1243 if (!ctdb_set_helper("NAT gateway helper", prog
, sizeof(prog
),
1244 "CTDB_NATGW_HELPER", CTDB_HELPER_BINDIR
,
1246 DEBUG(DEBUG_ERR
, ("Unable to set NAT gateway helper\n"));
1250 execl(prog
, prog
, "natgwlist", NULL
);
1253 ("Unable to run NAT gateway helper %s\n", strerror(errno
)));
1258 display the status of the scripts for monitoring (or other events)
1260 static int control_one_scriptstatus(struct ctdb_context
*ctdb
,
1261 enum ctdb_event type
)
1263 struct ctdb_script_list_old
*script_status
;
1266 ret
= ctdb_ctrl_getscriptstatus(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, type
, &script_status
);
1268 DEBUG(DEBUG_ERR
, ("Unable to get script status from node %u\n", options
.pnn
));
1272 if (script_status
== NULL
) {
1273 if (!options
.machinereadable
) {
1274 printf("%s cycle never run\n",
1275 ctdb_eventscript_call_names
[type
]);
1280 if (!options
.machinereadable
) {
1282 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1283 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1287 printf("%d scripts were executed last %s cycle\n",
1289 ctdb_eventscript_call_names
[type
]);
1291 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1292 const char *status
= NULL
;
1294 /* The ETIME status is ignored for certain events.
1295 * In that case the status is 0, but endtime is not set.
1297 if (script_status
->scripts
[i
].status
== 0 &&
1298 timeval_is_zero(&script_status
->scripts
[i
].finished
)) {
1299 script_status
->scripts
[i
].status
= -ETIME
;
1302 switch (script_status
->scripts
[i
].status
) {
1304 status
= "TIMEDOUT";
1307 status
= "DISABLED";
1313 if (script_status
->scripts
[i
].status
> 0)
1317 if (options
.machinereadable
) {
1318 printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1319 ctdb_eventscript_call_names
[type
],
1320 script_status
->scripts
[i
].name
,
1321 script_status
->scripts
[i
].status
,
1323 (long)script_status
->scripts
[i
].start
.tv_sec
,
1324 (long)script_status
->scripts
[i
].start
.tv_usec
,
1325 (long)script_status
->scripts
[i
].finished
.tv_sec
,
1326 (long)script_status
->scripts
[i
].finished
.tv_usec
,
1327 script_status
->scripts
[i
].output
);
1331 printf("%-20s Status:%s ",
1332 script_status
->scripts
[i
].name
, status
);
1334 /* Some other error, eg from stat. */
1335 printf("%-20s Status:CANNOT RUN (%s)",
1336 script_status
->scripts
[i
].name
,
1337 strerror(-script_status
->scripts
[i
].status
));
1339 if (script_status
->scripts
[i
].status
>= 0) {
1340 printf("Duration:%.3lf ",
1341 timeval_delta(&script_status
->scripts
[i
].finished
,
1342 &script_status
->scripts
[i
].start
));
1344 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1346 ctime(&script_status
->scripts
[i
].start
.tv_sec
));
1347 if (script_status
->scripts
[i
].status
!= 0) {
1348 printf(" OUTPUT:%s\n",
1349 script_status
->scripts
[i
].output
);
1359 static int control_scriptstatus(struct ctdb_context
*ctdb
,
1360 int argc
, const char **argv
)
1363 enum ctdb_event type
, min
, max
;
1367 DEBUG(DEBUG_ERR
, ("Unknown arguments to scriptstatus\n"));
1372 arg
= ctdb_eventscript_call_names
[CTDB_EVENT_MONITOR
];
1376 for (type
= 0; type
< CTDB_EVENT_MAX
; type
++) {
1377 if (strcmp(arg
, ctdb_eventscript_call_names
[type
]) == 0) {
1383 if (type
== CTDB_EVENT_MAX
) {
1384 if (strcmp(arg
, "all") == 0) {
1386 max
= CTDB_EVENT_MAX
;
1388 DEBUG(DEBUG_ERR
, ("Unknown event type %s\n", argv
[0]));
1393 if (options
.machinereadable
) {
1394 printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1397 for (type
= min
; type
< max
; type
++) {
1398 ret
= control_one_scriptstatus(ctdb
, type
);
1408 enable an eventscript
1410 static int control_enablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1418 ret
= ctdb_ctrl_enablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1420 DEBUG(DEBUG_ERR
, ("Unable to enable script %s on node %u\n", argv
[0], options
.pnn
));
1428 disable an eventscript
1430 static int control_disablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1438 ret
= ctdb_ctrl_disablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1440 DEBUG(DEBUG_ERR
, ("Unable to disable script %s on node %u\n", argv
[0], options
.pnn
));
1448 display the pnn of the recovery master
1450 static int control_recmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1455 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
1457 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1460 printf("%d\n",recmaster
);
1466 add a tickle to a public address
1468 static int control_add_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1470 struct ctdb_connection t
;
1474 assert_single_node_only();
1480 if (parse_ip_port(argv
[0], &t
.src
) == 0) {
1481 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1484 if (parse_ip_port(argv
[1], &t
.dst
) == 0) {
1485 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1489 data
.dptr
= (uint8_t *)&t
;
1490 data
.dsize
= sizeof(t
);
1492 /* tell all nodes about this tcp connection */
1493 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE
,
1494 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1496 DEBUG(DEBUG_ERR
,("Failed to add tickle\n"));
1505 delete a tickle from a node
1507 static int control_del_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1509 struct ctdb_connection t
;
1513 assert_single_node_only();
1519 if (parse_ip_port(argv
[0], &t
.src
) == 0) {
1520 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1523 if (parse_ip_port(argv
[1], &t
.dst
) == 0) {
1524 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1528 data
.dptr
= (uint8_t *)&t
;
1529 data
.dsize
= sizeof(t
);
1531 /* tell all nodes about this tcp connection */
1532 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_REMOVE
,
1533 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1535 DEBUG(DEBUG_ERR
,("Failed to remove tickle\n"));
1544 get a list of all tickles for this pnn
1546 static int control_get_tickles(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1548 struct ctdb_tickle_list_old
*list
;
1549 ctdb_sock_addr addr
;
1553 assert_single_node_only();
1560 port
= atoi(argv
[1]);
1563 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1564 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1568 ret
= ctdb_ctrl_get_tcp_tickles(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &addr
, &list
);
1570 DEBUG(DEBUG_ERR
, ("Unable to list tickles\n"));
1574 if (options
.machinereadable
){
1575 printm(":source ip:port:destination ip:port:\n");
1576 for (i
=0;i
<list
->num
;i
++) {
1577 if (port
&& port
!= ntohs(list
->connections
[i
].dst
.ip
.sin_port
)) {
1580 printm(":%s:%u", ctdb_addr_to_str(&list
->connections
[i
].src
), ntohs(list
->connections
[i
].src
.ip
.sin_port
));
1581 printm(":%s:%u:\n", ctdb_addr_to_str(&list
->connections
[i
].dst
), ntohs(list
->connections
[i
].dst
.ip
.sin_port
));
1584 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list
->addr
));
1585 printf("Num tickles:%u\n", list
->num
);
1586 for (i
=0;i
<list
->num
;i
++) {
1587 if (port
&& port
!= ntohs(list
->connections
[i
].dst
.ip
.sin_port
)) {
1590 printf("SRC: %s:%u ", ctdb_addr_to_str(&list
->connections
[i
].src
), ntohs(list
->connections
[i
].src
.ip
.sin_port
));
1591 printf("DST: %s:%u\n", ctdb_addr_to_str(&list
->connections
[i
].dst
), ntohs(list
->connections
[i
].dst
.ip
.sin_port
));
1601 static int move_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1603 struct ctdb_public_ip_list_old
*ips
;
1604 struct ctdb_public_ip ip
;
1607 uint32_t disable_time
;
1609 struct ctdb_node_map_old
*nodemap
=NULL
;
1610 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1613 data
.dptr
= (uint8_t*)&disable_time
;
1614 data
.dsize
= sizeof(disable_time
);
1615 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1617 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1623 /* read the public ip list from the node */
1624 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), pnn
, ctdb
, &ips
);
1626 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", pnn
));
1627 talloc_free(tmp_ctx
);
1631 for (i
=0;i
<ips
->num
;i
++) {
1632 if (ctdb_same_ip(addr
, &ips
->ips
[i
].addr
)) {
1637 DEBUG(DEBUG_ERR
, ("Node %u can not host ip address '%s'\n",
1638 pnn
, ctdb_addr_to_str(addr
)));
1639 talloc_free(tmp_ctx
);
1646 data
.dptr
= (uint8_t *)&ip
;
1647 data
.dsize
= sizeof(ip
);
1649 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1651 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1652 talloc_free(tmp_ctx
);
1656 nodes
= list_of_nodes(ctdb
, nodemap
, tmp_ctx
, NODE_FLAGS_INACTIVE
, pnn
);
1657 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1664 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1665 talloc_free(tmp_ctx
);
1669 ret
= ctdb_ctrl_takeover_ip(ctdb
, LONGTIMELIMIT(), pnn
, &ip
);
1671 DEBUG(DEBUG_ERR
,("Failed to take over IP on node %d\n", pnn
));
1672 talloc_free(tmp_ctx
);
1676 /* update the recovery daemon so it now knows to expect the new
1677 node assignment for this ip.
1679 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_RECD_UPDATE_IP
, data
);
1681 DEBUG(DEBUG_ERR
,("Failed to send message to update the ip on the recovery master.\n"));
1685 talloc_free(tmp_ctx
);
1691 * scans all other nodes and returns a pnn for another node that can host this
1695 find_other_host_for_public_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1697 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1698 struct ctdb_public_ip_list_old
*ips
;
1699 struct ctdb_node_map_old
*nodemap
=NULL
;
1703 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1705 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1706 talloc_free(tmp_ctx
);
1710 for(i
=0;i
<nodemap
->num
;i
++){
1711 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
1714 if (nodemap
->nodes
[i
].pnn
== options
.pnn
) {
1718 /* read the public ip list from this node */
1719 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
1721 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
1725 for (j
=0;j
<ips
->num
;j
++) {
1726 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
1727 pnn
= nodemap
->nodes
[i
].pnn
;
1728 talloc_free(tmp_ctx
);
1735 talloc_free(tmp_ctx
);
1739 /* If pnn is -1 then try to find a node to move IP to... */
1740 static bool try_moveip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1742 bool pnn_specified
= (pnn
== -1 ? false : true);
1745 while (retries
< 5) {
1746 if (!pnn_specified
) {
1747 pnn
= find_other_host_for_public_ip(ctdb
, addr
);
1752 ("Trying to move public IP to node %u\n", pnn
));
1755 if (move_ip(ctdb
, addr
, pnn
) == 0) {
1768 move/failover an ip address to a specific node
1770 static int control_moveip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1773 ctdb_sock_addr addr
;
1775 assert_single_node_only();
1782 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1783 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1788 if (sscanf(argv
[1], "%u", &pnn
) != 1) {
1789 DEBUG(DEBUG_ERR
, ("Badly formed pnn\n"));
1793 if (!try_moveip(ctdb
, &addr
, pnn
)) {
1794 DEBUG(DEBUG_ERR
,("Failed to move IP to node %d.\n", pnn
));
1801 static int rebalance_node(struct ctdb_context
*ctdb
, uint32_t pnn
)
1805 data
.dptr
= (uint8_t *)&pnn
;
1806 data
.dsize
= sizeof(uint32_t);
1807 if (ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_REBALANCE_NODE
, data
) != 0) {
1809 ("Failed to send message to force node %u to be a rebalancing target\n",
1819 rebalance a node by setting it to allow failback and triggering a
1822 static int control_rebalancenode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1824 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1829 assert_single_node_only();
1835 /* Determine the nodes where IPs need to be reloaded */
1836 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1837 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1842 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1843 if (!rebalance_node(ctdb
, nodes
[i
])) {
1849 talloc_free(tmp_ctx
);
1853 static int rebalance_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1855 struct ctdb_public_ip ip
;
1858 uint32_t disable_time
;
1860 struct ctdb_node_map_old
*nodemap
=NULL
;
1861 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1864 data
.dptr
= (uint8_t*)&disable_time
;
1865 data
.dsize
= sizeof(disable_time
);
1866 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1868 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1875 data
.dptr
= (uint8_t *)&ip
;
1876 data
.dsize
= sizeof(ip
);
1878 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1880 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1881 talloc_free(tmp_ctx
);
1885 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
1886 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1893 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1894 talloc_free(tmp_ctx
);
1898 talloc_free(tmp_ctx
);
1903 release an ip form all nodes and have it re-assigned by recd
1905 static int control_rebalanceip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1907 ctdb_sock_addr addr
;
1909 assert_single_node_only();
1916 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1917 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1921 if (rebalance_ip(ctdb
, &addr
) != 0) {
1922 DEBUG(DEBUG_ERR
,("Error when trying to reassign ip\n"));
1929 static int getips_store_callback(void *param
, void *data
)
1931 struct ctdb_public_ip
*node_ip
= (struct ctdb_public_ip
*)data
;
1932 struct ctdb_public_ip_list_old
*ips
= param
;
1936 ips
->ips
[i
].pnn
= node_ip
->pnn
;
1937 ips
->ips
[i
].addr
= node_ip
->addr
;
1941 static int getips_count_callback(void *param
, void *data
)
1943 uint32_t *count
= param
;
1950 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
1952 static uint32_t key
[IP_KEYLEN
];
1954 bzero(key
, sizeof(key
));
1956 switch (ip
->sa
.sa_family
) {
1958 key
[0] = ip
->ip
.sin_addr
.s_addr
;
1961 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
1969 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
1976 static void *add_ip_callback(void *parm
, void *data
)
1982 control_get_all_public_ips(struct ctdb_context
*ctdb
, TALLOC_CTX
*tmp_ctx
, struct ctdb_public_ip_list_old
**ips
)
1984 struct ctdb_public_ip_list_old
*tmp_ips
;
1985 struct ctdb_node_map_old
*nodemap
=NULL
;
1986 trbt_tree_t
*ip_tree
;
1990 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1992 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1996 ip_tree
= trbt_create(tmp_ctx
, 0);
1998 for(i
=0;i
<nodemap
->num
;i
++){
1999 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
2002 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2006 /* read the public ip list from this node */
2007 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &tmp_ips
);
2009 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
2013 for (j
=0; j
<tmp_ips
->num
;j
++) {
2014 struct ctdb_public_ip
*node_ip
;
2016 node_ip
= talloc(tmp_ctx
, struct ctdb_public_ip
);
2017 node_ip
->pnn
= tmp_ips
->ips
[j
].pnn
;
2018 node_ip
->addr
= tmp_ips
->ips
[j
].addr
;
2020 trbt_insertarray32_callback(ip_tree
,
2021 IP_KEYLEN
, ip_key(&tmp_ips
->ips
[j
].addr
),
2025 talloc_free(tmp_ips
);
2030 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &count
);
2032 len
= offsetof(struct ctdb_public_ip_list_old
, ips
) +
2033 count
*sizeof(struct ctdb_public_ip
);
2034 tmp_ips
= talloc_zero_size(tmp_ctx
, len
);
2035 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_store_callback
, tmp_ips
);
2043 static void ctdb_every_second(struct tevent_context
*ev
,
2044 struct tevent_timer
*te
,
2045 struct timeval t
, void *p
)
2047 struct ctdb_context
*ctdb
= talloc_get_type(p
, struct ctdb_context
);
2049 tevent_add_timer(ctdb
->ev
, ctdb
, timeval_current_ofs(1, 0),
2050 ctdb_every_second
, ctdb
);
2053 struct srvid_reply_handler_data
{
2057 const char *srvid_str
;
2060 static void srvid_broadcast_reply_handler(uint64_t srvid
, TDB_DATA data
,
2063 struct srvid_reply_handler_data
*d
=
2064 (struct srvid_reply_handler_data
*)private_data
;
2068 if (data
.dsize
!= sizeof(ret
)) {
2069 DEBUG(DEBUG_ERR
, (__location__
" Wrong reply size\n"));
2073 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2074 ret
= *(int32_t *)data
.dptr
;
2077 ("%s failed with result %d\n", d
->srvid_str
, ret
));
2081 if (!d
->wait_for_all
) {
2086 /* Wait for all replies */
2088 for (i
= 0; i
< talloc_array_length(d
->nodes
); i
++) {
2089 if (d
->nodes
[i
] == ret
) {
2091 ("%s reply received from node %u\n",
2092 d
->srvid_str
, ret
));
2095 if (d
->nodes
[i
] != -1) {
2096 /* Found a node that hasn't yet replied */
2102 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2103 * or replies from all connected nodes. arg is the data argument to
2104 * pass in the srvid_request structure - pass 0 if this isn't needed.
2106 static int srvid_broadcast(struct ctdb_context
*ctdb
,
2107 uint64_t srvid
, uint32_t *arg
,
2108 const char *srvid_str
, bool wait_for_all
)
2113 uint64_t reply_srvid
;
2114 struct ctdb_srvid_message request
;
2115 struct ctdb_disable_message request_data
;
2116 struct srvid_reply_handler_data reply_data
;
2119 ZERO_STRUCT(request
);
2121 /* Time ticks to enable timeouts to be processed */
2122 tevent_add_timer(ctdb
->ev
, ctdb
, timeval_current_ofs(1, 0),
2123 ctdb_every_second
, ctdb
);
2125 pnn
= ctdb_get_pnn(ctdb
);
2126 reply_srvid
= getpid();
2130 request
.srvid
= reply_srvid
;
2132 data
.dptr
= (uint8_t *)&request
;
2133 data
.dsize
= sizeof(request
);
2135 request_data
.pnn
= pnn
;
2136 request_data
.srvid
= reply_srvid
;
2137 request_data
.timeout
= *arg
;
2139 data
.dptr
= (uint8_t *)&request_data
;
2140 data
.dsize
= sizeof(request_data
);
2143 /* Register message port for reply from recovery master */
2144 ctdb_client_set_message_handler(ctdb
, reply_srvid
,
2145 srvid_broadcast_reply_handler
,
2148 reply_data
.wait_for_all
= wait_for_all
;
2149 reply_data
.nodes
= NULL
;
2150 reply_data
.srvid_str
= srvid_str
;
2153 reply_data
.done
= false;
2156 struct ctdb_node_map_old
*nodemap
;
2158 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
2159 CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
2162 ("Unable to get nodemap from current node, try again\n"));
2167 if (reply_data
.nodes
!= NULL
) {
2168 talloc_free(reply_data
.nodes
);
2170 reply_data
.nodes
= list_of_connected_nodes(ctdb
, nodemap
,
2173 talloc_free(nodemap
);
2176 /* Send to all connected nodes. Only recmaster replies */
2177 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
,
2180 /* This can only happen if the socket is closed and
2181 * there's no way to recover from that, so don't try
2185 ("Failed to send %s request to connected nodes\n",
2190 tv
= timeval_current();
2191 /* This loop terminates the reply is received */
2192 while (timeval_elapsed(&tv
) < 5.0 && !reply_data
.done
) {
2193 tevent_loop_once(ctdb
->ev
);
2196 if (!reply_data
.done
) {
2198 ("Still waiting for confirmation of %s\n", srvid_str
));
2203 ctdb_client_remove_message_handler(ctdb
, reply_srvid
, &reply_data
);
2205 talloc_free(reply_data
.nodes
);
2210 static int ipreallocate(struct ctdb_context
*ctdb
)
2212 return srvid_broadcast(ctdb
, CTDB_SRVID_TAKEOVER_RUN
, NULL
,
2213 "IP reallocation", false);
2217 static int control_ipreallocate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2219 return ipreallocate(ctdb
);
2223 add a public ip address to a node
2225 static int control_addip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2228 int len
, retries
= 0;
2230 ctdb_sock_addr addr
;
2231 struct ctdb_addr_info_old
*pub
;
2232 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2233 struct ctdb_public_ip_list_old
*ips
;
2237 talloc_free(tmp_ctx
);
2241 if (!parse_ip_mask(argv
[0], argv
[1], &addr
, &mask
)) {
2242 DEBUG(DEBUG_ERR
, ("Badly formed ip/mask : %s\n", argv
[0]));
2243 talloc_free(tmp_ctx
);
2247 /* read the public ip list from the node */
2248 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2250 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", options
.pnn
));
2251 talloc_free(tmp_ctx
);
2254 for (i
=0;i
<ips
->num
;i
++) {
2255 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2256 DEBUG(DEBUG_ERR
,("Can not add ip to node. Node already hosts this ip\n"));
2263 /* Dont timeout. This command waits for an ip reallocation
2264 which sometimes can take wuite a while if there has
2265 been a recent recovery
2269 len
= offsetof(struct ctdb_addr_info_old
, iface
) + strlen(argv
[1]) + 1;
2270 pub
= talloc_size(tmp_ctx
, len
);
2271 CTDB_NO_MEMORY(ctdb
, pub
);
2275 pub
->len
= strlen(argv
[1])+1;
2276 memcpy(&pub
->iface
[0], argv
[1], strlen(argv
[1])+1);
2279 ret
= ctdb_ctrl_add_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, pub
);
2281 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options
.pnn
));
2285 } while (retries
< 5 && ret
!= 0);
2287 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Giving up.\n", options
.pnn
));
2288 talloc_free(tmp_ctx
);
2292 if (rebalance_node(ctdb
, options
.pnn
) != 0) {
2293 DEBUG(DEBUG_ERR
,("Error when trying to rebalance node\n"));
2297 talloc_free(tmp_ctx
);
2302 add a public ip address to a node
2304 static int control_ipiface(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2306 ctdb_sock_addr addr
;
2313 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2314 printf("Badly formed ip : %s\n", argv
[0]);
2318 iface
= ctdb_sys_find_ifname(&addr
);
2319 if (iface
== NULL
) {
2320 printf("Failed to get interface name for ip: %s", argv
[0]);
2324 printf("IP on interface %s\n", iface
);
2331 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
);
2333 static int control_delip_all(struct ctdb_context
*ctdb
, int argc
, const char **argv
, ctdb_sock_addr
*addr
)
2335 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2336 struct ctdb_node_map_old
*nodemap
=NULL
;
2337 struct ctdb_public_ip_list_old
*ips
;
2340 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2342 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from current node\n"));
2346 /* remove it from the nodes that are not hosting the ip currently */
2347 for(i
=0;i
<nodemap
->num
;i
++){
2348 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2351 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2353 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2357 for (j
=0;j
<ips
->num
;j
++) {
2358 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2366 if (ips
->ips
[j
].pnn
== nodemap
->nodes
[i
].pnn
) {
2370 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2371 control_delip(ctdb
, argc
, argv
);
2375 /* remove it from every node (also the one hosting it) */
2376 for(i
=0;i
<nodemap
->num
;i
++){
2377 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2380 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2382 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2386 for (j
=0;j
<ips
->num
;j
++) {
2387 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2395 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2396 control_delip(ctdb
, argc
, argv
);
2399 talloc_free(tmp_ctx
);
2404 delete a public ip address from a node
2406 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2409 ctdb_sock_addr addr
;
2410 struct ctdb_addr_info_old pub
;
2411 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2412 struct ctdb_public_ip_list_old
*ips
;
2415 talloc_free(tmp_ctx
);
2419 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2420 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2424 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2425 return control_delip_all(ctdb
, argc
, argv
, &addr
);
2432 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2434 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from cluster\n"));
2435 talloc_free(tmp_ctx
);
2439 for (i
=0;i
<ips
->num
;i
++) {
2440 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2446 DEBUG(DEBUG_ERR
, ("This node does not support this public address '%s'\n",
2447 ctdb_addr_to_str(&addr
)));
2448 talloc_free(tmp_ctx
);
2452 /* This is an optimisation. If this node is hosting the IP
2453 * then try to move it somewhere else without invoking a full
2454 * takeover run. We don't care if this doesn't work!
2456 if (ips
->ips
[i
].pnn
== options
.pnn
) {
2457 (void) try_moveip(ctdb
, &addr
, -1);
2460 ret
= ctdb_ctrl_del_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, &pub
);
2462 DEBUG(DEBUG_ERR
, ("Unable to del public ip from node %u\n", options
.pnn
));
2463 talloc_free(tmp_ctx
);
2467 talloc_free(tmp_ctx
);
2471 static int kill_tcp_from_file(struct ctdb_context
*ctdb
,
2472 int argc
, const char **argv
)
2474 struct ctdb_connection
*killtcp
;
2475 int max_entries
, current
, i
;
2476 struct timeval timeout
;
2477 char line
[128], src
[128], dst
[128];
2480 struct client_async_data
*async_data
;
2481 struct ctdb_client_control_state
*state
;
2491 while (!feof(stdin
)) {
2492 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2496 /* Silently skip empty lines */
2497 if (line
[0] == '\n') {
2501 if (sscanf(line
, "%s %s\n", src
, dst
) != 2) {
2502 DEBUG(DEBUG_ERR
, ("Bad line [%d]: '%s'\n",
2504 talloc_free(killtcp
);
2508 if (current
>= max_entries
) {
2509 max_entries
+= 1024;
2510 killtcp
= talloc_realloc(ctdb
, killtcp
,
2511 struct ctdb_connection
,
2513 CTDB_NO_MEMORY(ctdb
, killtcp
);
2516 if (!parse_ip_port(src
, &killtcp
[current
].src
)) {
2517 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2519 talloc_free(killtcp
);
2523 if (!parse_ip_port(dst
, &killtcp
[current
].dst
)) {
2524 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2526 talloc_free(killtcp
);
2533 async_data
= talloc_zero(ctdb
, struct client_async_data
);
2534 if (async_data
== NULL
) {
2535 talloc_free(killtcp
);
2539 for (i
= 0; i
< current
; i
++) {
2541 data
.dsize
= sizeof(struct ctdb_connection
);
2542 data
.dptr
= (unsigned char *)&killtcp
[i
];
2544 timeout
= TIMELIMIT();
2545 state
= ctdb_control_send(ctdb
, options
.pnn
, 0,
2546 CTDB_CONTROL_KILL_TCP
, 0, data
,
2547 async_data
, &timeout
, NULL
);
2549 if (state
== NULL
) {
2551 ("Failed to call async killtcp control to node %u\n",
2553 talloc_free(killtcp
);
2557 ctdb_client_async_add(async_data
, state
);
2560 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2561 DEBUG(DEBUG_ERR
,("killtcp failed\n"));
2562 talloc_free(killtcp
);
2566 talloc_free(killtcp
);
2572 kill a tcp connection
2574 static int kill_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2577 struct ctdb_connection killtcp
;
2579 assert_single_node_only();
2582 return kill_tcp_from_file(ctdb
, argc
, argv
);
2589 if (!parse_ip_port(argv
[0], &killtcp
.src
)) {
2590 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2594 if (!parse_ip_port(argv
[1], &killtcp
.dst
)) {
2595 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2599 ret
= ctdb_ctrl_killtcp(ctdb
, TIMELIMIT(), options
.pnn
, &killtcp
);
2601 DEBUG(DEBUG_ERR
, ("Unable to killtcp from node %u\n", options
.pnn
));
2612 static int control_gratious_arp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2615 ctdb_sock_addr addr
;
2617 assert_single_node_only();
2623 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2624 DEBUG(DEBUG_ERR
, ("Bad IP '%s'\n", argv
[0]));
2628 ret
= ctdb_ctrl_gratious_arp(ctdb
, TIMELIMIT(), options
.pnn
, &addr
, argv
[1]);
2630 DEBUG(DEBUG_ERR
, ("Unable to send gratious_arp from node %u\n", options
.pnn
));
2638 register a server id
2640 static int regsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2643 struct ctdb_client_id server_id
;
2649 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2650 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2651 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2653 ret
= ctdb_ctrl_register_server_id(ctdb
, TIMELIMIT(), &server_id
);
2655 DEBUG(DEBUG_ERR
, ("Unable to register server_id from node %u\n", options
.pnn
));
2658 DEBUG(DEBUG_ERR
,("Srvid registered. Sleeping for 999 seconds\n"));
2664 unregister a server id
2666 static int unregsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2669 struct ctdb_client_id server_id
;
2675 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2676 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2677 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2679 ret
= ctdb_ctrl_unregister_server_id(ctdb
, TIMELIMIT(), &server_id
);
2681 DEBUG(DEBUG_ERR
, ("Unable to unregister server_id from node %u\n", options
.pnn
));
2688 check if a server id exists
2690 static int chksrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2692 uint32_t status
= 0;
2694 struct ctdb_client_id server_id
;
2700 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2701 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2702 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2704 ret
= ctdb_ctrl_check_server_id(ctdb
, TIMELIMIT(), options
.pnn
, &server_id
, &status
);
2706 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n", options
.pnn
));
2711 printf("Server id %d:%d:%d EXISTS\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2713 printf("Server id %d:%d:%d does NOT exist\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2719 get a list of all server ids that are registered on a node
2721 static int getsrvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2724 struct ctdb_client_id_list_old
*server_ids
;
2726 ret
= ctdb_ctrl_get_server_id_list(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &server_ids
);
2728 DEBUG(DEBUG_ERR
, ("Unable to get server_id list from node %u\n", options
.pnn
));
2732 for (i
=0; i
<server_ids
->num
; i
++) {
2733 printf("Server id %d:%d:%d\n",
2734 server_ids
->server_ids
[i
].pnn
,
2735 server_ids
->server_ids
[i
].type
,
2736 server_ids
->server_ids
[i
].server_id
);
2743 check if a server id exists
2745 static int check_srvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2747 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
2753 talloc_free(tmp_ctx
);
2757 ids
= talloc_array(tmp_ctx
, uint64_t, argc
);
2758 result
= talloc_array(tmp_ctx
, uint8_t, argc
);
2760 for (i
= 0; i
< argc
; i
++) {
2761 ids
[i
] = strtoull(argv
[i
], NULL
, 0);
2764 if (!ctdb_client_check_message_handlers(ctdb
, ids
, argc
, result
)) {
2765 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n",
2767 talloc_free(tmp_ctx
);
2771 for (i
=0; i
< argc
; i
++) {
2772 printf("Server id %d:%llu %s\n", options
.pnn
, (long long)ids
[i
],
2773 result
[i
] ? "exists" : "does not exist");
2776 talloc_free(tmp_ctx
);
2781 send a tcp tickle ack
2783 static int tickle_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2786 ctdb_sock_addr src
, dst
;
2792 if (!parse_ip_port(argv
[0], &src
)) {
2793 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2797 if (!parse_ip_port(argv
[1], &dst
)) {
2798 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2802 ret
= ctdb_sys_send_tcp(&src
, &dst
, 0, 0, 0);
2806 DEBUG(DEBUG_ERR
, ("Error while sending tickle ack\n"));
2813 display public ip status
2815 static int control_ip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2818 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2819 struct ctdb_public_ip_list_old
*ips
;
2821 if (argc
== 1 && strcmp(argv
[0], "all") == 0) {
2822 options
.pnn
= CTDB_BROADCAST_ALL
;
2825 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2826 /* read the list of public ips from all nodes */
2827 ret
= control_get_all_public_ips(ctdb
, tmp_ctx
, &ips
);
2829 /* read the public ip list from this node */
2830 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2833 DEBUG(DEBUG_ERR
, ("Unable to get public ips from node %u\n", options
.pnn
));
2834 talloc_free(tmp_ctx
);
2838 if (options
.machinereadable
){
2839 printm(":Public IP:Node:");
2840 if (options
.verbose
){
2841 printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2845 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2846 printf("Public IPs on ALL nodes\n");
2848 printf("Public IPs on node %u\n", options
.pnn
);
2852 for (i
=1;i
<=ips
->num
;i
++) {
2853 struct ctdb_public_ip_info_old
*info
= NULL
;
2855 char *aciface
= NULL
;
2856 char *avifaces
= NULL
;
2857 char *cifaces
= NULL
;
2859 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2860 pnn
= ips
->ips
[ips
->num
-i
].pnn
;
2866 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), pnn
, ctdb
,
2867 &ips
->ips
[ips
->num
-i
].addr
, &info
);
2874 for (j
=0; j
< info
->num
; j
++) {
2875 if (cifaces
== NULL
) {
2876 cifaces
= talloc_strdup(info
,
2877 info
->ifaces
[j
].name
);
2879 cifaces
= talloc_asprintf_append(cifaces
,
2881 info
->ifaces
[j
].name
);
2884 if (info
->active_idx
== j
) {
2885 aciface
= info
->ifaces
[j
].name
;
2888 if (info
->ifaces
[j
].link_state
== 0) {
2892 if (avifaces
== NULL
) {
2893 avifaces
= talloc_strdup(info
, info
->ifaces
[j
].name
);
2895 avifaces
= talloc_asprintf_append(avifaces
,
2897 info
->ifaces
[j
].name
);
2902 if (options
.machinereadable
){
2904 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2905 ips
->ips
[ips
->num
-i
].pnn
);
2906 if (options
.verbose
){
2909 avifaces
?avifaces
:"",
2910 cifaces
?cifaces
:"");
2914 if (options
.verbose
) {
2915 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2916 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2917 ips
->ips
[ips
->num
-i
].pnn
,
2919 avifaces
?avifaces
:"",
2920 cifaces
?cifaces
:"");
2923 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2924 ips
->ips
[ips
->num
-i
].pnn
);
2930 talloc_free(tmp_ctx
);
2937 static int control_ipinfo(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2940 ctdb_sock_addr addr
;
2941 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2942 struct ctdb_public_ip_info_old
*info
;
2945 talloc_free(tmp_ctx
);
2949 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2950 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2954 /* read the public ip info from this node */
2955 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), options
.pnn
,
2956 tmp_ctx
, &addr
, &info
);
2958 DEBUG(DEBUG_ERR
, ("Unable to get public ip[%s]info from node %u\n",
2959 argv
[0], options
.pnn
));
2960 talloc_free(tmp_ctx
);
2964 printf("Public IP[%s] info on node %u\n",
2965 ctdb_addr_to_str(&info
->ip
.addr
),
2968 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2969 ctdb_addr_to_str(&info
->ip
.addr
),
2970 info
->ip
.pnn
, info
->num
);
2972 for (i
=0; i
<info
->num
; i
++) {
2973 info
->ifaces
[i
].name
[CTDB_IFACE_SIZE
] = '\0';
2975 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2976 i
+1, info
->ifaces
[i
].name
,
2977 info
->ifaces
[i
].link_state
?"up":"down",
2978 (unsigned int)info
->ifaces
[i
].references
,
2979 (i
==info
->active_idx
)?" (active)":"");
2982 talloc_free(tmp_ctx
);
2987 display interfaces status
2989 static int control_ifaces(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2991 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2993 struct ctdb_iface_list_old
*ifaces
;
2996 /* read the public ip list from this node */
2997 ret
= ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ifaces
);
2999 DEBUG(DEBUG_ERR
, ("Unable to get interfaces from node %u\n",
3001 talloc_free(tmp_ctx
);
3005 if (options
.machinereadable
){
3006 printm(":Name:LinkStatus:References:\n");
3008 printf("Interfaces on node %u\n", options
.pnn
);
3011 for (i
=0; i
<ifaces
->num
; i
++) {
3012 if (options
.machinereadable
){
3013 printm(":%s:%s:%u:\n",
3014 ifaces
->ifaces
[i
].name
,
3015 ifaces
->ifaces
[i
].link_state
?"1":"0",
3016 (unsigned int)ifaces
->ifaces
[i
].references
);
3018 printf("name:%s link:%s references:%u\n",
3019 ifaces
->ifaces
[i
].name
,
3020 ifaces
->ifaces
[i
].link_state
?"up":"down",
3021 (unsigned int)ifaces
->ifaces
[i
].references
);
3025 talloc_free(tmp_ctx
);
3031 set link status of an interface
3033 static int control_setifacelink(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3036 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3037 struct ctdb_iface info
;
3045 if (strlen(argv
[0]) > CTDB_IFACE_SIZE
) {
3046 DEBUG(DEBUG_ERR
, ("interfaces name '%s' too long\n",
3048 talloc_free(tmp_ctx
);
3051 strcpy(info
.name
, argv
[0]);
3053 if (strcmp(argv
[1], "up") == 0) {
3054 info
.link_state
= 1;
3055 } else if (strcmp(argv
[1], "down") == 0) {
3056 info
.link_state
= 0;
3058 DEBUG(DEBUG_ERR
, ("link state invalid '%s' should be 'up' or 'down'\n",
3060 talloc_free(tmp_ctx
);
3064 /* read the public ip list from this node */
3065 ret
= ctdb_ctrl_set_iface_link(ctdb
, TIMELIMIT(), options
.pnn
,
3068 DEBUG(DEBUG_ERR
, ("Unable to set link state for interfaces %s node %u\n",
3069 argv
[0], options
.pnn
));
3070 talloc_free(tmp_ctx
);
3074 talloc_free(tmp_ctx
);
3079 display pid of a ctdb daemon
3081 static int control_getpid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3086 ret
= ctdb_ctrl_getpid(ctdb
, TIMELIMIT(), options
.pnn
, &pid
);
3088 DEBUG(DEBUG_ERR
, ("Unable to get daemon pid from node %u\n", options
.pnn
));
3091 printf("Pid:%d\n", pid
);
3096 typedef bool update_flags_handler_t(struct ctdb_context
*ctdb
, void *data
);
3098 static int update_flags_and_ipreallocate(struct ctdb_context
*ctdb
,
3100 update_flags_handler_t handler
,
3105 struct ctdb_node_map_old
*nodemap
= NULL
;
3109 /* Check if the node is already in the desired state */
3110 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3112 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3115 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3116 if (set_flag
== flag_is_set
) {
3117 DEBUG(DEBUG_NOTICE
, ("Node %d is %s %s\n", options
.pnn
,
3118 (set_flag
? "already" : "not"), desc
));
3123 if (!handler(ctdb
, data
)) {
3124 DEBUG(DEBUG_WARNING
,
3125 ("Failed to send control to set state %s on node %u, try again\n",
3126 desc
, options
.pnn
));
3131 /* Read the nodemap and verify the change took effect.
3132 * Even if the above control/hanlder timed out then it
3133 * could still have worked!
3135 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
3138 DEBUG(DEBUG_WARNING
,
3139 ("Unable to get nodemap from local node, try again\n"));
3141 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3142 } while (nodemap
== NULL
|| (set_flag
!= flag_is_set
));
3144 return ipreallocate(ctdb
);
3147 /* Administratively disable a node */
3148 static bool update_flags_disabled(struct ctdb_context
*ctdb
, void *data
)
3152 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3153 NODE_FLAGS_PERMANENTLY_DISABLED
, 0);
3157 static int control_disable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3159 return update_flags_and_ipreallocate(ctdb
, NULL
,
3160 update_flags_disabled
,
3161 NODE_FLAGS_PERMANENTLY_DISABLED
,
3163 true /* set_flag*/);
3166 /* Administratively re-enable a node */
3167 static bool update_flags_not_disabled(struct ctdb_context
*ctdb
, void *data
)
3171 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3172 0, NODE_FLAGS_PERMANENTLY_DISABLED
);
3176 static int control_enable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3178 return update_flags_and_ipreallocate(ctdb
, NULL
,
3179 update_flags_not_disabled
,
3180 NODE_FLAGS_PERMANENTLY_DISABLED
,
3182 false /* set_flag*/);
3186 static bool update_flags_stopped(struct ctdb_context
*ctdb
, void *data
)
3190 ret
= ctdb_ctrl_stop_node(ctdb
, TIMELIMIT(), options
.pnn
);
3195 static int control_stop(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3197 return update_flags_and_ipreallocate(ctdb
, NULL
,
3198 update_flags_stopped
,
3201 true /* set_flag*/);
3204 /* Continue a stopped node */
3205 static bool update_flags_not_stopped(struct ctdb_context
*ctdb
, void *data
)
3209 ret
= ctdb_ctrl_continue_node(ctdb
, TIMELIMIT(), options
.pnn
);
3214 static int control_continue(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3216 return update_flags_and_ipreallocate(ctdb
, NULL
,
3217 update_flags_not_stopped
,
3220 false /* set_flag */);
3223 static uint32_t get_generation(struct ctdb_context
*ctdb
)
3225 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3226 struct ctdb_vnn_map
*vnnmap
=NULL
;
3228 uint32_t generation
;
3230 /* wait until the recmaster is not in recovery mode */
3232 uint32_t recmode
, recmaster
;
3234 if (vnnmap
!= NULL
) {
3235 talloc_free(vnnmap
);
3239 /* get the recmaster */
3240 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), CTDB_CURRENT_NODE
, &recmaster
);
3242 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
3243 talloc_free(tmp_ctx
);
3247 /* get recovery mode */
3248 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), recmaster
, &recmode
);
3250 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
3251 talloc_free(tmp_ctx
);
3255 /* get the current generation number */
3256 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), recmaster
, tmp_ctx
, &vnnmap
);
3258 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from recmaster (%u)\n", recmaster
));
3259 talloc_free(tmp_ctx
);
3263 if ((recmode
== CTDB_RECOVERY_NORMAL
) && (vnnmap
->generation
!= 1)) {
3264 generation
= vnnmap
->generation
;
3265 talloc_free(tmp_ctx
);
3273 static bool update_state_banned(struct ctdb_context
*ctdb
, void *data
)
3275 struct ctdb_ban_state
*bantime
= (struct ctdb_ban_state
*)data
;
3278 ret
= ctdb_ctrl_set_ban(ctdb
, TIMELIMIT(), options
.pnn
, bantime
);
3283 static int control_ban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3285 struct ctdb_ban_state bantime
;
3291 bantime
.pnn
= options
.pnn
;
3292 bantime
.time
= strtoul(argv
[0], NULL
, 0);
3294 if (bantime
.time
== 0) {
3295 DEBUG(DEBUG_ERR
, ("Invalid ban time specified - must be >0\n"));
3299 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3300 update_state_banned
,
3303 true /* set_flag*/);
3308 static int control_unban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3310 struct ctdb_ban_state bantime
;
3312 bantime
.pnn
= options
.pnn
;
3315 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3316 update_state_banned
,
3319 false /* set_flag*/);
3323 show ban information for a node
3325 static int control_showban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3328 struct ctdb_node_map_old
*nodemap
=NULL
;
3329 struct ctdb_ban_state
*bantime
;
3331 /* verify the node exists */
3332 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3334 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3338 ret
= ctdb_ctrl_get_ban(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &bantime
);
3340 DEBUG(DEBUG_ERR
,("Showing ban info for node %d failed.\n", options
.pnn
));
3344 if (bantime
->time
== 0) {
3345 printf("Node %u is not banned\n", bantime
->pnn
);
3347 printf("Node %u is banned, %d seconds remaining\n",
3348 bantime
->pnn
, bantime
->time
);
3357 static int control_shutdown(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3361 ret
= ctdb_ctrl_shutdown(ctdb
, TIMELIMIT(), options
.pnn
);
3363 DEBUG(DEBUG_ERR
, ("Unable to shutdown node %u\n", options
.pnn
));
3373 static int control_recover(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3376 uint32_t generation
, next_generation
;
3378 /* record the current generation number */
3379 generation
= get_generation(ctdb
);
3381 ret
= ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
3383 DEBUG(DEBUG_ERR
, ("Unable to set recovery mode\n"));
3387 /* wait until we are in a new generation */
3389 next_generation
= get_generation(ctdb
);
3390 if (next_generation
!= generation
) {
3401 display monitoring mode of a remote node
3403 static int control_getmonmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3408 ret
= ctdb_ctrl_getmonmode(ctdb
, TIMELIMIT(), options
.pnn
, &monmode
);
3410 DEBUG(DEBUG_ERR
, ("Unable to get monmode from node %u\n", options
.pnn
));
3413 if (!options
.machinereadable
){
3414 printf("Monitoring mode:%s (%d)\n",monmode
==CTDB_MONITORING_ACTIVE
?"ACTIVE":"DISABLED",monmode
);
3417 printm(":%d:\n",monmode
);
3424 display capabilities of a remote node
3426 static int control_getcapabilities(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3428 uint32_t capabilities
;
3431 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), options
.pnn
, &capabilities
);
3433 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", options
.pnn
));
3437 if (!options
.machinereadable
){
3438 printf("RECMASTER: %s\n", (capabilities
&CTDB_CAP_RECMASTER
)?"YES":"NO");
3439 printf("LMASTER: %s\n", (capabilities
&CTDB_CAP_LMASTER
)?"YES":"NO");
3440 printf("LVS: %s\n", (capabilities
&CTDB_CAP_LVS
)?"YES":"NO");
3442 printm(":RECMASTER:LMASTER:LVS:\n");
3443 printm(":%d:%d:%d:\n",
3444 !!(capabilities
&CTDB_CAP_RECMASTER
),
3445 !!(capabilities
&CTDB_CAP_LMASTER
),
3446 !!(capabilities
&CTDB_CAP_LVS
));
3452 display lvs configuration
3455 static uint32_t lvs_exclude_flags
[] = {
3456 /* Look for a nice healthy node */
3457 NODE_FLAGS_INACTIVE
|NODE_FLAGS_DISABLED
,
3458 /* If not found, an UNHEALTHY node will do */
3459 NODE_FLAGS_INACTIVE
|NODE_FLAGS_PERMANENTLY_DISABLED
,
3463 static int control_lvs(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3465 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3466 struct ctdb_node_map_old
*orig_nodemap
=NULL
;
3467 struct ctdb_node_map_old
*nodemap
;
3470 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3471 tmp_ctx
, &orig_nodemap
);
3473 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3474 talloc_free(tmp_ctx
);
3478 nodemap
= filter_nodemap_by_capabilities(ctdb
, orig_nodemap
,
3479 CTDB_CAP_LVS
, false);
3480 if (nodemap
== NULL
) {
3488 for (i
= 0; lvs_exclude_flags
[i
] != 0; i
++) {
3489 struct ctdb_node_map_old
*t
=
3490 filter_nodemap_by_flags(ctdb
, nodemap
,
3491 lvs_exclude_flags
[i
]);
3498 /* At least 1 node without excluded flags */
3500 for (j
= 0; j
< t
->num
; j
++) {
3501 printf("%d:%s\n", t
->nodes
[j
].pnn
,
3502 ctdb_addr_to_str(&t
->nodes
[j
].addr
));
3509 talloc_free(tmp_ctx
);
3514 display who is the lvs master
3516 static int control_lvsmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3518 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3519 struct ctdb_node_map_old
*nodemap
=NULL
;
3522 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3525 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3526 talloc_free(tmp_ctx
);
3530 for (i
= 0; lvs_exclude_flags
[i
] != 0; i
++) {
3531 struct ctdb_node_map_old
*t
=
3532 filter_nodemap_by_flags(ctdb
, nodemap
,
3533 lvs_exclude_flags
[i
]);
3540 struct ctdb_node_map_old
*n
;
3541 n
= filter_nodemap_by_capabilities(ctdb
,
3552 if (options
.machinereadable
) {
3553 printm("%d\n", n
->nodes
[0].pnn
);
3555 printf("Node %d is LVS master\n", n
->nodes
[0].pnn
);
3563 printf("There is no LVS master\n");
3566 talloc_free(tmp_ctx
);
3571 disable monitoring on a node
3573 static int control_disable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3578 ret
= ctdb_ctrl_disable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3580 DEBUG(DEBUG_ERR
, ("Unable to disable monmode on node %u\n", options
.pnn
));
3583 printf("Monitoring mode:%s\n","DISABLED");
3589 enable monitoring on a node
3591 static int control_enable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3596 ret
= ctdb_ctrl_enable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3598 DEBUG(DEBUG_ERR
, ("Unable to enable monmode on node %u\n", options
.pnn
));
3601 printf("Monitoring mode:%s\n","ACTIVE");
3607 display remote list of keys/data for a db
3609 static int control_catdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3611 const char *db_name
;
3612 struct ctdb_db_context
*ctdb_db
;
3614 struct ctdb_dump_db_context c
;
3621 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3625 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3626 if (ctdb_db
== NULL
) {
3627 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3631 if (options
.printlmaster
) {
3632 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
,
3633 ctdb
, &ctdb
->vnn_map
);
3635 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
3644 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3645 c
.printdatasize
= (bool)options
.printdatasize
;
3646 c
.printlmaster
= (bool)options
.printlmaster
;
3647 c
.printhash
= (bool)options
.printhash
;
3648 c
.printrecordflags
= (bool)options
.printrecordflags
;
3650 /* traverse and dump the cluster tdb */
3651 ret
= ctdb_dump_db(ctdb_db
, &c
);
3653 DEBUG(DEBUG_ERR
, ("Unable to dump database\n"));
3654 DEBUG(DEBUG_ERR
, ("Maybe try 'ctdb getdbstatus %s'"
3655 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3659 talloc_free(ctdb_db
);
3661 printf("Dumped %d records\n", ret
);
3665 struct cattdb_data
{
3666 struct ctdb_context
*ctdb
;
3670 static int cattdb_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private_data
)
3672 struct cattdb_data
*d
= private_data
;
3673 struct ctdb_dump_db_context c
;
3680 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3681 c
.printdatasize
= (bool)options
.printdatasize
;
3682 c
.printlmaster
= false;
3683 c
.printhash
= (bool)options
.printhash
;
3684 c
.printrecordflags
= true;
3686 return ctdb_dumpdb_record(key
, data
, &c
);
3690 cat the local tdb database using same format as catdb
3692 static int control_cattdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3694 const char *db_name
;
3695 struct ctdb_db_context
*ctdb_db
;
3696 struct cattdb_data d
;
3703 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3707 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3708 if (ctdb_db
== NULL
) {
3709 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3713 /* traverse the local tdb */
3716 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, cattdb_traverse
, &d
) == -1) {
3717 printf("Failed to cattdb data\n");
3720 talloc_free(ctdb_db
);
3722 printf("Dumped %d records\n", d
.count
);
3727 display the content of a database key
3729 static int control_readkey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3731 const char *db_name
;
3732 struct ctdb_db_context
*ctdb_db
;
3733 struct ctdb_record_handle
*h
;
3734 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3742 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3746 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3747 if (ctdb_db
== NULL
) {
3748 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3752 key
.dptr
= discard_const(argv
[1]);
3753 key
.dsize
= strlen((char *)key
.dptr
);
3755 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3757 printf("Failed to fetch record '%s' on node %d\n",
3758 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3759 talloc_free(tmp_ctx
);
3763 printf("Data: size:%d ptr:[%.*s]\n", (int)data
.dsize
, (int)data
.dsize
, data
.dptr
);
3765 talloc_free(tmp_ctx
);
3766 talloc_free(ctdb_db
);
3771 display the content of a database key
3773 static int control_writekey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3775 const char *db_name
;
3776 struct ctdb_db_context
*ctdb_db
;
3777 struct ctdb_record_handle
*h
;
3778 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3786 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3790 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3791 if (ctdb_db
== NULL
) {
3792 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3796 key
.dptr
= discard_const(argv
[1]);
3797 key
.dsize
= strlen((char *)key
.dptr
);
3799 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3801 printf("Failed to fetch record '%s' on node %d\n",
3802 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3803 talloc_free(tmp_ctx
);
3807 data
.dptr
= discard_const(argv
[2]);
3808 data
.dsize
= strlen((char *)data
.dptr
);
3810 if (ctdb_record_store(h
, data
) != 0) {
3811 printf("Failed to store record\n");
3815 talloc_free(tmp_ctx
);
3816 talloc_free(ctdb_db
);
3821 fetch a record from a persistent database
3823 static int control_pfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3825 const char *db_name
;
3826 struct ctdb_db_context
*ctdb_db
;
3827 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3828 struct ctdb_transaction_handle
*h
;
3835 talloc_free(tmp_ctx
);
3839 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3840 talloc_free(tmp_ctx
);
3844 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
3846 DEBUG(DEBUG_ERR
,("Database '%s' is not persistent\n", db_name
));
3847 talloc_free(tmp_ctx
);
3851 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
3852 if (ctdb_db
== NULL
) {
3853 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3854 talloc_free(tmp_ctx
);
3858 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
3860 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
3861 talloc_free(tmp_ctx
);
3865 key
.dptr
= discard_const(argv
[1]);
3866 key
.dsize
= strlen(argv
[1]);
3867 ret
= ctdb_transaction_fetch(h
, tmp_ctx
, key
, &data
);
3869 DEBUG(DEBUG_ERR
,("Failed to fetch record\n"));
3870 talloc_free(tmp_ctx
);
3874 if (data
.dsize
== 0 || data
.dptr
== NULL
) {
3875 DEBUG(DEBUG_ERR
,("Record is empty\n"));
3876 talloc_free(tmp_ctx
);
3881 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
3883 DEBUG(DEBUG_ERR
,("Failed to open output file %s\n", argv
[2]));
3884 talloc_free(tmp_ctx
);
3887 sys_write(fd
, data
.dptr
, data
.dsize
);
3890 sys_write(1, data
.dptr
, data
.dsize
);
3893 /* abort the transaction */
3897 talloc_free(tmp_ctx
);
3902 fetch a record from a tdb-file
3904 static int control_tfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3906 const char *tdb_file
;
3909 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
3918 tdb
= tdb_open(tdb_file
, 0, 0, O_RDONLY
, 0);
3920 printf("Failed to open TDB file %s\n", tdb_file
);
3924 key
= strtodata(tmp_ctx
, argv
[1], strlen(argv
[1]));
3925 if (key
.dptr
== NULL
) {
3926 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
3930 data
= tdb_fetch(tdb
, key
);
3931 if (data
.dptr
== NULL
|| data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
3932 printf("Failed to read record %s from tdb %s\n", argv
[1], tdb_file
);
3940 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
3942 printf("Failed to open output file %s\n", argv
[2]);
3945 if (options
.verbose
){
3946 sys_write(fd
, data
.dptr
, data
.dsize
);
3948 sys_write(fd
, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
3952 if (options
.verbose
){
3953 sys_write(1, data
.dptr
, data
.dsize
);
3955 sys_write(1, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
3959 talloc_free(tmp_ctx
);
3964 store a record and header to a tdb-file
3966 static int control_tstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3968 const char *tdb_file
;
3970 TDB_DATA key
, value
, data
;
3971 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
3972 struct ctdb_ltdb_header header
;
3980 tdb
= tdb_open(tdb_file
, 0, 0, O_RDWR
, 0);
3982 printf("Failed to open TDB file %s\n", tdb_file
);
3986 key
= strtodata(tmp_ctx
, argv
[1], strlen(argv
[1]));
3987 if (key
.dptr
== NULL
) {
3988 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
3992 value
= strtodata(tmp_ctx
, argv
[2], strlen(argv
[2]));
3993 if (value
.dptr
== NULL
) {
3994 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[2]);
3998 ZERO_STRUCT(header
);
4000 header
.rsn
= atoll(argv
[3]);
4003 header
.dmaster
= atoi(argv
[4]);
4006 header
.flags
= atoi(argv
[5]);
4009 data
.dsize
= sizeof(struct ctdb_ltdb_header
) + value
.dsize
;
4010 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4011 if (data
.dptr
== NULL
) {
4012 printf("Failed to allocate header+value\n");
4016 *(struct ctdb_ltdb_header
*)data
.dptr
= header
;
4017 memcpy(data
.dptr
+ sizeof(struct ctdb_ltdb_header
), value
.dptr
, value
.dsize
);
4019 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0) {
4020 printf("Failed to write record %s to tdb %s\n", argv
[1], tdb_file
);
4027 talloc_free(tmp_ctx
);
4032 write a record to a persistent database
4034 static int control_pstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4036 const char *db_name
;
4037 struct ctdb_db_context
*ctdb_db
;
4038 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4039 struct ctdb_transaction_handle
*h
;
4045 talloc_free(tmp_ctx
);
4049 fd
= open(argv
[2], O_RDONLY
);
4051 DEBUG(DEBUG_ERR
,("Failed to open file containing record data : %s %s\n", argv
[2], strerror(errno
)));
4052 talloc_free(tmp_ctx
);
4056 ret
= fstat(fd
, &st
);
4058 DEBUG(DEBUG_ERR
,("fstat of file %s failed: %s\n", argv
[2], strerror(errno
)));
4060 talloc_free(tmp_ctx
);
4064 if (!S_ISREG(st
.st_mode
)) {
4065 DEBUG(DEBUG_ERR
,("Not a regular file %s\n", argv
[2]));
4067 talloc_free(tmp_ctx
);
4071 data
.dsize
= st
.st_size
;
4072 if (data
.dsize
== 0) {
4075 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4076 if (data
.dptr
== NULL
) {
4077 DEBUG(DEBUG_ERR
,("Failed to talloc %d of memory to store record data\n", (int)data
.dsize
));
4079 talloc_free(tmp_ctx
);
4082 ret
= sys_read(fd
, data
.dptr
, data
.dsize
);
4083 if (ret
!= data
.dsize
) {
4084 DEBUG(DEBUG_ERR
,("Failed to read %d bytes of record data\n", (int)data
.dsize
));
4086 talloc_free(tmp_ctx
);
4095 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4096 if (ctdb_db
== NULL
) {
4097 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4098 talloc_free(tmp_ctx
);
4102 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4104 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4105 talloc_free(tmp_ctx
);
4109 key
= strtodata(tmp_ctx
, argv
[1], strlen(argv
[1]));
4110 if (key
.dptr
== NULL
) {
4111 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4115 ret
= ctdb_transaction_store(h
, key
, data
);
4117 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4118 talloc_free(tmp_ctx
);
4122 ret
= ctdb_transaction_commit(h
);
4124 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4125 talloc_free(tmp_ctx
);
4130 talloc_free(tmp_ctx
);
4135 * delete a record from a persistent database
4137 static int control_pdelete(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4139 const char *db_name
;
4140 struct ctdb_db_context
*ctdb_db
;
4141 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4142 struct ctdb_transaction_handle
*h
;
4149 talloc_free(tmp_ctx
);
4153 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
4154 talloc_free(tmp_ctx
);
4158 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
4160 DEBUG(DEBUG_ERR
, ("Database '%s' is not persistent\n", db_name
));
4161 talloc_free(tmp_ctx
);
4165 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4166 if (ctdb_db
== NULL
) {
4167 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n", db_name
));
4168 talloc_free(tmp_ctx
);
4172 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4174 DEBUG(DEBUG_ERR
, ("Failed to start transaction on database %s\n", db_name
));
4175 talloc_free(tmp_ctx
);
4179 key
= strtodata(tmp_ctx
, argv
[1], strlen(argv
[1]));
4180 if (key
.dptr
== NULL
) {
4181 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4185 ret
= ctdb_transaction_store(h
, key
, tdb_null
);
4187 DEBUG(DEBUG_ERR
, ("Failed to delete record\n"));
4188 talloc_free(tmp_ctx
);
4192 ret
= ctdb_transaction_commit(h
);
4194 DEBUG(DEBUG_ERR
, ("Failed to commit transaction\n"));
4195 talloc_free(tmp_ctx
);
4199 talloc_free(tmp_ctx
);
4203 static const char *ptrans_parse_string(TALLOC_CTX
*mem_ctx
, const char *s
,
4208 const char *ret
; /* Next byte after successfully parsed value */
4210 /* Error, unless someone says otherwise */
4212 /* Indicates no value to parse */
4215 /* Skip whitespace */
4216 n
= strspn(s
, " \t");
4220 /* Quoted ASCII string - no wide characters! */
4222 n
= strcspn(t
, "\"");
4225 *data
= strtodata(mem_ctx
, t
, n
);
4226 CTDB_NOMEM_ABORT(data
->dptr
);
4230 DEBUG(DEBUG_WARNING
,("Unmatched \" in input %s\n", s
));
4233 DEBUG(DEBUG_WARNING
,("Unsupported input format in %s\n", s
));
4239 static bool ptrans_get_key_value(TALLOC_CTX
*mem_ctx
, FILE *file
,
4240 TDB_DATA
*key
, TDB_DATA
*value
)
4242 char line
[1024]; /* FIXME: make this more flexible? */
4246 ptr
= fgets(line
, sizeof(line
), file
);
4253 t
= ptrans_parse_string(mem_ctx
, line
, key
);
4254 if (t
== NULL
|| key
->dptr
== NULL
) {
4255 /* Line Ignored but not EOF */
4260 t
= ptrans_parse_string(mem_ctx
, t
, value
);
4262 /* Line Ignored but not EOF */
4263 talloc_free(key
->dptr
);
4272 * Update a persistent database as per file/stdin
4274 static int control_ptrans(struct ctdb_context
*ctdb
,
4275 int argc
, const char **argv
)
4277 const char *db_name
;
4278 struct ctdb_db_context
*ctdb_db
;
4279 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4280 struct ctdb_transaction_handle
*h
;
4281 TDB_DATA key
, value
;
4286 talloc_free(tmp_ctx
);
4292 file
= fopen(argv
[1], "r");
4294 DEBUG(DEBUG_ERR
,("Unable to open file for reading '%s'\n", argv
[1]));
4295 talloc_free(tmp_ctx
);
4302 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4303 if (ctdb_db
== NULL
) {
4304 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4308 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4310 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4314 while (ptrans_get_key_value(tmp_ctx
, file
, &key
, &value
)) {
4315 if (key
.dsize
!= 0) {
4316 ret
= ctdb_transaction_store(h
, key
, value
);
4317 /* Minimise memory use */
4318 talloc_free(key
.dptr
);
4319 if (value
.dptr
!= NULL
) {
4320 talloc_free(value
.dptr
);
4323 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4324 ctdb_transaction_cancel(h
);
4330 ret
= ctdb_transaction_commit(h
);
4332 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4336 if (file
!= stdin
) {
4339 talloc_free(tmp_ctx
);
4343 if (file
!= stdin
) {
4347 talloc_free(tmp_ctx
);
4352 check if a service is bound to a port or not
4354 static int control_chktcpport(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4359 struct sockaddr_in sin
;
4362 printf("Use: ctdb chktcport <port>\n");
4366 port
= atoi(argv
[0]);
4368 s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
4370 printf("Failed to open local socket\n");
4374 v
= fcntl(s
, F_GETFL
, 0);
4375 if (v
== -1 || fcntl(s
, F_SETFL
, v
| O_NONBLOCK
) != 0) {
4376 printf("Unable to set socket non-blocking: %s\n", strerror(errno
));
4379 bzero(&sin
, sizeof(sin
));
4380 sin
.sin_family
= AF_INET
;
4381 sin
.sin_port
= htons(port
);
4382 ret
= bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
));
4385 printf("Failed to bind to local socket: %d %s\n", errno
, strerror(errno
));
4393 /* Reload public IPs on a specified nodes */
4394 static int control_reloadips(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4396 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4402 assert_single_node_only();
4408 /* Determine the nodes where IPs need to be reloaded */
4409 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
4410 options
.pnn
, true, &nodes
, &pnn_mode
)) {
4416 /* Disable takeover runs on all connected nodes. A reply
4417 * indicating success is needed from each node so all nodes
4418 * will need to be active. This will retry until maxruntime
4419 * is exceeded, hence no error handling.
4421 * A check could be added to not allow reloading of IPs when
4422 * there are disconnected nodes. However, this should
4423 * probably be left up to the administrator.
4425 timeout
= LONGTIMEOUT
;
4426 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4427 "Disable takeover runs", true);
4429 /* Now tell all the desired nodes to reload their public IPs.
4430 * Keep trying this until it succeeds. This assumes all
4431 * failures are transient, which might not be true...
4433 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELOAD_PUBLIC_IPS
,
4434 nodes
, 0, LONGTIMELIMIT(),
4436 NULL
, NULL
, NULL
) != 0) {
4438 ("Unable to reload IPs on some nodes, try again.\n"));
4442 /* It isn't strictly necessary to wait until takeover runs are
4443 * re-enabled but doing so can't hurt.
4446 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4447 "Enable takeover runs", true);
4453 talloc_free(tmp_ctx
);
4458 display a list of the databases on a remote ctdb
4460 static int control_getdbmap(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4463 struct ctdb_dbid_map_old
*dbmap
=NULL
;
4465 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &dbmap
);
4467 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
4471 if(options
.machinereadable
){
4472 printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4473 for(i
=0;i
<dbmap
->num
;i
++){
4474 const char *path
= NULL
;
4475 const char *name
= NULL
;
4476 const char *health
= NULL
;
4481 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
,
4482 dbmap
->dbs
[i
].db_id
, ctdb
, &path
);
4483 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
,
4484 dbmap
->dbs
[i
].db_id
, ctdb
, &name
);
4485 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
4486 dbmap
->dbs
[i
].db_id
, ctdb
, &health
);
4487 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4488 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4489 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4490 printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4491 dbmap
->dbs
[i
].db_id
, name
, path
,
4492 !!(persistent
), !!(sticky
),
4493 !!(health
), !!(readonly
));
4498 printf("Number of databases:%d\n", dbmap
->num
);
4499 for(i
=0;i
<dbmap
->num
;i
++){
4500 const char *path
= NULL
;
4501 const char *name
= NULL
;
4502 const char *health
= NULL
;
4507 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].db_id
, ctdb
, &path
);
4508 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].db_id
, ctdb
, &name
);
4509 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].db_id
, ctdb
, &health
);
4510 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4511 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4512 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4513 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4514 dbmap
->dbs
[i
].db_id
, name
, path
,
4515 persistent
?" PERSISTENT":"",
4516 sticky
?" STICKY":"",
4517 readonly
?" READONLY":"",
4518 health
?" UNHEALTHY":"");
4525 display the status of a database on a remote ctdb
4527 static int control_getdbstatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4529 const char *db_name
;
4532 const char *path
= NULL
;
4533 const char *health
= NULL
;
4539 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
4543 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &path
);
4544 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &health
);
4545 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4546 db_id
, db_name
, path
,
4547 (flags
& CTDB_DB_FLAGS_PERSISTENT
? "yes" : "no"),
4548 (flags
& CTDB_DB_FLAGS_STICKY
? "yes" : "no"),
4549 (flags
& CTDB_DB_FLAGS_READONLY
? "yes" : "no"),
4550 (health
? health
: "OK"));
4556 check if the local node is recmaster or not
4557 it will return 1 if this node is the recmaster and 0 if it is not
4558 or if the local ctdb daemon could not be contacted
4560 static int control_isnotrecmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4562 uint32_t mypnn
, recmaster
;
4565 assert_single_node_only();
4567 mypnn
= getpnn(ctdb
);
4569 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
4571 printf("Failed to get the recmaster\n");
4575 if (recmaster
!= mypnn
) {
4576 printf("this node is not the recmaster\n");
4580 printf("this node is the recmaster\n");
4587 static int control_ping(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4590 struct timeval tv
= timeval_current();
4591 ret
= ctdb_ctrl_ping(ctdb
, options
.pnn
);
4593 printf("Unable to get ping response from node %u\n", options
.pnn
);
4596 printf("response from %u time=%.6f sec (%d clients)\n",
4597 options
.pnn
, timeval_elapsed(&tv
), ret
);
4604 get a node's runstate
4606 static int control_runstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4609 enum ctdb_runstate runstate
;
4611 ret
= ctdb_ctrl_get_runstate(ctdb
, TIMELIMIT(), options
.pnn
, &runstate
);
4613 printf("Unable to get runstate response from node %u\n",
4618 enum ctdb_runstate t
;
4620 for (i
=0; i
<argc
; i
++) {
4622 t
= runstate_from_string(argv
[i
]);
4623 if (t
== CTDB_RUNSTATE_UNKNOWN
) {
4624 printf("Invalid run state (%s)\n", argv
[i
]);
4628 if (t
== runstate
) {
4635 printf("CTDB not in required run state (got %s)\n",
4636 runstate_to_string((enum ctdb_runstate
)runstate
));
4641 printf("%s\n", runstate_to_string(runstate
));
4649 static int control_getvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4660 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, &value
);
4662 DEBUG(DEBUG_ERR
, ("Unable to get tunable variable '%s'\n", name
));
4666 printf("%-23s = %u\n", name
, value
);
4673 static int control_setvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4684 value
= strtoul(argv
[1], NULL
, 0);
4686 ret
= ctdb_ctrl_set_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, value
);
4688 DEBUG(DEBUG_ERR
, ("Unable to set tunable variable '%s'\n", name
));
4692 DEBUG(DEBUG_WARNING
,
4693 ("Setting obsolete tunable variable '%s'\n",
4702 static int control_listvars(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4708 ret
= ctdb_ctrl_list_tunables(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &list
, &count
);
4710 DEBUG(DEBUG_ERR
, ("Unable to list tunable variables\n"));
4714 for (i
=0;i
<count
;i
++) {
4715 control_getvar(ctdb
, 1, &list
[i
]);
4724 display debug level on a node
4726 static int control_getdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4731 ret
= ctdb_ctrl_get_debuglevel(ctdb
, options
.pnn
, &level
);
4733 DEBUG(DEBUG_ERR
, ("Unable to get debuglevel response from node %u\n", options
.pnn
));
4736 enum debug_level log_level
= debug_level_from_int(level
);
4737 const char *desc
= debug_level_to_string(log_level
);
4739 /* This should never happen */
4742 if (options
.machinereadable
){
4743 printm(":Name:Level:\n");
4744 printm(":%s:%d:\n", desc
, level
);
4746 printf("Node %u is at debug level %s (%d)\n",
4747 options
.pnn
, desc
, level
);
4754 display reclock file of a node
4756 static int control_getreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4759 const char *reclock
;
4761 ret
= ctdb_ctrl_getreclock(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &reclock
);
4763 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4766 if (options
.machinereadable
){
4767 if (reclock
!= NULL
) {
4768 printm("%s", reclock
);
4771 if (reclock
== NULL
) {
4772 printf("No reclock file used.\n");
4774 printf("Reclock file:%s\n", reclock
);
4782 set the reclock file of a node
4784 static int control_setreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4787 const char *reclock
= NULL
;
4791 } else if (argc
== 1) {
4797 ret
= ctdb_ctrl_setreclock(ctdb
, TIMELIMIT(), options
.pnn
, reclock
);
4799 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4806 set the lmaster role on/off
4808 static int control_setlmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4811 uint32_t lmasterrole
= 0;
4817 if (!strcmp(argv
[0], "on")) {
4819 } else if (!strcmp(argv
[0], "off")) {
4825 ret
= ctdb_ctrl_setlmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, lmasterrole
);
4827 DEBUG(DEBUG_ERR
, ("Unable to set the lmaster role for node %u\n", options
.pnn
));
4835 set the recmaster role on/off
4837 static int control_setrecmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4840 uint32_t recmasterrole
= 0;
4846 if (!strcmp(argv
[0], "on")) {
4848 } else if (!strcmp(argv
[0], "off")) {
4854 ret
= ctdb_ctrl_setrecmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, recmasterrole
);
4856 DEBUG(DEBUG_ERR
, ("Unable to set the recmaster role for node %u\n", options
.pnn
));
4864 set debug level on a node or all nodes
4866 static int control_setdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4870 enum debug_level log_level
;
4873 printf("You must specify the debug level. Valid levels are:\n");
4874 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4878 if (!debug_level_parse(argv
[0], &log_level
)) {
4879 printf("Invalid debug level, must be one of\n");
4880 printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
4884 level
= debug_level_to_int(log_level
);
4886 ret
= ctdb_ctrl_set_debuglevel(ctdb
, options
.pnn
, level
);
4888 DEBUG(DEBUG_ERR
, ("Unable to set debug level on node %u\n", options
.pnn
));
4897 static int control_thaw(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4903 priority
= strtol(argv
[0], NULL
, 0);
4907 DEBUG(DEBUG_ERR
,("Thaw by priority %u\n", priority
));
4909 ret
= ctdb_ctrl_thaw_priority(ctdb
, TIMELIMIT(), options
.pnn
, priority
);
4911 DEBUG(DEBUG_ERR
, ("Unable to thaw node %u\n", options
.pnn
));
4918 attach to a database
4920 static int control_attach(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4922 const char *db_name
;
4923 struct ctdb_db_context
*ctdb_db
;
4924 bool persistent
= false;
4934 if (strcmp(argv
[1], "persistent") != 0) {
4940 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4941 if (ctdb_db
== NULL
) {
4942 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4950 * detach from a database
4952 static int control_detach(struct ctdb_context
*ctdb
, int argc
,
4957 int ret
, i
, status
= 0;
4958 struct ctdb_node_map_old
*nodemap
= NULL
;
4959 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4966 assert_single_node_only();
4968 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
,
4971 DEBUG(DEBUG_ERR
, ("Database cannot be detached "
4972 "when recovery is active\n"));
4973 talloc_free(tmp_ctx
);
4977 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
4980 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
4982 talloc_free(tmp_ctx
);
4986 for (i
=0; i
<nodemap
->num
; i
++) {
4989 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
4993 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
4997 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
4998 DEBUG(DEBUG_ERR
, ("Database cannot be detached on "
4999 "inactive (stopped or banned) node "
5000 "%u\n", nodemap
->nodes
[i
].pnn
));
5001 talloc_free(tmp_ctx
);
5005 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(),
5006 nodemap
->nodes
[i
].pnn
,
5007 "AllowClientDBAttach",
5010 DEBUG(DEBUG_ERR
, ("Unable to get tunable "
5011 "AllowClientDBAttach from node %u\n",
5012 nodemap
->nodes
[i
].pnn
));
5013 talloc_free(tmp_ctx
);
5018 DEBUG(DEBUG_ERR
, ("Database access is still active on "
5019 "node %u. Set AllowClientDBAttach=0 "
5021 nodemap
->nodes
[i
].pnn
));
5022 talloc_free(tmp_ctx
);
5027 talloc_free(tmp_ctx
);
5029 for (i
=0; i
<argc
; i
++) {
5030 if (!db_exists(ctdb
, argv
[i
], &db_id
, NULL
, &flags
)) {
5034 if (flags
& CTDB_DB_FLAGS_PERSISTENT
) {
5035 DEBUG(DEBUG_ERR
, ("Persistent database '%s' "
5036 "cannot be detached\n", argv
[i
]));
5041 ret
= ctdb_detach(ctdb
, db_id
);
5043 DEBUG(DEBUG_ERR
, ("Database '%s' detach failed\n",
5055 static int control_setdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5057 struct ctdb_db_priority db_prio
;
5064 db_prio
.db_id
= strtoul(argv
[0], NULL
, 0);
5065 db_prio
.priority
= strtoul(argv
[1], NULL
, 0);
5067 ret
= ctdb_ctrl_set_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, &db_prio
);
5069 DEBUG(DEBUG_ERR
,("Unable to set db prio\n"));
5079 static int control_getdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5081 uint32_t db_id
, priority
;
5088 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5092 ret
= ctdb_ctrl_get_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &priority
);
5094 DEBUG(DEBUG_ERR
,("Unable to get db prio\n"));
5098 DEBUG(DEBUG_ERR
,("Priority:%u\n", priority
));
5104 set the sticky records capability for a database
5106 static int control_setdbsticky(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5108 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5116 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5120 ret
= ctdb_ctrl_set_db_sticky(ctdb
, options
.pnn
, db_id
);
5122 DEBUG(DEBUG_ERR
,("Unable to set db to support sticky records\n"));
5123 talloc_free(tmp_ctx
);
5127 talloc_free(tmp_ctx
);
5132 set the readonly capability for a database
5134 static int control_setdbreadonly(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5136 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5144 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5148 ret
= ctdb_ctrl_set_db_readonly(ctdb
, options
.pnn
, db_id
);
5150 DEBUG(DEBUG_ERR
,("Unable to set db to support readonly\n"));
5151 talloc_free(tmp_ctx
);
5155 talloc_free(tmp_ctx
);
5162 static int control_getdbseqnum(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5172 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5176 ret
= ctdb_ctrl_getdbseqnum(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &seqnum
);
5178 DEBUG(DEBUG_ERR
, ("Unable to get seqnum from node."));
5182 printf("Sequence number:%lld\n", (long long)seqnum
);
5188 run an eventscript on a node
5190 static int control_eventscript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5196 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5199 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5203 data
.dptr
= (unsigned char *)discard_const(argv
[0]);
5204 data
.dsize
= strlen((char *)data
.dptr
) + 1;
5206 DEBUG(DEBUG_ERR
, ("Running eventscripts with arguments \"%s\" on node %u\n", data
.dptr
, options
.pnn
));
5208 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS
,
5209 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
5210 if (ret
!= 0 || res
!= 0) {
5211 if (errmsg
!= NULL
) {
5213 ("Failed to run eventscripts - %s\n", errmsg
));
5215 DEBUG(DEBUG_ERR
, ("Failed to run eventscripts\n"));
5217 talloc_free(tmp_ctx
);
5220 talloc_free(tmp_ctx
);
5224 #define DB_VERSION 1
5225 #define MAX_DB_NAME 64
5226 struct db_file_header
{
5227 unsigned long version
;
5229 unsigned long persistent
;
5231 const char name
[MAX_DB_NAME
];
5234 struct backup_data
{
5235 struct ctdb_marshall_buffer
*records
;
5238 bool traverse_error
;
5241 static int backup_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private)
5243 struct backup_data
*bd
= talloc_get_type(private, struct backup_data
);
5244 struct ctdb_rec_data_old
*rec
;
5246 /* add the record */
5247 rec
= ctdb_marshall_record(bd
->records
, 0, key
, NULL
, data
);
5249 bd
->traverse_error
= true;
5250 DEBUG(DEBUG_ERR
,("Failed to marshall record\n"));
5253 bd
->records
= talloc_realloc_size(NULL
, bd
->records
, rec
->length
+ bd
->len
);
5254 if (bd
->records
== NULL
) {
5255 DEBUG(DEBUG_ERR
,("Failed to expand marshalling buffer\n"));
5256 bd
->traverse_error
= true;
5259 bd
->records
->count
++;
5260 memcpy(bd
->len
+(uint8_t *)bd
->records
, rec
, rec
->length
);
5261 bd
->len
+= rec
->length
;
5269 * backup a database to a file
5271 static int control_backupdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5273 const char *db_name
;
5275 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5276 struct db_file_header dbhdr
;
5277 struct ctdb_db_context
*ctdb_db
;
5278 struct backup_data
*bd
;
5281 const char *reason
= NULL
;
5285 assert_single_node_only();
5288 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5292 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
5296 ret
= ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
5297 db_id
, tmp_ctx
, &reason
);
5299 DEBUG(DEBUG_ERR
,("Unable to get dbhealth for database '%s'\n",
5301 talloc_free(tmp_ctx
);
5305 uint32_t allow_unhealthy
= 0;
5307 ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
,
5308 "AllowUnhealthyDBRead",
5311 if (allow_unhealthy
!= 1) {
5312 DEBUG(DEBUG_ERR
,("database '%s' is unhealthy: %s\n",
5315 DEBUG(DEBUG_ERR
,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5317 talloc_free(tmp_ctx
);
5321 DEBUG(DEBUG_WARNING
,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5323 DEBUG(DEBUG_WARNING
,("WARNING! allow backup of unhealthy database: "
5324 "tunnable AllowUnhealthyDBRead = %u\n",
5328 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5329 if (ctdb_db
== NULL
) {
5330 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", argv
[0]));
5331 talloc_free(tmp_ctx
);
5336 ret
= tdb_transaction_start(ctdb_db
->ltdb
->tdb
);
5338 DEBUG(DEBUG_ERR
,("Failed to start transaction\n"));
5339 talloc_free(tmp_ctx
);
5344 bd
= talloc_zero(tmp_ctx
, struct backup_data
);
5346 DEBUG(DEBUG_ERR
,("Failed to allocate backup_data\n"));
5347 talloc_free(tmp_ctx
);
5351 bd
->records
= talloc_zero(bd
, struct ctdb_marshall_buffer
);
5352 if (bd
->records
== NULL
) {
5353 DEBUG(DEBUG_ERR
,("Failed to allocate ctdb_marshall_buffer\n"));
5354 talloc_free(tmp_ctx
);
5358 bd
->len
= offsetof(struct ctdb_marshall_buffer
, data
);
5359 bd
->records
->db_id
= ctdb_db
->db_id
;
5360 /* traverse the database collecting all records */
5361 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, backup_traverse
, bd
) == -1 ||
5362 bd
->traverse_error
) {
5363 DEBUG(DEBUG_ERR
,("Traverse error\n"));
5364 talloc_free(tmp_ctx
);
5368 tdb_transaction_cancel(ctdb_db
->ltdb
->tdb
);
5371 fh
= open(argv
[1], O_RDWR
|O_CREAT
, 0600);
5373 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[1]));
5374 talloc_free(tmp_ctx
);
5379 dbhdr
.version
= DB_VERSION
;
5380 dbhdr
.timestamp
= time(NULL
);
5381 dbhdr
.persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
5382 dbhdr
.size
= bd
->len
;
5383 if (strlen(argv
[0]) >= MAX_DB_NAME
) {
5384 DEBUG(DEBUG_ERR
,("Too long dbname\n"));
5387 strncpy(discard_const(dbhdr
.name
), argv
[0], MAX_DB_NAME
-1);
5388 ret
= sys_write(fh
, &dbhdr
, sizeof(dbhdr
));
5390 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5393 ret
= sys_write(fh
, bd
->records
, bd
->len
);
5395 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5404 DEBUG(DEBUG_ERR
,("close failed: %s\n", strerror(errno
)));
5408 DEBUG(DEBUG_ERR
,("Database backed up to %s\n", argv
[1]));
5410 talloc_free(tmp_ctx
);
5415 * restore a database from a file
5417 static int control_restoredb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5420 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5423 struct db_file_header dbhdr
;
5424 struct ctdb_db_context
*ctdb_db
;
5425 struct ctdb_node_map_old
*nodemap
=NULL
;
5426 struct ctdb_vnn_map
*vnnmap
=NULL
;
5428 struct ctdb_transdb w
;
5430 uint32_t generation
;
5435 assert_single_node_only();
5437 if (argc
< 1 || argc
> 2) {
5438 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5442 fh
= open(argv
[0], O_RDONLY
);
5444 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5445 talloc_free(tmp_ctx
);
5449 sys_read(fh
, &dbhdr
, sizeof(dbhdr
));
5450 if (dbhdr
.version
!= DB_VERSION
) {
5451 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5453 talloc_free(tmp_ctx
);
5457 dbname
= discard_const(dbhdr
.name
);
5459 dbname
= discard_const(argv
[1]);
5462 outdata
.dsize
= dbhdr
.size
;
5463 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5464 if (outdata
.dptr
== NULL
) {
5465 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5467 talloc_free(tmp_ctx
);
5470 sys_read(fh
, outdata
.dptr
, outdata
.dsize
);
5473 tm
= localtime(&dbhdr
.timestamp
);
5474 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5475 printf("Restoring database '%s' from backup @ %s\n",
5479 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), dbname
, dbhdr
.persistent
, 0);
5480 if (ctdb_db
== NULL
) {
5481 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", dbname
));
5482 talloc_free(tmp_ctx
);
5486 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &nodemap
);
5488 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
5489 talloc_free(tmp_ctx
);
5494 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
5496 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
5497 talloc_free(tmp_ctx
);
5501 /* freeze all nodes */
5502 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5503 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5504 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5510 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5511 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5512 talloc_free(tmp_ctx
);
5517 generation
= vnnmap
->generation
;
5518 data
.dptr
= (void *)&generation
;
5519 data
.dsize
= sizeof(generation
);
5521 /* start a cluster wide transaction */
5522 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5523 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5525 TIMELIMIT(), false, data
,
5528 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide transactions.\n"));
5533 w
.db_id
= ctdb_db
->db_id
;
5536 data
.dptr
= (void *)&w
;
5537 data
.dsize
= sizeof(w
);
5539 /* wipe all the remote databases. */
5540 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5541 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5543 TIMELIMIT(), false, data
,
5546 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5547 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5548 talloc_free(tmp_ctx
);
5552 /* push the database */
5553 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5554 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_PUSH_DB
,
5556 TIMELIMIT(), false, outdata
,
5559 DEBUG(DEBUG_ERR
, ("Failed to push database.\n"));
5560 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5561 talloc_free(tmp_ctx
);
5565 data
.dptr
= (void *)&ctdb_db
->db_id
;
5566 data
.dsize
= sizeof(ctdb_db
->db_id
);
5568 /* mark the database as healthy */
5569 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5570 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5572 TIMELIMIT(), false, data
,
5575 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5576 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5577 talloc_free(tmp_ctx
);
5581 data
.dptr
= (void *)&generation
;
5582 data
.dsize
= sizeof(generation
);
5584 /* commit all the changes */
5585 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5587 TIMELIMIT(), false, data
,
5590 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5591 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5592 talloc_free(tmp_ctx
);
5597 /* thaw all nodes */
5598 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5599 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5605 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5606 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5607 talloc_free(tmp_ctx
);
5612 talloc_free(tmp_ctx
);
5617 * dump a database backup from a file
5619 static int control_dumpdbbackup(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5621 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5623 struct db_file_header dbhdr
;
5627 struct ctdb_rec_data_old
*rec
= NULL
;
5628 struct ctdb_marshall_buffer
*m
;
5629 struct ctdb_dump_db_context c
;
5631 assert_single_node_only();
5634 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5638 fh
= open(argv
[0], O_RDONLY
);
5640 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5641 talloc_free(tmp_ctx
);
5645 sys_read(fh
, &dbhdr
, sizeof(dbhdr
));
5646 if (dbhdr
.version
!= DB_VERSION
) {
5647 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5649 talloc_free(tmp_ctx
);
5653 outdata
.dsize
= dbhdr
.size
;
5654 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5655 if (outdata
.dptr
== NULL
) {
5656 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5658 talloc_free(tmp_ctx
);
5661 sys_read(fh
, outdata
.dptr
, outdata
.dsize
);
5663 m
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
5665 tm
= localtime(&dbhdr
.timestamp
);
5666 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5667 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5668 dbhdr
.name
, m
->db_id
, tbuf
);
5673 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
5674 c
.printdatasize
= (bool)options
.printdatasize
;
5675 c
.printlmaster
= false;
5676 c
.printhash
= (bool)options
.printhash
;
5677 c
.printrecordflags
= (bool)options
.printrecordflags
;
5679 for (i
=0; i
< m
->count
; i
++) {
5683 /* we do not want the header splitted, so we pass NULL*/
5684 rec
= ctdb_marshall_loop_next(m
, rec
, &reqid
,
5687 ctdb_dumpdb_record(key
, data
, &c
);
5690 printf("Dumped %d records\n", i
);
5691 talloc_free(tmp_ctx
);
5696 * wipe a database from a file
5698 static int control_wipedb(struct ctdb_context
*ctdb
, int argc
,
5701 const char *db_name
;
5703 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5705 struct ctdb_db_context
*ctdb_db
;
5706 struct ctdb_node_map_old
*nodemap
= NULL
;
5707 struct ctdb_vnn_map
*vnnmap
= NULL
;
5709 struct ctdb_transdb w
;
5711 uint32_t generation
;
5714 assert_single_node_only();
5717 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5721 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
5725 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5726 if (ctdb_db
== NULL
) {
5727 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n",
5729 talloc_free(tmp_ctx
);
5733 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
,
5736 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5738 talloc_free(tmp_ctx
);
5742 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5745 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
5747 talloc_free(tmp_ctx
);
5751 /* freeze all nodes */
5752 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5753 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5754 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5761 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5762 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
,
5763 CTDB_RECOVERY_ACTIVE
);
5764 talloc_free(tmp_ctx
);
5769 generation
= vnnmap
->generation
;
5770 data
.dptr
= (void *)&generation
;
5771 data
.dsize
= sizeof(generation
);
5773 /* start a cluster wide transaction */
5774 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5775 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5777 TIMELIMIT(), false, data
,
5781 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide "
5782 "transactions.\n"));
5786 w
.db_id
= ctdb_db
->db_id
;
5789 data
.dptr
= (void *)&w
;
5790 data
.dsize
= sizeof(w
);
5792 /* wipe all the remote databases. */
5793 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5794 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5796 TIMELIMIT(), false, data
,
5799 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5800 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5801 talloc_free(tmp_ctx
);
5805 data
.dptr
= (void *)&ctdb_db
->db_id
;
5806 data
.dsize
= sizeof(ctdb_db
->db_id
);
5808 /* mark the database as healthy */
5809 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5810 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5812 TIMELIMIT(), false, data
,
5815 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5816 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5817 talloc_free(tmp_ctx
);
5821 data
.dptr
= (void *)&generation
;
5822 data
.dsize
= sizeof(generation
);
5824 /* commit all the changes */
5825 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5827 TIMELIMIT(), false, data
,
5830 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5831 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5832 talloc_free(tmp_ctx
);
5836 /* thaw all nodes */
5837 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5838 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5844 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5845 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5846 talloc_free(tmp_ctx
);
5850 DEBUG(DEBUG_ERR
, ("Database wiped.\n"));
5852 talloc_free(tmp_ctx
);
5859 static int control_dumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5865 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5866 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_DUMP_MEMORY
,
5867 0, tdb_null
, tmp_ctx
, &data
, &res
, NULL
, &errmsg
);
5868 if (ret
!= 0 || res
!= 0) {
5869 DEBUG(DEBUG_ERR
,("Failed to dump memory - %s\n", errmsg
));
5870 talloc_free(tmp_ctx
);
5873 sys_write(1, data
.dptr
, data
.dsize
);
5874 talloc_free(tmp_ctx
);
5879 handler for memory dumps
5881 static void mem_dump_handler(uint64_t srvid
, TDB_DATA data
, void *private_data
)
5883 sys_write(1, data
.dptr
, data
.dsize
);
5888 dump memory usage on the recovery daemon
5890 static int control_rddumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5894 struct ctdb_srvid_message rd
;
5896 rd
.pnn
= ctdb_get_pnn(ctdb
);
5897 rd
.srvid
= getpid();
5899 /* register a message port for receiveing the reply so that we
5900 can receive the reply
5902 ctdb_client_set_message_handler(ctdb
, rd
.srvid
, mem_dump_handler
, NULL
);
5905 data
.dptr
= (uint8_t *)&rd
;
5906 data
.dsize
= sizeof(rd
);
5908 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_MEM_DUMP
, data
);
5910 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
5914 /* this loop will terminate when we have received the reply */
5916 tevent_loop_once(ctdb
->ev
);
5923 send a message to a srvid
5925 static int control_msgsend(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5927 unsigned long srvid
;
5935 srvid
= strtoul(argv
[0], NULL
, 0);
5937 data
.dptr
= (uint8_t *)discard_const(argv
[1]);
5938 data
.dsize
= strlen(argv
[1]);
5940 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, srvid
, data
);
5942 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
5950 handler for msglisten
5952 static void msglisten_handler(uint64_t srvid
, TDB_DATA data
,
5957 printf("Message received: ");
5958 for (i
=0;i
<data
.dsize
;i
++) {
5959 printf("%c", data
.dptr
[i
]);
5965 listen for messages on a messageport
5967 static int control_msglisten(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5973 /* register a message port and listen for messages
5975 ctdb_client_set_message_handler(ctdb
, srvid
, msglisten_handler
, NULL
);
5976 printf("Listening for messages on srvid:%d\n", (int)srvid
);
5979 tevent_loop_once(ctdb
->ev
);
5986 list all nodes in the cluster
5987 we parse the nodes file directly
5989 static int control_listnodes(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5991 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
5992 struct ctdb_node_map_old
*node_map
;
5995 assert_single_node_only();
5997 node_map
= read_nodes_file(mem_ctx
);
5998 if (node_map
== NULL
) {
5999 talloc_free(mem_ctx
);
6003 for (i
= 0; i
< node_map
->num
; i
++) {
6006 if (node_map
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
6009 addr
= ctdb_addr_to_str(&node_map
->nodes
[i
].addr
);
6010 if (options
.machinereadable
){
6011 printm(":%d:%s:\n", node_map
->nodes
[i
].pnn
, addr
);
6013 printf("%s\n", addr
);
6016 talloc_free(mem_ctx
);
6021 /**********************************************************************/
6022 /* reload the nodes file on all nodes */
6024 static void get_nodes_files_callback(struct ctdb_context
*ctdb
,
6025 uint32_t node_pnn
, int32_t res
,
6026 TDB_DATA outdata
, void *callback_data
)
6028 struct ctdb_node_map_old
**maps
=
6029 talloc_get_type(callback_data
, struct ctdb_node_map_old
*);
6031 if (outdata
.dsize
< offsetof(struct ctdb_node_map_old
, nodes
) ||
6032 outdata
.dptr
== NULL
) {
6034 (__location__
" Invalid return data: %u %p\n",
6035 (unsigned)outdata
.dsize
, outdata
.dptr
));
6039 if (node_pnn
>= talloc_array_length(maps
)) {
6041 (__location__
" unexpected PNN %u\n", node_pnn
));
6045 maps
[node_pnn
] = talloc_memdup(maps
, outdata
.dptr
, outdata
.dsize
);
6048 static void get_nodes_files_fail_callback(struct ctdb_context
*ctdb
,
6049 uint32_t node_pnn
, int32_t res
,
6050 TDB_DATA outdata
, void *callback_data
)
6053 ("ERROR: Failed to get nodes file from node %u\n", node_pnn
));
6056 static struct ctdb_node_map_old
**
6057 ctdb_get_nodes_files(struct ctdb_context
*ctdb
,
6058 TALLOC_CTX
*mem_ctx
,
6059 struct timeval timeout
,
6060 struct ctdb_node_map_old
*nodemap
)
6064 struct ctdb_node_map_old
**maps
;
6066 maps
= talloc_zero_array(mem_ctx
, struct ctdb_node_map_old
*, nodemap
->num
);
6067 CTDB_NO_MEMORY_NULL(ctdb
, maps
);
6069 nodes
= list_of_connected_nodes(ctdb
, nodemap
, mem_ctx
, true);
6071 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_GET_NODES_FILE
,
6072 nodes
, 0, TIMELIMIT(),
6074 get_nodes_files_callback
,
6075 get_nodes_files_fail_callback
,
6085 static bool node_files_are_identical(struct ctdb_node_map_old
*nm1
,
6086 struct ctdb_node_map_old
*nm2
)
6090 if (nm1
->num
!= nm2
->num
) {
6093 for (i
= 0; i
< nm1
->num
; i
++) {
6094 if (memcmp(&nm1
->nodes
[i
], &nm2
->nodes
[i
],
6095 sizeof(struct ctdb_node_and_flags
)) != 0) {
6103 static bool check_all_node_files_are_identical(struct ctdb_context
*ctdb
,
6104 TALLOC_CTX
*mem_ctx
,
6105 struct timeval timeout
,
6106 struct ctdb_node_map_old
*nodemap
,
6107 struct ctdb_node_map_old
*file_nodemap
)
6109 static struct ctdb_node_map_old
**maps
;
6113 maps
= ctdb_get_nodes_files(ctdb
, mem_ctx
, timeout
, nodemap
);
6118 for (i
= 0; i
< talloc_array_length(maps
); i
++) {
6119 if (maps
[i
] == NULL
) {
6122 if (!node_files_are_identical(file_nodemap
, maps
[i
])) {
6124 ("ERROR: Node file on node %u differs from current node (%u)\n",
6125 i
, ctdb_get_pnn(ctdb
)));
6134 reload the nodes file on the local node
6136 static bool sanity_check_nodes_file_changes(TALLOC_CTX
*mem_ctx
,
6137 struct ctdb_node_map_old
*nodemap
,
6138 struct ctdb_node_map_old
*file_nodemap
)
6141 bool should_abort
= false;
6142 bool have_changes
= false;
6144 for (i
=0; i
<nodemap
->num
; i
++) {
6145 if (i
>= file_nodemap
->num
) {
6147 ("ERROR: Node %u (%s) missing from nodes file\n",
6148 nodemap
->nodes
[i
].pnn
,
6149 ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
)));
6150 should_abort
= true;
6153 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
&&
6154 file_nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
6155 /* Node remains deleted */
6157 ("Node %u is unchanged (DELETED)\n",
6158 nodemap
->nodes
[i
].pnn
));
6159 } else if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) &&
6160 !(file_nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
)) {
6161 /* Node not newly nor previously deleted */
6162 if (!ctdb_same_ip(&nodemap
->nodes
[i
].addr
,
6163 &file_nodemap
->nodes
[i
].addr
)) {
6165 ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
6166 nodemap
->nodes
[i
].pnn
,
6167 /* ctdb_addr_to_str() returns a static */
6168 talloc_strdup(mem_ctx
,
6169 ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
)),
6170 ctdb_addr_to_str(&file_nodemap
->nodes
[i
].addr
)));
6171 should_abort
= true;
6174 ("Node %u is unchanged\n",
6175 nodemap
->nodes
[i
].pnn
));
6176 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
6177 DEBUG(DEBUG_WARNING
,
6178 ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
6179 nodemap
->nodes
[i
].pnn
));
6182 } else if (file_nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
6183 /* Node is being deleted */
6185 ("Node %u is DELETED\n",
6186 nodemap
->nodes
[i
].pnn
));
6187 have_changes
= true;
6188 if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
)) {
6190 ("ERROR: Node %u is still connected\n",
6191 nodemap
->nodes
[i
].pnn
));
6192 should_abort
= true;
6194 } else if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
6195 /* Node was previously deleted */
6197 ("Node %u is UNDELETED\n", nodemap
->nodes
[i
].pnn
));
6198 have_changes
= true;
6204 ("ERROR: Nodes will not be reloaded due to previous error\n"));
6205 talloc_free(mem_ctx
);
6209 /* Leftover nodes in file are NEW */
6210 for (; i
< file_nodemap
->num
; i
++) {
6211 DEBUG(DEBUG_NOTICE
, ("Node %u is NEW\n",
6212 file_nodemap
->nodes
[i
].pnn
));
6213 have_changes
= true;
6216 return have_changes
;
6219 static void reload_nodes_fail_callback(struct ctdb_context
*ctdb
,
6220 uint32_t node_pnn
, int32_t res
,
6221 TDB_DATA outdata
, void *callback_data
)
6223 DEBUG(DEBUG_WARNING
,
6224 ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
6228 static int control_reload_nodes_file(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6231 struct ctdb_node_map_old
*nodemap
=NULL
;
6232 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
6233 struct ctdb_node_map_old
*file_nodemap
;
6237 assert_current_node_only(ctdb
);
6239 /* Load both the current nodemap and the contents of the local
6240 * nodes file. Compare and sanity check them before doing
6243 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
6245 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
6249 file_nodemap
= read_nodes_file(tmp_ctx
);
6250 if (file_nodemap
== NULL
) {
6251 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
6252 talloc_free(tmp_ctx
);
6256 if (!check_all_node_files_are_identical(ctdb
, tmp_ctx
, TIMELIMIT(),
6257 nodemap
, file_nodemap
)) {
6261 if (!sanity_check_nodes_file_changes(tmp_ctx
, nodemap
, file_nodemap
)) {
6263 ("No change in nodes file, skipping unnecessary reload\n"));
6264 talloc_free(tmp_ctx
);
6268 /* Now make the changes */
6269 conn
= list_of_connected_nodes(ctdb
, nodemap
, tmp_ctx
, true);
6270 for (i
= 0; i
< talloc_array_length(conn
); i
++) {
6271 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n",
6275 /* Another timeout could be used, such as ReRecoveryTimeout or
6276 * a new one for this purpose. However, this is the simplest
6278 timeout
= options
.timelimit
;
6279 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_RECOVERIES
, &timeout
,
6280 "Disable recoveries", true);
6283 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELOAD_NODES_FILE
,
6284 conn
, 0, TIMELIMIT(),
6286 NULL
, reload_nodes_fail_callback
,
6290 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_RECOVERIES
, &timeout
,
6291 "Enable recoveries", true);
6293 talloc_free(tmp_ctx
);
6299 static const struct {
6301 int (*fn
)(struct ctdb_context
*, int, const char **);
6303 bool without_daemon
; /* can be run without daemon running ? */
6306 } ctdb_commands
[] = {
6307 { "version", control_version
, true, true, "show version of ctdb" },
6308 { "status", control_status
, true, false, "show node status" },
6309 { "uptime", control_uptime
, true, false, "show node uptime" },
6310 { "ping", control_ping
, true, false, "ping all nodes" },
6311 { "runstate", control_runstate
, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6312 { "getvar", control_getvar
, true, false, "get a tunable variable", "<name>"},
6313 { "setvar", control_setvar
, true, false, "set a tunable variable", "<name> <value>"},
6314 { "listvars", control_listvars
, true, false, "list tunable variables"},
6315 { "statistics", control_statistics
, false, false, "show statistics" },
6316 { "statisticsreset", control_statistics_reset
, true, false, "reset statistics"},
6317 { "stats", control_stats
, false, false, "show rolling statistics", "[number of history records]" },
6318 { "ip", control_ip
, false, false, "show which public ip's that ctdb manages" },
6319 { "ipinfo", control_ipinfo
, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6320 { "ifaces", control_ifaces
, true, false, "show which interfaces that ctdb manages" },
6321 { "setifacelink", control_setifacelink
, true, false, "set interface link status", "<iface> <status>" },
6322 { "process-exists", control_process_exists
, true, false, "check if a process exists on a node", "<pid>"},
6323 { "getdbmap", control_getdbmap
, true, false, "show the database map" },
6324 { "getdbstatus", control_getdbstatus
, true, false, "show the status of a database", "<dbname|dbid>" },
6325 { "catdb", control_catdb
, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6326 { "cattdb", control_cattdb
, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6327 { "getmonmode", control_getmonmode
, true, false, "show monitoring mode" },
6328 { "getcapabilities", control_getcapabilities
, true, false, "show node capabilities" },
6329 { "pnn", control_pnn
, true, false, "show the pnn of the currnet node" },
6330 { "lvs", control_lvs
, true, false, "show lvs configuration" },
6331 { "lvsmaster", control_lvsmaster
, true, false, "show which node is the lvs master" },
6332 { "disablemonitor", control_disable_monmode
,true, false, "set monitoring mode to DISABLE" },
6333 { "enablemonitor", control_enable_monmode
, true, false, "set monitoring mode to ACTIVE" },
6334 { "setdebug", control_setdebug
, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6335 { "getdebug", control_getdebug
, true, false, "get debug level" },
6336 { "attach", control_attach
, true, false, "attach to a database", "<dbname> [persistent]" },
6337 { "detach", control_detach
, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6338 { "dumpmemory", control_dumpmemory
, true, false, "dump memory map to stdout" },
6339 { "rddumpmemory", control_rddumpmemory
, true, false, "dump memory map from the recovery daemon to stdout" },
6340 { "getpid", control_getpid
, true, false, "get ctdbd process ID" },
6341 { "disable", control_disable
, true, false, "disable a nodes public IP" },
6342 { "enable", control_enable
, true, false, "enable a nodes public IP" },
6343 { "stop", control_stop
, true, false, "stop a node" },
6344 { "continue", control_continue
, true, false, "re-start a stopped node" },
6345 { "ban", control_ban
, true, false, "ban a node from the cluster", "<bantime>"},
6346 { "unban", control_unban
, true, false, "unban a node" },
6347 { "showban", control_showban
, true, false, "show ban information"},
6348 { "shutdown", control_shutdown
, true, false, "shutdown ctdbd" },
6349 { "recover", control_recover
, true, false, "force recovery" },
6350 { "sync", control_ipreallocate
, false, false, "wait until ctdbd has synced all state changes" },
6351 { "ipreallocate", control_ipreallocate
, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6352 { "thaw", control_thaw
, true, false, "thaw databases", "[priority:1-3]" },
6353 { "isnotrecmaster", control_isnotrecmaster
, false, false, "check if the local node is recmaster or not" },
6354 { "killtcp", kill_tcp
, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6355 { "gratiousarp", control_gratious_arp
, false, false, "send a gratious arp", "<ip> <interface>" },
6356 { "tickle", tickle_tcp
, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6357 { "gettickles", control_get_tickles
, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6358 { "addtickle", control_add_tickle
, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6360 { "deltickle", control_del_tickle
, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6362 { "regsrvid", regsrvid
, false, false, "register a server id", "<pnn> <type> <id>" },
6363 { "unregsrvid", unregsrvid
, false, false, "unregister a server id", "<pnn> <type> <id>" },
6364 { "chksrvid", chksrvid
, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6365 { "getsrvids", getsrvids
, false, false, "get a list of all server ids"},
6366 { "check_srvids", check_srvids
, false, false, "check if a srvid exists", "<id>+" },
6367 { "listnodes", control_listnodes
, false, true, "list all nodes in the cluster"},
6368 { "reloadnodes", control_reload_nodes_file
, false, false, "reload the nodes file and restart the transport on all nodes"},
6369 { "moveip", control_moveip
, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6370 { "rebalanceip", control_rebalanceip
, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6371 { "addip", control_addip
, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6372 { "delip", control_delip
, false, false, "delete an ip address from a node", "<ip>"},
6373 { "eventscript", control_eventscript
, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6374 { "backupdb", control_backupdb
, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6375 { "restoredb", control_restoredb
, false, false, "restore the database from a file.", "<file> [dbname]"},
6376 { "dumpdbbackup", control_dumpdbbackup
, false, true, "dump database backup from a file.", "<file>"},
6377 { "wipedb", control_wipedb
, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6378 { "recmaster", control_recmaster
, true, false, "show the pnn for the recovery master."},
6379 { "scriptstatus", control_scriptstatus
, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6380 { "enablescript", control_enablescript
, true, false, "enable an eventscript", "<script>"},
6381 { "disablescript", control_disablescript
, true, false, "disable an eventscript", "<script>"},
6382 { "natgwlist", control_natgwlist
, false, true, "show the nodes belonging to this natgw configuration"},
6383 { "xpnn", control_xpnn
, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6384 { "getreclock", control_getreclock
, true, false, "Show the reclock file of a node"},
6385 { "setreclock", control_setreclock
, true, false, "Set/clear the reclock file of a node", "[filename]"},
6386 { "setlmasterrole", control_setlmasterrole
, false, false, "Set LMASTER role to on/off", "{on|off}"},
6387 { "setrecmasterrole", control_setrecmasterrole
, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6388 { "setdbprio", control_setdbprio
, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6389 { "getdbprio", control_getdbprio
, false, false, "Get DB priority", "<dbname|dbid>"},
6390 { "setdbreadonly", control_setdbreadonly
, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6391 { "setdbsticky", control_setdbsticky
, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6392 { "msglisten", control_msglisten
, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6393 { "msgsend", control_msgsend
, false, false, "Send a message to srvid", "<srvid> <message>"},
6394 { "pfetch", control_pfetch
, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6395 { "pstore", control_pstore
, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6396 { "pdelete", control_pdelete
, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6397 { "ptrans", control_ptrans
, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6398 { "tfetch", control_tfetch
, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6399 { "tstore", control_tstore
, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6400 { "readkey", control_readkey
, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6401 { "writekey", control_writekey
, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6402 { "checktcpport", control_chktcpport
, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6403 { "rebalancenode", control_rebalancenode
, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6404 { "getdbseqnum", control_getdbseqnum
, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6405 { "nodestatus", control_nodestatus
, true, false, "show and return node status", "[<pnn-list>]" },
6406 { "dbstatistics", control_dbstatistics
, false, false, "show db statistics", "<dbname|dbid>" },
6407 { "reloadips", control_reloadips
, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6408 { "ipiface", control_ipiface
, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6414 static void usage(void)
6418 "Usage: ctdb [options] <control>\n" \
6420 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6421 " -Y generate machine readable output\n"
6422 " -x <char> specify delimiter for machine readable output\n"
6423 " -v generate verbose output\n"
6424 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options
.timelimit
);
6425 printf("Controls:\n");
6426 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6427 printf(" %-15s %-27s %s\n",
6428 ctdb_commands
[i
].name
,
6429 ctdb_commands
[i
].args
?ctdb_commands
[i
].args
:"",
6430 ctdb_commands
[i
].msg
);
6436 static void ctdb_alarm(int sig
)
6438 printf("Maximum runtime exceeded - exiting\n");
6445 int main(int argc
, const char *argv
[])
6447 struct ctdb_context
*ctdb
;
6448 char *nodestring
= NULL
;
6449 int machineparsable
= 0;
6450 struct poptOption popt_options
[] = {
6453 { "timelimit", 't', POPT_ARG_INT
, &options
.timelimit
, 0, "timelimit", "integer" },
6454 { "node", 'n', POPT_ARG_STRING
, &nodestring
, 0, "node", "integer|all" },
6455 { "machinereadable", 'Y', POPT_ARG_NONE
, &options
.machinereadable
, 0, "enable machine readable output", NULL
},
6456 { NULL
, 'x', POPT_ARG_STRING
, &options
.machineseparator
, 0, "specify separator for machine readable output", "char" },
6457 { NULL
, 'X', POPT_ARG_NONE
, &machineparsable
, 0, "enable machine parsable output with separator |", NULL
},
6458 { "verbose", 'v', POPT_ARG_NONE
, &options
.verbose
, 0, "enable verbose output", NULL
},
6459 { "maxruntime", 'T', POPT_ARG_INT
, &options
.maxruntime
, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6460 { "print-emptyrecords", 0, POPT_ARG_NONE
, &options
.printemptyrecords
, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL
},
6461 { "print-datasize", 0, POPT_ARG_NONE
, &options
.printdatasize
, 0, "do not print record data when dumping databases, only the data size", NULL
},
6462 { "print-lmaster", 0, POPT_ARG_NONE
, &options
.printlmaster
, 0, "print the record's lmaster in catdb", NULL
},
6463 { "print-hash", 0, POPT_ARG_NONE
, &options
.printhash
, 0, "print the record's hash when dumping databases", NULL
},
6464 { "print-recordflags", 0, POPT_ARG_NONE
, &options
.printrecordflags
, 0, "print the record flags in catdb and dumpdbbackup", NULL
},
6468 const char **extra_argv
;
6472 struct tevent_context
*ev
;
6473 const char *control
;
6477 /* set some defaults */
6478 options
.maxruntime
= 0;
6479 options
.timelimit
= 10;
6480 options
.pnn
= CTDB_CURRENT_NODE
;
6482 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
6484 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6487 DEBUG(DEBUG_ERR
, ("Invalid option %s: %s\n",
6488 poptBadOption(pc
, 0), poptStrerror(opt
)));
6493 /* setup the remaining options for the main program to use */
6494 extra_argv
= poptGetArgs(pc
);
6497 while (extra_argv
[extra_argc
]) extra_argc
++;
6500 if (extra_argc
< 1) {
6504 if (options
.maxruntime
== 0) {
6505 const char *ctdb_timeout
;
6506 ctdb_timeout
= getenv("CTDB_TIMEOUT");
6507 if (ctdb_timeout
!= NULL
) {
6508 options
.maxruntime
= strtoul(ctdb_timeout
, NULL
, 0);
6510 /* default timeout is 120 seconds */
6511 options
.maxruntime
= 120;
6515 if (machineparsable
) {
6516 options
.machineseparator
= "|";
6518 if (options
.machineseparator
!= NULL
) {
6519 if (strlen(options
.machineseparator
) != 1) {
6520 printf("Invalid separator \"%s\" - "
6521 "must be single character\n",
6522 options
.machineseparator
);
6527 options
.machinereadable
= true;
6528 } else if (options
.machinereadable
) {
6529 options
.machineseparator
= ":";
6532 signal(SIGALRM
, ctdb_alarm
);
6533 alarm(options
.maxruntime
);
6535 control
= extra_argv
[0];
6537 /* Default value for CTDB_BASE - don't override */
6538 setenv("CTDB_BASE", CTDB_ETCDIR
, 0);
6540 ev
= tevent_context_init(NULL
);
6542 DEBUG(DEBUG_ERR
, ("Failed to initialize event system\n"));
6546 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6547 if (strcmp(control
, ctdb_commands
[i
].name
) == 0) {
6552 if (i
== ARRAY_SIZE(ctdb_commands
)) {
6553 DEBUG(DEBUG_ERR
, ("Unknown control '%s'\n", control
));
6557 if (ctdb_commands
[i
].without_daemon
== true) {
6558 if (nodestring
!= NULL
) {
6559 DEBUG(DEBUG_ERR
, ("Can't specify node(s) with \"ctdb %s\"\n", control
));
6562 return ctdb_commands
[i
].fn(NULL
, extra_argc
-1, extra_argv
+1);
6565 /* initialise ctdb */
6566 ctdb
= ctdb_cmdline_client(ev
, TIMELIMIT());
6570 DEBUG(DEBUG_ERR
, ("Failed to init ctdb\n"));
6572 pnn
= find_node_xpnn();
6575 ("Is this node part of a CTDB cluster?\n"));
6580 /* setup the node number(s) to contact */
6581 if (!parse_nodestring(ctdb
, ctdb
, nodestring
, CTDB_CURRENT_NODE
, false,
6582 &options
.nodes
, &options
.pnn
)) {
6586 if (options
.pnn
== CTDB_CURRENT_NODE
) {
6587 options
.pnn
= options
.nodes
[0];
6590 if (ctdb_commands
[i
].auto_all
&&
6591 ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
6592 (options
.pnn
== CTDB_MULTICAST
))) {
6596 for (j
= 0; j
< talloc_array_length(options
.nodes
); j
++) {
6597 options
.pnn
= options
.nodes
[j
];
6598 ret
|= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6601 ret
= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6606 (void)poptFreeContext(pc
);