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"
34 #define ERR_TIMEOUT 20 /* timed out trying to reach node */
35 #define ERR_NONODE 21 /* node does not exist */
36 #define ERR_DISNODE 22 /* node is disconnected */
38 static void usage(void);
47 int printemptyrecords
;
54 #define LONGTIMEOUT options.timelimit*10
56 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
57 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
59 static int control_version(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
61 printf("CTDB version: %s\n", CTDB_VERSION_STRING
);
65 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) { \
66 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", \
67 "Out of memory in " __location__ )); \
71 static uint32_t getpnn(struct ctdb_context
*ctdb
)
73 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
74 (options
.pnn
== CTDB_MULTICAST
)) {
76 ("Cannot get PNN for node %u\n", options
.pnn
));
80 if (options
.pnn
== CTDB_CURRENT_NODE
) {
81 return ctdb_get_pnn(ctdb
);
87 static void assert_single_node_only(void)
89 if ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
90 (options
.pnn
== CTDB_MULTICAST
)) {
92 ("This control can not be applied to multiple PNNs\n"));
97 /* Pretty print the flags to a static buffer in human-readable format.
98 * This never returns NULL!
100 static const char *pretty_print_flags(uint32_t flags
)
103 static const struct {
107 { NODE_FLAGS_DISCONNECTED
, "DISCONNECTED" },
108 { NODE_FLAGS_PERMANENTLY_DISABLED
, "DISABLED" },
109 { NODE_FLAGS_BANNED
, "BANNED" },
110 { NODE_FLAGS_UNHEALTHY
, "UNHEALTHY" },
111 { NODE_FLAGS_DELETED
, "DELETED" },
112 { NODE_FLAGS_STOPPED
, "STOPPED" },
113 { NODE_FLAGS_INACTIVE
, "INACTIVE" },
115 static char flags_str
[512]; /* Big enough to contain all flag names */
118 for (j
=0;j
<ARRAY_SIZE(flag_names
);j
++) {
119 if (flags
& flag_names
[j
].flag
) {
120 if (flags_str
[0] == '\0') {
121 (void) strcpy(flags_str
, flag_names
[j
].name
);
123 (void) strncat(flags_str
, "|", sizeof(flags_str
)-1);
124 (void) strncat(flags_str
, flag_names
[j
].name
,
125 sizeof(flags_str
)-1);
129 if (flags_str
[0] == '\0') {
130 (void) strcpy(flags_str
, "OK");
136 static int h2i(char h
)
138 if (h
>= 'a' && h
<= 'f') return h
- 'a' + 10;
139 if (h
>= 'A' && h
<= 'F') return h
- 'f' + 10;
143 static TDB_DATA
hextodata(TALLOC_CTX
*mem_ctx
, const char *str
)
146 TDB_DATA key
= {NULL
, 0};
150 DEBUG(DEBUG_ERR
,("Key specified with odd number of hexadecimal digits\n"));
155 key
.dptr
= talloc_size(mem_ctx
, key
.dsize
);
157 for (i
=0; i
< len
/2; i
++) {
158 key
.dptr
[i
] = h2i(str
[i
*2]) << 4 | h2i(str
[i
*2+1]);
163 /* Parse a nodestring. Parameter dd_ok controls what happens to nodes
164 * that are disconnected or deleted. If dd_ok is true those nodes are
165 * included in the output list of nodes. If dd_ok is false, those
166 * nodes are filtered from the "all" case and cause an error if
167 * explicitly specified.
169 static bool parse_nodestring(struct ctdb_context
*ctdb
,
171 const char * nodestring
,
172 uint32_t current_pnn
,
177 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
180 struct ctdb_node_map
*nodemap
;
185 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
187 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
188 talloc_free(tmp_ctx
);
192 if (nodestring
!= NULL
) {
193 *nodes
= talloc_array(mem_ctx
, uint32_t, 0);
194 if (*nodes
== NULL
) {
200 if (strcmp(nodestring
, "all") == 0) {
201 *pnn_mode
= CTDB_BROADCAST_ALL
;
204 for (i
= 0; i
< nodemap
->num
; i
++) {
205 if ((nodemap
->nodes
[i
].flags
&
206 (NODE_FLAGS_DISCONNECTED
|
207 NODE_FLAGS_DELETED
)) && !dd_ok
) {
210 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
212 if (*nodes
== NULL
) {
222 ns
= talloc_strdup(tmp_ctx
, nodestring
);
223 tok
= strtok(ns
, ",");
224 while (tok
!= NULL
) {
227 i
= (uint32_t)strtoul(tok
, &endptr
, 0);
228 if (i
== 0 && tok
== endptr
) {
230 ("Invalid node %s\n", tok
));
231 talloc_free(tmp_ctx
);
234 if (i
>= nodemap
->num
) {
235 DEBUG(DEBUG_ERR
, ("Node %u does not exist\n", i
));
236 talloc_free(tmp_ctx
);
239 if ((nodemap
->nodes
[i
].flags
&
240 (NODE_FLAGS_DISCONNECTED
|
241 NODE_FLAGS_DELETED
)) && !dd_ok
) {
242 DEBUG(DEBUG_ERR
, ("Node %u has status %s\n", i
, pretty_print_flags(nodemap
->nodes
[i
].flags
)));
243 talloc_free(tmp_ctx
);
246 if ((pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), i
)) < 0) {
247 DEBUG(DEBUG_ERR
, ("Can not access node %u. Node is not operational.\n", i
));
248 talloc_free(tmp_ctx
);
252 *nodes
= talloc_realloc(mem_ctx
, *nodes
,
254 if (*nodes
== NULL
) {
261 tok
= strtok(NULL
, ",");
266 *pnn_mode
= (*nodes
)[0];
268 *pnn_mode
= CTDB_MULTICAST
;
272 /* default - no nodes specified */
273 *nodes
= talloc_array(mem_ctx
, uint32_t, 1);
274 if (*nodes
== NULL
) {
277 *pnn_mode
= CTDB_CURRENT_NODE
;
279 if (((*nodes
)[0] = ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), current_pnn
)) < 0) {
284 talloc_free(tmp_ctx
);
288 talloc_free(tmp_ctx
);
293 check if a database exists
295 static bool db_exists(struct ctdb_context
*ctdb
, const char *dbarg
,
296 uint32_t *dbid
, const char **dbname
, uint8_t *flags
)
299 struct ctdb_dbid_map
*dbmap
=NULL
;
300 bool dbid_given
= false, found
= false;
302 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
305 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &dbmap
);
307 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
311 if (strncmp(dbarg
, "0x", 2) == 0) {
312 id
= strtoul(dbarg
, NULL
, 0);
316 for(i
=0; i
<dbmap
->num
; i
++) {
318 if (id
== dbmap
->dbs
[i
].dbid
) {
323 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, tmp_ctx
, &name
);
325 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].dbid
));
329 if (strcmp(name
, dbarg
) == 0) {
330 id
= dbmap
->dbs
[i
].dbid
;
337 if (found
&& dbid_given
&& dbname
!= NULL
) {
338 ret
= ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, tmp_ctx
, &name
);
340 DEBUG(DEBUG_ERR
, ("Unable to get dbname from dbid %u\n", dbmap
->dbs
[i
].dbid
));
347 if (dbid
) *dbid
= id
;
348 if (dbname
) *dbname
= talloc_strdup(ctdb
, name
);
349 if (flags
) *flags
= dbmap
->dbs
[i
].flags
;
351 DEBUG(DEBUG_ERR
,("No database matching '%s' found\n", dbarg
));
355 talloc_free(tmp_ctx
);
360 see if a process exists
362 static int control_process_exists(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
370 if (sscanf(argv
[0], "%u:%u", &pnn
, &pid
) != 2) {
371 DEBUG(DEBUG_ERR
, ("Badly formed pnn:pid\n"));
375 ret
= ctdb_ctrl_process_exists(ctdb
, pnn
, pid
);
377 printf("%u:%u exists\n", pnn
, pid
);
379 printf("%u:%u does not exist\n", pnn
, pid
);
385 display statistics structure
387 static void show_statistics(struct ctdb_statistics
*s
, int show_header
)
389 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
391 const char *prefix
=NULL
;
393 int tmp
, days
, hours
, minutes
, seconds
;
398 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
399 STATISTICS_FIELD(num_clients
),
400 STATISTICS_FIELD(frozen
),
401 STATISTICS_FIELD(recovering
),
402 STATISTICS_FIELD(num_recoveries
),
403 STATISTICS_FIELD(client_packets_sent
),
404 STATISTICS_FIELD(client_packets_recv
),
405 STATISTICS_FIELD(node_packets_sent
),
406 STATISTICS_FIELD(node_packets_recv
),
407 STATISTICS_FIELD(keepalive_packets_sent
),
408 STATISTICS_FIELD(keepalive_packets_recv
),
409 STATISTICS_FIELD(node
.req_call
),
410 STATISTICS_FIELD(node
.reply_call
),
411 STATISTICS_FIELD(node
.req_dmaster
),
412 STATISTICS_FIELD(node
.reply_dmaster
),
413 STATISTICS_FIELD(node
.reply_error
),
414 STATISTICS_FIELD(node
.req_message
),
415 STATISTICS_FIELD(node
.req_control
),
416 STATISTICS_FIELD(node
.reply_control
),
417 STATISTICS_FIELD(client
.req_call
),
418 STATISTICS_FIELD(client
.req_message
),
419 STATISTICS_FIELD(client
.req_control
),
420 STATISTICS_FIELD(timeouts
.call
),
421 STATISTICS_FIELD(timeouts
.control
),
422 STATISTICS_FIELD(timeouts
.traverse
),
423 STATISTICS_FIELD(locks
.num_calls
),
424 STATISTICS_FIELD(locks
.num_current
),
425 STATISTICS_FIELD(locks
.num_pending
),
426 STATISTICS_FIELD(locks
.num_failed
),
427 STATISTICS_FIELD(total_calls
),
428 STATISTICS_FIELD(pending_calls
),
429 STATISTICS_FIELD(childwrite_calls
),
430 STATISTICS_FIELD(pending_childwrite_calls
),
431 STATISTICS_FIELD(memory_used
),
432 STATISTICS_FIELD(max_hop_count
),
433 STATISTICS_FIELD(total_ro_delegations
),
434 STATISTICS_FIELD(total_ro_revokes
),
437 tmp
= s
->statistics_current_time
.tv_sec
- s
->statistics_start_time
.tv_sec
;
446 if (options
.machinereadable
){
448 printf("CTDB version:");
449 printf("Current time of statistics:");
450 printf("Statistics collected since:");
451 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
452 printf("%s:", fields
[i
].name
);
454 printf("num_reclock_ctdbd_latency:");
455 printf("min_reclock_ctdbd_latency:");
456 printf("avg_reclock_ctdbd_latency:");
457 printf("max_reclock_ctdbd_latency:");
459 printf("num_reclock_recd_latency:");
460 printf("min_reclock_recd_latency:");
461 printf("avg_reclock_recd_latency:");
462 printf("max_reclock_recd_latency:");
464 printf("num_call_latency:");
465 printf("min_call_latency:");
466 printf("avg_call_latency:");
467 printf("max_call_latency:");
469 printf("num_lockwait_latency:");
470 printf("min_lockwait_latency:");
471 printf("avg_lockwait_latency:");
472 printf("max_lockwait_latency:");
474 printf("num_childwrite_latency:");
475 printf("min_childwrite_latency:");
476 printf("avg_childwrite_latency:");
477 printf("max_childwrite_latency:");
480 printf("%d:", CTDB_VERSION
);
481 printf("%d:", (int)s
->statistics_current_time
.tv_sec
);
482 printf("%d:", (int)s
->statistics_start_time
.tv_sec
);
483 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
484 printf("%d:", *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
486 printf("%d:", s
->reclock
.ctdbd
.num
);
487 printf("%.6f:", s
->reclock
.ctdbd
.min
);
488 printf("%.6f:", s
->reclock
.ctdbd
.num
?s
->reclock
.ctdbd
.total
/s
->reclock
.ctdbd
.num
:0.0);
489 printf("%.6f:", s
->reclock
.ctdbd
.max
);
491 printf("%d:", s
->reclock
.recd
.num
);
492 printf("%.6f:", s
->reclock
.recd
.min
);
493 printf("%.6f:", s
->reclock
.recd
.num
?s
->reclock
.recd
.total
/s
->reclock
.recd
.num
:0.0);
494 printf("%.6f:", s
->reclock
.recd
.max
);
496 printf("%d:", s
->call_latency
.num
);
497 printf("%.6f:", s
->call_latency
.min
);
498 printf("%.6f:", s
->call_latency
.num
?s
->call_latency
.total
/s
->call_latency
.num
:0.0);
499 printf("%.6f:", s
->call_latency
.max
);
501 printf("%d:", s
->childwrite_latency
.num
);
502 printf("%.6f:", s
->childwrite_latency
.min
);
503 printf("%.6f:", s
->childwrite_latency
.num
?s
->childwrite_latency
.total
/s
->childwrite_latency
.num
:0.0);
504 printf("%.6f:", s
->childwrite_latency
.max
);
507 printf("CTDB version %u\n", CTDB_VERSION
);
508 printf("Current time of statistics : %s", ctime(&s
->statistics_current_time
.tv_sec
));
509 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&s
->statistics_start_time
.tv_sec
));
511 for (i
=0;i
<ARRAY_SIZE(fields
);i
++) {
512 if (strchr(fields
[i
].name
, '.')) {
513 preflen
= strcspn(fields
[i
].name
, ".")+1;
514 if (!prefix
|| strncmp(prefix
, fields
[i
].name
, preflen
) != 0) {
515 prefix
= fields
[i
].name
;
516 printf(" %*.*s\n", preflen
-1, preflen
-1, fields
[i
].name
);
521 printf(" %*s%-22s%*s%10u\n",
523 fields
[i
].name
+preflen
,
525 *(uint32_t *)(fields
[i
].offset
+(uint8_t *)s
));
527 printf(" hop_count_buckets:");
528 for (i
=0;i
<MAX_COUNT_BUCKETS
;i
++) {
529 printf(" %d", s
->hop_count_bucket
[i
]);
532 printf(" lock_buckets:");
533 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
534 printf(" %d", s
->locks
.buckets
[i
]);
537 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
);
539 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
);
541 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
);
543 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
);
544 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
);
547 talloc_free(tmp_ctx
);
551 display remote ctdb statistics combined from all nodes
553 static int control_statistics_all(struct ctdb_context
*ctdb
)
556 struct ctdb_statistics statistics
;
560 nodes
= ctdb_get_connected_nodes(ctdb
, TIMELIMIT(), ctdb
, &num_nodes
);
561 CTDB_NO_MEMORY(ctdb
, nodes
);
563 ZERO_STRUCT(statistics
);
565 for (i
=0;i
<num_nodes
;i
++) {
566 struct ctdb_statistics s1
;
568 uint32_t *v1
= (uint32_t *)&s1
;
569 uint32_t *v2
= (uint32_t *)&statistics
;
571 offsetof(struct ctdb_statistics
, __last_counter
) / sizeof(uint32_t);
572 ret
= ctdb_ctrl_statistics(ctdb
, nodes
[i
], &s1
);
574 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", nodes
[i
]));
577 for (j
=0;j
<num_ints
;j
++) {
580 statistics
.max_hop_count
=
581 MAX(statistics
.max_hop_count
, s1
.max_hop_count
);
582 statistics
.call_latency
.max
=
583 MAX(statistics
.call_latency
.max
, s1
.call_latency
.max
);
586 printf("Gathered statistics for %u nodes\n", num_nodes
);
587 show_statistics(&statistics
, 1);
592 display remote ctdb statistics
594 static int control_statistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
597 struct ctdb_statistics statistics
;
599 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
600 return control_statistics_all(ctdb
);
603 ret
= ctdb_ctrl_statistics(ctdb
, options
.pnn
, &statistics
);
605 DEBUG(DEBUG_ERR
, ("Unable to get statistics from node %u\n", options
.pnn
));
608 show_statistics(&statistics
, 1);
614 reset remote ctdb statistics
616 static int control_statistics_reset(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
620 ret
= ctdb_statistics_reset(ctdb
, options
.pnn
);
622 DEBUG(DEBUG_ERR
, ("Unable to reset statistics on node %u\n", options
.pnn
));
630 display remote ctdb rolling statistics
632 static int control_stats(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
635 struct ctdb_statistics_wire
*stats
;
636 int i
, num_records
= -1;
638 assert_single_node_only();
641 num_records
= atoi(argv
[0]) - 1;
644 ret
= ctdb_ctrl_getstathistory(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &stats
);
646 DEBUG(DEBUG_ERR
, ("Unable to get rolling statistics from node %u\n", options
.pnn
));
649 for (i
=0;i
<stats
->num
;i
++) {
650 if (stats
->stats
[i
].statistics_start_time
.tv_sec
== 0) {
653 show_statistics(&stats
->stats
[i
], i
==0);
654 if (i
== num_records
) {
663 display remote ctdb db statistics
665 static int control_dbstatistics(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
667 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
668 struct ctdb_db_statistics
*dbstat
;
678 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
682 ret
= ctdb_ctrl_dbstatistics(ctdb
, options
.pnn
, db_id
, tmp_ctx
, &dbstat
);
684 DEBUG(DEBUG_ERR
,("Failed to read db statistics from node\n"));
685 talloc_free(tmp_ctx
);
689 printf("DB Statistics: %s\n", argv
[0]);
690 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
691 dbstat
->db_ro_delegations
);
692 printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
693 dbstat
->db_ro_delegations
);
694 printf(" %s\n", "locks");
695 printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
696 dbstat
->locks
.num_calls
);
697 printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
698 dbstat
->locks
.num_failed
);
699 printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
700 dbstat
->locks
.num_current
);
701 printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
702 dbstat
->locks
.num_pending
);
703 printf(" %s", "hop_count_buckets:");
704 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
705 printf(" %d", dbstat
->hop_count_bucket
[i
]);
708 printf(" %s", "lock_buckets:");
709 for (i
=0; i
<MAX_COUNT_BUCKETS
; i
++) {
710 printf(" %d", dbstat
->locks
.buckets
[i
]);
713 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
714 "locks_latency MIN/AVG/MAX",
715 dbstat
->locks
.latency
.min
,
716 (dbstat
->locks
.latency
.num
?
717 dbstat
->locks
.latency
.total
/dbstat
->locks
.latency
.num
:
719 dbstat
->locks
.latency
.max
,
720 dbstat
->locks
.latency
.num
);
722 for (i
=0; i
<dbstat
->num_hot_keys
; i
++) {
723 if (dbstat
->hot_keys
[i
].count
> 0) {
727 dbstat
->num_hot_keys
= num_hot_keys
;
729 printf(" Num Hot Keys: %d\n", dbstat
->num_hot_keys
);
730 for (i
= 0; i
< dbstat
->num_hot_keys
; i
++) {
732 printf(" Count:%d Key:", dbstat
->hot_keys
[i
].count
);
733 for (j
= 0; j
< dbstat
->hot_keys
[i
].key
.dsize
; j
++) {
734 printf("%02x", dbstat
->hot_keys
[i
].key
.dptr
[j
]&0xff);
739 talloc_free(tmp_ctx
);
744 display uptime of remote node
746 static int control_uptime(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
749 struct ctdb_uptime
*uptime
= NULL
;
750 int tmp
, days
, hours
, minutes
, seconds
;
752 ret
= ctdb_ctrl_uptime(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &uptime
);
754 DEBUG(DEBUG_ERR
, ("Unable to get uptime from node %u\n", options
.pnn
));
758 if (options
.machinereadable
){
759 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
760 printf(":%u:%u:%u:%lf\n",
761 (unsigned int)uptime
->current_time
.tv_sec
,
762 (unsigned int)uptime
->ctdbd_start_time
.tv_sec
,
763 (unsigned int)uptime
->last_recovery_finished
.tv_sec
,
764 timeval_delta(&uptime
->last_recovery_finished
,
765 &uptime
->last_recovery_started
)
770 printf("Current time of node : %s", ctime(&uptime
->current_time
.tv_sec
));
772 tmp
= uptime
->current_time
.tv_sec
- uptime
->ctdbd_start_time
.tv_sec
;
780 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->ctdbd_start_time
.tv_sec
));
782 tmp
= uptime
->current_time
.tv_sec
- uptime
->last_recovery_finished
.tv_sec
;
790 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days
, hours
, minutes
, seconds
, ctime(&uptime
->last_recovery_finished
.tv_sec
));
792 printf("Duration of last recovery/failover: %lf seconds\n",
793 timeval_delta(&uptime
->last_recovery_finished
,
794 &uptime
->last_recovery_started
));
800 show the PNN of the current node
802 static int control_pnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
806 mypnn
= getpnn(ctdb
);
808 printf("PNN:%d\n", mypnn
);
814 struct pnn_node
*next
;
819 static struct pnn_node
*read_nodes_file(TALLOC_CTX
*mem_ctx
)
821 const char *nodes_list
;
825 struct pnn_node
*pnn_nodes
= NULL
;
826 struct pnn_node
*pnn_node
;
827 struct pnn_node
*tmp_node
;
829 /* read the nodes file */
830 nodes_list
= getenv("CTDB_NODES");
831 if (nodes_list
== NULL
) {
832 nodes_list
= talloc_asprintf(mem_ctx
, "%s/nodes",
833 getenv("CTDB_BASE"));
834 if (nodes_list
== NULL
) {
835 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
839 lines
= file_lines_load(nodes_list
, &nlines
, mem_ctx
);
843 while (nlines
> 0 && strcmp(lines
[nlines
-1], "") == 0) {
846 for (i
=0, pnn
=0; i
<nlines
; i
++) {
850 /* strip leading spaces */
851 while((*node
== ' ') || (*node
== '\t')) {
858 if (strcmp(node
, "") == 0) {
861 pnn_node
= talloc(mem_ctx
, struct pnn_node
);
862 pnn_node
->pnn
= pnn
++;
863 pnn_node
->addr
= talloc_strdup(pnn_node
, node
);
864 pnn_node
->next
= pnn_nodes
;
865 pnn_nodes
= pnn_node
;
868 /* swap them around so we return them in incrementing order */
869 pnn_node
= pnn_nodes
;
873 pnn_node
= pnn_node
->next
;
875 tmp_node
->next
= pnn_nodes
;
876 pnn_nodes
= tmp_node
;
883 show the PNN of the current node
884 discover the pnn by loading the nodes file and try to bind to all
885 addresses one at a time until the ip address is found.
887 static int control_xpnn(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
889 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
890 struct pnn_node
*pnn_nodes
;
891 struct pnn_node
*pnn_node
;
893 assert_single_node_only();
895 pnn_nodes
= read_nodes_file(mem_ctx
);
896 if (pnn_nodes
== NULL
) {
897 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
898 talloc_free(mem_ctx
);
902 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
905 if (parse_ip(pnn_node
->addr
, NULL
, 63999, &addr
) == 0) {
906 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s' in nodes file\n", pnn_node
->addr
));
907 talloc_free(mem_ctx
);
911 if (ctdb_sys_have_ip(&addr
)) {
912 printf("PNN:%d\n", pnn_node
->pnn
);
913 talloc_free(mem_ctx
);
918 printf("Failed to detect which PNN this node is\n");
919 talloc_free(mem_ctx
);
923 /* Helpers for ctdb status
925 static bool is_partially_online(struct ctdb_context
*ctdb
, struct ctdb_node_and_flags
*node
)
927 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
931 if (node
->flags
== 0) {
932 struct ctdb_control_get_ifaces
*ifaces
;
934 if (ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), node
->pnn
,
935 tmp_ctx
, &ifaces
) == 0) {
936 for (j
=0; j
< ifaces
->num
; j
++) {
937 if (ifaces
->ifaces
[j
].link_state
!= 0) {
945 talloc_free(tmp_ctx
);
950 static void control_status_header_machine(void)
952 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
953 ":Inactive:PartiallyOnline:ThisNode:\n");
956 static int control_status_1_machine(struct ctdb_context
*ctdb
, int mypnn
,
957 struct ctdb_node_and_flags
*node
)
959 printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node
->pnn
,
960 ctdb_addr_to_str(&node
->addr
),
961 !!(node
->flags
&NODE_FLAGS_DISCONNECTED
),
962 !!(node
->flags
&NODE_FLAGS_BANNED
),
963 !!(node
->flags
&NODE_FLAGS_PERMANENTLY_DISABLED
),
964 !!(node
->flags
&NODE_FLAGS_UNHEALTHY
),
965 !!(node
->flags
&NODE_FLAGS_STOPPED
),
966 !!(node
->flags
&NODE_FLAGS_INACTIVE
),
967 is_partially_online(ctdb
, node
) ? 1 : 0,
968 (node
->pnn
== mypnn
)?'Y':'N');
973 static int control_status_1_human(struct ctdb_context
*ctdb
, int mypnn
,
974 struct ctdb_node_and_flags
*node
)
976 printf("pnn:%d %-16s %s%s\n", node
->pnn
,
977 ctdb_addr_to_str(&node
->addr
),
978 is_partially_online(ctdb
, node
) ? "PARTIALLYONLINE" : pretty_print_flags(node
->flags
),
979 node
->pnn
== mypnn
?" (THIS NODE)":"");
985 display remote ctdb status
987 static int control_status(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
989 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
991 struct ctdb_vnn_map
*vnnmap
=NULL
;
992 struct ctdb_node_map
*nodemap
=NULL
;
993 uint32_t recmode
, recmaster
, mypnn
;
994 int num_deleted_nodes
= 0;
997 mypnn
= getpnn(ctdb
);
999 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1001 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1002 talloc_free(tmp_ctx
);
1006 if (options
.machinereadable
) {
1007 control_status_header_machine();
1008 for (i
=0;i
<nodemap
->num
;i
++) {
1009 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1012 (void) control_status_1_machine(ctdb
, mypnn
,
1013 &nodemap
->nodes
[i
]);
1015 talloc_free(tmp_ctx
);
1019 for (i
=0; i
<nodemap
->num
; i
++) {
1020 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1021 num_deleted_nodes
++;
1024 if (num_deleted_nodes
== 0) {
1025 printf("Number of nodes:%d\n", nodemap
->num
);
1027 printf("Number of nodes:%d (including %d deleted nodes)\n",
1028 nodemap
->num
, num_deleted_nodes
);
1030 for(i
=0;i
<nodemap
->num
;i
++){
1031 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1034 (void) control_status_1_human(ctdb
, mypnn
, &nodemap
->nodes
[i
]);
1037 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
1039 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
1040 talloc_free(tmp_ctx
);
1043 if (vnnmap
->generation
== INVALID_GENERATION
) {
1044 printf("Generation:INVALID\n");
1046 printf("Generation:%d\n",vnnmap
->generation
);
1048 printf("Size:%d\n",vnnmap
->size
);
1049 for(i
=0;i
<vnnmap
->size
;i
++){
1050 printf("hash:%d lmaster:%d\n", i
, vnnmap
->map
[i
]);
1053 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmode
);
1055 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
1056 talloc_free(tmp_ctx
);
1059 printf("Recovery mode:%s (%d)\n",recmode
==CTDB_RECOVERY_NORMAL
?"NORMAL":"RECOVERY",recmode
);
1061 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), options
.pnn
, &recmaster
);
1063 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1064 talloc_free(tmp_ctx
);
1067 printf("Recovery master:%d\n",recmaster
);
1069 talloc_free(tmp_ctx
);
1073 static int control_nodestatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1075 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1077 struct ctdb_node_map
*nodemap
=NULL
;
1079 uint32_t pnn_mode
, mypnn
;
1085 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1086 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1090 if (options
.machinereadable
) {
1091 control_status_header_machine();
1092 } else if (pnn_mode
== CTDB_BROADCAST_ALL
) {
1093 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes
));
1096 mypnn
= getpnn(ctdb
);
1098 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1100 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1101 talloc_free(tmp_ctx
);
1107 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1108 if (options
.machinereadable
) {
1109 ret
|= control_status_1_machine(ctdb
, mypnn
,
1110 &nodemap
->nodes
[nodes
[i
]]);
1112 ret
|= control_status_1_human(ctdb
, mypnn
,
1113 &nodemap
->nodes
[nodes
[i
]]);
1117 talloc_free(tmp_ctx
);
1122 struct natgw_node
*next
;
1126 /* talloc off the existing nodemap... */
1127 static struct ctdb_node_map
*talloc_nodemap(struct ctdb_node_map
*nodemap
)
1129 return talloc_zero_size(nodemap
,
1130 offsetof(struct ctdb_node_map
, nodes
) +
1131 nodemap
->num
* sizeof(struct ctdb_node_and_flags
));
1134 static struct ctdb_node_map
*
1135 filter_nodemap_by_capabilities(struct ctdb_context
*ctdb
,
1136 struct ctdb_node_map
*nodemap
,
1137 uint32_t required_capabilities
)
1140 uint32_t capabilities
;
1141 struct ctdb_node_map
*ret
;
1143 ret
= talloc_nodemap(nodemap
);
1144 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1148 for (i
= 0; i
< nodemap
->num
; i
++) {
1149 int res
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(),
1150 nodemap
->nodes
[i
].pnn
,
1153 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n",
1154 nodemap
->nodes
[i
].pnn
));
1158 if (!(capabilities
& required_capabilities
)) {
1162 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1169 static struct ctdb_node_map
*
1170 filter_nodemap_by_flags(struct ctdb_context
*ctdb
,
1171 struct ctdb_node_map
*nodemap
,
1172 uint32_t flags_mask
)
1175 struct ctdb_node_map
*ret
;
1177 ret
= talloc_nodemap(nodemap
);
1178 CTDB_NO_MEMORY_NULL(ctdb
, ret
);
1182 for (i
= 0; i
< nodemap
->num
; i
++) {
1183 if (nodemap
->nodes
[i
].flags
& flags_mask
) {
1187 ret
->nodes
[ret
->num
] = nodemap
->nodes
[i
];
1195 display the list of nodes belonging to this natgw configuration
1197 static int control_natgwlist(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1199 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1201 const char *natgw_list
;
1204 struct natgw_node
*natgw_nodes
= NULL
;
1205 struct natgw_node
*natgw_node
;
1206 struct ctdb_node_map
*nodemap
=NULL
;
1207 struct ctdb_node_map
*cnodemap
;
1208 uint32_t mypnn
, pnn
;
1211 /* When we have some nodes that could be the NATGW, make a
1212 * series of attempts to find the first node that doesn't have
1213 * certain status flags set.
1215 uint32_t exclude_flags
[] = {
1216 /* Look for a nice healthy node */
1217 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
|NODE_FLAGS_BANNED
|NODE_FLAGS_UNHEALTHY
,
1218 /* If not found, an UNHEALTHY/BANNED node will do */
1219 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_STOPPED
|NODE_FLAGS_DELETED
,
1220 /* If not found, a STOPPED node will do */
1221 NODE_FLAGS_DISCONNECTED
|NODE_FLAGS_DELETED
,
1225 /* read the natgw nodes file into a linked list */
1226 natgw_list
= getenv("CTDB_NATGW_NODES");
1227 if (natgw_list
== NULL
) {
1228 natgw_list
= talloc_asprintf(tmp_ctx
, "%s/natgw_nodes",
1229 getenv("CTDB_BASE"));
1230 if (natgw_list
== NULL
) {
1231 DEBUG(DEBUG_ALERT
,(__location__
" Out of memory\n"));
1235 lines
= file_lines_load(natgw_list
, &nlines
, ctdb
);
1236 if (lines
== NULL
) {
1237 ctdb_set_error(ctdb
, "Failed to load natgw node list '%s'\n", natgw_list
);
1238 talloc_free(tmp_ctx
);
1241 for (i
=0;i
<nlines
;i
++) {
1245 /* strip leading spaces */
1246 while((*node
== ' ') || (*node
== '\t')) {
1252 if (strcmp(node
, "") == 0) {
1255 natgw_node
= talloc(ctdb
, struct natgw_node
);
1256 natgw_node
->addr
= talloc_strdup(natgw_node
, node
);
1257 CTDB_NO_MEMORY(ctdb
, natgw_node
->addr
);
1258 natgw_node
->next
= natgw_nodes
;
1259 natgw_nodes
= natgw_node
;
1262 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1264 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node.\n"));
1265 talloc_free(tmp_ctx
);
1269 /* Trim the nodemap so it only includes connected nodes in the
1270 * current natgw group.
1273 while(i
<nodemap
->num
) {
1274 for(natgw_node
=natgw_nodes
;natgw_node
;natgw_node
=natgw_node
->next
) {
1275 if (!strcmp(natgw_node
->addr
, ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
))) {
1280 /* this node was not in the natgw so we just remove it from
1283 if ((natgw_node
== NULL
)
1284 || (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) ) {
1287 for (j
=i
+1; j
<nodemap
->num
; j
++) {
1288 nodemap
->nodes
[j
-1] = nodemap
->nodes
[j
];
1297 /* Get a nodemap that includes only the nodes with the NATGW
1299 cnodemap
= filter_nodemap_by_capabilities(ctdb
, nodemap
,
1301 if (cnodemap
== NULL
) {
1306 ret
= 2; /* matches ENOENT */
1309 for (i
= 0; exclude_flags
[i
] != 0; i
++) {
1310 struct ctdb_node_map
*t
=
1311 filter_nodemap_by_flags(ctdb
, cnodemap
,
1320 pnn
= t
->nodes
[0].pnn
;
1321 ip
= ctdb_addr_to_str(&t
->nodes
[0].addr
);
1327 if (options
.machinereadable
) {
1328 printf(":Node:IP:\n");
1329 printf(":%d:%s:\n", pnn
, ip
);
1331 printf("%d %s\n", pnn
, ip
);
1334 /* print the pruned list of nodes belonging to this natgw list */
1335 mypnn
= getpnn(ctdb
);
1336 if (options
.machinereadable
) {
1337 control_status_header_machine();
1339 printf("Number of nodes:%d\n", nodemap
->num
);
1341 for(i
=0;i
<nodemap
->num
;i
++){
1342 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
1345 if (options
.machinereadable
) {
1346 control_status_1_machine(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1348 control_status_1_human(ctdb
, mypnn
, &(nodemap
->nodes
[i
]));
1353 talloc_free(tmp_ctx
);
1358 display the status of the scripts for monitoring (or other events)
1360 static int control_one_scriptstatus(struct ctdb_context
*ctdb
,
1361 enum ctdb_eventscript_call type
)
1363 struct ctdb_scripts_wire
*script_status
;
1366 ret
= ctdb_ctrl_getscriptstatus(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, type
, &script_status
);
1368 DEBUG(DEBUG_ERR
, ("Unable to get script status from node %u\n", options
.pnn
));
1372 if (script_status
== NULL
) {
1373 if (!options
.machinereadable
) {
1374 printf("%s cycle never run\n",
1375 ctdb_eventscript_call_names
[type
]);
1380 if (!options
.machinereadable
) {
1381 printf("%d scripts were executed last %s cycle\n",
1382 script_status
->num_scripts
,
1383 ctdb_eventscript_call_names
[type
]);
1385 for (i
=0; i
<script_status
->num_scripts
; i
++) {
1386 const char *status
= NULL
;
1388 switch (script_status
->scripts
[i
].status
) {
1390 status
= "TIMEDOUT";
1393 status
= "DISABLED";
1399 if (script_status
->scripts
[i
].status
> 0)
1403 if (options
.machinereadable
) {
1404 printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1405 ctdb_eventscript_call_names
[type
],
1406 script_status
->scripts
[i
].name
,
1407 script_status
->scripts
[i
].status
,
1409 (long)script_status
->scripts
[i
].start
.tv_sec
,
1410 (long)script_status
->scripts
[i
].start
.tv_usec
,
1411 (long)script_status
->scripts
[i
].finished
.tv_sec
,
1412 (long)script_status
->scripts
[i
].finished
.tv_usec
,
1413 script_status
->scripts
[i
].output
);
1417 printf("%-20s Status:%s ",
1418 script_status
->scripts
[i
].name
, status
);
1420 /* Some other error, eg from stat. */
1421 printf("%-20s Status:CANNOT RUN (%s)",
1422 script_status
->scripts
[i
].name
,
1423 strerror(-script_status
->scripts
[i
].status
));
1425 if (script_status
->scripts
[i
].status
>= 0) {
1426 printf("Duration:%.3lf ",
1427 timeval_delta(&script_status
->scripts
[i
].finished
,
1428 &script_status
->scripts
[i
].start
));
1430 if (script_status
->scripts
[i
].status
!= -ENOEXEC
) {
1432 ctime(&script_status
->scripts
[i
].start
.tv_sec
));
1433 if (script_status
->scripts
[i
].status
!= 0) {
1434 printf(" OUTPUT:%s\n",
1435 script_status
->scripts
[i
].output
);
1445 static int control_scriptstatus(struct ctdb_context
*ctdb
,
1446 int argc
, const char **argv
)
1449 enum ctdb_eventscript_call type
, min
, max
;
1453 DEBUG(DEBUG_ERR
, ("Unknown arguments to scriptstatus\n"));
1458 arg
= ctdb_eventscript_call_names
[CTDB_EVENT_MONITOR
];
1462 for (type
= 0; type
< CTDB_EVENT_MAX
; type
++) {
1463 if (strcmp(arg
, ctdb_eventscript_call_names
[type
]) == 0) {
1469 if (type
== CTDB_EVENT_MAX
) {
1470 if (strcmp(arg
, "all") == 0) {
1472 max
= CTDB_EVENT_MAX
;
1474 DEBUG(DEBUG_ERR
, ("Unknown event type %s\n", argv
[0]));
1479 if (options
.machinereadable
) {
1480 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1483 for (type
= min
; type
< max
; type
++) {
1484 ret
= control_one_scriptstatus(ctdb
, type
);
1494 enable an eventscript
1496 static int control_enablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1504 ret
= ctdb_ctrl_enablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1506 DEBUG(DEBUG_ERR
, ("Unable to enable script %s on node %u\n", argv
[0], options
.pnn
));
1514 disable an eventscript
1516 static int control_disablescript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1524 ret
= ctdb_ctrl_disablescript(ctdb
, TIMELIMIT(), options
.pnn
, argv
[0]);
1526 DEBUG(DEBUG_ERR
, ("Unable to disable script %s on node %u\n", argv
[0], options
.pnn
));
1534 display the pnn of the recovery master
1536 static int control_recmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1541 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
1543 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
1546 printf("%d\n",recmaster
);
1552 add a tickle to a public address
1554 static int control_add_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1556 struct ctdb_tcp_connection t
;
1560 assert_single_node_only();
1566 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1567 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1570 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1571 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1575 data
.dptr
= (uint8_t *)&t
;
1576 data
.dsize
= sizeof(t
);
1578 /* tell all nodes about this tcp connection */
1579 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE
,
1580 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1582 DEBUG(DEBUG_ERR
,("Failed to add tickle\n"));
1591 delete a tickle from a node
1593 static int control_del_tickle(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1595 struct ctdb_tcp_connection t
;
1599 assert_single_node_only();
1605 if (parse_ip_port(argv
[0], &t
.src_addr
) == 0) {
1606 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1609 if (parse_ip_port(argv
[1], &t
.dst_addr
) == 0) {
1610 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[1]));
1614 data
.dptr
= (uint8_t *)&t
;
1615 data
.dsize
= sizeof(t
);
1617 /* tell all nodes about this tcp connection */
1618 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_TCP_REMOVE
,
1619 0, data
, ctdb
, NULL
, NULL
, NULL
, NULL
);
1621 DEBUG(DEBUG_ERR
,("Failed to remove tickle\n"));
1630 get a list of all tickles for this pnn
1632 static int control_get_tickles(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1634 struct ctdb_control_tcp_tickle_list
*list
;
1635 ctdb_sock_addr addr
;
1639 assert_single_node_only();
1646 port
= atoi(argv
[1]);
1649 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1650 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1654 ret
= ctdb_ctrl_get_tcp_tickles(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &addr
, &list
);
1656 DEBUG(DEBUG_ERR
, ("Unable to list tickles\n"));
1660 if (options
.machinereadable
){
1661 printf(":source ip:port:destination ip:port:\n");
1662 for (i
=0;i
<list
->tickles
.num
;i
++) {
1663 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1666 printf(":%s:%u", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1667 printf(":%s:%u:\n", ctdb_addr_to_str(&list
->tickles
.connections
[i
].dst_addr
), ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
));
1670 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list
->addr
));
1671 printf("Num tickles:%u\n", list
->tickles
.num
);
1672 for (i
=0;i
<list
->tickles
.num
;i
++) {
1673 if (port
&& port
!= ntohs(list
->tickles
.connections
[i
].dst_addr
.ip
.sin_port
)) {
1676 printf("SRC: %s:%u ", ctdb_addr_to_str(&list
->tickles
.connections
[i
].src_addr
), ntohs(list
->tickles
.connections
[i
].src_addr
.ip
.sin_port
));
1677 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
));
1687 static int move_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1689 struct ctdb_all_public_ips
*ips
;
1690 struct ctdb_public_ip ip
;
1693 uint32_t disable_time
;
1695 struct ctdb_node_map
*nodemap
=NULL
;
1696 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1699 data
.dptr
= (uint8_t*)&disable_time
;
1700 data
.dsize
= sizeof(disable_time
);
1701 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1703 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1709 /* read the public ip list from the node */
1710 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), pnn
, ctdb
, &ips
);
1712 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", pnn
));
1713 talloc_free(tmp_ctx
);
1717 for (i
=0;i
<ips
->num
;i
++) {
1718 if (ctdb_same_ip(addr
, &ips
->ips
[i
].addr
)) {
1723 DEBUG(DEBUG_ERR
, ("Node %u can not host ip address '%s'\n",
1724 pnn
, ctdb_addr_to_str(addr
)));
1725 talloc_free(tmp_ctx
);
1732 data
.dptr
= (uint8_t *)&ip
;
1733 data
.dsize
= sizeof(ip
);
1735 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1737 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1738 talloc_free(tmp_ctx
);
1742 nodes
= list_of_nodes(ctdb
, nodemap
, tmp_ctx
, NODE_FLAGS_INACTIVE
, pnn
);
1743 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1750 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1751 talloc_free(tmp_ctx
);
1755 ret
= ctdb_ctrl_takeover_ip(ctdb
, LONGTIMELIMIT(), pnn
, &ip
);
1757 DEBUG(DEBUG_ERR
,("Failed to take over IP on node %d\n", pnn
));
1758 talloc_free(tmp_ctx
);
1762 /* update the recovery daemon so it now knows to expect the new
1763 node assignment for this ip.
1765 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_RECD_UPDATE_IP
, data
);
1767 DEBUG(DEBUG_ERR
,("Failed to send message to update the ip on the recovery master.\n"));
1771 talloc_free(tmp_ctx
);
1777 * scans all other nodes and returns a pnn for another node that can host this
1781 find_other_host_for_public_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1783 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1784 struct ctdb_all_public_ips
*ips
;
1785 struct ctdb_node_map
*nodemap
=NULL
;
1788 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
1790 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1791 talloc_free(tmp_ctx
);
1795 for(i
=0;i
<nodemap
->num
;i
++){
1796 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
1799 if (nodemap
->nodes
[i
].pnn
== options
.pnn
) {
1803 /* read the public ip list from this node */
1804 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
1806 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
1810 for (j
=0;j
<ips
->num
;j
++) {
1811 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
1812 talloc_free(tmp_ctx
);
1813 return nodemap
->nodes
[i
].pnn
;
1819 talloc_free(tmp_ctx
);
1823 /* If pnn is -1 then try to find a node to move IP to... */
1824 static bool try_moveip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
, uint32_t pnn
)
1826 bool pnn_specified
= (pnn
== -1 ? false : true);
1829 while (retries
< 5) {
1830 if (!pnn_specified
) {
1831 pnn
= find_other_host_for_public_ip(ctdb
, addr
);
1836 ("Trying to move public IP to node %u\n", pnn
));
1839 if (move_ip(ctdb
, addr
, pnn
) == 0) {
1852 move/failover an ip address to a specific node
1854 static int control_moveip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1857 ctdb_sock_addr addr
;
1859 assert_single_node_only();
1866 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
1867 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
1872 if (sscanf(argv
[1], "%u", &pnn
) != 1) {
1873 DEBUG(DEBUG_ERR
, ("Badly formed pnn\n"));
1877 if (!try_moveip(ctdb
, &addr
, pnn
)) {
1878 DEBUG(DEBUG_ERR
,("Failed to move IP to node %d.\n", pnn
));
1885 static int rebalance_node(struct ctdb_context
*ctdb
, uint32_t pnn
)
1889 data
.dptr
= (uint8_t *)&pnn
;
1890 data
.dsize
= sizeof(uint32_t);
1891 if (ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_REBALANCE_NODE
, data
) != 0) {
1893 ("Failed to send message to force node %u to be a rebalancing target\n",
1903 rebalance a node by setting it to allow failback and triggering a
1906 static int control_rebalancenode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1908 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1913 assert_single_node_only();
1919 /* Determine the nodes where IPs need to be reloaded */
1920 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
1921 options
.pnn
, true, &nodes
, &pnn_mode
)) {
1926 for (i
= 0; i
< talloc_array_length(nodes
); i
++) {
1927 if (!rebalance_node(ctdb
, nodes
[i
])) {
1933 talloc_free(tmp_ctx
);
1937 static int rebalance_ip(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
1939 struct ctdb_public_ip ip
;
1942 uint32_t disable_time
;
1944 struct ctdb_node_map
*nodemap
=NULL
;
1945 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
1948 data
.dptr
= (uint8_t*)&disable_time
;
1949 data
.dsize
= sizeof(disable_time
);
1950 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, CTDB_SRVID_DISABLE_IP_CHECK
, data
);
1952 DEBUG(DEBUG_ERR
,("Failed to send message to disable ipcheck\n"));
1959 data
.dptr
= (uint8_t *)&ip
;
1960 data
.dsize
= sizeof(ip
);
1962 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &nodemap
);
1964 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
1965 talloc_free(tmp_ctx
);
1969 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
1970 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELEASE_IP
,
1977 DEBUG(DEBUG_ERR
,("Failed to release IP on nodes\n"));
1978 talloc_free(tmp_ctx
);
1982 talloc_free(tmp_ctx
);
1987 release an ip form all nodes and have it re-assigned by recd
1989 static int control_rebalanceip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
1991 ctdb_sock_addr addr
;
1993 assert_single_node_only();
2000 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2001 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2005 if (rebalance_ip(ctdb
, &addr
) != 0) {
2006 DEBUG(DEBUG_ERR
,("Error when trying to reassign ip\n"));
2013 static int getips_store_callback(void *param
, void *data
)
2015 struct ctdb_public_ip
*node_ip
= (struct ctdb_public_ip
*)data
;
2016 struct ctdb_all_public_ips
*ips
= param
;
2020 ips
->ips
[i
].pnn
= node_ip
->pnn
;
2021 ips
->ips
[i
].addr
= node_ip
->addr
;
2025 static int getips_count_callback(void *param
, void *data
)
2027 uint32_t *count
= param
;
2034 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
2036 static uint32_t key
[IP_KEYLEN
];
2038 bzero(key
, sizeof(key
));
2040 switch (ip
->sa
.sa_family
) {
2042 key
[0] = ip
->ip
.sin_addr
.s_addr
;
2045 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
2053 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
2060 static void *add_ip_callback(void *parm
, void *data
)
2066 control_get_all_public_ips(struct ctdb_context
*ctdb
, TALLOC_CTX
*tmp_ctx
, struct ctdb_all_public_ips
**ips
)
2068 struct ctdb_all_public_ips
*tmp_ips
;
2069 struct ctdb_node_map
*nodemap
=NULL
;
2070 trbt_tree_t
*ip_tree
;
2074 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2076 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
2080 ip_tree
= trbt_create(tmp_ctx
, 0);
2082 for(i
=0;i
<nodemap
->num
;i
++){
2083 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DELETED
) {
2086 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2090 /* read the public ip list from this node */
2091 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &tmp_ips
);
2093 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", nodemap
->nodes
[i
].pnn
));
2097 for (j
=0; j
<tmp_ips
->num
;j
++) {
2098 struct ctdb_public_ip
*node_ip
;
2100 node_ip
= talloc(tmp_ctx
, struct ctdb_public_ip
);
2101 node_ip
->pnn
= tmp_ips
->ips
[j
].pnn
;
2102 node_ip
->addr
= tmp_ips
->ips
[j
].addr
;
2104 trbt_insertarray32_callback(ip_tree
,
2105 IP_KEYLEN
, ip_key(&tmp_ips
->ips
[j
].addr
),
2109 talloc_free(tmp_ips
);
2114 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &count
);
2116 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
2117 count
*sizeof(struct ctdb_public_ip
);
2118 tmp_ips
= talloc_zero_size(tmp_ctx
, len
);
2119 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_store_callback
, tmp_ips
);
2127 static void ctdb_every_second(struct event_context
*ev
, struct timed_event
*te
, struct timeval t
, void *p
)
2129 struct ctdb_context
*ctdb
= talloc_get_type(p
, struct ctdb_context
);
2131 event_add_timed(ctdb
->ev
, ctdb
,
2132 timeval_current_ofs(1, 0),
2133 ctdb_every_second
, ctdb
);
2136 struct srvid_reply_handler_data
{
2140 const char *srvid_str
;
2143 static void srvid_broadcast_reply_handler(struct ctdb_context
*ctdb
,
2148 struct srvid_reply_handler_data
*d
=
2149 (struct srvid_reply_handler_data
*)private_data
;
2153 if (data
.dsize
!= sizeof(ret
)) {
2154 DEBUG(DEBUG_ERR
, (__location__
" Wrong reply size\n"));
2158 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2159 ret
= *(int32_t *)data
.dptr
;
2162 ("%s failed with result %d\n", d
->srvid_str
, ret
));
2166 if (!d
->wait_for_all
) {
2171 /* Wait for all replies */
2173 for (i
= 0; i
< talloc_array_length(d
->nodes
); i
++) {
2174 if (d
->nodes
[i
] == ret
) {
2176 ("%s reply received from node %u\n",
2177 d
->srvid_str
, ret
));
2180 if (d
->nodes
[i
] != -1) {
2181 /* Found a node that hasn't yet replied */
2187 /* Broadcast the given SRVID to all connected nodes. Wait for 1 reply
2188 * or replies from all connected nodes. arg is the data argument to
2189 * pass in the srvid_request structure - pass 0 if this isn't needed.
2191 static int srvid_broadcast(struct ctdb_context
*ctdb
,
2192 uint64_t srvid
, uint32_t *arg
,
2193 const char *srvid_str
, bool wait_for_all
)
2198 uint64_t reply_srvid
;
2199 struct srvid_request request
;
2200 struct srvid_request_data request_data
;
2201 struct srvid_reply_handler_data reply_data
;
2204 ZERO_STRUCT(request
);
2206 /* Time ticks to enable timeouts to be processed */
2207 event_add_timed(ctdb
->ev
, ctdb
,
2208 timeval_current_ofs(1, 0),
2209 ctdb_every_second
, ctdb
);
2211 pnn
= ctdb_get_pnn(ctdb
);
2212 reply_srvid
= getpid();
2216 request
.srvid
= reply_srvid
;
2218 data
.dptr
= (uint8_t *)&request
;
2219 data
.dsize
= sizeof(request
);
2221 request_data
.pnn
= pnn
;
2222 request_data
.srvid
= reply_srvid
;
2223 request_data
.data
= *arg
;
2225 data
.dptr
= (uint8_t *)&request_data
;
2226 data
.dsize
= sizeof(request_data
);
2229 /* Register message port for reply from recovery master */
2230 ctdb_client_set_message_handler(ctdb
, reply_srvid
,
2231 srvid_broadcast_reply_handler
,
2234 reply_data
.wait_for_all
= wait_for_all
;
2235 reply_data
.nodes
= NULL
;
2236 reply_data
.srvid_str
= srvid_str
;
2239 reply_data
.done
= false;
2242 struct ctdb_node_map
*nodemap
;
2244 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
2245 CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
2248 ("Unable to get nodemap from current node, try again\n"));
2253 if (reply_data
.nodes
!= NULL
) {
2254 talloc_free(reply_data
.nodes
);
2256 reply_data
.nodes
= list_of_connected_nodes(ctdb
, nodemap
,
2259 talloc_free(nodemap
);
2262 /* Send to all connected nodes. Only recmaster replies */
2263 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
,
2266 /* This can only happen if the socket is closed and
2267 * there's no way to recover from that, so don't try
2271 ("Failed to send %s request to connected nodes\n",
2276 tv
= timeval_current();
2277 /* This loop terminates the reply is received */
2278 while (timeval_elapsed(&tv
) < 5.0 && !reply_data
.done
) {
2279 event_loop_once(ctdb
->ev
);
2282 if (!reply_data
.done
) {
2284 ("Still waiting for confirmation of %s\n", srvid_str
));
2289 ctdb_client_remove_message_handler(ctdb
, reply_srvid
, &reply_data
);
2291 talloc_free(reply_data
.nodes
);
2296 static int ipreallocate(struct ctdb_context
*ctdb
)
2298 return srvid_broadcast(ctdb
, CTDB_SRVID_TAKEOVER_RUN
, NULL
,
2299 "IP reallocation", false);
2303 static int control_ipreallocate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2305 return ipreallocate(ctdb
);
2309 add a public ip address to a node
2311 static int control_addip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2314 int len
, retries
= 0;
2316 ctdb_sock_addr addr
;
2317 struct ctdb_control_ip_iface
*pub
;
2318 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2319 struct ctdb_all_public_ips
*ips
;
2323 talloc_free(tmp_ctx
);
2327 if (!parse_ip_mask(argv
[0], argv
[1], &addr
, &mask
)) {
2328 DEBUG(DEBUG_ERR
, ("Badly formed ip/mask : %s\n", argv
[0]));
2329 talloc_free(tmp_ctx
);
2333 /* read the public ip list from the node */
2334 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2336 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %u\n", options
.pnn
));
2337 talloc_free(tmp_ctx
);
2340 for (i
=0;i
<ips
->num
;i
++) {
2341 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2342 DEBUG(DEBUG_ERR
,("Can not add ip to node. Node already hosts this ip\n"));
2349 /* Dont timeout. This command waits for an ip reallocation
2350 which sometimes can take wuite a while if there has
2351 been a recent recovery
2355 len
= offsetof(struct ctdb_control_ip_iface
, iface
) + strlen(argv
[1]) + 1;
2356 pub
= talloc_size(tmp_ctx
, len
);
2357 CTDB_NO_MEMORY(ctdb
, pub
);
2361 pub
->len
= strlen(argv
[1])+1;
2362 memcpy(&pub
->iface
[0], argv
[1], strlen(argv
[1])+1);
2365 ret
= ctdb_ctrl_add_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, pub
);
2367 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options
.pnn
));
2371 } while (retries
< 5 && ret
!= 0);
2373 DEBUG(DEBUG_ERR
, ("Unable to add public ip to node %u. Giving up.\n", options
.pnn
));
2374 talloc_free(tmp_ctx
);
2378 if (rebalance_node(ctdb
, options
.pnn
) != 0) {
2379 DEBUG(DEBUG_ERR
,("Error when trying to rebalance node\n"));
2383 talloc_free(tmp_ctx
);
2388 add a public ip address to a node
2390 static int control_ipiface(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2392 ctdb_sock_addr addr
;
2398 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2399 printf("Badly formed ip : %s\n", argv
[0]);
2403 printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr
));
2408 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
);
2410 static int control_delip_all(struct ctdb_context
*ctdb
, int argc
, const char **argv
, ctdb_sock_addr
*addr
)
2412 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2413 struct ctdb_node_map
*nodemap
=NULL
;
2414 struct ctdb_all_public_ips
*ips
;
2417 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, tmp_ctx
, &nodemap
);
2419 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from current node\n"));
2423 /* remove it from the nodes that are not hosting the ip currently */
2424 for(i
=0;i
<nodemap
->num
;i
++){
2425 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2428 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2430 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2434 for (j
=0;j
<ips
->num
;j
++) {
2435 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2443 if (ips
->ips
[j
].pnn
== nodemap
->nodes
[i
].pnn
) {
2447 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2448 control_delip(ctdb
, argc
, argv
);
2452 /* remove it from every node (also the one hosting it) */
2453 for(i
=0;i
<nodemap
->num
;i
++){
2454 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2457 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), nodemap
->nodes
[i
].pnn
, tmp_ctx
, &ips
);
2459 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from node %d\n", nodemap
->nodes
[i
].pnn
));
2463 for (j
=0;j
<ips
->num
;j
++) {
2464 if (ctdb_same_ip(addr
, &ips
->ips
[j
].addr
)) {
2472 options
.pnn
= nodemap
->nodes
[i
].pnn
;
2473 control_delip(ctdb
, argc
, argv
);
2476 talloc_free(tmp_ctx
);
2481 delete a public ip address from a node
2483 static int control_delip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2486 ctdb_sock_addr addr
;
2487 struct ctdb_control_ip_iface pub
;
2488 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2489 struct ctdb_all_public_ips
*ips
;
2492 talloc_free(tmp_ctx
);
2496 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
2497 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
2501 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2502 return control_delip_all(ctdb
, argc
, argv
, &addr
);
2509 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2511 DEBUG(DEBUG_ERR
, ("Unable to get public ip list from cluster\n"));
2512 talloc_free(tmp_ctx
);
2516 for (i
=0;i
<ips
->num
;i
++) {
2517 if (ctdb_same_ip(&addr
, &ips
->ips
[i
].addr
)) {
2523 DEBUG(DEBUG_ERR
, ("This node does not support this public address '%s'\n",
2524 ctdb_addr_to_str(&addr
)));
2525 talloc_free(tmp_ctx
);
2529 /* This is an optimisation. If this node is hosting the IP
2530 * then try to move it somewhere else without invoking a full
2531 * takeover run. We don't care if this doesn't work!
2533 if (ips
->ips
[i
].pnn
== options
.pnn
) {
2534 (void) try_moveip(ctdb
, &addr
, -1);
2537 ret
= ctdb_ctrl_del_public_ip(ctdb
, TIMELIMIT(), options
.pnn
, &pub
);
2539 DEBUG(DEBUG_ERR
, ("Unable to del public ip from node %u\n", options
.pnn
));
2540 talloc_free(tmp_ctx
);
2544 talloc_free(tmp_ctx
);
2548 static int kill_tcp_from_file(struct ctdb_context
*ctdb
,
2549 int argc
, const char **argv
)
2551 struct ctdb_control_killtcp
*killtcp
;
2552 int max_entries
, current
, i
;
2553 struct timeval timeout
;
2554 char line
[128], src
[128], dst
[128];
2557 struct client_async_data
*async_data
;
2558 struct ctdb_client_control_state
*state
;
2568 while (!feof(stdin
)) {
2569 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2573 /* Silently skip empty lines */
2574 if (line
[0] == '\n') {
2578 if (sscanf(line
, "%s %s\n", src
, dst
) != 2) {
2579 DEBUG(DEBUG_ERR
, ("Bad line [%d]: '%s'\n",
2581 talloc_free(killtcp
);
2585 if (current
>= max_entries
) {
2586 max_entries
+= 1024;
2587 killtcp
= talloc_realloc(ctdb
, killtcp
,
2588 struct ctdb_control_killtcp
,
2590 CTDB_NO_MEMORY(ctdb
, killtcp
);
2593 if (!parse_ip_port(src
, &killtcp
[current
].src_addr
)) {
2594 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2596 talloc_free(killtcp
);
2600 if (!parse_ip_port(dst
, &killtcp
[current
].dst_addr
)) {
2601 DEBUG(DEBUG_ERR
, ("Bad IP:port on line [%d]: '%s'\n",
2603 talloc_free(killtcp
);
2610 async_data
= talloc_zero(ctdb
, struct client_async_data
);
2611 if (async_data
== NULL
) {
2612 talloc_free(killtcp
);
2616 for (i
= 0; i
< current
; i
++) {
2618 data
.dsize
= sizeof(struct ctdb_control_killtcp
);
2619 data
.dptr
= (unsigned char *)&killtcp
[i
];
2621 timeout
= TIMELIMIT();
2622 state
= ctdb_control_send(ctdb
, options
.pnn
, 0,
2623 CTDB_CONTROL_KILL_TCP
, 0, data
,
2624 async_data
, &timeout
, NULL
);
2626 if (state
== NULL
) {
2628 ("Failed to call async killtcp control to node %u\n",
2630 talloc_free(killtcp
);
2634 ctdb_client_async_add(async_data
, state
);
2637 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2638 DEBUG(DEBUG_ERR
,("killtcp failed\n"));
2639 talloc_free(killtcp
);
2643 talloc_free(killtcp
);
2649 kill a tcp connection
2651 static int kill_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2654 struct ctdb_control_killtcp killtcp
;
2656 assert_single_node_only();
2659 return kill_tcp_from_file(ctdb
, argc
, argv
);
2666 if (!parse_ip_port(argv
[0], &killtcp
.src_addr
)) {
2667 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2671 if (!parse_ip_port(argv
[1], &killtcp
.dst_addr
)) {
2672 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2676 ret
= ctdb_ctrl_killtcp(ctdb
, TIMELIMIT(), options
.pnn
, &killtcp
);
2678 DEBUG(DEBUG_ERR
, ("Unable to killtcp from node %u\n", options
.pnn
));
2689 static int control_gratious_arp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2692 ctdb_sock_addr addr
;
2694 assert_single_node_only();
2700 if (!parse_ip(argv
[0], NULL
, 0, &addr
)) {
2701 DEBUG(DEBUG_ERR
, ("Bad IP '%s'\n", argv
[0]));
2705 ret
= ctdb_ctrl_gratious_arp(ctdb
, TIMELIMIT(), options
.pnn
, &addr
, argv
[1]);
2707 DEBUG(DEBUG_ERR
, ("Unable to send gratious_arp from node %u\n", options
.pnn
));
2715 register a server id
2717 static int regsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2720 struct ctdb_server_id server_id
;
2726 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2727 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2728 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2730 ret
= ctdb_ctrl_register_server_id(ctdb
, TIMELIMIT(), &server_id
);
2732 DEBUG(DEBUG_ERR
, ("Unable to register server_id from node %u\n", options
.pnn
));
2735 DEBUG(DEBUG_ERR
,("Srvid registered. Sleeping for 999 seconds\n"));
2741 unregister a server id
2743 static int unregsrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2746 struct ctdb_server_id server_id
;
2752 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2753 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2754 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2756 ret
= ctdb_ctrl_unregister_server_id(ctdb
, TIMELIMIT(), &server_id
);
2758 DEBUG(DEBUG_ERR
, ("Unable to unregister server_id from node %u\n", options
.pnn
));
2765 check if a server id exists
2767 static int chksrvid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2771 struct ctdb_server_id server_id
;
2777 server_id
.pnn
= strtoul(argv
[0], NULL
, 0);
2778 server_id
.type
= strtoul(argv
[1], NULL
, 0);
2779 server_id
.server_id
= strtoul(argv
[2], NULL
, 0);
2781 ret
= ctdb_ctrl_check_server_id(ctdb
, TIMELIMIT(), options
.pnn
, &server_id
, &status
);
2783 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n", options
.pnn
));
2788 printf("Server id %d:%d:%d EXISTS\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2790 printf("Server id %d:%d:%d does NOT exist\n", server_id
.pnn
, server_id
.type
, server_id
.server_id
);
2796 get a list of all server ids that are registered on a node
2798 static int getsrvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2801 struct ctdb_server_id_list
*server_ids
;
2803 ret
= ctdb_ctrl_get_server_id_list(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &server_ids
);
2805 DEBUG(DEBUG_ERR
, ("Unable to get server_id list from node %u\n", options
.pnn
));
2809 for (i
=0; i
<server_ids
->num
; i
++) {
2810 printf("Server id %d:%d:%d\n",
2811 server_ids
->server_ids
[i
].pnn
,
2812 server_ids
->server_ids
[i
].type
,
2813 server_ids
->server_ids
[i
].server_id
);
2820 check if a server id exists
2822 static int check_srvids(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2824 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
2830 talloc_free(tmp_ctx
);
2834 ids
= talloc_array(tmp_ctx
, uint64_t, argc
);
2835 result
= talloc_array(tmp_ctx
, uint8_t, argc
);
2837 for (i
= 0; i
< argc
; i
++) {
2838 ids
[i
] = strtoull(argv
[i
], NULL
, 0);
2841 if (!ctdb_client_check_message_handlers(ctdb
, ids
, argc
, result
)) {
2842 DEBUG(DEBUG_ERR
, ("Unable to check server_id from node %u\n",
2844 talloc_free(tmp_ctx
);
2848 for (i
=0; i
< argc
; i
++) {
2849 printf("Server id %d:%llu %s\n", options
.pnn
, (long long)ids
[i
],
2850 result
[i
] ? "exists" : "does not exist");
2853 talloc_free(tmp_ctx
);
2858 send a tcp tickle ack
2860 static int tickle_tcp(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2863 ctdb_sock_addr src
, dst
;
2869 if (!parse_ip_port(argv
[0], &src
)) {
2870 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[0]));
2874 if (!parse_ip_port(argv
[1], &dst
)) {
2875 DEBUG(DEBUG_ERR
, ("Bad IP:port '%s'\n", argv
[1]));
2879 ret
= ctdb_sys_send_tcp(&src
, &dst
, 0, 0, 0);
2883 DEBUG(DEBUG_ERR
, ("Error while sending tickle ack\n"));
2890 display public ip status
2892 static int control_ip(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
2895 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2896 struct ctdb_all_public_ips
*ips
;
2898 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2899 /* read the list of public ips from all nodes */
2900 ret
= control_get_all_public_ips(ctdb
, tmp_ctx
, &ips
);
2902 /* read the public ip list from this node */
2903 ret
= ctdb_ctrl_get_public_ips(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ips
);
2906 DEBUG(DEBUG_ERR
, ("Unable to get public ips from node %u\n", options
.pnn
));
2907 talloc_free(tmp_ctx
);
2911 if (options
.machinereadable
){
2912 printf(":Public IP:Node:");
2913 if (options
.verbose
){
2914 printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2918 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2919 printf("Public IPs on ALL nodes\n");
2921 printf("Public IPs on node %u\n", options
.pnn
);
2925 for (i
=1;i
<=ips
->num
;i
++) {
2926 struct ctdb_control_public_ip_info
*info
= NULL
;
2928 char *aciface
= NULL
;
2929 char *avifaces
= NULL
;
2930 char *cifaces
= NULL
;
2932 if (options
.pnn
== CTDB_BROADCAST_ALL
) {
2933 pnn
= ips
->ips
[ips
->num
-i
].pnn
;
2939 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), pnn
, ctdb
,
2940 &ips
->ips
[ips
->num
-i
].addr
, &info
);
2947 for (j
=0; j
< info
->num
; j
++) {
2948 if (cifaces
== NULL
) {
2949 cifaces
= talloc_strdup(info
,
2950 info
->ifaces
[j
].name
);
2952 cifaces
= talloc_asprintf_append(cifaces
,
2954 info
->ifaces
[j
].name
);
2957 if (info
->active_idx
== j
) {
2958 aciface
= info
->ifaces
[j
].name
;
2961 if (info
->ifaces
[j
].link_state
== 0) {
2965 if (avifaces
== NULL
) {
2966 avifaces
= talloc_strdup(info
, info
->ifaces
[j
].name
);
2968 avifaces
= talloc_asprintf_append(avifaces
,
2970 info
->ifaces
[j
].name
);
2975 if (options
.machinereadable
){
2977 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2978 ips
->ips
[ips
->num
-i
].pnn
);
2979 if (options
.verbose
){
2982 avifaces
?avifaces
:"",
2983 cifaces
?cifaces
:"");
2987 if (options
.verbose
) {
2988 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2989 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2990 ips
->ips
[ips
->num
-i
].pnn
,
2992 avifaces
?avifaces
:"",
2993 cifaces
?cifaces
:"");
2996 ctdb_addr_to_str(&ips
->ips
[ips
->num
-i
].addr
),
2997 ips
->ips
[ips
->num
-i
].pnn
);
3003 talloc_free(tmp_ctx
);
3010 static int control_ipinfo(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3013 ctdb_sock_addr addr
;
3014 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3015 struct ctdb_control_public_ip_info
*info
;
3018 talloc_free(tmp_ctx
);
3022 if (parse_ip(argv
[0], NULL
, 0, &addr
) == 0) {
3023 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s'\n", argv
[0]));
3027 /* read the public ip info from this node */
3028 ret
= ctdb_ctrl_get_public_ip_info(ctdb
, TIMELIMIT(), options
.pnn
,
3029 tmp_ctx
, &addr
, &info
);
3031 DEBUG(DEBUG_ERR
, ("Unable to get public ip[%s]info from node %u\n",
3032 argv
[0], options
.pnn
));
3033 talloc_free(tmp_ctx
);
3037 printf("Public IP[%s] info on node %u\n",
3038 ctdb_addr_to_str(&info
->ip
.addr
),
3041 printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
3042 ctdb_addr_to_str(&info
->ip
.addr
),
3043 info
->ip
.pnn
, info
->num
);
3045 for (i
=0; i
<info
->num
; i
++) {
3046 info
->ifaces
[i
].name
[CTDB_IFACE_SIZE
] = '\0';
3048 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
3049 i
+1, info
->ifaces
[i
].name
,
3050 info
->ifaces
[i
].link_state
?"up":"down",
3051 (unsigned int)info
->ifaces
[i
].references
,
3052 (i
==info
->active_idx
)?" (active)":"");
3055 talloc_free(tmp_ctx
);
3060 display interfaces status
3062 static int control_ifaces(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3064 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3066 struct ctdb_control_get_ifaces
*ifaces
;
3069 /* read the public ip list from this node */
3070 ret
= ctdb_ctrl_get_ifaces(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &ifaces
);
3072 DEBUG(DEBUG_ERR
, ("Unable to get interfaces from node %u\n",
3074 talloc_free(tmp_ctx
);
3078 if (options
.machinereadable
){
3079 printf(":Name:LinkStatus:References:\n");
3081 printf("Interfaces on node %u\n", options
.pnn
);
3084 for (i
=0; i
<ifaces
->num
; i
++) {
3085 if (options
.machinereadable
){
3086 printf(":%s:%s:%u\n",
3087 ifaces
->ifaces
[i
].name
,
3088 ifaces
->ifaces
[i
].link_state
?"1":"0",
3089 (unsigned int)ifaces
->ifaces
[i
].references
);
3091 printf("name:%s link:%s references:%u\n",
3092 ifaces
->ifaces
[i
].name
,
3093 ifaces
->ifaces
[i
].link_state
?"up":"down",
3094 (unsigned int)ifaces
->ifaces
[i
].references
);
3098 talloc_free(tmp_ctx
);
3104 set link status of an interface
3106 static int control_setifacelink(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3109 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3110 struct ctdb_control_iface_info info
;
3118 if (strlen(argv
[0]) > CTDB_IFACE_SIZE
) {
3119 DEBUG(DEBUG_ERR
, ("interfaces name '%s' too long\n",
3121 talloc_free(tmp_ctx
);
3124 strcpy(info
.name
, argv
[0]);
3126 if (strcmp(argv
[1], "up") == 0) {
3127 info
.link_state
= 1;
3128 } else if (strcmp(argv
[1], "down") == 0) {
3129 info
.link_state
= 0;
3131 DEBUG(DEBUG_ERR
, ("link state invalid '%s' should be 'up' or 'down'\n",
3133 talloc_free(tmp_ctx
);
3137 /* read the public ip list from this node */
3138 ret
= ctdb_ctrl_set_iface_link(ctdb
, TIMELIMIT(), options
.pnn
,
3141 DEBUG(DEBUG_ERR
, ("Unable to set link state for interfaces %s node %u\n",
3142 argv
[0], options
.pnn
));
3143 talloc_free(tmp_ctx
);
3147 talloc_free(tmp_ctx
);
3152 display pid of a ctdb daemon
3154 static int control_getpid(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3159 ret
= ctdb_ctrl_getpid(ctdb
, TIMELIMIT(), options
.pnn
, &pid
);
3161 DEBUG(DEBUG_ERR
, ("Unable to get daemon pid from node %u\n", options
.pnn
));
3164 printf("Pid:%d\n", pid
);
3169 typedef bool update_flags_handler_t(struct ctdb_context
*ctdb
, void *data
);
3171 static int update_flags_and_ipreallocate(struct ctdb_context
*ctdb
,
3173 update_flags_handler_t handler
,
3178 struct ctdb_node_map
*nodemap
= NULL
;
3182 /* Check if the node is already in the desired state */
3183 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3185 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3188 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3189 if (set_flag
== flag_is_set
) {
3190 DEBUG(DEBUG_NOTICE
, ("Node %d is %s %s\n", options
.pnn
,
3191 (set_flag
? "already" : "not"), desc
));
3196 if (!handler(ctdb
, data
)) {
3197 DEBUG(DEBUG_WARNING
,
3198 ("Failed to send control to set state %s on node %u, try again\n",
3199 desc
, options
.pnn
));
3204 /* Read the nodemap and verify the change took effect.
3205 * Even if the above control/hanlder timed out then it
3206 * could still have worked!
3208 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
,
3211 DEBUG(DEBUG_WARNING
,
3212 ("Unable to get nodemap from local node, try again\n"));
3214 flag_is_set
= nodemap
->nodes
[options
.pnn
].flags
& flag
;
3215 } while (nodemap
== NULL
|| (set_flag
!= flag_is_set
));
3217 return ipreallocate(ctdb
);
3220 /* Administratively disable a node */
3221 static bool update_flags_disabled(struct ctdb_context
*ctdb
, void *data
)
3225 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3226 NODE_FLAGS_PERMANENTLY_DISABLED
, 0);
3230 static int control_disable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3232 return update_flags_and_ipreallocate(ctdb
, NULL
,
3233 update_flags_disabled
,
3234 NODE_FLAGS_PERMANENTLY_DISABLED
,
3236 true /* set_flag*/);
3239 /* Administratively re-enable a node */
3240 static bool update_flags_not_disabled(struct ctdb_context
*ctdb
, void *data
)
3244 ret
= ctdb_ctrl_modflags(ctdb
, TIMELIMIT(), options
.pnn
,
3245 0, NODE_FLAGS_PERMANENTLY_DISABLED
);
3249 static int control_enable(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3251 return update_flags_and_ipreallocate(ctdb
, NULL
,
3252 update_flags_not_disabled
,
3253 NODE_FLAGS_PERMANENTLY_DISABLED
,
3255 false /* set_flag*/);
3259 static bool update_flags_stopped(struct ctdb_context
*ctdb
, void *data
)
3263 ret
= ctdb_ctrl_stop_node(ctdb
, TIMELIMIT(), options
.pnn
);
3268 static int control_stop(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3270 return update_flags_and_ipreallocate(ctdb
, NULL
,
3271 update_flags_stopped
,
3274 true /* set_flag*/);
3277 /* Continue a stopped node */
3278 static bool update_flags_not_stopped(struct ctdb_context
*ctdb
, void *data
)
3282 ret
= ctdb_ctrl_continue_node(ctdb
, TIMELIMIT(), options
.pnn
);
3287 static int control_continue(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3289 return update_flags_and_ipreallocate(ctdb
, NULL
,
3290 update_flags_not_stopped
,
3293 false /* set_flag */);
3296 static uint32_t get_generation(struct ctdb_context
*ctdb
)
3298 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3299 struct ctdb_vnn_map
*vnnmap
=NULL
;
3301 uint32_t generation
;
3303 /* wait until the recmaster is not in recovery mode */
3305 uint32_t recmode
, recmaster
;
3307 if (vnnmap
!= NULL
) {
3308 talloc_free(vnnmap
);
3312 /* get the recmaster */
3313 ret
= ctdb_ctrl_getrecmaster(ctdb
, tmp_ctx
, TIMELIMIT(), CTDB_CURRENT_NODE
, &recmaster
);
3315 DEBUG(DEBUG_ERR
, ("Unable to get recmaster from node %u\n", options
.pnn
));
3316 talloc_free(tmp_ctx
);
3320 /* get recovery mode */
3321 ret
= ctdb_ctrl_getrecmode(ctdb
, tmp_ctx
, TIMELIMIT(), recmaster
, &recmode
);
3323 DEBUG(DEBUG_ERR
, ("Unable to get recmode from node %u\n", options
.pnn
));
3324 talloc_free(tmp_ctx
);
3328 /* get the current generation number */
3329 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), recmaster
, tmp_ctx
, &vnnmap
);
3331 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from recmaster (%u)\n", recmaster
));
3332 talloc_free(tmp_ctx
);
3336 if ((recmode
== CTDB_RECOVERY_NORMAL
) && (vnnmap
->generation
!= 1)) {
3337 generation
= vnnmap
->generation
;
3338 talloc_free(tmp_ctx
);
3346 static bool update_state_banned(struct ctdb_context
*ctdb
, void *data
)
3348 struct ctdb_ban_time
*bantime
= (struct ctdb_ban_time
*)data
;
3351 ret
= ctdb_ctrl_set_ban(ctdb
, TIMELIMIT(), options
.pnn
, bantime
);
3356 static int control_ban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3358 struct ctdb_ban_time bantime
;
3364 bantime
.pnn
= options
.pnn
;
3365 bantime
.time
= strtoul(argv
[0], NULL
, 0);
3367 if (bantime
.time
== 0) {
3368 DEBUG(DEBUG_ERR
, ("Invalid ban time specified - must be >0\n"));
3372 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3373 update_state_banned
,
3376 true /* set_flag*/);
3381 static int control_unban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3383 struct ctdb_ban_time bantime
;
3385 bantime
.pnn
= options
.pnn
;
3388 return update_flags_and_ipreallocate(ctdb
, &bantime
,
3389 update_state_banned
,
3392 false /* set_flag*/);
3396 show ban information for a node
3398 static int control_showban(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3401 struct ctdb_node_map
*nodemap
=NULL
;
3402 struct ctdb_ban_time
*bantime
;
3404 /* verify the node exists */
3405 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
3407 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
3411 ret
= ctdb_ctrl_get_ban(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &bantime
);
3413 DEBUG(DEBUG_ERR
,("Showing ban info for node %d failed.\n", options
.pnn
));
3417 if (bantime
->time
== 0) {
3418 printf("Node %u is not banned\n", bantime
->pnn
);
3420 printf("Node %u is banned, %d seconds remaining\n",
3421 bantime
->pnn
, bantime
->time
);
3430 static int control_shutdown(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3434 ret
= ctdb_ctrl_shutdown(ctdb
, TIMELIMIT(), options
.pnn
);
3436 DEBUG(DEBUG_ERR
, ("Unable to shutdown node %u\n", options
.pnn
));
3446 static int control_recover(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3449 uint32_t generation
, next_generation
;
3452 /* "force" option ignores freeze failure and forces recovery */
3453 force
= (argc
== 1) && (strcasecmp(argv
[0], "force") == 0);
3455 /* record the current generation number */
3456 generation
= get_generation(ctdb
);
3458 ret
= ctdb_ctrl_freeze_priority(ctdb
, TIMELIMIT(), options
.pnn
, 1);
3461 DEBUG(DEBUG_ERR
, ("Unable to freeze node\n"));
3464 DEBUG(DEBUG_WARNING
, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3467 ret
= ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
3469 DEBUG(DEBUG_ERR
, ("Unable to set recovery mode\n"));
3473 /* wait until we are in a new generation */
3475 next_generation
= get_generation(ctdb
);
3476 if (next_generation
!= generation
) {
3487 display monitoring mode of a remote node
3489 static int control_getmonmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3494 ret
= ctdb_ctrl_getmonmode(ctdb
, TIMELIMIT(), options
.pnn
, &monmode
);
3496 DEBUG(DEBUG_ERR
, ("Unable to get monmode from node %u\n", options
.pnn
));
3499 if (!options
.machinereadable
){
3500 printf("Monitoring mode:%s (%d)\n",monmode
==CTDB_MONITORING_ACTIVE
?"ACTIVE":"DISABLED",monmode
);
3503 printf(":%d:\n",monmode
);
3510 display capabilities of a remote node
3512 static int control_getcapabilities(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3514 uint32_t capabilities
;
3517 ret
= ctdb_ctrl_getcapabilities(ctdb
, TIMELIMIT(), options
.pnn
, &capabilities
);
3519 DEBUG(DEBUG_ERR
, ("Unable to get capabilities from node %u\n", options
.pnn
));
3523 if (!options
.machinereadable
){
3524 printf("RECMASTER: %s\n", (capabilities
&CTDB_CAP_RECMASTER
)?"YES":"NO");
3525 printf("LMASTER: %s\n", (capabilities
&CTDB_CAP_LMASTER
)?"YES":"NO");
3526 printf("LVS: %s\n", (capabilities
&CTDB_CAP_LVS
)?"YES":"NO");
3527 printf("NATGW: %s\n", (capabilities
&CTDB_CAP_NATGW
)?"YES":"NO");
3529 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3530 printf(":%d:%d:%d:%d:\n",
3531 !!(capabilities
&CTDB_CAP_RECMASTER
),
3532 !!(capabilities
&CTDB_CAP_LMASTER
),
3533 !!(capabilities
&CTDB_CAP_LVS
),
3534 !!(capabilities
&CTDB_CAP_NATGW
));
3540 display lvs configuration
3542 static int control_lvs(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3544 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3545 struct ctdb_node_map
*orig_nodemap
=NULL
;
3546 struct ctdb_node_map
*nodemap
;
3548 int healthy_count
= 0;
3550 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3551 tmp_ctx
, &orig_nodemap
);
3553 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3554 talloc_free(tmp_ctx
);
3558 nodemap
= filter_nodemap_by_capabilities(ctdb
, orig_nodemap
,
3560 if (nodemap
== NULL
) {
3568 /* collect capabilities for all connected nodes */
3569 for (i
=0; i
<nodemap
->num
; i
++) {
3570 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3573 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3577 if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
)) {
3582 /* Print all LVS nodes */
3583 for (i
=0; i
<nodemap
->num
; i
++) {
3584 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3587 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3591 if (healthy_count
!= 0) {
3592 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
) {
3597 printf("%d:%s\n", nodemap
->nodes
[i
].pnn
,
3598 ctdb_addr_to_str(&nodemap
->nodes
[i
].addr
));
3602 talloc_free(tmp_ctx
);
3607 display who is the lvs master
3609 static int control_lvsmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3611 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3612 struct ctdb_node_map
*orig_nodemap
=NULL
;
3613 struct ctdb_node_map
*nodemap
;
3615 int healthy_count
= 0;
3617 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
,
3618 tmp_ctx
, &orig_nodemap
);
3620 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
3621 talloc_free(tmp_ctx
);
3625 nodemap
= filter_nodemap_by_capabilities(ctdb
, orig_nodemap
,
3627 if (nodemap
== NULL
) {
3633 /* collect capabilities for all connected nodes */
3634 for (i
=0; i
<nodemap
->num
; i
++) {
3635 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3638 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3642 if (!(nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
)) {
3649 /* find and show the lvsmaster */
3650 for (i
=0; i
<nodemap
->num
; i
++) {
3651 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
3654 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_PERMANENTLY_DISABLED
) {
3657 if (healthy_count
!= 0) {
3658 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_UNHEALTHY
) {
3663 if (options
.machinereadable
){
3664 printf("%d\n", nodemap
->nodes
[i
].pnn
);
3666 printf("Node %d is LVS master\n",
3667 nodemap
->nodes
[i
].pnn
);
3673 printf("There is no LVS master\n");
3675 talloc_free(tmp_ctx
);
3680 disable monitoring on a node
3682 static int control_disable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3687 ret
= ctdb_ctrl_disable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3689 DEBUG(DEBUG_ERR
, ("Unable to disable monmode on node %u\n", options
.pnn
));
3692 printf("Monitoring mode:%s\n","DISABLED");
3698 enable monitoring on a node
3700 static int control_enable_monmode(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3705 ret
= ctdb_ctrl_enable_monmode(ctdb
, TIMELIMIT(), options
.pnn
);
3707 DEBUG(DEBUG_ERR
, ("Unable to enable monmode on node %u\n", options
.pnn
));
3710 printf("Monitoring mode:%s\n","ACTIVE");
3716 display remote list of keys/data for a db
3718 static int control_catdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3720 const char *db_name
;
3721 struct ctdb_db_context
*ctdb_db
;
3723 struct ctdb_dump_db_context c
;
3730 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3734 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3735 if (ctdb_db
== NULL
) {
3736 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3740 if (options
.printlmaster
) {
3741 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
,
3742 ctdb
, &ctdb
->vnn_map
);
3744 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
3752 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3753 c
.printdatasize
= (bool)options
.printdatasize
;
3754 c
.printlmaster
= (bool)options
.printlmaster
;
3755 c
.printhash
= (bool)options
.printhash
;
3756 c
.printrecordflags
= (bool)options
.printrecordflags
;
3758 /* traverse and dump the cluster tdb */
3759 ret
= ctdb_dump_db(ctdb_db
, &c
);
3761 DEBUG(DEBUG_ERR
, ("Unable to dump database\n"));
3762 DEBUG(DEBUG_ERR
, ("Maybe try 'ctdb getdbstatus %s'"
3763 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3767 talloc_free(ctdb_db
);
3769 printf("Dumped %d records\n", ret
);
3773 struct cattdb_data
{
3774 struct ctdb_context
*ctdb
;
3778 static int cattdb_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private_data
)
3780 struct cattdb_data
*d
= private_data
;
3781 struct ctdb_dump_db_context c
;
3787 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
3788 c
.printdatasize
= (bool)options
.printdatasize
;
3789 c
.printlmaster
= false;
3790 c
.printhash
= (bool)options
.printhash
;
3791 c
.printrecordflags
= true;
3793 return ctdb_dumpdb_record(d
->ctdb
, key
, data
, &c
);
3797 cat the local tdb database using same format as catdb
3799 static int control_cattdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3801 const char *db_name
;
3802 struct ctdb_db_context
*ctdb_db
;
3803 struct cattdb_data d
;
3810 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3814 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3815 if (ctdb_db
== NULL
) {
3816 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3820 /* traverse the local tdb */
3823 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, cattdb_traverse
, &d
) == -1) {
3824 printf("Failed to cattdb data\n");
3827 talloc_free(ctdb_db
);
3829 printf("Dumped %d records\n", d
.count
);
3834 display the content of a database key
3836 static int control_readkey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3838 const char *db_name
;
3839 struct ctdb_db_context
*ctdb_db
;
3840 struct ctdb_record_handle
*h
;
3841 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3849 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3853 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3854 if (ctdb_db
== NULL
) {
3855 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3859 key
.dptr
= discard_const(argv
[1]);
3860 key
.dsize
= strlen((char *)key
.dptr
);
3862 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3864 printf("Failed to fetch record '%s' on node %d\n",
3865 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3866 talloc_free(tmp_ctx
);
3870 printf("Data: size:%d ptr:[%.*s]\n", (int)data
.dsize
, (int)data
.dsize
, data
.dptr
);
3872 talloc_free(ctdb_db
);
3873 talloc_free(tmp_ctx
);
3878 display the content of a database key
3880 static int control_writekey(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3882 const char *db_name
;
3883 struct ctdb_db_context
*ctdb_db
;
3884 struct ctdb_record_handle
*h
;
3885 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3893 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3897 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
3898 if (ctdb_db
== NULL
) {
3899 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3903 key
.dptr
= discard_const(argv
[1]);
3904 key
.dsize
= strlen((char *)key
.dptr
);
3906 h
= ctdb_fetch_lock(ctdb_db
, tmp_ctx
, key
, &data
);
3908 printf("Failed to fetch record '%s' on node %d\n",
3909 (const char *)key
.dptr
, ctdb_get_pnn(ctdb
));
3910 talloc_free(tmp_ctx
);
3914 data
.dptr
= discard_const(argv
[2]);
3915 data
.dsize
= strlen((char *)data
.dptr
);
3917 if (ctdb_record_store(h
, data
) != 0) {
3918 printf("Failed to store record\n");
3922 talloc_free(ctdb_db
);
3923 talloc_free(tmp_ctx
);
3928 fetch a record from a persistent database
3930 static int control_pfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
3932 const char *db_name
;
3933 struct ctdb_db_context
*ctdb_db
;
3934 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
3935 struct ctdb_transaction_handle
*h
;
3942 talloc_free(tmp_ctx
);
3946 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
3947 talloc_free(tmp_ctx
);
3951 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
3953 DEBUG(DEBUG_ERR
,("Database '%s' is not persistent\n", db_name
));
3954 talloc_free(tmp_ctx
);
3958 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
3959 if (ctdb_db
== NULL
) {
3960 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
3961 talloc_free(tmp_ctx
);
3965 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
3967 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
3968 talloc_free(tmp_ctx
);
3972 key
.dptr
= discard_const(argv
[1]);
3973 key
.dsize
= strlen(argv
[1]);
3974 ret
= ctdb_transaction_fetch(h
, tmp_ctx
, key
, &data
);
3976 DEBUG(DEBUG_ERR
,("Failed to fetch record\n"));
3977 talloc_free(tmp_ctx
);
3981 if (data
.dsize
== 0 || data
.dptr
== NULL
) {
3982 DEBUG(DEBUG_ERR
,("Record is empty\n"));
3983 talloc_free(tmp_ctx
);
3988 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
3990 DEBUG(DEBUG_ERR
,("Failed to open output file %s\n", argv
[2]));
3991 talloc_free(tmp_ctx
);
3994 write(fd
, data
.dptr
, data
.dsize
);
3997 write(1, data
.dptr
, data
.dsize
);
4000 /* abort the transaction */
4004 talloc_free(tmp_ctx
);
4009 fetch a record from a tdb-file
4011 static int control_tfetch(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4013 const char *tdb_file
;
4016 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4025 tdb
= tdb_open(tdb_file
, 0, 0, O_RDONLY
, 0);
4027 printf("Failed to open TDB file %s\n", tdb_file
);
4031 if (!strncmp(argv
[1], "0x", 2)) {
4032 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4033 if (key
.dsize
== 0) {
4034 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4038 key
.dptr
= discard_const(argv
[1]);
4039 key
.dsize
= strlen(argv
[1]);
4042 data
= tdb_fetch(tdb
, key
);
4043 if (data
.dptr
== NULL
|| data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
4044 printf("Failed to read record %s from tdb %s\n", argv
[1], tdb_file
);
4052 fd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
4054 printf("Failed to open output file %s\n", argv
[2]);
4057 if (options
.verbose
){
4058 write(fd
, data
.dptr
, data
.dsize
);
4060 write(fd
, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4064 if (options
.verbose
){
4065 write(1, data
.dptr
, data
.dsize
);
4067 write(1, data
.dptr
+sizeof(struct ctdb_ltdb_header
), data
.dsize
-sizeof(struct ctdb_ltdb_header
));
4071 talloc_free(tmp_ctx
);
4076 store a record and header to a tdb-file
4078 static int control_tstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4080 const char *tdb_file
;
4082 TDB_DATA key
, value
, data
;
4083 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
4084 struct ctdb_ltdb_header header
;
4092 tdb
= tdb_open(tdb_file
, 0, 0, O_RDWR
, 0);
4094 printf("Failed to open TDB file %s\n", tdb_file
);
4098 if (!strncmp(argv
[1], "0x", 2)) {
4099 key
= hextodata(tmp_ctx
, argv
[1] + 2);
4100 if (key
.dsize
== 0) {
4101 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[1]);
4105 key
.dptr
= discard_const(argv
[1]);
4106 key
.dsize
= strlen(argv
[1]);
4109 if (!strncmp(argv
[2], "0x", 2)) {
4110 value
= hextodata(tmp_ctx
, argv
[2] + 2);
4111 if (value
.dsize
== 0) {
4112 printf("Failed to convert \"%s\" into a TDB_DATA\n", argv
[2]);
4116 value
.dptr
= discard_const(argv
[2]);
4117 value
.dsize
= strlen(argv
[2]);
4120 ZERO_STRUCT(header
);
4122 header
.rsn
= atoll(argv
[3]);
4125 header
.dmaster
= atoi(argv
[4]);
4128 header
.flags
= atoi(argv
[5]);
4131 data
.dsize
= sizeof(struct ctdb_ltdb_header
) + value
.dsize
;
4132 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4133 if (data
.dptr
== NULL
) {
4134 printf("Failed to allocate header+value\n");
4138 *(struct ctdb_ltdb_header
*)data
.dptr
= header
;
4139 memcpy(data
.dptr
+ sizeof(struct ctdb_ltdb_header
), value
.dptr
, value
.dsize
);
4141 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0) {
4142 printf("Failed to write record %s to tdb %s\n", argv
[1], tdb_file
);
4149 talloc_free(tmp_ctx
);
4154 write a record to a persistent database
4156 static int control_pstore(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4158 const char *db_name
;
4159 struct ctdb_db_context
*ctdb_db
;
4160 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4161 struct ctdb_transaction_handle
*h
;
4167 talloc_free(tmp_ctx
);
4171 fd
= open(argv
[2], O_RDONLY
);
4173 DEBUG(DEBUG_ERR
,("Failed to open file containing record data : %s %s\n", argv
[2], strerror(errno
)));
4174 talloc_free(tmp_ctx
);
4178 ret
= fstat(fd
, &st
);
4180 DEBUG(DEBUG_ERR
,("fstat of file %s failed: %s\n", argv
[2], strerror(errno
)));
4182 talloc_free(tmp_ctx
);
4186 if (!S_ISREG(st
.st_mode
)) {
4187 DEBUG(DEBUG_ERR
,("Not a regular file %s\n", argv
[2]));
4189 talloc_free(tmp_ctx
);
4193 data
.dsize
= st
.st_size
;
4194 if (data
.dsize
== 0) {
4197 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
4198 if (data
.dptr
== NULL
) {
4199 DEBUG(DEBUG_ERR
,("Failed to talloc %d of memory to store record data\n", (int)data
.dsize
));
4201 talloc_free(tmp_ctx
);
4204 ret
= read(fd
, data
.dptr
, data
.dsize
);
4205 if (ret
!= data
.dsize
) {
4206 DEBUG(DEBUG_ERR
,("Failed to read %d bytes of record data\n", (int)data
.dsize
));
4208 talloc_free(tmp_ctx
);
4217 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4218 if (ctdb_db
== NULL
) {
4219 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4220 talloc_free(tmp_ctx
);
4224 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4226 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4227 talloc_free(tmp_ctx
);
4231 key
.dptr
= discard_const(argv
[1]);
4232 key
.dsize
= strlen(argv
[1]);
4233 ret
= ctdb_transaction_store(h
, key
, data
);
4235 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4236 talloc_free(tmp_ctx
);
4240 ret
= ctdb_transaction_commit(h
);
4242 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4243 talloc_free(tmp_ctx
);
4248 talloc_free(tmp_ctx
);
4253 * delete a record from a persistent database
4255 static int control_pdelete(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4257 const char *db_name
;
4258 struct ctdb_db_context
*ctdb_db
;
4259 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4260 struct ctdb_transaction_handle
*h
;
4267 talloc_free(tmp_ctx
);
4271 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
4272 talloc_free(tmp_ctx
);
4276 persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
4278 DEBUG(DEBUG_ERR
, ("Database '%s' is not persistent\n", db_name
));
4279 talloc_free(tmp_ctx
);
4283 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
4284 if (ctdb_db
== NULL
) {
4285 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n", db_name
));
4286 talloc_free(tmp_ctx
);
4290 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4292 DEBUG(DEBUG_ERR
, ("Failed to start transaction on database %s\n", db_name
));
4293 talloc_free(tmp_ctx
);
4297 key
.dptr
= discard_const(argv
[1]);
4298 key
.dsize
= strlen(argv
[1]);
4299 ret
= ctdb_transaction_store(h
, key
, tdb_null
);
4301 DEBUG(DEBUG_ERR
, ("Failed to delete record\n"));
4302 talloc_free(tmp_ctx
);
4306 ret
= ctdb_transaction_commit(h
);
4308 DEBUG(DEBUG_ERR
, ("Failed to commit transaction\n"));
4309 talloc_free(tmp_ctx
);
4313 talloc_free(tmp_ctx
);
4317 static const char *ptrans_parse_string(TALLOC_CTX
*mem_ctx
, const char *s
,
4322 const char *ret
; /* Next byte after successfully parsed value */
4324 /* Error, unless someone says otherwise */
4326 /* Indicates no value to parse */
4329 /* Skip whitespace */
4330 n
= strspn(s
, " \t");
4334 /* Quoted ASCII string - no wide characters! */
4336 n
= strcspn(t
, "\"");
4340 data
->dptr
= talloc_memdup(mem_ctx
, t
, n
);
4341 CTDB_NOMEM_ABORT(data
->dptr
);
4345 DEBUG(DEBUG_WARNING
,("Unmatched \" in input %s\n", s
));
4348 DEBUG(DEBUG_WARNING
,("Unsupported input format in %s\n", s
));
4354 static bool ptrans_get_key_value(TALLOC_CTX
*mem_ctx
, FILE *file
,
4355 TDB_DATA
*key
, TDB_DATA
*value
)
4357 char line
[1024]; /* FIXME: make this more flexible? */
4361 ptr
= fgets(line
, sizeof(line
), file
);
4368 t
= ptrans_parse_string(mem_ctx
, line
, key
);
4369 if (t
== NULL
|| key
->dptr
== NULL
) {
4370 /* Line Ignored but not EOF */
4375 t
= ptrans_parse_string(mem_ctx
, t
, value
);
4377 /* Line Ignored but not EOF */
4378 talloc_free(key
->dptr
);
4387 * Update a persistent database as per file/stdin
4389 static int control_ptrans(struct ctdb_context
*ctdb
,
4390 int argc
, const char **argv
)
4392 const char *db_name
;
4393 struct ctdb_db_context
*ctdb_db
;
4394 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4395 struct ctdb_transaction_handle
*h
;
4396 TDB_DATA key
, value
;
4401 talloc_free(tmp_ctx
);
4407 file
= fopen(argv
[1], "r");
4409 DEBUG(DEBUG_ERR
,("Unable to open file for reading '%s'\n", argv
[1]));
4410 talloc_free(tmp_ctx
);
4417 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, true, 0);
4418 if (ctdb_db
== NULL
) {
4419 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
4423 h
= ctdb_transaction_start(ctdb_db
, tmp_ctx
);
4425 DEBUG(DEBUG_ERR
,("Failed to start transaction on database %s\n", db_name
));
4429 while (ptrans_get_key_value(tmp_ctx
, file
, &key
, &value
)) {
4430 if (key
.dsize
!= 0) {
4431 ret
= ctdb_transaction_store(h
, key
, value
);
4432 /* Minimise memory use */
4433 talloc_free(key
.dptr
);
4434 if (value
.dptr
!= NULL
) {
4435 talloc_free(value
.dptr
);
4438 DEBUG(DEBUG_ERR
,("Failed to store record\n"));
4439 ctdb_transaction_cancel(h
);
4445 ret
= ctdb_transaction_commit(h
);
4447 DEBUG(DEBUG_ERR
,("Failed to commit transaction\n"));
4451 if (file
!= stdin
) {
4454 talloc_free(tmp_ctx
);
4458 if (file
!= stdin
) {
4462 talloc_free(tmp_ctx
);
4467 check if a service is bound to a port or not
4469 static int control_chktcpport(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4474 struct sockaddr_in sin
;
4477 printf("Use: ctdb chktcport <port>\n");
4481 port
= atoi(argv
[0]);
4483 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
4485 printf("Failed to open local socket\n");
4489 v
= fcntl(s
, F_GETFL
, 0);
4490 if (v
== -1 || fcntl(s
, F_SETFL
, v
| O_NONBLOCK
) != 0) {
4491 printf("Unable to set socket non-blocking: %s\n", strerror(errno
));
4494 bzero(&sin
, sizeof(sin
));
4495 sin
.sin_family
= PF_INET
;
4496 sin
.sin_port
= htons(port
);
4497 ret
= bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
));
4500 printf("Failed to bind to local socket: %d %s\n", errno
, strerror(errno
));
4509 static void log_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
4510 TDB_DATA data
, void *private_data
)
4512 DEBUG(DEBUG_ERR
,("Log data received\n"));
4513 if (data
.dsize
> 0) {
4514 printf("%s", data
.dptr
);
4521 display a list of log messages from the in memory ringbuffer
4523 static int control_getlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4527 struct ctdb_get_log_addr log_addr
;
4531 /* Process options */
4533 log_addr
.pnn
= ctdb_get_pnn(ctdb
);
4534 log_addr
.level
= DEBUG_NOTICE
;
4535 for (i
= 0; i
< argc
; i
++) {
4536 if (strcmp(argv
[i
], "recoverd") == 0) {
4537 main_daemon
= false;
4539 if (isalpha(argv
[i
][0]) || argv
[i
][0] == '-') {
4540 log_addr
.level
= get_debug_by_desc(argv
[i
]);
4542 log_addr
.level
= strtol(argv
[i
], NULL
, 0);
4547 /* Our message port is our PID */
4548 log_addr
.srvid
= getpid();
4550 data
.dptr
= (unsigned char *)&log_addr
;
4551 data
.dsize
= sizeof(log_addr
);
4553 DEBUG(DEBUG_ERR
, ("Pulling logs from node %u\n", options
.pnn
));
4555 ctdb_client_set_message_handler(ctdb
, log_addr
.srvid
, log_handler
, NULL
);
4558 DEBUG(DEBUG_ERR
,("Listen for response on %d\n", (int)log_addr
.srvid
));
4563 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4565 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_GET_LOG
,
4566 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4567 if (ret
!= 0 || res
!= 0) {
4568 DEBUG(DEBUG_ERR
,("Failed to get logs - %s\n", errmsg
));
4569 talloc_free(tmp_ctx
);
4572 talloc_free(tmp_ctx
);
4574 ret
= ctdb_client_send_message(ctdb
, options
.pnn
,
4575 CTDB_SRVID_GETLOG
, data
);
4577 DEBUG(DEBUG_ERR
,("Failed to send getlog request message to %u\n", options
.pnn
));
4582 tv
= timeval_current();
4583 /* this loop will terminate when we have received the reply */
4584 while (timeval_elapsed(&tv
) < (double)options
.timelimit
) {
4585 event_loop_once(ctdb
->ev
);
4588 DEBUG(DEBUG_INFO
,("Timed out waiting for log data.\n"));
4594 clear the in memory log area
4596 static int control_clearlog(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4600 if (argc
== 0 || (argc
>= 1 && strcmp(argv
[0], "recoverd") != 0)) {
4601 /* "recoverd" not given - get logs from main daemon */
4604 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4606 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_CLEAR_LOG
,
4607 0, tdb_null
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
4608 if (ret
!= 0 || res
!= 0) {
4609 DEBUG(DEBUG_ERR
,("Failed to clear logs\n"));
4610 talloc_free(tmp_ctx
);
4614 talloc_free(tmp_ctx
);
4616 TDB_DATA data
; /* unused in recoverd... */
4619 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_CLEARLOG
, data
);
4621 DEBUG(DEBUG_ERR
,("Failed to send clearlog request message to %u\n", options
.pnn
));
4629 /* Reload public IPs on a specified nodes */
4630 static int control_reloadips(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4632 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
4638 assert_single_node_only();
4644 /* Determine the nodes where IPs need to be reloaded */
4645 if (!parse_nodestring(ctdb
, tmp_ctx
, argc
== 1 ? argv
[0] : NULL
,
4646 options
.pnn
, true, &nodes
, &pnn_mode
)) {
4652 /* Disable takeover runs on all connected nodes. A reply
4653 * indicating success is needed from each node so all nodes
4654 * will need to be active. This will retry until maxruntime
4655 * is exceeded, hence no error handling.
4657 * A check could be added to not allow reloading of IPs when
4658 * there are disconnected nodes. However, this should
4659 * probably be left up to the administrator.
4661 timeout
= LONGTIMEOUT
;
4662 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4663 "Disable takeover runs", true);
4665 /* Now tell all the desired nodes to reload their public IPs.
4666 * Keep trying this until it succeeds. This assumes all
4667 * failures are transient, which might not be true...
4669 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_RELOAD_PUBLIC_IPS
,
4670 nodes
, 0, LONGTIMELIMIT(),
4672 NULL
, NULL
, NULL
) != 0) {
4674 ("Unable to reload IPs on some nodes, try again.\n"));
4678 /* It isn't strictly necessary to wait until takeover runs are
4679 * re-enabled but doing so can't hurt.
4682 srvid_broadcast(ctdb
, CTDB_SRVID_DISABLE_TAKEOVER_RUNS
, &timeout
,
4683 "Enable takeover runs", true);
4689 talloc_free(tmp_ctx
);
4694 display a list of the databases on a remote ctdb
4696 static int control_getdbmap(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4699 struct ctdb_dbid_map
*dbmap
=NULL
;
4701 ret
= ctdb_ctrl_getdbmap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &dbmap
);
4703 DEBUG(DEBUG_ERR
, ("Unable to get dbids from node %u\n", options
.pnn
));
4707 if(options
.machinereadable
){
4708 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4709 for(i
=0;i
<dbmap
->num
;i
++){
4717 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
,
4718 dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4719 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
,
4720 dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4721 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
4722 dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4723 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4724 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4725 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4726 printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4727 dbmap
->dbs
[i
].dbid
, name
, path
,
4728 !!(persistent
), !!(sticky
),
4729 !!(health
), !!(readonly
));
4734 printf("Number of databases:%d\n", dbmap
->num
);
4735 for(i
=0;i
<dbmap
->num
;i
++){
4743 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &path
);
4744 ctdb_ctrl_getdbname(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &name
);
4745 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, dbmap
->dbs
[i
].dbid
, ctdb
, &health
);
4746 persistent
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_PERSISTENT
;
4747 readonly
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_READONLY
;
4748 sticky
= dbmap
->dbs
[i
].flags
& CTDB_DB_FLAGS_STICKY
;
4749 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4750 dbmap
->dbs
[i
].dbid
, name
, path
,
4751 persistent
?" PERSISTENT":"",
4752 sticky
?" STICKY":"",
4753 readonly
?" READONLY":"",
4754 health
?" UNHEALTHY":"");
4761 display the status of a database on a remote ctdb
4763 static int control_getdbstatus(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4765 const char *db_name
;
4775 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
4779 ctdb_ctrl_getdbpath(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &path
);
4780 ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, ctdb
, &health
);
4781 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4782 db_id
, db_name
, path
,
4783 (flags
& CTDB_DB_FLAGS_PERSISTENT
? "yes" : "no"),
4784 (flags
& CTDB_DB_FLAGS_STICKY
? "yes" : "no"),
4785 (flags
& CTDB_DB_FLAGS_READONLY
? "yes" : "no"),
4786 (health
? health
: "OK"));
4792 check if the local node is recmaster or not
4793 it will return 1 if this node is the recmaster and 0 if it is not
4794 or if the local ctdb daemon could not be contacted
4796 static int control_isnotrecmaster(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4798 uint32_t mypnn
, recmaster
;
4801 assert_single_node_only();
4803 mypnn
= getpnn(ctdb
);
4805 ret
= ctdb_ctrl_getrecmaster(ctdb
, ctdb
, TIMELIMIT(), options
.pnn
, &recmaster
);
4807 printf("Failed to get the recmaster\n");
4811 if (recmaster
!= mypnn
) {
4812 printf("this node is not the recmaster\n");
4816 printf("this node is the recmaster\n");
4823 static int control_ping(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4826 struct timeval tv
= timeval_current();
4827 ret
= ctdb_ctrl_ping(ctdb
, options
.pnn
);
4829 printf("Unable to get ping response from node %u\n", options
.pnn
);
4832 printf("response from %u time=%.6f sec (%d clients)\n",
4833 options
.pnn
, timeval_elapsed(&tv
), ret
);
4840 get a node's runstate
4842 static int control_runstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4845 enum ctdb_runstate runstate
;
4847 ret
= ctdb_ctrl_get_runstate(ctdb
, TIMELIMIT(), options
.pnn
, &runstate
);
4849 printf("Unable to get runstate response from node %u\n",
4854 enum ctdb_runstate t
;
4856 for (i
=0; i
<argc
; i
++) {
4858 t
= runstate_from_string(argv
[i
]);
4859 if (t
== CTDB_RUNSTATE_UNKNOWN
) {
4860 printf("Invalid run state (%s)\n", argv
[i
]);
4864 if (t
== runstate
) {
4871 printf("CTDB not in required run state (got %s)\n",
4872 runstate_to_string((enum ctdb_runstate
)runstate
));
4877 printf("%s\n", runstate_to_string(runstate
));
4885 static int control_getvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4896 ret
= ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, &value
);
4898 DEBUG(DEBUG_ERR
, ("Unable to get tunable variable '%s'\n", name
));
4902 printf("%-23s = %u\n", name
, value
);
4909 static int control_setvar(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4920 value
= strtoul(argv
[1], NULL
, 0);
4922 ret
= ctdb_ctrl_set_tunable(ctdb
, TIMELIMIT(), options
.pnn
, name
, value
);
4924 DEBUG(DEBUG_ERR
, ("Unable to set tunable variable '%s'\n", name
));
4933 static int control_listvars(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4939 ret
= ctdb_ctrl_list_tunables(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &list
, &count
);
4941 DEBUG(DEBUG_ERR
, ("Unable to list tunable variables\n"));
4945 for (i
=0;i
<count
;i
++) {
4946 control_getvar(ctdb
, 1, &list
[i
]);
4955 display debug level on a node
4957 static int control_getdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4962 ret
= ctdb_ctrl_get_debuglevel(ctdb
, options
.pnn
, &level
);
4964 DEBUG(DEBUG_ERR
, ("Unable to get debuglevel response from node %u\n", options
.pnn
));
4967 if (options
.machinereadable
){
4968 printf(":Name:Level:\n");
4969 printf(":%s:%d:\n",get_debug_by_level(level
),level
);
4971 printf("Node %u is at debug level %s (%d)\n", options
.pnn
, get_debug_by_level(level
), level
);
4978 display reclock file of a node
4980 static int control_getreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
4983 const char *reclock
;
4985 ret
= ctdb_ctrl_getreclock(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &reclock
);
4987 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
4990 if (options
.machinereadable
){
4991 if (reclock
!= NULL
) {
4992 printf("%s", reclock
);
4995 if (reclock
== NULL
) {
4996 printf("No reclock file used.\n");
4998 printf("Reclock file:%s\n", reclock
);
5006 set the reclock file of a node
5008 static int control_setreclock(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5011 const char *reclock
;
5015 } else if (argc
== 1) {
5021 ret
= ctdb_ctrl_setreclock(ctdb
, TIMELIMIT(), options
.pnn
, reclock
);
5023 DEBUG(DEBUG_ERR
, ("Unable to get reclock file from node %u\n", options
.pnn
));
5030 set the natgw state on/off
5032 static int control_setnatgwstate(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5035 uint32_t natgwstate
;
5041 if (!strcmp(argv
[0], "on")) {
5043 } else if (!strcmp(argv
[0], "off")) {
5049 ret
= ctdb_ctrl_setnatgwstate(ctdb
, TIMELIMIT(), options
.pnn
, natgwstate
);
5051 DEBUG(DEBUG_ERR
, ("Unable to set the natgw state for node %u\n", options
.pnn
));
5059 set the lmaster role on/off
5061 static int control_setlmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5064 uint32_t lmasterrole
;
5070 if (!strcmp(argv
[0], "on")) {
5072 } else if (!strcmp(argv
[0], "off")) {
5078 ret
= ctdb_ctrl_setlmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, lmasterrole
);
5080 DEBUG(DEBUG_ERR
, ("Unable to set the lmaster role for node %u\n", options
.pnn
));
5088 set the recmaster role on/off
5090 static int control_setrecmasterrole(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5093 uint32_t recmasterrole
;
5099 if (!strcmp(argv
[0], "on")) {
5101 } else if (!strcmp(argv
[0], "off")) {
5107 ret
= ctdb_ctrl_setrecmasterrole(ctdb
, TIMELIMIT(), options
.pnn
, recmasterrole
);
5109 DEBUG(DEBUG_ERR
, ("Unable to set the recmaster role for node %u\n", options
.pnn
));
5117 set debug level on a node or all nodes
5119 static int control_setdebug(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5125 printf("You must specify the debug level. Valid levels are:\n");
5126 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5127 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
5133 if (isalpha(argv
[0][0]) || argv
[0][0] == '-') {
5134 level
= get_debug_by_desc(argv
[0]);
5136 level
= strtol(argv
[0], NULL
, 0);
5139 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5140 if (level
== debug_levels
[i
].level
) {
5144 if (debug_levels
[i
].description
== NULL
) {
5145 printf("Invalid debug level, must be one of\n");
5146 for (i
=0; debug_levels
[i
].description
!= NULL
; i
++) {
5147 printf("%s (%d)\n", debug_levels
[i
].description
, debug_levels
[i
].level
);
5152 ret
= ctdb_ctrl_set_debuglevel(ctdb
, options
.pnn
, level
);
5154 DEBUG(DEBUG_ERR
, ("Unable to set debug level on node %u\n", options
.pnn
));
5163 static int control_thaw(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5169 priority
= strtol(argv
[0], NULL
, 0);
5173 DEBUG(DEBUG_ERR
,("Thaw by priority %u\n", priority
));
5175 ret
= ctdb_ctrl_thaw_priority(ctdb
, TIMELIMIT(), options
.pnn
, priority
);
5177 DEBUG(DEBUG_ERR
, ("Unable to thaw node %u\n", options
.pnn
));
5184 attach to a database
5186 static int control_attach(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5188 const char *db_name
;
5189 struct ctdb_db_context
*ctdb_db
;
5190 bool persistent
= false;
5200 if (strcmp(argv
[1], "persistent") != 0) {
5206 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, persistent
, 0);
5207 if (ctdb_db
== NULL
) {
5208 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", db_name
));
5218 static int control_setdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5220 struct ctdb_db_priority db_prio
;
5227 db_prio
.db_id
= strtoul(argv
[0], NULL
, 0);
5228 db_prio
.priority
= strtoul(argv
[1], NULL
, 0);
5230 ret
= ctdb_ctrl_set_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, &db_prio
);
5232 DEBUG(DEBUG_ERR
,("Unable to set db prio\n"));
5242 static int control_getdbprio(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5244 uint32_t db_id
, priority
;
5251 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5255 ret
= ctdb_ctrl_get_db_priority(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &priority
);
5257 DEBUG(DEBUG_ERR
,("Unable to get db prio\n"));
5261 DEBUG(DEBUG_ERR
,("Priority:%u\n", priority
));
5267 set the sticky records capability for a database
5269 static int control_setdbsticky(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5271 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5279 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5283 ret
= ctdb_ctrl_set_db_sticky(ctdb
, options
.pnn
, db_id
);
5285 DEBUG(DEBUG_ERR
,("Unable to set db to support sticky records\n"));
5286 talloc_free(tmp_ctx
);
5290 talloc_free(tmp_ctx
);
5295 set the readonly capability for a database
5297 static int control_setdbreadonly(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5299 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5307 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5311 ret
= ctdb_ctrl_set_db_readonly(ctdb
, options
.pnn
, db_id
);
5313 DEBUG(DEBUG_ERR
,("Unable to set db to support readonly\n"));
5314 talloc_free(tmp_ctx
);
5318 talloc_free(tmp_ctx
);
5325 static int control_getdbseqnum(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5335 if (!db_exists(ctdb
, argv
[0], &db_id
, NULL
, NULL
)) {
5339 ret
= ctdb_ctrl_getdbseqnum(ctdb
, TIMELIMIT(), options
.pnn
, db_id
, &seqnum
);
5341 DEBUG(DEBUG_ERR
, ("Unable to get seqnum from node."));
5345 printf("Sequence number:%lld\n", (long long)seqnum
);
5351 run an eventscript on a node
5353 static int control_eventscript(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5359 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5362 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5366 data
.dptr
= (unsigned char *)discard_const(argv
[0]);
5367 data
.dsize
= strlen((char *)data
.dptr
) + 1;
5369 DEBUG(DEBUG_ERR
, ("Running eventscripts with arguments \"%s\" on node %u\n", data
.dptr
, options
.pnn
));
5371 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS
,
5372 0, data
, tmp_ctx
, NULL
, &res
, NULL
, &errmsg
);
5373 if (ret
!= 0 || res
!= 0) {
5374 DEBUG(DEBUG_ERR
,("Failed to run eventscripts - %s\n", errmsg
));
5375 talloc_free(tmp_ctx
);
5378 talloc_free(tmp_ctx
);
5382 #define DB_VERSION 1
5383 #define MAX_DB_NAME 64
5384 struct db_file_header
{
5385 unsigned long version
;
5387 unsigned long persistent
;
5389 const char name
[MAX_DB_NAME
];
5392 struct backup_data
{
5393 struct ctdb_marshall_buffer
*records
;
5396 bool traverse_error
;
5399 static int backup_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *private)
5401 struct backup_data
*bd
= talloc_get_type(private, struct backup_data
);
5402 struct ctdb_rec_data
*rec
;
5404 /* add the record */
5405 rec
= ctdb_marshall_record(bd
->records
, 0, key
, NULL
, data
);
5407 bd
->traverse_error
= true;
5408 DEBUG(DEBUG_ERR
,("Failed to marshall record\n"));
5411 bd
->records
= talloc_realloc_size(NULL
, bd
->records
, rec
->length
+ bd
->len
);
5412 if (bd
->records
== NULL
) {
5413 DEBUG(DEBUG_ERR
,("Failed to expand marshalling buffer\n"));
5414 bd
->traverse_error
= true;
5417 bd
->records
->count
++;
5418 memcpy(bd
->len
+(uint8_t *)bd
->records
, rec
, rec
->length
);
5419 bd
->len
+= rec
->length
;
5427 * backup a database to a file
5429 static int control_backupdb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5431 const char *db_name
;
5433 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5434 struct db_file_header dbhdr
;
5435 struct ctdb_db_context
*ctdb_db
;
5436 struct backup_data
*bd
;
5439 const char *reason
= NULL
;
5443 assert_single_node_only();
5446 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5450 if (!db_exists(ctdb
, argv
[0], &db_id
, &db_name
, &flags
)) {
5454 ret
= ctdb_ctrl_getdbhealth(ctdb
, TIMELIMIT(), options
.pnn
,
5455 db_id
, tmp_ctx
, &reason
);
5457 DEBUG(DEBUG_ERR
,("Unable to get dbhealth for database '%s'\n",
5459 talloc_free(tmp_ctx
);
5463 uint32_t allow_unhealthy
= 0;
5465 ctdb_ctrl_get_tunable(ctdb
, TIMELIMIT(), options
.pnn
,
5466 "AllowUnhealthyDBRead",
5469 if (allow_unhealthy
!= 1) {
5470 DEBUG(DEBUG_ERR
,("database '%s' is unhealthy: %s\n",
5473 DEBUG(DEBUG_ERR
,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5475 talloc_free(tmp_ctx
);
5479 DEBUG(DEBUG_WARNING
,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5481 DEBUG(DEBUG_WARNING
,("WARNING! allow backup of unhealthy database: "
5482 "tunnable AllowUnhealthyDBRead = %u\n",
5486 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5487 if (ctdb_db
== NULL
) {
5488 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", argv
[0]));
5489 talloc_free(tmp_ctx
);
5494 ret
= tdb_transaction_start(ctdb_db
->ltdb
->tdb
);
5496 DEBUG(DEBUG_ERR
,("Failed to start transaction\n"));
5497 talloc_free(tmp_ctx
);
5502 bd
= talloc_zero(tmp_ctx
, struct backup_data
);
5504 DEBUG(DEBUG_ERR
,("Failed to allocate backup_data\n"));
5505 talloc_free(tmp_ctx
);
5509 bd
->records
= talloc_zero(bd
, struct ctdb_marshall_buffer
);
5510 if (bd
->records
== NULL
) {
5511 DEBUG(DEBUG_ERR
,("Failed to allocate ctdb_marshall_buffer\n"));
5512 talloc_free(tmp_ctx
);
5516 bd
->len
= offsetof(struct ctdb_marshall_buffer
, data
);
5517 bd
->records
->db_id
= ctdb_db
->db_id
;
5518 /* traverse the database collecting all records */
5519 if (tdb_traverse_read(ctdb_db
->ltdb
->tdb
, backup_traverse
, bd
) == -1 ||
5520 bd
->traverse_error
) {
5521 DEBUG(DEBUG_ERR
,("Traverse error\n"));
5522 talloc_free(tmp_ctx
);
5526 tdb_transaction_cancel(ctdb_db
->ltdb
->tdb
);
5529 fh
= open(argv
[1], O_RDWR
|O_CREAT
, 0600);
5531 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[1]));
5532 talloc_free(tmp_ctx
);
5537 dbhdr
.version
= DB_VERSION
;
5538 dbhdr
.timestamp
= time(NULL
);
5539 dbhdr
.persistent
= flags
& CTDB_DB_FLAGS_PERSISTENT
;
5540 dbhdr
.size
= bd
->len
;
5541 if (strlen(argv
[0]) >= MAX_DB_NAME
) {
5542 DEBUG(DEBUG_ERR
,("Too long dbname\n"));
5545 strncpy(discard_const(dbhdr
.name
), argv
[0], MAX_DB_NAME
-1);
5546 ret
= write(fh
, &dbhdr
, sizeof(dbhdr
));
5548 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5551 ret
= write(fh
, bd
->records
, bd
->len
);
5553 DEBUG(DEBUG_ERR
,("write failed: %s\n", strerror(errno
)));
5562 DEBUG(DEBUG_ERR
,("close failed: %s\n", strerror(errno
)));
5566 DEBUG(DEBUG_ERR
,("Database backed up to %s\n", argv
[1]));
5568 talloc_free(tmp_ctx
);
5573 * restore a database from a file
5575 static int control_restoredb(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5578 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5581 struct db_file_header dbhdr
;
5582 struct ctdb_db_context
*ctdb_db
;
5583 struct ctdb_node_map
*nodemap
=NULL
;
5584 struct ctdb_vnn_map
*vnnmap
=NULL
;
5586 struct ctdb_control_wipe_database w
;
5588 uint32_t generation
;
5593 assert_single_node_only();
5595 if (argc
< 1 || argc
> 2) {
5596 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5600 fh
= open(argv
[0], O_RDONLY
);
5602 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5603 talloc_free(tmp_ctx
);
5607 read(fh
, &dbhdr
, sizeof(dbhdr
));
5608 if (dbhdr
.version
!= DB_VERSION
) {
5609 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5611 talloc_free(tmp_ctx
);
5615 dbname
= discard_const(dbhdr
.name
);
5617 dbname
= discard_const(argv
[1]);
5620 outdata
.dsize
= dbhdr
.size
;
5621 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5622 if (outdata
.dptr
== NULL
) {
5623 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5625 talloc_free(tmp_ctx
);
5628 read(fh
, outdata
.dptr
, outdata
.dsize
);
5631 tm
= localtime(&dbhdr
.timestamp
);
5632 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5633 printf("Restoring database '%s' from backup @ %s\n",
5637 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), dbname
, dbhdr
.persistent
, 0);
5638 if (ctdb_db
== NULL
) {
5639 DEBUG(DEBUG_ERR
,("Unable to attach to database '%s'\n", dbname
));
5640 talloc_free(tmp_ctx
);
5644 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
, &nodemap
);
5646 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n", options
.pnn
));
5647 talloc_free(tmp_ctx
);
5652 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
, &vnnmap
);
5654 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n", options
.pnn
));
5655 talloc_free(tmp_ctx
);
5659 /* freeze all nodes */
5660 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5661 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5662 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5668 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5669 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5670 talloc_free(tmp_ctx
);
5675 generation
= vnnmap
->generation
;
5676 data
.dptr
= (void *)&generation
;
5677 data
.dsize
= sizeof(generation
);
5679 /* start a cluster wide transaction */
5680 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5681 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5683 TIMELIMIT(), false, data
,
5686 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide transactions.\n"));
5691 w
.db_id
= ctdb_db
->db_id
;
5692 w
.transaction_id
= generation
;
5694 data
.dptr
= (void *)&w
;
5695 data
.dsize
= sizeof(w
);
5697 /* wipe all the remote databases. */
5698 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5699 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5701 TIMELIMIT(), false, data
,
5704 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5705 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5706 talloc_free(tmp_ctx
);
5710 /* push the database */
5711 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5712 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_PUSH_DB
,
5714 TIMELIMIT(), false, outdata
,
5717 DEBUG(DEBUG_ERR
, ("Failed to push database.\n"));
5718 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5719 talloc_free(tmp_ctx
);
5723 data
.dptr
= (void *)&ctdb_db
->db_id
;
5724 data
.dsize
= sizeof(ctdb_db
->db_id
);
5726 /* mark the database as healthy */
5727 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5728 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5730 TIMELIMIT(), false, data
,
5733 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5734 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5735 talloc_free(tmp_ctx
);
5739 data
.dptr
= (void *)&generation
;
5740 data
.dsize
= sizeof(generation
);
5742 /* commit all the changes */
5743 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5745 TIMELIMIT(), false, data
,
5748 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5749 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5750 talloc_free(tmp_ctx
);
5755 /* thaw all nodes */
5756 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5757 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
5763 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
5764 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5765 talloc_free(tmp_ctx
);
5770 talloc_free(tmp_ctx
);
5775 * dump a database backup from a file
5777 static int control_dumpdbbackup(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
5779 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5781 struct db_file_header dbhdr
;
5785 struct ctdb_rec_data
*rec
= NULL
;
5786 struct ctdb_marshall_buffer
*m
;
5787 struct ctdb_dump_db_context c
;
5789 assert_single_node_only();
5792 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5796 fh
= open(argv
[0], O_RDONLY
);
5798 DEBUG(DEBUG_ERR
,("Failed to open file '%s'\n", argv
[0]));
5799 talloc_free(tmp_ctx
);
5803 read(fh
, &dbhdr
, sizeof(dbhdr
));
5804 if (dbhdr
.version
!= DB_VERSION
) {
5805 DEBUG(DEBUG_ERR
,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr
.version
, DB_VERSION
));
5807 talloc_free(tmp_ctx
);
5811 outdata
.dsize
= dbhdr
.size
;
5812 outdata
.dptr
= talloc_size(tmp_ctx
, outdata
.dsize
);
5813 if (outdata
.dptr
== NULL
) {
5814 DEBUG(DEBUG_ERR
,("Failed to allocate data of size '%lu'\n", dbhdr
.size
));
5816 talloc_free(tmp_ctx
);
5819 read(fh
, outdata
.dptr
, outdata
.dsize
);
5821 m
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
5823 tm
= localtime(&dbhdr
.timestamp
);
5824 strftime(tbuf
,sizeof(tbuf
)-1,"%Y/%m/%d %H:%M:%S", tm
);
5825 printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5826 dbhdr
.name
, m
->db_id
, tbuf
);
5830 c
.printemptyrecords
= (bool)options
.printemptyrecords
;
5831 c
.printdatasize
= (bool)options
.printdatasize
;
5832 c
.printlmaster
= false;
5833 c
.printhash
= (bool)options
.printhash
;
5834 c
.printrecordflags
= (bool)options
.printrecordflags
;
5836 for (i
=0; i
< m
->count
; i
++) {
5840 /* we do not want the header splitted, so we pass NULL*/
5841 rec
= ctdb_marshall_loop_next(m
, rec
, &reqid
,
5844 ctdb_dumpdb_record(ctdb
, key
, data
, &c
);
5847 printf("Dumped %d records\n", i
);
5848 talloc_free(tmp_ctx
);
5853 * wipe a database from a file
5855 static int control_wipedb(struct ctdb_context
*ctdb
, int argc
,
5858 const char *db_name
;
5860 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
5862 struct ctdb_db_context
*ctdb_db
;
5863 struct ctdb_node_map
*nodemap
= NULL
;
5864 struct ctdb_vnn_map
*vnnmap
= NULL
;
5866 struct ctdb_control_wipe_database w
;
5868 uint32_t generation
;
5871 assert_single_node_only();
5874 DEBUG(DEBUG_ERR
,("Invalid arguments\n"));
5878 if (!db_exists(ctdb
, argv
[0], NULL
, &db_name
, &flags
)) {
5882 ctdb_db
= ctdb_attach(ctdb
, TIMELIMIT(), db_name
, flags
& CTDB_DB_FLAGS_PERSISTENT
, 0);
5883 if (ctdb_db
== NULL
) {
5884 DEBUG(DEBUG_ERR
, ("Unable to attach to database '%s'\n",
5886 talloc_free(tmp_ctx
);
5890 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), options
.pnn
, ctdb
,
5893 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from node %u\n",
5895 talloc_free(tmp_ctx
);
5899 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), options
.pnn
, tmp_ctx
,
5902 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from node %u\n",
5904 talloc_free(tmp_ctx
);
5908 /* freeze all nodes */
5909 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5910 for (i
=1; i
<=NUM_DB_PRIORITIES
; i
++) {
5911 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_FREEZE
,
5918 DEBUG(DEBUG_ERR
, ("Unable to freeze nodes.\n"));
5919 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
,
5920 CTDB_RECOVERY_ACTIVE
);
5921 talloc_free(tmp_ctx
);
5926 generation
= vnnmap
->generation
;
5927 data
.dptr
= (void *)&generation
;
5928 data
.dsize
= sizeof(generation
);
5930 /* start a cluster wide transaction */
5931 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5932 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_START
,
5934 TIMELIMIT(), false, data
,
5938 DEBUG(DEBUG_ERR
, ("Unable to start cluster wide "
5939 "transactions.\n"));
5943 w
.db_id
= ctdb_db
->db_id
;
5944 w
.transaction_id
= generation
;
5946 data
.dptr
= (void *)&w
;
5947 data
.dsize
= sizeof(w
);
5949 /* wipe all the remote databases. */
5950 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5951 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_WIPE_DATABASE
,
5953 TIMELIMIT(), false, data
,
5956 DEBUG(DEBUG_ERR
, ("Unable to wipe database.\n"));
5957 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5958 talloc_free(tmp_ctx
);
5962 data
.dptr
= (void *)&ctdb_db
->db_id
;
5963 data
.dsize
= sizeof(ctdb_db
->db_id
);
5965 /* mark the database as healthy */
5966 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5967 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_DB_SET_HEALTHY
,
5969 TIMELIMIT(), false, data
,
5972 DEBUG(DEBUG_ERR
, ("Failed to mark database as healthy.\n"));
5973 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5974 talloc_free(tmp_ctx
);
5978 data
.dptr
= (void *)&generation
;
5979 data
.dsize
= sizeof(generation
);
5981 /* commit all the changes */
5982 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_TRANSACTION_COMMIT
,
5984 TIMELIMIT(), false, data
,
5987 DEBUG(DEBUG_ERR
, ("Unable to commit databases.\n"));
5988 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
5989 talloc_free(tmp_ctx
);
5993 /* thaw all nodes */
5994 nodes
= list_of_active_nodes(ctdb
, nodemap
, tmp_ctx
, true);
5995 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_THAW
,
6001 DEBUG(DEBUG_ERR
, ("Unable to thaw nodes.\n"));
6002 ctdb_ctrl_setrecmode(ctdb
, TIMELIMIT(), options
.pnn
, CTDB_RECOVERY_ACTIVE
);
6003 talloc_free(tmp_ctx
);
6007 DEBUG(DEBUG_ERR
, ("Database wiped.\n"));
6009 talloc_free(tmp_ctx
);
6016 static int control_dumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6022 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
6023 ret
= ctdb_control(ctdb
, options
.pnn
, 0, CTDB_CONTROL_DUMP_MEMORY
,
6024 0, tdb_null
, tmp_ctx
, &data
, &res
, NULL
, &errmsg
);
6025 if (ret
!= 0 || res
!= 0) {
6026 DEBUG(DEBUG_ERR
,("Failed to dump memory - %s\n", errmsg
));
6027 talloc_free(tmp_ctx
);
6030 write(1, data
.dptr
, data
.dsize
);
6031 talloc_free(tmp_ctx
);
6036 handler for memory dumps
6038 static void mem_dump_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6039 TDB_DATA data
, void *private_data
)
6041 write(1, data
.dptr
, data
.dsize
);
6046 dump memory usage on the recovery daemon
6048 static int control_rddumpmemory(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6052 struct srvid_request rd
;
6054 rd
.pnn
= ctdb_get_pnn(ctdb
);
6055 rd
.srvid
= getpid();
6057 /* register a message port for receiveing the reply so that we
6058 can receive the reply
6060 ctdb_client_set_message_handler(ctdb
, rd
.srvid
, mem_dump_handler
, NULL
);
6063 data
.dptr
= (uint8_t *)&rd
;
6064 data
.dsize
= sizeof(rd
);
6066 ret
= ctdb_client_send_message(ctdb
, options
.pnn
, CTDB_SRVID_MEM_DUMP
, data
);
6068 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6072 /* this loop will terminate when we have received the reply */
6074 event_loop_once(ctdb
->ev
);
6081 send a message to a srvid
6083 static int control_msgsend(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6085 unsigned long srvid
;
6093 srvid
= strtoul(argv
[0], NULL
, 0);
6095 data
.dptr
= (uint8_t *)discard_const(argv
[1]);
6096 data
.dsize
= strlen(argv
[1]);
6098 ret
= ctdb_client_send_message(ctdb
, CTDB_BROADCAST_CONNECTED
, srvid
, data
);
6100 DEBUG(DEBUG_ERR
,("Failed to send memdump request message to %u\n", options
.pnn
));
6108 handler for msglisten
6110 static void msglisten_handler(struct ctdb_context
*ctdb
, uint64_t srvid
,
6111 TDB_DATA data
, void *private_data
)
6115 printf("Message received: ");
6116 for (i
=0;i
<data
.dsize
;i
++) {
6117 printf("%c", data
.dptr
[i
]);
6123 listen for messages on a messageport
6125 static int control_msglisten(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6131 /* register a message port and listen for messages
6133 ctdb_client_set_message_handler(ctdb
, srvid
, msglisten_handler
, NULL
);
6134 printf("Listening for messages on srvid:%d\n", (int)srvid
);
6137 event_loop_once(ctdb
->ev
);
6144 list all nodes in the cluster
6145 we parse the nodes file directly
6147 static int control_listnodes(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6149 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
6150 struct pnn_node
*pnn_nodes
;
6151 struct pnn_node
*pnn_node
;
6153 assert_single_node_only();
6155 pnn_nodes
= read_nodes_file(mem_ctx
);
6156 if (pnn_nodes
== NULL
) {
6157 DEBUG(DEBUG_ERR
,("Failed to read nodes file\n"));
6158 talloc_free(mem_ctx
);
6162 for(pnn_node
=pnn_nodes
;pnn_node
;pnn_node
=pnn_node
->next
) {
6163 ctdb_sock_addr addr
;
6164 if (parse_ip(pnn_node
->addr
, NULL
, 63999, &addr
) == 0) {
6165 DEBUG(DEBUG_ERR
,("Wrongly formed ip address '%s' in nodes file\n", pnn_node
->addr
));
6166 talloc_free(mem_ctx
);
6169 if (options
.machinereadable
){
6170 printf(":%d:%s:\n", pnn_node
->pnn
, pnn_node
->addr
);
6172 printf("%s\n", pnn_node
->addr
);
6175 talloc_free(mem_ctx
);
6181 reload the nodes file on the local node
6183 static int control_reload_nodes_file(struct ctdb_context
*ctdb
, int argc
, const char **argv
)
6187 struct ctdb_node_map
*nodemap
=NULL
;
6189 assert_single_node_only();
6191 mypnn
= ctdb_get_pnn(ctdb
);
6193 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &nodemap
);
6195 DEBUG(DEBUG_ERR
, ("Unable to get nodemap from local node\n"));
6199 /* reload the nodes file on all remote nodes */
6200 for (i
=0;i
<nodemap
->num
;i
++) {
6201 if (nodemap
->nodes
[i
].pnn
== mypnn
) {
6204 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", nodemap
->nodes
[i
].pnn
));
6205 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(),
6206 nodemap
->nodes
[i
].pnn
);
6208 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap
->nodes
[i
].pnn
));
6212 /* reload the nodes file on the local node */
6213 DEBUG(DEBUG_NOTICE
, ("Reloading nodes file on node %u\n", mypnn
));
6214 ret
= ctdb_ctrl_reload_nodes_file(ctdb
, TIMELIMIT(), mypnn
);
6216 DEBUG(DEBUG_ERR
, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn
));
6219 /* initiate a recovery */
6220 control_recover(ctdb
, argc
, argv
);
6226 static const struct {
6228 int (*fn
)(struct ctdb_context
*, int, const char **);
6230 bool without_daemon
; /* can be run without daemon running ? */
6233 } ctdb_commands
[] = {
6234 { "version", control_version
, true, true, "show version of ctdb" },
6235 { "status", control_status
, true, false, "show node status" },
6236 { "uptime", control_uptime
, true, false, "show node uptime" },
6237 { "ping", control_ping
, true, false, "ping all nodes" },
6238 { "runstate", control_runstate
, true, false, "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6239 { "getvar", control_getvar
, true, false, "get a tunable variable", "<name>"},
6240 { "setvar", control_setvar
, true, false, "set a tunable variable", "<name> <value>"},
6241 { "listvars", control_listvars
, true, false, "list tunable variables"},
6242 { "statistics", control_statistics
, false, false, "show statistics" },
6243 { "statisticsreset", control_statistics_reset
, true, false, "reset statistics"},
6244 { "stats", control_stats
, false, false, "show rolling statistics", "[number of history records]" },
6245 { "ip", control_ip
, false, false, "show which public ip's that ctdb manages" },
6246 { "ipinfo", control_ipinfo
, true, false, "show details about a public ip that ctdb manages", "<ip>" },
6247 { "ifaces", control_ifaces
, true, false, "show which interfaces that ctdb manages" },
6248 { "setifacelink", control_setifacelink
, true, false, "set interface link status", "<iface> <status>" },
6249 { "process-exists", control_process_exists
, true, false, "check if a process exists on a node", "<pid>"},
6250 { "getdbmap", control_getdbmap
, true, false, "show the database map" },
6251 { "getdbstatus", control_getdbstatus
, true, false, "show the status of a database", "<dbname|dbid>" },
6252 { "catdb", control_catdb
, true, false, "dump a ctdb database" , "<dbname|dbid>"},
6253 { "cattdb", control_cattdb
, true, false, "dump a local tdb database" , "<dbname|dbid>"},
6254 { "getmonmode", control_getmonmode
, true, false, "show monitoring mode" },
6255 { "getcapabilities", control_getcapabilities
, true, false, "show node capabilities" },
6256 { "pnn", control_pnn
, true, false, "show the pnn of the currnet node" },
6257 { "lvs", control_lvs
, true, false, "show lvs configuration" },
6258 { "lvsmaster", control_lvsmaster
, true, false, "show which node is the lvs master" },
6259 { "disablemonitor", control_disable_monmode
,true, false, "set monitoring mode to DISABLE" },
6260 { "enablemonitor", control_enable_monmode
, true, false, "set monitoring mode to ACTIVE" },
6261 { "setdebug", control_setdebug
, true, false, "set debug level", "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6262 { "getdebug", control_getdebug
, true, false, "get debug level" },
6263 { "getlog", control_getlog
, true, false, "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6264 { "clearlog", control_clearlog
, true, false, "clear the log data from the in memory ringbuffer", "[recoverd]" },
6265 { "attach", control_attach
, true, false, "attach to a database", "<dbname> [persistent]" },
6266 { "dumpmemory", control_dumpmemory
, true, false, "dump memory map to stdout" },
6267 { "rddumpmemory", control_rddumpmemory
, true, false, "dump memory map from the recovery daemon to stdout" },
6268 { "getpid", control_getpid
, true, false, "get ctdbd process ID" },
6269 { "disable", control_disable
, true, false, "disable a nodes public IP" },
6270 { "enable", control_enable
, true, false, "enable a nodes public IP" },
6271 { "stop", control_stop
, true, false, "stop a node" },
6272 { "continue", control_continue
, true, false, "re-start a stopped node" },
6273 { "ban", control_ban
, true, false, "ban a node from the cluster", "<bantime>"},
6274 { "unban", control_unban
, true, false, "unban a node" },
6275 { "showban", control_showban
, true, false, "show ban information"},
6276 { "shutdown", control_shutdown
, true, false, "shutdown ctdbd" },
6277 { "recover", control_recover
, true, false, "force recovery" },
6278 { "sync", control_ipreallocate
, false, false, "wait until ctdbd has synced all state changes" },
6279 { "ipreallocate", control_ipreallocate
, false, false, "force the recovery daemon to perform a ip reallocation procedure" },
6280 { "thaw", control_thaw
, true, false, "thaw databases", "[priority:1-3]" },
6281 { "isnotrecmaster", control_isnotrecmaster
, false, false, "check if the local node is recmaster or not" },
6282 { "killtcp", kill_tcp
, false, false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6283 { "gratiousarp", control_gratious_arp
, false, false, "send a gratious arp", "<ip> <interface>" },
6284 { "tickle", tickle_tcp
, false, false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6285 { "gettickles", control_get_tickles
, false, false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6286 { "addtickle", control_add_tickle
, false, false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6288 { "deltickle", control_del_tickle
, false, false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6290 { "regsrvid", regsrvid
, false, false, "register a server id", "<pnn> <type> <id>" },
6291 { "unregsrvid", unregsrvid
, false, false, "unregister a server id", "<pnn> <type> <id>" },
6292 { "chksrvid", chksrvid
, false, false, "check if a server id exists", "<pnn> <type> <id>" },
6293 { "getsrvids", getsrvids
, false, false, "get a list of all server ids"},
6294 { "check_srvids", check_srvids
, false, false, "check if a srvid exists", "<id>+" },
6295 { "repack", ctdb_repack
, false, false, "repack all databases", "[max_freelist]"},
6296 { "listnodes", control_listnodes
, false, true, "list all nodes in the cluster"},
6297 { "reloadnodes", control_reload_nodes_file
, false, false, "reload the nodes file and restart the transport on all nodes"},
6298 { "moveip", control_moveip
, false, false, "move/failover an ip address to another node", "<ip> <node>"},
6299 { "rebalanceip", control_rebalanceip
, false, false, "release an ip from the node and let recd rebalance it", "<ip>"},
6300 { "addip", control_addip
, true, false, "add a ip address to a node", "<ip/mask> <iface>"},
6301 { "delip", control_delip
, false, false, "delete an ip address from a node", "<ip>"},
6302 { "eventscript", control_eventscript
, true, false, "run the eventscript with the given parameters on a node", "<arguments>"},
6303 { "backupdb", control_backupdb
, false, false, "backup the database into a file.", "<dbname|dbid> <file>"},
6304 { "restoredb", control_restoredb
, false, false, "restore the database from a file.", "<file> [dbname]"},
6305 { "dumpdbbackup", control_dumpdbbackup
, false, true, "dump database backup from a file.", "<file>"},
6306 { "wipedb", control_wipedb
, false, false, "wipe the contents of a database.", "<dbname|dbid>"},
6307 { "recmaster", control_recmaster
, true, false, "show the pnn for the recovery master."},
6308 { "scriptstatus", control_scriptstatus
, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6309 { "enablescript", control_enablescript
, true, false, "enable an eventscript", "<script>"},
6310 { "disablescript", control_disablescript
, true, false, "disable an eventscript", "<script>"},
6311 { "natgwlist", control_natgwlist
, true, false, "show the nodes belonging to this natgw configuration"},
6312 { "xpnn", control_xpnn
, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
6313 { "getreclock", control_getreclock
, true, false, "Show the reclock file of a node"},
6314 { "setreclock", control_setreclock
, true, false, "Set/clear the reclock file of a node", "[filename]"},
6315 { "setnatgwstate", control_setnatgwstate
, false, false, "Set NATGW state to on/off", "{on|off}"},
6316 { "setlmasterrole", control_setlmasterrole
, false, false, "Set LMASTER role to on/off", "{on|off}"},
6317 { "setrecmasterrole", control_setrecmasterrole
, false, false, "Set RECMASTER role to on/off", "{on|off}"},
6318 { "setdbprio", control_setdbprio
, false, false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6319 { "getdbprio", control_getdbprio
, false, false, "Get DB priority", "<dbname|dbid>"},
6320 { "setdbreadonly", control_setdbreadonly
, false, false, "Set DB readonly capable", "<dbname|dbid>"},
6321 { "setdbsticky", control_setdbsticky
, false, false, "Set DB sticky-records capable", "<dbname|dbid>"},
6322 { "msglisten", control_msglisten
, false, false, "Listen on a srvid port for messages", "<msg srvid>"},
6323 { "msgsend", control_msgsend
, false, false, "Send a message to srvid", "<srvid> <message>"},
6324 { "pfetch", control_pfetch
, false, false, "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6325 { "pstore", control_pstore
, false, false, "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6326 { "pdelete", control_pdelete
, false, false, "delete a record from a persistent database", "<dbname|dbid> <key>" },
6327 { "ptrans", control_ptrans
, false, false, "update a persistent database (from stdin)", "<dbname|dbid>" },
6328 { "tfetch", control_tfetch
, false, true, "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6329 { "tstore", control_tstore
, false, true, "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
6330 { "readkey", control_readkey
, true, false, "read the content off a database key", "<dbname|dbid> <key>" },
6331 { "writekey", control_writekey
, true, false, "write to a database key", "<dbname|dbid> <key> <value>" },
6332 { "checktcpport", control_chktcpport
, false, true, "check if a service is bound to a specific tcp port or not", "<port>" },
6333 { "rebalancenode", control_rebalancenode
, false, false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6334 { "getdbseqnum", control_getdbseqnum
, false, false, "get the sequence number off a database", "<dbname|dbid>" },
6335 { "nodestatus", control_nodestatus
, true, false, "show and return node status", "[<pnn-list>]" },
6336 { "dbstatistics", control_dbstatistics
, false, false, "show db statistics", "<dbname|dbid>" },
6337 { "reloadips", control_reloadips
, false, false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6338 { "ipiface", control_ipiface
, false, true, "Find which interface an ip address is hosted on", "<ip>" },
6344 static void usage(void)
6348 "Usage: ctdb [options] <control>\n" \
6350 " -n <node> choose node number, or 'all' (defaults to local node)\n"
6351 " -Y generate machinereadable output\n"
6352 " -v generate verbose output\n"
6353 " -t <timelimit> set timelimit for control in seconds (default %u)\n", options
.timelimit
);
6354 printf("Controls:\n");
6355 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6356 printf(" %-15s %-27s %s\n",
6357 ctdb_commands
[i
].name
,
6358 ctdb_commands
[i
].args
?ctdb_commands
[i
].args
:"",
6359 ctdb_commands
[i
].msg
);
6365 static void ctdb_alarm(int sig
)
6367 printf("Maximum runtime exceeded - exiting\n");
6374 int main(int argc
, const char *argv
[])
6376 struct ctdb_context
*ctdb
;
6377 char *nodestring
= NULL
;
6378 struct poptOption popt_options
[] = {
6381 { "timelimit", 't', POPT_ARG_INT
, &options
.timelimit
, 0, "timelimit", "integer" },
6382 { "node", 'n', POPT_ARG_STRING
, &nodestring
, 0, "node", "integer|all" },
6383 { "machinereadable", 'Y', POPT_ARG_NONE
, &options
.machinereadable
, 0, "enable machinereadable output", NULL
},
6384 { "verbose", 'v', POPT_ARG_NONE
, &options
.verbose
, 0, "enable verbose output", NULL
},
6385 { "maxruntime", 'T', POPT_ARG_INT
, &options
.maxruntime
, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6386 { "print-emptyrecords", 0, POPT_ARG_NONE
, &options
.printemptyrecords
, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL
},
6387 { "print-datasize", 0, POPT_ARG_NONE
, &options
.printdatasize
, 0, "do not print record data when dumping databases, only the data size", NULL
},
6388 { "print-lmaster", 0, POPT_ARG_NONE
, &options
.printlmaster
, 0, "print the record's lmaster in catdb", NULL
},
6389 { "print-hash", 0, POPT_ARG_NONE
, &options
.printhash
, 0, "print the record's hash when dumping databases", NULL
},
6390 { "print-recordflags", 0, POPT_ARG_NONE
, &options
.printrecordflags
, 0, "print the record flags in catdb and dumpdbbackup", NULL
},
6394 const char **extra_argv
;
6398 struct event_context
*ev
;
6399 const char *control
;
6403 /* set some defaults */
6404 options
.maxruntime
= 0;
6405 options
.timelimit
= 10;
6406 options
.pnn
= CTDB_CURRENT_NODE
;
6408 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
6410 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6413 DEBUG(DEBUG_ERR
, ("Invalid option %s: %s\n",
6414 poptBadOption(pc
, 0), poptStrerror(opt
)));
6419 /* setup the remaining options for the main program to use */
6420 extra_argv
= poptGetArgs(pc
);
6423 while (extra_argv
[extra_argc
]) extra_argc
++;
6426 if (extra_argc
< 1) {
6430 if (options
.maxruntime
== 0) {
6431 const char *ctdb_timeout
;
6432 ctdb_timeout
= getenv("CTDB_TIMEOUT");
6433 if (ctdb_timeout
!= NULL
) {
6434 options
.maxruntime
= strtoul(ctdb_timeout
, NULL
, 0);
6436 /* default timeout is 120 seconds */
6437 options
.maxruntime
= 120;
6441 signal(SIGALRM
, ctdb_alarm
);
6442 alarm(options
.maxruntime
);
6444 control
= extra_argv
[0];
6446 /* Default value for CTDB_BASE - don't override */
6447 setenv("CTDB_BASE", ETCDIR
"/ctdb", 0);
6449 ev
= event_context_init(NULL
);
6451 DEBUG(DEBUG_ERR
, ("Failed to initialize event system\n"));
6455 for (i
=0;i
<ARRAY_SIZE(ctdb_commands
);i
++) {
6456 if (strcmp(control
, ctdb_commands
[i
].name
) == 0) {
6461 if (i
== ARRAY_SIZE(ctdb_commands
)) {
6462 DEBUG(DEBUG_ERR
, ("Unknown control '%s'\n", control
));
6466 if (ctdb_commands
[i
].without_daemon
== true) {
6467 if (nodestring
!= NULL
) {
6468 DEBUG(DEBUG_ERR
, ("Can't specify node(s) with \"ctdb %s\"\n", control
));
6471 return ctdb_commands
[i
].fn(NULL
, extra_argc
-1, extra_argv
+1);
6474 /* initialise ctdb */
6475 ctdb
= ctdb_cmdline_client(ev
, TIMELIMIT());
6478 DEBUG(DEBUG_ERR
, ("Failed to init ctdb\n"));
6482 /* setup the node number(s) to contact */
6483 if (!parse_nodestring(ctdb
, ctdb
, nodestring
, CTDB_CURRENT_NODE
, false,
6484 &options
.nodes
, &options
.pnn
)) {
6488 if (options
.pnn
== CTDB_CURRENT_NODE
) {
6489 options
.pnn
= options
.nodes
[0];
6492 if (ctdb_commands
[i
].auto_all
&&
6493 ((options
.pnn
== CTDB_BROADCAST_ALL
) ||
6494 (options
.pnn
== CTDB_MULTICAST
))) {
6498 for (j
= 0; j
< talloc_array_length(options
.nodes
); j
++) {
6499 options
.pnn
= options
.nodes
[j
];
6500 ret
|= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6503 ret
= ctdb_commands
[i
].fn(ctdb
, extra_argc
-1, extra_argv
+1);
6508 (void)poptFreeContext(pc
);