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"
28 #include "../include/ctdb_version.h"
29 #include "../include/ctdb_client.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
32 #include "lib/tdb_wrap/tdb_wrap.h"
33 #include "lib/util/dlinklist.h"
35 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
36 #define ERR_NONODE 21 /* node does not exist */
37 #define ERR_DISNODE 22 /* node is disconnected */
39 static void usage(void);
48 int printemptyrecords
;
55 #define LONGTIMEOUT options.timelimit*10
57 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
58 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
60 static double timeval_delta(struct timeval
*tv2
, struct timeval
*tv
)
62 return (tv2
->tv_sec
- tv
->tv_sec
) +
63 (tv2
->tv_usec
- tv
->tv_usec
)*1.0e-6;
66 static int control_version(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
68 printf("CTDB version: %s\n", CTDB_VERSION_STRING
);
72 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
73 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
74 "Out of memory in " __location__ )); \
78 static uint32_t getpnn(struct ctdb_context
*ctdb
)
80 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
81 (options
.pnn
== CTDB_MULTICAST
)) {
83 ("Cannot get PNN for node %u\n", options
.pnn
));
87 if (options
.pnn
== CTDB_CURRENT_NODE
) {
88 return ctdb_get_pnn(ctdb
);
94 static void assert_single_node_only(void)
96 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
97 (options
.pnn
== CTDB_MULTICAST
)) {
99 ("This control can not be applied to multiple PNNs\n"));
104 /* Pretty print the flags to a static buffer in human-readable format.
105 * This never returns NULL!
107 static const char *pretty_print_flags(uint32_t flags
)
110 static const struct {
114 { NODE_FLAGS_DISCONNECTED
, "DISCONNECTED" },
115 { NODE_FLAGS_PERMANENTLY_DISABLED
, "DISABLED" },
116 { NODE_FLAGS_BANNED
, "BANNED" },
117 { NODE_FLAGS_UNHEALTHY
, "UNHEALTHY" },
118 { NODE_FLAGS_DELETED
, "DELETED" },
119 { NODE_FLAGS_STOPPED
, "STOPPED" },
120 { NODE_FLAGS_INACTIVE
, "INACTIVE" },
122 static char flags_str
[512]; /* Big enough to contain all flag names */
125 for (j
=0;j
<ARRAY_SIZE(flag_names
);j
++) {
126 if (flags
& flag_names
[j
].flag
) {
127 if (flags_str
[0] == '\0') {
128 (void) strcpy(flags_str
, flag_names
[j
].name
);
130 (void) strncat(flags_str
, "|", sizeof(flags_str
)-1);
131 (void) strncat(flags_str
, flag_names
[j
].name
,
132 sizeof(flags_str
)-1);
136 if (flags_str
[0] == '\0') {
137 (void) strcpy(flags_str
, "OK");
143 static int h2i(char h
)
145 if (h
>= 'a' && h
<= 'f') return h
- 'a' + 10;
146 if (h
>= 'A' && h
<= 'F') return h
- 'f' + 10;
150 static TDB_DATA
hextodata(TALLOC_CTX
*mem_ctx
, const char *str
)
153 TDB_DATA key
= {NULL
, 0};
157 DEBUG(DEBUG_ERR
,("Key specified with odd number of hexadecimal digits\n"));
162 key
.dptr
= talloc_size(mem_ctx
, key
.dsize
);
164 for (i
=0; i
< len
/2; i
++) {
165 key
.dptr
[i
] = h2i(str
[i
*2]) << 4 | h2i(str
[i
*2+1]);
170 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
171 * that are disconnected or deleted. If dd_ok is true those nodes are
172 * included in the output list of nodes. If dd_ok is false, those
173 * nodes are filtered from the "all" case and cause an error if
174 * explicitly specified.
176 static bool parse_nodestring(struct ctdb_context
*ctdb
,
178 const char * nodestring
,
179 uint32_t current_pnn
,
184 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
187 struct ctdb_node_map
*nodemap
;
192 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
194 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
195 talloc_free(tmp_ctx
);
199 if (nodestring
!= NULL
) {
200 *nodes
= talloc_array(mem_ctx
, uint32_t, 0);
201 if (*nodes
== NULL
) {
207 if (strcmp(nodestring
, "all") == 0) {
208 *pnn_mode
= CTDB_BROADCAST_ALL
;
211 for (i
= 0; i
< nodemap
->num
; i
++) {
212 if ((nodemap
->nodes
[i
].flags
&
213 (NODE_FLAGS_DISCONNECTED
|
214 NODE_FLAGS_DELETED
)) && !dd_ok
) {
217 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
219 if (*nodes
== NULL
) {
229 ns
= talloc_strdup(tmp_ctx
, nodestring
);
230 tok
= strtok(ns
, ",");
231 while (tok
!= NULL
) {
234 i
= (uint32_t)strtoul(tok
, &endptr
, 0);
235 if (i
== 0 && tok
== endptr
) {
237 ("Invalid node %s\n", tok
));
238 talloc_free(tmp_ctx
);
241 if (i
>= nodemap
->num
) {
242 DEBUG(DEBUG_ERR
, ("Node %u does not exist\n", i
));
243 talloc_free(tmp_ctx
);
246 if ((nodemap
->nodes
[i
].flags
&
247 (NODE_FLAGS_DISCONNECTED
|
248 NODE_FLAGS_DELETED
)) && !dd_ok
) {
249 DEBUG(DEBUG_ERR
, ("Node %u has status %s\n", i
, pretty_print_flags(nodemap
->nodes
[i
].flags
)));
250 talloc_free(tmp_ctx
);
253 if ((pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), i
)) < 0) {
254 DEBUG(DEBUG_ERR
, ("Can not access node %u. Node is not operational.\n", i
));
255 talloc_free(tmp_ctx
);
259 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
261 if (*nodes
== NULL
) {
268 tok
= strtok(NULL
, ",");
273 *pnn_mode
= (*nodes
)[0];
275 *pnn_mode
= CTDB_MULTICAST
;
279 /* default - no nodes specified */
280 *nodes
= talloc_array(mem_ctx
, uint32_t, 1);
281 if (*nodes
== NULL
) {
284 *pnn_mode
= CTDB_CURRENT_NODE
;
286 if (((*nodes
)[0] = ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), current_pnn
)) < 0) {
291 talloc_free(tmp_ctx
);
295 talloc_free(tmp_ctx
);
300 check if a database exists
302 static bool db_exists(struct ctdb_context
*ctdb
, const char *dbarg
,
303 uint32_t *dbid
, const char **dbname
, uint8_t *flags
)
306 struct ctdb_dbid_map
*dbmap
=NULL
;
307 bool dbid_given
= false, found
= false;
309 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
312 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &dbmap
);
314 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
318 if (strncmp(dbarg
, "0x", 2) == 0) {
319 id
= strtoul(dbarg
, NULL
, 0);
323 for(i
=0; i
<dbmap
->num
; i
++) {
325 if (id
== dbmap
->dbs
[i
].dbid
) {
330 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, tmp_ctx
, &name
);
332 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].dbid
));
336 if (strcmp(name
, dbarg
) == 0) {
337 id
= dbmap
->dbs
[i
].dbid
;
344 if (found
&& dbid_given
&& dbname
!= NULL
) {
345 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, tmp_ctx
, &name
);
347 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].dbid
));
354 if (dbid
) *dbid
= id
;
355 if (dbname
) *dbname
= talloc_strdup(ctdb
, name
);
356 if (flags
) *flags
= dbmap
->dbs
[i
].flags
;
358 DEBUG(DEBUG_ERR
,("No database matching '%s' found\n", dbarg
));
362 talloc_free(tmp_ctx
);
367 see if a process exists
369 static int control_process_exists(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
377 if (sscanf(argv
[0], "%u:%u", &pnn
, &pid
) != 2) {
378 DEBUG(DEBUG_ERR
, ("Badly formed pnn:pid\n"));
382 ret
= ctdb_ctrl_process_exists(ctdb
, pnn
, pid
);
384 printf("%u:%u exists\n", pnn
, pid
);
386 printf("%u:%u does not exist\n", pnn
, pid
);
392 display statistics structure
394 static void show_statistics(struct ctdb_statistics
*s
, int show_header
)
396 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
398 const char *prefix
=NULL
;
400 int tmp
, days
, hours
, minutes
, seconds
;
405 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
406 STATISTICS_FIELD(num_clients
),
407 STATISTICS_FIELD(frozen
),
408 STATISTICS_FIELD(recovering
),
409 STATISTICS_FIELD(num_recoveries
),
410 STATISTICS_FIELD(client_packets_sent
),
411 STATISTICS_FIELD(client_packets_recv
),
412 STATISTICS_FIELD(node_packets_sent
),
413 STATISTICS_FIELD(node_packets_recv
),
414 STATISTICS_FIELD(keepalive_packets_sent
),
415 STATISTICS_FIELD(keepalive_packets_recv
),
416 STATISTICS_FIELD(node
.req_call
),
417 STATISTICS_FIELD(node
.reply_call
),
418 STATISTICS_FIELD(node
.req_dmaster
),
419 STATISTICS_FIELD(node
.reply_dmaster
),
420 STATISTICS_FIELD(node
.reply_error
),
421 STATISTICS_FIELD(node
.req_message
),
422 STATISTICS_FIELD(node
.req_control
),
423 STATISTICS_FIELD(node
.reply_control
),
424 STATISTICS_FIELD(client
.req_call
),
425 STATISTICS_FIELD(client
.req_message
),
426 STATISTICS_FIELD(client
.req_control
),
427 STATISTICS_FIELD(timeouts
.call
),
428 STATISTICS_FIELD(timeouts
.control
),
429 STATISTICS_FIELD(timeouts
.traverse
),
430 STATISTICS_FIELD(locks
.num_calls
),
431 STATISTICS_FIELD(locks
.num_current
),
432 STATISTICS_FIELD(locks
.num_pending
),
433 STATISTICS_FIELD(locks
.num_failed
),
434 STATISTICS_FIELD(total_calls
),
435 STATISTICS_FIELD(pending_calls
),
436 STATISTICS_FIELD(childwrite_calls
),
437 STATISTICS_FIELD(pending_childwrite_calls
),
438 STATISTICS_FIELD(memory_used
),
439 STATISTICS_FIELD(max_hop_count
),
440 STATISTICS_FIELD(total_ro_delegations
),
441 STATISTICS_FIELD(total_ro_revokes
),
444 tmp
= s
->statistics_current_time
.tv_sec
- s
->statistics_start_time
.tv_sec
;
453 if (options
.machinereadable
){
455 printf("CTDB version:");
456 printf("Current time of statistics:");
457 printf("Statistics collected since:");
458 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
459 printf("%s:", fields
[i
].name
);
461 printf("num_reclock_ctdbd_latency:");
462 printf("min_reclock_ctdbd_latency:");
463 printf("avg_reclock_ctdbd_latency:");
464 printf("max_reclock_ctdbd_latency:");
466 printf("num_reclock_recd_latency:");
467 printf("min_reclock_recd_latency:");
468 printf("avg_reclock_recd_latency:");
469 printf("max_reclock_recd_latency:");
471 printf("num_call_latency:");
472 printf("min_call_latency:");
473 printf("avg_call_latency:");
474 printf("max_call_latency:");
476 printf("num_lockwait_latency:");
477 printf("min_lockwait_latency:");
478 printf("avg_lockwait_latency:");
479 printf("max_lockwait_latency:");
481 printf("num_childwrite_latency:");
482 printf("min_childwrite_latency:");
483 printf("avg_childwrite_latency:");
484 printf("max_childwrite_latency:");
487 printf("%d:", CTDB_PROTOCOL
);
488 printf("%d:", (int)s
->statistics_current_time
.tv_sec
);
489 printf("%d:", (int)s
->statistics_start_time
.tv_sec
);
490 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
491 printf("%d:", *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
493 printf("%d:", s
->reclock
.ctdbd
.num
);
494 printf("%.6f:", s
->reclock
.ctdbd
.min
);
495 printf("%.6f:", s
->reclock
.ctdbd
.num
?s
->reclock
.ctdbd
.total
/s
->reclock
.ctdbd
.num
:0.0);
496 printf("%.6f:", s
->reclock
.ctdbd
.max
);
498 printf("%d:", s
->reclock
.recd
.num
);
499 printf("%.6f:", s
->reclock
.recd
.min
);
500 printf("%.6f:", s
->reclock
.recd
.num
?s
->reclock
.recd
.total
/s
->reclock
.recd
.num
:0.0);
501 printf("%.6f:", s
->reclock
.recd
.max
);
503 printf("%d:", s
->call_latency
.num
);
504 printf("%.6f:", s
->call_latency
.min
);
505 printf("%.6f:", s
->call_latency
.num
?s
->call_latency
.total
/s
->call_latency
.num
:0.0);
506 printf("%.6f:", s
->call_latency
.max
);
508 printf("%d:", s
->childwrite_latency
.num
);
509 printf("%.6f:", s
->childwrite_latency
.min
);
510 printf("%.6f:", s
->childwrite_latency
.num
?s
->childwrite_latency
.total
/s
->childwrite_latency
.num
:0.0);
511 printf("%.6f:", s
->childwrite_latency
.max
);
514 printf("CTDB version %u\n", CTDB_PROTOCOL
);
515 printf("Current time of statistics : %s", ctime(&s
->statistics_current_time
.tv_sec
));
516 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&s
->statistics_start_time
.tv_sec
));
518 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
519 if (strchr(fields
[i
].name
, '.')) {
520 preflen
= strcspn(fields
[i
].name
, ".")+1;
521 if (!prefix
|| strncmp(prefix
, fields
[i
].name
, preflen
) != 0) {
522 prefix
= fields
[i
].name
;
523 printf(" %*.*s\n", preflen
-1, preflen
-1, fields
[i
].name
);
528 printf(" %*s%-22s%*s%10u\n",
530 fields
[i
].name
+preflen
,
532 *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
534 printf(" hop_count_buckets:");
535 for (i
=0;i
<MAX_COUNT_BUCKETS
;i
++) {
536 printf(" %d", s
->hop_count_bucket
[i
]);
539 printf(" lock_buckets:");
540 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
541 printf(" %d", s
->locks
.buckets
[i
]);
544 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
);
546 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
);
548 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
);
550 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
);
551 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
);
554 talloc_free(tmp_ctx
);
558 display remote ctdb statistics combined from all nodes
560 static int control_statistics_all(struct ctdb_context
*ctdb
)
563 struct ctdb_statistics statistics
;
567 nodes
= ctdb_get_connected_nodes(ctdb
, TIMELIMIT(), ctdb
, &num_nodes
);
568 CTDB_NO_MEMORY(ctdb
, nodes
);
570 ZERO_STRUCT(statistics
);
572 for (i
=0;i
<num_nodes
;i
++) {
573 struct ctdb_statistics s1
;
575 uint32_t *v1
= (uint32_t *)&s1
;
576 uint32_t *v2
= (uint32_t *)&statistics
;
578 offsetof(struct ctdb_statistics
, __last_counter
) / sizeof(uint32_t);
579 ret
= ctdb_ctrl_statistics(ctdb
, nodes
[i
], &s1
);
581 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", nodes
[i
]));
584 for (j
=0;j
<num_ints
;j
++) {
587 statistics
.max_hop_count
=
588 MAX(statistics
.max_hop_count
, s1
.max_hop_count
);
589 statistics
.call_latency
.max
=
590 MAX(statistics
.call_latency
.max
, s1
.call_latency
.max
);
593 printf("Gathered statistics for %u nodes\n", num_nodes
);
594 show_statistics(&statistics
, 1);
599 display remote ctdb statistics
601 static int control_statistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
604 struct ctdb_statistics statistics
;
606 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
607 return control_statistics_all(ctdb
);
610 ret
= ctdb_ctrl_statistics(ctdb
, options
.pnn
, &statistics
);
612 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", options
.pnn
));
615 show_statistics(&statistics
, 1);
621 reset remote ctdb statistics
623 static int control_statistics_reset(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
627 ret
= ctdb_statistics_reset(ctdb
, options
.pnn
);
629 DEBUG(DEBUG_ERR
, ("Unable to reset statistics on node %u\n", options
.pnn
));
637 display remote ctdb rolling statistics
639 static int control_stats(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
642 struct ctdb_statistics_wire
*stats
;
643 int i
, num_records
= -1;
645 assert_single_node_only();
648 num_records
= atoi(argv
[0]) - 1;
651 ret
= ctdb_ctrl_getstathistory(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &stats
);
653 DEBUG(DEBUG_ERR
, ("Unable to get rolling statistics from node %u\n", options
.pnn
));
656 for (i
=0;i
<stats
->num
;i
++) {
657 if (stats
->stats
[i
].statistics_start_time
.tv_sec
== 0) {
660 show_statistics(&stats
->stats
[i
], i
==0);
661 if (i
== num_records
) {
670 display remote ctdb db statistics
672 static int control_dbstatistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
674 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
675 struct ctdb_db_statistics
*dbstat
;
685 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
689 ret
= ctdb_ctrl_dbstatistics(ctdb
, options
.pnn
, db_id
, tmp_ctx
, &dbstat
);
691 DEBUG(DEBUG_ERR
,("Failed to read db statistics from node\n"));
692 talloc_free(tmp_ctx
);
696 printf("DB Statistics: %s\n", argv
[0]);
697 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
698 dbstat
->db_ro_delegations
);
699 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
700 dbstat
->db_ro_delegations
);
701 printf(" %s\n", "locks");
702 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
703 dbstat
->locks
.num_calls
);
704 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
705 dbstat
->locks
.num_failed
);
706 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
707 dbstat
->locks
.num_current
);
708 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
709 dbstat
->locks
.num_pending
);
710 printf(" %s", "hop_count_buckets:");
711 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
712 printf(" %d", dbstat
->hop_count_bucket
[i
]);
715 printf(" %s", "lock_buckets:");
716 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
717 printf(" %d", dbstat
->locks
.buckets
[i
]);
720 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
721 "locks_latency MIN/AVG/MAX",
722 dbstat
->locks
.latency
.min
,
723 (dbstat
->locks
.latency
.num
?
724 dbstat
->locks
.latency
.total
/dbstat
->locks
.latency
.num
:
726 dbstat
->locks
.latency
.max
,
727 dbstat
->locks
.latency
.num
);
728 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
729 "vacuum_latency MIN/AVG/MAX",
730 dbstat
->vacuum
.latency
.min
,
731 (dbstat
->vacuum
.latency
.num
?
732 dbstat
->vacuum
.latency
.total
/dbstat
->vacuum
.latency
.num
:
734 dbstat
->vacuum
.latency
.max
,
735 dbstat
->vacuum
.latency
.num
);
737 for (i
=0; i
<dbstat
->num_hot_keys
; i
++) {
738 if (dbstat
->hot_keys
[i
].count
> 0) {
742 dbstat
->num_hot_keys
= num_hot_keys
;
744 printf(" Num Hot Keys: %d\n", dbstat
->num_hot_keys
);
745 for (i
= 0; i
< dbstat
->num_hot_keys
; i
++) {
747 printf(" Count:%d Key:", dbstat
->hot_keys
[i
].count
);
748 for (j
= 0; j
< dbstat
->hot_keys
[i
].key
.dsize
; j
++) {
749 printf("%02x", dbstat
->hot_keys
[i
].key
.dptr
[j
]&0xff);
754 talloc_free(tmp_ctx
);
759 display uptime of remote node
761 static int control_uptime(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
764 struct ctdb_uptime
*uptime
= NULL
;
765 int tmp
, days
, hours
, minutes
, seconds
;
767 ret
= ctdb_ctrl_uptime(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &uptime
);
769 DEBUG(DEBUG_ERR
, ("Unable to get uptime from node %u\n", options
.pnn
));
773 if (options
.machinereadable
){
774 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
775 printf(":%u:%u:%u:%lf\n",
776 (unsigned int)uptime
->current_time
.tv_sec
,
777 (unsigned int)uptime
->ctdbd_start_time
.tv_sec
,
778 (unsigned int)uptime
->last_recovery_finished
.tv_sec
,
779 timeval_delta(&uptime
->last_recovery_finished
,
780 &uptime
->last_recovery_started
)
785 printf("Current time of node : %s", ctime(&uptime
->current_time
.tv_sec
));
787 tmp
= uptime
->current_time
.tv_sec
- uptime
->ctdbd_start_time
.tv_sec
;
795 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->ctdbd_start_time
.tv_sec
));
797 tmp
= uptime
->current_time
.tv_sec
- uptime
->last_recovery_finished
.tv_sec
;
805 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->last_recovery_finished
.tv_sec
));
807 printf("Duration of last recovery/failover: %lf seconds\n",
808 timeval_delta(&uptime
->last_recovery_finished
,
809 &uptime
->last_recovery_started
));
815 show the PNN of the current node
817 static int control_pnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
821 mypnn
= getpnn(ctdb
);
823 printf("PNN:%d\n", mypnn
);
829 struct pnn_node
*next
, *prev
;
834 static struct pnn_node
*read_pnn_node_file(TALLOC_CTX
*mem_ctx
,
840 struct pnn_node
*pnn_nodes
= NULL
;
841 struct pnn_node
*pnn_node
;
843 lines
= file_lines_load(file
, &nlines
, 0, mem_ctx
);
847 for (i
=0, pnn
=0; i
<nlines
; i
++) {
851 /* strip leading spaces */
852 while((*node
== ' ') || (*node
== '\t')) {
859 if (strcmp(node
, "") == 0) {
862 pnn_node
= talloc(mem_ctx
, struct pnn_node
);
863 pnn_node
->pnn
= pnn
++;
865 if (!parse_ip(node
, NULL
, 0, &pnn_node
->addr
)) {
867 ("Invalid IP address '%s' in file %s\n",
869 /* Caller will free mem_ctx */
873 DLIST_ADD_END(pnn_nodes
, pnn_node
, NULL
);
879 static struct pnn_node
*read_nodes_file(TALLOC_CTX
*mem_ctx
)
881 const char *nodes_list
;
883 /* read the nodes file */
884 nodes_list
= getenv("CTDB_NODES");
885 if (nodes_list
== NULL
) {
886 nodes_list
= talloc_asprintf(mem_ctx
, "%s/nodes",
887 getenv("CTDB_BASE"));
888 if (nodes_list
== NULL
) {
889 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
894 return read_pnn_node_file(mem_ctx
, nodes_list
);
898 show the PNN of the current node
899 discover the pnn by loading the nodes file and try to bind to all
900 addresses one at a time until the ip address is found.
902 static int find_node_xpnn(void)
904 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
905 struct pnn_node
*pnn_nodes
;
906 struct pnn_node
*pnn_node
;
909 pnn_nodes
= read_nodes_file(mem_ctx
);
910 if (pnn_nodes
== NULL
) {
911 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
912 talloc_free(mem_ctx
);
916 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
917 if (ctdb_sys_have_ip(&pnn_node
->addr
)) {
919 talloc_free(mem_ctx
);
924 printf("Failed to detect which PNN this node is\n");
925 talloc_free(mem_ctx
);
929 static int control_xpnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
933 assert_single_node_only();
935 pnn
= find_node_xpnn();
940 printf("PNN:%d\n", pnn
);
944 /* Helpers for ctdb status
946 static bool is_partially_online(struct ctdb_context
*ctdb
, struct ctdb_node_and_flags
*node
)
948 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
952 if (node
->flags
== 0) {
953 struct ctdb_control_get_ifaces
*ifaces
;
955 if (ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), node
->pnn
,
956 tmp_ctx
, &ifaces
) == 0) {
957 for (j
=0; j
< ifaces
->num
; j
++) {
958 if (ifaces
->ifaces
[j
].link_state
!= 0) {
966 talloc_free(tmp_ctx
);
971 static void control_status_header_machine(void)
973 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
974 ":Inactive:PartiallyOnline:ThisNode:\n");
977 static int control_status_1_machine(struct ctdb_context
*ctdb
, int mypnn
,
978 struct ctdb_node_and_flags
*node
)
980 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node
->pnn
,
981 ctdb_addr_to_str(&node
->addr
),
982 !!(node
->flags
&NODE_FLAGS_DISCONNECTED
),
983 !!(node
->flags
&NODE_FLAGS_BANNED
),
984 !!(node
->flags
&NODE_FLAGS_PERMANENTLY_DISABLED
),
985 !!(node
->flags
&NODE_FLAGS_UNHEALTHY
),
986 !!(node
->flags
&NODE_FLAGS_STOPPED
),
987 !!(node
->flags
&NODE_FLAGS_INACTIVE
),
988 is_partially_online(ctdb
, node
) ? 1 : 0,
989 (node
->pnn
== mypnn
)?'Y':'N');
994 static int control_status_1_human(struct ctdb_context
*ctdb
, int mypnn
,
995 struct ctdb_node_and_flags
*node
)
997 printf("pnn:%d %-16s %s%s\n", node
->pnn
,
998 ctdb_addr_to_str(&node
->addr
),
999 is_partially_online(ctdb
, node
) ? "PARTIALLYONLINE" : pretty_print_flags(node
->flags
),
1000 node
->pnn
== mypnn
?" (THIS NODE)":"");
1006 display remote ctdb status
1008 static int control_status(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1010 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1012 struct ctdb_vnn_map
*vnnmap
=NULL
;
1013 struct ctdb_node_map
*nodemap
=NULL
;
1014 uint32_t recmode
, recmaster
, mypnn
;
1015 int num_deleted_nodes
= 0;
1018 mypnn
= getpnn(ctdb
);
1020 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1022 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1023 talloc_free(tmp_ctx
);
1027 if (options
.machinereadable
) {
1028 control_status_header_machine();
1029 for (i
=0;i
<nodemap
->num
;i
++) {
1030 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1033 (void) control_status_1_machine(ctdb
, mypnn
,
1034 &nodemap
->nodes
[i
]);
1036 talloc_free(tmp_ctx
);
1040 for (i
=0; i
<nodemap
->num
; i
++) {
1041 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1042 num_deleted_nodes
++;
1045 if (num_deleted_nodes
== 0) {
1046 printf("Number of nodes:%d\n", nodemap
->num
);
1048 printf("Number of nodes:%d (including %d deleted nodes)\n",
1049 nodemap
->num
, num_deleted_nodes
);
1051 for(i
=0;i
<nodemap
->num
;i
++){
1052 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1055 (void) control_status_1_human(ctdb
, mypnn
, &nodemap
->nodes
[i
]);
1058 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
1060 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
1061 talloc_free(tmp_ctx
);
1064 if (vnnmap
->generation
== INVALID_GENERATION
) {
1065 printf("Generation:INVALID\n");
1067 printf("Generation:%d\n",vnnmap
->generation
);
1069 printf("Size:%d\n",vnnmap
->size
);
1070 for(i
=0;i
<vnnmap
->size
;i
++){
1071 printf("hash:%d lmaster:%d\n", i
, vnnmap
->map
[i
]);
1074 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmode
);
1076 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
1077 talloc_free(tmp_ctx
);
1080 printf("Recovery mode:%s (%d)\n",recmode
==CTDB_RECOVERY_NORMAL
?"NORMAL":"RECOVERY",recmode
);
1082 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmaster
);
1084 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1085 talloc_free(tmp_ctx
);
1088 printf("Recovery master:%d\n",recmaster
);
1090 talloc_free(tmp_ctx
);
1094 static int control_nodestatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1096 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1098 struct ctdb_node_map
*nodemap
=NULL
;
1100 uint32_t pnn_mode
, mypnn
;
1106 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1107 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1111 if (options
.machinereadable
) {
1112 control_status_header_machine();
1113 } else if (pnn_mode
== CTDB_BROADCAST_ALL
) {
1114 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes
));
1117 mypnn
= getpnn(ctdb
);
1119 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1121 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1122 talloc_free(tmp_ctx
);
1128 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1129 if (options
.machinereadable
) {
1130 ret
|= control_status_1_machine(ctdb
, mypnn
,
1131 &nodemap
->nodes
[nodes
[i
]]);
1133 ret
|= control_status_1_human(ctdb
, mypnn
,
1134 &nodemap
->nodes
[nodes
[i
]]);
1138 talloc_free(tmp_ctx
);
1142 static struct pnn_node
*read_natgw_nodes_file(struct ctdb_context
*ctdb
,
1143 TALLOC_CTX
*mem_ctx
)
1145 const char *natgw_list
;
1146 struct pnn_node
*natgw_nodes
= NULL
;
1148 natgw_list
= getenv("CTDB_NATGW_NODES");
1149 if (natgw_list
== NULL
) {
1150 natgw_list
= talloc_asprintf(mem_ctx
, "%s/natgw_nodes",
1151 getenv("CTDB_BASE"));
1152 if (natgw_list
== NULL
) {
1153 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
1157 /* The PNNs will be junk but they're not used */
1158 natgw_nodes
= read_pnn_node_file(mem_ctx
, natgw_list
);
1159 if (natgw_nodes
== NULL
) {
1161 ("Failed to load natgw node list '%s'\n", natgw_list
));
1167 /* talloc off the existing nodemap... */
1168 static struct ctdb_node_map
*talloc_nodemap(struct ctdb_node_map
*nodemap
)
1170 return talloc_zero_size(nodemap
,
1171 offsetof(struct ctdb_node_map
, nodes
) +
1172 nodemap
->num
* sizeof(struct ctdb_node_and_flags
));
1175 static struct ctdb_node_map
*
1176 filter_nodemap_by_addrs(struct ctdb_context
*ctdb
,
1177 struct ctdb_node_map
*nodemap
,
1178 struct pnn_node
*nodes
)
1182 struct ctdb_node_map
*ret
;
1184 ret
= talloc_nodemap(nodemap
);
1185 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1189 for (i
= 0; i
< nodemap
->num
; i
++) {
1190 for(n
= nodes
; n
!= NULL
; n
= n
->next
) {
1191 if (ctdb_same_ip(&n
->addr
,
1192 &nodemap
->nodes
[i
].addr
)) {
1200 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1207 static struct ctdb_node_map
*
1208 filter_nodemap_by_capabilities(struct ctdb_context
*ctdb
,
1209 struct ctdb_node_map
*nodemap
,
1210 uint32_t required_capabilities
,
1214 uint32_t capabilities
;
1215 struct ctdb_node_map
*ret
;
1217 ret
= talloc_nodemap(nodemap
);
1218 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1222 for (i
= 0; i
< nodemap
->num
; i
++) {
1225 /* Disconnected nodes have no capabilities! */
1226 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
1230 res
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(),
1231 nodemap
->nodes
[i
].pnn
,
1234 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n",
1235 nodemap
->nodes
[i
].pnn
));
1239 if (!(capabilities
& required_capabilities
)) {
1243 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1253 static struct ctdb_node_map
*
1254 filter_nodemap_by_flags(struct ctdb_context
*ctdb
,
1255 struct ctdb_node_map
*nodemap
,
1256 uint32_t flags_mask
)
1259 struct ctdb_node_map
*ret
;
1261 ret
= talloc_nodemap(nodemap
);
1262 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1266 for (i
= 0; i
< nodemap
->num
; i
++) {
1267 if (nodemap
->nodes
[i
].flags
& flags_mask
) {
1271 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1279 display the list of nodes belonging to this natgw configuration
1281 static int control_natgwlist(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1283 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1285 struct pnn_node
*natgw_nodes
= NULL
;
1286 struct ctdb_node_map
*orig_nodemap
=NULL
;
1287 struct ctdb_node_map
*nodemap
;
1288 uint32_t mypnn
, pnn
;
1291 /* When we have some nodes that could be the NATGW, make a
1292 * series of attempts to find the first node that doesn't have
1293 * certain status flags set.
1295 uint32_t exclude_flags
[] = {
1296 /* Look for a nice healthy node */
1297 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
|NODE_FLAGS_BANNED
|NODE_FLAGS_UNHEALTHY
,
1298 /* If not found, an UNHEALTHY/BANNED node will do */
1299 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
,
1300 /* If not found, a STOPPED node will do */
1301 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_DELETED
,
1305 /* read the natgw nodes file into a linked list */
1306 natgw_nodes
= read_natgw_nodes_file(ctdb
, tmp_ctx
);
1307 if (natgw_nodes
== NULL
) {
1312 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
1313 tmp_ctx
, &orig_nodemap
);
1315 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node.\n"));
1316 talloc_free(tmp_ctx
);
1320 /* Get a nodemap that includes only the nodes in the NATGW
1322 nodemap
= filter_nodemap_by_addrs(ctdb
, orig_nodemap
, natgw_nodes
);
1323 if (nodemap
== NULL
) {
1328 ret
= 2; /* matches ENOENT */
1331 /* For each flag mask... */
1332 for (i
= 0; exclude_flags
[i
] != 0; i
++) {
1333 /* ... get a nodemap that excludes nodes with with
1334 * masked flags... */
1335 struct ctdb_node_map
*t
=
1336 filter_nodemap_by_flags(ctdb
, nodemap
,
1344 /* ... and find the first node with the NATGW
1346 struct ctdb_node_map
*n
;
1347 n
= filter_nodemap_by_capabilities(ctdb
, t
,
1357 pnn
= n
->nodes
[0].pnn
;
1358 ip
= ctdb_addr_to_str(&n
->nodes
[0].addr
);
1365 if (options
.machinereadable
) {
1366 printf(":Node:IP:\n");
1367 printf(":%d:%s:\n", pnn
, ip
);
1369 printf("%d %s\n", pnn
, ip
);
1372 /* print the pruned list of nodes belonging to this natgw list */
1373 mypnn
= getpnn(ctdb
);
1374 if (options
.machinereadable
) {
1375 control_status_header_machine();
1377 printf("Number of nodes:%d\n", nodemap
->num
);
1379 for(i
=0;i
<nodemap
->num
;i
++){
1380 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1383 if (options
.machinereadable
) {
1384 control_status_1_machine(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1386 control_status_1_human(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1391 talloc_free(tmp_ctx
);
1396 display the status of the scripts for monitoring (or other events)
1398 static int control_one_scriptstatus(struct ctdb_context
*ctdb
,
1399 enum ctdb_eventscript_call type
)
1401 struct ctdb_scripts_wire
*script_status
;
1404 ret
= ctdb_ctrl_getscriptstatus(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, type
, &script_status
);
1406 DEBUG(DEBUG_ERR
, ("Unable to get script status from node %u\n", options
.pnn
));
1410 if (script_status
== NULL
) {
1411 if (!options
.machinereadable
) {
1412 printf("%s cycle never run\n",
1413 ctdb_eventscript_call_names
[type
]);
1418 if (!options
.machinereadable
) {
1420 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1421 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1425 printf("%d scripts were executed last %s cycle\n",
1427 ctdb_eventscript_call_names
[type
]);
1429 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1430 const char *status
= NULL
;
1432 switch (script_status
->scripts
[i
].status
) {
1434 status
= "TIMEDOUT";
1437 status
= "DISABLED";
1443 if (script_status
->scripts
[i
].status
> 0)
1447 if (options
.machinereadable
) {
1448 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1449 ctdb_eventscript_call_names
[type
],
1450 script_status
->scripts
[i
].name
,
1451 script_status
->scripts
[i
].status
,
1453 (long)script_status
->scripts
[i
].start
.tv_sec
,
1454 (long)script_status
->scripts
[i
].start
.tv_usec
,
1455 (long)script_status
->scripts
[i
].finished
.tv_sec
,
1456 (long)script_status
->scripts
[i
].finished
.tv_usec
,
1457 script_status
->scripts
[i
].output
);
1461 printf("%-20s Status:%s ",
1462 script_status
->scripts
[i
].name
, status
);
1464 /* Some other error, eg from stat. */
1465 printf("%-20s Status:CANNOT RUN (%s)",
1466 script_status
->scripts
[i
].name
,
1467 strerror(-script_status
->scripts
[i
].status
));
1469 if (script_status
->scripts
[i
].status
>= 0) {
1470 printf("Duration:%.3lf ",
1471 timeval_delta(&script_status
->scripts
[i
].finished
,
1472 &script_status
->scripts
[i
].start
));
1474 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1476 ctime(&script_status
->scripts
[i
].start
.tv_sec
));
1477 if (script_status
->scripts
[i
].status
!= 0) {
1478 printf(" OUTPUT:%s\n",
1479 script_status
->scripts
[i
].output
);
1489 static int control_scriptstatus(struct ctdb_context
*ctdb
,
1490 int argc
, const char **argv
)
1493 enum ctdb_eventscript_call type
, min
, max
;
1497 DEBUG(DEBUG_ERR
, ("Unknown arguments to scriptstatus\n"));
1502 arg
= ctdb_eventscript_call_names
[CTDB_EVENT_MONITOR
];
1506 for (type
= 0; type
< CTDB_EVENT_MAX
; type
++) {
1507 if (strcmp(arg
, ctdb_eventscript_call_names
[type
]) == 0) {
1513 if (type
== CTDB_EVENT_MAX
) {
1514 if (strcmp(arg
, "all") == 0) {
1516 max
= CTDB_EVENT_MAX
;
1518 DEBUG(DEBUG_ERR
, ("Unknown event type %s\n", argv
[0]));
1523 if (options
.machinereadable
) {
1524 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1527 for (type
= min
; type
< max
; type
++) {
1528 ret
= control_one_scriptstatus(ctdb
, type
);
1538 enable an eventscript
1540 static int control_enablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1548 ret
= ctdb_ctrl_enablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1550 DEBUG(DEBUG_ERR
, ("Unable to enable script %s on node %u\n", argv
[0], options
.pnn
));
1558 disable an eventscript
1560 static int control_disablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1568 ret
= ctdb_ctrl_disablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1570 DEBUG(DEBUG_ERR
, ("Unable to disable script %s on node %u\n", argv
[0], options
.pnn
));
1578 display the pnn of the recovery master
1580 static int control_recmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1585 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
1587 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1590 printf("%d\n",recmaster
);
1596 add a tickle to a public address
1598 static int control_add_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1600 struct ctdb_tcp_connection t
;
1604 assert_single_node_only();
1610 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1611 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1614 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1615 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1619 data
.dptr
= (uint8_t *)&t
;
1620 data
.dsize
= sizeof(t
);
1622 /* tell all nodes about this tcp connection */
1623 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE
,
1624 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1626 DEBUG(DEBUG_ERR
,("Failed to add tickle\n"));
1635 delete a tickle from a node
1637 static int control_del_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1639 struct ctdb_tcp_connection t
;
1643 assert_single_node_only();
1649 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1650 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1653 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1654 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1658 data
.dptr
= (uint8_t *)&t
;
1659 data
.dsize
= sizeof(t
);
1661 /* tell all nodes about this tcp connection */
1662 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_REMOVE
,
1663 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1665 DEBUG(DEBUG_ERR
,("Failed to remove tickle\n"));
1674 get a list of all tickles for this pnn
1676 static int control_get_tickles(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1678 struct ctdb_control_tcp_tickle_list
*list
;
1679 ctdb_sock_addr addr
;
1683 assert_single_node_only();
1690 port
= atoi(argv
[1]);
1693 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1694 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1698 ret
= ctdb_ctrl_get_tcp_tickles(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &addr
, &list
);
1700 DEBUG(DEBUG_ERR
, ("Unable to list tickles\n"));
1704 if (options
.machinereadable
){
1705 printf(":source ip:port:destination ip:port:\n");
1706 for (i
=0;i
<list
->tickles
.num
;i
++) {
1707 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1710 printf(":%s:%u", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1711 printf(":%s:%u:\n", ctdb_addr_to_str(&list
->tickles
.connections
[i
].dst_addr
), ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
));
1714 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list
->addr
));
1715 printf("Num tickles:%u\n", list
->tickles
.num
);
1716 for (i
=0;i
<list
->tickles
.num
;i
++) {
1717 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1720 printf("SRC: %s:%u ", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1721 printf("DST: %s:%u\n", ctdb_addr_to_str(&list
->tickles
.connections
[i
].dst_addr
), ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
));
1731 static int move_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1733 struct ctdb_all_public_ips
*ips
;
1734 struct ctdb_public_ip ip
;
1737 uint32_t disable_time
;
1739 struct ctdb_node_map
*nodemap
=NULL
;
1740 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1743 data
.dptr
= (uint8_t*)&disable_time
;
1744 data
.dsize
= sizeof(disable_time
);
1745 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1747 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1753 /* read the public ip list from the node */
1754 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), pnn
, ctdb
, &ips
);
1756 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", pnn
));
1757 talloc_free(tmp_ctx
);
1761 for (i
=0;i
<ips
->num
;i
++) {
1762 if (ctdb_same_ip(addr
, &ips
->ips
[i
].addr
)) {
1767 DEBUG(DEBUG_ERR
, ("Node %u can not host ip address '%s'\n",
1768 pnn
, ctdb_addr_to_str(addr
)));
1769 talloc_free(tmp_ctx
);
1776 data
.dptr
= (uint8_t *)&ip
;
1777 data
.dsize
= sizeof(ip
);
1779 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1781 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1782 talloc_free(tmp_ctx
);
1786 nodes
= list_of_nodes(ctdb
, nodemap
, tmp_ctx
, NODE_FLAGS_INACTIVE
, pnn
);
1787 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1794 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1795 talloc_free(tmp_ctx
);
1799 ret
= ctdb_ctrl_takeover_ip(ctdb
, LONGTIMELIMIT(), pnn
, &ip
);
1801 DEBUG(DEBUG_ERR
,("Failed to take over IP on node %d\n", pnn
));
1802 talloc_free(tmp_ctx
);
1806 /* update the recovery daemon so it now knows to expect the new
1807 node assignment for this ip.
1809 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_RECD_UPDATE_IP
, data
);
1811 DEBUG(DEBUG_ERR
,("Failed to send message to update the ip on the recovery master.\n"));
1815 talloc_free(tmp_ctx
);
1821 * scans all other nodes and returns a pnn for another node that can host this
1825 find_other_host_for_public_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1827 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1828 struct ctdb_all_public_ips
*ips
;
1829 struct ctdb_node_map
*nodemap
=NULL
;
1833 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1835 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1836 talloc_free(tmp_ctx
);
1840 for(i
=0;i
<nodemap
->num
;i
++){
1841 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
1844 if (nodemap
->nodes
[i
].pnn
== options
.pnn
) {
1848 /* read the public ip list from this node */
1849 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
1851 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
1855 for (j
=0;j
<ips
->num
;j
++) {
1856 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
1857 pnn
= nodemap
->nodes
[i
].pnn
;
1858 talloc_free(tmp_ctx
);
1865 talloc_free(tmp_ctx
);
1869 /* If pnn is -1 then try to find a node to move IP to... */
1870 static bool try_moveip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1872 bool pnn_specified
= (pnn
== -1 ? false : true);
1875 while (retries
< 5) {
1876 if (!pnn_specified
) {
1877 pnn
= find_other_host_for_public_ip(ctdb
, addr
);
1882 ("Trying to move public IP to node %u\n", pnn
));
1885 if (move_ip(ctdb
, addr
, pnn
) == 0) {
1898 move/failover an ip address to a specific node
1900 static int control_moveip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1903 ctdb_sock_addr addr
;
1905 assert_single_node_only();
1912 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1913 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1918 if (sscanf(argv
[1], "%u", &pnn
) != 1) {
1919 DEBUG(DEBUG_ERR
, ("Badly formed pnn\n"));
1923 if (!try_moveip(ctdb
, &addr
, pnn
)) {
1924 DEBUG(DEBUG_ERR
,("Failed to move IP to node %d.\n", pnn
));
1931 static int rebalance_node(struct ctdb_context
*ctdb
, uint32_t pnn
)
1935 data
.dptr
= (uint8_t *)&pnn
;
1936 data
.dsize
= sizeof(uint32_t);
1937 if (ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_REBALANCE_NODE
, data
) != 0) {
1939 ("Failed to send message to force node %u to be a rebalancing target\n",
1949 rebalance a node by setting it to allow failback and triggering a
1952 static int control_rebalancenode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1954 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1959 assert_single_node_only();
1965 /* Determine the nodes where IPs need to be reloaded */
1966 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1967 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1972 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1973 if (!rebalance_node(ctdb
, nodes
[i
])) {
1979 talloc_free(tmp_ctx
);
1983 static int rebalance_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1985 struct ctdb_public_ip ip
;
1988 uint32_t disable_time
;
1990 struct ctdb_node_map
*nodemap
=NULL
;
1991 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1994 data
.dptr
= (uint8_t*)&disable_time
;
1995 data
.dsize
= sizeof(disable_time
);
1996 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1998 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
2005 data
.dptr
= (uint8_t *)&ip
;
2006 data
.dsize
= sizeof(ip
);
2008 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
2010 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
2011 talloc_free(tmp_ctx
);
2015 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
2016 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
2023 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
2024 talloc_free(tmp_ctx
);
2028 talloc_free(tmp_ctx
);
2033 release an ip form all nodes and have it re-assigned by recd
2035 static int control_rebalanceip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2037 ctdb_sock_addr addr
;
2039 assert_single_node_only();
2046 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2047 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2051 if (rebalance_ip(ctdb
, &addr
) != 0) {
2052 DEBUG(DEBUG_ERR
,("Error when trying to reassign ip\n"));
2059 static int getips_store_callback(void *param
, void *data
)
2061 struct ctdb_public_ip
*node_ip
= (struct ctdb_public_ip
*)data
;
2062 struct ctdb_all_public_ips
*ips
= param
;
2066 ips
->ips
[i
].pnn
= node_ip
->pnn
;
2067 ips
->ips
[i
].addr
= node_ip
->addr
;
2071 static int getips_count_callback(void *param
, void *data
)
2073 uint32_t *count
= param
;
2080 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
2082 static uint32_t key
[IP_KEYLEN
];
2084 bzero(key
, sizeof(key
));
2086 switch (ip
->sa
.sa_family
) {
2088 key
[0] = ip
->ip
.sin_addr
.s_addr
;
2091 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
2099 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
2106 static void *add_ip_callback(void *parm
, void *data
)
2112 control_get_all_public_ips(struct ctdb_context
*ctdb
, TALLOC_CTX
*tmp_ctx
, struct ctdb_all_public_ips
**ips
)
2114 struct ctdb_all_public_ips
*tmp_ips
;
2115 struct ctdb_node_map
*nodemap
=NULL
;
2116 trbt_tree_t
*ip_tree
;
2120 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2122 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
2126 ip_tree
= trbt_create(tmp_ctx
, 0);
2128 for(i
=0;i
<nodemap
->num
;i
++){
2129 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
2132 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2136 /* read the public ip list from this node */
2137 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &tmp_ips
);
2139 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
2143 for (j
=0; j
<tmp_ips
->num
;j
++) {
2144 struct ctdb_public_ip
*node_ip
;
2146 node_ip
= talloc(tmp_ctx
, struct ctdb_public_ip
);
2147 node_ip
->pnn
= tmp_ips
->ips
[j
].pnn
;
2148 node_ip
->addr
= tmp_ips
->ips
[j
].addr
;
2150 trbt_insertarray32_callback(ip_tree
,
2151 IP_KEYLEN
, ip_key(&tmp_ips
->ips
[j
].addr
),
2155 talloc_free(tmp_ips
);
2160 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &count
);
2162 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
2163 count
*sizeof(struct ctdb_public_ip
);
2164 tmp_ips
= talloc_zero_size(tmp_ctx
, len
);
2165 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_store_callback
, tmp_ips
);
2173 static void ctdb_every_second(struct event_context
*ev
, struct timed_event
*te
, struct timeval t
, void *p
)
2175 struct ctdb_context
*ctdb
= talloc_get_type(p
, struct ctdb_context
);
2177 event_add_timed(ctdb
->ev
, ctdb
,
2178 timeval_current_ofs(1, 0),
2179 ctdb_every_second
, ctdb
);
2182 struct srvid_reply_handler_data
{
2186 const char *srvid_str
;
2189 static void srvid_broadcast_reply_handler(struct ctdb_context
*ctdb
,
2194 struct srvid_reply_handler_data
*d
=
2195 (struct srvid_reply_handler_data
*)private_data
;
2199 if (data
.dsize
!= sizeof(ret
)) {
2200 DEBUG(DEBUG_ERR
, (__location__
" Wrong reply size\n"));
2204 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2205 ret
= *(int32_t *)data
.dptr
;
2208 ("%s failed with result %d\n", d
->srvid_str
, ret
));
2212 if (!d
->wait_for_all
) {
2217 /* Wait for all replies */
2219 for (i
= 0; i
< talloc_array_length(d
->nodes
); i
++) {
2220 if (d
->nodes
[i
] == ret
) {
2222 ("%s reply received from node %u\n",
2223 d
->srvid_str
, ret
));
2226 if (d
->nodes
[i
] != -1) {
2227 /* Found a node that hasn't yet replied */
2233 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2234 * or replies from all connected nodes. arg is the data argument to
2235 * pass in the srvid_request structure - pass 0 if this isn't needed.
2237 static int srvid_broadcast(struct ctdb_context
*ctdb
,
2238 uint64_t srvid
, uint32_t *arg
,
2239 const char *srvid_str
, bool wait_for_all
)
2244 uint64_t reply_srvid
;
2245 struct srvid_request request
;
2246 struct srvid_request_data request_data
;
2247 struct srvid_reply_handler_data reply_data
;
2250 ZERO_STRUCT(request
);
2252 /* Time ticks to enable timeouts to be processed */
2253 event_add_timed(ctdb
->ev
, ctdb
,
2254 timeval_current_ofs(1, 0),
2255 ctdb_every_second
, ctdb
);
2257 pnn
= ctdb_get_pnn(ctdb
);
2258 reply_srvid
= getpid();
2262 request
.srvid
= reply_srvid
;
2264 data
.dptr
= (uint8_t *)&request
;
2265 data
.dsize
= sizeof(request
);
2267 request_data
.pnn
= pnn
;
2268 request_data
.srvid
= reply_srvid
;
2269 request_data
.data
= *arg
;
2271 data
.dptr
= (uint8_t *)&request_data
;
2272 data
.dsize
= sizeof(request_data
);
2275 /* Register message port for reply from recovery master */
2276 ctdb_client_set_message_handler(ctdb
, reply_srvid
,
2277 srvid_broadcast_reply_handler
,
2280 reply_data
.wait_for_all
= wait_for_all
;
2281 reply_data
.nodes
= NULL
;
2282 reply_data
.srvid_str
= srvid_str
;
2285 reply_data
.done
= false;
2288 struct ctdb_node_map
*nodemap
;
2290 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
2291 CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
2294 ("Unable to get nodemap from current node, try again\n"));
2299 if (reply_data
.nodes
!= NULL
) {
2300 talloc_free(reply_data
.nodes
);
2302 reply_data
.nodes
= list_of_connected_nodes(ctdb
, nodemap
,
2305 talloc_free(nodemap
);
2308 /* Send to all connected nodes. Only recmaster replies */
2309 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
,
2312 /* This can only happen if the socket is closed and
2313 * there's no way to recover from that, so don't try
2317 ("Failed to send %s request to connected nodes\n",
2322 tv
= timeval_current();
2323 /* This loop terminates the reply is received */
2324 while (timeval_elapsed(&tv
) < 5.0 && !reply_data
.done
) {
2325 event_loop_once(ctdb
->ev
);
2328 if (!reply_data
.done
) {
2330 ("Still waiting for confirmation of %s\n", srvid_str
));
2335 ctdb_client_remove_message_handler(ctdb
, reply_srvid
, &reply_data
);
2337 talloc_free(reply_data
.nodes
);
2342 static int ipreallocate(struct ctdb_context
*ctdb
)
2344 return srvid_broadcast(ctdb
, CTDB_SRVID_TAKEOVER_RUN
, NULL
,
2345 "IP reallocation", false);
2349 static int control_ipreallocate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2351 return ipreallocate(ctdb
);
2355 add a public ip address to a node
2357 static int control_addip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2360 int len
, retries
= 0;
2362 ctdb_sock_addr addr
;
2363 struct ctdb_control_ip_iface
*pub
;
2364 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2365 struct ctdb_all_public_ips
*ips
;
2369 talloc_free(tmp_ctx
);
2373 if (!parse_ip_mask(argv
[0], argv
[1], &addr
, &mask
)) {
2374 DEBUG(DEBUG_ERR
, ("Badly formed ip/mask : %s\n", argv
[0]));
2375 talloc_free(tmp_ctx
);
2379 /* read the public ip list from the node */
2380 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2382 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", options
.pnn
));
2383 talloc_free(tmp_ctx
);
2386 for (i
=0;i
<ips
->num
;i
++) {
2387 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2388 DEBUG(DEBUG_ERR
,("Can not add ip to node. Node already hosts this ip\n"));
2395 /* Dont timeout. This command waits for an ip reallocation
2396 which sometimes can take wuite a while if there has
2397 been a recent recovery
2401 len
= offsetof(struct ctdb_control_ip_iface
, iface
) + strlen(argv
[1]) + 1;
2402 pub
= talloc_size(tmp_ctx
, len
);
2403 CTDB_NO_MEMORY(ctdb
, pub
);
2407 pub
->len
= strlen(argv
[1])+1;
2408 memcpy(&pub
->iface
[0], argv
[1], strlen(argv
[1])+1);
2411 ret
= ctdb_ctrl_add_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, pub
);
2413 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options
.pnn
));
2417 } while (retries
< 5 && ret
!= 0);
2419 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Giving up.\n", options
.pnn
));
2420 talloc_free(tmp_ctx
);
2424 if (rebalance_node(ctdb
, options
.pnn
) != 0) {
2425 DEBUG(DEBUG_ERR
,("Error when trying to rebalance node\n"));
2429 talloc_free(tmp_ctx
);
2434 add a public ip address to a node
2436 static int control_ipiface(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2438 ctdb_sock_addr addr
;
2444 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2445 printf("Badly formed ip : %s\n", argv
[0]);
2449 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr
));
2454 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
);
2456 static int control_delip_all(struct ctdb_context
*ctdb
, int argc
, const char **argv
, ctdb_sock_addr
*addr
)
2458 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2459 struct ctdb_node_map
*nodemap
=NULL
;
2460 struct ctdb_all_public_ips
*ips
;
2463 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2465 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from current node\n"));
2469 /* remove it from the nodes that are not hosting the ip currently */
2470 for(i
=0;i
<nodemap
->num
;i
++){
2471 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2474 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2476 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2480 for (j
=0;j
<ips
->num
;j
++) {
2481 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2489 if (ips
->ips
[j
].pnn
== nodemap
->nodes
[i
].pnn
) {
2493 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2494 control_delip(ctdb
, argc
, argv
);
2498 /* remove it from every node (also the one hosting it) */
2499 for(i
=0;i
<nodemap
->num
;i
++){
2500 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2503 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2505 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2509 for (j
=0;j
<ips
->num
;j
++) {
2510 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2518 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2519 control_delip(ctdb
, argc
, argv
);
2522 talloc_free(tmp_ctx
);
2527 delete a public ip address from a node
2529 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2532 ctdb_sock_addr addr
;
2533 struct ctdb_control_ip_iface pub
;
2534 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2535 struct ctdb_all_public_ips
*ips
;
2538 talloc_free(tmp_ctx
);
2542 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2543 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2547 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2548 return control_delip_all(ctdb
, argc
, argv
, &addr
);
2555 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2557 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from cluster\n"));
2558 talloc_free(tmp_ctx
);
2562 for (i
=0;i
<ips
->num
;i
++) {
2563 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2569 DEBUG(DEBUG_ERR
, ("This node does not support this public address '%s'\n",
2570 ctdb_addr_to_str(&addr
)));
2571 talloc_free(tmp_ctx
);
2575 /* This is an optimisation. If this node is hosting the IP
2576 * then try to move it somewhere else without invoking a full
2577 * takeover run. We don't care if this doesn't work!
2579 if (ips
->ips
[i
].pnn
== options
.pnn
) {
2580 (void) try_moveip(ctdb
, &addr
, -1);
2583 ret
= ctdb_ctrl_del_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, &pub
);
2585 DEBUG(DEBUG_ERR
, ("Unable to del public ip from node %u\n", options
.pnn
));
2586 talloc_free(tmp_ctx
);
2590 talloc_free(tmp_ctx
);
2594 static int kill_tcp_from_file(struct ctdb_context
*ctdb
,
2595 int argc
, const char **argv
)
2597 struct ctdb_control_killtcp
*killtcp
;
2598 int max_entries
, current
, i
;
2599 struct timeval timeout
;
2600 char line
[128], src
[128], dst
[128];
2603 struct client_async_data
*async_data
;
2604 struct ctdb_client_control_state
*state
;
2614 while (!feof(stdin
)) {
2615 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2619 /* Silently skip empty lines */
2620 if (line
[0] == '\n') {
2624 if (sscanf(line
, "%s %s\n", src
, dst
) != 2) {
2625 DEBUG(DEBUG_ERR
, ("Bad line [%d]: '%s'\n",
2627 talloc_free(killtcp
);
2631 if (current
>= max_entries
) {
2632 max_entries
+= 1024;
2633 killtcp
= talloc_realloc(ctdb
, killtcp
,
2634 struct ctdb_control_killtcp
,
2636 CTDB_NO_MEMORY(ctdb
, killtcp
);
2639 if (!parse_ip_port(src
, &killtcp
[current
].src_addr
)) {
2640 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2642 talloc_free(killtcp
);
2646 if (!parse_ip_port(dst
, &killtcp
[current
].dst_addr
)) {
2647 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2649 talloc_free(killtcp
);
2656 async_data
= talloc_zero(ctdb
, struct client_async_data
);
2657 if (async_data
== NULL
) {
2658 talloc_free(killtcp
);
2662 for (i
= 0; i
< current
; i
++) {
2664 data
.dsize
= sizeof(struct ctdb_control_killtcp
);
2665 data
.dptr
= (unsigned char *)&killtcp
[i
];
2667 timeout
= TIMELIMIT();
2668 state
= ctdb_control_send(ctdb
, options
.pnn
, 0,
2669 CTDB_CONTROL_KILL_TCP
, 0, data
,
2670 async_data
, &timeout
, NULL
);
2672 if (state
== NULL
) {
2674 ("Failed to call async killtcp control to node %u\n",
2676 talloc_free(killtcp
);
2680 ctdb_client_async_add(async_data
, state
);
2683 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2684 DEBUG(DEBUG_ERR
,("killtcp failed\n"));
2685 talloc_free(killtcp
);
2689 talloc_free(killtcp
);
2695 kill a tcp connection
2697 static int kill_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2700 struct ctdb_control_killtcp killtcp
;
2702 assert_single_node_only();
2705 return kill_tcp_from_file(ctdb
, argc
, argv
);
2712 if (!parse_ip_port(argv
[0], &killtcp
.src_addr
)) {
2713 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2717 if (!parse_ip_port(argv
[1], &killtcp
.dst_addr
)) {
2718 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2722 ret
= ctdb_ctrl_killtcp(ctdb
, TIMELIMIT(), options
.pnn
, &killtcp
);
2724 DEBUG(DEBUG_ERR
, ("Unable to killtcp from node %u\n", options
.pnn
));
2735 static int control_gratious_arp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2738 ctdb_sock_addr addr
;
2740 assert_single_node_only();
2746 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2747 DEBUG(DEBUG_ERR
, ("Bad IP '%s'\n", argv
[0]));
2751 ret
= ctdb_ctrl_gratious_arp(ctdb
, TIMELIMIT(), options
.pnn
, &addr
, argv
[1]);
2753 DEBUG(DEBUG_ERR
, ("Unable to send gratious_arp from node %u\n", options
.pnn
));
2761 register a server id
2763 static int regsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2766 struct ctdb_server_id server_id
;
2772 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2773 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2774 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2776 ret
= ctdb_ctrl_register_server_id(ctdb
, TIMELIMIT(), &server_id
);
2778 DEBUG(DEBUG_ERR
, ("Unable to register server_id from node %u\n", options
.pnn
));
2781 DEBUG(DEBUG_ERR
,("Srvid registered. Sleeping for 999 seconds\n"));
2787 unregister a server id
2789 static int unregsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2792 struct ctdb_server_id server_id
;
2798 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2799 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2800 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2802 ret
= ctdb_ctrl_unregister_server_id(ctdb
, TIMELIMIT(), &server_id
);
2804 DEBUG(DEBUG_ERR
, ("Unable to unregister server_id from node %u\n", options
.pnn
));
2811 check if a server id exists
2813 static int chksrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2817 struct ctdb_server_id server_id
;
2823 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2824 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2825 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2827 ret
= ctdb_ctrl_check_server_id(ctdb
, TIMELIMIT(), options
.pnn
, &server_id
, &status
);
2829 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n", options
.pnn
));
2834 printf("Server id %d:%d:%d EXISTS\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2836 printf("Server id %d:%d:%d does NOT exist\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2842 get a list of all server ids that are registered on a node
2844 static int getsrvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2847 struct ctdb_server_id_list
*server_ids
;
2849 ret
= ctdb_ctrl_get_server_id_list(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &server_ids
);
2851 DEBUG(DEBUG_ERR
, ("Unable to get server_id list from node %u\n", options
.pnn
));
2855 for (i
=0; i
<server_ids
->num
; i
++) {
2856 printf("Server id %d:%d:%d\n",
2857 server_ids
->server_ids
[i
].pnn
,
2858 server_ids
->server_ids
[i
].type
,
2859 server_ids
->server_ids
[i
].server_id
);
2866 check if a server id exists
2868 static int check_srvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2870 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
2876 talloc_free(tmp_ctx
);
2880 ids
= talloc_array(tmp_ctx
, uint64_t, argc
);
2881 result
= talloc_array(tmp_ctx
, uint8_t, argc
);
2883 for (i
= 0; i
< argc
; i
++) {
2884 ids
[i
] = strtoull(argv
[i
], NULL
, 0);
2887 if (!ctdb_client_check_message_handlers(ctdb
, ids
, argc
, result
)) {
2888 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n",
2890 talloc_free(tmp_ctx
);
2894 for (i
=0; i
< argc
; i
++) {
2895 printf("Server id %d:%llu %s\n", options
.pnn
, (long long)ids
[i
],
2896 result
[i
] ? "exists" : "does not exist");
2899 talloc_free(tmp_ctx
);
2904 send a tcp tickle ack
2906 static int tickle_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2909 ctdb_sock_addr src
, dst
;
2915 if (!parse_ip_port(argv
[0], &src
)) {
2916 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2920 if (!parse_ip_port(argv
[1], &dst
)) {
2921 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2925 ret
= ctdb_sys_send_tcp(&src
, &dst
, 0, 0, 0);
2929 DEBUG(DEBUG_ERR
, ("Error while sending tickle ack\n"));
2936 display public ip status
2938 static int control_ip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2941 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2942 struct ctdb_all_public_ips
*ips
;
2944 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2945 /* read the list of public ips from all nodes */
2946 ret
= control_get_all_public_ips(ctdb
, tmp_ctx
, &ips
);
2948 /* read the public ip list from this node */
2949 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2952 DEBUG(DEBUG_ERR
, ("Unable to get public ips from node %u\n", options
.pnn
));
2953 talloc_free(tmp_ctx
);
2957 if (options
.machinereadable
){
2958 printf(":Public IP:Node:");
2959 if (options
.verbose
){
2960 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2964 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2965 printf("Public IPs on ALL nodes\n");
2967 printf("Public IPs on node %u\n", options
.pnn
);
2971 for (i
=1;i
<=ips
->num
;i
++) {
2972 struct ctdb_control_public_ip_info
*info
= NULL
;
2974 char *aciface
= NULL
;
2975 char *avifaces
= NULL
;
2976 char *cifaces
= NULL
;
2978 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2979 pnn
= ips
->ips
[ips
->num
-i
].pnn
;
2985 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), pnn
, ctdb
,
2986 &ips
->ips
[ips
->num
-i
].addr
, &info
);
2993 for (j
=0; j
< info
->num
; j
++) {
2994 if (cifaces
== NULL
) {
2995 cifaces
= talloc_strdup(info
,
2996 info
->ifaces
[j
].name
);
2998 cifaces
= talloc_asprintf_append(cifaces
,
3000 info
->ifaces
[j
].name
);
3003 if (info
->active_idx
== j
) {
3004 aciface
= info
->ifaces
[j
].name
;
3007 if (info
->ifaces
[j
].link_state
== 0) {
3011 if (avifaces
== NULL
) {
3012 avifaces
= talloc_strdup(info
, info
->ifaces
[j
].name
);
3014 avifaces
= talloc_asprintf_append(avifaces
,
3016 info
->ifaces
[j
].name
);
3021 if (options
.machinereadable
){
3023 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
3024 ips
->ips
[ips
->num
-i
].pnn
);
3025 if (options
.verbose
){
3028 avifaces
?avifaces
:"",
3029 cifaces
?cifaces
:"");
3033 if (options
.verbose
) {
3034 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
3035 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
3036 ips
->ips
[ips
->num
-i
].pnn
,
3038 avifaces
?avifaces
:"",
3039 cifaces
?cifaces
:"");
3042 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
3043 ips
->ips
[ips
->num
-i
].pnn
);
3049 talloc_free(tmp_ctx
);
3056 static int control_ipinfo(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3059 ctdb_sock_addr addr
;
3060 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3061 struct ctdb_control_public_ip_info
*info
;
3064 talloc_free(tmp_ctx
);
3068 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
3069 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
3073 /* read the public ip info from this node */
3074 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), options
.pnn
,
3075 tmp_ctx
, &addr
, &info
);
3077 DEBUG(DEBUG_ERR
, ("Unable to get public ip[%s]info from node %u\n",
3078 argv
[0], options
.pnn
));
3079 talloc_free(tmp_ctx
);
3083 printf("Public IP[%s] info on node %u\n",
3084 ctdb_addr_to_str(&info
->ip
.addr
),
3087 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3088 ctdb_addr_to_str(&info
->ip
.addr
),
3089 info
->ip
.pnn
, info
->num
);
3091 for (i
=0; i
<info
->num
; i
++) {
3092 info
->ifaces
[i
].name
[CTDB_IFACE_SIZE
] = '\0';
3094 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3095 i
+1, info
->ifaces
[i
].name
,
3096 info
->ifaces
[i
].link_state
?"up":"down",
3097 (unsigned int)info
->ifaces
[i
].references
,
3098 (i
==info
->active_idx
)?" (active)":"");
3101 talloc_free(tmp_ctx
);
3106 display interfaces status
3108 static int control_ifaces(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3110 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3112 struct ctdb_control_get_ifaces
*ifaces
;
3115 /* read the public ip list from this node */
3116 ret
= ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ifaces
);
3118 DEBUG(DEBUG_ERR
, ("Unable to get interfaces from node %u\n",
3120 talloc_free(tmp_ctx
);
3124 if (options
.machinereadable
){
3125 printf(":Name:LinkStatus:References:\n");
3127 printf("Interfaces on node %u\n", options
.pnn
);
3130 for (i
=0; i
<ifaces
->num
; i
++) {
3131 if (options
.machinereadable
){
3132 printf(":%s:%s:%u\n",
3133 ifaces
->ifaces
[i
].name
,
3134 ifaces
->ifaces
[i
].link_state
?"1":"0",
3135 (unsigned int)ifaces
->ifaces
[i
].references
);
3137 printf("name:%s link:%s references:%u\n",
3138 ifaces
->ifaces
[i
].name
,
3139 ifaces
->ifaces
[i
].link_state
?"up":"down",
3140 (unsigned int)ifaces
->ifaces
[i
].references
);
3144 talloc_free(tmp_ctx
);
3150 set link status of an interface
3152 static int control_setifacelink(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3155 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3156 struct ctdb_control_iface_info info
;
3164 if (strlen(argv
[0]) > CTDB_IFACE_SIZE
) {
3165 DEBUG(DEBUG_ERR
, ("interfaces name '%s' too long\n",
3167 talloc_free(tmp_ctx
);
3170 strcpy(info
.name
, argv
[0]);
3172 if (strcmp(argv
[1], "up") == 0) {
3173 info
.link_state
= 1;
3174 } else if (strcmp(argv
[1], "down") == 0) {
3175 info
.link_state
= 0;
3177 DEBUG(DEBUG_ERR
, ("link state invalid '%s' should be 'up' or 'down'\n",
3179 talloc_free(tmp_ctx
);
3183 /* read the public ip list from this node */
3184 ret
= ctdb_ctrl_set_iface_link(ctdb
, TIMELIMIT(), options
.pnn
,
3187 DEBUG(DEBUG_ERR
, ("Unable to set link state for interfaces %s node %u\n",
3188 argv
[0], options
.pnn
));
3189 talloc_free(tmp_ctx
);
3193 talloc_free(tmp_ctx
);
3198 display pid of a ctdb daemon
3200 static int control_getpid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3205 ret
= ctdb_ctrl_getpid(ctdb
, TIMELIMIT(), options
.pnn
, &pid
);
3207 DEBUG(DEBUG_ERR
, ("Unable to get daemon pid from node %u\n", options
.pnn
));
3210 printf("Pid:%d\n", pid
);
3215 typedef bool update_flags_handler_t(struct ctdb_context
*ctdb
, void *data
);
3217 static int update_flags_and_ipreallocate(struct ctdb_context
*ctdb
,
3219 update_flags_handler_t handler
,
3224 struct ctdb_node_map
*nodemap
= NULL
;
3228 /* Check if the node is already in the desired state */
3229 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3231 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3234 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3235 if (set_flag
== flag_is_set
) {
3236 DEBUG(DEBUG_NOTICE
, ("Node %d is %s %s\n", options
.pnn
,
3237 (set_flag
? "already" : "not"), desc
));
3242 if (!handler(ctdb
, data
)) {
3243 DEBUG(DEBUG_WARNING
,
3244 ("Failed to send control to set state %s on node %u, try again\n",
3245 desc
, options
.pnn
));
3250 /* Read the nodemap and verify the change took effect.
3251 * Even if the above control/hanlder timed out then it
3252 * could still have worked!
3254 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
3257 DEBUG(DEBUG_WARNING
,
3258 ("Unable to get nodemap from local node, try again\n"));
3260 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3261 } while (nodemap
== NULL
|| (set_flag
!= flag_is_set
));
3263 return ipreallocate(ctdb
);
3266 /* Administratively disable a node */
3267 static bool update_flags_disabled(struct ctdb_context
*ctdb
, void *data
)
3271 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3272 NODE_FLAGS_PERMANENTLY_DISABLED
, 0);
3276 static int control_disable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3278 return update_flags_and_ipreallocate(ctdb
, NULL
,
3279 update_flags_disabled
,
3280 NODE_FLAGS_PERMANENTLY_DISABLED
,
3282 true /* set_flag*/);
3285 /* Administratively re-enable a node */
3286 static bool update_flags_not_disabled(struct ctdb_context
*ctdb
, void *data
)
3290 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3291 0, NODE_FLAGS_PERMANENTLY_DISABLED
);
3295 static int control_enable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3297 return update_flags_and_ipreallocate(ctdb
, NULL
,
3298 update_flags_not_disabled
,
3299 NODE_FLAGS_PERMANENTLY_DISABLED
,
3301 false /* set_flag*/);
3305 static bool update_flags_stopped(struct ctdb_context
*ctdb
, void *data
)
3309 ret
= ctdb_ctrl_stop_node(ctdb
, TIMELIMIT(), options
.pnn
);
3314 static int control_stop(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3316 return update_flags_and_ipreallocate(ctdb
, NULL
,
3317 update_flags_stopped
,
3320 true /* set_flag*/);
3323 /* Continue a stopped node */
3324 static bool update_flags_not_stopped(struct ctdb_context
*ctdb
, void *data
)
3328 ret
= ctdb_ctrl_continue_node(ctdb
, TIMELIMIT(), options
.pnn
);
3333 static int control_continue(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3335 return update_flags_and_ipreallocate(ctdb
, NULL
,
3336 update_flags_not_stopped
,
3339 false /* set_flag */);
3342 static uint32_t get_generation(struct ctdb_context
*ctdb
)
3344 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3345 struct ctdb_vnn_map
*vnnmap
=NULL
;
3347 uint32_t generation
;
3349 /* wait until the recmaster is not in recovery mode */
3351 uint32_t recmode
, recmaster
;
3353 if (vnnmap
!= NULL
) {
3354 talloc_free(vnnmap
);
3358 /* get the recmaster */
3359 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), CTDB_CURRENT_NODE
, &recmaster
);
3361 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
3362 talloc_free(tmp_ctx
);
3366 /* get recovery mode */
3367 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), recmaster
, &recmode
);
3369 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
3370 talloc_free(tmp_ctx
);
3374 /* get the current generation number */
3375 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), recmaster
, tmp_ctx
, &vnnmap
);
3377 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from recmaster (%u)\n", recmaster
));
3378 talloc_free(tmp_ctx
);
3382 if ((recmode
== CTDB_RECOVERY_NORMAL
) && (vnnmap
->generation
!= 1)) {
3383 generation
= vnnmap
->generation
;
3384 talloc_free(tmp_ctx
);
3392 static bool update_state_banned(struct ctdb_context
*ctdb
, void *data
)
3394 struct ctdb_ban_time
*bantime
= (struct ctdb_ban_time
*)data
;
3397 ret
= ctdb_ctrl_set_ban(ctdb
, TIMELIMIT(), options
.pnn
, bantime
);
3402 static int control_ban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3404 struct ctdb_ban_time bantime
;
3410 bantime
.pnn
= options
.pnn
;
3411 bantime
.time
= strtoul(argv
[0], NULL
, 0);
3413 if (bantime
.time
== 0) {
3414 DEBUG(DEBUG_ERR
, ("Invalid ban time specified - must be >0\n"));
3418 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3419 update_state_banned
,
3422 true /* set_flag*/);
3427 static int control_unban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3429 struct ctdb_ban_time bantime
;
3431 bantime
.pnn
= options
.pnn
;
3434 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3435 update_state_banned
,
3438 false /* set_flag*/);
3442 show ban information for a node
3444 static int control_showban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3447 struct ctdb_node_map
*nodemap
=NULL
;
3448 struct ctdb_ban_time
*bantime
;
3450 /* verify the node exists */
3451 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3453 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3457 ret
= ctdb_ctrl_get_ban(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &bantime
);
3459 DEBUG(DEBUG_ERR
,("Showing ban info for node %d failed.\n", options
.pnn
));
3463 if (bantime
->time
== 0) {
3464 printf("Node %u is not banned\n", bantime
->pnn
);
3466 printf("Node %u is banned, %d seconds remaining\n",
3467 bantime
->pnn
, bantime
->time
);
3476 static int control_shutdown(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3480 ret
= ctdb_ctrl_shutdown(ctdb
, TIMELIMIT(), options
.pnn
);
3482 DEBUG(DEBUG_ERR
, ("Unable to shutdown node %u\n", options
.pnn
));
3492 static int control_recover(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3495 uint32_t generation
, next_generation
;
3497 /* record the current generation number */
3498 generation
= get_generation(ctdb
);
3500 ret
= ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
3502 DEBUG(DEBUG_ERR
, ("Unable to set recovery mode\n"));
3506 /* wait until we are in a new generation */
3508 next_generation
= get_generation(ctdb
);
3509 if (next_generation
!= generation
) {
3520 display monitoring mode of a remote node
3522 static int control_getmonmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3527 ret
= ctdb_ctrl_getmonmode(ctdb
, TIMELIMIT(), options
.pnn
, &monmode
);
3529 DEBUG(DEBUG_ERR
, ("Unable to get monmode from node %u\n", options
.pnn
));
3532 if (!options
.machinereadable
){
3533 printf("Monitoring mode:%s (%d)\n",monmode
==CTDB_MONITORING_ACTIVE
?"ACTIVE":"DISABLED",monmode
);
3536 printf(":%d:\n",monmode
);
3543 display capabilities of a remote node
3545 static int control_getcapabilities(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3547 uint32_t capabilities
;
3550 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), options
.pnn
, &capabilities
);
3552 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", options
.pnn
));
3556 if (!options
.machinereadable
){
3557 printf("RECMASTER: %s\n", (capabilities
&CTDB_CAP_RECMASTER
)?"YES":"NO");
3558 printf("LMASTER: %s\n", (capabilities
&CTDB_CAP_LMASTER
)?"YES":"NO");
3559 printf("LVS: %s\n", (capabilities
&CTDB_CAP_LVS
)?"YES":"NO");
3560 printf("NATGW: %s\n", (capabilities
&CTDB_CAP_NATGW
)?"YES":"NO");
3562 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3563 printf(":%d:%d:%d:%d:\n",
3564 !!(capabilities
&CTDB_CAP_RECMASTER
),
3565 !!(capabilities
&CTDB_CAP_LMASTER
),
3566 !!(capabilities
&CTDB_CAP_LVS
),
3567 !!(capabilities
&CTDB_CAP_NATGW
));
3573 display lvs configuration
3576 static uint32_t lvs_exclude_flags
[] = {
3577 /* Look for a nice healthy node */
3578 NODE_FLAGS_INACTIVE
|NODE_FLAGS_DISABLED
,
3579 /* If not found, an UNHEALTHY node will do */
3580 NODE_FLAGS_INACTIVE
|NODE_FLAGS_PERMANENTLY_DISABLED
,
3584 static int control_lvs(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3586 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3587 struct ctdb_node_map
*orig_nodemap
=NULL
;
3588 struct ctdb_node_map
*nodemap
;
3591 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3592 tmp_ctx
, &orig_nodemap
);
3594 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3595 talloc_free(tmp_ctx
);
3599 nodemap
= filter_nodemap_by_capabilities(ctdb
, orig_nodemap
,
3600 CTDB_CAP_LVS
, false);
3601 if (nodemap
== NULL
) {
3609 for (i
= 0; lvs_exclude_flags
[i
] != 0; i
++) {
3610 struct ctdb_node_map
*t
=
3611 filter_nodemap_by_flags(ctdb
, nodemap
,
3612 lvs_exclude_flags
[i
]);
3619 /* At least 1 node without excluded flags */
3621 for (j
= 0; j
< t
->num
; j
++) {
3622 printf("%d:%s\n", t
->nodes
[j
].pnn
,
3623 ctdb_addr_to_str(&t
->nodes
[j
].addr
));
3630 talloc_free(tmp_ctx
);
3635 display who is the lvs master
3637 static int control_lvsmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3639 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3640 struct ctdb_node_map
*nodemap
=NULL
;
3643 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3646 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3647 talloc_free(tmp_ctx
);
3651 for (i
= 0; lvs_exclude_flags
[i
] != 0; i
++) {
3652 struct ctdb_node_map
*t
=
3653 filter_nodemap_by_flags(ctdb
, nodemap
,
3654 lvs_exclude_flags
[i
]);
3661 struct ctdb_node_map
*n
;
3662 n
= filter_nodemap_by_capabilities(ctdb
,
3673 printf(options
.machinereadable
?
3674 "%d\n" : "Node %d is LVS master\n",
3682 printf("There is no LVS master\n");
3685 talloc_free(tmp_ctx
);
3690 disable monitoring on a node
3692 static int control_disable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3697 ret
= ctdb_ctrl_disable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3699 DEBUG(DEBUG_ERR
, ("Unable to disable monmode on node %u\n", options
.pnn
));
3702 printf("Monitoring mode:%s\n","DISABLED");
3708 enable monitoring on a node
3710 static int control_enable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3715 ret
= ctdb_ctrl_enable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3717 DEBUG(DEBUG_ERR
, ("Unable to enable monmode on node %u\n", options
.pnn
));
3720 printf("Monitoring mode:%s\n","ACTIVE");
3726 display remote list of keys/data for a db
3728 static int control_catdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3730 const char *db_name
;
3731 struct ctdb_db_context
*ctdb_db
;
3733 struct ctdb_dump_db_context c
;
3740 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3744 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3745 if (ctdb_db
== NULL
) {
3746 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3750 if (options
.printlmaster
) {
3751 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
,
3752 ctdb
, &ctdb
->vnn_map
);
3754 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
3762 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3763 c
.printdatasize
= (bool)options
.printdatasize
;
3764 c
.printlmaster
= (bool)options
.printlmaster
;
3765 c
.printhash
= (bool)options
.printhash
;
3766 c
.printrecordflags
= (bool)options
.printrecordflags
;
3768 /* traverse and dump the cluster tdb */
3769 ret
= ctdb_dump_db(ctdb_db
, &c
);
3771 DEBUG(DEBUG_ERR
, ("Unable to dump database\n"));
3772 DEBUG(DEBUG_ERR
, ("Maybe try 'ctdb getdbstatus %s'"
3773 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3777 talloc_free(ctdb_db
);
3779 printf("Dumped %d records\n", ret
);
3783 struct cattdb_data
{
3784 struct ctdb_context
*ctdb
;
3788 static int cattdb_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private_data
)
3790 struct cattdb_data
*d
= private_data
;
3791 struct ctdb_dump_db_context c
;
3797 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3798 c
.printdatasize
= (bool)options
.printdatasize
;
3799 c
.printlmaster
= false;
3800 c
.printhash
= (bool)options
.printhash
;
3801 c
.printrecordflags
= true;
3803 return ctdb_dumpdb_record(d
->ctdb
, key
, data
, &c
);
3807 cat the local tdb database using same format as catdb
3809 static int control_cattdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3811 const char *db_name
;
3812 struct ctdb_db_context
*ctdb_db
;
3813 struct cattdb_data d
;
3820 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3824 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3825 if (ctdb_db
== NULL
) {
3826 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3830 /* traverse the local tdb */
3833 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, cattdb_traverse
, &d
) == -1) {
3834 printf("Failed to cattdb data\n");
3837 talloc_free(ctdb_db
);
3839 printf("Dumped %d records\n", d
.count
);
3844 display the content of a database key
3846 static int control_readkey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3848 const char *db_name
;
3849 struct ctdb_db_context
*ctdb_db
;
3850 struct ctdb_record_handle
*h
;
3851 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3859 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3863 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3864 if (ctdb_db
== NULL
) {
3865 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3869 key
.dptr
= discard_const(argv
[1]);
3870 key
.dsize
= strlen((char *)key
.dptr
);
3872 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3874 printf("Failed to fetch record '%s' on node %d\n",
3875 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3876 talloc_free(tmp_ctx
);
3880 printf("Data: size:%d ptr:[%.*s]\n", (int)data
.dsize
, (int)data
.dsize
, data
.dptr
);
3882 talloc_free(tmp_ctx
);
3883 talloc_free(ctdb_db
);
3888 display the content of a database key
3890 static int control_writekey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3892 const char *db_name
;
3893 struct ctdb_db_context
*ctdb_db
;
3894 struct ctdb_record_handle
*h
;
3895 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3903 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3907 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3908 if (ctdb_db
== NULL
) {
3909 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3913 key
.dptr
= discard_const(argv
[1]);
3914 key
.dsize
= strlen((char *)key
.dptr
);
3916 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3918 printf("Failed to fetch record '%s' on node %d\n",
3919 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3920 talloc_free(tmp_ctx
);
3924 data
.dptr
= discard_const(argv
[2]);
3925 data
.dsize
= strlen((char *)data
.dptr
);
3927 if (ctdb_record_store(h
, data
) != 0) {
3928 printf("Failed to store record\n");
3932 talloc_free(tmp_ctx
);
3933 talloc_free(ctdb_db
);
3938 fetch a record from a persistent database
3940 static int control_pfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3942 const char *db_name
;
3943 struct ctdb_db_context
*ctdb_db
;
3944 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3945 struct ctdb_transaction_handle
*h
;
3952 talloc_free(tmp_ctx
);
3956 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3957 talloc_free(tmp_ctx
);
3961 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
3963 DEBUG(DEBUG_ERR
,("Database '%s' is not persistent\n", db_name
));
3964 talloc_free(tmp_ctx
);
3968 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
3969 if (ctdb_db
== NULL
) {
3970 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3971 talloc_free(tmp_ctx
);
3975 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
3977 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
3978 talloc_free(tmp_ctx
);
3982 key
.dptr
= discard_const(argv
[1]);
3983 key
.dsize
= strlen(argv
[1]);
3984 ret
= ctdb_transaction_fetch(h
, tmp_ctx
, key
, &data
);
3986 DEBUG(DEBUG_ERR
,("Failed to fetch record\n"));
3987 talloc_free(tmp_ctx
);
3991 if (data
.dsize
== 0 || data
.dptr
== NULL
) {
3992 DEBUG(DEBUG_ERR
,("Record is empty\n"));
3993 talloc_free(tmp_ctx
);
3998 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
4000 DEBUG(DEBUG_ERR
,("Failed to open output file %s\n", argv
[2]));
4001 talloc_free(tmp_ctx
);
4004 sys_write(fd
, data
.dptr
, data
.dsize
);
4007 sys_write(1, data
.dptr
, data
.dsize
);
4010 /* abort the transaction */
4014 talloc_free(tmp_ctx
);
4019 fetch a record from a tdb-file
4021 static int control_tfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4023 const char *tdb_file
;
4026 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4035 tdb
= tdb_open(tdb_file
, 0, 0, O_RDONLY
, 0);
4037 printf("Failed to open TDB file %s\n", tdb_file
);
4041 if (!strncmp(argv
[1], "0x", 2)) {
4042 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4043 if (key
.dsize
== 0) {
4044 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4048 key
.dptr
= discard_const(argv
[1]);
4049 key
.dsize
= strlen(argv
[1]);
4052 data
= tdb_fetch(tdb
, key
);
4053 if (data
.dptr
== NULL
|| data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
4054 printf("Failed to read record %s from tdb %s\n", argv
[1], tdb_file
);
4062 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
4064 printf("Failed to open output file %s\n", argv
[2]);
4067 if (options
.verbose
){
4068 sys_write(fd
, data
.dptr
, data
.dsize
);
4070 sys_write(fd
, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4074 if (options
.verbose
){
4075 sys_write(1, data
.dptr
, data
.dsize
);
4077 sys_write(1, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4081 talloc_free(tmp_ctx
);
4086 store a record and header to a tdb-file
4088 static int control_tstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4090 const char *tdb_file
;
4092 TDB_DATA key
, value
, data
;
4093 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4094 struct ctdb_ltdb_header header
;
4102 tdb
= tdb_open(tdb_file
, 0, 0, O_RDWR
, 0);
4104 printf("Failed to open TDB file %s\n", tdb_file
);
4108 if (!strncmp(argv
[1], "0x", 2)) {
4109 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4110 if (key
.dsize
== 0) {
4111 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4115 key
.dptr
= discard_const(argv
[1]);
4116 key
.dsize
= strlen(argv
[1]);
4119 if (!strncmp(argv
[2], "0x", 2)) {
4120 value
= hextodata(tmp_ctx
, argv
[2] + 2);
4121 if (value
.dsize
== 0) {
4122 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[2]);
4126 value
.dptr
= discard_const(argv
[2]);
4127 value
.dsize
= strlen(argv
[2]);
4130 ZERO_STRUCT(header
);
4132 header
.rsn
= atoll(argv
[3]);
4135 header
.dmaster
= atoi(argv
[4]);
4138 header
.flags
= atoi(argv
[5]);
4141 data
.dsize
= sizeof(struct ctdb_ltdb_header
) + value
.dsize
;
4142 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4143 if (data
.dptr
== NULL
) {
4144 printf("Failed to allocate header+value\n");
4148 *(struct ctdb_ltdb_header
*)data
.dptr
= header
;
4149 memcpy(data
.dptr
+ sizeof(struct ctdb_ltdb_header
), value
.dptr
, value
.dsize
);
4151 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0) {
4152 printf("Failed to write record %s to tdb %s\n", argv
[1], tdb_file
);
4159 talloc_free(tmp_ctx
);
4164 write a record to a persistent database
4166 static int control_pstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4168 const char *db_name
;
4169 struct ctdb_db_context
*ctdb_db
;
4170 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4171 struct ctdb_transaction_handle
*h
;
4177 talloc_free(tmp_ctx
);
4181 fd
= open(argv
[2], O_RDONLY
);
4183 DEBUG(DEBUG_ERR
,("Failed to open file containing record data : %s %s\n", argv
[2], strerror(errno
)));
4184 talloc_free(tmp_ctx
);
4188 ret
= fstat(fd
, &st
);
4190 DEBUG(DEBUG_ERR
,("fstat of file %s failed: %s\n", argv
[2], strerror(errno
)));
4192 talloc_free(tmp_ctx
);
4196 if (!S_ISREG(st
.st_mode
)) {
4197 DEBUG(DEBUG_ERR
,("Not a regular file %s\n", argv
[2]));
4199 talloc_free(tmp_ctx
);
4203 data
.dsize
= st
.st_size
;
4204 if (data
.dsize
== 0) {
4207 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4208 if (data
.dptr
== NULL
) {
4209 DEBUG(DEBUG_ERR
,("Failed to talloc %d of memory to store record data\n", (int)data
.dsize
));
4211 talloc_free(tmp_ctx
);
4214 ret
= sys_read(fd
, data
.dptr
, data
.dsize
);
4215 if (ret
!= data
.dsize
) {
4216 DEBUG(DEBUG_ERR
,("Failed to read %d bytes of record data\n", (int)data
.dsize
));
4218 talloc_free(tmp_ctx
);
4227 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4228 if (ctdb_db
== NULL
) {
4229 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4230 talloc_free(tmp_ctx
);
4234 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4236 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4237 talloc_free(tmp_ctx
);
4241 key
.dptr
= discard_const(argv
[1]);
4242 key
.dsize
= strlen(argv
[1]);
4243 ret
= ctdb_transaction_store(h
, key
, data
);
4245 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4246 talloc_free(tmp_ctx
);
4250 ret
= ctdb_transaction_commit(h
);
4252 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4253 talloc_free(tmp_ctx
);
4258 talloc_free(tmp_ctx
);
4263 * delete a record from a persistent database
4265 static int control_pdelete(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4267 const char *db_name
;
4268 struct ctdb_db_context
*ctdb_db
;
4269 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4270 struct ctdb_transaction_handle
*h
;
4277 talloc_free(tmp_ctx
);
4281 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
4282 talloc_free(tmp_ctx
);
4286 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
4288 DEBUG(DEBUG_ERR
, ("Database '%s' is not persistent\n", db_name
));
4289 talloc_free(tmp_ctx
);
4293 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4294 if (ctdb_db
== NULL
) {
4295 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n", db_name
));
4296 talloc_free(tmp_ctx
);
4300 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4302 DEBUG(DEBUG_ERR
, ("Failed to start transaction on database %s\n", db_name
));
4303 talloc_free(tmp_ctx
);
4307 key
.dptr
= discard_const(argv
[1]);
4308 key
.dsize
= strlen(argv
[1]);
4309 ret
= ctdb_transaction_store(h
, key
, tdb_null
);
4311 DEBUG(DEBUG_ERR
, ("Failed to delete record\n"));
4312 talloc_free(tmp_ctx
);
4316 ret
= ctdb_transaction_commit(h
);
4318 DEBUG(DEBUG_ERR
, ("Failed to commit transaction\n"));
4319 talloc_free(tmp_ctx
);
4323 talloc_free(tmp_ctx
);
4327 static const char *ptrans_parse_string(TALLOC_CTX
*mem_ctx
, const char *s
,
4332 const char *ret
; /* Next byte after successfully parsed value */
4334 /* Error, unless someone says otherwise */
4336 /* Indicates no value to parse */
4339 /* Skip whitespace */
4340 n
= strspn(s
, " \t");
4344 /* Quoted ASCII string - no wide characters! */
4346 n
= strcspn(t
, "\"");
4350 data
->dptr
= talloc_memdup(mem_ctx
, t
, n
);
4351 CTDB_NOMEM_ABORT(data
->dptr
);
4355 DEBUG(DEBUG_WARNING
,("Unmatched \" in input %s\n", s
));
4358 DEBUG(DEBUG_WARNING
,("Unsupported input format in %s\n", s
));
4364 static bool ptrans_get_key_value(TALLOC_CTX
*mem_ctx
, FILE *file
,
4365 TDB_DATA
*key
, TDB_DATA
*value
)
4367 char line
[1024]; /* FIXME: make this more flexible? */
4371 ptr
= fgets(line
, sizeof(line
), file
);
4378 t
= ptrans_parse_string(mem_ctx
, line
, key
);
4379 if (t
== NULL
|| key
->dptr
== NULL
) {
4380 /* Line Ignored but not EOF */
4385 t
= ptrans_parse_string(mem_ctx
, t
, value
);
4387 /* Line Ignored but not EOF */
4388 talloc_free(key
->dptr
);
4397 * Update a persistent database as per file/stdin
4399 static int control_ptrans(struct ctdb_context
*ctdb
,
4400 int argc
, const char **argv
)
4402 const char *db_name
;
4403 struct ctdb_db_context
*ctdb_db
;
4404 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4405 struct ctdb_transaction_handle
*h
;
4406 TDB_DATA key
, value
;
4411 talloc_free(tmp_ctx
);
4417 file
= fopen(argv
[1], "r");
4419 DEBUG(DEBUG_ERR
,("Unable to open file for reading '%s'\n", argv
[1]));
4420 talloc_free(tmp_ctx
);
4427 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4428 if (ctdb_db
== NULL
) {
4429 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4433 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4435 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4439 while (ptrans_get_key_value(tmp_ctx
, file
, &key
, &value
)) {
4440 if (key
.dsize
!= 0) {
4441 ret
= ctdb_transaction_store(h
, key
, value
);
4442 /* Minimise memory use */
4443 talloc_free(key
.dptr
);
4444 if (value
.dptr
!= NULL
) {
4445 talloc_free(value
.dptr
);
4448 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4449 ctdb_transaction_cancel(h
);
4455 ret
= ctdb_transaction_commit(h
);
4457 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4461 if (file
!= stdin
) {
4464 talloc_free(tmp_ctx
);
4468 if (file
!= stdin
) {
4472 talloc_free(tmp_ctx
);
4477 check if a service is bound to a port or not
4479 static int control_chktcpport(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4484 struct sockaddr_in sin
;
4487 printf("Use: ctdb chktcport <port>\n");
4491 port
= atoi(argv
[0]);
4493 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
4495 printf("Failed to open local socket\n");
4499 v
= fcntl(s
, F_GETFL
, 0);
4500 if (v
== -1 || fcntl(s
, F_SETFL
, v
| O_NONBLOCK
) != 0) {
4501 printf("Unable to set socket non-blocking: %s\n", strerror(errno
));
4504 bzero(&sin
, sizeof(sin
));
4505 sin
.sin_family
= PF_INET
;
4506 sin
.sin_port
= htons(port
);
4507 ret
= bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
));
4510 printf("Failed to bind to local socket: %d %s\n", errno
, strerror(errno
));
4518 /* Reload public IPs on a specified nodes */
4519 static int control_reloadips(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4521 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4527 assert_single_node_only();
4533 /* Determine the nodes where IPs need to be reloaded */
4534 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
4535 options
.pnn
, true, &nodes
, &pnn_mode
)) {
4541 /* Disable takeover runs on all connected nodes. A reply
4542 * indicating success is needed from each node so all nodes
4543 * will need to be active. This will retry until maxruntime
4544 * is exceeded, hence no error handling.
4546 * A check could be added to not allow reloading of IPs when
4547 * there are disconnected nodes. However, this should
4548 * probably be left up to the administrator.
4550 timeout
= LONGTIMEOUT
;
4551 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4552 "Disable takeover runs", true);
4554 /* Now tell all the desired nodes to reload their public IPs.
4555 * Keep trying this until it succeeds. This assumes all
4556 * failures are transient, which might not be true...
4558 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELOAD_PUBLIC_IPS
,
4559 nodes
, 0, LONGTIMELIMIT(),
4561 NULL
, NULL
, NULL
) != 0) {
4563 ("Unable to reload IPs on some nodes, try again.\n"));
4567 /* It isn't strictly necessary to wait until takeover runs are
4568 * re-enabled but doing so can't hurt.
4571 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4572 "Enable takeover runs", true);
4578 talloc_free(tmp_ctx
);
4583 display a list of the databases on a remote ctdb
4585 static int control_getdbmap(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4588 struct ctdb_dbid_map
*dbmap
=NULL
;
4590 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &dbmap
);
4592 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
4596 if(options
.machinereadable
){
4597 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4598 for(i
=0;i
<dbmap
->num
;i
++){
4606 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
,
4607 dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4608 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
,
4609 dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4610 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
4611 dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4612 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4613 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4614 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4615 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4616 dbmap
->dbs
[i
].dbid
, name
, path
,
4617 !!(persistent
), !!(sticky
),
4618 !!(health
), !!(readonly
));
4623 printf("Number of databases:%d\n", dbmap
->num
);
4624 for(i
=0;i
<dbmap
->num
;i
++){
4632 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4633 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4634 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4635 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4636 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4637 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4638 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4639 dbmap
->dbs
[i
].dbid
, name
, path
,
4640 persistent
?" PERSISTENT":"",
4641 sticky
?" STICKY":"",
4642 readonly
?" READONLY":"",
4643 health
?" UNHEALTHY":"");
4650 display the status of a database on a remote ctdb
4652 static int control_getdbstatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4654 const char *db_name
;
4664 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
4668 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &path
);
4669 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &health
);
4670 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4671 db_id
, db_name
, path
,
4672 (flags
& CTDB_DB_FLAGS_PERSISTENT
? "yes" : "no"),
4673 (flags
& CTDB_DB_FLAGS_STICKY
? "yes" : "no"),
4674 (flags
& CTDB_DB_FLAGS_READONLY
? "yes" : "no"),
4675 (health
? health
: "OK"));
4681 check if the local node is recmaster or not
4682 it will return 1 if this node is the recmaster and 0 if it is not
4683 or if the local ctdb daemon could not be contacted
4685 static int control_isnotrecmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4687 uint32_t mypnn
, recmaster
;
4690 assert_single_node_only();
4692 mypnn
= getpnn(ctdb
);
4694 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
4696 printf("Failed to get the recmaster\n");
4700 if (recmaster
!= mypnn
) {
4701 printf("this node is not the recmaster\n");
4705 printf("this node is the recmaster\n");
4712 static int control_ping(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4715 struct timeval tv
= timeval_current();
4716 ret
= ctdb_ctrl_ping(ctdb
, options
.pnn
);
4718 printf("Unable to get ping response from node %u\n", options
.pnn
);
4721 printf("response from %u time=%.6f sec (%d clients)\n",
4722 options
.pnn
, timeval_elapsed(&tv
), ret
);
4729 get a node's runstate
4731 static int control_runstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4734 enum ctdb_runstate runstate
;
4736 ret
= ctdb_ctrl_get_runstate(ctdb
, TIMELIMIT(), options
.pnn
, &runstate
);
4738 printf("Unable to get runstate response from node %u\n",
4743 enum ctdb_runstate t
;
4745 for (i
=0; i
<argc
; i
++) {
4747 t
= runstate_from_string(argv
[i
]);
4748 if (t
== CTDB_RUNSTATE_UNKNOWN
) {
4749 printf("Invalid run state (%s)\n", argv
[i
]);
4753 if (t
== runstate
) {
4760 printf("CTDB not in required run state (got %s)\n",
4761 runstate_to_string((enum ctdb_runstate
)runstate
));
4766 printf("%s\n", runstate_to_string(runstate
));
4774 static int control_getvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4785 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, &value
);
4787 DEBUG(DEBUG_ERR
, ("Unable to get tunable variable '%s'\n", name
));
4791 printf("%-23s = %u\n", name
, value
);
4798 static int control_setvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4809 value
= strtoul(argv
[1], NULL
, 0);
4811 ret
= ctdb_ctrl_set_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, value
);
4813 DEBUG(DEBUG_ERR
, ("Unable to set tunable variable '%s'\n", name
));
4822 static int control_listvars(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4828 ret
= ctdb_ctrl_list_tunables(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &list
, &count
);
4830 DEBUG(DEBUG_ERR
, ("Unable to list tunable variables\n"));
4834 for (i
=0;i
<count
;i
++) {
4835 control_getvar(ctdb
, 1, &list
[i
]);
4844 display debug level on a node
4846 static int control_getdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4851 ret
= ctdb_ctrl_get_debuglevel(ctdb
, options
.pnn
, &level
);
4853 DEBUG(DEBUG_ERR
, ("Unable to get debuglevel response from node %u\n", options
.pnn
));
4856 const char *desc
= get_debug_by_level(level
);
4858 /* This should never happen */
4861 if (options
.machinereadable
){
4862 printf(":Name:Level:\n");
4863 printf(":%s:%d:\n", desc
, level
);
4865 printf("Node %u is at debug level %s (%d)\n",
4866 options
.pnn
, desc
, level
);
4873 display reclock file of a node
4875 static int control_getreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4878 const char *reclock
;
4880 ret
= ctdb_ctrl_getreclock(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &reclock
);
4882 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4885 if (options
.machinereadable
){
4886 if (reclock
!= NULL
) {
4887 printf("%s", reclock
);
4890 if (reclock
== NULL
) {
4891 printf("No reclock file used.\n");
4893 printf("Reclock file:%s\n", reclock
);
4901 set the reclock file of a node
4903 static int control_setreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4906 const char *reclock
;
4910 } else if (argc
== 1) {
4916 ret
= ctdb_ctrl_setreclock(ctdb
, TIMELIMIT(), options
.pnn
, reclock
);
4918 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4925 set the natgw state on/off
4927 static int control_setnatgwstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4930 uint32_t natgwstate
;
4936 if (!strcmp(argv
[0], "on")) {
4938 } else if (!strcmp(argv
[0], "off")) {
4944 ret
= ctdb_ctrl_setnatgwstate(ctdb
, TIMELIMIT(), options
.pnn
, natgwstate
);
4946 DEBUG(DEBUG_ERR
, ("Unable to set the natgw state for node %u\n", options
.pnn
));
4954 set the lmaster role on/off
4956 static int control_setlmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4959 uint32_t lmasterrole
;
4965 if (!strcmp(argv
[0], "on")) {
4967 } else if (!strcmp(argv
[0], "off")) {
4973 ret
= ctdb_ctrl_setlmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, lmasterrole
);
4975 DEBUG(DEBUG_ERR
, ("Unable to set the lmaster role for node %u\n", options
.pnn
));
4983 set the recmaster role on/off
4985 static int control_setrecmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4988 uint32_t recmasterrole
;
4994 if (!strcmp(argv
[0], "on")) {
4996 } else if (!strcmp(argv
[0], "off")) {
5002 ret
= ctdb_ctrl_setrecmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, recmasterrole
);
5004 DEBUG(DEBUG_ERR
, ("Unable to set the recmaster role for node %u\n", options
.pnn
));
5012 set debug level on a node or all nodes
5014 static int control_setdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5020 printf("You must specify the debug level. Valid levels are:\n");
5021 print_debug_levels(stdout
);
5025 if (!parse_debug(argv
[0], &level
)) {
5026 printf("Invalid debug level, must be one of\n");
5027 print_debug_levels(stdout
);
5031 ret
= ctdb_ctrl_set_debuglevel(ctdb
, options
.pnn
, level
);
5033 DEBUG(DEBUG_ERR
, ("Unable to set debug level on node %u\n", options
.pnn
));
5042 static int control_thaw(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5048 priority
= strtol(argv
[0], NULL
, 0);
5052 DEBUG(DEBUG_ERR
,("Thaw by priority %u\n", priority
));
5054 ret
= ctdb_ctrl_thaw_priority(ctdb
, TIMELIMIT(), options
.pnn
, priority
);
5056 DEBUG(DEBUG_ERR
, ("Unable to thaw node %u\n", options
.pnn
));
5063 attach to a database
5065 static int control_attach(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5067 const char *db_name
;
5068 struct ctdb_db_context
*ctdb_db
;
5069 bool persistent
= false;
5079 if (strcmp(argv
[1], "persistent") != 0) {
5085 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
5086 if (ctdb_db
== NULL
) {
5087 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
5095 * detach from a database
5097 static int control_detach(struct ctdb_context
*ctdb
, int argc
,
5102 int ret
, i
, status
= 0;
5103 struct ctdb_node_map
*nodemap
= NULL
;
5104 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5111 assert_single_node_only();
5113 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
,
5116 DEBUG(DEBUG_ERR
, ("Database cannot be detached "
5117 "when recovery is active\n"));
5118 talloc_free(tmp_ctx
);
5122 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5125 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5127 talloc_free(tmp_ctx
);
5131 for (i
=0; i
<nodemap
->num
; i
++) {
5134 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
5138 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
5142 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
5143 DEBUG(DEBUG_ERR
, ("Database cannot be detached on "
5144 "inactive (stopped or banned) node "
5145 "%u\n", nodemap
->nodes
[i
].pnn
));
5146 talloc_free(tmp_ctx
);
5150 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(),
5151 nodemap
->nodes
[i
].pnn
,
5152 "AllowClientDBAttach",
5155 DEBUG(DEBUG_ERR
, ("Unable to get tunable "
5156 "AllowClientDBAttach from node %u\n",
5157 nodemap
->nodes
[i
].pnn
));
5158 talloc_free(tmp_ctx
);
5163 DEBUG(DEBUG_ERR
, ("Database access is still active on "
5164 "node %u. Set AllowClientDBAttach=0 "
5166 nodemap
->nodes
[i
].pnn
));
5167 talloc_free(tmp_ctx
);
5172 talloc_free(tmp_ctx
);
5174 for (i
=0; i
<argc
; i
++) {
5175 if (!db_exists(ctdb
, argv
[i
], &db_id
, NULL
, &flags
)) {
5179 if (flags
& CTDB_DB_FLAGS_PERSISTENT
) {
5180 DEBUG(DEBUG_ERR
, ("Persistent database '%s' "
5181 "cannot be detached\n", argv
[i
]));
5186 ret
= ctdb_detach(ctdb
, db_id
);
5188 DEBUG(DEBUG_ERR
, ("Database '%s' detach failed\n",
5200 static int control_setdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5202 struct ctdb_db_priority db_prio
;
5209 db_prio
.db_id
= strtoul(argv
[0], NULL
, 0);
5210 db_prio
.priority
= strtoul(argv
[1], NULL
, 0);
5212 ret
= ctdb_ctrl_set_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, &db_prio
);
5214 DEBUG(DEBUG_ERR
,("Unable to set db prio\n"));
5224 static int control_getdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5226 uint32_t db_id
, priority
;
5233 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5237 ret
= ctdb_ctrl_get_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &priority
);
5239 DEBUG(DEBUG_ERR
,("Unable to get db prio\n"));
5243 DEBUG(DEBUG_ERR
,("Priority:%u\n", priority
));
5249 set the sticky records capability for a database
5251 static int control_setdbsticky(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5253 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5261 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5265 ret
= ctdb_ctrl_set_db_sticky(ctdb
, options
.pnn
, db_id
);
5267 DEBUG(DEBUG_ERR
,("Unable to set db to support sticky records\n"));
5268 talloc_free(tmp_ctx
);
5272 talloc_free(tmp_ctx
);
5277 set the readonly capability for a database
5279 static int control_setdbreadonly(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5281 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5289 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5293 ret
= ctdb_ctrl_set_db_readonly(ctdb
, options
.pnn
, db_id
);
5295 DEBUG(DEBUG_ERR
,("Unable to set db to support readonly\n"));
5296 talloc_free(tmp_ctx
);
5300 talloc_free(tmp_ctx
);
5307 static int control_getdbseqnum(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5317 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5321 ret
= ctdb_ctrl_getdbseqnum(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &seqnum
);
5323 DEBUG(DEBUG_ERR
, ("Unable to get seqnum from node."));
5327 printf("Sequence number:%lld\n", (long long)seqnum
);
5333 run an eventscript on a node
5335 static int control_eventscript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5341 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5344 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5348 data
.dptr
= (unsigned char *)discard_const(argv
[0]);
5349 data
.dsize
= strlen((char *)data
.dptr
) + 1;
5351 DEBUG(DEBUG_ERR
, ("Running eventscripts with arguments \"%s\" on node %u\n", data
.dptr
, options
.pnn
));
5353 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS
,
5354 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
5355 if (ret
!= 0 || res
!= 0) {
5356 DEBUG(DEBUG_ERR
,("Failed to run eventscripts - %s\n", errmsg
));
5357 talloc_free(tmp_ctx
);
5360 talloc_free(tmp_ctx
);
5364 #define DB_VERSION 1
5365 #define MAX_DB_NAME 64
5366 struct db_file_header
{
5367 unsigned long version
;
5369 unsigned long persistent
;
5371 const char name
[MAX_DB_NAME
];
5374 struct backup_data
{
5375 struct ctdb_marshall_buffer
*records
;
5378 bool traverse_error
;
5381 static int backup_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private)
5383 struct backup_data
*bd
= talloc_get_type(private, struct backup_data
);
5384 struct ctdb_rec_data
*rec
;
5386 /* add the record */
5387 rec
= ctdb_marshall_record(bd
->records
, 0, key
, NULL
, data
);
5389 bd
->traverse_error
= true;
5390 DEBUG(DEBUG_ERR
,("Failed to marshall record\n"));
5393 bd
->records
= talloc_realloc_size(NULL
, bd
->records
, rec
->length
+ bd
->len
);
5394 if (bd
->records
== NULL
) {
5395 DEBUG(DEBUG_ERR
,("Failed to expand marshalling buffer\n"));
5396 bd
->traverse_error
= true;
5399 bd
->records
->count
++;
5400 memcpy(bd
->len
+(uint8_t *)bd
->records
, rec
, rec
->length
);
5401 bd
->len
+= rec
->length
;
5409 * backup a database to a file
5411 static int control_backupdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5413 const char *db_name
;
5415 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5416 struct db_file_header dbhdr
;
5417 struct ctdb_db_context
*ctdb_db
;
5418 struct backup_data
*bd
;
5421 const char *reason
= NULL
;
5425 assert_single_node_only();
5428 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5432 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
5436 ret
= ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
5437 db_id
, tmp_ctx
, &reason
);
5439 DEBUG(DEBUG_ERR
,("Unable to get dbhealth for database '%s'\n",
5441 talloc_free(tmp_ctx
);
5445 uint32_t allow_unhealthy
= 0;
5447 ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
,
5448 "AllowUnhealthyDBRead",
5451 if (allow_unhealthy
!= 1) {
5452 DEBUG(DEBUG_ERR
,("database '%s' is unhealthy: %s\n",
5455 DEBUG(DEBUG_ERR
,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5457 talloc_free(tmp_ctx
);
5461 DEBUG(DEBUG_WARNING
,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5463 DEBUG(DEBUG_WARNING
,("WARNING! allow backup of unhealthy database: "
5464 "tunnable AllowUnhealthyDBRead = %u\n",
5468 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5469 if (ctdb_db
== NULL
) {
5470 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", argv
[0]));
5471 talloc_free(tmp_ctx
);
5476 ret
= tdb_transaction_start(ctdb_db
->ltdb
->tdb
);
5478 DEBUG(DEBUG_ERR
,("Failed to start transaction\n"));
5479 talloc_free(tmp_ctx
);
5484 bd
= talloc_zero(tmp_ctx
, struct backup_data
);
5486 DEBUG(DEBUG_ERR
,("Failed to allocate backup_data\n"));
5487 talloc_free(tmp_ctx
);
5491 bd
->records
= talloc_zero(bd
, struct ctdb_marshall_buffer
);
5492 if (bd
->records
== NULL
) {
5493 DEBUG(DEBUG_ERR
,("Failed to allocate ctdb_marshall_buffer\n"));
5494 talloc_free(tmp_ctx
);
5498 bd
->len
= offsetof(struct ctdb_marshall_buffer
, data
);
5499 bd
->records
->db_id
= ctdb_db
->db_id
;
5500 /* traverse the database collecting all records */
5501 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, backup_traverse
, bd
) == -1 ||
5502 bd
->traverse_error
) {
5503 DEBUG(DEBUG_ERR
,("Traverse error\n"));
5504 talloc_free(tmp_ctx
);
5508 tdb_transaction_cancel(ctdb_db
->ltdb
->tdb
);
5511 fh
= open(argv
[1], O_RDWR
|O_CREAT
, 0600);
5513 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[1]));
5514 talloc_free(tmp_ctx
);
5519 dbhdr
.version
= DB_VERSION
;
5520 dbhdr
.timestamp
= time(NULL
);
5521 dbhdr
.persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
5522 dbhdr
.size
= bd
->len
;
5523 if (strlen(argv
[0]) >= MAX_DB_NAME
) {
5524 DEBUG(DEBUG_ERR
,("Too long dbname\n"));
5527 strncpy(discard_const(dbhdr
.name
), argv
[0], MAX_DB_NAME
-1);
5528 ret
= sys_write(fh
, &dbhdr
, sizeof(dbhdr
));
5530 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5533 ret
= sys_write(fh
, bd
->records
, bd
->len
);
5535 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5544 DEBUG(DEBUG_ERR
,("close failed: %s\n", strerror(errno
)));
5548 DEBUG(DEBUG_ERR
,("Database backed up to %s\n", argv
[1]));
5550 talloc_free(tmp_ctx
);
5555 * restore a database from a file
5557 static int control_restoredb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5560 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5563 struct db_file_header dbhdr
;
5564 struct ctdb_db_context
*ctdb_db
;
5565 struct ctdb_node_map
*nodemap
=NULL
;
5566 struct ctdb_vnn_map
*vnnmap
=NULL
;
5568 struct ctdb_control_wipe_database w
;
5570 uint32_t generation
;
5575 assert_single_node_only();
5577 if (argc
< 1 || argc
> 2) {
5578 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5582 fh
= open(argv
[0], O_RDONLY
);
5584 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5585 talloc_free(tmp_ctx
);
5589 sys_read(fh
, &dbhdr
, sizeof(dbhdr
));
5590 if (dbhdr
.version
!= DB_VERSION
) {
5591 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5593 talloc_free(tmp_ctx
);
5597 dbname
= discard_const(dbhdr
.name
);
5599 dbname
= discard_const(argv
[1]);
5602 outdata
.dsize
= dbhdr
.size
;
5603 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5604 if (outdata
.dptr
== NULL
) {
5605 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5607 talloc_free(tmp_ctx
);
5610 sys_read(fh
, outdata
.dptr
, outdata
.dsize
);
5613 tm
= localtime(&dbhdr
.timestamp
);
5614 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5615 printf("Restoring database '%s' from backup @ %s\n",
5619 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), dbname
, dbhdr
.persistent
, 0);
5620 if (ctdb_db
== NULL
) {
5621 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", dbname
));
5622 talloc_free(tmp_ctx
);
5626 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &nodemap
);
5628 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
5629 talloc_free(tmp_ctx
);
5634 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
5636 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
5637 talloc_free(tmp_ctx
);
5641 /* freeze all nodes */
5642 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5643 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5644 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5650 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5651 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5652 talloc_free(tmp_ctx
);
5657 generation
= vnnmap
->generation
;
5658 data
.dptr
= (void *)&generation
;
5659 data
.dsize
= sizeof(generation
);
5661 /* start a cluster wide transaction */
5662 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5663 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5665 TIMELIMIT(), false, data
,
5668 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide transactions.\n"));
5673 w
.db_id
= ctdb_db
->db_id
;
5674 w
.transaction_id
= generation
;
5676 data
.dptr
= (void *)&w
;
5677 data
.dsize
= sizeof(w
);
5679 /* wipe all the remote databases. */
5680 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5681 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5683 TIMELIMIT(), false, data
,
5686 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5687 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5688 talloc_free(tmp_ctx
);
5692 /* push the database */
5693 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5694 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_PUSH_DB
,
5696 TIMELIMIT(), false, outdata
,
5699 DEBUG(DEBUG_ERR
, ("Failed to push database.\n"));
5700 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5701 talloc_free(tmp_ctx
);
5705 data
.dptr
= (void *)&ctdb_db
->db_id
;
5706 data
.dsize
= sizeof(ctdb_db
->db_id
);
5708 /* mark the database as healthy */
5709 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5710 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5712 TIMELIMIT(), false, data
,
5715 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5716 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5717 talloc_free(tmp_ctx
);
5721 data
.dptr
= (void *)&generation
;
5722 data
.dsize
= sizeof(generation
);
5724 /* commit all the changes */
5725 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5727 TIMELIMIT(), false, data
,
5730 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5731 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5732 talloc_free(tmp_ctx
);
5737 /* thaw all nodes */
5738 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5739 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5745 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5746 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5747 talloc_free(tmp_ctx
);
5752 talloc_free(tmp_ctx
);
5757 * dump a database backup from a file
5759 static int control_dumpdbbackup(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5761 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5763 struct db_file_header dbhdr
;
5767 struct ctdb_rec_data
*rec
= NULL
;
5768 struct ctdb_marshall_buffer
*m
;
5769 struct ctdb_dump_db_context c
;
5771 assert_single_node_only();
5774 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5778 fh
= open(argv
[0], O_RDONLY
);
5780 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5781 talloc_free(tmp_ctx
);
5785 sys_read(fh
, &dbhdr
, sizeof(dbhdr
));
5786 if (dbhdr
.version
!= DB_VERSION
) {
5787 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5789 talloc_free(tmp_ctx
);
5793 outdata
.dsize
= dbhdr
.size
;
5794 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5795 if (outdata
.dptr
== NULL
) {
5796 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5798 talloc_free(tmp_ctx
);
5801 sys_read(fh
, outdata
.dptr
, outdata
.dsize
);
5803 m
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
5805 tm
= localtime(&dbhdr
.timestamp
);
5806 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5807 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5808 dbhdr
.name
, m
->db_id
, tbuf
);
5812 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
5813 c
.printdatasize
= (bool)options
.printdatasize
;
5814 c
.printlmaster
= false;
5815 c
.printhash
= (bool)options
.printhash
;
5816 c
.printrecordflags
= (bool)options
.printrecordflags
;
5818 for (i
=0; i
< m
->count
; i
++) {
5822 /* we do not want the header splitted, so we pass NULL*/
5823 rec
= ctdb_marshall_loop_next(m
, rec
, &reqid
,
5826 ctdb_dumpdb_record(ctdb
, key
, data
, &c
);
5829 printf("Dumped %d records\n", i
);
5830 talloc_free(tmp_ctx
);
5835 * wipe a database from a file
5837 static int control_wipedb(struct ctdb_context
*ctdb
, int argc
,
5840 const char *db_name
;
5842 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5844 struct ctdb_db_context
*ctdb_db
;
5845 struct ctdb_node_map
*nodemap
= NULL
;
5846 struct ctdb_vnn_map
*vnnmap
= NULL
;
5848 struct ctdb_control_wipe_database w
;
5850 uint32_t generation
;
5853 assert_single_node_only();
5856 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5860 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
5864 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5865 if (ctdb_db
== NULL
) {
5866 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n",
5868 talloc_free(tmp_ctx
);
5872 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
,
5875 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5877 talloc_free(tmp_ctx
);
5881 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5884 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
5886 talloc_free(tmp_ctx
);
5890 /* freeze all nodes */
5891 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5892 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5893 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5900 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5901 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
,
5902 CTDB_RECOVERY_ACTIVE
);
5903 talloc_free(tmp_ctx
);
5908 generation
= vnnmap
->generation
;
5909 data
.dptr
= (void *)&generation
;
5910 data
.dsize
= sizeof(generation
);
5912 /* start a cluster wide transaction */
5913 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5914 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5916 TIMELIMIT(), false, data
,
5920 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide "
5921 "transactions.\n"));
5925 w
.db_id
= ctdb_db
->db_id
;
5926 w
.transaction_id
= generation
;
5928 data
.dptr
= (void *)&w
;
5929 data
.dsize
= sizeof(w
);
5931 /* wipe all the remote databases. */
5932 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5933 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5935 TIMELIMIT(), false, data
,
5938 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5939 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5940 talloc_free(tmp_ctx
);
5944 data
.dptr
= (void *)&ctdb_db
->db_id
;
5945 data
.dsize
= sizeof(ctdb_db
->db_id
);
5947 /* mark the database as healthy */
5948 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5949 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5951 TIMELIMIT(), false, data
,
5954 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5955 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5956 talloc_free(tmp_ctx
);
5960 data
.dptr
= (void *)&generation
;
5961 data
.dsize
= sizeof(generation
);
5963 /* commit all the changes */
5964 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5966 TIMELIMIT(), false, data
,
5969 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5970 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5971 talloc_free(tmp_ctx
);
5975 /* thaw all nodes */
5976 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5977 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5983 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5984 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5985 talloc_free(tmp_ctx
);
5989 DEBUG(DEBUG_ERR
, ("Database wiped.\n"));
5991 talloc_free(tmp_ctx
);
5998 static int control_dumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6004 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
6005 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_DUMP_MEMORY
,
6006 0, tdb_null
, tmp_ctx
, &data
, &res
, NULL
, &errmsg
);
6007 if (ret
!= 0 || res
!= 0) {
6008 DEBUG(DEBUG_ERR
,("Failed to dump memory - %s\n", errmsg
));
6009 talloc_free(tmp_ctx
);
6012 sys_write(1, data
.dptr
, data
.dsize
);
6013 talloc_free(tmp_ctx
);
6018 handler for memory dumps
6020 static void mem_dump_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6021 TDB_DATA data
, void *private_data
)
6023 sys_write(1, data
.dptr
, data
.dsize
);
6028 dump memory usage on the recovery daemon
6030 static int control_rddumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6034 struct srvid_request rd
;
6036 rd
.pnn
= ctdb_get_pnn(ctdb
);
6037 rd
.srvid
= getpid();
6039 /* register a message port for receiveing the reply so that we
6040 can receive the reply
6042 ctdb_client_set_message_handler(ctdb
, rd
.srvid
, mem_dump_handler
, NULL
);
6045 data
.dptr
= (uint8_t *)&rd
;
6046 data
.dsize
= sizeof(rd
);
6048 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_MEM_DUMP
, data
);
6050 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6054 /* this loop will terminate when we have received the reply */
6056 event_loop_once(ctdb
->ev
);
6063 send a message to a srvid
6065 static int control_msgsend(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6067 unsigned long srvid
;
6075 srvid
= strtoul(argv
[0], NULL
, 0);
6077 data
.dptr
= (uint8_t *)discard_const(argv
[1]);
6078 data
.dsize
= strlen(argv
[1]);
6080 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, srvid
, data
);
6082 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6090 handler for msglisten
6092 static void msglisten_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6093 TDB_DATA data
, void *private_data
)
6097 printf("Message received: ");
6098 for (i
=0;i
<data
.dsize
;i
++) {
6099 printf("%c", data
.dptr
[i
]);
6105 listen for messages on a messageport
6107 static int control_msglisten(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6113 /* register a message port and listen for messages
6115 ctdb_client_set_message_handler(ctdb
, srvid
, msglisten_handler
, NULL
);
6116 printf("Listening for messages on srvid:%d\n", (int)srvid
);
6119 event_loop_once(ctdb
->ev
);
6126 list all nodes in the cluster
6127 we parse the nodes file directly
6129 static int control_listnodes(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6131 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
6132 struct pnn_node
*pnn_nodes
;
6133 struct pnn_node
*pnn_node
;
6135 assert_single_node_only();
6137 pnn_nodes
= read_nodes_file(mem_ctx
);
6138 if (pnn_nodes
== NULL
) {
6139 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
6140 talloc_free(mem_ctx
);
6144 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
6145 const char *addr
= ctdb_addr_to_str(&pnn_node
->addr
);
6146 if (options
.machinereadable
){
6147 printf(":%d:%s:\n", pnn_node
->pnn
, addr
);
6149 printf("%s\n", addr
);
6152 talloc_free(mem_ctx
);
6158 reload the nodes file on the local node
6160 static int control_reload_nodes_file(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6164 struct ctdb_node_map
*nodemap
=NULL
;
6166 assert_single_node_only();
6168 mypnn
= ctdb_get_pnn(ctdb
);
6170 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
6172 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
6176 /* reload the nodes file on all remote nodes */
6177 for (i
=0;i
<nodemap
->num
;i
++) {
6178 if (nodemap
->nodes
[i
].pnn
== mypnn
) {
6181 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", nodemap
->nodes
[i
].pnn
));
6182 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(),
6183 nodemap
->nodes
[i
].pnn
);
6185 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap
->nodes
[i
].pnn
));
6189 /* reload the nodes file on the local node */
6190 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", mypnn
));
6191 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(), mypnn
);
6193 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn
));
6196 /* initiate a recovery */
6197 control_recover(ctdb
, argc
, argv
);
6203 static const struct {
6205 int (*fn
)(struct ctdb_context
*, int, const char **);
6207 bool without_daemon
; /* can be run without daemon running ? */
6210 } ctdb_commands
[] = {
6211 { "version", control_version
, true, true, "show version of ctdb" },
6212 { "status", control_status
, true, false, "show node status" },
6213 { "uptime", control_uptime
, true, false, "show node uptime" },
6214 { "ping", control_ping
, true, false, "ping all nodes" },
6215 { "runstate", control_runstate
, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6216 { "getvar", control_getvar
, true, false, "get a tunable variable", "<name>"},
6217 { "setvar", control_setvar
, true, false, "set a tunable variable", "<name> <value>"},
6218 { "listvars", control_listvars
, true, false, "list tunable variables"},
6219 { "statistics", control_statistics
, false, false, "show statistics" },
6220 { "statisticsreset", control_statistics_reset
, true, false, "reset statistics"},
6221 { "stats", control_stats
, false, false, "show rolling statistics", "[number of history records]" },
6222 { "ip", control_ip
, false, false, "show which public ip's that ctdb manages" },
6223 { "ipinfo", control_ipinfo
, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6224 { "ifaces", control_ifaces
, true, false, "show which interfaces that ctdb manages" },
6225 { "setifacelink", control_setifacelink
, true, false, "set interface link status", "<iface> <status>" },
6226 { "process-exists", control_process_exists
, true, false, "check if a process exists on a node", "<pid>"},
6227 { "getdbmap", control_getdbmap
, true, false, "show the database map" },
6228 { "getdbstatus", control_getdbstatus
, true, false, "show the status of a database", "<dbname|dbid>" },
6229 { "catdb", control_catdb
, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6230 { "cattdb", control_cattdb
, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6231 { "getmonmode", control_getmonmode
, true, false, "show monitoring mode" },
6232 { "getcapabilities", control_getcapabilities
, true, false, "show node capabilities" },
6233 { "pnn", control_pnn
, true, false, "show the pnn of the currnet node" },
6234 { "lvs", control_lvs
, true, false, "show lvs configuration" },
6235 { "lvsmaster", control_lvsmaster
, true, false, "show which node is the lvs master" },
6236 { "disablemonitor", control_disable_monmode
,true, false, "set monitoring mode to DISABLE" },
6237 { "enablemonitor", control_enable_monmode
, true, false, "set monitoring mode to ACTIVE" },
6238 { "setdebug", control_setdebug
, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6239 { "getdebug", control_getdebug
, true, false, "get debug level" },
6240 { "attach", control_attach
, true, false, "attach to a database", "<dbname> [persistent]" },
6241 { "detach", control_detach
, false, false, "detach from a database", "<dbname|dbid> [<dbname|dbid> ...]" },
6242 { "dumpmemory", control_dumpmemory
, true, false, "dump memory map to stdout" },
6243 { "rddumpmemory", control_rddumpmemory
, true, false, "dump memory map from the recovery daemon to stdout" },
6244 { "getpid", control_getpid
, true, false, "get ctdbd process ID" },
6245 { "disable", control_disable
, true, false, "disable a nodes public IP" },
6246 { "enable", control_enable
, true, false, "enable a nodes public IP" },
6247 { "stop", control_stop
, true, false, "stop a node" },
6248 { "continue", control_continue
, true, false, "re-start a stopped node" },
6249 { "ban", control_ban
, true, false, "ban a node from the cluster", "<bantime>"},
6250 { "unban", control_unban
, true, false, "unban a node" },
6251 { "showban", control_showban
, true, false, "show ban information"},
6252 { "shutdown", control_shutdown
, true, false, "shutdown ctdbd" },
6253 { "recover", control_recover
, true, false, "force recovery" },
6254 { "sync", control_ipreallocate
, false, false, "wait until ctdbd has synced all state changes" },
6255 { "ipreallocate", control_ipreallocate
, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6256 { "thaw", control_thaw
, true, false, "thaw databases", "[priority:1-3]" },
6257 { "isnotrecmaster", control_isnotrecmaster
, false, false, "check if the local node is recmaster or not" },
6258 { "killtcp", kill_tcp
, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6259 { "gratiousarp", control_gratious_arp
, false, false, "send a gratious arp", "<ip> <interface>" },
6260 { "tickle", tickle_tcp
, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6261 { "gettickles", control_get_tickles
, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6262 { "addtickle", control_add_tickle
, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6264 { "deltickle", control_del_tickle
, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6266 { "regsrvid", regsrvid
, false, false, "register a server id", "<pnn> <type> <id>" },
6267 { "unregsrvid", unregsrvid
, false, false, "unregister a server id", "<pnn> <type> <id>" },
6268 { "chksrvid", chksrvid
, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6269 { "getsrvids", getsrvids
, false, false, "get a list of all server ids"},
6270 { "check_srvids", check_srvids
, false, false, "check if a srvid exists", "<id>+" },
6271 { "repack", ctdb_repack
, false, false, "repack all databases", "[max_freelist]"},
6272 { "listnodes", control_listnodes
, false, true, "list all nodes in the cluster"},
6273 { "reloadnodes", control_reload_nodes_file
, false, false, "reload the nodes file and restart the transport on all nodes"},
6274 { "moveip", control_moveip
, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6275 { "rebalanceip", control_rebalanceip
, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6276 { "addip", control_addip
, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6277 { "delip", control_delip
, false, false, "delete an ip address from a node", "<ip>"},
6278 { "eventscript", control_eventscript
, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6279 { "backupdb", control_backupdb
, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6280 { "restoredb", control_restoredb
, false, false, "restore the database from a file.", "<file> [dbname]"},
6281 { "dumpdbbackup", control_dumpdbbackup
, false, true, "dump database backup from a file.", "<file>"},
6282 { "wipedb", control_wipedb
, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6283 { "recmaster", control_recmaster
, true, false, "show the pnn for the recovery master."},
6284 { "scriptstatus", control_scriptstatus
, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6285 { "enablescript", control_enablescript
, true, false, "enable an eventscript", "<script>"},
6286 { "disablescript", control_disablescript
, true, false, "disable an eventscript", "<script>"},
6287 { "natgwlist", control_natgwlist
, true, false, "show the nodes belonging to this natgw configuration"},
6288 { "xpnn", control_xpnn
, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6289 { "getreclock", control_getreclock
, true, false, "Show the reclock file of a node"},
6290 { "setreclock", control_setreclock
, true, false, "Set/clear the reclock file of a node", "[filename]"},
6291 { "setnatgwstate", control_setnatgwstate
, false, false, "Set NATGW state to on/off", "{on|off}"},
6292 { "setlmasterrole", control_setlmasterrole
, false, false, "Set LMASTER role to on/off", "{on|off}"},
6293 { "setrecmasterrole", control_setrecmasterrole
, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6294 { "setdbprio", control_setdbprio
, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6295 { "getdbprio", control_getdbprio
, false, false, "Get DB priority", "<dbname|dbid>"},
6296 { "setdbreadonly", control_setdbreadonly
, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6297 { "setdbsticky", control_setdbsticky
, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6298 { "msglisten", control_msglisten
, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6299 { "msgsend", control_msgsend
, false, false, "Send a message to srvid", "<srvid> <message>"},
6300 { "pfetch", control_pfetch
, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6301 { "pstore", control_pstore
, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6302 { "pdelete", control_pdelete
, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6303 { "ptrans", control_ptrans
, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6304 { "tfetch", control_tfetch
, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6305 { "tstore", control_tstore
, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6306 { "readkey", control_readkey
, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6307 { "writekey", control_writekey
, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6308 { "checktcpport", control_chktcpport
, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6309 { "rebalancenode", control_rebalancenode
, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6310 { "getdbseqnum", control_getdbseqnum
, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6311 { "nodestatus", control_nodestatus
, true, false, "show and return node status", "[<pnn-list>]" },
6312 { "dbstatistics", control_dbstatistics
, false, false, "show db statistics", "<dbname|dbid>" },
6313 { "reloadips", control_reloadips
, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6314 { "ipiface", control_ipiface
, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6320 static void usage(void)
6324 "Usage: ctdb [options] <control>\n" \
6326 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6327 " -Y generate machinereadable output\n"
6328 " -v generate verbose output\n"
6329 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options
.timelimit
);
6330 printf("Controls:\n");
6331 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6332 printf(" %-15s %-27s %s\n",
6333 ctdb_commands
[i
].name
,
6334 ctdb_commands
[i
].args
?ctdb_commands
[i
].args
:"",
6335 ctdb_commands
[i
].msg
);
6341 static void ctdb_alarm(int sig
)
6343 printf("Maximum runtime exceeded - exiting\n");
6350 int main(int argc
, const char *argv
[])
6352 struct ctdb_context
*ctdb
;
6353 char *nodestring
= NULL
;
6354 struct poptOption popt_options
[] = {
6357 { "timelimit", 't', POPT_ARG_INT
, &options
.timelimit
, 0, "timelimit", "integer" },
6358 { "node", 'n', POPT_ARG_STRING
, &nodestring
, 0, "node", "integer|all" },
6359 { "machinereadable", 'Y', POPT_ARG_NONE
, &options
.machinereadable
, 0, "enable machinereadable output", NULL
},
6360 { "verbose", 'v', POPT_ARG_NONE
, &options
.verbose
, 0, "enable verbose output", NULL
},
6361 { "maxruntime", 'T', POPT_ARG_INT
, &options
.maxruntime
, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6362 { "print-emptyrecords", 0, POPT_ARG_NONE
, &options
.printemptyrecords
, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL
},
6363 { "print-datasize", 0, POPT_ARG_NONE
, &options
.printdatasize
, 0, "do not print record data when dumping databases, only the data size", NULL
},
6364 { "print-lmaster", 0, POPT_ARG_NONE
, &options
.printlmaster
, 0, "print the record's lmaster in catdb", NULL
},
6365 { "print-hash", 0, POPT_ARG_NONE
, &options
.printhash
, 0, "print the record's hash when dumping databases", NULL
},
6366 { "print-recordflags", 0, POPT_ARG_NONE
, &options
.printrecordflags
, 0, "print the record flags in catdb and dumpdbbackup", NULL
},
6370 const char **extra_argv
;
6374 struct event_context
*ev
;
6375 const char *control
;
6379 /* set some defaults */
6380 options
.maxruntime
= 0;
6381 options
.timelimit
= 10;
6382 options
.pnn
= CTDB_CURRENT_NODE
;
6384 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
6386 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6389 DEBUG(DEBUG_ERR
, ("Invalid option %s: %s\n",
6390 poptBadOption(pc
, 0), poptStrerror(opt
)));
6395 /* setup the remaining options for the main program to use */
6396 extra_argv
= poptGetArgs(pc
);
6399 while (extra_argv
[extra_argc
]) extra_argc
++;
6402 if (extra_argc
< 1) {
6406 if (options
.maxruntime
== 0) {
6407 const char *ctdb_timeout
;
6408 ctdb_timeout
= getenv("CTDB_TIMEOUT");
6409 if (ctdb_timeout
!= NULL
) {
6410 options
.maxruntime
= strtoul(ctdb_timeout
, NULL
, 0);
6412 /* default timeout is 120 seconds */
6413 options
.maxruntime
= 120;
6417 signal(SIGALRM
, ctdb_alarm
);
6418 alarm(options
.maxruntime
);
6420 control
= extra_argv
[0];
6422 /* Default value for CTDB_BASE - don't override */
6423 setenv("CTDB_BASE", CTDB_ETCDIR
, 0);
6425 ev
= event_context_init(NULL
);
6427 DEBUG(DEBUG_ERR
, ("Failed to initialize event system\n"));
6431 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6432 if (strcmp(control
, ctdb_commands
[i
].name
) == 0) {
6437 if (i
== ARRAY_SIZE(ctdb_commands
)) {
6438 DEBUG(DEBUG_ERR
, ("Unknown control '%s'\n", control
));
6442 if (ctdb_commands
[i
].without_daemon
== true) {
6443 if (nodestring
!= NULL
) {
6444 DEBUG(DEBUG_ERR
, ("Can't specify node(s) with \"ctdb %s\"\n", control
));
6447 return ctdb_commands
[i
].fn(NULL
, extra_argc
-1, extra_argv
+1);
6450 /* initialise ctdb */
6451 ctdb
= ctdb_cmdline_client(ev
, TIMELIMIT());
6455 DEBUG(DEBUG_ERR
, ("Failed to init ctdb\n"));
6457 pnn
= find_node_xpnn();
6460 ("Is this node part of a CTDB cluster?\n"));
6465 /* setup the node number(s) to contact */
6466 if (!parse_nodestring(ctdb
, ctdb
, nodestring
, CTDB_CURRENT_NODE
, false,
6467 &options
.nodes
, &options
.pnn
)) {
6471 if (options
.pnn
== CTDB_CURRENT_NODE
) {
6472 options
.pnn
= options
.nodes
[0];
6475 if (ctdb_commands
[i
].auto_all
&&
6476 ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
6477 (options
.pnn
== CTDB_MULTICAST
))) {
6481 for (j
= 0; j
< talloc_array_length(options
.nodes
); j
++) {
6482 options
.pnn
= options
.nodes
[j
];
6483 ret
|= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6486 ret
= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6491 (void)poptFreeContext(pc
);